Skip to content

Commit

Permalink
Add CECObjectiveFunction class that allows for calling all CEC functi…
Browse files Browse the repository at this point in the history
…ons. Switch to opfunu from cec2017 dependency.
  • Loading branch information
mlojek committed Jan 14, 2025
1 parent 306029e commit ca98150
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 18 deletions.
6 changes: 3 additions & 3 deletions docs/optilab.functions.benchmarks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ optilab.functions.benchmarks package
Submodules
----------

optilab.functions.benchmarks.cec2017\_objective\_function module
----------------------------------------------------------------
optilab.functions.benchmarks.cec\_objective\_function module
------------------------------------------------------------

.. automodule:: optilab.functions.benchmarks.cec2017_objective_function
.. automodule:: optilab.functions.benchmarks.cec_objective_function
:members:
:undoc-members:
:show-inheritance:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ cma==4.0.0
typing
black
isort
opfunu==1.0.1
pylint
pytest
jsonschema
tabulate
pandas==2.2.3
tqdm
git+https://github.com/tilleyd/cec2017-py
scikit-learn==1.5.2
shapely==2.0.6
pre-commit
Expand Down
2 changes: 1 addition & 1 deletion src/optilab/functions/benchmarks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Optimization benchmarks functions.
"""

from .cec2017_objective_function import CEC2017ObjectiveFunction
from .cec_objective_function import CECObjectiveFunction
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
"""
Objective function from CEC 2017 benchmark.
Objective functions from CEC benchmarks.
"""

from cec2017.functions import all_functions
import opfunu

from ...data_classes import FunctionMetadata, Point
from ..objective_function import ObjectiveFunction


class CEC2017ObjectiveFunction(ObjectiveFunction):
class CECObjectiveFunction(ObjectiveFunction):
"""
Objective function from CEC 2017 benchmark.
Objective functions from CEC benchmarks.
"""

def __init__(self, function_num: int, dim: int) -> None:
def __init__(self, year: int, function_num: int, dim: int) -> None:
"""
Class constructor.
Args:
function_num (int): The number of CEC2017 function, The range is 1 to 30.
year (int): Year of the CEC competition.
function_num (int): The number of benchmark function.
dim (int): Dimensionality of the function.
"""
if not function_num in range(1, 31):
raise ValueError(f"Invalid cec2017 function number {function_num}.")

super().__init__(f"cec2017_f{function_num}", dim)
super().__init__(f"cec{year}_f{function_num}", dim)

self.function_num = function_num
self.function = all_functions[function_num - 1]
self.minimum = function_num * 100
self.function = opfunu.get_functions_by_classname(f"F{function_num}{year}")[0](
ndim=dim
)

def get_metadata(self) -> FunctionMetadata:
"""
Expand Down Expand Up @@ -57,6 +56,6 @@ def __call__(self, point: Point) -> Point:
super().__call__(point)
return Point(
x=point.x,
y=float(self.function([point.x])[0]) - self.minimum,
y=self.function.evaluate(point.x) - self.function.f_global,
is_evaluated=True,
)

0 comments on commit ca98150

Please sign in to comment.