2
$\begingroup$

My pyomo abstract model has 2 sets with 3 parameters for each set.

I am minimizing the objective function with 4 constraints as follows:

from pyomo.environ import*
model=AbstractModel() # Model Creation

model.SUPPLIES=Set()  #Sets
model.DEMANDS=Set()


model.ccpu=Param(model.SUPPLIES)  #Parameters
model.csto=Param(model.SUPPLIES)
model.cmem=Param(model.SUPPLIES)
model.dcpu=Param(model.DEMANDS)
model.dsto=Param(model.DEMANDS)
model.dmem=Param(model.DEMANDS)

model.x=Var(model.SUPPLIES, model.DEMANDS, domain=Binary)  #Decision Variable

def objective_rule(model): #Objective Functions
    return sum(model.x[i,j]*model.dcpu[j] + model.x[i,j]*model.dmem[j] 
+model.x[i,j]*model.dsto[j] for i in model.SUPPLIES for j in model.DEMANDS)
model.mincost=Objective(rule=objective_rule, sense=minimize)

def cpu_rule(model, i): #Constraints
    return sum(model.x[i,j]*model.dcpu[j] for j in model.DEMANDS)<=model.ccpu[i]
const1=Constraint(model.SUPPLIES, rule=cpu_rule)

def mem_rule(model, i):
    return sum(model.x[i,j]*model.dmem[j] for j in model.DEMANDS)<=model.cmem[i]
const2=Constraint(model.SUPPLIES, rule=mem_rule)

def sto_rule(model, i):
    return sum(model.x[i,j]*model.dsto[j] for j in model.DEMANDS)<=model.csto[i]
const3=Constraint(model.SUPPLIES, rule=sto_rule)

def x_rule(model,j):
    return sum(model.x[i,j] for i in model.SUPPLIES)<=1
const4=Constraint(model.DEMANDS, rule=x_rule)


data=DataPortal() #Instance Creation and Solving
data.load(filename='transportationtry.dat', model=model)
instance = model.create_instance(data)
optimizer=SolverFactory("glpk")
optimizer.solve(instance)
instance.display()

The "transportationtry.dat" consist of the following data:

set SUPPLIES := P1 P2 P3;

set DEMANDS := C1 C2 C3 C4;

param: SUPPLIES: ccpu:= 

P1 35

P2 50

P3 40;

param: DEMANDS: dcpu:=

C1 1

C2 20

C3 5

C4 3;

param: SUPPLIES: csto:=

P1 35

P2 50

P3 40;

param: DEMANDS: dsto:=

C1 10

C2 2

C3 10

C4 3;

param: SUPPLIES: cmem:=

P1 35

P2 50

P3 40;

param: DEMANDS: dmem:=

C1 1

C2 2

C3 5

C4 3;
$\endgroup$

1 Answer 1

3
$\begingroup$

You need to attach the Constraint objects to the model you are building (otherwise they are not part of the model):

Instead of

const1 = Constraint(#...
    )

you need to do

model.const1 = Constraint(#...
    )
$\endgroup$
1
  • $\begingroup$ Thank you @jsiirola for the prompt response. You are a saviour. $\endgroup$ Commented Sep 13, 2022 at 7:23

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.