7

I am writing a Pyomo model and trying to code the following mathematical constraint:

$$ \sum_{i=1}^I\sum_{j=1}^J\sum_{t=1}^T 5I_{ijt} + 10L_{ijt}I_{ijt} $$ where $L_{ijt}$ is binary.

However, I am hoping that I could get someone to clarify if what I have written in my model makes sense:

def objective_rule(model):
    return sum (sum (sum (model.obj[i,j,t] for i in model.Iset ) for j in model.Jset ) for t in model.Tset )
model.damages = Objective(rule=objective_rule, sense=minimize)

def obj_rule(model, i,j,t):
    return model.obj[i,j,t] == 5*model.inf_b4treat[i,j,t] + 10 *model.level1[i,j,t]*model.inf_b4treat[i,j,t]
model.object = Constraint(model.Iset, model.Jset, model.Tset, rule=obj_rule)

I am getting a couple of strange results and having debugged my model I am thinking that it might be due to my objective function not being formatted properly. To clarify, the reason that I chose to create an additional object called model.obj[i,j,t] is so that I could access the objective function values after I solve the model.

I would appreciate any corrections.

GrayLiterature
  • 2,309
  • 7
  • 27

1 Answers1

2

You actually don't need to add so many "sum" while writing constraints or objective function in Pyomo, write your objective function like this

def objective_rule(model):
    return sum((5*model.inf_b4treat[i,j,t] + 10*model.level1[i,j,t]*model.inf_b4treat[i,j,t]) for i in model.Iset for j in model.Jset for t in model.Tset)
model.objective_rule = Objective(rule=objective_rule, sense=minimize, doc='Objective Function')

You can check your constraints or objective function using

model.objective_rule.pprint()
khrithik
  • 101
  • 4