Pyomo If Else statement in the constraint

154 views
Skip to first unread message

Dongjin Cho

unread,
Jul 16, 2018, 6:38:34 PM7/16/18
to Pyomo Forum
Hi,

I have the constraint as below. When t==1, it uses SISC, otherwise, it uses sbat[t-1]. However, when I ran the mode didn't show me a correct solution because of this constraint. 

Is there anyone who could help?


## Battery Charge and Discharge 1
def BatteryChargeandDischarge1 (model, t) :

    if (t == 1) :
        return model.SISC * (1 - model.RDCH) + (model.echr[t] * model.RCHR)/model.CBAT - model.eapp_bat[t]/model.CBAT == model.sbat[t] 
    else :
        return model.sbat[t-1] * (1 - model.RDCH) + (model.echr[t] * model.RCHR)/model.CBAT - model.eapp_bat[t]/model.CBAT == model.sbat[t] 

model.BatteryChargeandDischarge1 = Constraint (model.Time, rule=BatteryChargeandDischarge1) 

Nicholson, Bethany L.

unread,
Jul 16, 2018, 6:46:19 PM7/16/18
to pyomo...@googlegroups.com

You should try printing the constraint and verify that it’s what you expect:

 

model.BetteryChargeandDischarge1.pprint()

 

Bethany

--
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.

Dongjin Cho

unread,
Jul 16, 2018, 7:52:17 PM7/16/18
to pyomo...@googlegroups.com
Actually, this constraint doesn't work in pyomo environment. In AMPL, it works. But in pyomo, it doesn't work. That's why I need your help to describe if statement in pyomo.
Thanks.

Ornélio Hinterholz Junior

unread,
Jul 17, 2018, 9:48:16 AM7/17/18
to pyomo...@googlegroups.com
Hi,

In a water network problem (with hybrid topology), I have to calculate the pressure at every node (I already know the pressures at the Reservoirs, Tanks and Pumps).

Is there any possibility to solve this with Pyomo using some sort of a optimization model? or do I need to calculate using just a simulation model?

Have already someone had a similar problem?

Thanks.

Nicholson, Bethany L.

unread,
Jul 17, 2018, 10:01:22 AM7/17/18
to pyomo...@googlegroups.com

I’m not sure what you mean by “it doesn’t work”. Are you getting an error when that constraint is constructed? If so could you send the error message? Is model.Time a Pyomo Set? Are you working with an Abstract or Concrete model? What component type is model.sbat? Are you solving the model using the pyomo command line or through a script with SolverFactory?

 

We need a lot more information about your model to diagnose the problem.

Dongjin Cho

unread,
Jul 17, 2018, 4:33:36 PM7/17/18
to Pyomo Forum
Hi,

I found the problem.. it was my mistake. and it works. !

By the way, I appreciate answering my question. And I have several more questions. Could you answer me?

1. I have another constraint in AMPL as below and want to transfer this syntax to my Pyomo model.

subject to xapp_endProcess00 {a in 1..2, t in 1..23}:
T[1] - sum{n in 1..t} xapp_state[1,n] <=  M*(1-xapp_state[2,t+1]) ;

T is a parameter (constant number), xapp_state is the binary variable and M is a big number (constant). When a= 1 or 2, it has several different kinds of summation equations. For example, when a=1, the equation is as follows :

t==1 : T[1] - sum{n in 1..1}  xapp_state[1,n] <=  M*(1-xapp_state[2,2]) ;
t==2 : T[1] - sum{n in 1..2}  xapp_state[1,n] <=  M*(1-xapp_state[2,3]) ;
.
.
.

t==22 : T[1] - sum{n in 1..22}  xapp_state[1,n] <=  M*(1-xapp_state[2,23]) ;
t==23 : T[1] - sum{n in 1..23}  xapp_state[1,n] <=  M*(1-xapp_state[2,24]) ;


Can you explain how to transfer this constraint to the Pyomo syntax?



2. I'd like to see the result of one variable in my abstract model as the matrix format as below.

xapp_state[i,j]: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0
3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0
4 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
5 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0;


However, the console window shows the result of the variable as below and it is really hard to identify it. In AMPL, it automatically shows me the matrix format, but, What do I have to do to see the result of the variable as the matrix format?

Variables:
    xapp_state : Size=120, Index=onOff_index
        Key     : Lower : Value : Upper : Fixed : Stale : Domain
         (1, 1) :     0 :   0.0 :     1 : False : False : Binary
         (1, 2) :     0 :   0.0 :     1 : False : False : Binary
         (1, 3) :     0 :   0.0 :     1 : False : False : Binary
         (1, 4) :     0 :   0.0 :     1 : False : False : Binary
         (1, 5) :     0 :   0.0 :     1 : False : False : Binary
         (1, 6) :     0 :   0.0 :     1 : False : False : Binary
         (1, 7) :     0 :   0.0 :     1 : False : False : Binary
         (1, 8) :     0 :   0.0 :     1 : False : False : Binary

Bynum, Michael Lee

unread,
Jul 17, 2018, 9:58:21 PM7/17/18
to pyomo...@googlegroups.com
You could certainly do this with Pyomo. However, there is a Python package called WNTR that is designed for simulating water distribution hydraulics (computing flows and pressures). WNTR does not do optimization though.

Michael

Reply all
Reply to author
Forward
0 new messages