1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

fix algorithmSearchProgress...Listener - reset it when algorithm starts,

deprecate setters VehicleRoutingProblem to make it immutable in the next
release, add Example
This commit is contained in:
Stefan Schroeder 2013-06-11 09:57:48 +02:00
parent a113a6cf18
commit bc6c1e23b5
5 changed files with 114 additions and 38 deletions

View file

@ -30,9 +30,11 @@ import org.jfree.data.Range;
import org.jfree.data.xy.XYSeries; import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection; import org.jfree.data.xy.XYSeriesCollection;
import basics.VehicleRoutingAlgorithm;
import basics.VehicleRoutingProblem; import basics.VehicleRoutingProblem;
import basics.VehicleRoutingProblemSolution; import basics.VehicleRoutingProblemSolution;
import basics.algo.AlgorithmEndsListener; import basics.algo.AlgorithmEndsListener;
import basics.algo.AlgorithmStartsListener;
import basics.algo.IterationEndsListener; import basics.algo.IterationEndsListener;
@ -45,7 +47,7 @@ import basics.algo.IterationEndsListener;
* *
*/ */
public class AlgorithmSearchProgressChartListener implements IterationEndsListener, AlgorithmEndsListener { public class AlgorithmSearchProgressChartListener implements IterationEndsListener, AlgorithmEndsListener, AlgorithmStartsListener {
private static Logger log = Logger.getLogger(AlgorithmSearchProgressChartListener.class); private static Logger log = Logger.getLogger(AlgorithmSearchProgressChartListener.class);
@ -146,4 +148,15 @@ public class AlgorithmSearchProgressChartListener implements IterationEndsListen
avgResultList.add(sum/(double)solutions.size()); avgResultList.add(sum/(double)solutions.size());
} }
@Override
public void informAlgorithmStarts(VehicleRoutingProblem problem,VehicleRoutingAlgorithm algorithm,Collection<VehicleRoutingProblemSolution> solutions) {
bestResults = null;
worstResults = null;
avgResults = null;
bestResultList.clear();
worstResultList.clear();
avgResultList.clear();
}
} }

View file

@ -123,6 +123,7 @@ public class VehicleRoutingAlgorithm {
logger.info("algorithm starts"); logger.info("algorithm starts");
double now = System.currentTimeMillis(); double now = System.currentTimeMillis();
verify(); verify();
int nuOfIterationsThisAlgoIsRunning = nOfIterations;
counter.reset(); counter.reset();
Collection<VehicleRoutingProblemSolution> solutions = new ArrayList<VehicleRoutingProblemSolution>(initialSolutions); Collection<VehicleRoutingProblemSolution> solutions = new ArrayList<VehicleRoutingProblemSolution>(initialSolutions);
algorithmStarts(problem,solutions); algorithmStarts(problem,solutions);
@ -138,11 +139,12 @@ public class VehicleRoutingAlgorithm {
else iterWithoutImprovement++; else iterWithoutImprovement++;
if(iterWithoutImprovement > prematureBreak){ if(iterWithoutImprovement > prematureBreak){
logger.info("premature break at iteration "+ (i+1)); logger.info("premature break at iteration "+ (i+1));
nuOfIterationsThisAlgoIsRunning = (i+1);
break; break;
} }
iterationEnds(i+1,problem,solutions); iterationEnds(i+1,problem,solutions);
} }
logger.info("iterations end at " + nOfIterations + " iterations"); logger.info("iterations end at " + nuOfIterationsThisAlgoIsRunning + " iterations");
algorithmEnds(problem,solutions); algorithmEnds(problem,solutions);
logger.info("total time: " + ((System.currentTimeMillis()-now)/1000.0) + "s"); logger.info("total time: " + ((System.currentTimeMillis()-now)/1000.0) + "s");
logger.info("done"); logger.info("done");

View file

