Vikas Agrawal
Vikas Agrawal

Reputation: 61

Optaplanner: Vehicle Routing – order of precedence while visiting cities

Suppose that you have a car that is required to visit cities A, B, C, D and E in the shortest possible time or distance. But there is order of precedence in which these cities can be visited. For e.g., B must be visited first before you visit “A,” and “E” must be visited first before you can visit “C.” So all of the following solutions are valid: Car -> B, D, E, A, C Or Car -> D, E, B, A, C Or Car -> E, B, A, D, C Following routes will be invalid: Car -> A, B, D, E, C (Constraint violated since B must be visited first before you can visit A) Or Car -> B, D, A, C, E (Constraint violated since E must be visited first before you can visit C)

In Optaplanner, is there any way to enforce such constraints? I think this has to be done while forming a chain. The default chain might have to be manually revisited to enforce such a constraint. But I don’t know how. Any pointers will be greatly appreciated. Vikas

Upvotes: 2

Views: 241

Answers (1)

Geoffrey De Smet
Geoffrey De Smet

Reputation: 27312

Introduce a shadow variable that keeps the chainIndex in a chain of a vehicles.

For example: Vehicle A starts in Brussels then goes to Paris then to London. Vehicle B starts in Brussels then goes to Berlin and then to Prague. Then it works like this:

  • Paris's anchor is A and its chainIndex is 1.
  • London's anchor is A and its chainIndex is 2.
  • Berlin's anchor is B and its chainIndex is 1.
  • Prague's anchor is B and its chainIndex is 2.

Adding your constraint by using that chainIndex and the anchor is then trivial.

Upvotes: 1

Related Questions