mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
Merge pull request #4 from bringg/VehicleRoutingAlgorithms-receive-cost-func-in-create
VehicleRoutingAlgorithms receive cost func in create
This commit is contained in:
commit
9af2fbbc0f
2 changed files with 48 additions and 19 deletions
|
|
@ -364,11 +364,11 @@ public class VehicleRoutingAlgorithms {
|
||||||
* @return {@link com.graphhopper.jsprit.core.algorithm.VehicleRoutingAlgorithm}
|
* @return {@link com.graphhopper.jsprit.core.algorithm.VehicleRoutingAlgorithm}
|
||||||
*/
|
*/
|
||||||
public static VehicleRoutingAlgorithm createAlgorithm(final VehicleRoutingProblem vrp, final AlgorithmConfig algorithmConfig) {
|
public static VehicleRoutingAlgorithm createAlgorithm(final VehicleRoutingProblem vrp, final AlgorithmConfig algorithmConfig) {
|
||||||
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), 0, null);
|
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), 0, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static VehicleRoutingAlgorithm createAlgorithm(final VehicleRoutingProblem vrp, int nThreads, final AlgorithmConfig algorithmConfig) {
|
public static VehicleRoutingAlgorithm createAlgorithm(final VehicleRoutingProblem vrp, int nThreads, final AlgorithmConfig algorithmConfig) {
|
||||||
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), nThreads, null);
|
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), nThreads, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -382,14 +382,14 @@ public class VehicleRoutingAlgorithms {
|
||||||
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
|
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
|
||||||
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
|
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
|
||||||
xmlReader.read(configURL);
|
xmlReader.read(configURL);
|
||||||
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), 0, null);
|
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), 0, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, int nThreads, final URL configURL) {
|
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, int nThreads, final URL configURL) {
|
||||||
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
|
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
|
||||||
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
|
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
|
||||||
xmlReader.read(configURL);
|
xmlReader.read(configURL);
|
||||||
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), nThreads, null);
|
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), nThreads, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -403,28 +403,28 @@ public class VehicleRoutingAlgorithms {
|
||||||
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
|
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
|
||||||
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
|
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
|
||||||
xmlReader.read(configFileName);
|
xmlReader.read(configFileName);
|
||||||
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), 0, null);
|
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), 0, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, final String configFileName, StateManager stateManager) {
|
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, final String configFileName, StateManager stateManager) {
|
||||||
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
|
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
|
||||||
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
|
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
|
||||||
xmlReader.read(configFileName);
|
xmlReader.read(configFileName);
|
||||||
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), 0, stateManager);
|
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), 0, stateManager, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, int nThreads, final String configFileName, StateManager stateManager) {
|
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, int nThreads, final String configFileName, StateManager stateManager, SolutionCostCalculator solutionCostCalculator) {
|
||||||
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
|
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
|
||||||
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
|
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
|
||||||
xmlReader.read(configFileName);
|
xmlReader.read(configFileName);
|
||||||
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), nThreads, stateManager);
|
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), nThreads, stateManager, solutionCostCalculator);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(VehicleRoutingProblem vrp, int nThreads, String configFileName) {
|
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(VehicleRoutingProblem vrp, int nThreads, String configFileName) {
|
||||||
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
|
AlgorithmConfig algorithmConfig = new AlgorithmConfig();
|
||||||
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
|
AlgorithmConfigXmlReader xmlReader = new AlgorithmConfigXmlReader(algorithmConfig);
|
||||||
xmlReader.read(configFileName);
|
xmlReader.read(configFileName);
|
||||||
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), nThreads, null);
|
return createAlgo(vrp, algorithmConfig.getXMLConfiguration(), nThreads, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class OpenRouteStateVerifier implements StateUpdater, ReverseActivityVisitor {
|
private static class OpenRouteStateVerifier implements StateUpdater, ReverseActivityVisitor {
|
||||||
|
|
@ -459,7 +459,7 @@ public class VehicleRoutingAlgorithms {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static VehicleRoutingAlgorithm createAlgo(final VehicleRoutingProblem vrp, XMLConfiguration config, int nuOfThreads, StateManager stateMan) {
|
private static VehicleRoutingAlgorithm createAlgo(final VehicleRoutingProblem vrp, XMLConfiguration config, int nuOfThreads, StateManager stateMan, SolutionCostCalculator solutionCostCalculator) {
|
||||||
//create state-manager
|
//create state-manager
|
||||||
final StateManager stateManager;
|
final StateManager stateManager;
|
||||||
if (stateMan != null) {
|
if (stateMan != null) {
|
||||||
|
|
@ -485,7 +485,7 @@ public class VehicleRoutingAlgorithms {
|
||||||
constraintManager.addSkillsConstraint();
|
constraintManager.addSkillsConstraint();
|
||||||
constraintManager.addConstraint(new SwitchNotFeasible(stateManager));
|
constraintManager.addConstraint(new SwitchNotFeasible(stateManager));
|
||||||
|
|
||||||
return readAndCreateAlgorithm(vrp, config, nuOfThreads, null, stateManager, constraintManager, true, true);
|
return readAndCreateAlgorithm(vrp, config, nuOfThreads, solutionCostCalculator, stateManager, constraintManager, true, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, AlgorithmConfig config,
|
public static VehicleRoutingAlgorithm readAndCreateAlgorithm(final VehicleRoutingProblem vrp, AlgorithmConfig config,
|
||||||
|
|
@ -584,11 +584,17 @@ public class VehicleRoutingAlgorithms {
|
||||||
stateManager.addStateUpdater(new UpdateActivityTimes(vrp.getTransportCosts(), activityPolicy, vrp.getActivityCosts()));
|
stateManager.addStateUpdater(new UpdateActivityTimes(vrp.getTransportCosts(), activityPolicy, vrp.getActivityCosts()));
|
||||||
stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager, activityPolicy));
|
stateManager.addStateUpdater(new UpdateVariableCosts(vrp.getActivityCosts(), vrp.getTransportCosts(), stateManager, activityPolicy));
|
||||||
|
|
||||||
final SolutionCostCalculator costCalculator;
|
|
||||||
if (solutionCostCalculator == null) costCalculator = getDefaultCostCalculator(stateManager);
|
|
||||||
else costCalculator = solutionCostCalculator;
|
|
||||||
|
|
||||||
PrettyAlgorithmBuilder prettyAlgorithmBuilder = PrettyAlgorithmBuilder.newInstance(vrp, vehicleFleetManager, stateManager, constraintManager);
|
PrettyAlgorithmBuilder prettyAlgorithmBuilder = PrettyAlgorithmBuilder.newInstance(vrp, vehicleFleetManager, stateManager, constraintManager);
|
||||||
|
final SolutionCostCalculator costCalculator;
|
||||||
|
if (solutionCostCalculator == null) {
|
||||||
|
costCalculator = getDefaultCostCalculator(stateManager);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
costCalculator = solutionCostCalculator;
|
||||||
|
prettyAlgorithmBuilder.withObjectiveFunction(solutionCostCalculator);
|
||||||
|
}
|
||||||
|
|
||||||
if(addCoreConstraints)
|
if(addCoreConstraints)
|
||||||
prettyAlgorithmBuilder.addCoreStateAndConstraintStuff();
|
prettyAlgorithmBuilder.addCoreStateAndConstraintStuff();
|
||||||
//construct initial solution creator
|
//construct initial solution creator
|
||||||
|
|
|
||||||
|
|
@ -28,8 +28,10 @@ import com.graphhopper.jsprit.core.algorithm.ruin.RuinStrategy;
|
||||||
import com.graphhopper.jsprit.core.algorithm.ruin.listener.RuinListener;
|
import com.graphhopper.jsprit.core.algorithm.ruin.listener.RuinListener;
|
||||||
import com.graphhopper.jsprit.core.algorithm.selector.SelectBest;
|
import com.graphhopper.jsprit.core.algorithm.selector.SelectBest;
|
||||||
import com.graphhopper.jsprit.core.algorithm.selector.SolutionSelector;
|
import com.graphhopper.jsprit.core.algorithm.selector.SolutionSelector;
|
||||||
|
import com.graphhopper.jsprit.core.algorithm.state.StateManager;
|
||||||
import com.graphhopper.jsprit.core.problem.VehicleRoutingProblem;
|
import com.graphhopper.jsprit.core.problem.VehicleRoutingProblem;
|
||||||
import com.graphhopper.jsprit.core.problem.job.Job;
|
import com.graphhopper.jsprit.core.problem.job.Job;
|
||||||
|
import com.graphhopper.jsprit.core.problem.solution.SolutionCostCalculator;
|
||||||
import com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
import com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
||||||
import com.graphhopper.jsprit.core.problem.solution.route.VehicleRoute;
|
import com.graphhopper.jsprit.core.problem.solution.route.VehicleRoute;
|
||||||
import com.graphhopper.jsprit.io.algorithm.VehicleRoutingAlgorithms.ModKey;
|
import com.graphhopper.jsprit.io.algorithm.VehicleRoutingAlgorithms.ModKey;
|
||||||
|
|
@ -42,12 +44,16 @@ import junit.framework.Assert;
|
||||||
import org.apache.commons.configuration.ConfigurationException;
|
import org.apache.commons.configuration.ConfigurationException;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.mockito.Mockito.doNothing;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
|
||||||
public class TestAlgorithmReader {
|
public class TestAlgorithmReader {
|
||||||
|
|
@ -283,4 +289,21 @@ public class TestAlgorithmReader {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWithVehicleRoutingAlgorithm() {
|
||||||
|
SolutionCostCalculator solutionCostCalculator = Mockito.mock(SolutionCostCalculator.class);
|
||||||
|
StateManager stateMan = Mockito.mock(StateManager.class);
|
||||||
|
doNothing().when(stateMan).updateLoadStates();
|
||||||
|
doNothing().when(stateMan).updateTimeWindowStates();
|
||||||
|
doNothing().when(stateMan).updateSkillStates();
|
||||||
|
|
||||||
|
VehicleRoutingAlgorithm vra = VehicleRoutingAlgorithms.readAndCreateAlgorithm(vrp, 10, getClass().getResource("algorithmConfig_withoutIterations.xml").getFile(), stateMan, solutionCostCalculator);
|
||||||
|
vra.getObjectiveFunction().equals(solutionCostCalculator);
|
||||||
|
|
||||||
|
verify(stateMan, times(2)).updateLoadStates();
|
||||||
|
verify(stateMan, times(2)).updateTimeWindowStates();
|
||||||
|
verify(stateMan, times(2)).updateSkillStates();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue