1
$\begingroup$

I would like to display all the results of Pyomo variables in a pandas dataframe. I had a look at this question Pyomo: Looping Over A Variable Method and tried to adjust it to my case without success. I always get error messages. Here is a part of my code:

model.set_timeslots = pyo.RangeSet(1, 288)
iterables = list (range (1, 288))
...
...
...
for v in model.component_objects(pyo.Var, active=True):
    for index in v:
        print(index, ': ', pyo.value(v[index]))

DF = pd.DataFrame(index=iterables)
for v in model.component_objects(pyo.Var, active=True):
    for index in v:
        DF.at[index, 'val'] = pyo.value(v[index]) 

So I would like to print each value of the variables for all 288 set values for the set model.set_timeslots. Further, I also have some variables that do not depend on a set (for example objective variables). I would also like them to be displayed in a pandas dataframe. Do you know how I can do that? I'd appreciate every comment and would be quite thankful for your help.

Reminder: Does nobody have an idea how I can do that?

$\endgroup$
3
  • $\begingroup$ Would you try using this or this as offered by Pyomo? I hope they will be helpful. :) $\endgroup$
    – A.Omidi
    Commented Apr 20, 2021 at 10:40
  • $\begingroup$ Thanks for your comment A.Omidi. So shall I not ask questions about Pyomo in the Operations Research channel? I have seen quite many questions about Pyomo here. $\endgroup$
    – PeterBe
    Commented Apr 20, 2021 at 11:51
  • $\begingroup$ OR.SE is an optimization forum and actually, you can ask your related questions here. What I mentioned is two useful links offered by Pyomo developers in its host. You might get the answer to your technical questions there a bit quickly 😉 $\endgroup$
    – A.Omidi
    Commented Apr 20, 2021 at 19:31

1 Answer 1

2
$\begingroup$

I don't know your entire code or the errors you get, but the general way is

optimal_values = [value(model.x[key]) for key in model.x]
df = pd.DataFrame(optimal_values)

where

model.x

is your target variable that is optimized.

$\endgroup$
17
  • $\begingroup$ Thanks Steven for your answer. Unfortunately I get an error message when using your suggested code optimal_values = [value(model.variable_heatGenerationCoefficient_SpaceHeating[key]) for key in model.variable_heatGenerationCoefficient_SpaceHeating] df = pd.DataFrame(model.variable_heatGenerationCoefficient_SpaceHeating) stating "NameError: name 'value' is not defined". $\endgroup$
    – PeterBe
    Commented Apr 21, 2021 at 9:32
  • $\begingroup$ Further, I have quite many variables in my model (about 30). Is there no way how I can just get the values for all of them together e.g. via a loop? With your approach I would have to specify this for each of the 30 variables which is quite time consuming $\endgroup$
    – PeterBe
    Commented Apr 21, 2021 at 9:33
  • $\begingroup$ your second question: yes ofc, you can put all the variables in a list and then loop over the list (e.g. for model in model_list). I don't get why you get that name error (has something to do with your variable), maybe it's good to share you entire code or at least the crucial parts. $\endgroup$ Commented Apr 21, 2021 at 9:40
  • 1
    $\begingroup$ I don't know what they mean with that error, but the part "for model.model_item in outputVariablesList" should be "for model_item in outputVariablesList" $\endgroup$ Commented Apr 22, 2021 at 7:35
  • 1
    $\begingroup$ Thanks a lot Steven for your tremendous help and effort. It helped a lot. I upvoted and accepted your answer $\endgroup$
    – PeterBe
    Commented Apr 22, 2021 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.