Help ! i want to know how to export the optimization results to excel from the spyder.

1,831 views
Skip to first unread message

sergio balderrama

unread,
Jan 8, 2016, 1:39:51 PM1/8/16
to Pyomo Forum
I am starting in pyomo, and i am modeling the charge and discharge of a battery with different price in the grid. So the objective is to sell at high price and buy when the energy is cheap. There is some constraints about the battery capacity and the energy flow into and from the battery. I perform the optimization for 8760 periods of time. My problem is that I dont know how to save the variables into a csv fill or excel because I want to post-process the data. I use spyder as my development environment. The code that I used is the following:

from pyomo.environ import *

model = AbstractModel()

#SETS

# Time frame of analisis
model.T = Set()

# PARAMETERS


# # Price of buying energy from the grid for each time "t"
model.Cgrid = Param(model.T, within=NonNegativeReals)
# Maximun energy flow for the discharge of the baterry
model.Pmax = Param(within=NonNegativeReals)
# Maximun energy flow for the charge of the battery
model.Pmin = Param(within=NonPositiveReals)

# VARIABLES

# Energy from the discharge of the battery
model.P = Var(model.T, bounds=(-100.0, 150.0))
# State of charge of the battery
model.L = Var(model.T, bounds=(20.0,100.0))


# EQUATIONS

# Objetivo:minimize the total cost of supplying electricity to a home

def Total_cost(model):
    return sum(model.P[t]*model.Cgrid[t] for t in model.T)
model.cost = Objective(rule=Total_cost, sense=maximize)

# Constraints

# State of charge of the baterry
def State(model, t):
    if t==1:
        return model.L[t] == 100 + model.P[t]
    else:       
        return model.L[t] == model.L[t-1] + model.P[t]
model.State_of_charge = Constraint(model.T, rule=State)

 
   
from pyomo.opt import SolverFactory
opt = SolverFactory('glpk')
instance = model.create_instance("/home/sergio/Dropbox/Energia/pyomo/Intership/CargaYDescarga/Draft 1/data3.dat")                              
results = opt.solve(instance)
instance.display()

David Woodruff

unread,
Jan 10, 2016, 12:15:00 PM1/10/16
to pyomo...@googlegroups.com
You can read Python documentation about how to write files, including csv files. There are numerous places in the Pyomo documentation that discuss accessing variable values after a solve, see, e.g.,
https://software.sandia.gov/downloads/pub/pyomo/PyomoOnlineDocs.html#_accessing_variable_values

--
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.

vz101

unread,
Jan 12, 2016, 5:51:30 PM1/12/16
to Pyomo Forum
I cobbled together this code that prints a space separated file. first, the variable name, then the value; one per line. Excel can read that. note that 'cost' is name of my objective function
hope it helps.

with open('H:/folder1/Results.txt', 'w') as f:
    f.write ('{} {}\n'.format("; objective; ", value(model.cost)))
    for v in model.component_objects(Var, active=True):
      varobject = getattr(model, str(v))
      for index in varobject:
          f.write ('{} {}\n'.format(v, varobject[index].value))
Reply all
Reply to author
Forward
0 new messages