Skip to content

Commit

Permalink
Add plot_2d function in Benchmark class
Browse files Browse the repository at this point in the history
  • Loading branch information
thieu1995 committed May 24, 2024
1 parent fa489db commit 027684c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion opfunu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

import inspect
import re
from .utils import *
from .utils.visualize import draw_2d
from . import name_based
from . import cec_based

Expand Down
49 changes: 49 additions & 0 deletions opfunu/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# --------------------------------------------------%

import numpy as np
from opfunu.utils.visualize import draw_2d


class Benchmark:
Expand Down Expand Up @@ -52,6 +53,7 @@ class Benchmark:
parametric = True

modality = True # Number of ambiguous peaks, unknown # peaks

# n_basins = 1
# n_valleys = 1

Expand Down Expand Up @@ -250,3 +252,50 @@ def create_solution(self) -> np.ndarray:
The random solution
"""
return np.random.uniform(self.lb, self.ub)

def plot_2d(self, selected_dims=None, n_points=500, ct_cmap="viridis", ct_levels=30, ct_alpha=0.7, fixed_strategy="mean", fixed_values=None,
title="Contour map of the function", x_label=None, y_label=None, filename=None, exts=(".png", ".pdf"), verbose=True):
"""
Draw 2D contour of the function.
Parameters
----------
selected_dims : list, tuple, np.ndarray
The selected dimensions you want to draw.
If your function has only 2 dimensions, it will select (1, 2) automatically.
n_points : int
The number of points that will be used to draw the contour
ct_cmap : str
The cmap of matplotlib.pyplot.contourf function (https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.contourf.html)
ct_levels : int
The levels parameter of contourf function
ct_alpha : float
The alpha parameter of contourf function
fixed_strategy : str
The selected strategy to set values for other dimensions.
When your function has > 2 dimensions, you need to set a fixed value for other dimensions to be able to calculate value.
List of available strategy: ["min", "max", "mean', "values", "zero"]
+ min: Set the other dimensions by its lower bound
+ max: Set the other dimensions by its upper bound
+ mean: Set the other dimensions by it average value (lower bound + upper bound) / 2
+ zero: Set the other dimensions by 0
+ values: Set the other dimensions by your passed values through the parameter: `fixed_values`.
fixed_values : list, tuple, np.ndarray
Fixed values for all dimensions (length should be the same as lower bound), the selected dimensions will be replaced in the drawing process.
title : str
Title for the figure
x_label : str
Set the x label
y_label : str
Set the y label
filename : str, default = None
Set the file name, If None, the file will not be saved
exts : list, tuple, np.ndarray
The list of extensions to save file, for example: (".png", ".pdf", ".jpg")
verbose : bool
Show the figure or not. It will not show on linux system.
"""
draw_2d(self.evaluate, self.lb, self.ub, selected_dims=selected_dims, n_points=n_points,
ct_cmap=ct_cmap, ct_levels=ct_levels, ct_alpha=ct_alpha, fixed_strategy=fixed_strategy, fixed_values=fixed_values,
title=title, x_label=x_label, y_label=y_label, filename=filename, exts=exts, verbose=verbose)
2 changes: 0 additions & 2 deletions opfunu/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@
# Email: [email protected] %
# Github: https://github.com/thieu1995 %
# --------------------------------------------------%

from .visualize import *

0 comments on commit 027684c

Please sign in to comment.