1

我已经为*在我的图表中访问 1000 个节点的最佳路线“设置了一个很好的求解器。

但我想解决“访问我的图中 1000 个给定节点中的任何 500 个的最短路径是什么”这个问题。

我想,我必须以某种方式向我的 python添加析取约束RoutingModel,但是如何?

这是我当前求解器的粗略草图:

from ortools.constraint_solver import pywrapcp

nodes = readNodes()      # graph nodes
matrix = readMatrix()    # costs between edges, distances
assert len(nodes) == len(matrix)
# Create routing model
routing = pywrapcp.RoutingModel(len(nodes), 1)
parameters = ...
# Setting the cost function.
distance = lambda p,q: matrix[p][q]
routing.SetArcCostEvaluatorOfAllVehicles(distance)
# --> here is probably more setup needed <--
# Solve, returns a solution if any.
assignment = routing.SolveWithParameters(parameters, None)
if assignment:
  # Solution cost.
  print assignment.ObjectiveValue()
  # Inspect solution
  path = ...
  printSolution(nodes, table, path)
else:
  print 'No solution found.'
4

0 回答 0