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.

  • 1
    $\begingroup$ It might be easier for people to help you if you can provide a minimum working example, which demonstrates your "strange results". $\endgroup$ Sep 19 '19 at 2:27
  • $\begingroup$ I think you probably can change it to sum(model.obj[i,j,t] for i in model.Iset for j in model.Jset for t in model.Tset) but it should be the same. Can you provide additional information on the other variables? What type are they and what bounds do they have? $\endgroup$
    – JakobS
    Sep 19 '19 at 11:07
  • 2
    $\begingroup$ To @KevinDalmeijer's comment, here's OR.SE's version of the minimum working example page. $\endgroup$
    – LarrySnyder610
    Sep 20 '19 at 2:53
  • $\begingroup$ Cool, good to know we have our own! $\endgroup$ Sep 20 '19 at 3:01
  • $\begingroup$ The expression given in your example seems like it is not a constraint but the way you coded it in your Pyomo code, it is the right-hand side of a constraint for which the left-hand side expression is missing in your example. Is that correct? What would be the complete constraint rule? $\endgroup$ Sep 26 '19 at 16:33

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


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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