Skip to content

Commit

Permalink
bugfix to rebuild_platypus_population (#276)
Browse files Browse the repository at this point in the history
* bugfix to rebuild_platypus_population

decision variables are now properly encoded using the underlying platypus datatypes

* some additional exceptions are now raised

* Update ema_workbench/em_framework/optimization.py

Co-authored-by: Ewout ter Hoeven <E.M.terHoeven@student.tudelft.nl>

* Update ema_workbench/em_framework/optimization.py

Co-authored-by: Ewout ter Hoeven <E.M.terHoeven@student.tudelft.nl>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Ewout ter Hoeven <E.M.terHoeven@student.tudelft.nl>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 14, 2023
1 parent cccad48 commit 3b68895
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions ema_workbench/em_framework/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ def epsilon_nondominated(results, epsilons, problem):
Returns
-------
DataFrame
Notes
-----
this is a platypus based alternative to pareto.py (https://github.com/matthewjwoodruff/pareto.py)
Expand Down Expand Up @@ -872,13 +873,37 @@ def rebuild_platypus_population(archive, problem):
list of platypus Solutions
"""

expected_columns = problem.nvars + problem.nobjs
actual_columns = len(archive.columns)

if actual_columns != expected_columns:
raise EMAError(
f"The number of columns in the archive ({actual_columns}) does not match the "
f"expected number of decision variables and objectives ({expected_columns})."
)

solutions = []
for row in archive.itertuples():
decision_variables = [getattr(row, attr) for attr in problem.parameter_names]
objectives = [getattr(row, attr) for attr in problem.outcome_names]
try:
decision_variables = [getattr(row, attr) for attr in problem.parameter_names]
except AttributeError:
missing_parameters = [
attr for attr in problem.parameter_names if not hasattr(row, attr)
]
raise EMAError(f"parameter names {missing_parameters} not found in archive")

try:
objectives = [getattr(row, attr) for attr in problem.outcome_names]
except AttributeError:
missing_outcomes = [attr for attr in problem.outcome_names if not hasattr(row, attr)]
raise EMAError(f"outcome names {missing_outcomes} not found in archive'")

solution = Solution(problem)
solution.variables = decision_variables
solution.variables = [
platypus_type.encode(value)
for platypus_type, value in zip(problem.types, decision_variables)
]
solution.objectives = objectives
solutions.append(solution)
return solutions
Expand Down

0 comments on commit 3b68895

Please sign in to comment.