-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#71) Consolidate problem settings in base class and integrate pymoo …
…compatibility
- Loading branch information
1 parent
0c4d77e
commit 8ae1f55
Showing
80 changed files
with
1,982 additions
and
1,146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,75 @@ | ||
class AbstractProblem: | ||
""" | ||
An abstract base class for optimization problems. | ||
from abc import ABC, abstractmethod | ||
from typing import List, Tuple, Union, Any | ||
from pymoo.core.problem import Problem | ||
|
||
Methods | ||
------- | ||
f(x) | ||
Evaluates the fitness of a given solution x. | ||
class AbstractProblem(Problem, ABC): | ||
""" | ||
Abstract base class for optimization problems. | ||
""" | ||
|
||
def f(self, x): | ||
def __init__(self, | ||
design_variables: Union[int, List[str]], | ||
bounds: List[Tuple[float, float]], | ||
objectives: Union[str, int, List[str]], | ||
constraints: Union[str, int, List[str]] = []): | ||
""" | ||
Initialize the problem with variables, bounds, objectives, and constraints. | ||
Parameters | ||
---------- | ||
design_variables : int or List[str] | ||
If an integer, it specifies the number of design variables. | ||
If a list of strings, it specifies the names of design variables. | ||
bounds : List[Tuple[float, float]] | ||
Bounds for each design variable as (min, max). | ||
objectives : str, int, or List[str] | ||
Objectives for optimization, e.g., "minimize" or "maximize". | ||
constraints : str, int, or List[str], optional | ||
Constraints for the problem (default is an empty list). | ||
""" | ||
Evaluate the fitness of a given solution x. | ||
# Ensure objectives and constraints are always lists | ||
objectives = [str(objectives)] if isinstance(objectives, (str, int)) else list(objectives) | ||
constraints = [str(constraints)] if isinstance(constraints, (str, int)) else list(constraints) | ||
|
||
# Pymoo-specific attributes | ||
n_var = design_variables if isinstance(design_variables, int) else len(design_variables) | ||
xl = [bound[0] for bound in bounds] | ||
xu = [bound[1] for bound in bounds] | ||
|
||
super().__init__(n_var=n_var, n_obj=len(objectives), n_constr=len(constraints), xl=xl, xu=xu) | ||
|
||
# Custom attributes | ||
self.design_variables = [f"x{i+1}" for i in range(n_var)] if isinstance(design_variables, int) else design_variables | ||
self.bounds = bounds | ||
self.objectives = objectives | ||
self.constraints = constraints | ||
|
||
@abstractmethod | ||
def f(self, x: List[Any]) -> float: | ||
""" | ||
Abstract method for evaluating the fitness of a solution. | ||
Parameters | ||
---------- | ||
x : list | ||
A list representing a candidate solution. | ||
List of design variable values. | ||
Returns | ||
------- | ||
float | ||
The fitness value of the candidate solution. | ||
Fitness value. | ||
""" | ||
raise NotImplementedError("Subclasses should implement this method.") | ||
|
||
Raises | ||
------ | ||
NotImplementedError | ||
If the method is not implemented by a subclass. | ||
def evaluate(self, x, out, *args, **kwargs): | ||
""" | ||
Evaluate function for compatibility with pymoo's optimizer. | ||
Parameters | ||
---------- | ||
x : numpy.ndarray | ||
Array of input variables. | ||
out : dict | ||
Dictionary to store the output fitness values. | ||
""" | ||
raise NotImplementedError() | ||
out["F"] = self.f(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.