mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
Compare commits
4 commits
0df29956fc
...
47566d750c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
47566d750c | ||
|
|
6691cf5ab1 | ||
|
|
579964fb72 | ||
|
|
a2d67c857e |
4 changed files with 49 additions and 74 deletions
|
|
@ -51,7 +51,7 @@ public final class InsertionInitialSolutionFactory implements InitialSolutionFac
|
|||
@Override
|
||||
public VehicleRoutingProblemSolution createSolution(final VehicleRoutingProblem vrp) {
|
||||
logger.info("create initial solution");
|
||||
List<VehicleRoute> vehicleRoutes = new ArrayList<VehicleRoute>();
|
||||
List<VehicleRoute> vehicleRoutes = new ArrayList<>();
|
||||
vehicleRoutes.addAll(vrp.getInitialVehicleRoutes());
|
||||
Collection<Job> badJobs = insertion.insertJobs(vehicleRoutes, getUnassignedJobs(vrp));
|
||||
VehicleRoutingProblemSolution solution = new VehicleRoutingProblemSolution(vehicleRoutes, badJobs, Double.MAX_VALUE);
|
||||
|
|
@ -61,11 +61,7 @@ public final class InsertionInitialSolutionFactory implements InitialSolutionFac
|
|||
}
|
||||
|
||||
private List<Job> getUnassignedJobs(VehicleRoutingProblem vrp) {
|
||||
ArrayList<Job> jobs = new ArrayList<Job>(vrp.getJobs().values());
|
||||
// for (Vehicle v : vrp.getVehicles()) {
|
||||
// if (v.getBreak() != null) jobs.add(v.getBreak());
|
||||
// }
|
||||
return jobs;
|
||||
return new ArrayList<>(vrp.getJobs().values());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,19 +23,13 @@ import com.graphhopper.jsprit.core.algorithm.acceptor.SolutionAcceptor;
|
|||
import com.graphhopper.jsprit.core.algorithm.listener.AlgorithmStartsListener;
|
||||
import com.graphhopper.jsprit.core.algorithm.recreate.InsertionStrategy;
|
||||
import com.graphhopper.jsprit.core.algorithm.recreate.VehicleSwitched;
|
||||
import com.graphhopper.jsprit.core.algorithm.state.*;
|
||||
import com.graphhopper.jsprit.core.algorithm.state.StateManager;
|
||||
import com.graphhopper.jsprit.core.problem.VehicleRoutingProblem;
|
||||
import com.graphhopper.jsprit.core.problem.constraint.ConstraintManager;
|
||||
import com.graphhopper.jsprit.core.problem.constraint.SwitchNotFeasible;
|
||||
import com.graphhopper.jsprit.core.problem.solution.SolutionCostCalculator;
|
||||
import com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
||||
import com.graphhopper.jsprit.core.problem.solution.route.VehicleRoute;
|
||||
import com.graphhopper.jsprit.core.problem.vehicle.Vehicle;
|
||||
import com.graphhopper.jsprit.core.problem.vehicle.VehicleFleetManager;
|
||||
import com.graphhopper.jsprit.core.problem.vehicle.VehicleTypeKey;
|
||||
import com.graphhopper.jsprit.core.util.ActivityTimeTracker;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Created by schroeder on 10.12.14.
|
||||
|
|
@ -109,12 +103,9 @@ public class PrettyAlgorithmBuilder {
|
|||
iniInsertionStrategy.addListener(vehicleSwitched);
|
||||
if (!iniInsertionStrategy.getListeners().contains(stateManager))
|
||||
iniInsertionStrategy.addListener(stateManager);
|
||||
vra.addListener(new AlgorithmStartsListener() {
|
||||
@Override
|
||||
public void informAlgorithmStarts(VehicleRoutingProblem problem, VehicleRoutingAlgorithm algorithm, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
if (solutions.isEmpty()) {
|
||||
solutions.add(new InsertionInitialSolutionFactory(iniInsertionStrategy, iniObjFunction).createSolution(vrp));
|
||||
}
|
||||
vra.addListener((AlgorithmStartsListener) (problem, algorithm, solutions) -> {
|
||||
if (solutions.isEmpty()) {
|
||||
solutions.add(new InsertionInitialSolutionFactory(iniInsertionStrategy, iniObjFunction).createSolution(vrp));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -115,7 +115,7 @@ public class VehicleRoutingAlgorithm {
|
|||
super();
|
||||
this.problem = problem;
|
||||
this.searchStrategyManager = searchStrategyManager;
|
||||
initialSolutions = new ArrayList<VehicleRoutingProblemSolution>();
|
||||
initialSolutions = new ArrayList<>();
|
||||
objectiveFunction = null;
|
||||
}
|
||||
|
||||
|
|
@ -131,7 +131,7 @@ public class VehicleRoutingAlgorithm {
|
|||
super();
|
||||
this.problem = problem;
|
||||
this.searchStrategyManager = searchStrategyManager;
|
||||
initialSolutions = new ArrayList<VehicleRoutingProblemSolution>();
|
||||
initialSolutions = new ArrayList<>();
|
||||
this.objectiveFunction = objectiveFunction;
|
||||
}
|
||||
|
||||
|
|
@ -153,7 +153,7 @@ public class VehicleRoutingAlgorithm {
|
|||
//this method may lead to errors if tour activities in the solution are different to the ones in the VRP
|
||||
//(including differences in indexing)
|
||||
private void verifyAndAdaptSolution(VehicleRoutingProblemSolution solution) {
|
||||
Set<Job> jobsNotInSolution = new HashSet<Job>(problem.getJobs().values());
|
||||
Set<Job> jobsNotInSolution = new HashSet<>(problem.getJobs().values());
|
||||
jobsNotInSolution.removeAll(solution.getUnassignedJobs());
|
||||
for (VehicleRoute route : solution.getRoutes()) {
|
||||
jobsNotInSolution.removeAll(route.getTourActivities().getJobs());
|
||||
|
|
@ -225,7 +225,7 @@ public class VehicleRoutingAlgorithm {
|
|||
double now = System.currentTimeMillis();
|
||||
int noIterationsThisAlgoIsRunning = maxIterations;
|
||||
counter.reset();
|
||||
Collection<VehicleRoutingProblemSolution> solutions = new ArrayList<VehicleRoutingProblemSolution>(initialSolutions);
|
||||
Collection<VehicleRoutingProblemSolution> solutions = new ArrayList<>(initialSolutions);
|
||||
algorithmStarts(problem, solutions);
|
||||
bestEver = Solutions.bestOf(solutions);
|
||||
if (logger.isTraceEnabled()) {
|
||||
|
|
|
|||
|
|
@ -43,7 +43,6 @@ import com.graphhopper.jsprit.core.problem.solution.route.activity.TourActivity;
|
|||
import com.graphhopper.jsprit.core.problem.vehicle.FiniteFleetManagerFactory;
|
||||
import com.graphhopper.jsprit.core.problem.vehicle.InfiniteFleetManagerFactory;
|
||||
import com.graphhopper.jsprit.core.problem.vehicle.VehicleFleetManager;
|
||||
import com.graphhopper.jsprit.core.util.NoiseMaker;
|
||||
import com.graphhopper.jsprit.core.util.RandomNumberGeneration;
|
||||
import com.graphhopper.jsprit.core.util.Solutions;
|
||||
|
||||
|
|
@ -353,11 +352,11 @@ public class Jsprit {
|
|||
|
||||
}
|
||||
|
||||
private StateManager stateManager = null;
|
||||
private StateManager stateManager;
|
||||
|
||||
private ConstraintManager constraintManager = null;
|
||||
private ConstraintManager constraintManager;
|
||||
|
||||
private ExecutorService es = null;
|
||||
private ExecutorService es;
|
||||
|
||||
private Integer noThreads;
|
||||
|
||||
|
|
@ -365,7 +364,7 @@ public class Jsprit {
|
|||
|
||||
private boolean addCoreConstraints;
|
||||
|
||||
private SolutionCostCalculator objectiveFunction = null;
|
||||
private SolutionCostCalculator objectiveFunction;
|
||||
|
||||
private Properties properties;
|
||||
|
||||
|
|
@ -493,20 +492,12 @@ public class Jsprit {
|
|||
toInteger(properties.getProperty(Parameter.WORST_MAX_SHARE.toString())),
|
||||
random)
|
||||
);
|
||||
IterationStartsListener noise = new IterationStartsListener() {
|
||||
@Override
|
||||
public void informIterationStarts(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
worst.setNoiseMaker(new NoiseMaker() {
|
||||
|
||||
public double makeNoise() {
|
||||
if (random.nextDouble() < toDouble(getProperty(Parameter.RUIN_WORST_NOISE_PROB.toString()))) {
|
||||
return toDouble(getProperty(Parameter.RUIN_WORST_NOISE_LEVEL.toString()))
|
||||
* maxCosts * random.nextDouble();
|
||||
} else return 0.;
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
IterationStartsListener noise = (i, problem, solutions) -> worst.setNoiseMaker(() -> {
|
||||
if (random.nextDouble() < toDouble(getProperty(Parameter.RUIN_WORST_NOISE_PROB.toString()))) {
|
||||
return toDouble(getProperty(Parameter.RUIN_WORST_NOISE_LEVEL.toString()))
|
||||
* maxCosts * random.nextDouble();
|
||||
} else return 0.;
|
||||
});
|
||||
|
||||
final RuinClusters clusters = new RuinClusters(vrp, (int) (vrp.getJobs().values().size() * 0.5), jobNeighborhoods);
|
||||
clusters.setRandom(random);
|
||||
|
|
@ -612,13 +603,10 @@ public class Jsprit {
|
|||
if (properties.containsKey(Parameter.THRESHOLD_INI_ABS.toString())) {
|
||||
schrimpfAcceptance.setInitialThreshold(Double.valueOf(properties.getProperty(Parameter.THRESHOLD_INI_ABS.toString())));
|
||||
} else {
|
||||
schrimpfThreshold = new IterationStartsListener() {
|
||||
@Override
|
||||
public void informIterationStarts(int i, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
if (i == 1) {
|
||||
double initialThreshold = Solutions.bestOf(solutions).getCost() * toDouble(getProperty(Parameter.THRESHOLD_INI.toString()));
|
||||
schrimpfAcceptance.setInitialThreshold(initialThreshold);
|
||||
}
|
||||
schrimpfThreshold = (i, problem, solutions) -> {
|
||||
if (i == 1) {
|
||||
double initialThreshold = Solutions.bestOf(solutions).getCost() * toDouble(getProperty(Parameter.THRESHOLD_INI.toString()));
|
||||
schrimpfAcceptance.setInitialThreshold(initialThreshold);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -626,29 +614,29 @@ public class Jsprit {
|
|||
}
|
||||
|
||||
SolutionCostCalculator objectiveFunction = getObjectiveFunction(vrp, maxCosts);
|
||||
SearchStrategy radial_regret = new SearchStrategy(Strategy.RADIAL_REGRET.toString(), new SelectBest(), acceptor, objectiveFunction);
|
||||
radial_regret.addModule(configureModule(new RuinAndRecreateModule(Strategy.RADIAL_REGRET.toString(), regret, radial)));
|
||||
SearchStrategy radialRegret = new SearchStrategy(Strategy.RADIAL_REGRET.toString(), new SelectBest(), acceptor, objectiveFunction);
|
||||
radialRegret.addModule(configureModule(new RuinAndRecreateModule(Strategy.RADIAL_REGRET.toString(), regret, radial)));
|
||||
|
||||
SearchStrategy radial_best = new SearchStrategy(Strategy.RADIAL_BEST.toString(), new SelectBest(), acceptor, objectiveFunction);
|
||||
radial_best.addModule(configureModule(new RuinAndRecreateModule(Strategy.RADIAL_BEST.toString(), best, radial)));
|
||||
SearchStrategy radialBest = new SearchStrategy(Strategy.RADIAL_BEST.toString(), new SelectBest(), acceptor, objectiveFunction);
|
||||
radialBest.addModule(configureModule(new RuinAndRecreateModule(Strategy.RADIAL_BEST.toString(), best, radial)));
|
||||
|
||||
SearchStrategy random_best = new SearchStrategy(Strategy.RANDOM_BEST.toString(), new SelectBest(), acceptor, objectiveFunction);
|
||||
random_best.addModule(configureModule(new RuinAndRecreateModule(Strategy.RANDOM_BEST.toString(), best, random_for_best)));
|
||||
SearchStrategy randomBest = new SearchStrategy(Strategy.RANDOM_BEST.toString(), new SelectBest(), acceptor, objectiveFunction);
|
||||
randomBest.addModule(configureModule(new RuinAndRecreateModule(Strategy.RANDOM_BEST.toString(), best, random_for_best)));
|
||||
|
||||
SearchStrategy random_regret = new SearchStrategy(Strategy.RANDOM_REGRET.toString(), new SelectBest(), acceptor, objectiveFunction);
|
||||
random_regret.addModule(configureModule(new RuinAndRecreateModule(Strategy.RANDOM_REGRET.toString(), regret, random_for_regret)));
|
||||
SearchStrategy randomRegret = new SearchStrategy(Strategy.RANDOM_REGRET.toString(), new SelectBest(), acceptor, objectiveFunction);
|
||||
randomRegret.addModule(configureModule(new RuinAndRecreateModule(Strategy.RANDOM_REGRET.toString(), regret, random_for_regret)));
|
||||
|
||||
SearchStrategy worst_regret = new SearchStrategy(Strategy.WORST_REGRET.toString(), new SelectBest(), acceptor, objectiveFunction);
|
||||
worst_regret.addModule(configureModule(new RuinAndRecreateModule(Strategy.WORST_REGRET.toString(), regret, worst)));
|
||||
SearchStrategy worstRegret = new SearchStrategy(Strategy.WORST_REGRET.toString(), new SelectBest(), acceptor, objectiveFunction);
|
||||
worstRegret.addModule(configureModule(new RuinAndRecreateModule(Strategy.WORST_REGRET.toString(), regret, worst)));
|
||||
|
||||
SearchStrategy worst_best = new SearchStrategy(Strategy.WORST_BEST.toString(), new SelectBest(), acceptor, objectiveFunction);
|
||||
worst_best.addModule(configureModule(new RuinAndRecreateModule(Strategy.WORST_BEST.toString(), best, worst)));
|
||||
SearchStrategy worstBest = new SearchStrategy(Strategy.WORST_BEST.toString(), new SelectBest(), acceptor, objectiveFunction);
|
||||
worstBest.addModule(configureModule(new RuinAndRecreateModule(Strategy.WORST_BEST.toString(), best, worst)));
|
||||
|
||||
final SearchStrategy clusters_regret = new SearchStrategy(Strategy.CLUSTER_REGRET.toString(), new SelectBest(), acceptor, objectiveFunction);
|
||||
clusters_regret.addModule(configureModule(new RuinAndRecreateModule(Strategy.CLUSTER_REGRET.toString(), regret, clusters)));
|
||||
final SearchStrategy clustersRegret = new SearchStrategy(Strategy.CLUSTER_REGRET.toString(), new SelectBest(), acceptor, objectiveFunction);
|
||||
clustersRegret.addModule(configureModule(new RuinAndRecreateModule(Strategy.CLUSTER_REGRET.toString(), regret, clusters)));
|
||||
|
||||
final SearchStrategy clusters_best = new SearchStrategy(Strategy.CLUSTER_BEST.toString(), new SelectBest(), acceptor, objectiveFunction);
|
||||
clusters_best.addModule(configureModule(new RuinAndRecreateModule(Strategy.CLUSTER_BEST.toString(), best, clusters)));
|
||||
final SearchStrategy clustersBest = new SearchStrategy(Strategy.CLUSTER_BEST.toString(), new SelectBest(), acceptor, objectiveFunction);
|
||||
clustersBest.addModule(configureModule(new RuinAndRecreateModule(Strategy.CLUSTER_BEST.toString(), best, clusters)));
|
||||
|
||||
SearchStrategy stringRegret = new SearchStrategy(Strategy.STRING_REGRET.toString(), new SelectBest(), acceptor, objectiveFunction);
|
||||
stringRegret.addModule(configureModule(new RuinAndRecreateModule(Strategy.STRING_REGRET.toString(), regret, stringRuin)));
|
||||
|
|
@ -661,14 +649,14 @@ public class Jsprit {
|
|||
if (addCoreConstraints) {
|
||||
prettyBuilder.addCoreStateAndConstraintStuff();
|
||||
}
|
||||
prettyBuilder.withStrategy(radial_regret, toDouble(getProperty(Strategy.RADIAL_REGRET.toString())))
|
||||
.withStrategy(radial_best, toDouble(getProperty(Strategy.RADIAL_BEST.toString())))
|
||||
.withStrategy(random_best, toDouble(getProperty(Strategy.RANDOM_BEST.toString())))
|
||||
.withStrategy(random_regret, toDouble(getProperty(Strategy.RANDOM_REGRET.toString())))
|
||||
.withStrategy(worst_best, toDouble(getProperty(Strategy.WORST_BEST.toString())))
|
||||
.withStrategy(worst_regret, toDouble(getProperty(Strategy.WORST_REGRET.toString())))
|
||||
.withStrategy(clusters_regret, toDouble(getProperty(Strategy.CLUSTER_REGRET.toString())))
|
||||
.withStrategy(clusters_best, toDouble(getProperty(Strategy.CLUSTER_BEST.toString())))
|
||||
prettyBuilder.withStrategy(radialRegret, toDouble(getProperty(Strategy.RADIAL_REGRET.toString())))
|
||||
.withStrategy(radialBest, toDouble(getProperty(Strategy.RADIAL_BEST.toString())))
|
||||
.withStrategy(randomBest, toDouble(getProperty(Strategy.RANDOM_BEST.toString())))
|
||||
.withStrategy(randomRegret, toDouble(getProperty(Strategy.RANDOM_REGRET.toString())))
|
||||
.withStrategy(worstBest, toDouble(getProperty(Strategy.WORST_BEST.toString())))
|
||||
.withStrategy(worstRegret, toDouble(getProperty(Strategy.WORST_REGRET.toString())))
|
||||
.withStrategy(clustersRegret, toDouble(getProperty(Strategy.CLUSTER_REGRET.toString())))
|
||||
.withStrategy(clustersBest, toDouble(getProperty(Strategy.CLUSTER_BEST.toString())))
|
||||
.withStrategy(stringBest, toDouble(getProperty(Strategy.STRING_BEST.toString())))
|
||||
.withStrategy(stringRegret, toDouble(getProperty(Strategy.STRING_REGRET.toString())));
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue