Pyomo中存在和forall量词的复杂约束



如果我有这样的约束:

For all a in A there exists a b in B so that for all c in C it holds 
that a variable x(a, b + c) is equal to a parameter m(a, c)

短:

forall a in A, exists b in B, forall c in C: x(a,b+c)=m(a,c)

我尝试创建约束如下:

constr = lambda model, a: any(all(model.x[a, b + c] <= model.m[a, c] for c in model.C[a]) for b in model.B if b + max(model.C[a]) <= max(model.B))
model.Constr = pyo.Constraint(model.A, rule = constr))

变量和参数如下:如何在pyomo中定义代表不同大小的向量和集合的参数?:

对于m.data
  • model.m[a, c]的构造如图所示,但具有二进制值
  • model.A对应m.nodes,键为整数(some_vector键)
  • model.Bpyo.Set(initialize=list(range(1000)))
  • model.C对应m.elements
  • model.xispyo.Var(model.A, model.B, domain = pyo.Binary)

目前我收到以下错误:

ERROR: evaluating object as numeric value: x[0,0]
(object: <class 'pyomo.core.base.var._GeneralVarData'>)
No value for uninitialized NumericValue object x[0,0]
ERROR: Rule failed when generating expression for constraint Constr2 with
index 0: ValueError: No value for uninitialized NumericValue object x[0,0]
ERROR: Constructing component 'Constr2' from data=None failed: ValueError: No
value for uninitialized NumericValue object x[0,0]
Traceback (most recent call last):
File "gadget_chain.py", line 134, in <module>
op.satisfiability_check(mgcm)
File "optimise_payload.py", line 70, in satisfiability_check
model.Constr2 = pyo.Constraint(model.Nodes, rule = constr2)
File "/usr/lib/python3.10/site-packages/pyomo/core/base/block.py", line 544, in __setattr__
self.add_component(name, val)
File "/usr/lib/python3.10/site-packages/pyomo/core/base/block.py", line 1089, in add_component
val.construct(data)
File "/usr/lib/python3.10/site-packages/pyomo/core/base/constraint.py", line 836, in construct
index, self.rule(block, index)
File "/usr/lib/python3.10/site-packages/pyomo/core/base/util.py", line 304, in __call__
return self._fcn(parent, idx)
File "optimise_payload.py", line 69, in <lambda>
any(all(model.x[node, addr + reg] == model.NodeRegisterUsage[node, reg] for reg in model.RegisterIndices[node]) for addr in model.Payload if addr + max(model.RegisterIndices[node]) <= max(model.Payload))
File "optimise_payload.py", line 69, in <genexpr>
any(all(model.x[node, addr + reg] == model.NodeRegisterUsage[node, reg] for reg in model.RegisterIndices[node]) for addr in model.Payload if addr + max(model.RegisterIndices[node]) <= max(model.Payload))
File "pyomo/core/expr/logical_expr.pyx", line 304, in pyomo.core.expr.logical_expr.EqualityExpression.__nonzero__
File "pyomo/core/expr/numeric_expr.pyx", line 218, in pyomo.core.expr.numeric_expr.ExpressionBase.__call__
File "/usr/lib/python3.10/site-packages/pyomo/core/expr/visitor.py", line 1054, in evaluate_expression
return visitor.dfs_postorder_stack(exp)
File "/usr/lib/python3.10/site-packages/pyomo/core/expr/visitor.py", line 584, in dfs_postorder_stack
flag, value = self.visiting_potential_leaf(_sub)
File "/usr/lib/python3.10/site-packages/pyomo/core/expr/visitor.py", line 962, in visiting_potential_leaf
return True, value(node)
File "pyomo/core/expr/numvalue.pyx", line 246, in pyomo.core.expr.numvalue.value
File "pyomo/core/expr/numvalue.pyx", line 233, in pyomo.core.expr.numvalue.value
ValueError: No value for uninitialized NumericValue object x[0,0]

在这里参数的命名不同。我检查了彼此独立的约束,无论我是否评论其他约束,错误都保持不变。因此,误差必须与此约束相对应。

在一般情况下,也可以自由地提出在Pyomo中创建约束的更有效的方法。我对这个软件完全是个初学者。

forall a in A, exists b in B, forall c in C: x(a,b+c)=m(a,c)

我们遗漏了很多细节,我对你的非标准描述有点困惑,但我猜你的意思是:

x[a,1+c] = m[a,c] or x[a,2+c] = m[a,c] or ...   ∀a,c

对于最后一个c,这没有意义。所以不确定你想做什么。

这些or条件可以用Pyomo中的析取或使用二进制或SOS1变量来处理。

相关内容

  • 没有找到相关文章

最新更新