введите описание изображения здесь из будущее импорт 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 = точка (42000,8400), x2 = решатель.IntVar (12600, 42000, 'x2') , D2 = Точка (x2,8400)
Как определить расстояние между P1 до D2?В моем коде, когда я запускаю расстояние до функции, он превращает TypeError (неподдерживаемые типы операндов для ** или pow (): 'SumArray' и 'int') в эту строку
Я новыйученик питона об исследовании операций, есть ли кто-нибудь, чтобы поговорить вместе?