mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
Merge branch 'master' into driver-breaks
This commit is contained in:
commit
11a320c2e4
19 changed files with 259 additions and 77 deletions
|
|
@ -35,6 +35,8 @@ import java.util.concurrent.Executors;
|
|||
|
||||
public class Jsprit {
|
||||
|
||||
private final ActivityInsertionCostsCalculator activityInsertion;
|
||||
|
||||
public enum Construction {
|
||||
|
||||
BEST_INSERTION("best_insertion"), REGRET_INSERTION("regret_insertion");
|
||||
|
|
@ -132,6 +134,8 @@ public class Jsprit {
|
|||
|
||||
private Random random = RandomNumberGeneration.newInstance();
|
||||
|
||||
private ActivityInsertionCostsCalculator activityInsertionCalculator;
|
||||
|
||||
public static Builder newInstance(VehicleRoutingProblem vrp) {
|
||||
return new Builder(vrp);
|
||||
}
|
||||
|
|
@ -225,6 +229,11 @@ public class Jsprit {
|
|||
return this;
|
||||
}
|
||||
|
||||
public Builder setActivityInsertionCalculator(ActivityInsertionCostsCalculator activityInsertionCalculator){
|
||||
this.activityInsertionCalculator = activityInsertionCalculator;
|
||||
return this;
|
||||
}
|
||||
|
||||
public VehicleRoutingAlgorithm buildAlgorithm() {
|
||||
return new Jsprit(this).create(vrp);
|
||||
}
|
||||
|
|
@ -294,6 +303,7 @@ public class Jsprit {
|
|||
this.properties = builder.properties;
|
||||
this.objectiveFunction = builder.objectiveFunction;
|
||||
this.random = builder.random;
|
||||
this.activityInsertion = builder.activityInsertionCalculator;
|
||||
}
|
||||
|
||||
private VehicleRoutingAlgorithm create(final VehicleRoutingProblem vrp) {
|
||||
|
|
@ -403,6 +413,7 @@ public class Jsprit {
|
|||
.setConcurrentMode(es, noThreads)
|
||||
.considerFixedCosts(toDouble(getProperty(Parameter.FIXED_COST_PARAM.toString())))
|
||||
.setAllowVehicleSwitch(toBoolean(getProperty(Parameter.VEHICLE_SWITCH.toString())))
|
||||
.setActivityInsertionCostCalculator(activityInsertion)
|
||||
.build();
|
||||
scorer = getRegretScorer(vrp);
|
||||
regretInsertion.setScoringFunction(scorer);
|
||||
|
|
@ -412,6 +423,7 @@ public class Jsprit {
|
|||
.setInsertionStrategy(InsertionBuilder.Strategy.REGRET)
|
||||
.setAllowVehicleSwitch(toBoolean(getProperty(Parameter.VEHICLE_SWITCH.toString())))
|
||||
.considerFixedCosts(toDouble(getProperty(Parameter.FIXED_COST_PARAM.toString())))
|
||||
.setActivityInsertionCostCalculator(activityInsertion)
|
||||
.build();
|
||||
scorer = getRegretScorer(vrp);
|
||||
regretInsertion.setScoringFunction(scorer);
|
||||
|
|
@ -425,6 +437,7 @@ public class Jsprit {
|
|||
.setInsertionStrategy(InsertionBuilder.Strategy.BEST)
|
||||
.considerFixedCosts(Double.valueOf(properties.getProperty(Parameter.FIXED_COST_PARAM.toString())))
|
||||
.setAllowVehicleSwitch(toBoolean(getProperty(Parameter.VEHICLE_SWITCH.toString())))
|
||||
.setActivityInsertionCostCalculator(activityInsertion)
|
||||
.build();
|
||||
best = bestInsertion;
|
||||
} else {
|
||||
|
|
@ -433,6 +446,7 @@ public class Jsprit {
|
|||
.considerFixedCosts(Double.valueOf(properties.getProperty(Parameter.FIXED_COST_PARAM.toString())))
|
||||
.setAllowVehicleSwitch(toBoolean(getProperty(Parameter.VEHICLE_SWITCH.toString())))
|
||||
.setConcurrentMode(es, noThreads)
|
||||
.setActivityInsertionCostCalculator(activityInsertion)
|
||||
.build();
|
||||
best = bestInsertion;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -77,9 +77,7 @@ public class AlgorithmConfigXmlReader {
|
|||
try {
|
||||
algorithmConfig.getXMLConfiguration().load();
|
||||
} catch (ConfigurationException e) {
|
||||
log.error(e);
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -519,7 +519,6 @@ public class VehicleRoutingAlgorithms {
|
|||
@Override
|
||||
public void uncaughtException(Thread arg0, Throwable arg1) {
|
||||
System.err.println(arg1.toString());
|
||||
System.exit(0);
|
||||
}
|
||||
});
|
||||
Runtime.getRuntime().addShutdownHook(new Thread() {
|
||||
|
|
|
|||
|
|
@ -130,9 +130,7 @@ public final class BestInsertionConcurrent extends AbstractInsertionStrategy {
|
|||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
logger.error("Exception", e);
|
||||
System.exit(1);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
VehicleRoute newRoute = VehicleRoute.emptyRoute();
|
||||
InsertionData newIData = bestInsertionCostCalculator.getInsertionData(newRoute, unassignedJob, NO_NEW_VEHICLE_YET, NO_NEW_DEPARTURE_TIME_YET, NO_NEW_DRIVER_YET, bestInsertionCost);
|
||||
|
|
|
|||
|
|
@ -259,6 +259,11 @@ public class RegretInsertion extends AbstractInsertionStrategy {
|
|||
if(scoredJob.getScore() > bestScoredJob.getScore()){
|
||||
bestScoredJob = scoredJob;
|
||||
}
|
||||
else if (scoredJob.getScore() == bestScoredJob.getScore()){
|
||||
if(scoredJob.getJob().getId().compareTo(bestScoredJob.getJob().getId()) <= 0){
|
||||
bestScoredJob = scoredJob;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return bestScoredJob;
|
||||
|
|
|
|||
|
|
@ -136,6 +136,11 @@ public class RegretInsertionConcurrent extends AbstractInsertionStrategy {
|
|||
} else if (sJob.getScore() > bestScoredJob.getScore()) {
|
||||
bestScoredJob = sJob;
|
||||
}
|
||||
else if (sJob.getScore() == bestScoredJob.getScore()){
|
||||
if(sJob.getJob().getId().compareTo(bestScoredJob.getJob().getId()) <= 0){
|
||||
bestScoredJob = sJob;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ public class Skills {
|
|||
* @return builder
|
||||
*/
|
||||
public Builder addSkill(String skill) {
|
||||
skills.add(skill.toLowerCase());
|
||||
skills.add(skill.trim().toLowerCase());
|
||||
return this;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -163,9 +163,7 @@ public class VrpXMLReader {
|
|||
try {
|
||||
xmlConfig.load();
|
||||
} catch (ConfigurationException e) {
|
||||
logger.error("Exception:", e);
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
readProblemType(xmlConfig);
|
||||
readVehiclesAndTheirTypes(xmlConfig);
|
||||
|
|
|
|||
|
|
@ -117,9 +117,7 @@ public class VrpXMLWriter {
|
|||
element.setAttribute("xsi:schemaLocation", "http://www.w3schools.com vrp_xml_schema.xsd");
|
||||
|
||||
} catch (ConfigurationException e) {
|
||||
logger.error("Exception:", e);
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
try {
|
||||
|
|
@ -128,9 +126,7 @@ public class VrpXMLWriter {
|
|||
serializer.serialize(xmlConfig.getDocument());
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
logger.error("Exception:", e);
|
||||
e.printStackTrace();
|
||||
System.exit(1);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -187,6 +187,7 @@ public class VehicleImpl extends AbstractVehicle {
|
|||
* @return this builder
|
||||
*/
|
||||
public Builder setEarliestStart(double earliest_startTime) {
|
||||
if(earliest_startTime < 0) throw new IllegalArgumentException("earliest start of vehicle " + id + " must not be negative");
|
||||
this.earliestStart = earliest_startTime;
|
||||
return this;
|
||||
}
|
||||
|
|
@ -198,6 +199,7 @@ public class VehicleImpl extends AbstractVehicle {
|
|||
* @return this builder
|
||||
*/
|
||||
public Builder setLatestArrival(double latest_arrTime) {
|
||||
if(latest_arrTime < 0) throw new IllegalArgumentException("latest arrival time of vehicle " + id + " must not be negative");
|
||||
this.latestArrival = latest_arrTime;
|
||||
return this;
|
||||
}
|
||||
|
|
@ -224,6 +226,7 @@ public class VehicleImpl extends AbstractVehicle {
|
|||
* or (endLocationId!=null AND returnToDepot=false)
|
||||
*/
|
||||
public VehicleImpl build() {
|
||||
if(latestArrival < earliestStart) throw new IllegalStateException("latest arrival of vehicle " + id + " must not be smaller than its start time");
|
||||
if (startLocation != null && endLocation != null) {
|
||||
if (!startLocation.getId().equals(endLocation.getId()) && !returnToDepot)
|
||||
throw new IllegalStateException("this must not be. you specified both endLocationId and open-routes. this is contradictory. <br>" +
|
||||
|
|
|
|||
|
|
@ -3,6 +3,8 @@ package jsprit.core.algorithm.box;
|
|||
import jsprit.core.algorithm.SearchStrategy;
|
||||
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
|
||||
import jsprit.core.algorithm.listener.StrategySelectedListener;
|
||||
import jsprit.core.algorithm.recreate.InsertionData;
|
||||
import jsprit.core.algorithm.recreate.listener.BeforeJobInsertionListener;
|
||||
import jsprit.core.algorithm.recreate.listener.JobInsertedListener;
|
||||
import jsprit.core.algorithm.ruin.listener.RuinListener;
|
||||
import jsprit.core.problem.Location;
|
||||
|
|
@ -185,6 +187,51 @@ public class JspritTest {
|
|||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void strategyDrawShouldBeReproducibleV2() {
|
||||
Service s = Service.Builder.newInstance("s1").setLocation(Location.newInstance(1, 1)).build();
|
||||
Service s2 = Service.Builder.newInstance("s2").setLocation(Location.newInstance(1, 2)).build();
|
||||
Service s3 = Service.Builder.newInstance("s3").setLocation(Location.newInstance(1, 2)).build();
|
||||
Service s4 = Service.Builder.newInstance("s4").setLocation(Location.newInstance(1, 2)).build();
|
||||
|
||||
VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance(0, 0)).build();
|
||||
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(s4).addJob(s3).addVehicle(v).addJob(s2).addJob(s).build();
|
||||
VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp).setProperty(Jsprit.Parameter.THREADS,"4").buildAlgorithm();
|
||||
vra.setMaxIterations(100);
|
||||
final List<String> firstRecord = new ArrayList<String>();
|
||||
vra.addListener(new StrategySelectedListener() {
|
||||
|
||||
@Override
|
||||
public void informSelectedStrategy(SearchStrategy.DiscoveredSolution discoveredSolution, VehicleRoutingProblem vehicleRoutingProblem, Collection<VehicleRoutingProblemSolution> vehicleRoutingProblemSolutions) {
|
||||
firstRecord.add(discoveredSolution.getStrategyId());
|
||||
}
|
||||
|
||||
});
|
||||
vra.searchSolutions();
|
||||
|
||||
RandomNumberGeneration.reset();
|
||||
VehicleRoutingAlgorithm second = Jsprit.Builder.newInstance(vrp).setProperty(Jsprit.Parameter.THREADS,"2").buildAlgorithm();
|
||||
second.setMaxIterations(100);
|
||||
final List<String> secondRecord = new ArrayList<String>();
|
||||
second.addListener(new StrategySelectedListener() {
|
||||
|
||||
@Override
|
||||
public void informSelectedStrategy(SearchStrategy.DiscoveredSolution discoveredSolution, VehicleRoutingProblem vehicleRoutingProblem, Collection<VehicleRoutingProblemSolution> vehicleRoutingProblemSolutions) {
|
||||
secondRecord.add(discoveredSolution.getStrategyId());
|
||||
}
|
||||
|
||||
});
|
||||
second.searchSolutions();
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
if (!firstRecord.get(i).equals(secondRecord.get(i))) {
|
||||
org.junit.Assert.assertFalse(true);
|
||||
}
|
||||
}
|
||||
org.junit.Assert.assertTrue(true);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ruinedJobsShouldBeReproducible() {
|
||||
Service s = Service.Builder.newInstance("s1").setLocation(Location.newInstance(1, 1)).build();
|
||||
|
|
@ -192,6 +239,72 @@ public class JspritTest {
|
|||
Service s3 = Service.Builder.newInstance("s3").setLocation(Location.newInstance(1, 2)).build();
|
||||
Service s4 = Service.Builder.newInstance("s4").setLocation(Location.newInstance(1, 2)).build();
|
||||
|
||||
VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance(0, 0)).build();
|
||||
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(s4).addJob(s3).addVehicle(v).addJob(s2).addJob(s).build();
|
||||
VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp)
|
||||
.setProperty(Jsprit.Strategy.WORST_REGRET,"0.")
|
||||
.setProperty(Jsprit.Strategy.WORST_BEST, "0.")
|
||||
.setProperty(Jsprit.Parameter.THREADS, "2").buildAlgorithm();
|
||||
vra.setMaxIterations(100);
|
||||
final List<String> firstRecord = new ArrayList<String>();
|
||||
vra.addListener(new RuinListener() {
|
||||
@Override
|
||||
public void ruinStarts(Collection<VehicleRoute> routes) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ruinEnds(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removed(Job job, VehicleRoute fromRoute) {
|
||||
firstRecord.add(job.getId());
|
||||
}
|
||||
});
|
||||
vra.searchSolutions();
|
||||
|
||||
VehicleRoutingAlgorithm second = Jsprit.Builder.newInstance(vrp).setProperty(Jsprit.Parameter.THREADS, "4")
|
||||
.setProperty(Jsprit.Strategy.WORST_REGRET, "0.")
|
||||
.setProperty(Jsprit.Strategy.WORST_BEST, "0.")
|
||||
.buildAlgorithm();
|
||||
second.setMaxIterations(100);
|
||||
final List<String> secondRecord = new ArrayList<String>();
|
||||
second.addListener(new RuinListener() {
|
||||
@Override
|
||||
public void ruinStarts(Collection<VehicleRoute> routes) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ruinEnds(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removed(Job job, VehicleRoute fromRoute) {
|
||||
secondRecord.add(job.getId());
|
||||
}
|
||||
});
|
||||
second.searchSolutions();
|
||||
|
||||
Assert.assertEquals(secondRecord.size(), firstRecord.size());
|
||||
for (int i = 0; i < firstRecord.size(); i++) {
|
||||
if (!firstRecord.get(i).equals(secondRecord.get(i))) {
|
||||
Assert.assertFalse(true);
|
||||
}
|
||||
}
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ruinedJobsShouldBeReproducibleV2() {
|
||||
Service s = Service.Builder.newInstance("s1").setLocation(Location.newInstance(1, 1)).build();
|
||||
Service s2 = Service.Builder.newInstance("s2").setLocation(Location.newInstance(1, 2)).build();
|
||||
Service s3 = Service.Builder.newInstance("s3").setLocation(Location.newInstance(1, 2)).build();
|
||||
Service s4 = Service.Builder.newInstance("s4").setLocation(Location.newInstance(1, 2)).build();
|
||||
|
||||
VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance(0, 0)).build();
|
||||
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().addJob(s4).addJob(s3).addVehicle(v).addJob(s2).addJob(s).build();
|
||||
VehicleRoutingAlgorithm vra = Jsprit.createAlgorithm(vrp);
|
||||
|
|
@ -286,5 +399,73 @@ public class JspritTest {
|
|||
Assert.assertTrue(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insertionShouldBeReproducibleV2() {
|
||||
Service s = Service.Builder.newInstance("s1").setLocation(Location.newInstance(1, 1)).build();
|
||||
Service s2 = Service.Builder.newInstance("s2").setLocation(Location.newInstance(1, 1)).build();
|
||||
Service s3 = Service.Builder.newInstance("s3").setLocation(Location.newInstance(1, 3)).build();
|
||||
Service s4 = Service.Builder.newInstance("s4").setLocation(Location.newInstance(1, 4)).build();
|
||||
|
||||
VehicleImpl v = VehicleImpl.Builder.newInstance("v").setStartLocation(Location.newInstance(0, 0)).build();
|
||||
VehicleRoutingProblem vrp = VehicleRoutingProblem.Builder.newInstance().setFleetSize(VehicleRoutingProblem.FleetSize.FINITE).addJob(s4).addJob(s3).addVehicle(v).addJob(s2).addJob(s).build();
|
||||
|
||||
VehicleRoutingAlgorithm vra = Jsprit.Builder.newInstance(vrp)
|
||||
.setProperty(Jsprit.Strategy.WORST_REGRET,"0.")
|
||||
.setProperty(Jsprit.Strategy.WORST_BEST, "0.")
|
||||
.setProperty(Jsprit.Parameter.THREADS, "4").buildAlgorithm();
|
||||
vra.setMaxIterations(100);
|
||||
final List<String> firstRecord = new ArrayList<String>();
|
||||
final List<Double> firstRecordCosts = new ArrayList<Double>();
|
||||
vra.addListener(new BeforeJobInsertionListener() {
|
||||
@Override
|
||||
public void informBeforeJobInsertion(Job job, InsertionData data, VehicleRoute route) {
|
||||
String id = job.getId();
|
||||
firstRecordCosts.add(data.getInsertionCost());
|
||||
firstRecord.add(id);
|
||||
}
|
||||
});
|
||||
vra.searchSolutions();
|
||||
|
||||
VehicleRoutingAlgorithm second = Jsprit.Builder.newInstance(vrp)
|
||||
.setProperty(Jsprit.Strategy.WORST_REGRET,"0.")
|
||||
.setProperty(Jsprit.Strategy.WORST_BEST, "0.")
|
||||
.setProperty(Jsprit.Parameter.THREADS, "5").buildAlgorithm();
|
||||
second.setMaxIterations(100);
|
||||
final List<String> secondRecord = new ArrayList<String>();
|
||||
final List<Double> secondRecordCosts = new ArrayList<Double>();
|
||||
second.addListener(new BeforeJobInsertionListener() {
|
||||
@Override
|
||||
public void informBeforeJobInsertion(Job job, InsertionData data, VehicleRoute route) {
|
||||
secondRecord.add(job.getId());
|
||||
secondRecordCosts.add(data.getInsertionCost());
|
||||
}
|
||||
});
|
||||
second.searchSolutions();
|
||||
|
||||
// for(int i=0;i<firstRecord.size();i++){
|
||||
// System.out.print(firstRecord.get(i) + " (" + ((int)(firstRecordCosts.get(i)*100.))/100. + "), ");
|
||||
// }
|
||||
// System.out.println();
|
||||
// for(int i=0;i<secondRecord.size();i++){
|
||||
// System.out.print(secondRecord.get(i) + " (" + ((int)(firstRecordCosts.get(i)*100.))/100. + "), ");
|
||||
// }
|
||||
|
||||
Assert.assertEquals(secondRecord.size(), firstRecord.size());
|
||||
for (int i = 0; i < firstRecord.size(); i++) {
|
||||
if (!firstRecord.get(i).equals(secondRecord.get(i))) {
|
||||
Assert.assertFalse(true);
|
||||
}
|
||||
}
|
||||
Assert.assertTrue(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void compare(){
|
||||
String s1 = "s2234";
|
||||
String s2 = "s1";
|
||||
int c = s1.compareTo(s2);
|
||||
Assert.assertEquals(1,c);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,4 +63,30 @@ public class SkillsTest {
|
|||
assertTrue(skills.containsSkill("skill2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSkillsAddedPrecedingWhitespaceShouldNotMatter() {
|
||||
Set<String> skillSet = new HashSet<String>();
|
||||
skillSet.add(" skill1");
|
||||
skillSet.add("Skill2");
|
||||
Skills skills = Skills.Builder.newInstance().addAllSkills(skillSet).build();
|
||||
assertTrue(skills.containsSkill("skill1"));
|
||||
assertTrue(skills.containsSkill("skill2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSkillsAddedTrailingWhitespaceShouldNotMatter() {
|
||||
Set<String> skillSet = new HashSet<String>();
|
||||
skillSet.add("skill1 ");
|
||||
skillSet.add("Skill2");
|
||||
Skills skills = Skills.Builder.newInstance().addAllSkills(skillSet).build();
|
||||
assertTrue(skills.containsSkill("skill1"));
|
||||
assertTrue(skills.containsSkill("skill2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSkillsAddedTrailingWhitespaceShouldNotMatter2() {
|
||||
Skills skills = Skills.Builder.newInstance().addSkill("skill1 ").build();
|
||||
assertTrue(skills.containsSkill("skill1"));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue