Search code examples
droolsoptaplanner

Optaplanner: Extend vehiclerouting example to include secure transport


I am want to add a simple boolean value to each vehicle that indicates if the vehicle is secure, then for each customer a similar boolean to indicate that the customer requires a secure vehicle.

I have got as far as working out I need to:

  • extend the domain objects for Vehicle and Customer to reflect these.
  • extend the input XML to have at least one secure Vehicle and one Customer requiring a secure collection.
  • extend the vehicleRoutingScoreRules.drl to add a new rule "secure" with a hard constraint.

This is a little pathetic - but I am pushed for time and the drools manual is massive and I cannot figure out the syntax for what I think is a simple rule. Could some kind soul give me a starter here please.


Solution

  • Got it working!

    I have done the following:

    • extend the domain objects for Vehicle and Customer to reflect these.

      //Vehicle.java
      protected boolean secure;
      public boolean isSecure() {return secure;}
      public void setSecure(boolean secure) {this.secure = secure;}
      
      //Customer.java
      protected boolean needsSecure;
      public boolean isNeedsSecure() {return needsSecure;}
      public void setNeedsSecure(boolean needsSecure) {this.needsSecure = needsSecure;}
      
    • extend the input XML to have at least one secure Vehicle and one Customer requiring a secure collection.

      //cvrptw-25customers-secure.xml - cloned from cvrptw-25customers.xml
      <VrpVehicle id="56">
        <id>24</id>
        <capacity>200</capacity>
        <depot class="VrpTimeWindowedDepot" reference="30"/>
        <secure>true</secure>
      </VrpVehicle>
      
      <VrpTimeWindowedCustomer id="58">
       <id>1</id>
       <location reference="4"/>
       <demand>10</demand>
       <readyTime>912000</readyTime>
       <dueTime>967000</dueTime>
       <serviceDuration>90000</serviceDuration>
       <needsSecure>true</needsSecure>
      </VrpTimeWindowedCustomer>
      
    • extend the vehicleRoutingScoreRules.drl to add a new rule "secure" with a hard constraint.

      rule "securePackage"
        when
          $customer : Customer(needsSecure == true, vehicle != null, vehicle.isSecure == false)
        then
          scoreHolder.addHardConstraintMatch(kcontext, -1);
      end
      

    And it all works! With 3 "secure" customers with a total need = the total secure vehicle capacity I can see that one vehicle (the secure one) does all 3 pickups and is full.