@ -143,6 +143,7 @@ public class VehicleRoutingProblem {
return this; return this;
} }
/** /**
* Sets the type of fleetSize. * Sets the type of fleetSize.
* *
@ -153,9 +154,6 @@ public class VehicleRoutingProblem {
*/ */
public Builder setFleetSize(FleetSize fleetSize){ public Builder setFleetSize(FleetSize fleetSize){
this.fleetSize = fleetSize; this.fleetSize = fleetSize;
// if(fleetSize.equals(FleetSize.INFINITE)){
// fleetSizeIsInfinite=true;
// }
return this; return this;
} }
@ -198,20 +196,10 @@ public class VehicleRoutingProblem {
if(job instanceof Service) { if(job instanceof Service) {
addService((Service) job); addService((Service) job);
} }
// else if(job instanceof Shipment){
// addShipment((Shipment)job);
// }
else throw new IllegalStateException("job can only be a shipment or a service, but is instance of " + job.getClass()); else throw new IllegalStateException("job can only be a shipment or a service, but is instance of " + job.getClass());
return this; return this;
} }
// private void addShipment(Shipment job) {
// coordinates.put(job.getFromId(),job.getFromCoord());
// coordinates.put(job.getToId(), job.getToCoord());
// if(jobs.containsKey(job.getId())){ logger.warn("service " + job + " already in job list. overrides existing job."); }
// jobs.put(job.getId(),job);
//
// }
/** /**
* Adds a vehicle. * Adds a vehicle.
@ -221,7 +209,6 @@ public class VehicleRoutingProblem {
* @return * @return
*/ */
public Builder addVehicle(Vehicle vehicle) { public Builder addVehicle(Vehicle vehicle) {
// fleetSizeIsFinite = true;
vehicles.add(vehicle); vehicles.add(vehicle);
if(!vehicleTypes.contains(vehicle.getType())){ if(!vehicleTypes.contains(vehicle.getType())){
vehicleTypes.add(vehicle.getType()); vehicleTypes.add(vehicle.getType());
@ -262,7 +249,6 @@ public class VehicleRoutingProblem {
logger.warn("set routing costs crowFlyDistance."); logger.warn("set routing costs crowFlyDistance.");
transportCosts = new CrowFlyCosts(getLocations()); transportCosts = new CrowFlyCosts(getLocations());
} }
return new VehicleRoutingProblem(this); return new VehicleRoutingProblem(this);
} }
@ -380,10 +366,6 @@ public class VehicleRoutingProblem {
return fleetComposition; return fleetComposition;
} }
public void setFleetComposition(FleetComposition fleetComposition){
this.fleetComposition = fleetComposition;
}
/** /**
* Returns type of fleetSize, either INFINITE or FINITE. * Returns type of fleetSize, either INFINITE or FINITE.
* *
@ -395,10 +377,6 @@ public class VehicleRoutingProblem {
return fleetSize; return fleetSize;
} }
public void setFleetSize(FleetSize fleetSize){
this.fleetSize = fleetSize;
}
/** /**
* Returns the unmodifiable job map. * Returns the unmodifiable job map.
* *
@ -440,17 +418,6 @@ public class VehicleRoutingProblem {
return transportCosts; return transportCosts;
} }
/**
* Sets routing costs.
*
* @param costs
* @see VehicleRoutingTransportCosts
*/
public void setTransportCosts(VehicleRoutingTransportCosts costs) {
this.transportCosts = costs;
logger.info("transport costs set to " + costs.getClass());
}
/** /**
* Returns activityCosts. * Returns activityCosts.
*/ */
@ -459,8 +426,38 @@ public class VehicleRoutingProblem {
} }
/** /**
* Sets activityCosts. * Is deprecated and is not going to be supported any longer. Use the builder instead.
*/ */
@Deprecated
public void setFleetComposition(FleetComposition fleetComposition){
this.fleetComposition = fleetComposition;
}
/**
* Is deprecated and is not going to be supported any longer. Use the builder instead.
*/
@Deprecated
public void setFleetSize(FleetSize fleetSize){
this.fleetSize = fleetSize;
}
/**
* Sets routing costs.
* Is deprecated and is not going to be supported any longer. Use the builder instead.
*
* @param costs
* @see VehicleRoutingTransportCosts
*/
@Deprecated
public void setTransportCosts(VehicleRoutingTransportCosts costs) {
this.transportCosts = costs;
logger.info("transport costs set to " + costs.getClass());
}
/**
* Is deprecated and is not going to be supported any longer. Use the builder instead.
*/
@Deprecated
public void setActivityCosts(VehicleRoutingActivityCosts activityCosts){ public void setActivityCosts(VehicleRoutingActivityCosts activityCosts){
this.activityCosts = activityCosts; this.activityCosts = activityCosts;
logger.info("activtiy costs set to " + activityCosts.getClass()); logger.info("activtiy costs set to " + activityCosts.getClass());

View file

@ -0,0 +1,64 @@
package examples;
import readers.SolomonReader;
import algorithms.GreedySchrimpfFactory;
import algorithms.SchrimpfFactory;
import analysis.AlgorithmSearchProgressChartListener;
import analysis.StopWatch;
import basics.VehicleRoutingAlgorithm;
import basics.VehicleRoutingProblem;
import basics.algo.VehicleRoutingAlgorithmListeners.Priority;
public class CompareAlgorithmExample {
/**
* @param args
*/
public static void main(String[] args) {
/*
* Build the problem.
*
* But define a problem-builder first.
*/
VehicleRoutingProblem.Builder vrpBuilder = VehicleRoutingProblem.Builder.newInstance();
/*
* A solomonReader reads solomon-instance files, and stores the required information in the builder.
*/
new SolomonReader(vrpBuilder).read("input/C101_solomon.txt");
/*
* Finally, the problem can be built. By default, transportCosts are crowFlyDistances (as usually used for vrp-instances).
*/
VehicleRoutingProblem vrp = vrpBuilder.build();
/*
* Get schrimpf with threshold accepting
* Note that Priority.LOW is a way to priorize AlgorithmListeners
*/
VehicleRoutingAlgorithm vra_withThreshold = new SchrimpfFactory().createAlgorithm(vrp);
vra_withThreshold.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/schrimpfThreshold_progress.png"), Priority.LOW);
vra_withThreshold.getAlgorithmListeners().addListener(new StopWatch(), Priority.HIGH);
/*
* Get greedy schrimpf
*/
VehicleRoutingAlgorithm vra_greedy = new GreedySchrimpfFactory().createAlgorithm(vrp);
vra_greedy.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener("output/schrimpfGreedy_progress.png"), Priority.LOW);
vra_greedy.getAlgorithmListeners().addListener(new StopWatch(), Priority.HIGH);
/*
* run both
*/
vra_withThreshold.searchSolutions();
vra_greedy.searchSolutions();
vra_greedy.setPrematureBreak(40);
vra_greedy.searchSolutions();
}
}

View file

@ -66,7 +66,7 @@ public class SolomonExample {
* The algorithm can be defined and configured in an xml-file. * The algorithm can be defined and configured in an xml-file.
*/ */
VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp); VehicleRoutingAlgorithm vra = new SchrimpfFactory().createAlgorithm(vrp);
// vra.getAlgorithmListeners().addListener(new AlgorithmSearchProgressChartListener(pngFileName));
/* /*
* Solve the problem. * Solve the problem.
* *