loading data from csv file

655 views
Skip to first unread message

airsq...@gmail.com

unread,
Feb 9, 2016, 1:12:25 AM2/9/16
to Pyomo Forum
Hello-

I'm trying to reconcile the best way to load bunches of table data into a Pyomo model from .csv files.

I note that the import command can be buried in another data file, but I can't get that working properly... and I note that import is deprecated in favor of load, but I can't see any options for pointing the load command at a csv file ...?  

any ideas?

I have something like this in a toy model:

m = AbstractModel()

m.A = Set(initialize=['ddg', 'lcs', 'ffg'])
m.Cost = Param(m.A)
m.Time = Param(m.A)

data = DataPortal()
data.load ????

and a csv file that has something like this:  (first column is index, second and third are parameter values):

A,Cost,Time
ddg,5.6,5
lcs,6.7,8
ffg,8,9


I'm considering shifting gears and using csv reader in core python to load these, but peeling out the multiple parameters from columnar data seems like a pain.

Andres Collart

unread,
Feb 26, 2016, 1:06:24 PM2/26/16
to Pyomo Forum
Hi,

I usually use Pandas to create dictionaries and then import those dictionaries into Pyomo via the initialize function. Hope that helps.

-Andres

aMRo alHarBi

unread,
Oct 7, 2016, 8:51:20 PM10/7/16
to Pyomo Forum
Hi Andres ,

I'm trying to solve the Transport problem, I made a xls_to_pyomo.py file other than the model file (transport.py) to extract the data from xls to lists and dictionaries that match the one in the example. but I don't know how to use the initialize function to call them. also, do I have to import the file xls_to_pyomo.py at the beginning of the model ?

for example : 
in the transpoert.py they used this to define model.i : model.i = Set(initialize=['seattle','san-diego'], doc='Canning plans')
I made a list called plans in xls_to_pyomo.py ( plans=['seattle','san-diego']. ). how should i call this list in transport.py

Also, in the transpoert.py in order to define model.d they made a dictionary in the model file called  dtab and used this line to define model.d model.d = Param(model.i, model.j, initialize=dtab, doc='Distance in thousands of miles').
again how do I call my dictionary that matches dtab but from xls_to_pyomo.py

Thanks!

Andres Collart

unread,
Oct 8, 2016, 9:24:49 AM10/8/16
to pyomo...@googlegroups.com

Hi Amro,

Unforunately Im travelling and only have my phone to respond with, thus no code snippets to really help.

Yes you do need to import xls_to_pyomo. In xls_to_pyomo.py, if you've defined a function that carries out the task of getting the dictionaries and returns those then it's as easy as including at the top of transport.py the following:

from xls_to_pyomo import myfunction as myfunction
Dict1,dict2,...,dict10= myfunction()

Then you can use those dictionaries in transport.py.

Let me know if there's anything else I can help with. Best of luck.

-Andres


--
You received this message because you are subscribed to a topic in the Google Groups "Pyomo Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyomo-forum/_WZTXhxrE38/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyomo-forum+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

aMRo alHarBi

unread,
Oct 10, 2016, 2:14:41 AM10/10/16
to pyomo...@googlegroups.com
Yeah, that works..! Thanks :) 

but it seems to work only in terminal, what it I want it to work in python using : 
if __name__ == '__main__':
    # This emulates what the pyomo command-line tools does
    from pyomo.opt import SolverFactory
    import pyomo.environ
    opt = SolverFactory("gurobi")
    results = opt.solve(model)
    #sends results to stdout
    results.write()
    print("\nDisplaying Solution\n" + '-'*60)
    pyomo_postprocess(None, instance, results)
--

Regards,
Amro Alharbi

Andres Collart

unread,
Oct 13, 2016, 9:06:35 PM10/13/16
to pyomo...@googlegroups.com
That seems like it should work to me. Maybe there are issues with the Gurobi path not being properly set. Hopefully someone else can help in that area.

Best of luck.

Cheers,
Andres
Reply all
Reply to author
Forward
0 new messages