Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

modify model by adding constraints

557 views
Skip to first unread message

Cacharmday

unread,
Jul 14, 2016, 8:12:38 AM7/14/16
to Pyomo Forum
Hi There, 

Im stuck so i hope you could help me with the following: 

Im working on a concrete model. 
I've initialized all my constraint by indexed rules. 

Example:

def fixed_on_subtype(model, p, d):
"""
In the (3-8) weeks period, only plan A-checks with the same subtype as in the current schedule
"""
return model.AC[p,d] <= model.FS[p,d] + model.slack2[p,d]

self.contextBO.model.fix_on_subtype = Constraint(self.model.p, self.model.d , rule=fixed_on_subtype)





Now, I have to modify my model by adding constraints after the first subproblem in solved.

I thought about this by initializing ConstraintList()  instead of the other constraint rules like: 

self.model.constraints = ConstraintList()
self.model.constraints.add(fixed_on_subtype(self.model, self.model.p, self.model.d))


However you cannot add indexed constraints to the constraintlist.
So i tried modifying my rule by: 


def fixed_on_subtype(model):
"""
In the (3-8) weeks period, only plan A-checks with the same subtype as in the current schedule
"""
for p in model.p:
for d in model.fs:
return model.AC[p,d] <= model.FS[p,d] + model.slack2[p,d]
And calling: 

self.model.constraints.add(fixed_on_subtype(self.model))


But this gives wrong results (not all the possibile constraints are being initialized in the problem).






Is there any other way to add constraints to the problem (iterative optimization) ?
I allready tried constraint.deactivate()  and after solving the subproblem constraint.activate() . 
This given the same solution (before and after activation)

Can someone please help me out i have to fix this quick since i have a presentation. '

Kind Regards, 

Cas

Santiago rodriguez

unread,
Jul 14, 2016, 9:51:59 AM7/14/16
to Pyomo Forum
Hi Cas,

I think your alternative solution is not working because your function 

def fixed_on_subtype(model):
"""
In the (3-8) weeks period, only plan A-checks with the same subtype as in the current schedule
"""
for p in model.p:
for d in model.fs:
return model.AC[p,d] <= model.FS[p,d] + model.slack2[p,d]

will only return one constraint (first index p and first index d). I suggest the following alternative

def fixed_on_subtype(model,p,d):

return model.AC[p,d] <= model.FS[p,d] + model.slack2[p,d]
for p in model.p:
for d in model.fs:
        self.model.constraints.add(fixed_on_subtype(self.model,p,d))

That would add all the constraints to your model. However the constraint list will lose the identity of your constraint. If you want to look at your constraints after solving based on the p and q indices this can also be a solution

for p in model.p:
    for d in model.fs:        
        setattr(model, 'fixed_on_subtype_{}_{}'.format(p,d), Constraint(fixed_on_subtype(model,p,d)))

hope that helps

Siirola, John D

unread,
Jul 14, 2016, 10:52:55 AM7/14/16
to pyomo...@googlegroups.com

The other option is to use a generator [note the use of ‘yield’ and not ‘return’] to initialize the ConstraintList (this is one of those tricks that only works with ConstraintList):

 

def fixed_on_subtype(model):
    
"""
    In the (3-8) weeks period, only plan A-checks with the same subtype as in the current schedule
    """
   
for p in model.p:
       
for d in
model.fs:
           
yield model.AC[p,d] <= model.FS[p,d] + model.slack2[p,d]
 
self.model.constraints = ConstraintList(rule=fixed_on_subtype)

 

Other thoughts: There is no reason that the additional cuts you want to add have to be in the same component.  That is, I will frequently do something like this:

 

def fixed_on_subtype(model, p, d):
   
"""
    In the (3-8) weeks period, only plan A-checks with the same subtype as in the current schedule
    """

   
return model.AC[p,d] <= model.FS[p,d] + model.slack2[p,d]

 

self.contextBO.model.fix_on_subtype = Constraint(self.model.p, self.model.d , rule=fixed_on_subtype)
 
self.contextB0.model.cuts = ConstraintList()

 

That is, make an empty ConstraintList for the initial solve, and then for subsequent solves, add additional constraints to the cuts component.  This is nice in that it preserves the original structure of your model.  Plus, if your subsequent constraints are of the same form as “fixed_on_subtype”, you can do something similar to what Santiago suggests where you re-use the rule:

 

self.contextB0.model.cuts.add(fixed_on_subtype(self.contextB0.model,new_p,new_d))

 

john

--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

roli....@gmail.com

unread,
Jul 13, 2024, 7:33:16 PM7/13/24
to Pyomo Forum
I'm trying to iteratively solve my model and update a constraint list using the way described here, but get an error:

RuntimeError: Cannot access '__len__' on AbstractOrderedScalarSet 'AbstractOrderedScalarSet' before it has been constructed (initialized): '__len__' is an attribute on an Abstract component and cannot be accessed until the component has been fully constructed (converted to a Concrete component) using AbstractModel.create_instance() or AbstractOrderedScalarSet.construct().

I tried constructing the constraint list before, but I get a different error then:

ValueError: Invalid constraint expression. The constraint expression resolved to a trivial Boolean (False) instead of a Pyomo object. Please modify your rule to return Constraint.Infeasible instead of False.
Error thrown for Constraint 'cnstr_cuts[1]'

Reply all
Reply to author
Forward
0 new messages