Skip to content

Commit

Permalink
Saving cost values and constraint multipliers in nested dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
S-Dafarra committed Jul 5, 2024
1 parent df9a72f commit 714c767
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/hippopt/base/problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,26 @@ def __post_init__(
self.constraint_multipliers = _constraint_multipliers

def to_dict(self) -> dict:
def set_nested_value(d, input_key, value):
keys = input_key.split(".")
assert all(isinstance(k, str) and len(k) > 0 for k in keys)
for key in keys[:-1]:
d = d.setdefault(key, {})
d[keys[-1]] = value

def flatten_to_nested_dict(flat_dict):
nested_dict = {}
for key, value in flat_dict.items():
set_nested_value(nested_dict, key, value)
return nested_dict

return {
"values": self.values.to_dict(flatten=False),
"cost_value": self.cost_value,
"cost_values": self.cost_values,
"constraint_multipliers": self.constraint_multipliers,
"cost_values": flatten_to_nested_dict(self.cost_values),
"constraint_multipliers": flatten_to_nested_dict(
self.constraint_multipliers
),
}


Expand Down

0 comments on commit 714c767

Please sign in to comment.