我是Pyomo的新手。在下面的代码部分中,我试图最小化一个函数,以满足另外两个约束。我有一个问题,特别是一个约束,我试图通过指定上界和下界来提供一个范围。字典的格式是dict[key]=[d,r,p];'这里不使用p。决策变量“x”是二进制的。目标是找出可供选择的项目(仅一个),以便“d”在“r”值范围内最小。“r”值的范围由“约束rxlambda\u约束规则\u上限”给出。因此,我们的目标是找出三个字典项(来自dict)中的项,这样“d”的值在“r”值范围内是最小的。我所理解的预期输出应该是第一项(正如我在这里所说的),如[2,0.8,0.3],r=0.8,小于0.9,大于0.5,在这个范围内,d=2是唯一的最小值。但是,我不明白这个错误的原因,我已经附上了错误消息和代码。我通过neos服务器使用“cplex”。
import pyomo.environ as pyo
from pyomo.opt import SolverFactory
from pyomo.core import *
from pyomo.environ import *
dict ={((1,2),(2,4)): [2, 0.8 ,0.3],((1,2),(2,3)): [3, 0.2 ,0.3], ((1,2),(2,5)): [3, 0.1,0.3] }
model = ConcreteModel()
model.I = Param(initialize =1)
model.J= Param(initialize = len(dict))
model.m = RangeSet(1,model.I)
model.n = RangeSet(1, model.J)
d_list =[]
r_list = []
for key in dict:
dict_value = dict.get(key)
d_list.append(dict_value[0])
r_list.append(dict_value[1])
d_dict ={}
r_dict={}
for i in range(len(dict)):
d_dict[i+1]=d_list[i]
r_dict[i+1]=r_list[i]
model.x = Var(model.n, within = Binary)
model.d = Param(model.n, initialize =d_dict )
model.r = Param(model.n, within=NonNegativeReals,initialize = r_dict)
def ObjRule(model):
return summation(model.d, model.x)
model.OBJ = Objective( rule =ObjRule ,sense = minimize)
def rxlambda_constraint_rule_upper(model,i):
return (0.5, model.r[i]*model.x[i], 0.9 )
model.rxlambdaConstraintUpper = Constraint(model.n, rule=rxlambda_constraint_rule_upper)
def sumx_constraint_rule(model,i):
value = sum(model.x[i] for i in model.n)
return value ==1
model.sumxConstraint = Constraint(model.n, rule = sumx_constraint_rule)
model.pprint()
instance = model.create()
opt = SolverFactory("cplex")
solver_manager = SolverManagerFactory('neos')
results = solver_manager.solve(instance, opt=opt, tee=True)
model.pprint()
instance.display()
for v in instance.component_objects(Var, active=True):
varobject = getattr(instance, str(v))
for index in varobject:
print (index, varobject[index].value)
######错误日志为:#####
2 RangeSet Declarations
m : Dim=0, Dimen=1, Size=1, Domain=Integers, Ordered=True, Bounds=(1, 1)
Virtual
n : Dim=0, Dimen=1, Size=3, Domain=Integers, Ordered=True, Bounds=(1, 3)
Virtual
4 Param Declarations
I : Size=1, Index=None, Domain=Any, Default=None, Mutable=False
Key : Value
None : 1
J : Size=1, Index=None, Domain=Any, Default=None, Mutable=False
Key : Value
None : 3
d : Size=3, Index=n, Domain=Any, Default=None, Mutable=False
Key : Value
1 : 2
2 : 3
3 : 3
r : Size=3, Index=n, Domain=NonNegativeReals, Default=None, Mutable=False
Key : Value
1 : 0.8
2 : 0.1
3 : 0.2
1 Var Declarations
x : Size=3, Index=n
Key : Lower : Value : Upper : Fixed : Stale : Domain
1 : 0 : None : 1 : False : True : Binary
2 : 0 : None : 1 : False : True : Binary
3 : 0 : None : 1 : False : True : Binary
1 Objective Declarations
OBJ : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : minimize : 2*x[1] + 3*x[2] + 3*x[3]
2 Constraint Declarations
rxlambdaConstraintUpper : Size=3, Index=n, Active=True
Key : Lower : Body : Upper : Active
1 : 0.5 : 0.8 * x[1] : 0.9 : True
2 : 0.5 : 0.1 * x[2] : 0.9 : True
3 : 0.5 : 0.2 * x[3] : 0.9 : True
sumxConstraint : Size=3, Index=n, Active=True
Key : Lower : Body : Upper : Active
1 : 1.0 : x[1] + x[2] + x[3] : 1.0 : True
2 : 1.0 : x[1] + x[2] + x[3] : 1.0 : True
3 : 1.0 : x[1] + x[2] + x[3] : 1.0 : True
10 Declarations: I J m n x d r OBJ rxlambdaConstraintUpper sumxConstraint
WARNING: DEPRECATION WARNING: the Model.create() method is deprecated. Call
Model.create_instance() to create a concrete instance from an abstract
model. You do not need to call Model.create() for a concrete model.
WARNING: DEPRECATED: Cannot call Model.create_instance() on a constructed
model; returning a clone of the current model instance.
Job "xxxxx" submitted to NEOS, password='xxxxx'
Check the following URL for progress report :
"link deleted"
Job "xxxxx" dispatched
password: "XXXXX"
---------- Begin Solver Output -----------
Condor submit: 'neos.submit'
Condor submit: 'watchdog.submit'
Job submitted to NEOS HTCondor pool.
WARNING: Loading a SolverResults object with a warning status into
model=unknown;
message from solver=CPLEX 12.7.0.0\x3a integer infeasible.; 0 MIP
simplex iterations; 0 branch-and-bound nodes; No basis.
2 RangeSet Declarations
m : Dim=0, Dimen=1, Size=1, Domain=Integers, Ordered=True, Bounds=(1, 1)
Virtual
n : Dim=0, Dimen=1, Size=3, Domain=Integers, Ordered=True, Bounds=(1, 3)
Virtual
4 Param Declarations
I : Size=1, Index=None, Domain=Any, Default=None, Mutable=False
Key : Value
None : 1
J : Size=1, Index=None, Domain=Any, Default=None, Mutable=False
Key : Value
None : 3
d : Size=3, Index=n, Domain=Any, Default=None, Mutable=False
Key : Value
1 : 2
2 : 3
3 : 3
r : Size=3, Index=n, Domain=NonNegativeReals, Default=None, Mutable=False
Key : Value
1 : 0.8
2 : 0.1
3 : 0.2
1 Var Declarations
x : Size=3, Index=n
Key : Lower : Value : Upper : Fixed : Stale : Domain
1 : 0 : None : 1 : False : True : Binary
2 : 0 : None : 1 : False : True : Binary
3 : 0 : None : 1 : False : True : Binary
1 Objective Declarations
OBJ : Size=1, Index=None, Active=True
Key : Active : Sense : Expression
None : True : minimize : 2*x[1] + 3*x[2] + 3*x[3]
2 Constraint Declarations
rxlambdaConstraintUpper : Size=3, Index=n, Active=True
Key : Lower : Body : Upper : Active
1 : 0.5 : 0.8 * x[1] : 0.9 : True
2 : 0.5 : 0.1 * x[2] : 0.9 : True
3 : 0.5 : 0.2 * x[3] : 0.9 : True
sumxConstraint : Size=3, Index=n, Active=True
Key : Lower : Body : Upper : Active
1 : 1.0 : x[1] + x[2] + x[3] : 1.0 : True
2 : 1.0 : x[1] + x[2] + x[3] : 1.0 : True
3 : 1.0 : x[1] + x[2] + x[3] : 1.0 : True
10 Declarations: I J m n x d r OBJ rxlambdaConstraintUpper sumxConstraint
Model unknown
Variables:
x : Size=3, Index=n
Key : Lower : Value : Upper : Fixed : Stale : Domain
1 : 0 : None : 1 : False : True : Binary
2 : 0 : None : 1 : False : True : Binary
3 : 0 : None : 1 : False : True : Binary
Objectives:
OBJ : Size=1, Index=None, Active=True
ERROR: evaluating expression: No value for uninitialized NumericValue object
x[1]
(expression: 2*x[1] + 3*x[2] + 3*x[3])
ERROR: evaluating object as numeric value: OBJ
(object: <class 'pyomo.core.base.objective.SimpleObjective'>)
No value for uninitialized NumericValue object x[1]
Key : Active : Value
None : None : None
Constraints:
rxlambdaConstraintUpper : Size=3
ERROR: evaluating expression: No value for uninitialized NumericValue object
x[1]
(expression: 0.8 * x[1])
ERROR: evaluating expression: No value for uninitialized NumericValue object
x[2]
(expression: 0.1 * x[2])
ERROR: evaluating expression: No value for uninitialized NumericValue object
x[3]
(expression: 0.2 * x[3])
Key : Lower : Body : Upper
1 : None : None : None
2 : None : None : None
3 : None : None : None
sumxConstraint : Size=3
ERROR: evaluating expression: No value for uninitialized NumericValue object
x[1]
(expression: x[1] + x[2] + x[3])
ERROR: evaluating expression: No value for uninitialized NumericValue object
x[1]
(expression: x[1] + x[2] + x[3])
ERROR: evaluating expression: No value for uninitialized NumericValue object
x[1]
(expression: x[1] + x[2] + x[3])
Key : Lower : Body : Upper
1 : None : None : None
2 : None : None : None
3 : None : None : None
(1, None)
(2, None)
(3, None)
您的rxlambdaConstraintUp为2,3是不可行的。
2 : 0.5 : 0.1 * x[2] : 0.9 : True
3 : 0.5 : 0.2 * x[3] : 0.9 : True
如果x[2]和x[3]取0或1,它们将小于0.5(它们的下界)。因此如果你改变路线
return (0.5, model.r[i]*model.x[i], 0.9 )
像
return (0, model.r[i]*model.x[i], 0.9 )
您可以获得工作模型和结果。