Google OR-Tools - Routing - Penalties - Adding Different Penalty to Different Location (Python) I am using Google's OR-Tools for route optimisation. References can be found here.

I am performing an optimisation where certain pick-up locations are dropped based on a penalty at each location. The documentation gives an example and states: "In this context, a disjunction is simply a variable that the solver uses to decide whether to include a given location in the solution. In this example, the method adds the same penalty to each location, but in general you can add different penalties to different locations.", and provides the following chunk of code as an example of adding a constant penalty to each location:

penalty = 1000
for node in range(1, len(data['distance_matrix'])):
    routing.AddDisjunction([manager.NodeToIndex(node)], penalty)


where data is a dict containing a n x n distance matrix (distance_matrix). (This code runs and returns a sensible outcome).

Now, I would like to vary the penalty at each location, and I've used the following code to do so:

for node in range(1, len(data['distance_matrix'])):
    routing.AddDisjunction([manager.NodeToIndex(node)], data['penalty'][manager.NodeToIndex(node)])


where penalty is an array of length n containing penalties for each location. (In these examples, the depot is assumed to be at the first location and is penalty-free; range starts at 1).

When I run my code, I get the following error message:

TypeError: Wrong number or type of arguments for overloaded function 'RoutingModel_AddDisjunction'.
Possible C/C++ prototypes are:
operations_research::RoutingModel::AddDisjunction(std::vector&lt; int64 &gt; const &amp;,int64,int64)
operations_research::RoutingModel::AddDisjunction(std::vector&lt; int64 &gt; const &amp;,int64)
operations_research::RoutingModel::AddDisjunction(std::vector&lt; int64 &gt; const &amp;)


Question: What am I doing wrong? How do I go about adding different penalties to different locations? Could someone please translate the C/C++ into Python?
Given your code, it looks like data['penalty'][manager.NodeToIndex(node)] is not an integer, probably a float, and therefore it doesn't match the function signature, as the error message suggests. Try converting it to an integer.