### Install pypop7 from GitHub Source: https://pypop.readthedocs.io/en/latest/_sources/installation.rst.txt Install the latest cutting-edge version of pypop7 directly from its GitHub repository for development purposes. This involves cloning the repository and performing an editable installation. ```bash git clone https://github.com/Evolutionary-Intelligence/pypop.git cd pypop pip install -e . ``` -------------------------------- ### Install PyPop7 using Pip Source: https://pypop.readthedocs.io/en/latest/index.html Use pip to automatically install pypop7 via PyPI. This is the recommended method for most users. ```bash $ pip install pypop7 ``` -------------------------------- ### Install PyPop7 using pip Source: https://pypop.readthedocs.io/en/latest/_sources/index.rst.txt Use pip to automatically install pypop7 via PyPI. Refer to the online documentation for alternative installation methods. ```bash $ pip install pypop7 ``` -------------------------------- ### DS Initialize Starting Point Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/optimizers/ds/ds.html Initializes or retrieves the starting point 'x' for the Direct Search optimizer. If restarting or if 'x' was not provided, it generates a random point within the initial boundaries. Otherwise, it uses the provided 'x'. ```python def _initialize_x(self, is_restart=False): if is_restart or (self.x is None): x = self.rng_initialization.uniform(self.initial_lower_boundary, self.initial_upper_boundary) else: x = np.copy(self.x) assert x.shape == (self.ndim_problem,) return x ``` -------------------------------- ### CLPSO Initialization and Optimization Example Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/optimizers/pso/clpso.html Demonstrates how to initialize and run the CLPSO optimizer to minimize the Rosenbrock function. Ensure problem and option dictionaries are correctly defined. ```python import numpy from pypop7.benchmarks.base_functions import rosenbrock # function to be minimized from pypop7.optimizers.pso.clpso import CLPSO problem = {'fitness_function': rosenbrock, # define problem arguments 'ndim_problem': 2, 'lower_boundary': -5*numpy.ones((2,)), 'upper_boundary': 5*numpy.ones((2,))} options = {'max_function_evaluations': 5000, # set optimizer options 'seed_rng': 2022} clpso = CLPSO(problem, options) # initialize the optimizer class results = clpso.optimize() # run the optimization process # return the number of function evaluations and best-so-far fitness print(f"CLPSO: {results['n_function_evaluations']}, {results['best_so_far_y']}") ``` -------------------------------- ### Benchmark Optimizers with COCO Platform Source: https://pypop.readthedocs.io/en/latest/tutorials/tutorials.html This snippet demonstrates benchmarking optimizers using the COCO platform with PyPop7. Ensure COCO is installed and follow the installation guide carefully. This code generates data for post-processing with cocopp. ```python """Demo for `COCO` benchmarking using only `PyPop7` here: https://github.com/numbbo/coco To install `COCO` successfully, please read the above open link carefully. """ import os import webbrowser # for post-processing in the browser import numpy as np import cocoex # experimentation module of `COCO` import cocopp # post-processing module of `COCO` from pypop7.optimizers.es.maes import MAES if __name__ == '__main__': suite, output = 'bbob', 'COCO-PyPop7-MAES' budget_multiplier = 1e3 # or 1e4, 1e5, ... observer = cocoex.Observer(suite, 'result_folder: ' + output) minimal_print = cocoex.utilities.MiniPrint() for function in cocoex.Suite(suite, '', ''): function.observe_with(observer) # to generate data for `cocopp` post-processing sigma = np.min(function.upper_bounds - function.lower_bounds) / 3.0 problem = {'fitness_function': function, 'ndim_problem': function.dimension, 'lower_boundary': function.lower_bounds, 'upper_boundary': function.upper_bounds} options = {'max_function_evaluations': function.dimension * budget_multiplier, 'seed_rng': 2022, 'x': function.initial_solution, 'sigma': sigma} solver = MAES(problem, options) print(solver.optimize()) cocopp.main(observer.result_folder) webbrowser.open('file://' + os.getcwd() + '/ppdata/index.html') ``` -------------------------------- ### Initialize and Optimize with Nelder-Mead Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/optimizers/ds/nm.html Demonstrates how to initialize the NM optimizer with problem and option settings, and then run the optimization process. It shows how to define the problem, set optimizer parameters like maximum function evaluations and initial guess, and print the optimization results. ```python import numpy from pypop7.benchmarks.base_functions import rosenbrock # function to be minimized from pypop7.optimizers.ds.nm import NM problem = {'fitness_function': rosenbrock, # define problem arguments 'ndim_problem': 2, 'lower_boundary': -5*numpy.ones((2,)), 'upper_boundary': 5*numpy.ones((2,))} options = {'max_function_evaluations': 5000, # set optimizer options 'seed_rng': 2022, 'x': 3*numpy.ones((2,)), 'sigma': 0.1, 'verbose': 500} nm = NM(problem, options) # initialize the optimizer class results = nm.optimize() # run the optimization process # return the number of function evaluations and best-so-far fitness print(f"NM: {results['n_function_evaluations']}, {results['best_so_far_y']}") ``` -------------------------------- ### Initialize and Optimize with SRS Source: https://pypop.readthedocs.io/en/latest/rs/srs.html Demonstrates how to initialize the SRS optimizer with problem and option configurations and run the optimization process. The output shows the number of function evaluations and the best fitness found. ```python 1>>> import numpy 2>>> from pypop7.benchmarks.base_functions import rosenbrock # function to be minimized 3>>> from pypop7.optimizers.rs.srs import SRS 4>>> problem = {'fitness_function': rosenbrock, # define problem arguments 5... 'ndim_problem': 2, 6... 'lower_boundary': -5*numpy.ones((2,)), 7... 'upper_boundary': 5*numpy.ones((2,))} 8>>> options = {'max_function_evaluations': 5000, # set optimizer options 9... 'seed_rng': 2022, 10... 'x': 3*numpy.ones((2,)), 11... 'sigma': 0.1} 12>>> srs = SRS(problem, options) # initialize the optimizer class 13>>> results = srs.optimize() # run the optimization process 14>>> # return the number of used function evaluations and found best-so-far fitness 15>>> print(f"SRS: {results['n_function_evaluations']}, {results['best_so_far_y']}") 16SRS: 5000, 0.0017821578376762473 ``` -------------------------------- ### Define a Rosenbrock objective function Source: https://pypop.readthedocs.io/en/latest/_sources/index.rst.txt Define a custom objective (cost or fitness) function to be minimized. This example uses the notorious Rosenbrock function and sets up the problem dictionary including the fitness function, dimension, and boundaries. This is a common setup for black-box optimization problems. ```python import numpy as np # for numerical computation (PyPop7's computing engine) def rosenbrock(x): # one notorious function in the optimization community return 100.0*np.sum(np.square(x[1:] - np.square(x[:-1]))) + np.sum(np.square(x[:-1] - 1.0)) ndim_problem = 1000 # problem dimension problem = {'fitness_function': rosenbrock, # fitness function to be minimized 'ndim_problem': ndim_problem, # problem dimension 'lower_boundary': -5.0*np.ones((ndim_problem,)), # lower search boundary 'upper_boundary': 5.0*np.ones((ndim_problem,))} ``` -------------------------------- ### Initialize and Optimize with MAES Source: https://pypop.readthedocs.io/en/latest/_sources/user-guide.rst.txt Demonstrates initializing the MAES optimizer with a defined problem and options, then running the optimization process. Ensure all necessary imports are present. ```python >>> problem = {'fitness_function': rosenbrock, # cost function to be minimized ... 'ndim_problem': ndim_problem, # dimension of cost function ... 'lower_boundary': -5.0*np.ones((ndim_problem,)), # lower search boundary ... 'upper_boundary': 5.0*np.ones((ndim_problem,))} # upper search boundary >>> from pypop7.optimizers.es.maes import MAES # replaced by any other BBO in this library >>> options = {'fitness_threshold': 1e-10, # to terminate when the best-so-far fitness is lower than 1e-10 ... 'max_function_evaluations': ndim_problem*10000, # maximum of function evaluations ... 'seed_rng': 0, # seed of random number generation (which should be set for repeatability) ... 'sigma': 3.0, # initial global step-size of Gaussian search distribution ... 'verbose': 500} # to print verbose information every 500 generations >>> maes = MAES(problem, options) # to initialize the black-box optimizer >>> results = maes.optimize(args=100.0) # args as input arguments of fitness function >>> print(results['best_so_far_y'], results['n_function_evaluations']) 7.57e-11 15537 ``` -------------------------------- ### Visualize Fitness Convergence with (1+1)-ES Source: https://pypop.readthedocs.io/en/latest/user-guide.html This example visualizes the fitness convergence of Rechenberg’s (1+1)-Evolution Strategy on the sphere function. It demonstrates how to set up the problem and optimizer options, run the optimization, and plot the fitness over function evaluations. Ensure seaborn and matplotlib are installed for plotting. ```python import numpy as np import seaborn as sns import matplotlib.pyplot as plt from pypop7.benchmarks.base_functions import sphere from pypop7.optimizers.es.res import RES sns.set_theme(style='darkgrid') plt.figure() for i in range(3): problem = {'fitness_function': sphere, 'ndim_problem': 10} options = {'max_function_evaluations': 1500, 'seed_rng': i, 'saving_fitness': 1, 'x': np.ones((10,)), 'sigma': 1e-9, 'lr_sigma': 1.0/(1.0 + 10.0/3.0), 'is_restart': False} res = RES(problem, options) fitness = res.optimize()['fitness'] plt.plot(fitness[:, 0], np.sqrt(fitness[:, 1]), 'b') # sqrt for distance plt.xticks([0, 500, 1000, 1500]) plt.xlim([0, 1500]) plt.yticks([1e-9, 1e-6, 1e-3, 1e0]) plt.yscale('log') plt.show() ``` -------------------------------- ### Initialize and Optimize with SCEM Source: https://pypop.readthedocs.io/en/latest/_sources/user-guide.rst.txt Shows how to initialize the SCEM optimizer with specific problem and hyper-parameter options, including custom mean and sigma values for the search distribution. Requires importing necessary components from pypop7. ```python >>> import numpy as np >>> from pypop7.benchmarks.base_functions import rosenbrock # function to be minimized >>> from pypop7.optimizers.cem.scem import SCEM >>> problem = {'fitness_function': rosenbrock, # define problem arguments ... 'ndim_problem': 10, ... 'lower_boundary': -5.0*np.ones((10,)), ... 'upper_boundary': 5.0*np.ones((10,))} >>> options = {'max_function_evaluations': 1000000, # set optimizer options ... 'seed_rng': 2022, ... 'mean': 4.0*np.ones((10,)), # initial mean of Gaussian search distribution ... 'sigma': 3.0} # initial std (aka global step-size) of Gaussian search distribution >>> scem = SCEM(problem, options) # initialize the optimizer class ``` -------------------------------- ### Initialize and Run NSA Optimizer Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/optimizers/sa/nsa.html Example demonstrating how to initialize and run the NSA optimizer to minimize the Rosenbrock function. Ensure all required problem and option parameters are correctly set. ```python import numpy # engine for numerical computing from pypop7.benchmarks.base_functions import rosenbrock # function to be minimized from pypop7.optimizers.sa.nsa import NSA problem = {'fitness_function': rosenbrock, # define problem arguments 'ndim_problem': 2, 'lower_boundary': -5*numpy.ones((2,)), 'upper_boundary': 5*numpy.ones((2,))} options = {'max_function_evaluations': 5000, # set optimizer options 'seed_rng': 2022, 'x': 3*numpy.ones((2,)), 'sigma': 1.0, 'temperature': 100.0} nsa = NSA(problem, options) # initialize the optimizer class results = nsa.optimize() # run the optimization process # return the number of function evaluations and best-so-far fitness print(f"NSA: {results['n_function_evaluations']}, {results['best_so_far_y']}") ``` -------------------------------- ### Install PyPop7 using Pip Source: https://pypop.readthedocs.io/en/latest/installation.html Use this command to install the PyPop7 package from the Python Package Index (PyPI). ```shell pip install pypop7 ``` -------------------------------- ### Initialize and Optimize with SGES Source: https://pypop.readthedocs.io/en/latest/nes/sges.html Demonstrates how to initialize the SGES optimizer with problem and option configurations and run the optimization process. Ensure 'fitness_function' is set to a function to be minimized. ```python 1>>> import numpy 2>>> from pypop7.benchmarks.base_functions import rosenbrock # function to be minimized 3>>> from pypop7.optimizers.nes.sges import SGES 4>>> problem = {'fitness_function': rosenbrock, # to define problem arguments 5... 'ndim_problem': 2, 6... 'lower_boundary': -5.0*numpy.ones((2,)), 7... 'upper_boundary': 5.0*numpy.ones((2,))} 8>>> options = {'max_function_evaluations': 5000, # to set optimizer options 9... 'seed_rng': 2022, 10... 'mean': 3.0*numpy.ones((2,))} 11>>> sges = SGES(problem, options) # to initialize the optimizer class 12>>> results = sges.optimize() # to run the optimization process 13>>> print(f"SGES: {results['n_function_evaluations']}, {results['best_so_far_y']}") 14SGES: 5000, 0.0190 ``` -------------------------------- ### IPSO Class Initialization and Optimization Source: https://pypop.readthedocs.io/en/latest/pso/ipso.html Demonstrates how to initialize the IPSO optimizer with problem and options, and then run the optimization process. ```APIDOC ## IPSO Class ### Description Initializes the Incremental Particle Swarm Optimizer (IPSO). ### Parameters #### problem (dict) - **fitness_function** (func) - objective function to be minimized - **ndim_problem** (int) - number of dimensionality - **upper_boundary** (array_like) - upper boundary of search range - **lower_boundary** (array_like) - lower boundary of search range #### options (dict) - **max_function_evaluations** (int) - maximum of function evaluations (default: np.inf) - **max_runtime** (float) - maximal runtime to be allowed (default: np.inf) - **seed_rng** (int) - seed for random number generation - **n_individuals** (int) - swarm (population) size (default: 20) - **constriction** (float) - constriction factor (default: 0.729) - **cognition** (float) - cognitive learning rate (default: 2.05) - **society** (float) - social learning rate (default: 2.05) - **max_ratio_v** (float) - maximal ratio of velocities w.r.t. search range (default: 0.5) ### Method IPSO(problem, options) ### Request Example ```python import numpy from pypop7.benchmarks.base_functions import rosenbrock from pypop7.optimizers.pso.ipso import IPSO problem = { 'fitness_function': rosenbrock, 'ndim_problem': 2, 'lower_boundary': -5*numpy.ones((2,)), 'upper_boundary': 5*numpy.ones((2,)) } options = { 'max_function_evaluations': 5000, 'seed_rng': 2022 } ipso = IPSO(problem, options) results = ipso.optimize() print(f"IPSO: {results['n_function_evaluations']}, {results['best_so_far_y']}") ``` ### Response #### Success Response (200) - **n_function_evaluations** (int) - number of function evaluations performed - **best_so_far_y** (float) - best fitness value found so far #### Response Example ```json { "n_function_evaluations": 5000, "best_so_far_y": 2.29225104244031e-07 } ``` ``` -------------------------------- ### Running Experiments with PyPop7 Source: https://pypop.readthedocs.io/en/latest/_sources/tutorials/tutorials.rst.txt This code demonstrates how to initialize and run experiments using a selected optimizer from PyPop7. It measures and prints the total runtime. Ensure 'start', 'end', and 'ndim_problem' are defined in the params dictionary. ```python experiments = Experiments(params['start'], params['end'], params['ndim_problem']) experiments.run(Optimizer) print('Total runtime: {:7.5e}.'.format(time.time() - start_runtime)) ``` -------------------------------- ### Install pypop7 using pip Source: https://pypop.readthedocs.io/en/latest/_sources/installation.rst.txt Use this command to install the latest stable version of pypop7 from the Python Package Index (PyPI). ```bash pip install pypop7 ``` -------------------------------- ### Create and Activate Conda Virtual Environment Source: https://pypop.readthedocs.io/en/latest/installation.html Steps to create a new conda virtual environment, activate it, install Python, and then install PyPop7. The Python version can be adjusted as needed. ```shell conda deactivate # close exiting virtual env, if exists conda create -y --prefix env_pypop7 # free to change name of virtual env conda activate ./env_pypop7 # on Windows OS conda activate env_pypop7/ # on Linux conda activate env_pypop7 # on MacOS conda install -y --prefix env_pypop7 python=3.8.12 # create new virtual env pip install pypop7 conda deactivate # close current virtual env `env_pypop7` ``` -------------------------------- ### Initialize and Run FEP Optimizer Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/optimizers/ep/fep.html Demonstrates how to initialize the FEP optimizer with problem and option parameters, and then run the optimization process. Ensure 'seed_rng' is explicitly set for reproducibility. ```python import numpy # engine for numerical computing from pypop7.benchmarks.base_functions import rosenbrock # function to be minimized from pypop7.optimizers.ep.fep import FEP problem = {'fitness_function': rosenbrock, # to define problem arguments 'ndim_problem': 2, 'lower_boundary': -5.0*numpy.ones((2,)), 'upper_boundary': 5.0*numpy.ones((2,))} options = {'max_function_evaluations': 5000, # to set optimizer options 'seed_rng': 2022, 'sigma': 3.0} # global step-size may need to be tuned fep = FEP(problem, options) # to initialize the optimizer class results = fep.optimize() # to run its optimization/evolution process # to return the number of function evaluations and the best-so-far fitness print(f"FEP: {results['n_function_evaluations']}, {results['best_so_far_y']}") ``` -------------------------------- ### Initialize BES Optimizer Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/optimizers/rs/bes.html Demonstrates how to initialize the BES optimizer with problem and option configurations, including setting parameters like learning rate and gradient estimation factor. ```python import numpy from pypop7.benchmarks.base_functions import rosenbrock # function to be minimized from pypop7.optimizers.rs.bes import BES problem = {'fitness_function': rosenbrock, # define problem arguments 'ndim_problem': 100, 'lower_boundary': -2*numpy.ones((100,)), 'upper_boundary': 2*numpy.ones((100,))} options = {'max_function_evaluations': 10000*101, # set optimizer options 'seed_rng': 2022, 'n_individuals': 10, 'c': 0.1, 'lr': 0.000001} bes = BES(problem, options) # initialize the optimizer class results = bes.optimize() # run the optimization process # return the number of used function evaluations and found best-so-far fitness print(f"BES: {results['n_function_evaluations']}, {results['best_so_far_y']}") ``` -------------------------------- ### MAES Optimizer Usage Example Source: https://pypop.readthedocs.io/en/latest/tutorials/tutorials.html Example demonstrating how to instantiate and run the extended MAESPLOT optimizer for a given problem and options. It visualizes the collected data using a plot function. ```python if __name__ == '__main__': ndim_problem = 15 problem = {'fitness_function': func_lens, 'ndim_problem': ndim_problem, 'lower_boundary': -5.0*np.ones((ndim_problem,)), 'upper_boundary': 5.0*np.ones((ndim_problem,))} options = {'max_function_evaluations': 7e3, 'seed_rng': 2022, 'x': d_init*np.ones((ndim_problem,)), 'sigma': 0.3, 'saving_fitness': 50, 'is_restart': False} results = MAESPLOT(problem, options).optimize() plot(results['xs']) ``` -------------------------------- ### ENES Optimizer Example Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/optimizers/nes/enes.html Example of using the ENES optimizer to minimize the Rosenbrock function. Demonstrates problem definition, option setting, optimizer initialization, and running the optimization process. ```python import numpy # engine for numerical computing from pypop7.benchmarks.base_functions import rosenbrock # function to be minimized from pypop7.optimizers.nes.enes import ENES problem = {'fitness_function': rosenbrock, # define problem arguments 'ndim_problem': 2, 'lower_boundary': -5*numpy.ones((2,)), 'upper_boundary': 5*numpy.ones((2,))} options = {'max_function_evaluations': 5000, # set optimizer options 'seed_rng': 2022, 'mean': 3*numpy.ones((2,))} ENES = ENES(problem, options) # initialize the optimizer class results = ENES.optimize() # run the optimization process # return the number of function evaluations and best-so-far fitness print(f"ENES: {results['n_function_evaluations']}, {results['best_so_far_y']}") ``` -------------------------------- ### Initialize and Run CSA Optimizer Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/optimizers/sa/csa.html Example of initializing the CSA optimizer with problem and option parameters, then running the optimization process. It demonstrates setting up the problem, defining options like maximum function evaluations and random seed, and printing the optimization results. ```python import numpy # engine for numerical computing from pypop7.benchmarks.base_functions import rosenbrock # function to be minimized from pypop7.optimizers.sa.csa import CSA problem = {'fitness_function': rosenbrock, # define problem arguments 'ndim_problem': 2, 'lower_boundary': -5*numpy.ones((2,)), 'upper_boundary': 5*numpy.ones((2,))} options = {'max_function_evaluations': 5000, # set optimizer options 'seed_rng': 2022, 'x': 3*numpy.ones((2,)), 'sigma': 1.0, 'temperature': 100} csa = CSA(problem, options) # initialize the optimizer class results = csa.optimize() # run the optimization process # return the number of function evaluations and best-so-far fitness print(f"CSA: {results['n_function_evaluations']}, {results['best_so_far_y']}") ``` -------------------------------- ### Initialize and Run EMNA Optimizer Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/optimizers/eda/emna.html Demonstrates how to initialize the EMNA optimizer with problem and option settings and then run the optimization process. It shows how to define the problem, set optimizer options, and print the results. ```python import numpy from pypop7.benchmarks.base_functions import rosenbrock # function to be minimized from pypop7.optimizers.eda.emna import EMNA problem = {'fitness_function': rosenbrock, # define problem arguments 'ndim_problem': 2, 'lower_boundary': -5*numpy.ones((2,)), 'upper_boundary': 5*numpy.ones((2,))} options = {'max_function_evaluations': 5000, # set optimizer options 'seed_rng': 2022} emna = EMNA(problem, options) # initialize the optimizer class results = emna.optimize() # run the optimization process # return the number of function evaluations and best-so-far fitness print(f"EMNA: {results['n_function_evaluations']}, {results['best_so_far_y']}") ``` -------------------------------- ### Initialize Simulated Annealing Optimizer Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/optimizers/sa/nsa.html Initializes the optimizer's state, including the starting point for the search. If no starting point is provided, a random one is generated within specified boundaries. ```python def initialize(self, args=None): if self.x is None: # starting point x = self.rng_initialization.uniform(self.initial_lower_boundary, self.initial_upper_boundary) else: x = np.copy(self.x) assert len(x) == self.ndim_problem y = self._evaluate_fitness(x, args) self.parent_x, self.parent_y = np.copy(x), np.copy(y) return y ``` -------------------------------- ### SNES Optimizer Initialization and Usage Source: https://pypop.readthedocs.io/en/latest/nes/snes.html Demonstrates how to initialize and use the SNES optimizer with a sample problem and options. ```APIDOC ## SNES Optimizer ### Description Initializes and runs the Separable Natural Evolution Strategies (SNES) optimizer. ### Class Signature `SNES(problem, options)` ### Parameters #### `problem` (dict) - **fitness_function** (func) - Objective function to be minimized. - **ndim_problem** (int) - Dimensionality of the problem. - **upper_boundary** (array_like) - Upper boundary of the search range. - **lower_boundary** (array_like) - Lower boundary of the search range. #### `options` (dict) - **max_function_evaluations** (int) - Maximum number of function evaluations (default: np.inf). - **max_runtime** (float) - Maximum runtime allowed in seconds (default: np.inf). - **seed_rng** (int) - Seed for random number generation. - **n_individuals** (int) - Number of offspring/descendants (offspring population size). - **n_parents** (int) - Number of parents/ancestors (parental population size). - **mean** (array_like) - Initial starting point. If not provided, a random sample is drawn. - **sigma** (float) - Initial global step-size (mutation strength). ### Example Usage ```python import numpy from pypop7.benchmarks.base_functions import rosenbrock from pypop7.optimizers.nes.snes import SNES problem = { 'fitness_function': rosenbrock, 'ndim_problem': 2, 'lower_boundary': -5*numpy.ones((2,)), 'upper_boundary': 5*numpy.ones((2,)) } options = { 'max_function_evaluations': 5000, 'seed_rng': 2022, 'mean': 3*numpy.ones((2,)), 'sigma': 0.1 } snes = SNES(problem, options) results = snes.optimize() print(f"SNES: {results['n_function_evaluations']}, {results['best_so_far_y']}") ``` ### Returns - **results** (dict) - Dictionary containing optimization results, including 'n_function_evaluations' and 'best_so_far_y'. ``` -------------------------------- ### CSA Optimizer Initialize Method Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/optimizers/sa/csa.html Sets the initial starting point for the optimization. If no starting point is provided, it generates a random one within the defined boundaries. Evaluates the fitness of the initial point. ```python def initialize(self, args=None): if self.x is None: # starting point x = self.rng_initialization.uniform(self.initial_lower_boundary, self.initial_upper_boundary) else: x = np.copy(self.x) assert len(x) == self.ndim_problem y = self._evaluate_fitness(x, args) self.parent_x, self.parent_y = np.copy(x), np.copy(y) return y ``` -------------------------------- ### Run Experiment and Print Runtime Source: https://pypop.readthedocs.io/en/latest/tutorials/tutorials.html This snippet shows how to initialize an experiment, run an optimizer, and print the execution runtime. It's useful for performance measurement during optimization tasks. ```python experiment = Experiment(index, f, self.seeds[i, index], self.ndim_problem) experiment.run(optimizer) print(' runtime: {:7.5e}.'.format(time.time() - start_time)) ``` -------------------------------- ### Example Usage of ENES Optimizer Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/optimizers/nes/enes.html Demonstrates how to use the ENES optimizer to minimize the Rosenbrock function. ```APIDOC ## Example: Minimizing Rosenbrock Function with ENES Use the optimizer `ENES` to minimize the well-known test function `Rosenbrock `_: ```python import numpy # engine for numerical computing from pypop7.benchmarks.base_functions import rosenbrock # function to be minimized from pypop7.optimizers.nes.enes import ENES # Define problem arguments problem = { 'fitness_function': rosenbrock, 'ndim_problem': 2, 'lower_boundary': -5*numpy.ones((2,)), 'upper_boundary': 5*numpy.ones((2,)) } # Set optimizer options options = { 'max_function_evaluations': 5000, 'seed_rng': 2022, 'mean': 3*numpy.ones((2,)) } # Initialize the optimizer class ENES_optimizer = ENES(problem, options) # Run the optimization process results = ENES_optimizer.optimize() # Print the number of function evaluations and best-so-far fitness print(f"ENES: {results['n_function_evaluations']}, {results['best_so_far_y']}") ``` ### Output Example ``` ENES: 5000, 0.00035668252927080496 ``` ``` -------------------------------- ### DSCEM Initialize Method Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/optimizers/cem/dscem.html Prepares the optimizer for a new run or restart. It calculates the initial mean and allocates space for samples (population) and their fitness values. ```python def initialize(self, is_restart=False): mean = self._initialize_mean(is_restart) x = np.empty((self.n_individuals, self.ndim_problem)) # samples (population) y = np.empty((self.n_individuals,)) # fitness (no evaluation) return mean, x, y ``` -------------------------------- ### Initialize Optimization State Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/optimizers/ds/cs.html Sets the initial point and evaluates its fitness. Used at the start of optimization or after a restart. ```python def initialize(self, args=None, is_restart=False): x = self._initialize_x(is_restart) # initial point y = self._evaluate_fitness(x, args) # fitness return x, y ``` -------------------------------- ### Initialize and Run SNES Optimizer Source: https://pypop.readthedocs.io/en/latest/nes/snes.html Example of initializing the SNES optimizer with problem and option configurations, then running the optimization process. Ensure 'seed_rng' is explicitly set for reproducible results. ```python 1>>> import numpy # engine for numerical computing 2>>> from pypop7.benchmarks.base_functions import rosenbrock # function to be minimized 3>>> from pypop7.optimizers.nes.snes import SNES 4>>> problem = {'fitness_function': rosenbrock, # define problem arguments 5... 'ndim_problem': 2, 6... 'lower_boundary': -5*numpy.ones((2,)), 7... 'upper_boundary': 5*numpy.ones((2,))} 8>>> options = {'max_function_evaluations': 5000, # set optimizer options 9... 'seed_rng': 2022, 10... 'mean': 3*numpy.ones((2,)), 11... 'sigma': 0.1} # the global step-size may need to be tuned for better performance 12>>> snes = SNES(problem, options) # initialize the optimizer class 13>>> results = snes.optimize() # run the optimization process 14>>> # return the number of function evaluations and best-so-far fitness 15>>> print(f"SNES: {results['n_function_evaluations']}, {results['best_so_far_y']}") 16SNES: 5000, 0.49730042657448875 ``` -------------------------------- ### Get Shubert Minimizers Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/benchmarks/cases.html Retrieves a list of minimizers for the Shaffer (Schaffer) test function. This data is used for validation. ```python def get_y_shubert(): """Get test data for **Schaffer** test function. """ minimizers = [[-7.0835, 4.858], [-7.0835, -7.7083], [-1.4251, -7.0835], [5.4828, 4.858], [-1.4251, -0.8003], [4.858, 5.4828], [-7.7083, -7.0835], [-7.0835, -1.4251], [-7.7083, -0.8003], [-7.7083, 5.4828], [-0.8003, -7.7083], [-0.8003, -1.4251], [-0.8003, 4.8580], [-1.4251, 5.4828], [5.4828, -7.7083], [4.858, -7.0835], [5.4828, -1.4251], [4.858, -0.8003]] return minimizers ``` -------------------------------- ### Run Optimization Experiments Source: https://pypop.readthedocs.io/en/latest/_sources/tutorials/tutorials.rst.txt This script demonstrates how to set up and run a series of optimization experiments using different algorithms from PyPop7. It includes argument parsing for experiment parameters and conditional imports for selected optimizers. ```python import pickle import time import argparse import numpy as np from pypop7.optimizers.rs.prs import PRS as Optimizer class Experiment(object): def __init__(self, index, fitness_function, seed, ndim_problem): self.index = index self.fitness_function = fitness_function self.seed = seed self.ndim_problem = ndim_problem self._file = 'results/{:s}/{:s}/{:d}D/{:d}.pickle' def run(self, optimizer): options = {'seed': self.seed, 'ndim_problem': self.ndim_problem, 'saving_fitness': 2000, 'verbose': 0} options['temperature'] = 100.0 # for simulated annealing (SA) solver = optimizer(problem, options) results = solver.optimize() file = self._file.format(solver.__class__.__name__, solver.fitness_function.__name__, solver.ndim_problem, self.index) with open(file, 'wb') as handle: # data format (pickle) pickle.dump(results, handle, protocol=pickle.HIGHEST_PROTOCOL) class Experiments(object): def __init__(self, start, end, ndim_problem): self.start, self.end = start, end self.ndim_problem = ndim_problem self.functions = [cf.sphere, cf.cigar, cf.discus, cf.cigar_discus, cf.ellipsoid, cf.different_powers, cf.schwefel221, cf.step, cf.rosenbrock, cf.schwefel12] # set RNG seeds for repeatability self.seeds = np.random.default_rng(2022).integers( np.iinfo(np.int64).max, size=(len(self.functions), 50)) def run(self, optimizer): for index in range(self.start, self.end + 1): print('* experiment: {:d} ***:'.format(index)) for i, f in enumerate(self.functions): start_time = time.time() print(' * function: {:s}:'.format(f.__name__)) experiment = Experiment(index, f, self.seeds[i, index], self.ndim_problem) experiment.run(optimizer) print(' runtime: {:7.5e}.'.format(time.time() - start_time)) if __name__ == '__main__': start_runtime = time.time() parser = argparse.ArgumentParser() # set starting index of experiments (from 0 to 49) parser.add_argument('--start', '-s', type=int) # set ending index of experiments (from 0 to 49) parser.add_argument('--end', '-e', type=int) # use any optimizer from PyPop7 parser.add_argument('--optimizer', '-o', type=str) # set dimension of all fitness functions parser.add_argument('--ndim_problem', '-d', type=int, default=2000) args = parser.parse_args() params = vars(args) # ensure seeds between 0 (include) and 49 (include) assert isinstance(params['start'], int) and 0 <= params['start'] < 50 assert isinstance(params['end'], int) and 0 <= params['end'] < 50 assert isinstance(params['optimizer'], str) assert isinstance(params['ndim_problem'], int) and params['ndim_problem'] > 0 if params['optimizer'] == 'PRS': # 1952 # PRS: Ashby's "Design for a Brain: The Origin of Adaptive Behavior" # (1952 - 1st Edition / 1960 - 2nd Edition) # https://link.springer.com/book/10.1007/978-94-015-1320-3 from pypop7.optimizers.rs.prs import PRS as Optimizer elif params['optimizer'] == 'SRS': # 2001 from pypop7.optimizers.rs.srs import SRS as Optimizer elif params['optimizer'] == 'ARHC': # 2008 from pypop7.optimizers.rs.arhc import ARHC as Optimizer elif params['optimizer'] == 'GS': # 2017 from pypop7.optimizers.rs.gs import GS as Optimizer elif params['optimizer'] == 'BES': from pypop7.optimizers.rs.bes import BES as Optimizer elif params['optimizer'] == 'HJ': from pypop7.optimizers.ds.hj import HJ as Optimizer elif params['optimizer'] == 'NM': from pypop7.optimizers.ds.nm import NM as Optimizer elif params['optimizer'] == 'POWELL': from pypop7.optimizers.ds.powell import POWELL as Optimizer elif params['optimizer'] == 'FEP': from pypop7.optimizers.ep.fep import FEP as Optimizer elif params['optimizer'] == 'GENITOR': from pypop7.optimizers.ga.genitor import GENITOR as Optimizer elif params['optimizer'] == 'G3PCX': from pypop7.optimizers.ga.g3pcx import G3PCX as Optimizer elif params['optimizer'] == 'GL25': from pypop7.optimizers.ga.gl25 import GL25 as Optimizer elif params['optimizer'] == 'COCMA': from pypop7.optimizers.cc.cocma import COCMA as Optimizer elif params['optimizer'] == 'HCC': from pypop7.optimizers.cc.hcc import HCC as Optimizer elif params['optimizer'] == 'SPSO': from pypop7.optimizers.pso.spso import SPSO as Optimizer else: raise ValueError('Optimizer not found.') experiments = Experiments(params['start'], params['end'], params['ndim_problem']) experiments.run(Optimizer) print('Total runtime: {:7.5e}.'.format(time.time() - start_runtime)) ``` -------------------------------- ### Get Salomon Test Data Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/benchmarks/cases.html Returns None for the Salomon test function, indicating no specific test data is provided. ```python def get_y_salomon(): """Get test data for **Salomon** test function. """ return None ``` -------------------------------- ### Get Michalewicz Test Data Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/benchmarks/cases.html Returns None for the Michalewicz test function, indicating no specific test data is provided. ```python def get_y_michalewicz(): """Get test data for **Michalewicz** test function. """ return None ``` -------------------------------- ### COEA Optimizer Initialization and Usage Source: https://pypop.readthedocs.io/en/latest/cc/coea.html Demonstrates how to initialize and use the COEA optimizer for a given problem and options, including running the optimization process and printing results. ```APIDOC ## COEA Class ### Description CoOperative Co-Evolutionary Algorithm (COEA). This is a modified version using real-valued representation for continuous optimization and the GENITOR suboptimizer. ### Parameters #### problem (dict) Arguments for the problem definition: - 'fitness_function' (func) - Objective function to be minimized. - 'ndim_problem' (int) - Number of dimensions of the problem. - 'upper_boundary' (array_like) - Upper boundary of the search range. - 'lower_boundary' (array_like) - Lower boundary of the search range. #### options (dict) Optimizer options: - 'max_function_evaluations' (int, default: np.inf) - Maximum number of function evaluations. - 'max_runtime' (float, default: np.inf) - Maximum runtime allowed in seconds. - 'seed_rng' (int) - Seed for random number generation. - 'n_individuals' (int, default: 100) - Number of individuals (population size). ### Request Example ```python import numpy from pypop7.benchmarks.base_functions import rosenbrock from pypop7.optimizers.cc.coea import COEA problem = { 'fitness_function': rosenbrock, 'ndim_problem': 2, 'lower_boundary': -5.0*numpy.ones((2,)), 'upper_boundary': 5.0*numpy.ones((2,)) } options = { 'max_function_evaluations': 5000, 'seed_rng': 2022, 'x': 3.0*numpy.ones((2,)) } coea = COEA(problem, options) results = coea.optimize() print(f"COEA: {results['n_function_evaluations']}, {results['best_so_far_y']}") ``` ### Response #### Success Response (dict) - 'n_function_evaluations' (int) - Number of function evaluations performed. - 'best_so_far_y' (float) - The best objective function value found. #### Response Example ```json { "n_function_evaluations": 5000, "best_so_far_y": 0.4308 } ``` ``` -------------------------------- ### Get LevyMontalvo Test Data Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/benchmarks/cases.html Returns None for the LevyMontalvo test function, indicating no specific test data is provided. ```python def get_y_levy_montalvo(): """Get test data for **LevyMontalvo** test function. """ return None ``` -------------------------------- ### Get Skew-Rastrigin Test Data Source: https://pypop.readthedocs.io/en/latest/_modules/pypop7/benchmarks/cases.html Retrieves test data for the Skew-Rastrigin function. Use this data to test optimization algorithms. ```python def get_y_skew_rastrigin(ndim): """Get test data for **Skew-Rastrigin** test function. """ y = [[4.0, 1.0, 0.0, 100.0, 400.0], [404.0, 101.0, 0.0, 200.0, 800.0], [804.0, 201.0, 0.0, 201.0, 804.0], [0.0, 400.0, 4.0, 202.0, 3000.0, 1020.0, 1317.0]] return y[ndim] ```