1
vehicle 1 vehicle 2 vehicle 3 vehicle 4
node 1 1 1 0 0
node 2 0 1 1 0
node 3 0 0 1 1

As could be seen from the matrix, a node could only be served by a specified list of vehicles. Node 1 should be served by vehicle 1 or vehicle 2, but not vehicle 3 or vehicle 4.

I tried to modify the solution proposed here (In or-tools, VRPTW, how can I give each vehicle a different weight/score for each node?), but it didn't work. Is there any way to add this type of constraint to VRPTW.

heman33
  • 25
  • 4
  • Hi and welcome to StackOverflow! Could you share exactly what you tried? How you modified the code in the other question you provided? – Ftagliacarne Jan 18 '23 at 08:20

2 Answers2

3
  1. you can use routing.VehicleVar(node_index).RemoveValues([list of vehicle to remove])

e.g. for node 1

node_one_idx = manager.NodeToIndex(1)
routing.VehicleVar(node_one_idx).RemoveValues([3, 4]) # forbidd vehicle 3 and 4

note: vehicle index start at 0 so 3rd and 4th vehicle should be vehicle index 2 and 3...

ref: https://github.com/google/or-tools/blob/5a3b2f304438dad72b3a877b26e9cf2d9cf6f8a2/ortools/constraint_solver/routing.h#L1499-L1501

Mizux
  • 8,222
  • 7
  • 32
  • 48
  • Thanks for the response, It is very helpful. I could iterate through each location and remove those vehicle for each node. I also have a follow up questions regarding the specification of time window constraints. I am wondering why the start and end of time windows have to be integers (7 or 11)? There could be case where a time window is between 7:30am-11:30am, which could be 7.5-11.5. – heman33 Jan 19 '23 at 15:12
  • because the solver is using integer (`int64_t`) internally, if you want a 30min you can use 1 unit == 30min aka `7H30 -> 15`, and `11h30 -> 23`. Tips: try to keep resolution as the lowest possible. – Mizux Jan 20 '23 at 16:27
3

There is also SetAllowedVehiclesForIndex.

  /// Sets the vehicles which can visit a given node. If the node is in a
  /// disjunction, this will not prevent it from being unperformed.
  /// Specifying an empty vector of vehicles has no effect (all vehicles
  /// will be allowed to visit the node).
  void SetAllowedVehiclesForIndex(const std::vector<int>& vehicles,
                                  int64_t index);

Example use in py:

routing.SetAllowedVehiclesForIndex([0, 1], manager.NodeToIndex(1))
watchdogs132
  • 185
  • 1
  • 1
  • 5