Skip to content

Commit 40a3b30

Browse files
committed
Move param loading to seperate file
1 parent d6e8ca8 commit 40a3b30

File tree

2 files changed

+74
-42
lines changed

2 files changed

+74
-42
lines changed

src/main/java/io/github/plastix/Main.java

Lines changed: 12 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,12 @@
1313
import com.graphhopper.util.EdgeIterator;
1414
import gurobi.*;
1515

16-
import java.io.FileInputStream;
17-
import java.io.FileNotFoundException;
18-
import java.io.IOException;
19-
import java.io.InputStream;
2016
import java.util.Arrays;
21-
import java.util.Properties;
2217

2318
public class Main {
2419

25-
private static String GRAPH_FILE;
26-
private static String GRAPH_FOLDER;
27-
private static double MAX_COST;
28-
private static double START_LAT;
29-
private static double START_LON;
20+
private static Params params;
3021
private static int START_NODE_ID;
31-
private static String VEHICLE;
3222

3323
// Graph variables
3424
private static GraphHopper hopper;
@@ -103,7 +93,7 @@ private static void setupSolver() throws GRBException {
10393
}
10494

10595
model.setObjective(objective, GRB.MAXIMIZE);
106-
model.addConstr(maxCost, GRB.LESS_EQUAL, MAX_COST, "max_cost");
96+
model.addConstr(maxCost, GRB.LESS_EQUAL, params.getMaxCost(), "max_cost");
10797

10898
for(int i = 0; i < numNodes; i++) {
10999
// (1d)
@@ -156,8 +146,9 @@ private static GRBVar getArc(EdgeIterator edge) {
156146
}
157147

158148
private static void runSolver() throws GRBException {
159-
System.out.println("Max cost: " + MAX_COST);
160-
System.out.println("Start position: " + START_LAT + ", " + START_LON + " (Node " + START_NODE_ID + ")");
149+
System.out.println("Max cost: " + params.getMaxCost());
150+
System.out.println("Start position: " + params.getStartLat() + ", " + params.getStartLon() +
151+
" (Node " + START_NODE_ID + ")");
161152

162153
model.optimize();
163154

@@ -169,26 +160,25 @@ private static void runSolver() throws GRBException {
169160
private static void loadOSM() {
170161
System.out.println("---- Starting GraphHopper ----");
171162
hopper = new GraphHopperOSM();
172-
hopper.setDataReaderFile(GRAPH_FILE);
173-
hopper.setGraphHopperLocation(GRAPH_FOLDER);
163+
hopper.setDataReaderFile(params.getGraphFile());
164+
hopper.setGraphHopperLocation(params.getGraphFolder());
174165
hopper.forDesktop();
175166

176-
EncodingManager encodingManager = new EncodingManager(VEHICLE);
167+
EncodingManager encodingManager = new EncodingManager(params.getVehicle());
177168
hopper.setEncodingManager(encodingManager);
178169
hopper.setCHEnabled(false);
179170
hopper.importOrLoad();
180171

181172
graph = hopper.getGraphHopperStorage().getBaseGraph();
182-
flagEncoder = encodingManager.getEncoder(VEHICLE);
173+
flagEncoder = encodingManager.getEncoder(params.getVehicle());
183174
score = new BikePriorityWeighting(flagEncoder);
184175
graphUtils = new GraphUtils(graph, flagEncoder);
185176

186-
QueryResult result = hopper.getLocationIndex().findClosest(START_LAT, START_LON,
177+
QueryResult result = hopper.getLocationIndex().findClosest(params.getStartLat(), params.getStartLon(),
187178
new DefaultEdgeFilter(flagEncoder));
188179
if(!result.isValid()) {
189180
System.out.println("Unable to find starting node near lat/lon points!");
190181
System.exit(1);
191-
192182
}
193183

194184
START_NODE_ID = result.getClosestNode();
@@ -206,29 +196,9 @@ private static void loadOSM() {
206196
graph.getAllEdges().getMaxId(), graph.getNodes(), nonTraversable, oneWay));
207197
}
208198

209-
private static void loadParams() {
210-
Properties properties = new Properties();
211-
InputStream inputStream = null;
212-
try {
213-
inputStream = new FileInputStream("src/main/resources/params.properties");
214-
properties.load(inputStream);
215-
} catch(FileNotFoundException e) {
216-
System.out.println("No params file!");
217-
e.printStackTrace();
218-
} catch(IOException e) {
219-
System.out.println("Error reading params!");
220-
e.printStackTrace();
221-
}
222-
GRAPH_FILE = properties.getProperty("graphFile");
223-
GRAPH_FOLDER = properties.getProperty("graphFolder");
224-
START_LAT = Double.parseDouble(properties.getProperty("startLat"));
225-
START_LON = Double.parseDouble(properties.getProperty("startLon"));
226-
MAX_COST = Double.parseDouble(properties.getProperty("maxCost"));
227-
VEHICLE = properties.getProperty("vehicle");
228-
}
229-
230199
public static void main(String[] args) {
231-
loadParams();
200+
params = new Params();
201+
params.loadParams();
232202

233203
// Parse & Load Open Street Map data
234204
loadOSM();
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package io.github.plastix;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileNotFoundException;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.util.Properties;
8+
9+
public class Params {
10+
11+
private String GRAPH_FILE;
12+
private String GRAPH_FOLDER;
13+
private double MAX_COST;
14+
private double START_LAT;
15+
private double START_LON;
16+
private String VEHICLE;
17+
18+
public void loadParams() {
19+
Properties properties = new Properties();
20+
InputStream inputStream;
21+
try {
22+
inputStream = new FileInputStream("src/main/resources/params.properties");
23+
properties.load(inputStream);
24+
} catch(FileNotFoundException e) {
25+
System.out.println("No params file!");
26+
e.printStackTrace();
27+
} catch(IOException e) {
28+
System.out.println("Error reading params!");
29+
e.printStackTrace();
30+
}
31+
GRAPH_FILE = properties.getProperty("graphFile");
32+
GRAPH_FOLDER = properties.getProperty("graphFolder");
33+
START_LAT = Double.parseDouble(properties.getProperty("startLat"));
34+
START_LON = Double.parseDouble(properties.getProperty("startLon"));
35+
MAX_COST = Double.parseDouble(properties.getProperty("maxCost"));
36+
VEHICLE = properties.getProperty("vehicle");
37+
}
38+
39+
public String getGraphFile() {
40+
return GRAPH_FILE;
41+
}
42+
43+
public String getGraphFolder() {
44+
return GRAPH_FOLDER;
45+
}
46+
47+
public double getMaxCost() {
48+
return MAX_COST;
49+
}
50+
51+
public double getStartLat() {
52+
return START_LAT;
53+
}
54+
55+
public double getStartLon() {
56+
return START_LON;
57+
}
58+
59+
public String getVehicle() {
60+
return VEHICLE;
61+
}
62+
}

0 commit comments

Comments
 (0)