1
$\begingroup$

I would like to create a pandas dataframe from the results (and the parameters) of an optimization problem and create a csv file out of it. In my previous example (1-dimensional case) I have just one set "set_timeslots" and all parameters and variables depend on that (this is the 1-dimensional case). For this basically everything works as it should by using this code (extract):

#Define the sets
model.set_timeslots = pyo.RangeSet(1, 288)
.....................................
#Variable definition
model.variable_heatGenerationCoefficient_SpaceHeating = pyo.Var(model.set_timeslots, bounds=(0,1))
model.variable_temperatureBufferStorage = pyo.Var(model.set_timeslots, bounds=(0 , 20))
model.param_heatDemand_In_W = pyo.Param(model.set_timeslots, initialize=dictionaryHeatDemand_In_W)
..........................
#After having solved the model
   outputVariables_list = [model.variable_heatGenerationCoefficient_SpaceHeating, model.variable_temperatureBufferStorage, model.param_heatDemand_In_W, model.set_timeslots]
    optimal_values_list = [[pyo.value(model_item[key]) for key in model_item] for model_item in outputVariables_list] 
    results = pd.DataFrame(optimal_values_list)
    results= results.T
    results = results.rename(columns = {0:'variable_heatGenerationCoefficient_SpaceHeating', 1:'variable_temperatureBufferStorage', 2:'param_heatDemand_In_W', 3:'set_timeslots'})
    cols = ['set_timeslots']
    results[cols]= results [cols].round(0).astype(int)
    results.set_index('set_timeslots', inplace=True)
    results.to_csv('Results.csv', index=True,  sep =";") 

You do not have to care about the assignment of the parameters and the model itself. It is just about printing the results after having solved the model

The problem is how can I extend that to 2-dimensional variables and parameters that depend on 2 sets. As before the set "timeslots" but also the set "set_buildings_BT1". This means that we have for every building 288 timeslots and for each of them I have a value of the parameters and the variables

#Define the sets
model.set_timeslots = pyo.RangeSet(1, 288)
model.set_buildings_BT1 = pyo.RangeSet(1, 3)

#Index of the buildings
indexOfBuildingsOverall_BT1 =  [0,1,2]

#Define variables and parameters
def init_heatDemand (model, i,j):
    return combinedDataframe_heatDemand_BT1.iloc[j-1, i-1]

model.param_heatDemand_In_W = pyo.Param(model.set_buildings_BT1, model.set_timeslots, mutable = True, initialize=init_heatDemand)

model.variable_heatGenerationCoefficient_SpaceHeating = pyo.Var(model.set_buildings_BT1, model.set_timeslots,bounds=(0,1))
model.variable_temperatureBufferStorage = pyo.Var(model.set_buildings_BT1, model.set_timeslots,  bounds=(0 , 20 ))

    #After having solved the model
    for index_BT1 in indexOfBuildingsOverall_BT1:
       outputVariables_list = [model.variable_heatGenerationCoefficient_SpaceHeating [index_BT1 + 1], model.variable_temperatureBufferStorage [index_BT1 + 1], model.param_heatDemand_In_W [index_BT1 + 1], model.set_timeslots]
        optimal_values_list = [[pyo.value(model_item[key]) for key in model_item] for model_item in outputVariables_list] 
        results = pd.DataFrame(optimal_values_list)
        results= results.T
        results = results.rename(columns = {0:'variable_heatGenerationCoefficient_SpaceHeating', 1:'variable_temperatureBufferStorage', 2:'param_heatDemand_In_W', 3:'set_timeslots'})
        cols = ['set_timeslots']
        results[cols]= results [cols].round(0).astype(int)
        results.set_index('set_timeslots', inplace=True)
        results.to_csv('Results\Result_BT1_Index_' + str(index_BT1) + '.csv', index=True,  sep =";") 

I tried to loop over the index_BT1 in indexOfBuildingsOverall_BT1 and access the values of the variables and parameters for each building by e.g. model.variable_heatGenerationCoefficient_SpaceHeating [index_BT1 + 1] (I tried it also without +1). However, I get the error "KeyError: "Index '1' is not valid for indexed component 'variable_heatGenerationCoefficient_SpaceHeating'"". Do you know why I get this error and how I can tackle this? I'd appreciate every comment.

Reminder: Does anybody have an idea how I can do this? I'd appreciate every suggestion.

$\endgroup$
1
  • 1
    $\begingroup$ People at stackoverflow usually would ask for a "minimal reproducible example". It might be better if you can provide a minimal program that people can directly run to reproduce the problem. The model is mostly irrelevant to the question. So you can cut the model down to like 1 - 2 equations. Similarly, saving to .csv file is not related to the problem. $\endgroup$ – Qurious Cube Jun 22 at 19:11
2
$\begingroup$

In your specific instance you use only a single index for a two-dimensional variable matrix, causing the KeyError.

You should loop over the index sets from your model to produce the results, so for example: outputVariables_list = [model.variable_heatGenerationCoefficient_SpaceHeating[building, time_slot], model.variable_temperatureBufferStorage[building, time_slot], model.param_heatDemand_In_W[building, time_slot] for building in model.set_buildings_BT1 for time_slot in model.set_timeslots]

$\endgroup$
1
  • $\begingroup$ Thanks for your answer Arjan. I upvoted it. $\endgroup$ – PeterBe Jun 25 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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