0

Hi OptaPlanner experts,

I'm new to OptaPlanner and I ask you some help.

As far as I find out from this thread that in the OptaPlanner's CVRPTW example (version CR4), the entity Vehicle (here) represents a vehicle's trip (rather than a vehicle), I would like to ask how to manage a real situation, that is with a limited number of vehicles that cannot have overlapping trips.

In the mentioned thread the suggestion was to replace entity Vehicle with VehicleTrip, and create a new Vehicle entity. I suppose the new Vehicle has a list of VehicleTrip objects (since a vehicle can do several trips in a day - e.g.). But how to configure the planner in order to minimize the number of VehicleTrip s per Vehicle?

How should the example need to be modified? (I don't care about Swing part, only the model and the planner's configuration - listeners/DRL/XML)

EDIT

I modified the model as described above and created the method int getOverlappingTime() in class VrpTimeWindowedVehicleRoutingSolution, that returns the the quantity of overlapping time of different VehicleTrips of a same Vehicle. Then I created the following rule:

rule "vehicleTripsOverlapping"
    when
        $solution : VrpTimeWindowedVehicleRoutingSolution()
    then
        scoreHolder.addHardConstraintMatch(kcontext, - $solution.getOverlappingTime());
end

The method works correctly (tested), but the score doesn't change (it seems the rule is not fired). Is there anybody who can help me please?

Community
  • 1
  • 1
user2664655
  • 251
  • 1
  • 3
  • 9
  • The solution itself (`VrpTimeWindowedVehicleRoutingSolution`) isn't inserted into the Drools working memory, so that's why that EDIT won't work. In any case, that `solution.getOverlappingTime()` is not incremental (see docs) so it's not scalable so it's very slow when scaling out. This is basically the reason why optaplanner doesn't bother inserting the solution (as it's an anti-pattern). – Geoffrey De Smet Oct 16 '13 at 13:06

1 Answers1

0

how to configure the planner in order to minimize the number of VehicleTrip s per Vehicle?

Use the squaring trick:

when
  $v : Vehicle
  $count : Number() from accumulate (VehicleTrip(vehicle == $v), count())
then
  scoreHolder.addSoftConstraintMatch(kcontext, - $count * $count);
end
Geoffrey De Smet
  • 26,223
  • 11
  • 73
  • 120
  • See [these docs to explain the squaring trick](https://github.com/droolsjbpm/optaplanner/commit/6937fde178274766b0b9b2be06fa236feab405d3). – Geoffrey De Smet Oct 17 '13 at 11:28
  • This rule is not triggered.. I'm lost. Moreover I changed the rule to: `rule "vehicleTripsPerVehicle" when $v : VrpVehicle() $count : Integer() from accumulate (VrpVehicleTrip(vehicle.equals($v)), count()) then scoreHolder.addSoftConstraintMatch(kcontext, - $count * $count); end` because it was producing syntax errors around "accumulate from".. Any hint? – user2664655 Oct 17 '13 at 18:20
  • I think I'm using a wrong model. The rule is not firing because the class `Vehicle` needs to be a PlanningEntity. It would be necessary to use much more space than these few characters to explain this issue... – user2664655 Oct 17 '13 at 19:04