问题
I'd like to define a routing problem using RoutingModel
including nodes that are freed from the must-visit constraint, but have an influence on a capacity dimension if visited. Do you have any suggestions how to do this?
回答1:
You can use disjunction constraints to make your nodes optional. You should add each node to its own disjunction group, each having a single element:
for node in [0, 1, 2, 3, 4]:
routing.AddDisjunction(
nodes=[node],
penalty=1)
Why does this work?
In a disjunction group, the solver will try to include in the solution exactly max_cardinality
nodes from that group.
- If
penalty
is negative, this will be treated as a hard constraint, which means that the solver will not return a valid solution if the constraint is not satisfied. - However, if
penalty
is positive, this will be a soft constraint, meaning that a violation will add a penalty to the global cost variable.
Therefore, in order to minimize the penalties, the solver will attempt to include in the solution as many nodes as possible, while still being allowed to leave some of them out.
来源:https://stackoverflow.com/questions/53104283/defining-nodes-that-do-not-have-to-be-visited