Change value of a parameter for solving

1,026 views
Skip to first unread message

Jan Stampfli

unread,
Apr 10, 2017, 11:45:34 AM4/10/17
to pyomo...@googlegroups.com
I have build up a abstract pyomo model using  python 2.7 and pyomo 5.1.1.
For the optimization I want to change one parameter (V_tot). 

To change the parameter I have used getattr() as described in the pyomo book. (This seems to work)

To update the instance I have used the command reconstruct() which seams to work for the parameter M_tot but not for the constraints and the objective.

I also tried to use preprocess().

In the appendix you can find my .py and .dat file.

Thank you for your help

Jan
1IL.dat
FTVM.py

Bethany Nicholson

unread,
Apr 10, 2017, 12:17:26 PM4/10/17
to pyomo...@googlegroups.com
Because you already declared the parameter V_tot as mutable by setting mutable=True, all you have to do is set it equal to a new value and solve. You shouldn't have to reconstruct anything.

instance.V_tot = new_value

For your parameters that are initialized with functions of other parameters, those rules are only called once when the parameters are constructed. In order to maintain those relationships when changing the value of V_tot, you should declare them as additional variables and include the rules as additional constraints or declare them as Expression objects.

# Either this
model.M_tot = Var()
def _rel_M_tot(m):
    return m.M_tol == m.V_tol *m.rho
model.M_tol_con = Constraint(rule=_rel_M_tot)

# or this
def _rel_M_tot(m):
    return m.V_tol *m.rho
model.M_tot = Expression(rule=_rel_M_tot)


Bethany

On Mon, Apr 10, 2017 at 9:44 AM, Jan Stampfli <stampf...@gmail.com> wrote:
I have build up a abstract pyomo model using  python 2.7 and pyomo 5.1.1.
For the optimization I want to change one parameter (V_tot). 

To change the parameter iI have used getattr as described in the pyomo book. (This seems to work)

To update the instance I have used the command reconstruct() which seams to work for the parameter M_tot but not for the constraints and the objective.

I also tried to use preprocess().

In the appendix you can find my .py and .dat file.

Thank you for your help

Jan

--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jan Stampfli

unread,
Apr 10, 2017, 12:46:49 PM4/10/17
to Pyomo Forum
Thank you for your answer. I use now the expression for M_tot as you mentioned:
 
# or this
def _rel_M_tot(m):
    return m.V_tol *m.rho
model.M_tot = Expression(rule=_rel_M_tot)

Due to this, M_tot is updating as it should. But the values of the objective function and the constraints doesn't change / update.

  

Bethany Nicholson

unread,
Apr 10, 2017, 12:55:44 PM4/10/17
to pyomo...@googlegroups.com
Did you change your other parameters similar to M_tot to Expressions? Did you remove calls to getattr and reconstruct?

You can print certain parts of your model to verify that they are being created as you expect using the pprint() function on any constructed model component or the model as a whole,

instance.OF.pprint()

--

Jan Stampfli

unread,
Apr 10, 2017, 1:27:44 PM4/10/17
to Pyomo Forum
Yes, I have changed m_hkl and m_ckl also to expressions and have removed getattr and reconstructs.

I have attached the output of the last pprint. As you can see, the Values in M_kl are always  14292...

The bounds of M_kmax are way too high (sum(M_kmax) <= M_tot) 


pprint.txt

Bethany Nicholson

unread,
Apr 10, 2017, 2:34:40 PM4/10/17
to pyomo...@googlegroups.com
The rules for your Expression components m_hkl and m_ckl are not specified correctly. When you print them you should see the expression instead of a single value.

Bethany

On Mon, Apr 10, 2017 at 11:27 AM, Jan Stampfli <stampf...@gmail.com> wrote:

Jan Stampfli

unread,
Apr 10, 2017, 3:18:39 PM4/10/17
to Pyomo Forum
I cannot find whats wrong. These are the three defined expressions:

def rel_M_tot(model):                                          
        return model.V_tot * model.rho
model.M_tot = Expression(rule=rel_M_tot)

def rel_m_hkl(model, h, k, l):                                  
        return model.Qh[h, k, l] * 3600 / (model.cp_sm \
        * (model.T_VSU[k + 1] - model.T_VSU[k]))
model.m_hkl = Expression(model.h, model.k, model.l,\
rule=rel_m_hkl)

def rel_m_ckl(model, c, k, l):                                 
        return model.Qc[c, k, l] * 3600 / (model.cp_sm \
         * (model.T_VSU[k + 1] - model.T_VSU[k]))
model.m_ckl = Expression(model.c, model.k, model.l,\
rule=rel_m_ckl)

The only difference between them is their dimension. 

Qh,Qc,cp_sm and T_VSU are all not mutable as rho (in M_tot)
rho is also a single value in the pprint. 

Jan Stampfli

unread,
Apr 10, 2017, 5:03:18 PM4/10/17
to Pyomo Forum
Now it works. The calculation of V_tot was wrong, which caused a way to large M_kmax and so the M_kl were not constraining. 

Benthany, thanky ou very much for your help!

Jan
Reply all
Reply to author
Forward
0 new messages