我正在尝试解决每个客户都有多个位置的车辆路线,其中只需要访问一个位置。我获得了optaplanner-master并以下列方式修改了车辆路径示例:
Customer.java:
/*
* Copyright 2012 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.optaplanner.examples.vehiclerouting.domain;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamInclude;
import org.optaplanner.core.api.domain.entity.PlanningEntity;
import org.optaplanner.core.api.domain.valuerange.CountableValueRange;
import org.optaplanner.core.api.domain.valuerange.ValueRangeFactory;
import org.optaplanner.core.api.domain.valuerange.ValueRangeProvider;
import org.optaplanner.core.api.domain.variable.AnchorShadowVariable;
import org.optaplanner.core.api.domain.variable.PlanningVariable;
import org.optaplanner.core.api.domain.variable.PlanningVariableGraphType;
import org.optaplanner.examples.common.domain.AbstractPersistable;
import org.optaplanner.examples.vehiclerouting.domain.location.Location;
import org.optaplanner.examples.vehiclerouting.domain.solver.DepotAngleCustomerDifficultyWeightFactory;
import org.optaplanner.examples.vehiclerouting.domain.timewindowed.TimeWindowedCustomer;
import java.util.List;
@PlanningEntity(difficultyWeightFactoryClass = DepotAngleCustomerDifficultyWeightFactory.class)
@XStreamAlias("VrpCustomer")
@XStreamInclude({
TimeWindowedCustomer.class
})
public class Customer extends AbstractPersistable implements Standstill {
protected int demand;
// Planning variables: changes during planning, between score calculations.
protected Standstill previousStandstill;
// Shadow variables
protected Customer nextCustomer;
protected Vehicle vehicle;
protected List<Location> locations;
protected Integer selectedLocation = 0;
public void setSelectedLocation(Integer selectedLocation){
this.selectedLocation = selectedLocation;
}
@PlanningVariable(valueRangeProviderRefs = {"selectedLocation"})
public Integer getSelectedLocation(){
return selectedLocation;
}
@ValueRangeProvider(id = "selectedLocation")
public CountableValueRange<Integer> getSelectableLocations(){
return ValueRangeFactory.createIntValueRange(0, locations.size());
}
public void setLocations(List<Location> locations) {
this.locations = locations;
}
public List<Location> getLocations(){
return locations;
}
@Override
public Location getLocation() {
return locations.get(selectedLocation);
}
@Override
public String toString() {
return "Customer " + getId();
}
public int getDemand() {
return demand;
}
public void setDemand(int demand) {
this.demand = demand;
}
@PlanningVariable(valueRangeProviderRefs = {"vehicleRange", "customerRange"},
graphType = PlanningVariableGraphType.CHAINED)
public Standstill getPreviousStandstill() {
return previousStandstill;
}
public void setPreviousStandstill(Standstill previousStandstill) {
this.previousStandstill = previousStandstill;
}
@Override
public Customer getNextCustomer() {
return nextCustomer;
}
@Override
public void setNextCustomer(Customer nextCustomer) {
this.nextCustomer = nextCustomer;
}
@Override
@AnchorShadowVariable(sourceVariableName = "previousStandstill")
public Vehicle getVehicle() {
return vehicle;
}
public void setVehicle(Vehicle vehicle) {
this.vehicle = vehicle;
}
// ************************************************************************
// Complex methods
// ************************************************************************
/**
* @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors
*/
public long getDistanceFromPreviousStandstill() {
if (previousStandstill == null) {
throw new IllegalStateException("This method must not be called when the previousStandstill ("
+ previousStandstill + ") is not initialized yet.");
}
return getDistanceFrom(previousStandstill);
}
/**
* @param standstill never null
* @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors
*/
public long getDistanceFrom(Standstill standstill) {
return standstill.getLocation().getDistanceTo(getLocation());
}
/**
* @param standstill never null
* @return a positive number, the distance multiplied by 1000 to avoid floating point arithmetic rounding errors
*/
public long getDistanceTo(Standstill standstill) {
return getLocation().getDistanceTo(standstill.getLocation());
}
}
在VehicleRoutingImporter.java中,读取客户位置的代码已更改为:
List<Location> locations = new ArrayList<>(lineTokens.length-2);
for (int j = 2; j < lineTokens.length; ++j){
long locationId = Long.parseLong(lineTokens[j]);
Location location = locationMap.get(locationId);
if (location == null)
throw new IllegalArgumentException("Missing location (id=" + locationId + ") specified for customer id=" + id);
locations.add(location)
}
customer.setLocations(locations);
以前调用Customer.setLocation(location)的所有其他代码现在调用Customer.setLocations(Collections.singletonList(location))。我认为其中任何一个都不会被调用。
在vehicleRoutingSolverConfig.xml中,我删除了这些行(否则,它会抱怨多个变量):
<subChainChangeMoveSelector>
<selectReversingMoveToo>true</selectReversingMoveToo>
</subChainChangeMoveSelector>
<subChainSwapMoveSelector>
<selectReversingMoveToo>true</selectReversingMoveToo>
</subChainSwapMoveSelector>
tutorial-01-uncapacitated.vrp现在是:
NAME : tutorial-01-uncapacitated
COMMENT : Geoffrey De Smet - OptaPlanner VRP demo 01
TYPE : CVRP
DIMENSION : 8
EDGE_WEIGHT_TYPE : EUC_2D
CAPACITY : 100
NODE_COORD_SECTION
1 50 50
2 45 100
3 30 80
4 70 85
5 60 60
6 35 10
7 30 30
8 45 20
DEMAND_SECTION
1 0
2 1 2 3
3 1 3 4
4 1 4 5
5 1 5 6
6 1 6 7
7 1 7 8
8 1 8 2
DEPOT_SECTION
1
-1
VEHICLES : 2
EOF
它似乎有效,并在小型测试案例中找到最佳解决方案(在大型案例中看起来像是一个合理的解决方案)。但是,当我设置FAST_ASSERT模式时,它会失败,并显示以下错误:
引起:java.lang.IllegalStateException:不可能的VariableListener损坏:在没有更改真实变量的情况下触发所有VariableListener后,expectedWorkingScore(0hard / -30000soft)不是workingScore(0hard / -34142soft)。 但是所有阴影变量值仍然相同,所以这是不可能的。