From 027684c59fa59f65de5dc63ed4c43ef90764aae2 Mon Sep 17 00:00:00 2001 From: Thieu Nguyen Date: Fri, 24 May 2024 10:29:49 +0700 Subject: [PATCH] Add plot_2d function in Benchmark class --- opfunu/__init__.py | 2 +- opfunu/benchmark.py | 49 ++++++++++++++++++++++++++++++++++++++++ opfunu/utils/__init__.py | 2 -- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/opfunu/__init__.py b/opfunu/__init__.py index 5c17652..f6bd5c4 100644 --- a/opfunu/__init__.py +++ b/opfunu/__init__.py @@ -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 diff --git a/opfunu/benchmark.py b/opfunu/benchmark.py index 8bb98dc..2e3fe89 100644 --- a/opfunu/benchmark.py +++ b/opfunu/benchmark.py @@ -5,6 +5,7 @@ # --------------------------------------------------% import numpy as np +from opfunu.utils.visualize import draw_2d class Benchmark: @@ -52,6 +53,7 @@ class Benchmark: parametric = True modality = True # Number of ambiguous peaks, unknown # peaks + # n_basins = 1 # n_valleys = 1 @@ -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) diff --git a/opfunu/utils/__init__.py b/opfunu/utils/__init__.py index 8acc67e..849d0cc 100644 --- a/opfunu/utils/__init__.py +++ b/opfunu/utils/__init__.py @@ -3,5 +3,3 @@ # Email: nguyenthieu2102@gmail.com % # Github: https://github.com/thieu1995 % # --------------------------------------------------% - -from .visualize import *