如何定义固定点和活动点之间的距离

时间:2018-10-18 02:48:22

标签: python distance minimize

enter image description here来自未来导入print_function     从ortools.linear_solver导入pywraplp

import numpy
import math
class Point(object):
    def __init__(self, x, y):
        self.x = x
        self.y = y

def square(x):
    return x**2
def distance(P1,P2):
    L = math.sqrt(square(P1.x-P2.x)+square(P1.y-P2.y))
    return L


def main():
  # Instantiate a mixed-integer solver, naming it SolveIntegerProblem.
  solver = pywraplp.Solver('SolveIntegerProblem',
                           pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)


  # x and y are integer non-negative variables.
  x3 = solver.IntVar(12600, 21000, 'x3')
  x4 = solver.IntVar(21000, 29400, 'x4')
  x2 = solver.IntVar(12600, 42000, 'x2')

  #import data
  P1 = Point(42000,8400)
  P2 = Point(42000,16800)
  P3 = Point(12600,8400)
  P4 = Point(29400,33600)
  P5 = Point(29400,42000)
  P6 = Point(12600,42000)

  D3 = Point(x3,33600)
  D4 = Point(x4,33600)
  D2 = Point(x2,8400)

  #Minmize total_distance####unsupported operand type(s) for ** or pow(): 'SumArray' and 'int'
  total_distance = distance(P1,D2)+distance(P2,D2)+distance(P4,D2)+distance(D3,D2)+distance(D4,D2)+distance(P6,D3)+distance(P5,D4)

#######TypeError(unsupported operand type(s) for ** or pow(): 'SumArray' and 'int') in this line

  # Maximize x + 10 * y.
  objective = solver.Objective()
  objective.SetMaximization(total_distance)

  """Solve the problem and print the solution."""
  result_status = solver.Solve()
  # The problem has an optimal solution.
  assert result_status == pywraplp.Solver.OPTIMAL

  # The solution looks legit (when using solvers other than
  # GLOP_LINEAR_PROGRAMMING, verifying the solution is highly recommended!).
  assert solver.VerifySolution(1e-7, True)

  print('Number of variables =', solver.NumVariables())
  print('Number of constraints =', solver.NumConstraints())

  # The objective value of the solution.
  print('Optimal objective value = %d' % solver.Objective().Value())
  print()
  # The value of each variable in the solution.
  variable_list = [x, y]

  for variable in variable_list:
    print('%s = %d' % (variable.name(), variable.solution_value()))

if __name__ == '__main__':
  main()`

P1 = Point(42000,8400),x2 =求解器.IntVar(12600,42000,'x2'),D2 = Point(x2,8400)

如何定义P1到D2之间的距离?在我的代码中,当我运行函数距离时,它会在此行中变为TypeError(**或pow()不支持的操作数类型:“ SumArray”和“ int”)

我是一位新的有关运筹学的python学习者,有人可以一起聊天吗?

1 个答案:

答案 0 :(得分:0)

有同样的问题。我认为这是因为距离函数(以及目标函数)不是线性的,而CBC Solver只能求解线性函数。您可能需要使用Gurobi Solver之类的工具。