mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
Merge pull request #6 from bringg/solutions-as-parameter
solutions as parameter
This commit is contained in:
commit
10187a270b
2 changed files with 30 additions and 1 deletions
|
|
@ -214,11 +214,14 @@ public class VehicleRoutingAlgorithm {
|
|||
* @see {@link SearchStrategyManager}, {@link com.graphhopper.jsprit.core.algorithm.listener.VehicleRoutingAlgorithmListener}, {@link com.graphhopper.jsprit.core.algorithm.listener.AlgorithmStartsListener}, {@link com.graphhopper.jsprit.core.algorithm.listener.AlgorithmEndsListener}, {@link com.graphhopper.jsprit.core.algorithm.listener.IterationStartsListener}, {@link com.graphhopper.jsprit.core.algorithm.listener.IterationEndsListener}
|
||||
*/
|
||||
public Collection<VehicleRoutingProblemSolution> searchSolutions() {
|
||||
return searchSolutions(new ArrayList<VehicleRoutingProblemSolution>());
|
||||
}
|
||||
|
||||
public Collection<VehicleRoutingProblemSolution> searchSolutions(Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
logger.info("algorithm starts: [maxIterations={}]", maxIterations);
|
||||
double now = System.currentTimeMillis();
|
||||
int noIterationsThisAlgoIsRunning = maxIterations;
|
||||
counter.reset();
|
||||
Collection<VehicleRoutingProblemSolution> solutions = new ArrayList<VehicleRoutingProblemSolution>(initialSolutions);
|
||||
algorithmStarts(problem, solutions);
|
||||
bestEver = Solutions.bestOf(solutions);
|
||||
if (logger.isTraceEnabled()) {
|
||||
|
|
|
|||
|
|
@ -21,10 +21,14 @@ import com.graphhopper.jsprit.core.algorithm.listener.IterationStartsListener;
|
|||
import com.graphhopper.jsprit.core.algorithm.termination.PrematureAlgorithmTermination;
|
||||
import com.graphhopper.jsprit.core.problem.VehicleRoutingProblem;
|
||||
import com.graphhopper.jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
||||
import com.graphhopper.jsprit.core.problem.solution.route.VehicleRoute;
|
||||
import com.graphhopper.jsprit.core.util.Solutions;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
|
@ -91,6 +95,28 @@ public class VehicleRoutingAlgorithmTest {
|
|||
assertEquals(1000, counter.getCountIterations());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSettingIterations_iterAreExecutedCorrectlyWithSolutions() {
|
||||
Collection<VehicleRoutingProblemSolution> solutions = new ArrayList<>();
|
||||
double bestSolutionCost = Double.MAX_VALUE;
|
||||
Random random = new Random();
|
||||
for (int i = 0; i < random.nextInt(10) + 10; ++i) {
|
||||
double cost = Math.abs(random.nextInt() + random.nextDouble());
|
||||
solutions.add(new VehicleRoutingProblemSolution(new ArrayList<VehicleRoute>(), cost));
|
||||
bestSolutionCost = Math.min(bestSolutionCost, cost);
|
||||
}
|
||||
SearchStrategyManager stratManager = mock(SearchStrategyManager.class);
|
||||
VehicleRoutingAlgorithm algorithm = new VehicleRoutingAlgorithm(mock(VehicleRoutingProblem.class), stratManager);
|
||||
when(stratManager.getRandomStrategy()).thenReturn(mock(SearchStrategy.class));
|
||||
when(stratManager.getWeights()).thenReturn(Arrays.asList(1.0));
|
||||
algorithm.setMaxIterations(1000);
|
||||
CountIterations counter = new CountIterations();
|
||||
algorithm.addListener(counter);
|
||||
Collection<VehicleRoutingProblemSolution> vehicleRoutingProblemSolutions = algorithm.searchSolutions(solutions);
|
||||
assertEquals(1000, counter.getCountIterations());
|
||||
assertEquals(Solutions.bestOf(vehicleRoutingProblemSolutions).getCost(), bestSolutionCost, 0.001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSettingPrematureTermination_itIsExecutedCorrectly() {
|
||||
SearchStrategyManager stratManager = mock(SearchStrategyManager.class);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue