Search code examples
routesoptaplannerchained

Require information on superclass/interface for Chained variable


For Vehicle Routing problem, I removed the interface (i.e. Standstill) and modified Solver Configuration (i.e. vehicleRoutingSolverConfig.xml) to replace Standstill entityClass with Vehicle entityClass. Also made other changes to the code.

However, I get the following exception while trying to run the Solver.

Exception in thread "main" java.lang.IllegalArgumentException: The entityClass (class org.optaplanner.examples.vehiclerouting.domain.Customer) has a PlanningVariable annotated property (previousVehicle) with chained (true) and propertyType (class org.optaplanner.examples.vehiclerouting.domain.Vehicle) which is not a superclass/interface of or the same as the entityClass (class org.optaplanner.examples.vehiclerouting.domain.Customer).

I'm trying to understand, why optaPlanner requires chain variable (i.e of type Vehicle) referenced in another Entity class (i.e. of type Customer) to have same superclass/interface.


Solution

  • See the docs about chained planning vars. There's a number of build-in specs for chained vars:

    • Each chain has exactly 1 anchor and 0 to n entities (in order).
    • Each entity points to the previous entity xor the anchor, so the chain is modeled as a linked list.

    Apply those 2 specs and it turns out that entity (such as Customer) and anchor (such as Vehicle) need a common interface (such as Standstill), because otherwise what type would the Entity.getPreviousInChain() planning variable return? It can be either.

    As for the error you're getting: it says that in that model, it's impossible for an entity to point to another entity, so it's impossible to have chains longer than 1 entity...