mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
improve ruinApi
This commit is contained in:
parent
f78886767b
commit
59b25d3d8e
8 changed files with 76 additions and 83 deletions
|
|
@ -1,22 +0,0 @@
|
||||||
package algorithms;
|
|
||||||
|
|
||||||
import java.util.Collection;
|
|
||||||
|
|
||||||
import basics.Job;
|
|
||||||
import basics.route.VehicleRoute;
|
|
||||||
|
|
||||||
abstract class AbstractRuinStrategy implements RuinStrategy{
|
|
||||||
|
|
||||||
public void ruinStarts(Collection<VehicleRoute> routes){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ruinEnds(Collection<VehicleRoute> routes){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void jobRemoved(Job job, VehicleRoute fromRoute){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
33
jsprit-core/src/main/java/algorithms/RuinListeners.java
Normal file
33
jsprit-core/src/main/java/algorithms/RuinListeners.java
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
package algorithms;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import algorithms.RuinStrategy.RuinListener;
|
||||||
|
import basics.Job;
|
||||||
|
import basics.route.VehicleRoute;
|
||||||
|
|
||||||
|
class RuinListeners {
|
||||||
|
|
||||||
|
private Collection<RuinListener> ruinListeners = new ArrayList<RuinListener>();
|
||||||
|
|
||||||
|
void ruinStarts(Collection<VehicleRoute> routes){
|
||||||
|
for(RuinListener l : ruinListeners) l.ruinStarts(routes);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ruinEnds(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs){
|
||||||
|
for(RuinListener l : ruinListeners) l.ruinEnds(routes, unassignedJobs);
|
||||||
|
}
|
||||||
|
|
||||||
|
void removed(Job job, VehicleRoute fromRoute){
|
||||||
|
for(RuinListener l : ruinListeners) l.removed(job, fromRoute);
|
||||||
|
}
|
||||||
|
|
||||||
|
void addListener(RuinListener ruinListener){
|
||||||
|
ruinListeners.add(ruinListener);
|
||||||
|
}
|
||||||
|
|
||||||
|
void removeListener(RuinListener ruinListener){
|
||||||
|
ruinListeners.remove(ruinListener);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -35,22 +35,6 @@ import basics.route.VehicleRoute;
|
||||||
|
|
||||||
final class RuinRadial implements RuinStrategy {
|
final class RuinRadial implements RuinStrategy {
|
||||||
|
|
||||||
private final static String NAME = "radialRuin";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* returns a new creation of instance of ruinRadial
|
|
||||||
* @param vrp
|
|
||||||
* @param fraction TODO
|
|
||||||
* @param jobDistance
|
|
||||||
* @param jobRemover TODO
|
|
||||||
* @param routeUpdater TODO
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
static RuinRadial newInstance(VehicleRoutingProblem vrp, double fraction, JobDistance jobDistance, JobRemover jobRemover, VehicleRouteUpdater routeUpdater){
|
|
||||||
return new RuinRadial(vrp, fraction, jobDistance);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static class ReferencedJob {
|
static class ReferencedJob {
|
||||||
private Job job;
|
private Job job;
|
||||||
private double distance;
|
private double distance;
|
||||||
|
|
@ -82,6 +66,8 @@ final class RuinRadial implements RuinStrategy {
|
||||||
|
|
||||||
private JobDistance jobDistance;
|
private JobDistance jobDistance;
|
||||||
|
|
||||||
|
private RuinListeners ruinListeners;
|
||||||
|
|
||||||
public void setRandom(Random random) {
|
public void setRandom(Random random) {
|
||||||
this.random = random;
|
this.random = random;
|
||||||
}
|
}
|
||||||
|
|
@ -91,6 +77,7 @@ final class RuinRadial implements RuinStrategy {
|
||||||
this.vrp = vrp;
|
this.vrp = vrp;
|
||||||
this.jobDistance = jobDistance;
|
this.jobDistance = jobDistance;
|
||||||
this.fractionOfAllNodes2beRuined = fraction;
|
this.fractionOfAllNodes2beRuined = fraction;
|
||||||
|
ruinListeners = new RuinListeners();
|
||||||
calculateDistancesFromJob2Job();
|
calculateDistancesFromJob2Job();
|
||||||
logger.info("intialise " + this);
|
logger.info("intialise " + this);
|
||||||
}
|
}
|
||||||
|
|
@ -150,7 +137,7 @@ final class RuinRadial implements RuinStrategy {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<Job> ruin(Collection<VehicleRoute> vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved){
|
public Collection<Job> ruin(Collection<VehicleRoute> vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved){
|
||||||
ruinStarts(vehicleRoutes);
|
ruinListeners.ruinStarts(vehicleRoutes);
|
||||||
List<Job> unassignedJobs = new ArrayList<Job>();
|
List<Job> unassignedJobs = new ArrayList<Job>();
|
||||||
TreeSet<ReferencedJob> tree = distanceNodeTree.get(targetJob.getId());
|
TreeSet<ReferencedJob> tree = distanceNodeTree.get(targetJob.getId());
|
||||||
Iterator<ReferencedJob> descendingIterator = tree.descendingIterator();
|
Iterator<ReferencedJob> descendingIterator = tree.descendingIterator();
|
||||||
|
|
@ -164,30 +151,15 @@ final class RuinRadial implements RuinStrategy {
|
||||||
for (VehicleRoute route : vehicleRoutes) {
|
for (VehicleRoute route : vehicleRoutes) {
|
||||||
removed = route.getTourActivities().removeJob(job);;
|
removed = route.getTourActivities().removeJob(job);;
|
||||||
if (removed) {
|
if (removed) {
|
||||||
jobRemoved(job,route);
|
ruinListeners.removed(job,route);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ruinEnds(vehicleRoutes);
|
ruinListeners.ruinEnds(vehicleRoutes, unassignedJobs);
|
||||||
return unassignedJobs;
|
return unassignedJobs;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ruinEnds(Collection<VehicleRoute> vehicleRoutes) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void jobRemoved(Job job, VehicleRoute route) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ruinStarts(Collection<VehicleRoute> vehicleRoutes) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private Job pickRandomJob() {
|
private Job pickRandomJob() {
|
||||||
int totNuOfJobs = vrp.getJobs().values().size();
|
int totNuOfJobs = vrp.getJobs().values().size();
|
||||||
int randomIndex = random.nextInt(totNuOfJobs);
|
int randomIndex = random.nextInt(totNuOfJobs);
|
||||||
|
|
@ -200,6 +172,12 @@ final class RuinRadial implements RuinStrategy {
|
||||||
* fractionOfAllNodes2beRuined);
|
* fractionOfAllNodes2beRuined);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addListener(RuinListener ruinListener) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// @Override
|
// @Override
|
||||||
// public VehicleRoutingProblemSolution runAndGetSolution(VehicleRoutingProblemSolution vrpSolution) {
|
// public VehicleRoutingProblemSolution runAndGetSolution(VehicleRoutingProblemSolution vrpSolution) {
|
||||||
// ruin(vrpSolution.getRoutes());
|
// ruin(vrpSolution.getRoutes());
|
||||||
|
|
|
||||||
|
|
@ -36,10 +36,6 @@ import basics.route.VehicleRoute;
|
||||||
|
|
||||||
final class RuinRandom implements RuinStrategy {
|
final class RuinRandom implements RuinStrategy {
|
||||||
|
|
||||||
public static RuinRandom newInstance(VehicleRoutingProblem vrp, double fraction, JobRemover jobRemover, VehicleRouteUpdater routeUpdater){
|
|
||||||
return new RuinRandom(vrp, fraction);
|
|
||||||
}
|
|
||||||
|
|
||||||
private Logger logger = Logger.getLogger(RuinRandom.class);
|
private Logger logger = Logger.getLogger(RuinRandom.class);
|
||||||
|
|
||||||
private VehicleRoutingProblem vrp;
|
private VehicleRoutingProblem vrp;
|
||||||
|
|
@ -48,6 +44,8 @@ final class RuinRandom implements RuinStrategy {
|
||||||
|
|
||||||
private Random random = RandomNumberGeneration.getRandom();
|
private Random random = RandomNumberGeneration.getRandom();
|
||||||
|
|
||||||
|
private RuinListeners ruinListeners;
|
||||||
|
|
||||||
public void setRandom(Random random) {
|
public void setRandom(Random random) {
|
||||||
this.random = random;
|
this.random = random;
|
||||||
}
|
}
|
||||||
|
|
@ -62,6 +60,7 @@ final class RuinRandom implements RuinStrategy {
|
||||||
super();
|
super();
|
||||||
this.vrp = vrp;
|
this.vrp = vrp;
|
||||||
this.fractionOfAllNodes2beRuined = fraction;
|
this.fractionOfAllNodes2beRuined = fraction;
|
||||||
|
ruinListeners = new RuinListeners();
|
||||||
logger.info("initialise " + this);
|
logger.info("initialise " + this);
|
||||||
logger.info("done");
|
logger.info("done");
|
||||||
}
|
}
|
||||||
|
|
@ -84,7 +83,7 @@ final class RuinRandom implements RuinStrategy {
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Collection<Job> ruin(Collection<VehicleRoute> vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved) {
|
public Collection<Job> ruin(Collection<VehicleRoute> vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved) {
|
||||||
ruinStarts(vehicleRoutes);
|
ruinListeners.ruinStarts(vehicleRoutes);
|
||||||
List<Job> unassignedJobs = new ArrayList<Job>();
|
List<Job> unassignedJobs = new ArrayList<Job>();
|
||||||
if(targetJob != null){
|
if(targetJob != null){
|
||||||
boolean removed = false;
|
boolean removed = false;
|
||||||
|
|
@ -93,13 +92,13 @@ final class RuinRandom implements RuinStrategy {
|
||||||
if (removed) {
|
if (removed) {
|
||||||
nOfJobs2BeRemoved--;
|
nOfJobs2BeRemoved--;
|
||||||
unassignedJobs.add(targetJob);
|
unassignedJobs.add(targetJob);
|
||||||
jobRemoved(targetJob,route);
|
ruinListeners.removed(targetJob,route);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ruin(vehicleRoutes, nOfJobs2BeRemoved, unassignedJobs);
|
ruin(vehicleRoutes, nOfJobs2BeRemoved, unassignedJobs);
|
||||||
ruinEnds(vehicleRoutes);
|
ruinListeners.ruinEnds(vehicleRoutes, unassignedJobs);
|
||||||
return unassignedJobs;
|
return unassignedJobs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -117,27 +116,13 @@ final class RuinRandom implements RuinStrategy {
|
||||||
for (VehicleRoute route : vehicleRoutes) {
|
for (VehicleRoute route : vehicleRoutes) {
|
||||||
boolean removed = route.getTourActivities().removeJob(job);
|
boolean removed = route.getTourActivities().removeJob(job);
|
||||||
if (removed) {
|
if (removed) {
|
||||||
jobRemoved(job,route);
|
ruinListeners.removed(job,route);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ruinEnds(Collection<VehicleRoute> vehicleRoutes) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ruinStarts(Collection<VehicleRoute> vehicleRoutes) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private void jobRemoved(Job job, VehicleRoute route) {
|
|
||||||
// TODO Auto-generated method stub
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
|
|
@ -153,4 +138,9 @@ final class RuinRandom implements RuinStrategy {
|
||||||
return (int) Math.ceil(vrp.getJobs().values().size() * fractionOfAllNodes2beRuined);
|
return (int) Math.ceil(vrp.getJobs().values().size() * fractionOfAllNodes2beRuined);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addListener(RuinListener ruinListener) {
|
||||||
|
ruinListeners.addListener(ruinListener);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,6 +31,12 @@ interface RuinStrategy {
|
||||||
|
|
||||||
public static interface RuinListener {
|
public static interface RuinListener {
|
||||||
|
|
||||||
|
public void ruinStarts(Collection<VehicleRoute> routes);
|
||||||
|
|
||||||
|
public void ruinEnds(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs);
|
||||||
|
|
||||||
|
public void removed(Job job, VehicleRoute fromRoute);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -44,4 +50,6 @@ interface RuinStrategy {
|
||||||
|
|
||||||
public Collection<Job> ruin(Collection<VehicleRoute> vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved);
|
public Collection<Job> ruin(Collection<VehicleRoute> vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved);
|
||||||
|
|
||||||
|
public void addListener(RuinListener ruinListener);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -757,7 +757,7 @@ public class VehicleRoutingAlgorithms {
|
||||||
RuinStrategyKey stratKey = new RuinStrategyKey(ruinKey);
|
RuinStrategyKey stratKey = new RuinStrategyKey(ruinKey);
|
||||||
RuinStrategy ruin = definedClasses.get(stratKey);
|
RuinStrategy ruin = definedClasses.get(stratKey);
|
||||||
if(ruin == null){
|
if(ruin == null){
|
||||||
ruin = RuinRadial.newInstance(vrp, 0.3, new JobDistanceAvgCosts(vrp.getTransportCosts()), new JobRemoverImpl(), new TourStateUpdater(activityStates, vrp.getTransportCosts(), vrp.getActivityCosts()));
|
ruin = new RuinRadial(vrp, 0.3, new JobDistanceAvgCosts(vrp.getTransportCosts()));
|
||||||
definedClasses.put(stratKey, ruin);
|
definedClasses.put(stratKey, ruin);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -795,7 +795,7 @@ public class VehicleRoutingAlgorithms {
|
||||||
RuinStrategyKey stratKey = new RuinStrategyKey(modKey);
|
RuinStrategyKey stratKey = new RuinStrategyKey(modKey);
|
||||||
RuinStrategy ruin = definedClasses.get(stratKey);
|
RuinStrategy ruin = definedClasses.get(stratKey);
|
||||||
if(ruin == null){
|
if(ruin == null){
|
||||||
ruin = RuinRadial.newInstance(vrp, shareToRuin, jobDistance, new JobRemoverImpl(), new TourStateUpdater(activityStates, vrp.getTransportCosts(), vrp.getActivityCosts()));
|
ruin = new RuinRadial(vrp, shareToRuin, jobDistance);
|
||||||
definedClasses.put(stratKey, ruin);
|
definedClasses.put(stratKey, ruin);
|
||||||
}
|
}
|
||||||
return ruin;
|
return ruin;
|
||||||
|
|
@ -807,7 +807,7 @@ public class VehicleRoutingAlgorithms {
|
||||||
RuinStrategyKey stratKey = new RuinStrategyKey(modKey);
|
RuinStrategyKey stratKey = new RuinStrategyKey(modKey);
|
||||||
RuinStrategy ruin = definedClasses.get(stratKey);
|
RuinStrategy ruin = definedClasses.get(stratKey);
|
||||||
if(ruin == null){
|
if(ruin == null){
|
||||||
ruin = RuinRandom.newInstance(vrp, shareToRuin, new JobRemoverImpl(), new TourStateUpdater(activityStates, vrp.getTransportCosts(), vrp.getActivityCosts()));
|
ruin = new RuinRandom(vrp, shareToRuin);
|
||||||
definedClasses.put(stratKey, ruin);
|
definedClasses.put(stratKey, ruin);
|
||||||
}
|
}
|
||||||
return ruin;
|
return ruin;
|
||||||
|
|
|
||||||
|
|
@ -202,7 +202,7 @@ public class GendreauPostOptTest {
|
||||||
|
|
||||||
assertEquals(110.0, sol.getCost(), 0.5);
|
assertEquals(110.0, sol.getCost(), 0.5);
|
||||||
|
|
||||||
RuinRadial radialRuin = RuinRadial.newInstance(vrp, 0.2, new JobDistanceAvgCosts(vrp.getTransportCosts()), new JobRemoverImpl(), updater);
|
RuinRadial radialRuin = new RuinRadial(vrp, 0.2, new JobDistanceAvgCosts(vrp.getTransportCosts()));
|
||||||
AbstractInsertionStrategy insertionStrategy = new BestInsertion(routeAlgorithm);
|
AbstractInsertionStrategy insertionStrategy = new BestInsertion(routeAlgorithm);
|
||||||
Gendreau postOpt = new Gendreau(vrp, radialRuin, insertionStrategy);
|
Gendreau postOpt = new Gendreau(vrp, radialRuin, insertionStrategy);
|
||||||
postOpt.setFleetManager(fleetManager);
|
postOpt.setFleetManager(fleetManager);
|
||||||
|
|
@ -243,7 +243,7 @@ public class GendreauPostOptTest {
|
||||||
|
|
||||||
assertEquals(110.0, sol.getCost(), 0.5);
|
assertEquals(110.0, sol.getCost(), 0.5);
|
||||||
|
|
||||||
RuinRadial radialRuin = RuinRadial.newInstance(vrp, 0.2, new JobDistanceAvgCosts(vrp.getTransportCosts()), new JobRemoverImpl(), updater);
|
RuinRadial radialRuin = new RuinRadial(vrp, 0.2, new JobDistanceAvgCosts(vrp.getTransportCosts()));
|
||||||
AbstractInsertionStrategy insertionStrategy = new BestInsertion(routeAlgorithm);
|
AbstractInsertionStrategy insertionStrategy = new BestInsertion(routeAlgorithm);
|
||||||
Gendreau postOpt = new Gendreau(vrp, radialRuin, insertionStrategy);
|
Gendreau postOpt = new Gendreau(vrp, radialRuin, insertionStrategy);
|
||||||
postOpt.setShareOfJobsToRuin(1.0);
|
postOpt.setShareOfJobsToRuin(1.0);
|
||||||
|
|
|
||||||
|
|
@ -170,6 +170,12 @@ public class TestAlgorithmReader {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addListener(RuinListener ruinListener) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
StrategyModuleKey moduleKey = new StrategyModuleKey(key);
|
StrategyModuleKey moduleKey = new StrategyModuleKey(key);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue