creating variables with increasing integersHow do I determine the size of an object in Python?How do I create a variable number of variables?Find out how much memory is being used by an object in PythonHow to increment variable names/Is this a bad ideaAre static class variables possible in Python?How can I safely create a nested directory?Using global variables in a functionWhat is the scope of variables in JavaScript?How do you check if a variable is an array in JavaScript?Converting integer to string?How do I pass a variable by reference?How to determine if variable is 'undefined' or 'null'?How to check if a variable is set in Bash?JavaScript check if variable exists (is defined/initialized)
Why don't electrons take the shorter path in coils
Why is 日本 read as "nihon" but not "nitsuhon"?
How do I find the fastest route from Heathrow to an address in London using all forms of transport?
Why don't we use Cavea-B
Why doesn't the Falcon-9 first stage use three legs to land?
Were there 486SX revisions without an FPU on the die?
What is the evidence on the danger of feeding whole blueberries and grapes to infants and toddlers?
Solve a logarithmic equation by NSolve
In an emergency, how do I find and share my position?
Was Tuvok bluffing when he said that Voyager's transporters rendered the Kazon weapons useless?
Potential new partner angry about first collaboration - how to answer email to close up this encounter in a graceful manner
Efficiently pathfinding many flocking enemies around obstacles
How should I face my manager if I make a mistake because a senior coworker explained something incorrectly to me?
Why did MS-DOS applications built using Turbo Pascal fail to start with a division by zero error on faster systems?
What does it mean to have a subnet mask /32?
Sleeping solo in a double sleeping bag
Avoiding racist tropes in fantasy
Give function defaults arguments from a dictionary in Python
What is this symbol: semicircles facing eachother
Check in to 2 hotels at same location
On the feasibility of space battleships
How big would a Daddy Longlegs Spider need to be to kill an average Human?
Can a character spend multiple hit dice at level 1?
LeetCode: Pascal's Triangle C#
creating variables with increasing integers
How do I determine the size of an object in Python?How do I create a variable number of variables?Find out how much memory is being used by an object in PythonHow to increment variable names/Is this a bad ideaAre static class variables possible in Python?How can I safely create a nested directory?Using global variables in a functionWhat is the scope of variables in JavaScript?How do you check if a variable is an array in JavaScript?Converting integer to string?How do I pass a variable by reference?How to determine if variable is 'undefined' or 'null'?How to check if a variable is set in Bash?JavaScript check if variable exists (is defined/initialized)
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I want to create a pyomo model with 1000 basic pyomo variables.
I know it is a bad idea to do this like the following script.
And it is also not working. I hope you understand the idea and be able to help me.
import pyomo.core as pyomo
def create_model_a():
m = pyomo.ConcreteModel()
for i in range(1000):
m.var_i = pyomo.Var(within=pyomo.NonNegativeReals)
return m
so basically instead of writing m.var_0 = ...
to m.var_999 = ...
, I used a for loop and of course in this way it is not working but the idea is creating 1000 variables without hard-coding m.var_0
, m.var_1
, m.var_2
, and so on till m.var_999
. How can I do it?
I want to create this not to model anything but I wanna use memory profile on this function to understand how much memory is needed for a pyomo model with 1000 variables.
Ps: I tried following and it is not working (cannot see any declarations when I cast m.pprint()
):
def create_model_a():
m = pyomo.ConcreteModel()
m.var =
for i in range(1000):
m.var[i] = pyomo.Var(within=pyomo.NonNegativeReals)
return m
PS2: checked also How to increment variable names/Is this a bad idea and How do I create a variable number of variables? ... sadly no help
python variables declaration pyomo
add a comment |
I want to create a pyomo model with 1000 basic pyomo variables.
I know it is a bad idea to do this like the following script.
And it is also not working. I hope you understand the idea and be able to help me.
import pyomo.core as pyomo
def create_model_a():
m = pyomo.ConcreteModel()
for i in range(1000):
m.var_i = pyomo.Var(within=pyomo.NonNegativeReals)
return m
so basically instead of writing m.var_0 = ...
to m.var_999 = ...
, I used a for loop and of course in this way it is not working but the idea is creating 1000 variables without hard-coding m.var_0
, m.var_1
, m.var_2
, and so on till m.var_999
. How can I do it?
I want to create this not to model anything but I wanna use memory profile on this function to understand how much memory is needed for a pyomo model with 1000 variables.
Ps: I tried following and it is not working (cannot see any declarations when I cast m.pprint()
):
def create_model_a():
m = pyomo.ConcreteModel()
m.var =
for i in range(1000):
m.var[i] = pyomo.Var(within=pyomo.NonNegativeReals)
return m
PS2: checked also How to increment variable names/Is this a bad idea and How do I create a variable number of variables? ... sadly no help
python variables declaration pyomo
Can't you use a dict? (I just noticed it's in your answer actually, not sure why it's not working). Something like this should probably work though:setattr(m, 'var_'+str(i), pyomo.Var(within=pyomo.NonNegativeReals))
– Peter
Mar 27 at 15:59
add a comment |
I want to create a pyomo model with 1000 basic pyomo variables.
I know it is a bad idea to do this like the following script.
And it is also not working. I hope you understand the idea and be able to help me.
import pyomo.core as pyomo
def create_model_a():
m = pyomo.ConcreteModel()
for i in range(1000):
m.var_i = pyomo.Var(within=pyomo.NonNegativeReals)
return m
so basically instead of writing m.var_0 = ...
to m.var_999 = ...
, I used a for loop and of course in this way it is not working but the idea is creating 1000 variables without hard-coding m.var_0
, m.var_1
, m.var_2
, and so on till m.var_999
. How can I do it?
I want to create this not to model anything but I wanna use memory profile on this function to understand how much memory is needed for a pyomo model with 1000 variables.
Ps: I tried following and it is not working (cannot see any declarations when I cast m.pprint()
):
def create_model_a():
m = pyomo.ConcreteModel()
m.var =
for i in range(1000):
m.var[i] = pyomo.Var(within=pyomo.NonNegativeReals)
return m
PS2: checked also How to increment variable names/Is this a bad idea and How do I create a variable number of variables? ... sadly no help
python variables declaration pyomo
I want to create a pyomo model with 1000 basic pyomo variables.
I know it is a bad idea to do this like the following script.
And it is also not working. I hope you understand the idea and be able to help me.
import pyomo.core as pyomo
def create_model_a():
m = pyomo.ConcreteModel()
for i in range(1000):
m.var_i = pyomo.Var(within=pyomo.NonNegativeReals)
return m
so basically instead of writing m.var_0 = ...
to m.var_999 = ...
, I used a for loop and of course in this way it is not working but the idea is creating 1000 variables without hard-coding m.var_0
, m.var_1
, m.var_2
, and so on till m.var_999
. How can I do it?
I want to create this not to model anything but I wanna use memory profile on this function to understand how much memory is needed for a pyomo model with 1000 variables.
Ps: I tried following and it is not working (cannot see any declarations when I cast m.pprint()
):
def create_model_a():
m = pyomo.ConcreteModel()
m.var =
for i in range(1000):
m.var[i] = pyomo.Var(within=pyomo.NonNegativeReals)
return m
PS2: checked also How to increment variable names/Is this a bad idea and How do I create a variable number of variables? ... sadly no help
python variables declaration pyomo
python variables declaration pyomo
edited Mar 27 at 15:40
oakca
asked Mar 27 at 15:33
oakcaoakca
3971 silver badge14 bronze badges
3971 silver badge14 bronze badges
Can't you use a dict? (I just noticed it's in your answer actually, not sure why it's not working). Something like this should probably work though:setattr(m, 'var_'+str(i), pyomo.Var(within=pyomo.NonNegativeReals))
– Peter
Mar 27 at 15:59
add a comment |
Can't you use a dict? (I just noticed it's in your answer actually, not sure why it's not working). Something like this should probably work though:setattr(m, 'var_'+str(i), pyomo.Var(within=pyomo.NonNegativeReals))
– Peter
Mar 27 at 15:59
Can't you use a dict? (I just noticed it's in your answer actually, not sure why it's not working). Something like this should probably work though:
setattr(m, 'var_'+str(i), pyomo.Var(within=pyomo.NonNegativeReals))
– Peter
Mar 27 at 15:59
Can't you use a dict? (I just noticed it's in your answer actually, not sure why it's not working). Something like this should probably work though:
setattr(m, 'var_'+str(i), pyomo.Var(within=pyomo.NonNegativeReals))
– Peter
Mar 27 at 15:59
add a comment |
1 Answer
1
active
oldest
votes
If you really want to understand the memory implications of having many Pyomo variables you should compare the case where you have many singleton variables with the case where you have one large indexed variable. Examples of both are below:
# Make a large indexed variable
m = ConcreteModel()
m.s = RangeSet(1000)
m.v = Var(m.s, within=NonNegativeReals)
# Make many singleton variables
m = ConcreteModel()
for i in range(1000):
name = 'var_' + str(i)
m.add_component(name, Var(within=NonNegativeReals))
this was definitly what I was looking for ty. Btw how do you measure memory of the firstm
and the secondm
with a package or something else? + I was also comparing it :) just had a problem where I did not want to hard code these 1000 variables. but ty!!
– oakca
Mar 27 at 16:06
Check out these other questions on determining the size of Python objects: stackoverflow.com/questions/449560/…, stackoverflow.com/questions/33978/…
– Bethany Nicholson
Mar 27 at 16:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55381023%2fcreating-variables-with-increasing-integers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you really want to understand the memory implications of having many Pyomo variables you should compare the case where you have many singleton variables with the case where you have one large indexed variable. Examples of both are below:
# Make a large indexed variable
m = ConcreteModel()
m.s = RangeSet(1000)
m.v = Var(m.s, within=NonNegativeReals)
# Make many singleton variables
m = ConcreteModel()
for i in range(1000):
name = 'var_' + str(i)
m.add_component(name, Var(within=NonNegativeReals))
this was definitly what I was looking for ty. Btw how do you measure memory of the firstm
and the secondm
with a package or something else? + I was also comparing it :) just had a problem where I did not want to hard code these 1000 variables. but ty!!
– oakca
Mar 27 at 16:06
Check out these other questions on determining the size of Python objects: stackoverflow.com/questions/449560/…, stackoverflow.com/questions/33978/…
– Bethany Nicholson
Mar 27 at 16:59
add a comment |
If you really want to understand the memory implications of having many Pyomo variables you should compare the case where you have many singleton variables with the case where you have one large indexed variable. Examples of both are below:
# Make a large indexed variable
m = ConcreteModel()
m.s = RangeSet(1000)
m.v = Var(m.s, within=NonNegativeReals)
# Make many singleton variables
m = ConcreteModel()
for i in range(1000):
name = 'var_' + str(i)
m.add_component(name, Var(within=NonNegativeReals))
this was definitly what I was looking for ty. Btw how do you measure memory of the firstm
and the secondm
with a package or something else? + I was also comparing it :) just had a problem where I did not want to hard code these 1000 variables. but ty!!
– oakca
Mar 27 at 16:06
Check out these other questions on determining the size of Python objects: stackoverflow.com/questions/449560/…, stackoverflow.com/questions/33978/…
– Bethany Nicholson
Mar 27 at 16:59
add a comment |
If you really want to understand the memory implications of having many Pyomo variables you should compare the case where you have many singleton variables with the case where you have one large indexed variable. Examples of both are below:
# Make a large indexed variable
m = ConcreteModel()
m.s = RangeSet(1000)
m.v = Var(m.s, within=NonNegativeReals)
# Make many singleton variables
m = ConcreteModel()
for i in range(1000):
name = 'var_' + str(i)
m.add_component(name, Var(within=NonNegativeReals))
If you really want to understand the memory implications of having many Pyomo variables you should compare the case where you have many singleton variables with the case where you have one large indexed variable. Examples of both are below:
# Make a large indexed variable
m = ConcreteModel()
m.s = RangeSet(1000)
m.v = Var(m.s, within=NonNegativeReals)
# Make many singleton variables
m = ConcreteModel()
for i in range(1000):
name = 'var_' + str(i)
m.add_component(name, Var(within=NonNegativeReals))
answered Mar 27 at 15:54
Bethany NicholsonBethany Nicholson
1,4681 gold badge4 silver badges13 bronze badges
1,4681 gold badge4 silver badges13 bronze badges
this was definitly what I was looking for ty. Btw how do you measure memory of the firstm
and the secondm
with a package or something else? + I was also comparing it :) just had a problem where I did not want to hard code these 1000 variables. but ty!!
– oakca
Mar 27 at 16:06
Check out these other questions on determining the size of Python objects: stackoverflow.com/questions/449560/…, stackoverflow.com/questions/33978/…
– Bethany Nicholson
Mar 27 at 16:59
add a comment |
this was definitly what I was looking for ty. Btw how do you measure memory of the firstm
and the secondm
with a package or something else? + I was also comparing it :) just had a problem where I did not want to hard code these 1000 variables. but ty!!
– oakca
Mar 27 at 16:06
Check out these other questions on determining the size of Python objects: stackoverflow.com/questions/449560/…, stackoverflow.com/questions/33978/…
– Bethany Nicholson
Mar 27 at 16:59
this was definitly what I was looking for ty. Btw how do you measure memory of the first
m
and the second m
with a package or something else? + I was also comparing it :) just had a problem where I did not want to hard code these 1000 variables. but ty!!– oakca
Mar 27 at 16:06
this was definitly what I was looking for ty. Btw how do you measure memory of the first
m
and the second m
with a package or something else? + I was also comparing it :) just had a problem where I did not want to hard code these 1000 variables. but ty!!– oakca
Mar 27 at 16:06
Check out these other questions on determining the size of Python objects: stackoverflow.com/questions/449560/…, stackoverflow.com/questions/33978/…
– Bethany Nicholson
Mar 27 at 16:59
Check out these other questions on determining the size of Python objects: stackoverflow.com/questions/449560/…, stackoverflow.com/questions/33978/…
– Bethany Nicholson
Mar 27 at 16:59
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55381023%2fcreating-variables-with-increasing-integers%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can't you use a dict? (I just noticed it's in your answer actually, not sure why it's not working). Something like this should probably work though:
setattr(m, 'var_'+str(i), pyomo.Var(within=pyomo.NonNegativeReals))
– Peter
Mar 27 at 15:59