gams constraint to pyomo

30 views
Skip to first unread message

Niki T.

unread,
Jul 20, 2021, 12:29:37 AM7/20/21
to Pyomo Forum
Hello,

I am new to pyomo and I have a question regarding sets.
I have a set t in gams and its alias tt

Set t / t1*t30 /;
alias (t,tt);

and the following constraint

MSBnew(p,m,t)..DURV(p,m,t)=e=sum(tt$(ord(tt)<=ord(t)), INM(p,m,tt-1)-OUTM(p,m,tt))+OUTM(p,m,t); 

In pyomo i translated it as:

model.t = Set() 
model.tt = Set(initialize=model.t) 

def MSBnew_rule(model,p,m,t):
    return model.DURV[p,m,t] == sum(model.INM[p,m,tt-1]-model.OUTM[p,m,tt] for tt in model.tt if tt<=t) + model.OUTM[p,m,t] 
model.MSBnew = Constraint(model.p, model.m, model.t, rule=MSBnew_rule)

But I get the error :
TypeError: unsupported operand type(s) for -: 'str' and 'int'

Could you please help me formulate this gams constraint in pyomo?

Siirola, John D

unread,
Jul 20, 2021, 10:38:44 AM7/20/21
to pyomo...@googlegroups.com

The problem is you are using string values in your Set (which is perfectly fine and something that Pyomo supports).  However, when you iterate over the Set, you are getting strings, so you cannot directly “do math” on the set values.  Instead, you should make use of the Set’s `ord()`, `next()`, and `prev()` methods.

 

I would do something similar to:

 

model.t = Set() # note that since Pyomo 5.6, Pyomo sets are ordered by insertion order unless otherwise specified

 

def MSBnew_rule(model, p, m, t):

    return model.DURV[p, m, t] == sum(

        model.INM[p, m, model.t.prev(tt)] - model.OUTM[p, m, tt]

        for tt in model.t if model.t.ord(tt) <= model.t.ord(t)

    ) + model.OUTM[p, m, t] 

model.MSBnew = Constraint(model.p, model.m, model.t, rule=MSBnew_rule)

 

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyomo-forum/e3e48249-6194-45a9-b589-80c545992ad9n%40googlegroups.com.

Niki T.

unread,
Jul 24, 2021, 11:39:50 AM7/24/21
to Pyomo Forum
That is so helpful. Thank you very much!
Reply all
Reply to author
Forward
0 new messages