我是Pyomo的新手。在下面的代码部分中,我试图最小化一个函数,以便满足其他两个约束。 我遇到一个特别的约束,我试图通过指定上限和下限来提供范围。 dict [key] = [d,r,p];此处未使用“ p”。 决策变量“ x”是二进制的。目的是找出要选择的项目(只有一个),以使“ r”值范围内的“ d”最小。 “ r”值的范围由“ constraint rxlambda_constraint_rule_upper”给出。 因此,目标是从字典中找出三个字典项中的一项,以使“ d”的值在“ r”值的范围内最小。T据我了解,应该是第一项(正如我在此处所述),如[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)
答案 0 :(得分:0)
2、3的rxlambdaConstraintUpper是不可行的。
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 )
您可以获得工作模型和结果。