Skip to content

Commit

Permalink
cont vrp listener
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerbrandl committed Apr 8, 2023
1 parent 68b36e0 commit 74d451d
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.kalasim.examples.taxiinc.vehiclerouting;

import org.kalasim.examples.taxiinc.vehiclerouting.domain.Customer;
import org.kalasim.examples.taxiinc.vehiclerouting.domain.VehicleRoutingSolution;
import org.kalasim.examples.taxiinc.vehiclerouting.domain.timewindowed.TimeWindowedCustomer;
import org.kalasim.examples.taxiinc.vehiclerouting.domain.timewindowed.TimeWindowedDepot;
import org.optaplanner.core.api.domain.variable.VariableListener;
import org.optaplanner.core.api.score.director.ScoreDirector;

import java.util.Objects;

// TODO When this class is added only for TimeWindowedCustomer, use TimeWindowedCustomer instead of Customer
public class SimpleVRPVariableListener implements VariableListener<VehicleRoutingSolution, Customer> {

@Override
public void beforeEntityAdded(ScoreDirector<VehicleRoutingSolution> scoreDirector, Customer customer) {
// Do nothing
}

@Override
public void afterEntityAdded(ScoreDirector<VehicleRoutingSolution> scoreDirector, Customer customer) {
updateArrivalTime(scoreDirector, customer);
}

@Override
public void beforeVariableChanged(ScoreDirector<VehicleRoutingSolution> scoreDirector, Customer customer) {
// Do nothing
}

@Override
public void afterVariableChanged(ScoreDirector<VehicleRoutingSolution> scoreDirector, Customer customer) {
updateArrivalTime(scoreDirector, customer);
}

@Override
public void beforeEntityRemoved(ScoreDirector<VehicleRoutingSolution> scoreDirector, Customer customer) {
// Do nothing
}

@Override
public void afterEntityRemoved(ScoreDirector<VehicleRoutingSolution> scoreDirector, Customer customer) {
// Do nothing
}

protected void updateArrivalTime(ScoreDirector<VehicleRoutingSolution> scoreDirector,
Customer customer) {

if (customer.getVehicle() == null) {
if (customer.routePostion != null) {
scoreDirector.beforeVariableChanged(customer, "routePostion");
customer.routePostion = null;
scoreDirector.afterVariableChanged(customer, "routePostion");
}

return;
}else{
customer.routePostion = 10;
}

Customer previousCustomer = customer.getPreviousCustomer();

// Long departureTime;
// if (previousCustomer == null) {
// departureTime = ((TimeWindowedDepot) customer.getVehicle().getDepot()).getReadyTime();
// } else {
// departureTime = ((TimeWindowedCustomer) previousCustomer).getDepartureTime();
// }
// Customer shadowCustomer = customer;
//
// Long arrivalTime = calculateArrivalTime(shadowCustomer, departureTime);
//
// while (shadowCustomer != null && !Objects.equals(shadowCustomer.getArrivalTime(), arrivalTime)) {
// scoreDirector.beforeVariableChanged(shadowCustomer, "arrivalTime");
// shadowCustomer.setArrivalTime(arrivalTime);
// scoreDirector.afterVariableChanged(shadowCustomer, "arrivalTime");
// departureTime = shadowCustomer.getDepartureTime();
// shadowCustomer = (TimeWindowedCustomer) shadowCustomer.getNextCustomer();
// arrivalTime = calculateArrivalTime(shadowCustomer, departureTime);
// }
}

private Long calculateArrivalTime(TimeWindowedCustomer customer, Long previousDepartureTime) {
if (customer == null || previousDepartureTime == null) {
return null;
}
return previousDepartureTime + customer.getDistanceFromPreviousStandstill();
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.kalasim.examples.taxiinc.vehiclerouting.domain;

import org.kalasim.examples.taxiinc.vehiclerouting.ArrivalTimeUpdatingVariableListener;
import org.kalasim.examples.taxiinc.vehiclerouting.SimpleVRPVariableListener;
import org.kalasim.examples.taxiinc.vehiclerouting.domain.solver.DepotAngleCustomerDifficultyWeightFactory;
import org.optaplanner.core.api.domain.entity.PlanningEntity;
import org.optaplanner.core.api.domain.variable.InverseRelationShadowVariable;
import org.optaplanner.core.api.domain.variable.NextElementShadowVariable;
import org.optaplanner.core.api.domain.variable.PreviousElementShadowVariable;
import org.kalasim.examples.taxiinc.vehiclerouting.domain.location.Location;
import org.optaplanner.core.api.domain.variable.ShadowVariable;

@PlanningEntity(difficultyWeightFactoryClass = DepotAngleCustomerDifficultyWeightFactory.class)
public class Customer extends AbstractPersistable {
Expand Down Expand Up @@ -66,6 +69,9 @@ public Customer getNextCustomer() {
return nextCustomer;
}

@ShadowVariable(variableListenerClass = SimpleVRPVariableListener.class, sourceVariableName = "vehicle")
public Integer routePostion;

public void setNextCustomer(Customer nextCustomer) {
this.nextCustomer = nextCustomer;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.kalasim.examples.taxiinc.vehiclerouting

import org.jetbrains.kotlinx.dataframe.api.toDataFrame
import org.kalasim.examples.taxiinc.vehiclerouting.domain.Customer
import org.kalasim.examples.taxiinc.vehiclerouting.domain.Depot
import org.kalasim.examples.taxiinc.vehiclerouting.domain.Vehicle
Expand Down Expand Up @@ -74,4 +75,5 @@ fun main() {
val solution = solver.solve(problem)

println(solution)
solution.vehicleList.toDataFrame()
}

0 comments on commit 74d451d

Please sign in to comment.