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.model.constraints = ConstraintList()
self.model.constraints.add(fixed_on_subtype(self.model, self.model.p, self.model.d))
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
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
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.