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);
|
||||
}
|
||||
}
|
||||
|
|
@ -34,22 +34,6 @@ import basics.route.VehicleRoute;
|
|||
|
||||
|
||||
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 {
|
||||
private Job job;
|
||||
|
|
@ -82,6 +66,8 @@ final class RuinRadial implements RuinStrategy {
|
|||
|
||||
private JobDistance jobDistance;
|
||||
|
||||
private RuinListeners ruinListeners;
|
||||
|
||||
public void setRandom(Random random) {
|
||||
this.random = random;
|
||||
}
|
||||
|
|
@ -91,6 +77,7 @@ final class RuinRadial implements RuinStrategy {
|
|||
this.vrp = vrp;
|
||||
this.jobDistance = jobDistance;
|
||||
this.fractionOfAllNodes2beRuined = fraction;
|
||||
ruinListeners = new RuinListeners();
|
||||
calculateDistancesFromJob2Job();
|
||||
logger.info("intialise " + this);
|
||||
}
|
||||
|
|
@ -150,7 +137,7 @@ final class RuinRadial implements RuinStrategy {
|
|||
}
|
||||
|
||||
public Collection<Job> ruin(Collection<VehicleRoute> vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved){
|
||||
ruinStarts(vehicleRoutes);
|
||||
ruinListeners.ruinStarts(vehicleRoutes);
|
||||
List<Job> unassignedJobs = new ArrayList<Job>();
|
||||
TreeSet<ReferencedJob> tree = distanceNodeTree.get(targetJob.getId());
|
||||
Iterator<ReferencedJob> descendingIterator = tree.descendingIterator();
|
||||
|
|
@ -164,30 +151,15 @@ final class RuinRadial implements RuinStrategy {
|
|||
for (VehicleRoute route : vehicleRoutes) {
|
||||
removed = route.getTourActivities().removeJob(job);;
|
||||
if (removed) {
|
||||
jobRemoved(job,route);
|
||||
ruinListeners.removed(job,route);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
ruinEnds(vehicleRoutes);
|
||||
ruinListeners.ruinEnds(vehicleRoutes, 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() {
|
||||
int totNuOfJobs = vrp.getJobs().values().size();
|
||||
int randomIndex = random.nextInt(totNuOfJobs);
|
||||
|
|
@ -200,6 +172,12 @@ final class RuinRadial implements RuinStrategy {
|
|||
* fractionOfAllNodes2beRuined);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(RuinListener ruinListener) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public VehicleRoutingProblemSolution runAndGetSolution(VehicleRoutingProblemSolution vrpSolution) {
|
||||
// ruin(vrpSolution.getRoutes());
|
||||
|
|
|
|||
|
|
@ -35,10 +35,6 @@ import basics.route.VehicleRoute;
|
|||
*/
|
||||
|
||||
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);
|
||||
|
||||
|
|
@ -47,6 +43,8 @@ final class RuinRandom implements RuinStrategy {
|
|||
private double fractionOfAllNodes2beRuined;
|
||||
|
||||
private Random random = RandomNumberGeneration.getRandom();
|
||||
|
||||
private RuinListeners ruinListeners;
|
||||
|
||||
public void setRandom(Random random) {
|
||||
this.random = random;
|
||||
|
|
@ -62,6 +60,7 @@ final class RuinRandom implements RuinStrategy {
|
|||
super();
|
||||
this.vrp = vrp;
|
||||
this.fractionOfAllNodes2beRuined = fraction;
|
||||
ruinListeners = new RuinListeners();
|
||||
logger.info("initialise " + this);
|
||||
logger.info("done");
|
||||
}
|
||||
|
|
@ -84,7 +83,7 @@ final class RuinRandom implements RuinStrategy {
|
|||
*/
|
||||
@Override
|
||||
public Collection<Job> ruin(Collection<VehicleRoute> vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved) {
|
||||
ruinStarts(vehicleRoutes);
|
||||
ruinListeners.ruinStarts(vehicleRoutes);
|
||||
List<Job> unassignedJobs = new ArrayList<Job>();
|
||||
if(targetJob != null){
|
||||
boolean removed = false;
|
||||
|
|
@ -93,13 +92,13 @@ final class RuinRandom implements RuinStrategy {
|
|||
if (removed) {
|
||||
nOfJobs2BeRemoved--;
|
||||
unassignedJobs.add(targetJob);
|
||||
jobRemoved(targetJob,route);
|
||||
ruinListeners.removed(targetJob,route);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
ruin(vehicleRoutes, nOfJobs2BeRemoved, unassignedJobs);
|
||||
ruinEnds(vehicleRoutes);
|
||||
ruinListeners.ruinEnds(vehicleRoutes, unassignedJobs);
|
||||
return unassignedJobs;
|
||||
}
|
||||
|
||||
|
|
@ -117,28 +116,14 @@ final class RuinRandom implements RuinStrategy {
|
|||
for (VehicleRoute route : vehicleRoutes) {
|
||||
boolean removed = route.getTourActivities().removeJob(job);
|
||||
if (removed) {
|
||||
jobRemoved(job,route);
|
||||
ruinListeners.removed(job,route);
|
||||
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
|
||||
public String toString() {
|
||||
return "[name=randomRuin][fraction="+fractionOfAllNodes2beRuined+"]";
|
||||
|
|
@ -153,4 +138,9 @@ final class RuinRandom implements RuinStrategy {
|
|||
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 void ruinStarts(Collection<VehicleRoute> routes);
|
||||
|
||||
public void ruinEnds(Collection<VehicleRoute> routes, Collection<Job> unassignedJobs);
|
||||
|
||||
public void removed(Job job, VehicleRoute fromRoute);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -43,5 +49,7 @@ interface RuinStrategy {
|
|||
public Collection<Job> ruin(Collection<VehicleRoute> vehicleRoutes);
|
||||
|
||||
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);
|
||||
RuinStrategy ruin = definedClasses.get(stratKey);
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
@ -795,7 +795,7 @@ public class VehicleRoutingAlgorithms {
|
|||
RuinStrategyKey stratKey = new RuinStrategyKey(modKey);
|
||||
RuinStrategy ruin = definedClasses.get(stratKey);
|
||||
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);
|
||||
}
|
||||
return ruin;
|
||||
|
|
@ -807,7 +807,7 @@ public class VehicleRoutingAlgorithms {
|
|||
RuinStrategyKey stratKey = new RuinStrategyKey(modKey);
|
||||
RuinStrategy ruin = definedClasses.get(stratKey);
|
||||
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);
|
||||
}
|
||||
return ruin;
|
||||
|
|
|
|||
|
|
@ -202,7 +202,7 @@ public class GendreauPostOptTest {
|
|||
|
||||
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);
|
||||
Gendreau postOpt = new Gendreau(vrp, radialRuin, insertionStrategy);
|
||||
postOpt.setFleetManager(fleetManager);
|
||||
|
|
@ -243,7 +243,7 @@ public class GendreauPostOptTest {
|
|||
|
||||
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);
|
||||
Gendreau postOpt = new Gendreau(vrp, radialRuin, insertionStrategy);
|
||||
postOpt.setShareOfJobsToRuin(1.0);
|
||||
|
|
|
|||
|
|
@ -169,6 +169,12 @@ public class TestAlgorithmReader {
|
|||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addListener(RuinListener ruinListener) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue