Or-tools: TypeError in SimpleMinCostFlow_AddArcWithCapacityAndUnitCost

Created on 10 Jan 2017  路  6Comments  路  Source: google/or-tools

I follow the examples in this page "https://developers.google.com/optimization/assignment/assignment_min_cost_flow", but when I try to write my own code, I am confused by the following error.

Followed by the code:

for i in range(len(start_nodes)):
    min_cost_flow.AddArcWithCapacityAndUnitCost(start_nodes[i], end_nodes[i],
                                                capacities[i], costs[i])

I wrote my own code:

for i, word in enumerate(word_to_id.keys()):
    min_cost_flow.AddArcWithCapacityAndUnitCost(0, i+1, 1, 0)
    min_cost_flow.SetNodeSupply(i+1, 0)

It raised the error:

TypeError: in method 'SimpleMinCostFlow_AddArcWithCapacityAndUnitCost', argument 3 of type 'operations_research::NodeIndex'

What does it mean? I don't know where it's wrong? How to solve it ? Thanks

Bug Python

Most helpful comment

I had the same issue. It was being caused by the types of the values returned by a call numpy.where. Before adding indices to the start_nodes list, I had to cast the indices to python ints (rather than numpy ints).

All 6 comments

Can you show your entire script? it's hard to see exactly how you defined your node data

It has been solved. Thank you!

@YisenWang Could you be so kind to share your solution, just stumbled across the same error while starting with ortools.
Thank you

I had the same issue. It was being caused by the types of the values returned by a call numpy.where. Before adding indices to the start_nodes list, I had to cast the indices to python ints (rather than numpy ints).

I just confirmed this issue when using integers from a numpy array, even if reformatted to list. Specific to Python 3.6.3. Casting to int (i.e. int(x)) resolves the problem. This error does not appear when running the exact code in 2.7.14, even without the re-casting.

You need to cast the inputs to integers, but it seems that casting them to numpy ints does not work... I casted with python ints and it worked well:

capacities = [int(i) for i in capacities]

costs = [int(i) for i in costs]

and so on...

Was this page helpful?
0 / 5 - 0 ratings