Skip to content

Commit 9440475

Browse files
committed
Read optimization parameters from properties file
1 parent 803b7cc commit 9440475

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

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

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,25 @@
1111
import com.graphhopper.storage.Graph;
1212
import com.graphhopper.storage.index.QueryResult;
1313
import com.graphhopper.util.EdgeIterator;
14-
import com.graphhopper.util.shapes.GHPoint;
1514
import gurobi.*;
1615

16+
import java.io.FileInputStream;
17+
import java.io.FileNotFoundException;
18+
import java.io.IOException;
19+
import java.io.InputStream;
1720
import java.util.Arrays;
21+
import java.util.Properties;
1822

1923
public class Main {
2024

21-
// TODO (Aidan) Read these params from file
22-
private static final int MAX_COST = 40_000; // In meters
23-
private static final int MIN_COST = 20_000; // In meters
24-
private static final GHPoint START_POSITION = new GHPoint(43.009449, -74.006824);
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;
2530
private static int START_NODE_ID;
26-
private static final String RACE_BIKE_VEHICLE = "racingbike";
27-
private static final String ENABLED_VEHICLES = RACE_BIKE_VEHICLE;
31+
private static String VEHICLE;
32+
private static String ENABLED_VEHICLES;
2833

2934
// Graph variables
3035
private static Graph graph;
@@ -151,16 +156,16 @@ private static GRBVar getArc(EdgeIterator edge) {
151156

152157
private static void runSolver() throws GRBException {
153158
System.out.println("Max cost: " + MAX_COST);
154-
System.out.println("Start position: " + START_POSITION + " (Node " + START_NODE_ID + ")");
159+
System.out.println("Start position: " + START_LAT + ", " + START_LON + " (Node " + START_NODE_ID + ")");
155160

156161
model.optimize();
157162
}
158163

159164
private static void loadOSM() {
160165
System.out.println("---- Starting GraphHopper ----");
161166
GraphHopper hopper = new GraphHopperOSM();
162-
hopper.setDataReaderFile("src/main/resources/ny_capital_district.pbf");
163-
hopper.setGraphHopperLocation("src/main/resources/ny_capital_district-gh/");
167+
hopper.setDataReaderFile(GRAPH_FILE);
168+
hopper.setGraphHopperLocation(GRAPH_FOLDER);
164169
hopper.forDesktop();
165170

166171
EncodingManager encodingManager = new EncodingManager(ENABLED_VEHICLES);
@@ -169,11 +174,11 @@ private static void loadOSM() {
169174
hopper.importOrLoad();
170175

171176
graph = hopper.getGraphHopperStorage().getBaseGraph();
172-
flagEncoder = encodingManager.getEncoder(RACE_BIKE_VEHICLE);
177+
flagEncoder = encodingManager.getEncoder(VEHICLE);
173178
score = new BikePriorityWeighting(flagEncoder);
174179
graphUtils = new GraphUtils(graph, flagEncoder);
175180

176-
QueryResult result = hopper.getLocationIndex().findClosest(START_POSITION.lat, START_POSITION.lon,
181+
QueryResult result = hopper.getLocationIndex().findClosest(START_LAT, START_LON,
177182
new DefaultEdgeFilter(flagEncoder));
178183
if(!result.isValid()) {
179184
System.out.println("Unable to find starting node near lat/lon points!");
@@ -196,7 +201,32 @@ private static void loadOSM() {
196201
graph.getAllEdges().getMaxId(), graph.getNodes(), nonTraversable, oneWay));
197202
}
198203

204+
private static void loadParams() {
205+
Properties properties = new Properties();
206+
InputStream inputStream = null;
207+
try {
208+
inputStream = new FileInputStream("src/main/resources/params.properties");
209+
properties.load(inputStream);
210+
} catch(FileNotFoundException e) {
211+
System.out.println("No params file!");
212+
e.printStackTrace();
213+
} catch(IOException e) {
214+
System.out.println("Error reading params!");
215+
e.printStackTrace();
216+
}
217+
GRAPH_FILE = properties.getProperty("graphFile");
218+
GRAPH_FOLDER = properties.getProperty("graphFolder");
219+
START_LAT = Double.parseDouble(properties.getProperty("startLat"));
220+
START_LON = Double.parseDouble(properties.getProperty("startLon"));
221+
MAX_COST = Double.parseDouble(properties.getProperty("maxCost"));
222+
VEHICLE = properties.getProperty("vehicle");
223+
ENABLED_VEHICLES = VEHICLE;
224+
}
225+
226+
199227
public static void main(String[] args) {
228+
loadParams();
229+
200230
// Parse & Load Open Street Map data
201231
loadOSM();
202232

@@ -210,5 +240,4 @@ public static void main(String[] args) {
210240
e.getMessage());
211241
}
212242
}
213-
214243
}

src/main/resources/params.properties

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
graphFile=src/main/resources/ny_capital_district.pbf
2+
graphFolder=src/main/resources/ny_capital_district-gh/
3+
vehicle=racingbike
4+
maxCost=40000
5+
startLat=43.009449
6+
startLon=-74.006824

0 commit comments

Comments
 (0)