How to dynamically create variables (model.x1,model.x2..etc) with different 'initialize' and 'bounds' in pyomo

1,582 views
Skip to first unread message

harish....@latentview.com

unread,
May 17, 2018, 8:05:30 AM5/17/18
to Pyomo Forum
Hi,
Is there a way to loop and create new variables in pyomo?
Eg:

OLD
model.x1 = Var( initialize= 1.0, bounds=(1.0,5.0) )
model.x2 = Var( initialize= 5.0, bounds=(1.0,5.0) )

NEW
for i in range(1):
    model.x + str(i) = Var( initialize= qw, bounds=(l,u) )

Please help! Thanks!


This email may contain confidential and privileged information for the sole use of the intended recipient. Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please reply back to the sender about the error and delete all copies of this email message. Thank you.

Siirola, John D

unread,
May 17, 2018, 8:20:06 AM5/17/18
to pyomo...@googlegroups.com
There are several ways to do this. The one closest to your suggestion is:

for i in range(1):
    model.add_component(“x%s” % i, Var(initialize=qw, bounds=(l,u))

You can also use a VarList:

model.x = VarList(initialize=qw, bounds=(l,u))
for i in range(1):
    model.x.add()

The first gives separate Var components (“x1”, “x2”, etc) whereas the second builds a single indexed Var (“x[0]”, “x[1]”, etc). 

Also note you have an off-by-1 error, as range(1) will only create 0 and not (0,1). 

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.

harish....@latentview.com

unread,
May 17, 2018, 8:23:57 AM5/17/18
to Pyomo Forum
Omg thats quick.. Thanks a lot john.. i will go with loop itself as i need separate variables. just out of curiosity im asking this - How did you explore this module? Are there any good docs which i can refer to as a beginner? Thanks a lot again!

Laird, Carl Damon

unread,
May 17, 2018, 8:40:07 AM5/17/18
to pyomo...@googlegroups.com

Hi,

 

Note that you can also create “indexed” variables (not sure if this is what you need or not). For example,

 

   model.x = Var(range(5), initialize=qw, bounds=(l,u))

 

will create x[0] through x[4].

 

In my opinion, the best reference right now is the Pyomo Book. The second edition is now out, and available in the libraries at many universities. We are working on updates to the online documentation now. This should be available soon (if not already).

 

Regards,

 

Carl.

harish....@latentview.com

unread,
May 17, 2018, 8:46:22 AM5/17/18
to Pyomo Forum
Thank you so much Carl. I refer to http://www.pyomo.org/documentation/. Is this the book you mentioned carl?

In this model.x = Var(range(5), initialize=qw, bounds=(l,u)), will i be able to provide list/tuple of tuples for the bounds(for each x value) and also list of initialize values for each x value? Thanks again! I am just lost with this package.

Laird, Carl Damon

unread,
May 17, 2018, 8:51:29 AM5/17/18
to pyomo...@googlegroups.com

Correct. “The Pyomo Book” on that page is the reference I meant. Please note that the second edition is the current edition. Several things have changed, and the first edition will be quite out of date.

harish....@latentview.com

unread,
May 17, 2018, 8:54:42 AM5/17/18
to Pyomo Forum
Thanks much Carl!!

harish....@latentview.com

unread,
May 17, 2018, 9:05:42 AM5/17/18
to Pyomo Forum
I'm a data analyst and I'm very much interested in optimization now. I'm decent with python and few scripting languages and this is why im able to solve few problems without actually knowing the theory behind these algorithms. I tried few algos in R but it threw errors for my data set. Then i moved to scipy where i got some errors like message: 'Singular matrix C in LSQ subproblem'. Then i came across pyomo and am trying it now. Hopefully this should work..Could you please share you email id sir? i would love to learn about optimization. 

John mentioned 'model.add_component(“x%s” % i, Var(initialize=qw, bounds=(l,u))' i did not even come across "add_component" in the book. Just want to know if i missed it or it isn't there like you said.

Siirola, John D

unread,
May 17, 2018, 9:17:14 AM5/17/18
to pyomo...@googlegroups.com
The book documents a subset of everything in Pyomo. That subset was chosen to be the part that is the most useful to general users as well as the part that we intend to support and enforce backwards compatibility over a longer period (ideally through the whole 5.x release series). 

Advanced features, developer-oriented features, and newer capabilities under development will (eventually) be documented in the online documentation (pyomo.readthedocs.org)

John

harish....@latentview.com

unread,
May 17, 2018, 9:20:20 AM5/17/18
to Pyomo Forum
Oh! got it john. thanks a lot :)  By the way, i didnt know that you and carl are the authors. thanks for taking time in helping me :)

Laird, Carl Damon

unread,
May 17, 2018, 9:21:59 AM5/17/18
to pyomo...@googlegroups.com

One of the reasons add_component is not documented in the book is because most (but not all) modeling problems can be captured more simply with the indexed component notation that I referenced. The book should explain the concept of indexed components pretty clearly.

harish....@latentview.com

unread,
May 17, 2018, 9:23:10 AM5/17/18
to Pyomo Forum
got it carl. thank you :)

harish....@latentview.com

unread,
May 17, 2018, 11:34:25 AM5/17/18
to pyomo...@googlegroups.com
Carl and John, Could you please take a look at my model(attached image) and tell me if the objective, constraints and variables are created properly. Thank you! 

I get the below error,
WARNING: Loading a SolverResults object with a warning status into
    model=unknown;
        message from solver=Ipopt 3.11.1\x3a Iterates diverging; problem might
        be unbounded.
No idea why. 
Capture.JPG

Laird, Carl Damon

