diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/ruin/RuinRadial.java b/jsprit-core/src/main/java/jsprit/core/algorithm/ruin/RuinRadial.java index a3b233d7..2c2653f4 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/ruin/RuinRadial.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/ruin/RuinRadial.java @@ -1,28 +1,25 @@ /******************************************************************************* - * Copyright (C) 2013 Stefan Schroeder - * + * Copyright (C) 2014 Stefan Schroeder + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either + * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . ******************************************************************************/ package jsprit.core.algorithm.ruin; import jsprit.core.algorithm.ruin.distance.JobDistance; -import jsprit.core.algorithm.ruin.listener.RuinListener; -import jsprit.core.algorithm.ruin.listener.RuinListeners; import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.job.Job; import jsprit.core.problem.solution.route.VehicleRoute; -import jsprit.core.util.RandomNumberGeneration; import jsprit.core.util.StopWatch; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -39,7 +36,7 @@ import java.util.*; * @author stefan * */ -final class RuinRadial implements RuinStrategy { +final class RuinRadial extends AbstractRuinStrategy { static interface JobNeighborhoods { @@ -255,15 +252,8 @@ final class RuinRadial implements RuinStrategy { private double fractionOfAllNodes2beRuined; - private Random random = RandomNumberGeneration.getRandom(); - - private RuinListeners ruinListeners; - private JobNeighborhoods jobNeighborhoods; - - public void setRandom(Random random) { - this.random = random; - } + /** * Constructs RuinRadial. @@ -276,7 +266,6 @@ final class RuinRadial implements RuinStrategy { super(); this.vrp = vrp; this.fractionOfAllNodes2beRuined = fraction2beRemoved; - ruinListeners = new RuinListeners(); int nJobsToMemorize = (int) Math.ceil(vrp.getJobs().values().size()*fraction2beRemoved); JobNeighborhoodsImplWithCapRestriction jobNeighborhoodsImpl = new JobNeighborhoodsImplWithCapRestriction(vrp, jobDistance, nJobsToMemorize); jobNeighborhoodsImpl.initialise(); @@ -294,7 +283,7 @@ final class RuinRadial implements RuinStrategy { * the neighborhood plus the randomly selected job from the number of vehicleRoutes. All removed jobs are then returned as a collection. */ @Override - public Collection ruin(Collection vehicleRoutes) { + public Collection ruinRoutes(Collection vehicleRoutes) { if(vehicleRoutes.isEmpty()){ return Collections.emptyList(); } @@ -303,16 +292,14 @@ final class RuinRadial implements RuinStrategy { return Collections.emptyList(); } Job randomJob = pickRandomJob(); - Collection unassignedJobs = ruin(vehicleRoutes,randomJob,nOfJobs2BeRemoved); - return unassignedJobs; + return ruin(vehicleRoutes,randomJob,nOfJobs2BeRemoved); } /** * Removes targetJob and its neighborhood and returns the removed jobs. */ - public Collection ruin(Collection vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved){ - ruinListeners.ruinStarts(vehicleRoutes); - List unassignedJobs = new ArrayList(); + public Collection ruinRoutes(Collection vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved){ + List unassignedJobs = new ArrayList(); int nNeighbors = nOfJobs2BeRemoved - 1; removeJob(targetJob,vehicleRoutes); unassignedJobs.add(targetJob); @@ -322,47 +309,19 @@ final class RuinRadial implements RuinStrategy { removeJob(job,vehicleRoutes); unassignedJobs.add(job); } - ruinListeners.ruinEnds(vehicleRoutes, unassignedJobs); - return unassignedJobs; - } - - private void removeJob(Job job, Collection vehicleRoutes) { - boolean removed = false; - for (VehicleRoute route : vehicleRoutes) { - removed = route.getTourActivities().removeJob(job);; - if (removed) { - ruinListeners.removed(job,route); - break; - } - } + return unassignedJobs; } private Job pickRandomJob() { int totNuOfJobs = vrp.getJobs().values().size(); int randomIndex = random.nextInt(totNuOfJobs); - Job job = new ArrayList(vrp.getJobs().values()).get(randomIndex); - return job; - } + return new ArrayList(vrp.getJobs().values()).get(randomIndex); + } private int getNuOfJobs2BeRemoved() { return (int) Math.ceil(vrp.getJobs().values().size() * fractionOfAllNodes2beRuined); } - @Override - public void addListener(RuinListener ruinListener) { - ruinListeners.addListener(ruinListener); - } - - @Override - public void removeListener(RuinListener ruinListener) { - ruinListeners.removeListener(ruinListener); - } - - @Override - public Collection getListeners() { - return ruinListeners.getListeners(); - } - } diff --git a/jsprit-core/src/main/java/jsprit/core/algorithm/ruin/RuinRandom.java b/jsprit-core/src/main/java/jsprit/core/algorithm/ruin/RuinRandom.java index 34d618e9..d15e6a10 100644 --- a/jsprit-core/src/main/java/jsprit/core/algorithm/ruin/RuinRandom.java +++ b/jsprit-core/src/main/java/jsprit/core/algorithm/ruin/RuinRandom.java @@ -1,31 +1,31 @@ /******************************************************************************* - * Copyright (C) 2013 Stefan Schroeder - * + * Copyright (C) 2014 Stefan Schroeder + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either + * License as published by the Free Software Foundation; either * version 3.0 of the License, or (at your option) any later version. - * + * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. - * + * * You should have received a copy of the GNU Lesser General Public * License along with this library. If not, see . ******************************************************************************/ package jsprit.core.algorithm.ruin; -import jsprit.core.algorithm.ruin.listener.RuinListener; -import jsprit.core.algorithm.ruin.listener.RuinListeners; import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.job.Job; import jsprit.core.problem.solution.route.VehicleRoute; -import jsprit.core.util.RandomNumberGeneration; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; /** @@ -36,7 +36,7 @@ import java.util.*; * */ -final class RuinRandom implements RuinStrategy { +final class RuinRandom extends AbstractRuinStrategy { private Logger logger = LogManager.getLogger(RuinRandom.class); @@ -44,14 +44,6 @@ final class RuinRandom implements RuinStrategy { private double fractionOfAllNodes2beRuined; - private Random random = RandomNumberGeneration.getRandom(); - - private RuinListeners ruinListeners; - - public void setRandom(Random random) { - this.random = random; - } - /** * Constructs ruinRandom. * @@ -62,8 +54,7 @@ final class RuinRandom implements RuinStrategy { super(); this.vrp = vrp; this.fractionOfAllNodes2beRuined = fraction; - ruinListeners = new RuinListeners(); - logger.info("initialise " + this); + logger.info("initialise " + this); logger.info("done"); } @@ -73,37 +64,32 @@ final class RuinRandom implements RuinStrategy { *

The number of jobs is calculated as follows: Math.ceil(vrp.getJobs().values().size() * fractionOfAllNodes2beRuined). */ @Override - public Collection ruin(Collection vehicleRoutes) { - ruinListeners.ruinStarts(vehicleRoutes); - List unassignedJobs = new ArrayList(); + public Collection ruinRoutes(Collection vehicleRoutes) { + List unassignedJobs = new ArrayList(); int nOfJobs2BeRemoved = selectNuOfJobs2BeRemoved(); ruin(vehicleRoutes, nOfJobs2BeRemoved, unassignedJobs); - ruinListeners.ruinEnds(vehicleRoutes, unassignedJobs); - return unassignedJobs; + return unassignedJobs; } /** * Removes nOfJobs2BeRemoved from vehicleRoutes, including targetJob. */ @Override - public Collection ruin(Collection vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved) { - ruinListeners.ruinStarts(vehicleRoutes); - List unassignedJobs = new ArrayList(); + public Collection ruinRoutes(Collection vehicleRoutes, Job targetJob, int nOfJobs2BeRemoved) { + List unassignedJobs = new ArrayList(); if(targetJob != null){ boolean removed = false; for (VehicleRoute route : vehicleRoutes) { - removed = route.getTourActivities().removeJob(targetJob); + removed = removeJob(targetJob,route); if (removed) { nOfJobs2BeRemoved--; unassignedJobs.add(targetJob); - ruinListeners.removed(targetJob,route); break; } } } ruin(vehicleRoutes, nOfJobs2BeRemoved, unassignedJobs); - ruinListeners.ruinEnds(vehicleRoutes, unassignedJobs); - return unassignedJobs; + return unassignedJobs; } public void setRuinFraction(double fractionOfAllNodes2beRuined) { @@ -117,13 +103,7 @@ final class RuinRandom implements RuinStrategy { Job job = pickRandomJob(availableJobs); unassignedJobs.add(job); availableJobs.remove(job); - for (VehicleRoute route : vehicleRoutes) { - boolean removed = route.getTourActivities().removeJob(job); - if (removed) { - ruinListeners.removed(job,route); - break; - } - } + removeJob(job,vehicleRoutes); } } @@ -142,19 +122,5 @@ final class RuinRandom implements RuinStrategy { return (int) Math.ceil(vrp.getJobs().values().size() * fractionOfAllNodes2beRuined); } - @Override - public void addListener(RuinListener ruinListener) { - ruinListeners.addListener(ruinListener); - } - - @Override - public void removeListener(RuinListener ruinListener) { - ruinListeners.removeListener(ruinListener); - } - - @Override - public Collection getListeners() { - return ruinListeners.getListeners(); - } }