### Install CR-FM-NES Package Source: https://github.com/nomuramasahir0/crfmnes/blob/master/README.md Install the CR-FM-NES library using pip. This command is used for setting up the optimization tool. ```bash $ pip install crfmnes ``` -------------------------------- ### Optimize Sphere Function with CR-FM-NES Source: https://github.com/nomuramasahir0/crfmnes/blob/master/README.md A basic example demonstrating the optimization of a sphere function using CR-FM-NES. Ensure the problem is formulated as a minimization problem. Requires NumPy. ```python import numpy as np from crfmnes import CRFMNES dim = 3 f = lambda x: np.sum(x**2) mean = np.ones([dim, 1]) * 0.5 sigma = 0.2 lamb = 6 crfmnes = CRFMNES(dim, f, mean, sigma, lamb) x_best, f_best = crfmnes.optimize(100) print("x_best:{}, f_best:{}".format(x_best, f_best)) # x_best:[1.64023896e-05 2.41682149e-05 3.40657594e-05], f_best:2.0136169613476005e-09 ``` -------------------------------- ### Initialize CRFMNES Optimizer Source: https://context7.com/nomuramasahir0/crfmnes/llms.txt Instantiate the CRFMNES optimizer with problem dimension, objective function, initial mean, step size, and population size. An optional random seed can be provided for reproducibility. ```python import numpy as np from crfmnes import CRFMNES # Define the objective function (must accept column vector, return scalar) def sphere(x): x = x.reshape(-1) return np.sum(x**2) # Problem setup dim = 40 # Problem dimensionality mean = np.ones([dim, 1]) * 2 # Initial mean (column vector shape [dim, 1]) sigma = 0.5 # Initial step size lamb = 16 # Population size (must be even positive integer) # Create optimizer instance optimizer = CRFMNES(dim, sphere, mean, sigma, lamb) # Optional: Create with reproducible random seed optimizer_seeded = CRFMNES(dim, sphere, mean, sigma, lamb, seed=42) ``` -------------------------------- ### Implicitly Constrained Optimization with CR-FM-NES Source: https://context7.com/nomuramasahir0/crfmnes/llms.txt Demonstrates how to handle implicit constraints by returning np.inf for infeasible solutions. Suitable for problems where feasibility can be determined by checking solution properties. ```python import numpy as np from crfmnes import CRFMNES def constrained_sphere(x): """Sphere function with implicit constraint: all variables must be non-negative""" x = x.reshape(-1) # Return infinity for infeasible solutions (any x_i < 0) if np.sum(x < 0) > 0: return np.inf return np.sum(x**2) # Start from positive region dim = 40 mean = np.ones([dim, 1]) * 10 # Initial mean in feasible region sigma = 2.0 lamb = 16 optimizer = CRFMNES(dim, constrained_sphere, mean, sigma, lamb) # More iterations may be needed for constrained problems x_best, f_best = optimizer.optimize(1500) print(f"Best value: {f_best}") print(f"All x >= 0: {np.all(x_best >= 0)}") # Should be True print(f"Min x value: {np.min(x_best)}") # Should be >= 0 (near 0) ``` -------------------------------- ### CRFMNES Class Constructor Source: https://context7.com/nomuramasahir0/crfmnes/llms.txt Initializes the CRFMNES optimizer with problem dimensions, objective function, initial mean, step size, and population size. Optional parameters for seed, search direction, and constraints are also supported. ```APIDOC ## CRFMNES Class Constructor ### Description The main optimizer class that implements the CR-FM-NES algorithm. It takes the problem dimension, objective function, initial mean vector, step size (sigma), and population size (lambda) as required parameters. Optional parameters include random seed for reproducibility, initial search direction vector, box constraints, and penalty coefficients for constraint handling. ### Method `CRFMNES(dim, objective_fn, mean, sigma, population_size, seed=None, initial_direction=None, box_constraints=None, penalty_coeffs=None)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **dim** (int) - Required - Problem dimensionality. - **objective_fn** (callable) - Required - The objective function to minimize. It must accept a column vector and return a scalar. - **mean** (numpy.ndarray) - Required - Initial mean vector (column vector shape [dim, 1]). - **sigma** (float) - Required - Initial step size. - **population_size** (int) - Required - Population size (must be an even positive integer). - **seed** (int) - Optional - Random seed for reproducibility. - **initial_direction** (numpy.ndarray) - Optional - Initial search direction vector. - **box_constraints** (tuple) - Optional - Tuple defining box constraints (min_vals, max_vals). - **penalty_coeffs** (tuple) - Optional - Tuple of penalty coefficients for constraint handling. ### Request Example ```python import numpy as np from crfmnes import CRFMNES def sphere(x): x = x.reshape(-1) return np.sum(x**2) dim = 40 mean = np.ones([dim, 1]) * 2 sigma = 0.5 lamb = 16 optimizer = CRFMNES(dim, sphere, mean, sigma, lamb) optimizer_seeded = CRFMNES(dim, sphere, mean, sigma, lamb, seed=42) ``` ### Response None (Constructor does not return a value, it initializes an object). ``` -------------------------------- ### Box Constraints with Penalty in CR-FM-NES Source: https://context7.com/nomuramasahir0/crfmnes/llms.txt Shows how to apply box constraints using penalty coefficients. Violations of bounds are penalized in the objective function, allowing optimization within specified ranges. ```python import numpy as np from crfmnes import CRFMNES def rastrigin(x): """Rastrigin function - highly multimodal test function""" x = x.reshape(-1) dim = len(x) return 10.0 * dim + np.sum(x**2 - 10.0 * np.cos(2.0 * np.pi * x)) dim = 20 # Define box constraints: each variable in range [-5.12, 5.12] constraints = [[-5.12, 5.12] for _ in range(dim)] mean = np.ones([dim, 1]) * 3 sigma = 2.0 lamb = 200 # Larger population for multimodal functions # Create optimizer with box constraints optimizer = CRFMNES( dim, rastrigin, mean, sigma, lamb, constraint=constraints, # Box constraints per dimension penalty_coef=1e5, # Penalty multiplier for violations use_constraint_violation=True # Enable constraint handling ) x_best, f_best = optimizer.optimize(1000) print(f"Best value: {f_best}") print(f"Solution in bounds: {np.all((x_best >= -5.12) & (x_best <= 5.12))}") ``` -------------------------------- ### optimize Method Source: https://context7.com/nomuramasahir0/crfmnes/llms.txt Executes the CR-FM-NES optimization algorithm for a specified number of iterations, updating distribution parameters and tracking the best solution found. Returns the best solution vector and its objective function value. ```APIDOC ## optimize Method ### Description The primary optimization method that runs the CR-FM-NES algorithm for a specified number of iterations. Each iteration evaluates `lamb` candidate solutions, updates the distribution parameters, and tracks the best solution found. Returns a tuple containing the best solution vector and its corresponding objective function value. ### Method `optimize(iterations)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **iterations** (int) - Required - The number of optimization iterations to perform. ### Request Example ```python import numpy as np from crfmnes import CRFMNES def rosenbrock_chain(x): """Rosenbrock function - a challenging non-convex test function""" x = x.reshape(-1) return np.sum(100.0*(x[1:] - x[:-1]**2)**2 + (1-x[:-1])**2) dim = 40 mean = np.zeros([dim, 1]) sigma = 2.0 lamb = 40 optimizer = CRFMNES(dim, rosenbrock_chain, mean, sigma, lamb) iteration_number = 1500 x_best, f_best = optimizer.optimize(iteration_number) print(f"Best solution found: x_best shape = {x_best.shape}") print(f"Best objective value: f_best = {f_best}") ``` ### Response #### Success Response (200) - **x_best** (numpy.ndarray) - The best solution vector found. - **f_best** (float) - The objective function value of the best solution. #### Response Example ```json { "x_best": "[numpy array representing the best solution]", "f_best": 1.2345e-15 } ``` ``` -------------------------------- ### High-Dimensional Optimization with CR-FM-NES Source: https://context7.com/nomuramasahir0/crfmnes/llms.txt Illustrates CR-FM-NES's capability in high-dimensional spaces (e.g., 80 dimensions). It highlights the practical application for problems where traditional methods face computational challenges. ```python import numpy as np from crfmnes import CRFMNES def ktablet(x): """k-Tablet function for testing variable scaling""" x = x.reshape(-1) dim = len(x) k = int(dim / 4) return np.sum(x[:k]**2) + np.sum((100 * x[k:])**2) # High-dimensional problem (80 dimensions) dim = 80 mean = np.ones([dim, 1]) * 5 sigma = 2.0 lamb = 32 # Scale population with dimension optimizer = CRFMNES(dim, ktablet, mean, sigma, lamb) # Calculate iterations based on expected evaluations expected_evals = 50000 iteration_number = int(expected_evals / lamb) + 1 x_best, f_best = optimizer.optimize(iteration_number) print(f"Dimension: {dim}") print(f"Best value: {f_best:.6e}") print(f"Total evaluations: {optimizer.no_of_evals}") print(f"Evaluations per dimension: {optimizer.no_of_evals / dim:.1f}") ``` -------------------------------- ### one_iteration Method Source: https://context7.com/nomuramasahir0/crfmnes/llms.txt Performs a single iteration of the CR-FM-NES algorithm, generating offspring, evaluating them, and updating distribution parameters. Returns candidate solutions, their evaluation values, and constraint violations. ```APIDOC ## one_iteration Method ### Description Performs a single iteration of the CR-FM-NES algorithm, providing fine-grained control over the optimization process. This method generates offspring, evaluates them, ranks solutions, and updates distribution parameters. Returns the unsorted candidate solutions, their evaluation values, and constraint violations, enabling custom logging, early stopping, or adaptive parameter adjustment. ### Method `one_iteration()` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python import numpy as np from crfmnes import CRFMNES def ellipsoid(x): """Ellipsoid function - tests optimizer on ill-conditioned problems""" x = x.reshape(-1) dim = len(x) return np.sum([((10 ** (6 * i / (dim - 1))) * (x[i] ** 2)) for i in range(dim)]) dim = 20 mean = np.ones([dim, 1]) * 5 sigma = 2.0 lamb = 16 optimizer = CRFMNES(dim, ellipsoid, mean, sigma, lamb) for gen in range(500): xs, evals, violations = optimizer.one_iteration() if gen % 100 == 0: print(f"Generation {gen}: f_best = {optimizer.f_best:.6e}, evaluations = {optimizer.no_of_evals}") print(f"Final best: {optimizer.f_best}") print(f"Total function evaluations: {optimizer.no_of_evals}") ``` ### Response #### Success Response (200) - **xs** (list of numpy.ndarray) - A list of candidate solution vectors generated in this iteration. - **evals** (list of float) - A list of the objective function values for each candidate solution. - **violations** (list of float) - A list of constraint violation values for each candidate solution. #### Response Example ```json { "xs": [ "[numpy array for candidate 1]", "[numpy array for candidate 2]", ... ], "evals": [0.123, 0.456, ...], "violations": [0.0, 0.0, ...] } ``` ``` -------------------------------- ### Perform Single CR-FM-NES Iteration Source: https://context7.com/nomuramasahir0/crfmnes/llms.txt Execute a single iteration of the CR-FM-NES algorithm using the one_iteration method. This provides candidate solutions, their evaluations, and constraint violations, allowing for custom monitoring and control. ```python import numpy as np from crfmnes import CRFMNES def ellipsoid(x): """Ellipsoid function - tests optimizer on ill-conditioned problems""" x = x.reshape(-1) dim = len(x) return np.sum([((10 ** (6 * i / (dim - 1))) * (x[i] ** 2)) for i in range(dim)]) dim = 20 mean = np.ones([dim, 1]) * 5 sigma = 2.0 lamb = 16 optimizer = CRFMNES(dim, ellipsoid, mean, sigma, lamb) # Manual iteration loop with custom monitoring for gen in range(500): # Run single iteration - returns candidates, evals, violations xs, evals, violations = optimizer.one_iteration() # Access current best via optimizer state if gen % 100 == 0: print(f"Generation {gen}: f_best = {optimizer.f_best:.6e}, " f"evaluations = {optimizer.no_of_evals}") # Final results print(f"Final best: {optimizer.f_best}") print(f"Total function evaluations: {optimizer.no_of_evals}") ``` -------------------------------- ### Run CRFMNES Optimization Source: https://context7.com/nomuramasahir0/crfmnes/llms.txt Execute the CR-FM-NES algorithm for a specified number of iterations using the optimize method. This method returns the best solution vector and its objective function value found during the optimization process. ```python import numpy as np from crfmnes import CRFMNES def rosenbrock_chain(x): """Rosenbrock function - a challenging non-convex test function""" x = x.reshape(-1) return np.sum(100.0*(x[1:] - x[:-1]**2)**2 + (1-x[:-1])**2) # Setup for 40-dimensional Rosenbrock dim = 40 mean = np.zeros([dim, 1]) sigma = 2.0 lamb = 40 # Larger population for multimodal landscapes # Create optimizer and run optimizer = CRFMNES(dim, rosenbrock_chain, mean, sigma, lamb) iteration_number = 1500 # Run optimization - returns best solution and best function value x_best, f_best = optimizer.optimize(iteration_number) print(f"Best solution found: x_best shape = {x_best.shape}") print(f"Best objective value: f_best = {f_best}") # Expected output: f_best should converge to < 1e-12 for sufficient iterations ``` -------------------------------- ### CR-FM-NES BibTeX Citation Source: https://github.com/nomuramasahir0/crfmnes/blob/master/README.md BibTeX entry for citing the CR-FM-NES paper in academic work. Use this when referencing the method in publications. ```bibtex @INPROCEEDINGS{ nomura2022fast, title={Fast Moving Natural Evolution Strategy for High-Dimensional Problems}, author={Nomura, Masahiro and Ono, Isao}, booktitle={2022 IEEE Congress on Evolutionary Computation (CEC)}, pages={1-8}, year={2022}, } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.