├── .gitignore ├── OptimizationTestFunctions ├── __init__.py ├── functions.py ├── plot_func.py └── transformations.py ├── README.md ├── _HarrixTestFunctions.pdf ├── how to push on PyPI.txt ├── requirements.txt ├── setup.py └── tests ├── Fletcher1.png ├── Fletcher2.png ├── Fletcher3.png ├── Fletcher4.png ├── Fletcher5.png ├── Fletcher6.png ├── Trans1.png ├── Trans2.png ├── Trans3.png ├── Trans4.png ├── Trans5.png ├── Trans6.png ├── heatmap for Abs.png ├── heatmap for Ackley.png ├── heatmap for AckleyTest.png ├── heatmap for Eggholder.png ├── heatmap for Fletcher.png ├── heatmap for Griewank.png ├── heatmap for Michalewicz.png ├── heatmap for Penalty2.png ├── heatmap for Quartic.png ├── heatmap for Rastrigin.png ├── heatmap for Rosenbrock.png ├── heatmap for Scheffer.png ├── heatmap for SchwefelAbs.png ├── heatmap for SchwefelDouble.png ├── heatmap for SchwefelMax.png ├── heatmap for SchwefelSin.png ├── heatmap for Sphere.png ├── heatmap for Stairs.png ├── heatmap for Weierstrass.png ├── heatmap_test.py ├── plotting_examples.py └── transformation_examples.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | 131 | 132 | 133 | 134 | 135 | .vscode/ 136 | build/ 137 | dist/ -------------------------------------------------------------------------------- /OptimizationTestFunctions/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | from .functions import Sphere, Ackley, AckleyTest, Rosenbrock, Fletcher, Griewank, Penalty2, Quartic, Rastrigin, SchwefelDouble, SchwefelMax, SchwefelAbs, SchwefelSin, Stairs, Abs, Michalewicz, Scheffer, Eggholder, Weierstrass 5 | 6 | from .transformations import Transformation, Noises 7 | 8 | from .plot_func import plot_3d 9 | 10 | 11 | -------------------------------------------------------------------------------- /OptimizationTestFunctions/functions.py: -------------------------------------------------------------------------------- 1 | # 2 | # 3 | # See also https://www.sfu.ca/~ssurjano/optimization.html 4 | # 5 | # 6 | 7 | 8 | 9 | import math 10 | import numpy as np 11 | 12 | 13 | def easy_bounds(bound): 14 | return (-bound, bound, -bound, bound) 15 | 16 | def check_dim(dim, min = 1): 17 | assert (type(dim) == int and dim >=min), f"Dimension should be int and not less than {min} for this function (got {dim})" 18 | 19 | 20 | class Sphere: 21 | 22 | b = 5.12 23 | 24 | def __init__(self, dim, degree = 2): 25 | 26 | check_dim(dim, 1) 27 | 28 | self.deg = degree 29 | self.x_best = np.zeros(dim) if degree % 2 == 0 else None 30 | self.f_best = 0 if not (self.x_best is None) else None 31 | 32 | self.bounds = easy_bounds(Sphere.b) 33 | 34 | def __call__(self, vec): 35 | return sum((x**self.deg for x in vec)) 36 | 37 | 38 | class Ackley: 39 | 40 | b = 3 41 | 42 | def __init__(self, dim): 43 | 44 | check_dim(dim, 1) 45 | 46 | self.x_best = np.zeros(dim) 47 | self.f_best = 0 48 | 49 | self.bounds = easy_bounds(Ackley.b) 50 | 51 | self.bias = 20 + math.e 52 | self.pi2 = 2 * math.pi 53 | 54 | def __call__(self, vec): 55 | 56 | s1 = sum((x*x for x in vec))/vec.size 57 | s2 = sum((math.cos(self.pi2 * x) for x in vec))/vec.size 58 | return self.bias - 20*math.exp(-0.2*math.sqrt(s1)) - math.exp(s2) 59 | 60 | class AckleyTest: 61 | 62 | b = 30 63 | 64 | def __init__(self, dim): 65 | 66 | check_dim(dim, 2) 67 | 68 | self.x_best = None 69 | self.f_best = None 70 | 71 | self.bounds = easy_bounds(AckleyTest.b) 72 | 73 | self.exp = math.exp(-0.2) 74 | 75 | def __call__(self, vec): 76 | 77 | s = sum((3*(math.cos(2*vec[i]) + math.sin(2*vec[i+1])) + self.exp * math.sqrt(vec[i]**2 + vec[i+1]**2) for i in range(vec.size-1))) 78 | 79 | return s 80 | 81 | 82 | class Rosenbrock: 83 | 84 | b = 2.048 85 | 86 | def __init__(self, dim): 87 | 88 | check_dim(dim, 2) 89 | 90 | self.x_best = np.ones(dim) 91 | self.f_best = 0 92 | 93 | self.bounds = easy_bounds(Rosenbrock.b) 94 | 95 | 96 | def __call__(self, vec): 97 | 98 | s = sum(( 100 * (vec[i+1] - vec[i]**2) ** 2 + (vec[i] - 1)**2 for i in range(vec.size-1))) 99 | 100 | return s 101 | 102 | class Fletcher: 103 | 104 | b = math.pi 105 | 106 | def __init__(self, dim, seed = None): 107 | 108 | if seed != None: 109 | np.random.seed(seed) 110 | 111 | check_dim(dim, 1) 112 | 113 | self.x_best = np.random.uniform(-np.pi, np.pi, dim) 114 | self.f_best = 0 115 | self.bounds = easy_bounds(Fletcher.b) 116 | 117 | self.a = np.random.uniform(-100, 100, (dim, dim)) 118 | self.b = np.random.uniform(-100, 100, (dim, dim)) 119 | 120 | self.A = np.sum(self.a * np.sin(self.x_best) + self.b * np.cos(self.x_best), axis = 0) 121 | 122 | 123 | 124 | 125 | 126 | def __call__(self, vec): 127 | 128 | B = np.sum(self.a * np.sin(vec) + self.b * np.cos(vec) ,axis = 0) 129 | #raise Exception() 130 | return sum((a-b)**2 for a, b in zip(self.A, B)) 131 | 132 | 133 | class Griewank: 134 | 135 | b = 600 136 | 137 | def __init__(self, dim): 138 | 139 | check_dim(dim, 1) 140 | 141 | self.x_best = np.zeros(dim) 142 | self.f_best = 0 143 | 144 | self.bounds = easy_bounds(Griewank.b) 145 | 146 | 147 | def __call__(self, vec): 148 | 149 | s = sum(( x*x for x in vec))/4000 150 | p = math.prod((math.cos(x/math.sqrt(i+1)) for i, x in enumerate(vec))) 151 | 152 | return 1 + s - p 153 | 154 | 155 | 156 | class Penalty2: 157 | 158 | b = 50 159 | 160 | def __init__(self, dim, a=5, k=100, m=4): 161 | 162 | check_dim(dim, 2) 163 | 164 | self.x_best = np.ones(dim) 165 | self.f_best = 0 166 | 167 | self.bounds = easy_bounds(Penalty2.b) 168 | 169 | self.a, self.k, self.m = a, k, m 170 | self.pi2 = 2 * math.pi 171 | self.pi3 = 3 * math.pi 172 | 173 | 174 | def __call__(self, vec): 175 | 176 | a, k, m = self.a, self.k, self.m 177 | 178 | u = np.sum((vec[vec>a]-a)**m) + np.sum((-vec[vec<-a]-a)**m) 179 | 180 | s1 = 10 * math.sin(self.pi3*vec[0])**2 + (vec[-1]-1)**2 * (1 + math.sin(self.pi2 * vec[-1]**2)) 181 | 182 | s2 = sum(((vec[i]-1)**2 * (1 + math.sin(self.pi3 * vec[i+1]**2)) for i in range(vec.size-1))) 183 | 184 | return k*u + 0.1 * (s1 + s2) 185 | 186 | 187 | class Quartic: 188 | 189 | b = 1.28 190 | 191 | def __init__(self, dim): 192 | 193 | check_dim(dim, 1) 194 | 195 | self.x_best = np.zeros(dim) 196 | self.f_best = 0 197 | 198 | self.bounds = easy_bounds(Quartic.b) 199 | 200 | 201 | def __call__(self, vec): 202 | 203 | s = sum(( (i+1)*x**4 for i, x in enumerate(vec))) 204 | 205 | return s 206 | 207 | 208 | 209 | class Rastrigin: 210 | 211 | b = 5.12 212 | 213 | def __init__(self, dim): 214 | 215 | check_dim(dim, 1) 216 | 217 | self.x_best = np.zeros(dim) 218 | self.f_best = 0 219 | 220 | self.bounds = easy_bounds(Rastrigin.b) 221 | 222 | 223 | self.pi2 = math.pi*2 224 | self.bias = 10*dim 225 | 226 | 227 | def __call__(self, vec): 228 | 229 | s = sum(( x*x - math.cos(self.pi2*x)*10 for x in vec)) 230 | 231 | return self.bias + s 232 | 233 | 234 | class SchwefelDouble: 235 | 236 | b = 65.536 237 | 238 | def __init__(self, dim): 239 | check_dim(dim) 240 | 241 | self.x_best = np.zeros(dim) 242 | self.f_best = 0 243 | 244 | self.bounds = easy_bounds(SchwefelDouble.b) 245 | 246 | 247 | 248 | def __call__(self, vec): 249 | 250 | cs = np.cumsum(vec) 251 | 252 | s = sum((cs[i]**2 for i in range(vec.size))) 253 | 254 | return s 255 | 256 | 257 | class SchwefelMax: 258 | 259 | b = 100 260 | 261 | def __init__(self, dim): 262 | check_dim(dim) 263 | 264 | self.x_best = np.zeros(dim) 265 | self.f_best = 0 266 | 267 | self.bounds = easy_bounds(SchwefelMax.b) 268 | 269 | def __call__(self, vec): 270 | 271 | return np.abs(vec).max() 272 | 273 | class SchwefelAbs: 274 | 275 | b = 10 276 | 277 | def __init__(self, dim): 278 | 279 | check_dim(dim) 280 | 281 | self.x_best = np.zeros(dim) 282 | self.f_best = 0 283 | 284 | self.bounds = easy_bounds(SchwefelAbs.b) 285 | 286 | def __call__(self, vec): 287 | 288 | mod = np.abs(vec) 289 | 290 | return np.sum(mod) + np.prod(mod) 291 | 292 | 293 | class SchwefelSin: 294 | 295 | b = 500 296 | 297 | def __init__(self, dim): 298 | 299 | check_dim(dim, 1) 300 | 301 | self.x_best = np.full(dim, 420.9687) 302 | self.f_best = -12965.5 303 | 304 | self.bounds = easy_bounds(SchwefelSin.b) 305 | 306 | def __call__(self, vec): 307 | 308 | return -sum((x*math.sin(math.sqrt(abs(x))) for x in vec)) 309 | 310 | 311 | class Stairs: 312 | 313 | b = 6 314 | 315 | def __init__(self, dim): 316 | 317 | check_dim(dim) 318 | 319 | self.x_best = np.zeros(dim) 320 | self.f_best = 0 321 | 322 | self.bounds = easy_bounds(Stairs.b) 323 | 324 | def __call__(self, vec): 325 | 326 | return sum(( math.floor(x + 0.5)**2 for x in vec)) 327 | 328 | 329 | class Abs: 330 | 331 | b = 10 332 | 333 | def __init__(self, dim): 334 | 335 | check_dim(dim) 336 | 337 | self.x_best = np.zeros(dim) 338 | self.f_best = 0 339 | 340 | self.bounds = easy_bounds(Abs.b) 341 | 342 | def __call__(self, vec): 343 | 344 | return sum(( abs(x) for x in vec)) 345 | 346 | 347 | class Michalewicz: 348 | 349 | 350 | def __init__(self, m = 10): 351 | 352 | self.x_best = None 353 | self.f_best = None 354 | 355 | self.bounds = (0, math.pi, 0, math.pi) 356 | 357 | self.m = m*2 358 | 359 | def __call__(self, vec): 360 | 361 | return -sum(( math.sin(x)*math.sin((i+1)*x**2/math.pi)**self.m for i, x in enumerate(vec))) 362 | 363 | 364 | class Scheffer: 365 | 366 | b = 7 367 | 368 | def __init__(self, dim): 369 | 370 | check_dim(dim, 2) 371 | 372 | self.x_best = np.zeros(dim) 373 | self.f_best = 0 374 | 375 | self.bounds = easy_bounds(Scheffer.b) 376 | 377 | 378 | def __call__(self, vec): 379 | 380 | return 0.5 + sum(( (math.sin( vec[i]**2 - vec[i+1]**2 )**2 - 0.5) / (1 + 0.001*(vec[i]**2 + vec[i+1]**2) )**2 for i in range(vec.size-1))) 381 | 382 | 383 | class Eggholder: 384 | 385 | b = 512 386 | 387 | def __init__(self, dim): 388 | 389 | check_dim(dim, 2) 390 | 391 | self.x_best = None 392 | self.f_best = None 393 | 394 | self.bounds = easy_bounds(Eggholder.b) 395 | 396 | 397 | def __call__(self, vec): 398 | 399 | return -sum(( (vec[i+1] + 47) * math.sin(math.sqrt(abs(vec[i+1] + vec[i]/2 + 47))) + vec[i] * math.sin(math.sqrt(abs(vec[i]-vec[i+1] - 47))) for i in range(vec.size-1))) 400 | 401 | 402 | class Weierstrass: 403 | 404 | b = 0.5 405 | 406 | def __init__(self, dim, a = 0.5, b = 3, kmax = 20): 407 | 408 | check_dim(dim, 1) 409 | 410 | self.x_best = np.zeros(dim) 411 | self.f_best = 0 412 | 413 | self.bounds = easy_bounds(Weierstrass.b) 414 | 415 | self.ak = np.array([a**k for k in range(kmax+1)]) 416 | self.pibk = np.pi * np.array([b**k for k in range(kmax+1)]) 417 | 418 | self.bias = -dim*np.sum(self.ak*np.cos(self.pibk)) 419 | 420 | 421 | def __call__(self, vec): 422 | 423 | return self.bias + sum(( sum((ak * math.cos(x*pibk) for ak, pibk in zip(self.ak, self.pibk) )) for x in vec*2 + 1 )) 424 | 425 | 426 | 427 | 428 | if __name__ == '__main__': 429 | 430 | arr = np.array([0.1, 0.2, 0.3]) 431 | 432 | dim = arr.size 433 | 434 | funcs = [ 435 | Sphere(dim, degree = 2), 436 | Ackley(dim), 437 | AckleyTest(dim), 438 | Rosenbrock(dim), 439 | Fletcher(dim, seed = None), 440 | Griewank(dim), 441 | Penalty2(dim, a=5, k=100, m=4), 442 | Quartic(dim), 443 | Rastrigin(dim), 444 | SchwefelDouble(dim), 445 | SchwefelMax(dim), 446 | SchwefelAbs(dim), 447 | SchwefelSin(dim), 448 | Stairs(dim), 449 | Abs(dim), 450 | Michalewicz(m = 10), 451 | Scheffer(dim), 452 | Eggholder(dim), 453 | Weierstrass(dim, a = 0.5, b = 3, kmax = 20) 454 | ] 455 | 456 | 457 | 458 | for f in funcs: 459 | print(f(arr)) 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | -------------------------------------------------------------------------------- /OptimizationTestFunctions/plot_func.py: -------------------------------------------------------------------------------- 1 | 2 | from mpl_toolkits.mplot3d import Axes3D 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | from matplotlib.ticker import MaxNLocator, LinearLocator, FormatStrFormatter 6 | 7 | from matplotlib import cm 8 | 9 | from OppOpPopInit import OppositionOperators 10 | 11 | 12 | def get_good_arrow_place(optimum, bounds): 13 | opt = np.array(optimum) 14 | minimums = np.array([bounds[0], bounds[2]]) 15 | maximums = np.array([bounds[1], bounds[3]]) 16 | 17 | t1 = OppositionOperators.Continual.over(minimums, maximums)(opt) 18 | t2 = OppositionOperators.Continual.quasi_reflect(minimums, maximums)(t1) 19 | 20 | return tuple(t2) 21 | 22 | 23 | 24 | def plot_3d(func, points_by_dim = 50, title = '', bounds = None, show_best_if_exists = True, save_as = None, cmap = 'twilight', plot_surface = True, plot_heatmap = True): 25 | """ 26 | Plots function surface and/or heatmap 27 | 28 | Parameters 29 | ---------- 30 | func : class callable object 31 | Object which can be called as function. 32 | points_by_dim : int, optional 33 | points for each dimension of plotting (50x50, 100x100...). The default is 50. 34 | title : str, optional 35 | title of plot with LaTeX notation. The default is ''. 36 | bounds : tuple, optional 37 | space bounds with structure (xmin, xmax, ymin, ymax). The default is None. 38 | show_best_if_exists : boolean, optional 39 | point best solution by arrow if x_best exists. The default is True. 40 | save_as : str/None, optional 41 | file path to save image (None if not needed). The default is None. 42 | cmap : str, optional 43 | color map of plot. The default is 'twilight'. 44 | plot_surface : boolean, optional 45 | plot 3D surface. The default is True. 46 | plot_heatmap : boolean, optional 47 | plot 2D heatmap. The default is True. 48 | """ 49 | 50 | assert (plot_surface or plot_heatmap), "should be plotted at least surface or heatmap!" 51 | 52 | if bounds is None: 53 | bounds = func.bounds 54 | 55 | xmin, xmax, ymin, ymax = bounds 56 | 57 | x = np.linspace(xmin, xmax, points_by_dim) 58 | y = np.linspace(ymin, ymax, points_by_dim) 59 | 60 | 61 | a, b = np.meshgrid(x, y) 62 | 63 | data = np.empty((points_by_dim, points_by_dim)) 64 | for i in range(points_by_dim): 65 | for j in range(points_by_dim): 66 | data[i,j] = func(np.array([x[i], y[j]])) 67 | 68 | a = a.T 69 | b = b.T 70 | 71 | l_a, r_a, l_b, r_b = xmin, xmax, ymin, ymax 72 | 73 | l_c, r_c = data.min(), data.max() 74 | 75 | levels = MaxNLocator(nbins=15).tick_values(l_c,r_c) 76 | 77 | if plot_heatmap and plot_surface: 78 | 79 | fig = plt.figure(figsize=(16, 6)) 80 | ax1 = fig.add_subplot(1,2,1) 81 | ax2 = fig.add_subplot(1,2,2, projection='3d') 82 | else: 83 | fig = plt.figure() 84 | if plot_heatmap: 85 | ax1 = fig.gca() 86 | else: 87 | ax2 = fig.gca(projection='3d') 88 | 89 | title = r"$\bf{" + title+ r"}$" 90 | min_title = title[::] 91 | 92 | def base_plot(): 93 | c = ax1.contourf(a, b, data , cmap=cmap, levels = levels, vmin=l_c, vmax=r_c) 94 | name = title 95 | ax1.set_title( name, fontsize = 15) 96 | ax1.axis([l_a, r_a, l_b, r_b]) 97 | fig.colorbar(c) 98 | 99 | if plot_surface: 100 | # Plot the surface. 101 | surf = ax2.plot_surface(a, b, data, cmap = cmap, linewidth=0, antialiased=False) 102 | 103 | # Customize the z axis. 104 | ax2.set_xlabel('first dim', fontsize=10) 105 | ax2.set_ylabel('second dim', fontsize=10) 106 | ax2.set_zlim(l_c, r_c) 107 | 108 | ax2.zaxis.set_major_locator(LinearLocator(4)) 109 | #ax2.zaxis.set_major_formatter(FormatStrFormatter('%.2f')) 110 | 111 | # Add a color bar which maps values to colors. 112 | if not plot_heatmap: fig.colorbar(surf)#, shrink=0.5, aspect=5) 113 | 114 | ax2.contour(a, b, data, zdir='z', offset=0, cmap = cmap) 115 | ax2.view_init(60, 35) 116 | ax2.set_title( min_title , fontsize = 15, loc = 'right') 117 | 118 | 119 | if not (func.x_best) is None: 120 | title += f"\n best solution: f{func.x_best} = {round(func(func.x_best))}" 121 | 122 | if show_best_if_exists and plot_heatmap: 123 | #xytext = tuple(np.array([(xmax+xmin)/2, (ymax+ymin)/2]) + np.random.uniform(-func.x_best/2, func.x_best/2, 2)) #tuple(np.random.uniform(0, min((xmax+xmin)/2, (ymax+ymin)/2), 2)) 124 | 125 | xytext = get_good_arrow_place(func.x_best, bounds) 126 | 127 | bbox = dict(boxstyle ="round", fc ="0.8") 128 | arrowprops = dict( 129 | arrowstyle = "->", 130 | connectionstyle = "angle, angleA = 0, angleB = 90, rad = 10") 131 | 132 | ax1.annotate(f'global minimum', xy= tuple(func.x_best), xytext = xytext, 133 | arrowprops=dict(facecolor='red', shrink=0.05), 134 | #color = 'red', 135 | bbox = bbox#, arrowprops = arrowprops 136 | ) 137 | 138 | if plot_heatmap: base_plot() 139 | 140 | 141 | fig.tight_layout() 142 | 143 | if save_as != None: 144 | plt.savefig(save_as, dpi = 250) 145 | 146 | plt.show() 147 | 148 | plt.close() 149 | 150 | 151 | 152 | 153 | 154 | -------------------------------------------------------------------------------- /OptimizationTestFunctions/transformations.py: -------------------------------------------------------------------------------- 1 | import warnings 2 | import numpy as np 3 | 4 | class Noises: 5 | @staticmethod 6 | def uniform(low = 0, high = 0.1): 7 | return lambda value: value + np.random.uniform(low = low, high = high) 8 | 9 | @staticmethod 10 | def normal(center = 0, sd = 0.1): 11 | return lambda value: value + np.random.normal(loc = center, scale = sd) 12 | 13 | 14 | 15 | class Transformation: 16 | 17 | def __init__(self, transformed_function, shift_step = None, rotation_matrix = None, noise_generator = None, seed = None): 18 | """ 19 | Creates Transformation object 20 | 21 | Parameters 22 | ---------- 23 | transformed_function : function or class callable object 24 | transformed function. 25 | shift_step : numpy 1D array/None, optional 26 | array of shifts by each dimension or None. The default is None. 27 | rotation_matrix : 2D-array/int/None, optional 28 | 2D ortogonal rotation matrix or dimension for creating random rotation matrix or None if no rotate. The default is None. 29 | noise_generator : function, optional 30 | function gets current value and returns value with some noise. The default is None. 31 | seed : int, optional 32 | random seed for rotation matrix if needed reproduce. The default is None. 33 | 34 | """ 35 | if not (seed is None): 36 | np.random.seed(seed) 37 | 38 | self.is_noised = not (noise_generator is None) 39 | self.is_rotated = not (rotation_matrix is None) 40 | self.is_shifted = not (shift_step is None) 41 | 42 | self.bounds = transformed_function.bounds 43 | if self.is_shifted: 44 | xmin, xmax, ymin, ymax = self.bounds 45 | self.bounds = xmin + shift_step[0], xmax + shift_step[0], ymin + shift_step[1], ymax + shift_step[1] 46 | #raise Exception() 47 | if (shift_step is None) and (rotation_matrix is None) and (noise_generator) is None: 48 | warnings.warn("No sense of transformation when all preparations are None!") 49 | self.f = lambda arr: transformed_function(arr) 50 | 51 | return 52 | 53 | empty_func = lambda arr: arr 54 | 55 | if self.is_shifted: 56 | 57 | assert (type(shift_step) == np.ndarray), "shift_step must be numpy array or None!" 58 | 59 | self.shifter = lambda arr: arr - shift_step 60 | self.unshifter = lambda arr: arr + shift_step 61 | else: 62 | self.shifter = empty_func 63 | self.unshifter = empty_func 64 | 65 | 66 | if self.is_rotated: 67 | if type(rotation_matrix) == np.ndarray: 68 | 69 | assert (np.allclose(rotation_matrix.T, np.linalg.inv(rotation_matrix))), f"Rotation matrix must be ortogonal!" 70 | 71 | # create rotator 72 | self.rotator = lambda arr: arr.dot(rotation_matrix) 73 | else: 74 | assert (type(rotation_matrix) == int), "rotation_matrix is not int dim and not a matrix!" 75 | # init rotator 76 | rotation_matrix, _ = np.linalg.qr(np.random.random((rotation_matrix, rotation_matrix)), mode='complete') 77 | self.rotator = lambda arr: arr.dot(rotation_matrix) 78 | self.unrotator = lambda arr: arr.dot(rotation_matrix.T) 79 | else: 80 | self.rotator = empty_func 81 | self.unrotator = empty_func 82 | self.is_rotated = False 83 | 84 | 85 | self.noiser = (lambda val: val) if noise_generator is None else noise_generator 86 | 87 | self.f = lambda arr: self.noiser(transformed_function(self.rotator(self.shifter(arr)))) 88 | 89 | self.x_best = None 90 | self.f_best = None 91 | 92 | if not self.is_noised and hasattr(transformed_function, 'x_best'): 93 | if not (transformed_function.x_best is None): 94 | self.x_best = self.unrotator(self.unshifter(transformed_function.x_best)) 95 | self.f_best = transformed_function(self.x_best) 96 | 97 | 98 | def __call__(self, arr): 99 | return self.f(arr) 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Optimization Test Functions 2 | 3 | [![PyPI 4 | version](https://badge.fury.io/py/OptimizationTestFunctions.svg)](https://pypi.org/project/OptimizationTestFunctions/) 5 | [![Downloads](https://pepy.tech/badge/optimizationtestfunctions)](https://pepy.tech/project/optimizationtestfunctions) 6 | [![Downloads](https://pepy.tech/badge/optimizationtestfunctions/month)](https://pepy.tech/project/optimizationtestfunctions) 7 | [![Downloads](https://pepy.tech/badge/optimizationtestfunctions/week)](https://pepy.tech/project/optimizationtestfunctions) 8 | 9 | The package from [DPEA](https://github.com/PasaOpasen/PasaOpasen.github.io/blob/master/EA_packages.md). Collection of optimization test functions and some useful methods for working with them 10 | 11 | ``` 12 | pip install OptimizationTestFunctions 13 | ``` 14 | 15 | - [Optimization Test Functions](#optimization-test-functions) 16 | - [Test function object](#test-function-object) 17 | - [Available test functions](#available-test-functions) 18 | - [Sphere](#sphere) 19 | - [Ackley](#ackley) 20 | - [AckleyTest](#ackleytest) 21 | - [Rosenbrock](#rosenbrock) 22 | - [Fletcher](#fletcher) 23 | - [Griewank](#griewank) 24 | - [Penalty2](#penalty2) 25 | - [Quartic](#quartic) 26 | - [Rastrigin](#rastrigin) 27 | - [SchwefelDouble](#schwefeldouble) 28 | - [SchwefelMax](#schwefelmax) 29 | - [SchwefelAbs](#schwefelabs) 30 | - [SchwefelSin](#schwefelsin) 31 | - [Stairs](#stairs) 32 | - [Abs](#abs) 33 | - [Michalewicz](#michalewicz) 34 | - [Scheffer](#scheffer) 35 | - [Eggholder](#eggholder) 36 | - [Weierstrass](#weierstrass) 37 | - [Plotting tools](#plotting-tools) 38 | - [Structure](#structure) 39 | - [How to use](#how-to-use) 40 | - [Transformation tools](#transformation-tools) 41 | - [Structure](#structure-1) 42 | - [How to use](#how-to-use-1) 43 | 44 | ## Test function object 45 | 46 | Each test function is the **callable object** of some class with next fields at least: 47 | 48 | * `bounds` -- tuple with structure `(xmin, xmax, ymin, ymax)`. It is recommended borders for 3D plotting and 2D optimization for this function 49 | * `x_best` -- global minimum argument of function in `bounds` area as numpy array. If unknown, it's `None` 50 | * `f_best` -- function value at `x_best` if `x_best` exists and `None` otherwise 51 | 52 | A lot of function objects need determined `dim` argument in constructor. 53 | 54 | U can call these "functions" like usual functions with structure `numpy 1D-array -> float value`. 55 | 56 | ## Available test functions 57 | 58 | Checklist: 59 | 60 | * `Sphere(dim, degree = 2)` 61 | * `Ackley(dim)` 62 | * `AckleyTest(dim)` 63 | * `Rosenbrock(dim)` 64 | * `Fletcher(dim, seed = None)` 65 | * `Griewank(dim)` 66 | * `Penalty2(dim, a=5, k=100, m=4)` 67 | * `Quartic(dim)` 68 | * `Rastrigin(dim)` 69 | * `SchwefelDouble(dim)` 70 | * `SchwefelMax(dim)` 71 | * `SchwefelAbs(dim)` 72 | * `SchwefelSin(dim)` 73 | * `Stairs(dim)` 74 | * `Abs(dim)` 75 | * `Michalewicz(m = 10)` 76 | * `Scheffer(dim)` 77 | * `Eggholder(dim)` 78 | * `Weierstrass(dim, a = 0.5, b = 3, kmax = 20)` 79 | 80 | U imports them using code: 81 | ```python 82 | from OptimizationTestFunctions import Sphere, Ackley, AckleyTest, Rosenbrock, Fletcher, Griewank, Penalty2, Quartic, Rastrigin, SchwefelDouble, SchwefelMax, SchwefelAbs, SchwefelSin, Stairs, Abs, Michalewicz, Scheffer, Eggholder, Weierstrass 83 | ``` 84 | 85 | And plot them using [code](tests/heatmap_test.py) 86 | 87 | 88 | ### Sphere 89 | ![](tests/heatmap%20for%20Sphere.png) 90 | ### Ackley 91 | ![](tests/heatmap%20for%20Ackley.png) 92 | ### AckleyTest 93 | ![](tests/heatmap%20for%20AckleyTest.png) 94 | ### Rosenbrock 95 | ![](tests/heatmap%20for%20Rosenbrock.png) 96 | ### Fletcher 97 | ![](tests/heatmap%20for%20Fletcher.png) 98 | ### Griewank 99 | ![](tests/heatmap%20for%20Griewank.png) 100 | ### Penalty2 101 | ![](tests/heatmap%20for%20Penalty2.png) 102 | ### Quartic 103 | ![](tests/heatmap%20for%20Quartic.png) 104 | ### Rastrigin 105 | ![](tests/heatmap%20for%20Rastrigin.png) 106 | ### SchwefelDouble 107 | ![](tests/heatmap%20for%20SchwefelDouble.png) 108 | ### SchwefelMax 109 | ![](tests/heatmap%20for%20SchwefelMax.png) 110 | ### SchwefelAbs 111 | ![](tests/heatmap%20for%20SchwefelAbs.png) 112 | ### SchwefelSin 113 | ![](tests/heatmap%20for%20SchwefelSin.png) 114 | ### Stairs 115 | ![](tests/heatmap%20for%20Stairs.png) 116 | ### Abs 117 | ![](tests/heatmap%20for%20Abs.png) 118 | ### Michalewicz 119 | ![](tests/heatmap%20for%20Michalewicz.png) 120 | ### Scheffer 121 | ![](tests/heatmap%20for%20Scheffer.png) 122 | ### Eggholder 123 | ![](tests/heatmap%20for%20Eggholder.png) 124 | ### Weierstrass 125 | ![](tests/heatmap%20for%20Weierstrass.png) 126 | 127 | 128 | ## Plotting tools 129 | 130 | ### Structure 131 | 132 | There are `plot_3d` function for 3D-plotting: 133 | ```python 134 | plot_3d(func, points_by_dim = 50, title = '', bounds = None, show_best_if_exists = True, save_as = None, cmap = 'twilight', plot_surface = True, plot_heatmap = True) 135 | ``` 136 | with arguments: 137 | 138 | * `func` : **class callable object**; 139 | Object which can be called as function. 140 | * `points_by_dim` : **int**, optional; 141 | points for each dimension of plotting (50x50, 100x100...). The default is 50. 142 | * `title` : **str**, optional; 143 | title of plot with LaTeX notation. The default is ''. 144 | * `bounds` : **tuple**, optional; 145 | space bounds with structure `(xmin, xmax, ymin, ymax)`. The default is None. 146 | * `show_best_if_exists` : **boolean**, optional; 147 | point best solution by arrow if x_best exists. The default is True. 148 | * `save_as` : **str/None**, optional; 149 | file path to save image (None if not needed). The default is None. 150 | * `cmap` : **str**, optional; 151 | color map of plot. The default is `'twilight'`. See another cmaps examples [here](https://github.com/PasaOpasen/Wave-animation-from-svd#heatmaps) 152 | * `plot_surface` : **boolean**, optional; 153 | plot 3D surface. The default is True. 154 | * `plot_heatmap` : **boolean**, optional; 155 | plot 2D heatmap. The default is True. 156 | 157 | ### How to use 158 | 159 | ```python 160 | from OptimizationTestFunctions import Fletcher, plot_3d 161 | 162 | # dim should be 2 for plotting 3D 163 | dim = 2 164 | 165 | # Fletcher is good function depends on random seed! 166 | 167 | seed = 1 168 | f1 = Fletcher(dim, seed) 169 | 170 | # full available functional of plotting 171 | 172 | plot_3d(f1, 173 | points_by_dim = 70, 174 | title = fr"{type(f1).__name__}\ with\ seed = {seed}", # LaTeX formula notation 175 | bounds = None, 176 | show_best_if_exists = True, 177 | save_as = "Fletcher1.png", 178 | cmap = 'twilight', 179 | plot_surface = True, 180 | plot_heatmap = True) 181 | ``` 182 | ![](tests/Fletcher1.png) 183 | 184 | ```python 185 | # disable arrow 186 | 187 | plot_3d(f1, 188 | points_by_dim = 70, 189 | title = fr"{type(f1).__name__}\ with\ seed = {seed}", 190 | bounds = None, 191 | show_best_if_exists = False, 192 | save_as = "Fletcher2.png", 193 | cmap = 'twilight', 194 | plot_surface = True, 195 | plot_heatmap = True) 196 | ``` 197 | ![](tests/Fletcher2.png) 198 | 199 | ```python 200 | # select another bounds 201 | 202 | plot_3d(f1, 203 | points_by_dim = 70, 204 | title = fr"{type(f1).__name__}\ with\ seed = {seed}", 205 | bounds = (-2, 6, -8, 10), 206 | show_best_if_exists = False, 207 | save_as = "Fletcher3.png", 208 | cmap = 'twilight', 209 | plot_surface = True, 210 | plot_heatmap = True) 211 | ``` 212 | ![](tests/Fletcher3.png) 213 | 214 | ```python 215 | # Create another Fletcher function 216 | 217 | seed = 33 218 | 219 | f2 = Fletcher(dim, seed) 220 | 221 | # use another cmap 222 | 223 | plot_3d(f2, 224 | points_by_dim = 70, 225 | title = fr"{type(f1).__name__}\ with\ seed = {seed}", 226 | bounds = None, 227 | show_best_if_exists = False, 228 | save_as = "Fletcher4.png", 229 | cmap = 'inferno', 230 | plot_surface = True, 231 | plot_heatmap = True) 232 | ``` 233 | ![](tests/Fletcher4.png) 234 | 235 | ```python 236 | # plot only 3D 237 | 238 | plot_3d(f2, 239 | points_by_dim = 70, 240 | title = fr"{type(f1).__name__}\ with\ seed = {seed}", 241 | bounds = None, 242 | show_best_if_exists = False, 243 | save_as = "Fletcher5.png", 244 | cmap = 'inferno', 245 | plot_surface = True, 246 | plot_heatmap = False) 247 | ``` 248 | ![](tests/Fletcher5.png) 249 | 250 | ```python 251 | # plot only heatmap 252 | 253 | plot_3d(f2, 254 | points_by_dim = 70, 255 | title = fr"{type(f1).__name__}\ with\ seed = {seed}", 256 | bounds = None, 257 | show_best_if_exists = True, 258 | save_as = "Fletcher6.png", 259 | cmap = 'inferno', 260 | plot_surface = False, 261 | plot_heatmap = True) 262 | ``` 263 | ![](tests/Fletcher6.png) 264 | 265 | 266 | ## Transformation tools 267 | 268 | ### Structure 269 | 270 | `Transformation` object is the callable object like "functions" of this package. It performs next useful transformations: 271 | 272 | * parallel transfer (*shift*) 273 | * rotation 274 | * add noises 275 | 276 | U can create `Transformation` object using code: 277 | 278 | ```python 279 | transform = Transformation(transformed_function, shift_step = None, rotation_matrix = None, noise_generator = None, seed = None) 280 | ``` 281 | 282 | where: 283 | 284 | * `transformed_function` : **function or class callable object**; 285 | transformed function. 286 | * `shift_step` : **numpy 1D array/None**, optional; 287 | array of shifts by each dimension or `None`. The default is `None`. 288 | * `rotation_matrix` : **2D-array/int/None**, optional; 289 | 2D ortogonal rotation matrix or dimension for creating random rotation matrix or `None` if no rotate. The default is `None`. 290 | * `noise_generator` : **function**, optional; 291 | function gets current value and returns value with some noise. The default is `None`. 292 | * `seed` : **int**, optional; 293 | random seed for rotation matrix if needed reproduce. The default is `None`. 294 | 295 | U also can create noises by using `Noises` static class. 296 | 297 | ### How to use 298 | 299 | ```python 300 | import numpy as np 301 | 302 | from OptimizationTestFunctions import Weierstrass, plot_3d, Transformation, Noises 303 | 304 | # dim should be 2 for plotting 3D 305 | dim = 2 306 | 307 | # Let's create Weierstrass function 308 | 309 | f = Weierstrass(dim, a = 0.5, b = 5, kmax = 20) 310 | 311 | # show it 312 | 313 | plot_3d(f, 314 | points_by_dim = 70, 315 | title = f"{type(f).__name__}", 316 | bounds = None, 317 | show_best_if_exists = True, 318 | save_as = "Trans1.png", 319 | cmap = 'hot', 320 | plot_surface = True, 321 | plot_heatmap = True) 322 | ``` 323 | 324 | ![](tests/Trans1.png) 325 | 326 | ```python 327 | # transformation with shift 328 | 329 | shifted_func = Transformation(f, shift_step=np.array([3, 4])) 330 | 331 | # show it 332 | 333 | plot_3d(shifted_func, 334 | points_by_dim = 70, 335 | title = "shifted", 336 | bounds = None, 337 | show_best_if_exists = True, 338 | save_as = "Trans2.png", 339 | cmap = 'hot', 340 | plot_surface = True, 341 | plot_heatmap = True) 342 | ``` 343 | ![](tests/Trans2.png) 344 | 345 | ```python 346 | # transformation with rotation 347 | 348 | rotated_func = Transformation(f, rotation_matrix = dim, seed = 2) # random rotation matrix with dim 2 349 | 350 | # show it 351 | 352 | plot_3d(rotated_func, 353 | points_by_dim = 70, 354 | title = "rotated", 355 | bounds = None, 356 | show_best_if_exists = True, 357 | save_as = "Trans3.png", 358 | cmap = 'hot', 359 | plot_surface = True, 360 | plot_heatmap = True) 361 | ``` 362 | ![](tests/Trans3.png) 363 | 364 | 365 | ```python 366 | # transformation with noise 367 | 368 | noised_func = Transformation(f, noise_generator = Noises.normal(center = 0, sd = 0.5)) 369 | 370 | # show it 371 | 372 | plot_3d(noised_func, 373 | points_by_dim = 70, 374 | title = "noised", 375 | bounds = None, 376 | show_best_if_exists = True, 377 | save_as = "Trans4.png", 378 | cmap = 'hot', 379 | plot_surface = True, 380 | plot_heatmap = True) 381 | ``` 382 | ![](tests/Trans4.png) 383 | 384 | ```python 385 | # U can specify your noise behavior 386 | 387 | def add_noise(current_val): 388 | if current_val > 5: 389 | return 0 390 | 391 | return current_val + np.random.random()/10 392 | 393 | noised_func = Transformation(f, noise_generator = add_noise) 394 | 395 | plot_3d(noised_func, 396 | points_by_dim = 70, 397 | title = "noised", 398 | bounds = None, 399 | show_best_if_exists = True, 400 | save_as = "Trans5.png", 401 | cmap = 'hot', 402 | plot_surface = True, 403 | plot_heatmap = True) 404 | ``` 405 | ![](tests/Trans5.png) 406 | 407 | ```python 408 | # Also u can combine all these transformations 409 | 410 | new_func = Transformation(f, 411 | shift_step= np.array([10, -10]), 412 | rotation_matrix = 2, seed = 3, 413 | noise_generator = Noises.uniform(-0.1, 0.5) 414 | ) 415 | 416 | plot_3d(new_func, 417 | points_by_dim = 70, 418 | title = "mixed", 419 | bounds = None, 420 | show_best_if_exists = True, 421 | save_as = "Trans6.png", 422 | cmap = 'hot', 423 | plot_surface = True, 424 | plot_heatmap = True) 425 | ``` 426 | ![](tests/Trans6.png) 427 | -------------------------------------------------------------------------------- /_HarrixTestFunctions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/_HarrixTestFunctions.pdf -------------------------------------------------------------------------------- /how to push on PyPI.txt: -------------------------------------------------------------------------------- 1 | https://medium.com/@joel.barmettler/how-to-upload-your-python-package-to-pypi-65edc5fe9c56 2 | 3 | https://realpython.com/pypi-publish-python-package/ 4 | 5 | cd "C:\Users\qtckp\OneDrive\Рабочий стол\PersianG2P" 6 | 7 | python setup.py develop 8 | python setup.py sdist 9 | python setup.py bdist_wheel 10 | 11 | twine upload dist/* 12 | 13 | twine upload dist/* --skip-existing -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | numpy==1.18.4 2 | matplotlib==3.2.1 3 | OppOpPopInit==1.0.2 4 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | 2 | import setuptools 3 | 4 | with open("README.md", "r") as fh: 5 | long_description = fh.read() 6 | 7 | 8 | setuptools.setup( 9 | name="OptimizationTestFunctions", 10 | version="1.0.1", 11 | author="Demetry Pascal", 12 | author_email="qtckpuhdsa@gmail.com", 13 | maintainer = ['Demetry Pascal'], 14 | description="PyPI package containing collection of optimization test functions and some useful methods for working with them", 15 | long_description=long_description, 16 | long_description_content_type="text/markdown", 17 | url="https://github.com/PasaOpasen/OptimizationTestFunctions", 18 | keywords=['optimization', 'evolutionary algorithms', 'fast', 'easy', 'evolution', 'generator', 'test', 'test-functions', '3D', 'functions'], 19 | packages = setuptools.find_packages(), 20 | classifiers=[ 21 | "Programming Language :: Python :: 3", 22 | "License :: OSI Approved :: MIT License", 23 | "Operating System :: OS Independent", 24 | "Topic :: Software Development :: Libraries :: Python Modules", 25 | ], 26 | install_requires=['numpy', 'matplotlib', 'OppOpPopInit'] 27 | 28 | ) 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /tests/Fletcher1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/Fletcher1.png -------------------------------------------------------------------------------- /tests/Fletcher2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/Fletcher2.png -------------------------------------------------------------------------------- /tests/Fletcher3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/Fletcher3.png -------------------------------------------------------------------------------- /tests/Fletcher4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/Fletcher4.png -------------------------------------------------------------------------------- /tests/Fletcher5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/Fletcher5.png -------------------------------------------------------------------------------- /tests/Fletcher6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/Fletcher6.png -------------------------------------------------------------------------------- /tests/Trans1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/Trans1.png -------------------------------------------------------------------------------- /tests/Trans2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/Trans2.png -------------------------------------------------------------------------------- /tests/Trans3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/Trans3.png -------------------------------------------------------------------------------- /tests/Trans4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/Trans4.png -------------------------------------------------------------------------------- /tests/Trans5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/Trans5.png -------------------------------------------------------------------------------- /tests/Trans6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/Trans6.png -------------------------------------------------------------------------------- /tests/heatmap for Abs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for Abs.png -------------------------------------------------------------------------------- /tests/heatmap for Ackley.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for Ackley.png -------------------------------------------------------------------------------- /tests/heatmap for AckleyTest.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for AckleyTest.png -------------------------------------------------------------------------------- /tests/heatmap for Eggholder.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for Eggholder.png -------------------------------------------------------------------------------- /tests/heatmap for Fletcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for Fletcher.png -------------------------------------------------------------------------------- /tests/heatmap for Griewank.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for Griewank.png -------------------------------------------------------------------------------- /tests/heatmap for Michalewicz.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for Michalewicz.png -------------------------------------------------------------------------------- /tests/heatmap for Penalty2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for Penalty2.png -------------------------------------------------------------------------------- /tests/heatmap for Quartic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for Quartic.png -------------------------------------------------------------------------------- /tests/heatmap for Rastrigin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for Rastrigin.png -------------------------------------------------------------------------------- /tests/heatmap for Rosenbrock.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for Rosenbrock.png -------------------------------------------------------------------------------- /tests/heatmap for Scheffer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for Scheffer.png -------------------------------------------------------------------------------- /tests/heatmap for SchwefelAbs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for SchwefelAbs.png -------------------------------------------------------------------------------- /tests/heatmap for SchwefelDouble.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for SchwefelDouble.png -------------------------------------------------------------------------------- /tests/heatmap for SchwefelMax.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for SchwefelMax.png -------------------------------------------------------------------------------- /tests/heatmap for SchwefelSin.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for SchwefelSin.png -------------------------------------------------------------------------------- /tests/heatmap for Sphere.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for Sphere.png -------------------------------------------------------------------------------- /tests/heatmap for Stairs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for Stairs.png -------------------------------------------------------------------------------- /tests/heatmap for Weierstrass.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PasaOpasen/OptimizationTestFunctions/bb45dc1625eef60014f2c584e6fefb855c2dac26/tests/heatmap for Weierstrass.png -------------------------------------------------------------------------------- /tests/heatmap_test.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.append('..') 3 | 4 | 5 | from OptimizationTestFunctions import Sphere, Ackley, AckleyTest, Rosenbrock, Fletcher, Griewank, Penalty2, Quartic, Rastrigin, SchwefelDouble, SchwefelMax, SchwefelAbs, SchwefelSin, Stairs, Abs, Michalewicz, Scheffer, Eggholder, Weierstrass, plot_3d 6 | 7 | dim = 2 8 | 9 | funcs = [ 10 | Sphere(dim, degree = 2), 11 | Ackley(dim), 12 | AckleyTest(dim), 13 | Rosenbrock(dim), 14 | Fletcher(dim, seed = 1488), 15 | Griewank(dim), 16 | Penalty2(dim), 17 | Quartic(dim), 18 | Rastrigin(dim), 19 | SchwefelDouble(dim), 20 | SchwefelMax(dim), 21 | SchwefelAbs(dim), 22 | SchwefelSin(dim), 23 | Stairs(dim), 24 | Abs(dim), 25 | Michalewicz(), 26 | Scheffer(dim), 27 | Eggholder(dim), 28 | Weierstrass(dim) 29 | ] 30 | 31 | for f in funcs: 32 | plot_3d(f, 33 | points_by_dim= 70, 34 | title = type(f).__name__, 35 | bounds=None, 36 | show_best_if_exists= False, 37 | save_as = None,#f"heatmap for {type(f).__name__}.png", 38 | cmap = 'twilight', 39 | plot_surface = True, 40 | plot_heatmap = True) 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /tests/plotting_examples.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.append('..') 3 | 4 | 5 | from OptimizationTestFunctions import Fletcher, plot_3d 6 | 7 | # dim should be 2 for plotting 3D 8 | dim = 2 9 | 10 | # Fletcher is good function depends on random seed! 11 | 12 | seed = 1 13 | f1 = Fletcher(dim, seed) 14 | 15 | # full available functional of plotting 16 | 17 | plot_3d(f1, 18 | points_by_dim = 70, 19 | title = fr"{type(f1).__name__}\ with\ seed = {seed}", # LaTeX formula notation 20 | bounds = None, 21 | show_best_if_exists = True, 22 | save_as = "Fletcher1.png", 23 | cmap = 'twilight', 24 | plot_surface = True, 25 | plot_heatmap = True) 26 | 27 | 28 | # disable arrow 29 | 30 | plot_3d(f1, 31 | points_by_dim = 70, 32 | title = fr"{type(f1).__name__}\ with\ seed = {seed}", 33 | bounds = None, 34 | show_best_if_exists = False, 35 | save_as = "Fletcher2.png", 36 | cmap = 'twilight', 37 | plot_surface = True, 38 | plot_heatmap = True) 39 | 40 | 41 | # select another bounds 42 | 43 | plot_3d(f1, 44 | points_by_dim = 70, 45 | title = fr"{type(f1).__name__}\ with\ seed = {seed}", 46 | bounds = (-2, 6, -8, 10), 47 | show_best_if_exists = False, 48 | save_as = "Fletcher3.png", 49 | cmap = 'twilight', 50 | plot_surface = True, 51 | plot_heatmap = True) 52 | 53 | 54 | 55 | # Create another Fletcher function 56 | 57 | seed = 33 58 | 59 | f2 = Fletcher(dim, seed) 60 | 61 | # use another cmap 62 | 63 | plot_3d(f2, 64 | points_by_dim = 70, 65 | title = fr"{type(f1).__name__}\ with\ seed = {seed}", 66 | bounds = None, 67 | show_best_if_exists = False, 68 | save_as = "Fletcher4.png", 69 | cmap = 'inferno', 70 | plot_surface = True, 71 | plot_heatmap = True) 72 | 73 | 74 | # plot only 3D 75 | 76 | plot_3d(f2, 77 | points_by_dim = 70, 78 | title = fr"{type(f1).__name__}\ with\ seed = {seed}", 79 | bounds = None, 80 | show_best_if_exists = False, 81 | save_as = "Fletcher5.png", 82 | cmap = 'inferno', 83 | plot_surface = True, 84 | plot_heatmap = False) 85 | 86 | 87 | # plot only heatmap 88 | 89 | plot_3d(f2, 90 | points_by_dim = 70, 91 | title = fr"{type(f1).__name__}\ with\ seed = {seed}", 92 | bounds = None, 93 | show_best_if_exists = True, 94 | save_as = "Fletcher6.png", 95 | cmap = 'inferno', 96 | plot_surface = False, 97 | plot_heatmap = True) 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | -------------------------------------------------------------------------------- /tests/transformation_examples.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu Dec 17 14:09:37 2020 4 | 5 | @author: qtckp 6 | """ 7 | 8 | import sys 9 | sys.path.append('..') 10 | 11 | import numpy as np 12 | 13 | from OptimizationTestFunctions import Weierstrass, plot_3d, Transformation, Noises 14 | 15 | # dim should be 2 for plotting 3D 16 | dim = 2 17 | 18 | # Let's create Weierstrass function 19 | 20 | f = Weierstrass(dim, a = 0.5, b = 5, kmax = 20) 21 | 22 | # show it 23 | 24 | plot_3d(f, 25 | points_by_dim = 70, 26 | title = f"{type(f).__name__}", 27 | bounds = None, 28 | show_best_if_exists = True, 29 | save_as = "Trans1.png", 30 | cmap = 'hot', 31 | plot_surface = True, 32 | plot_heatmap = True) 33 | 34 | 35 | # transformation with shift 36 | 37 | shifted_func = Transformation(f, shift_step=np.array([3, 4])) 38 | 39 | # show it 40 | 41 | plot_3d(shifted_func, 42 | points_by_dim = 70, 43 | title = "shifted", 44 | bounds = None, 45 | show_best_if_exists = True, 46 | save_as = "Trans2.png", 47 | cmap = 'hot', 48 | plot_surface = True, 49 | plot_heatmap = True) 50 | 51 | 52 | # transformation with rotation 53 | 54 | rotated_func = Transformation(f, rotation_matrix = dim, seed = 2) # random rotation matrix with dim 2 55 | 56 | # show it 57 | 58 | plot_3d(rotated_func, 59 | points_by_dim = 70, 60 | title = "rotated", 61 | bounds = None, 62 | show_best_if_exists = True, 63 | save_as = "Trans3.png", 64 | cmap = 'hot', 65 | plot_surface = True, 66 | plot_heatmap = True) 67 | 68 | 69 | 70 | 71 | 72 | # transformation with noise 73 | 74 | noised_func = Transformation(f, noise_generator = Noises.normal(center = 0, sd = 0.5)) 75 | 76 | # show it 77 | 78 | plot_3d(noised_func, 79 | points_by_dim = 70, 80 | title = "noised", 81 | bounds = None, 82 | show_best_if_exists = True, 83 | save_as = "Trans4.png", 84 | cmap = 'hot', 85 | plot_surface = True, 86 | plot_heatmap = True) 87 | 88 | 89 | # U can specify your noise behavior 90 | 91 | def add_noise(current_val): 92 | if current_val > 5: 93 | return 0 94 | 95 | return current_val + np.random.random()/10 96 | 97 | noised_func = Transformation(f, noise_generator = add_noise) 98 | 99 | plot_3d(noised_func, 100 | points_by_dim = 70, 101 | title = "noised", 102 | bounds = None, 103 | show_best_if_exists = True, 104 | save_as = "Trans5.png", 105 | cmap = 'hot', 106 | plot_surface = True, 107 | plot_heatmap = True) 108 | 109 | 110 | # Also u can combine all these transformations 111 | 112 | new_func = Transformation(f, 113 | shift_step= np.array([10, -10]), 114 | rotation_matrix = 2, seed = 3, 115 | noise_generator = Noises.uniform(-0.1, 0.5) 116 | ) 117 | 118 | plot_3d(new_func, 119 | points_by_dim = 70, 120 | title = "mixed", 121 | bounds = None, 122 | show_best_if_exists = True, 123 | save_as = "Trans6.png", 124 | cmap = 'hot', 125 | plot_surface = True, 126 | plot_heatmap = True) 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | --------------------------------------------------------------------------------