我不知道我的问题是否可能。我正在使用ortools来解决优化问题,我知道在条件部分中,参数应该以double类型定义,如下所示:
constraints[i] = solver.Constraint(0.0 , 10,0)
但我的问题是,我不想在创造条件时使用这种类型的论证。例如,我想要一个清单。
所以我在我的代码中写了这个:
constraints[i] = solver.Constraint([1,2,3,...])
我收到了这个错误:
return _pywraplp.Solver_Constraint(self, *args)
NotImplementedError: Wrong number or type of arguments for overloaded
function 'Solver_Constraint'.
Possible C/C++ prototypes are:
operations_research::MPSolver::MakeRowConstraint(double,double)
operations_research::MPSolver::MakeRowConstraint()
operations_research::MPSolver::MakeRowConstraint(double,double,std::string
const &)
operations_research::MPSolver::MakeRowConstraint(std::string const &)
有没有办法改变条件的论证类型?
答案 0 :(得分:0)
[1, 2, 3]
和[4, 5, 6]
,它们的总和将是 wise ,st [1, 2, 3] + [4, 5, 6]
= [1+4, 2+5, 3+6]
= [5, 7, 9]
。[x1, x2, x3] <= [1, 2, 3]
表示x1 <= 1
,x2 <= 2
和x3 <= 3
。问题是,ortools
仅允许您将标量值(如数字)设置为变量;可以这么说,您不能创建“列表变量”。
因此,您必须制作一个有效表示同一事物的标量变量列表。
例如,假设您希望“列表变量”是一个值列表,每个值都受到存储在列表中的特定约束的约束。假设您有一个上限列表:
upper_bounds = [1, 2, 3, ..., n]
并且您有一些求解器变量列表,如下所示:
vars1 = [
# variable bounds here are chosen arbitrarily; set them to your purposes
solver.NumVar(0, solver.infinity, 'x{0}'.format(i))
for i in range(n)
]
vars2 = [...] # you define any other variable lists in the same way
然后,您将创建约束对象列表,列表中的每个上限都有一个约束:
constraints = [
solver.Constraint(0, ubound)
for ubound in upper_bounds
]
然后将变量插入约束中,但这取决于您的问题:
# Example expression: X1 - X2 + 0.5*X3 < UBOUND
for i in range(n):
constraints[i].SetCoefficient(vars1[i], 1)
constraints[i].SetCoefficient(vars2[i], -1)
constraints[i].SetCoefficient(vars3[i], 0.5)
希望这会有所帮助!我建议您(如果已有的话)再看一下特定求解器的示例。可以找到here。用于GLOP的那个。