Skip to content

Instantly share code, notes, and snippets.

@bb-vh-suraj
Last active October 12, 2022 20:59
Show Gist options
  • Save bb-vh-suraj/6eff22df833fe70238f10e41724f29a3 to your computer and use it in GitHub Desktop.
Save bb-vh-suraj/6eff22df833fe70238f10e41724f29a3 to your computer and use it in GitHub Desktop.
This is a demo file to show an issue in or-tools I am facing.
import com.google.ortools.Loader;
import com.google.ortools.constraintsolver.Assignment;
import com.google.ortools.constraintsolver.FirstSolutionStrategy;
import com.google.ortools.constraintsolver.RoutingDimension;
import com.google.ortools.constraintsolver.RoutingIndexManager;
import com.google.ortools.constraintsolver.RoutingModel;
import com.google.ortools.constraintsolver.RoutingSearchParameters;
import com.google.ortools.constraintsolver.main;
public class Testortools {
public static void main(String[] args) {
Loader.loadNativeLibraries();
int deliveryCount = 5;
int vehicleCount = 1;
/* index zero is our start warehouse, index one is end warehouse */
/* deliveries start from index 2*/
long[] deliveryWeights = new long[] { 0, 0, 50, 30, 30 };
long[] penalties = new long[] { 1, 1, 100, 5, 5 };
long[] vehicleWeights = new long[] { 75 };
long[][] durations = new long[][] { { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 } };
int[] starts = new int[] { 0 };
int[] ends = new int[] { 1 };
RoutingIndexManager manager = new RoutingIndexManager(deliveryCount, vehicleCount, starts, ends);
RoutingModel routing = new RoutingModel(manager);
/* Time Dimension */
int timeCallback = routing.registerTransitCallback((fromIndex, toIndex) -> {
int from = manager.indexToNode(fromIndex);
int to = manager.indexToNode(toIndex);
return durations[from][to];
});
routing.setArcCostEvaluatorOfAllVehicles(timeCallback);
routing.addDimension(timeCallback, Long.MAX_VALUE, Long.MAX_VALUE, false,
"Time");
/* Weight dimension */
int weightCallback = routing.registerUnaryTransitCallback(index -> {
int node = manager.indexToNode(index);
return deliveryWeights[node];
});
for (int i = 0; i < vehicleCount; i++)
routing.addDimensionWithVehicleCapacity(weightCallback, 0, vehicleWeights, true, "Count");
/* Dropping Constraints */
for (int i = 2; i < deliveryCount; i++) {
long penalty = penalties[i];
routing.addDisjunction(new long[] { manager.nodeToIndex(i) }, penalty * 1000);
}
/* Search parameters & Solve */
RoutingSearchParameters searchParameters = main.defaultRoutingSearchParameters().toBuilder()
.setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
.build();
routing.closeModelWithParameters(searchParameters);
Assignment solution = routing.solveWithParameters(searchParameters);
/* No solution? */
if (solution == null) {
System.out.println("No solution");
return;
}
System.out.println("Objective value " + solution.objectiveValue());
/* See dropped nodes */
RoutingDimension count = routing.getMutableDimension("Count");
for (int i = 0; i < routing.size(); i++) {
if (routing.isStart(i) || routing.isEnd(i)) {
continue;
}
if (solution.value(routing.nextVar(i)) == i)
System.out.println("Dropped node " + manager.indexToNode(i));
}
/* See routes */
for (int i = 0; i < vehicleCount; i++) {
System.out.println("Vehicle " + i);
long index = routing.start(i);
while (index != routing.end(i)) {
System.out.println(
"Index " + manager.indexToNode(index) + ", weight "
+ solution.value(count.cumulVar(index)));
index = solution.value(routing.nextVar(index));
}
}
}
}
@bb-vh-suraj
Copy link
Author

bb-vh-suraj commented Oct 11, 2022

After running, logs are:

Objective value 100000
Dropped order 2
Vehicle 0
Index 0, weight 0
Index 4, weight 0
Index 3, weight 30

Here, index 0 is start warehouse, 1 is end warehouse.
Weights are {0,0,50,30,30}
Penalties {1,1,100,5,5}
Index 0 1 (2 3 4)

@abeham
Copy link

abeham commented Oct 12, 2022

The for loop in line 48 would lead to an error if vehicleCount was more than 1.

@bb-vh-suraj
Copy link
Author

Thanks @abeham , that's actually hard coded, but the issue is with solver probably. Do you see any other issues with code?

@abeham
Copy link

abeham commented Oct 12, 2022

No, not really.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment