mirror of
https://github.com/graphhopper/jsprit.git
synced 2020-01-24 07:45:05 +01:00
simplify to get strategies and their weight
This commit is contained in:
parent
73021253de
commit
349364dce6
5 changed files with 27 additions and 17 deletions
|
|
@ -33,15 +33,18 @@ import java.util.Collections;
|
|||
public class SearchStrategy {
|
||||
|
||||
public static class DiscoveredSolution {
|
||||
private VehicleRoutingProblemSolution solution;
|
||||
private boolean accepted;
|
||||
private String strategyName;
|
||||
|
||||
public DiscoveredSolution(VehicleRoutingProblemSolution solution,boolean accepted, String strategyName) {
|
||||
private VehicleRoutingProblemSolution solution;
|
||||
|
||||
private boolean accepted;
|
||||
|
||||
private String strategyId;
|
||||
|
||||
public DiscoveredSolution(VehicleRoutingProblemSolution solution, boolean accepted, String strategyId) {
|
||||
super();
|
||||
this.solution = solution;
|
||||
this.accepted = accepted;
|
||||
this.strategyName = strategyName;
|
||||
this.strategyId = strategyId;
|
||||
}
|
||||
|
||||
public VehicleRoutingProblemSolution getSolution() {
|
||||
|
|
@ -52,11 +55,13 @@ public class SearchStrategy {
|
|||
return accepted;
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
@Deprecated
|
||||
public String getStrategyName() {
|
||||
return strategyName;
|
||||
return strategyId;
|
||||
}
|
||||
|
||||
public String getStrategyId() { return strategyId; }
|
||||
|
||||
}
|
||||
|
||||
private static Logger logger = LogManager.getLogger(SearchStrategy.class);
|
||||
|
|
@ -137,7 +142,7 @@ public class SearchStrategy {
|
|||
double costs = solutionCostCalculator.getCosts(lastSolution);
|
||||
lastSolution.setCost(costs);
|
||||
boolean solutionAccepted = solutionAcceptor.acceptSolution(solutions, lastSolution);
|
||||
return new DiscoveredSolution(lastSolution, solutionAccepted, getName());
|
||||
return new DiscoveredSolution(lastSolution, solutionAccepted, getId());
|
||||
}
|
||||
|
||||
private String getErrMsg() {
|
||||
|
|
|
|||
|
|
@ -43,22 +43,27 @@ public class SearchStrategyManager {
|
|||
this.random = random;
|
||||
}
|
||||
|
||||
|
||||
public List<SearchStrategy> getStrategies() {
|
||||
return Collections.unmodifiableList(strategies);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the probabilities.
|
||||
* [schroeder (2014.11.21): Now they are actually no propabilities anymore but weights. The resulting probabilities
|
||||
* are calculated here with the sum of weights]
|
||||
* @return list of probabilities
|
||||
*/
|
||||
@Deprecated
|
||||
public List<Double> getProbabilities() {
|
||||
return Collections.unmodifiableList(weights);
|
||||
}
|
||||
|
||||
public List<Double> getWeights(){ return Collections.unmodifiableList(weights); }
|
||||
|
||||
public double getWeight(String strategyId){
|
||||
return weights.get(id2index.get(strategyId));
|
||||
}
|
||||
|
||||
/**
|
||||
* adds a new search strategy with a certain weight.
|
||||
* @param strategy strategy to be added
|
||||
|
|
|
|||
|
|
@ -205,7 +205,7 @@ public class VehicleRoutingAlgorithm {
|
|||
SearchStrategy strategy = searchStrategyManager.getRandomStrategy();
|
||||
DiscoveredSolution discoveredSolution = strategy.run(problem, solutions);
|
||||
memorizeIfBestEver(discoveredSolution);
|
||||
selectedStrategy(strategy.getId(),searchStrategyManager,problem, solutions);
|
||||
selectedStrategy(discoveredSolution,problem,solutions);
|
||||
if(terminationManager.isPrematureBreak(discoveredSolution)){
|
||||
logger.info("premature algorithm termination at iteration "+ (i+1));
|
||||
noIterationsThisAlgoIsRunning = (i+1);
|
||||
|
|
@ -233,8 +233,8 @@ public class VehicleRoutingAlgorithm {
|
|||
}
|
||||
|
||||
|
||||
private void selectedStrategy(String name, SearchStrategyManager searchStrategyManager, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
algoListeners.selectedStrategy(name, searchStrategyManager, problem, solutions);
|
||||
private void selectedStrategy(DiscoveredSolution discoveredSolution, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
algoListeners.selectedStrategy(discoveredSolution,problem,solutions);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@
|
|||
******************************************************************************/
|
||||
package jsprit.core.algorithm.listener;
|
||||
|
||||
import jsprit.core.algorithm.SearchStrategyManager;
|
||||
import jsprit.core.algorithm.SearchStrategy;
|
||||
import jsprit.core.problem.VehicleRoutingProblem;
|
||||
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
||||
|
||||
|
|
@ -27,6 +27,6 @@ import java.util.Collection;
|
|||
|
||||
public interface StrategySelectedListener extends VehicleRoutingAlgorithmListener{
|
||||
|
||||
void informSelectedStrategy(String strategyId, SearchStrategyManager searchStrategyManager, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions);
|
||||
void informSelectedStrategy(SearchStrategy.DiscoveredSolution discoveredSolution, VehicleRoutingProblem vehicleRoutingProblem, Collection<VehicleRoutingProblemSolution> vehicleRoutingProblemSolutions);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@
|
|||
******************************************************************************/
|
||||
package jsprit.core.algorithm.listener;
|
||||
|
||||
import jsprit.core.algorithm.SearchStrategyManager;
|
||||
import jsprit.core.algorithm.SearchStrategy;
|
||||
import jsprit.core.algorithm.VehicleRoutingAlgorithm;
|
||||
import jsprit.core.problem.VehicleRoutingProblem;
|
||||
import jsprit.core.problem.solution.VehicleRoutingProblemSolution;
|
||||
|
|
@ -172,10 +172,10 @@ public class VehicleRoutingAlgorithmListeners {
|
|||
}
|
||||
}
|
||||
|
||||
public void selectedStrategy(String name, SearchStrategyManager searchStrategyManager, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
public void selectedStrategy(SearchStrategy.DiscoveredSolution discoveredSolution, VehicleRoutingProblem problem, Collection<VehicleRoutingProblemSolution> solutions) {
|
||||
for(PrioritizedVRAListener l : algorithmListeners){
|
||||
if(l.getListener() instanceof StrategySelectedListener){
|
||||
((StrategySelectedListener)l.getListener()).informSelectedStrategy(name, searchStrategyManager, problem, solutions);
|
||||
((StrategySelectedListener)l.getListener()).informSelectedStrategy(discoveredSolution, problem, solutions);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue