Skip to content

Commit

Permalink
Add examples for visualize module
Browse files Browse the repository at this point in the history
  • Loading branch information
thieu1995 committed May 24, 2024
1 parent 61290d7 commit 37110ae
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
28 changes: 28 additions & 0 deletions examples/visualize/custom_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python
# Created by "Thieu" at 17:03, 24/05/2024 ----------%
# Email: [email protected] %
# Github: https://github.com/thieu1995 %
# --------------------------------------------------%

from opfunu import draw_2d, draw_3d

## Define a custom function, for example. I will use mealpy problem as an example
from mealpy import Problem, FloatVar
import numpy as np

# Our custom problem class
class Squared(Problem):
def __init__(self, bounds=None, minmax="min", data=None, **kwargs):
self.data = data
super().__init__(bounds, minmax, **kwargs)

def obj_func(self, solution):
x = self.decode_solution(solution)["my_var"]
return np.sum(x ** 2)

bound = FloatVar(lb=(-10., )*20, ub=(10., )*20, name="my_var")
custom_squared = Squared(bounds=bound, minmax="min", data="Amazing", name="Squared")

## Visualize function using utility function
draw_2d(custom_squared.obj_func, custom_squared.lb, custom_squared.ub, selected_dims=(2, 3), n_points=300)
draw_3d(custom_squared.obj_func, custom_squared.lb, custom_squared.ub, selected_dims=(2, 3), n_points=300)
18 changes: 18 additions & 0 deletions examples/visualize/inside_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env python
# Created by "Thieu" at 16:55, 24/05/2024 ----------%
# Email: [email protected] %
# Github: https://github.com/thieu1995 %
# --------------------------------------------------%

from opfunu.cec_based import F12010

# Visualize opfunu function using method in object
f0 = F12010()
f0.plot_2d(selected_dims=(2, 3), n_points=300)
f0.plot_3d(selected_dims=(2, 3), n_points=300)

## Visualize opfunu function using utility function
from opfunu import draw_2d, draw_3d

draw_2d(f0.evaluate, f0.lb, f0.ub, selected_dims=(2, 3), n_points=300)
draw_3d(f0.evaluate, f0.lb, f0.ub, selected_dims=(2, 3), n_points=300)

0 comments on commit 37110ae

Please sign in to comment.