Pyomo output into Excel

235 views
Skip to first unread message

Akmal N

unread,
Sep 10, 2018, 3:37:59 AM9/10/18
to Pyomo Forum
Hello,

I would like to know if it is possible to convert the Pyomo output (solution) into Excel. I heard that with Pandas it is possible. 

If you know  how do it, please share your knowledge

Akmal

Niki T.

unread,
Aug 4, 2021, 7:07:11 AM8/4/21
to Pyomo Forum
Hi,

Did you figure this out because I am looking for a way to output my solution to excel as well? 

Thanks

David Garmroudi

unread,
Aug 4, 2021, 7:42:55 AM8/4/21
to pyomo...@googlegroups.com
hi
I extract the variable's value after simulation and add it to the dataframe. then I use common pandas codes to write it to an excel file.
something like this:
results['monoBuy(KWh)'] =  [model.Purchase_from_Grid[i]() for i in model.T]

I hope it helps.

Bests
Davoud G.

--
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/5184aaa2-2a95-4fb7-8452-70ac1c706308n%40googlegroups.com.

Optimization team

unread,
Aug 5, 2021, 5:25:14 AM8/5/21
to Pyomo Forum
If you use Abstract model then do 
Screenshot 2021-08-05 at 10.20.29.png



Online Pyomo Course : https://tinyurl.com/2ter6ptj
Telegram-channel : https://t.me/PyomoChannel 


Message has been deleted

Niki T.

unread,
Aug 5, 2021, 9:48:06 AM8/5/21
to Pyomo Forum
Thank you very much everyone!

But what about 3D variables? I have this variable INM[p,m,t] and I want to display it as it is in the screenshot below. I figured it out for 2D variables, but I cannot for 3D.

Screenshot 2021-08-05 144254.png

My interpretation for the 2D variables is this:


df = pd.DataFrame(index=pd.Index([m for m in instance.m],name='CAP(m,t)'))
for t in instance.t:
    df['t{}'.format(t)] = [value(instance.CAP[m,t]) for m in instance.m]
    df.to_excel(writer,sheet_name='200_A',startrow=10, startcol=0)
    

Daniel Gebbran

unread,
Aug 5, 2021, 10:01:35 AM8/5/21
to Pyomo Forum
Hi Nicky,

If I understand correctly, in this case, it is not about Excel, but about how does one "visualize" and correlate a three-dimensional array with other data structures.
You could print according to indexes... but honestly, I would recommend first you figure out what kind of data processing you need out of this data, and pre-processing it in Python using pandas itself before going back to excel.
For instance, focus on ordering which outputs and which order you want out of them.

Best of luck,
Daniel

Niki T.

unread,
Aug 6, 2021, 7:34:31 AM8/6/21
to Pyomo Forum
Yes exactly it's about how to visualize this 3-dimensional variable into a matrix of the format I uploaded above. I figured it out about the 2-dimensional variables, but I am still struggling for the 3-dimensional ones where I need to have 2 index columns p and c for every t.
If anyone has any idea, I would really appreciate it.

Giuseppe Stecca

unread,
Aug 7, 2021, 8:54:11 PM8/7/21
to pyomo...@googlegroups.com

Suppose to have a variable x[(i,j,k)], so the code would be

##################
import pandas as pd

filename = 'myfile.xlsx'
ii = 0
writer = pd.ExcelWriter(filename, engine='xlsxwriter')
df = pd.DataFrame(columns=['i', 'j', 'x'])
for i in model.x.keys():
    val = pyo.value(model.x[i])
    print("x[{}] = {}".format(i, val))
    df.loc[ii] = [i[0], i[1], val]
    ii+=1
df.to_excel(writer, sheet_name='x', index=None)
writer.close()
###################

In this way you can save different sheets in the same excel file

If you want to save results such as time, object value and so on in a different file you can do as follows:


dfres = pd.DataFrame(columns=['ObjVal', 'Time', 'GAP'])
dfres.loc[0] = [myobjval, mytimeval, mygap]  # variables storing values of the run
dfres.to_excel('myresults.xlsx', index = None)


Hopes this can help
--
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/5184aaa2-2a95-4fb7-8452-70ac1c706308n%40googlegroups.com.


-- 
****************************************************************
Giuseppe Stecca, Research Scientist
Istituto di Analisi dei Sistemi ed Informatica "Antonio Ruberti"
Consiglio Nazionale delle Ricerche, IASI - CNR
(Institute for Systems Analysis and Computer Science)

Chair in Supply Chain Management at
University of Rome Tor Vergata, Department of Enterprise Engineering

Via dei Taurini 19
00185 Roma
Italy

email: giusepp...@iasi.cnr.it, giusepp...@uniroma2.it
web: http://www.iasi.cnr.it/~stecca/
skype: gius237
ph.:    +39 06 4993 7127
mob.: +39 338 1794 305
****************************************************************

Sebasta Joséi

unread,
Aug 8, 2021, 8:35:11 PM8/8/21
to pyomo...@googlegroups.com
https://github.com/SebastianJHM/dev/blob/main/Python/Optimization%20Pyomo/Asignacion%20Read%20and%20Write/AllocationRW.py
In this file is an example, look the function print_results_XLSX_Alloc
In this example I use openpyxl library, the excel file result is in the same folder.
Reply all
Reply to author
Forward
0 new messages