unread,
May 17, 2018, 11:50:58 AM5/17/18
to pyomo...@googlegroups.com

 

I see that you have added constraints that say x3==3 and x4==50, but you have bounds on those variables that do not contain these values.

 

From: "harish.padmakar via Pyomo Forum" <pyomo...@googlegroups.com>


Reply-To: "pyomo...@googlegroups.com" <pyomo...@googlegroups.com>
Date: Thursday, May 17, 2018 at 10:34 AM
To: Pyomo Forum <pyomo...@googlegroups.com>

Subject: [EXTERNAL] Re: How to dynamically create variables (model.x1,model.x2..etc) with different 'initialize' and 'bounds' in pyomo

Carl and John, Could you please take a look at my model(attached image) and tell me if the objective, constraints and variables are created properly. Thank you! 

I get the below error,

WARNING: Loading a SolverResults object with a warning status into
    model=unknown;
        message from solver=Ipopt 3.11.1\x3a Converged to a locally infeasible
        point. Problem may be infeasible.

No idea why. 


On Thursday, 17 May 2018 17:35:30 UTC+5:30, harish....@latentview.com wrote:

Hi,

Is there a way to loop and create new variables in pyomo?

Eg:

 

OLD

model.x1 = Var( initialize= 1.0, bounds=(1.0,5.0) )

model.x2 = Var( initialize= 5.0, bounds=(1.0,5.0) )

 

NEW

for i in range(1):

    model.x + str(i) = Var( initialize= qw, bounds=(l,u) )

 

Please help! Thanks!

 



This email may contain confidential and privileged information for the sole use of the intended recipient. Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please reply back to the sender about the error and delete all copies of this email message. Thank you.



This email may contain confidential and privileged information for the sole use of the intended recipient. Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please reply back to the sender about the error and delete all copies of this email message. Thank you.

--

harish....@latentview.com

unread,
May 17, 2018, 12:02:51 PM5/17/18
to Pyomo Forum
Ya this is just an example..actually i have 1100 variables and those constraint are for the equations such as x4 + x13 +... ==50. Otherwise is it fine carl? Also, could you share your thoughts on the error which i got when i ran for the full data set.
WARNING: Loading a SolverResults object with a warning status into
    model=unknown;
        message from solver=Ipopt 3.11.1\x3a Converged to a locally infeasible
        point. Problem may be infeasible.

Laird, Carl Damon

unread,
May 17, 2018, 12:04:16 PM5/17/18
to pyomo...@googlegroups.com

This error is indicating that the solver could not find a feasible solution. I was pointing out that the constraints were NOT feasible as written.

harish....@latentview.com

unread,
May 18, 2018, 12:41:18 AM5/18/18
to Pyomo Forum
oh okay carl. understood.. thank you for all the help :)

harish....@latentview.com

unread,
May 26, 2018, 7:39:46 AM5/26/18
to Pyomo Forum
Hello Carl & John,

I'm optimizing a column of 130 rows based on few constraints and bounds. I observe only 3 unique values among 130 rows. 
Here they are : [0.01, 0.2, 0.18000113]. Solver has found the optimal solution anyway. It is interesting to note that the system pushes the values of the upper/lower bounds to the rows. Here the bounds are (.01,0.2)..Among the 3 unique values, one of the bounds fill many rows whereas '0.18000113' is observed only at one or two places.
When I do the same optimization in excel I get many unique values with easily 4 decimals (0.1234,0.1453, etc) in each row and very rarely I see the value of either of the bounds. I prefer excel because I'm optimizing the spend for 130 weeks and I would like to see something that excel gives me.
I'm not aware of tweaking pyomo-ipopt solver. Could you provide some help here please? Something like an option for 'refinement steps' .. Thank you!

harish....@latentview.com

unread,
May 28, 2018, 7:31:29 AM5/28/18
to Pyomo Forum
Hello Carl & John,

I'm optimizing a column of 130 rows based on few constraints and bounds. I observe only 3 unique values among 130 rows. 
Here they are : [0.01, 0.2, 0.18000113]. Solver has found the optimal solution anyway. It is interesting to note that the system pushes the values of the upper/lower bounds to the rows. Here the bounds are (.01,0.2)..Among the 3 unique values, one of the bounds fill many rows whereas '0.18000113' is observed only at one or two places.
When I do the same optimization in excel I get many unique values with easily 4 decimals (0.1234,0.1453, etc) in each row and very rarely I see the value of either of the bounds. I prefer excel because I'm optimizing the spend for 130 weeks and I would like to see something that excel gives me.
I'm not aware of tweaking pyomo-ipopt solver. Could you provide some help here please? Something like an option for 'refinement steps' .. Thank you!

On Thursday, 17 May 2018 17:50:06 UTC+5:30, John wrote:

Siirola, John D

unread,
May 29, 2018, 10:58:22 AM5/29/18
to pyomo...@googlegroups.com

The question is really if the two approaches are returning the same objective value.  If they are not, then you have a discrepancy in your model formulations.  If they are, then your model is degenerate: there are multiple – potentially infinitely many – solutions that all evaluate to the same objective value.  In that case, they are all mathematically equivalent and the single solution returned by the solver is arbitrary and depends greatly on the solver implementation. If you have a preference as to the solution you get back, you will need to change your model to break the degeneracy (e.g., through regularization).

 

john

 

From: harish.padmakar via Pyomo Forum [mailto:pyomo...@googlegroups.com]
Sent: Monday, May 28, 2018 5:31 AM
To: Pyomo Forum <pyomo...@googlegroups.com>

Reply all
Reply to author
Forward
0 new messages