Skip to content

Commit 766194b

Browse files
committed
Move more logic out of Main and into GraphUtils
1 parent f9e5b27 commit 766194b

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
package io.github.plastix;
22

33
import com.graphhopper.routing.util.DefaultEdgeFilter;
4+
import com.graphhopper.routing.util.EncodingManager;
45
import com.graphhopper.routing.util.FlagEncoder;
56
import com.graphhopper.storage.Graph;
7+
import com.graphhopper.storage.index.LocationIndex;
8+
import com.graphhopper.storage.index.QueryResult;
69
import com.graphhopper.util.EdgeExplorer;
710
import com.graphhopper.util.EdgeIterator;
11+
import com.graphhopper.util.EdgeIteratorState;
812

913
public class GraphUtils {
1014

15+
private Params params;
1116
private Graph graph;
17+
private LocationIndex locationIndex;
1218
private FlagEncoder flagEncoder;
19+
private BikePriorityWeighting weighting;
1320

14-
GraphUtils(Graph graph, FlagEncoder flagEncoder) {
21+
GraphUtils(Graph graph, LocationIndex locationIndex, EncodingManager encodingManager, Params params) {
1522
this.graph = graph;
16-
this.flagEncoder = flagEncoder;
23+
this.locationIndex = locationIndex;
24+
this.flagEncoder = encodingManager.getEncoder(params.getVehicle());
25+
this.params = params;
26+
weighting = new BikePriorityWeighting(flagEncoder);
1727
}
1828

1929
public EdgeIterator outgoingEdges(int node) {
@@ -36,4 +46,25 @@ public EdgeExplorer getEdgeExplorer() {
3646
return graph.createEdgeExplorer(new DefaultEdgeFilter(flagEncoder));
3747
}
3848

49+
public double getArcScore(EdgeIteratorState edge) {
50+
return weighting.calcWeight(edge, false, edge.getEdge());
51+
}
52+
53+
public int getStartNode() {
54+
QueryResult result = locationIndex.findClosest(params.getStartLat(), params.getStartLon(),
55+
new DefaultEdgeFilter(flagEncoder));
56+
if(!result.isValid()) {
57+
throw new RuntimeException("Unable to find node at start lat/lon!");
58+
}
59+
return result.getClosestNode();
60+
61+
}
62+
63+
public boolean isForward(EdgeIteratorState edge) {
64+
return edge.isForward(flagEncoder);
65+
}
66+
67+
public boolean isBackward(EdgeIteratorState edge) {
68+
return edge.isBackward(flagEncoder);
69+
}
3970
}

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

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,8 @@
44
import com.graphhopper.GraphHopper;
55
import com.graphhopper.reader.osm.GraphHopperOSM;
66
import com.graphhopper.routing.util.AllEdgesIterator;
7-
import com.graphhopper.routing.util.DefaultEdgeFilter;
87
import com.graphhopper.routing.util.EncodingManager;
9-
import com.graphhopper.routing.util.FlagEncoder;
10-
import com.graphhopper.routing.weighting.Weighting;
118
import com.graphhopper.storage.Graph;
12-
import com.graphhopper.storage.index.QueryResult;
139
import com.graphhopper.util.EdgeIterator;
1410
import gurobi.*;
1511

@@ -22,8 +18,6 @@ public class Main {
2218
private static GraphHopper hopper;
2319
private static Graph graph;
2420
private static GraphUtils graphUtils;
25-
private static Weighting score;
26-
private static FlagEncoder flagEncoder;
2721

2822
// Solver variables
2923
private static GRBEnv env;
@@ -32,7 +26,7 @@ public class Main {
3226
private static void setupSolver() throws GRBException {
3327
env = new GRBEnv("osm.log");
3428
model = new GRBModel(env);
35-
Vars vars = new Vars(graph, model, flagEncoder);
29+
Vars vars = new Vars(graph, model, graphUtils);
3630

3731
// (1a)
3832
// Objective maximizes total collected score of all roads
@@ -44,7 +38,7 @@ private static void setupSolver() throws GRBException {
4438

4539
AllEdgesIterator edges = graph.getAllEdges();
4640
while(edges.next()) {
47-
double edgeScore = score.calcWeight(edges, false, edges.getEdge());
41+
double edgeScore = graphUtils.getArcScore(edges);
4842
double edgeDist = edges.getDistance();
4943

5044
GRBVar forward = vars.getArc(edges);
@@ -136,18 +130,8 @@ private static void loadOSM() {
136130
hopper.importOrLoad();
137131

138132
graph = hopper.getGraphHopperStorage().getBaseGraph();
139-
flagEncoder = encodingManager.getEncoder(params.getVehicle());
140-
score = new BikePriorityWeighting(flagEncoder);
141-
graphUtils = new GraphUtils(graph, flagEncoder);
142-
143-
QueryResult result = hopper.getLocationIndex().findClosest(params.getStartLat(), params.getStartLon(),
144-
new DefaultEdgeFilter(flagEncoder));
145-
if(!result.isValid()) {
146-
System.out.println("Unable to find starting node near lat/lon points!");
147-
System.exit(1);
148-
}
149-
150-
START_NODE_ID = result.getClosestNode();
133+
graphUtils = new GraphUtils(graph, hopper.getLocationIndex(), encodingManager, params);
134+
START_NODE_ID = graphUtils.getStartNode();
151135

152136
AllEdgesIterator edges = graph.getAllEdges();
153137
int nonTraversable = 0;

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.github.plastix;
22

33
import com.graphhopper.routing.util.AllEdgesIterator;
4-
import com.graphhopper.routing.util.FlagEncoder;
54
import com.graphhopper.storage.Graph;
65
import com.graphhopper.util.EdgeIterator;
76
import gurobi.GRB;
@@ -14,7 +13,7 @@
1413
public class Vars {
1514

1615
private Graph graph;
17-
private FlagEncoder flagEncoder;
16+
private GraphUtils graphUtils;
1817
private GRBModel model;
1918

2019
// arcs[arcId][0] = forwardArc
@@ -23,10 +22,10 @@ public class Vars {
2322
private GRBVar[] verts;
2423
private int[] arcBaseIds;
2524

26-
Vars(Graph graph, GRBModel model, FlagEncoder flagEncoder) throws GRBException {
25+
Vars(Graph graph, GRBModel model, GraphUtils graphUtils) throws GRBException {
2726
this.graph = graph;
2827
this.model = model;
29-
this.flagEncoder = flagEncoder;
28+
this.graphUtils = graphUtils;
3029
addVarsToModel();
3130
}
3231

@@ -60,11 +59,11 @@ private void addVarsToModel() throws GRBException {
6059
arcs[edgeId][0] = forward;
6160
arcs[edgeId][1] = backward;
6261

63-
if(!edges.isForward(flagEncoder)) {
62+
if(!graphUtils.isForward(edges)) {
6463
forward.set(GRB.DoubleAttr.UB, 0);
6564
}
6665

67-
if(!edges.isBackward(flagEncoder)) {
66+
if(!graphUtils.isBackward(edges)) {
6867
backward.set(GRB.DoubleAttr.UB, 0);
6968
}
7069
}

0 commit comments

Comments
 (0)