1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

add multiple termination criteria

This commit is contained in:
oblonski 2014-09-14 20:09:09 +02:00
parent 89350be1e4
commit c75d3d5cc2
8 changed files with 334 additions and 42 deletions

View file

@ -39,6 +39,25 @@ import java.util.Collection;
*/
public class VehicleRoutingAlgorithm {
private static class TerminationManager implements PrematureAlgorithmTermination {
private Collection<PrematureAlgorithmTermination> terminationCriteria = new ArrayList<PrematureAlgorithmTermination>();
void addTermination(PrematureAlgorithmTermination termination){
terminationCriteria.add(termination);
}
@Override
public boolean isPrematureBreak(DiscoveredSolution discoveredSolution) {
for(PrematureAlgorithmTermination termination : terminationCriteria){
if(termination.isPrematureBreak(discoveredSolution)){
return true;
}
}
return false;
}
}
private static class Counter {
private final String name;
private long counter = 0;
@ -87,6 +106,8 @@ public class VehicleRoutingAlgorithm {
};
private TerminationManager terminationManager = new TerminationManager();
private VehicleRoutingProblemSolution bestEver = null;
public VehicleRoutingAlgorithm(VehicleRoutingProblem problem, SearchStrategyManager searchStrategyManager) {
@ -142,9 +163,15 @@ public class VehicleRoutingAlgorithm {
* @param prematureAlgorithmTermination the termination criterion
*/
public void setPrematureAlgorithmTermination(PrematureAlgorithmTermination prematureAlgorithmTermination){
this.prematureAlgorithmTermination = prematureAlgorithmTermination;
terminationManager = new TerminationManager();
terminationManager.addTermination(prematureAlgorithmTermination);
// this.prematureAlgorithmTermination = prematureAlgorithmTermination;
}
public void addTerminationCriterion(PrematureAlgorithmTermination terminationCriterion){
terminationManager.addTermination(terminationCriterion);
}
/**
* Gets the {@link SearchStrategyManager}.
*
@ -182,7 +209,8 @@ public class VehicleRoutingAlgorithm {
DiscoveredSolution discoveredSolution = strategy.run(problem, solutions);
memorizeIfBestEver(discoveredSolution);
selectedStrategy(strategy.getName(),problem, solutions);
if(prematureAlgorithmTermination.isPrematureBreak(discoveredSolution)){
// if(prematureAlgorithmTermination.isPrematureBreak(discoveredSolution)){
if(terminationManager.isPrematureBreak(discoveredSolution)){
logger.info("premature break at iteration "+ (i+1));
noIterationsThisAlgoIsRunning = (i+1);
break;

View file

@ -1,16 +1,16 @@
/*******************************************************************************
* 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 <http://www.gnu.org/licenses/>.
******************************************************************************/
@ -446,7 +446,7 @@ public class VehicleRoutingAlgorithms {
if(firstAct){
firstAct=false;
if(!vehicle.isReturnToDepot()){
assert activity.getLocationId() == end.getLocationId() : "route end and last activity are not equal even route is open. this should not be.";
assert activity.getLocationId().equals(end.getLocationId()) : "route end and last activity are not equal even route is open. this should not be.";
}
}
@ -594,11 +594,10 @@ public class VehicleRoutingAlgorithms {
//construct algorithm
VehicleRoutingAlgorithm metaAlgorithm = new VehicleRoutingAlgorithm(vrp, searchStratManager);
if(config.containsKey("iterations")){
int iter = config.getInt("iterations");
metaAlgorithm.setNuOfIterations(iter);
}
String maxIterationsString = config.getString("iterations");
if(maxIterationsString == null) maxIterationsString = config.getString("maxIterations");
if(maxIterationsString != null) metaAlgorithm.setMaxIterations(Integer.parseInt(maxIterationsString));
metaAlgorithm.getSearchStrategyManager().addSearchStrategyModuleListener(stateManager);
metaAlgorithm.getAlgorithmListeners().addListener(stateManager);

View file

@ -1,14 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com"
<!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ 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
~ 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 <http://www.gnu.org/licenses/>.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com" elementFormDefault="qualified">
<xs:element name="algorithm">
<xs:complexType>
<xs:sequence>
<xs:element name="iterations" type="xs:integer" minOccurs="0" maxOccurs="1" default="100"/>
<xs:element name="prematureBreak" type="prematureBreakType" minOccurs="0" maxOccurs="1"/>
<xs:choice>
<xs:element name="iterations" type="xs:integer" minOccurs="0" maxOccurs="1"/>
<xs:element name="maxIterations" type="xs:integer" minOccurs="0" maxOccurs="1"/>
</xs:choice>
<xs:choice>
<xs:element name="prematureBreak" type="prematureBreakType" minOccurs="0" maxOccurs="1"/>
<xs:element name="terminationCriteria">
<xs:complexType>
<xs:sequence>
<xs:element name="termination" type="prematureBreakType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
<xs:element name="construction" minOccurs="0" maxOccurs="1">
<xs:complexType>
<xs:sequence>
@ -19,7 +51,7 @@
<xs:element name="strategy">
<xs:complexType>
<xs:all>
<xs:sequence>
<xs:element name="memory" type="xs:integer" minOccurs="0" maxOccurs="1" default="1"/>
<xs:element name="searchStrategies" minOccurs="1" maxOccurs="1">
<xs:complexType>
@ -28,9 +60,10 @@
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:all>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
@ -126,7 +159,7 @@
<xs:sequence>
<xs:element name="threshold" type="xs:double" minOccurs="1" maxOccurs="1"/>
<xs:element name="iterations" type="xs:integer" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:sequence>
</xs:group>
<xs:complexType name="moduleType">
@ -148,7 +181,7 @@
<xs:sequence>
<xs:element name="ruin" type="ruinType"/>
<xs:element name="insertion" type="insertionType"/>
</xs:sequence>
</xs:sequence>
</xs:group>
<xs:group name="gendreau_group">