Search code examples
pythonpandaspyomo

Extract pyomo results to a csv file


Can anyone help me extract the values from pyomo model into an excel file?

For example for this Objective function. I want the values for the models to be extracted into an excel file but dont know how...

def lastdeckung(model, t):
    return model.eigenproduktion[t] + model.stromimport[t] == model.verbrauch[t]
model.lastdeckung = Constraint(model.n, rule = lastdeckung)

As far as I was able to read online im supposed to create a pandas dataframe and afterwards extract that with the to_csv function.

I was able to get one function into a dataframe with this:

values = [value(model.stromimport[key]) for key in model.stromimport]

But I dont know how to add more...


Solution

  • This is probably better:

    results = pd.DataFrame()
    for v in model.component_objects(Var,active=True):
        for index in v:
            results.at[index, v.name] = value(v[index])