1
$\begingroup$

I am trying to update my mutable parameter Nc in my Abstract model the initial value is 4 I constructed the instance then change instance.Nc to 5 and solve it but it is still 4 (initial value) , can any body help ?

from pyomo.environ import *
import random 

model = AbstractModel()
model.i = RangeSet(40)
model.j = Set(initialize=model.i)
model.x = Var(model.i,model.j, initialize=0,within=Binary)
model.y = Var(model.i, within=Binary)

model.Nc=Param(initialize=5,mutable=True)
def Ninit(model,i):
    return random.randint(0,1)

model.N=Param(model.i,initialize=Ninit,mutable=True)

def Dinit(model,i,j):
    return random.random()
model.D=Param(model.i,model.j,initialize=Dinit,mutable=True)
def rule_C1(model,i,j):
        return model.x[i,j]<=model.N[i]*model.y[j] 
model.C1   = Constraint(model.i,model.j,rule=rule_C1)
def rule_C2(model):
        return sum(model.y[i] for i in model.i )==model.Nc
model.C2   = Constraint(rule=rule_C2)
def rule_C3(model,i):
        return sum(model.x[i,j] for j in model.j)==model.N[i]

model.C3   = Constraint(model.i,rule=rule_C3)

def rule_OF(model):
    return sum( model.x[i,j]*model.D[i,j] for i in model.i for j in model.j )
model.obj = Objective(rule=rule_OF, sense=minimize)
opt = SolverFactory('glpk')

#model.NC=4
instance = model.create_instance()
instance.NC=4
results = opt.solve(instance) # solves and updates instance
print('NC= ',value(instance.Nc))
print('OF= ',value(instance.obj))
$\endgroup$

2 Answers 2

2
$\begingroup$

Before creating the instance, you should provide the value for model.Nc (not model.NC).

It should look like:

opt = SolverFactory('glpk')

model.Nc=4 #correct this variable Nc

instance = model.create_instance()
results = opt.solve(instance) # solves and updates instance
print('NC= ',value(instance.Nc))
print('OF= ',value(instance.obj))
$\endgroup$
1
$\begingroup$

This is because instance.NC=4 has a wrong spelling for Nc

In order to fix this, the correct spelling of Nc should be used (not NC)

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.