0
$\begingroup$

I am using Pyomo and I have the following lines

outputVariables_list = [model.param1, model.variable1]
optimal_values_list = [[pyo.value(model_item[key]) for key in model_item] for model_item in outputVariables_list] 

When I run it I get a warning that I don't understand:

WARNING: DEPRECATED: Using __getitem__ to return a set value from its
    (ordered) position is deprecated.  Please use at()  (deprecated in 6.1,
    will be removed in 7.0)

I tried the following line but this led to an error:

optimal_values_list = [[pyo.at(model_item[key]) for key in model_item] for model_item in outputVariables_list]

Further, I tried to use pyo.value(model_item.at[key]) and pyo.value(model_item.at(key) and both lead to AttributeError: 'IndexedParam' object has no attribute 'at'.

How can I use the at() method recommended by Pyomo? What I don't understand is that Pyomo tells me to use "at" instead, but this not work as I get errors when using at. This is a quite confusing recommendation. Any idea how can I can integrate the at() method in my code?

Here is the full list of pyomo components that I use can be seen in the following line (the name indicates if it is a parameter, variable or set): outputVariables_list_BT2 = [model.param_helpTimeSlots_BT2, model.variable_heatGenerationCoefficient_SpaceHeating_BT2, model.variable_heatGenerationCoefficient_DHW_BT2, model.variable_help_OnlyOneStorage_BT2, model.variable_temperatureBufferStorage_BT2, model.variable_usableVolumeDHWTank_BT2, model.variable_electricalPowerTotal_BT2, model.variable_pvGeneration_BT2, model.variable_windPowerAssigned_BT2, model.param_heatDemand_In_W_BT2, model.param_DHWDemand_In_W_BT2, model.param_electricalDemand_In_W_BT2, model.param_pvGenerationNominal_BT2, model.param_outSideTemperature_In_C, model.param_windAssignedNominal_BT2, model.param_COPHeatPump_SpaceHeating_BT2, model.param_COPHeatPump_DHW_BT2, model.param_electricityPrice_In_Cents, model.set_timeslots]

Update: What I actually want is to store the output in a pandas dataframe by using the code:

    #Create pandas dataframe for displaying the results of BT2
    outputVariables_list_BT2 = [model.param_helpTimeSlots_BT2, model.variable_heatGenerationCoefficient_SpaceHeating_BT2, model.variable_heatGenerationCoefficient_DHW_BT2, model.variable_help_OnlyOneStorage_BT2, model.variable_temperatureBufferStorage_BT2, model.variable_usableVolumeDHWTank_BT2,  model.variable_electricalPowerTotal_BT2, model.variable_pvGeneration_BT2, model.variable_windPowerAssigned_BT2,  model.param_heatDemand_In_W_BT2, model.param_DHWDemand_In_W_BT2, model.param_electricalDemand_In_W_BT2, model.param_pvGenerationNominal_BT2, model.param_outSideTemperature_In_C, model.param_windAssignedNominal_BT2,  model.param_COPHeatPump_SpaceHeating_BT2, model.param_COPHeatPump_DHW_BT2, model.param_electricityPrice_In_Cents, model.set_timeslots]
    optimal_values_list_BT2 = [[pyo.value(model_item[key]) for key in model_item] for model_item in outputVariables_list_BT2]

    results_BT2 = pd.DataFrame(optimal_values_list_BT2)
    results_BT2= results_BT2.T
    results_BT2 = results_BT2.rename(columns = {0:'timeslot', 1:'variable_heatGenerationCoefficient_SpaceHeating', 2:'variable_heatGenerationCoefficient_DHW', 3:'variable_help_OnlyOneStorage', 4:'variable_temperatureBufferStorage', 5:'variable_usableVolumeDHWTank',    6:'variable_electricalPowerTotal',  7:'variable_pvGeneration', 8:'variable_windPowerAssigned', 9:'param_heatDemand_In_W', 10:'param_DHWDemand_In_W', 11:'param_electricalDemand_In_W', 12:'param_pvGenerationNominal', 13:'param_outSideTemperature_In_C', 14:'param_windAssignedNominal', 15:'param_COPHeatPump_SpaceHeating', 16:'param_COPHeatPump_DHW',  17:'param_PriceElectricity [Cents]', 18:'set_timeslots'})
    cols = ['set_timeslots']
    results_BT2.set_index('set_timeslots', inplace=True)
    results_BT2['variable_temperatureBufferStorage'] = results_BT2['variable_temperatureBufferStorage'].round(2)
    results_BT2['variable_usableVolumeDHWTank'] = results_BT2['variable_usableVolumeDHWTank'].round(1)
    results_BT2['param_COPHeatPump_SpaceHeating'] = results_BT2['param_COPHeatPump_SpaceHeating'].round(3)
    results_BT2['param_COPHeatPump_DHW'] = results_BT2['param_COPHeatPump_DHW'].round(3)
    results_BT2['variable_heatGenerationCoefficient_SpaceHeating'] = results_BT2['variable_heatGenerationCoefficient_SpaceHeating'].round(4)
    results_BT2['variable_heatGenerationCoefficient_DHW'] = results_BT2['variable_heatGenerationCoefficient_DHW'].round(4)
    filePath_BT2 = folderPath + "\Combined_BT2.csv"
    results_BT2.to_csv(filePath_BT2, index=False,  sep =";") 

Actually it works fine but I get this warning that however does not seem to have any effect. Still I am curious why it is popping up.

Reminder: My bounty is about to expire and so far I still have not received any answer that solves the problem. Thus, I'll highly appreciate any further suggestions.

$\endgroup$
7
  • $\begingroup$ I never used Pyomo, but could it be that model.param1 and model.variable1 are different types? From your error messages, set value from its (ordered) position and 'IndexedParam' object does not sound to be referencing the same. Maybe you need model.param1[key] and model.variable1.at(key)? $\endgroup$ Jun 8, 2023 at 6:58
  • $\begingroup$ @PeterBe: if you look at the source code you will see a line like this: "if not self.is_indexed() and (key in self._valid_getitem_keys or type(key) is slice):" if that if statement is true then the warning will pop up. I think there should be something related to the key that you pass to the .value $\endgroup$ Jun 9, 2023 at 3:45
  • $\begingroup$ Also I see that outputVariables_list_BT2 includes both variables and parameters, I don't think .value can be used for the parameters. Why don't you separate those two type of objects and after getting the values recombine them? $\endgroup$ Jun 9, 2023 at 3:47
  • $\begingroup$ If you can give a small example of your code (I know it is hard to share 3000 lines but still small example can be arranged)..., I can definitely look at that. $\endgroup$ Jun 9, 2023 at 3:48
  • $\begingroup$ @OguzToragay: Thanks Oguz for your comment. You are right that the outputVariables_list_BT2 includes both sets, parameters, and variables. I uploaded a little bit more of the code and stated my intension that I eventually want to store the output of the optimization in a pandas dataframe. $\endgroup$
    – PeterBe
    Jun 9, 2023 at 7:38

1 Answer 1

2
+50
$\begingroup$

The issue you have is that your output variables list has objects of different types that have different interfaces to access their data. The objects that are a kind of Set are the ones that give the warning about index access, and in fact shouldn't be accessed in this way.

When you iterate over the Set objects, you get the items in the set. When you iterate over Var or Param objects, you get their index values.

To correct the part of your code that you are struggling with, I suggest the following to build optimal_values_list:

optimal_values_list = []
for model_item in outputVariables_list:
    if isinstance(model_item, Union[pyo.Set, pyo.RangeSet]) :
        optimal_values_list.append(list(model_item))
    else:
        optimal_values_list.append([pyo.value(model_item[k]) for k in model_item.index_set()])

Another approach to getting Pyomo variable and parameter values into a pandas DataFrame can be found in https://stackoverflow.com/a/67494352. Without much work, you can extend the idea to also get parameters, expressions, sets, etc.

$\endgroup$
8
  • $\begingroup$ @pbd: Thanks pbd for your answer. Unforunately your suggested approach yields to the error "NameError: name 'Union' is not defined" $\endgroup$
    – PeterBe
    Jun 11, 2023 at 12:47
  • $\begingroup$ In the top of your program add from typing import Union $\endgroup$
    – pdb5627
    Jun 11, 2023 at 13:18
  • $\begingroup$ Thanks for your answer. I now get the error message " raise TypeError("Subscripted generics cannot be used with" TypeError: Subscripted generics cannot be used with class and instance checks" thrown by the line if isinstance(model_item, Union[pyo.Set, pyo.RangeSet]): $\endgroup$
    – PeterBe
    Jun 12, 2023 at 9:40
  • $\begingroup$ What python version are you using? Try upgrading it to at least 3.10 and check if you're getting the same error $\endgroup$
    – epilefst
    Jun 12, 2023 at 16:04
  • $\begingroup$ Thanks for your answer. When using Python 3.11 it works. But I noticed that you are not using the recommended at() methods from Pyomo. Can you not do this using the at() methods as this is the recommended way? $\endgroup$
    – PeterBe
    Jun 12, 2023 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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