### Install scikit-opt Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/README.md Install the scikit-opt library using pip. This is the standard way to add the package to your Python environment. ```bash pip install scikit-opt ``` -------------------------------- ### Cauchy Simulated Annealing Implementation Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/more_sa.md Provides an example of using Cauchy Simulated Annealing. The 'demo_func' must be defined. ```python from sko.SA import SACauchy sa_cauchy = SACauchy(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150) sa_cauchy.run() print('Cauchy Simulated Annealing: best_x is ', sa_cauchy.best_x, 'best_y is ', sa_cauchy.best_y) ``` -------------------------------- ### Install Developer Version of scikit-opt Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/README.md Clone the repository and install the developer version of scikit-opt. This is useful for contributing to the project or testing the latest changes. ```bash git clone git@github.com:guofei9987/scikit-opt.git cd scikit-opt pip install . ``` -------------------------------- ### Elitist Genetic Algorithm (EGA) Example Source: https://context7.com/guofei9987/scikit-opt/llms.txt Demonstrates the Elitist Genetic Algorithm (EGA) for optimization. The `n_elitist` parameter preserves the best individuals across generations. ```python from sko.GA import EGA def schaffer(p): x1, x2 = p return x1 ** 2 + x2 ** 2 ega = EGA( func=schaffer, n_dim=2, size_pop=50, max_iter=200, prob_mut=0.001, n_elitist=2, # Number of elite individuals to preserve lb=[-1, -1], ub=[1, 1], precision=1e-7 ) best_x, best_y = ega.run() print(f'Best x: {best_x}') print(f'Best y: {best_y}') ``` -------------------------------- ### User Defined Function (UDF) Example Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/README.md Demonstrates how to use a user-defined function for the 'selection' method in the Genetic Algorithm. This allows for custom algorithm implementations. ```python def selection(self, population): pass ``` -------------------------------- ### Run GA and Measure Time (Cache Mode) Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/speed_up.md Executes a Genetic Algorithm (GA) run and measures the time taken. This example is configured for 'cache mode', which may offer performance benefits for certain tasks. ```python start_time = datetime.datetime.now() best_x, best_y = ga4_2.run() print('cache mode, time costs: ', (datetime.datetime.now() - start_time).total_seconds()) ``` -------------------------------- ### Genetic Algorithm (GA) with Constraints Source: https://context7.com/guofei9987/scikit-opt/llms.txt Perform constrained optimization using the Genetic Algorithm. This example minimizes a quadratic function subject to equality and inequality constraints. ```python from sko.GA import GA # Objective function: minimize x1^2 + x2^2 + x3^2 def obj_func(p): x1, x2, x3 = p return x1 ** 2 + x2 ** 2 + x3 ** 2 # Equality constraint: x2 + x3 = 1 (expressed as 1 - x2 - x3 = 0) constraint_eq = [ lambda x: 1 - x[1] - x[2] ] # Inequality constraints: 1 <= x1*x2 <= 5 constraint_ueq = [ lambda x: 1 - x[0] * x[1], # x1*x2 >= 1 lambda x: x[0] * x[1] - 5 # x1*x2 <= 5 ] ga = GA( func=obj_func, n_dim=3, size_pop=50, max_iter=800, lb=[0, 0, 0], ub=[5, 5, 5], constraint_eq=constraint_eq, constraint_ueq=constraint_ueq, precision=1e-7 ) best_x, best_y = ga.run() print(f'Best x: {best_x}') print(f'Best y: {best_y}') ``` -------------------------------- ### Initialize GA with Cache Mode Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/speed_up.md Initializes a Genetic Algorithm (GA) instance with specific parameters, including a function to optimize, dimensions, population size, iteration limit, bounds, and precision. This setup is for using the cache mode. ```python ga4_2 = GA(func=obj_func4_2, n_dim=2, size_pop=6, max_iter=10, lb=[-2, -2], ub=[2, 2], precision=1) ``` -------------------------------- ### Compare Speed of Common, Parallel, and Vectorization Modes Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/speed_up.md This example compares the execution time of objective functions using 'common', 'multithreading', 'multiprocessing', and 'vectorization' modes. It requires the objective function to be compatible with the chosen mode. The 'vectorization' mode requires the function to explicitly support vectorized inputs. ```python import numpy as np from sko.GA import GA import time import datetime from sko.tools import set_run_mode def generate_costly_function(task_type='io_costly'): # generate a high cost function to test all the modes # cost_type can be 'io_costly' or 'cpu_costly' if task_type == 'io_costly': def costly_function(): time.sleep(0.1) return 1 else: def costly_function(): n = 10000 step1 = [np.log(i + 1) for i in range(n)] step2 = [np.power(i, 1.1) for i in range(n)] step3 = sum(step1) + sum(step2) return step3 return costly_function for task_type in ('io_costly', 'cpu_costly'): costly_function = generate_costly_function(task_type=task_type) def obj_func(p): costly_function() x1, x2 = p x = np.square(x1) + np.square(x2) return 0.5 + (np.square(np.sin(x)) - 0.5) / np.square(1 + 0.001 * x) for mode in ('common', 'multithreading', 'multiprocessing'): set_run_mode(obj_func, mode) ga = GA(func=obj_func, n_dim=2, size_pop=10, max_iter=5, lb=[-1, -1], ub=[1, 1], precision=1e-7) start_time = datetime.datetime.now() best_x, best_y = ga.run() print('on {task_type} task,use {mode} mode, costs {time_costs}s' .format(task_type=task_type, mode=mode, time_costs=(datetime.datetime.now() - start_time).total_seconds())) # to use the vectorization mode, the function itself should support the mode. mode = 'vectorization' def obj_func2(p): costly_function() x1, x2 = p[:, 0], p[:, 1] x = np.square(x1) + np.square(x2) return 0.5 + (np.square(np.sin(x)) - 0.5) / np.square(1 + 0.001 * x) set_run_mode(obj_func2, mode) ga = GA(func=obj_func2, n_dim=2, size_pop=10, max_iter=5, lb=[-1, -1], ub=[1, 1], precision=1e-7) start_time = datetime.datetime.now() best_x, best_y = ga.run() print('on {task_type} task,use {mode} mode, costs {time_costs}s' .format(task_type=task_type, mode=mode, time_costs=(datetime.datetime.now() - start_time).total_seconds())) ``` -------------------------------- ### Run GA and Measure Time (Common Mode) Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/speed_up.md Executes a Genetic Algorithm (GA) run and measures the time taken. This example assumes a 'common mode' of operation, likely without caching. ```python start_time = datetime.datetime.now() best_x, best_y = ga4_1.run() print('common mode, time costs: ', (datetime.datetime.now() - start_time).total_seconds()) ``` -------------------------------- ### Integer Programming with Genetic Algorithm Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/more_ga.md Use GA for integer programming by setting the 'precision' parameter to an integer. This example demonstrates setting specific variables to be integers within defined intervals. ```python from sko.GA import GA demo_func = lambda x: (x[0] - 1) ** 2 + (x[1] - 0.05) ** 2 + x[2] ** 2 ga = GA(func=demo_func, n_dim=3, max_iter=500, lb=[-1, -1, -1], ub=[5, 1, 1], precision=[2, 1, 1e-7]) best_x, best_y = ga.run() print('best_x:', best_x, '\n', 'best_y:', best_y) ``` -------------------------------- ### Genetic Algorithm (GA) for Continuous Functions Source: https://context7.com/guofei9987/scikit-opt/llms.txt Optimize continuous functions using the Genetic Algorithm. This example demonstrates minimizing the Schaffer function with specified bounds and precision. It also shows how to access and plot the optimization history. ```python import numpy as np from sko.GA import GA # Define objective function: Schaffer function with many local minima def schaffer(p): x1, x2 = p part1 = np.square(x1) - np.square(x2) part2 = np.square(x1) + np.square(x2) return 0.5 + (np.square(np.sin(part1)) - 0.5) / np.square(1 + 0.001 * part2) # Initialize GA with parameters ga = GA( func=schaffer, # Objective function to minimize n_dim=2, # Number of dimensions size_pop=50, # Population size (must be even) max_iter=800, # Maximum iterations prob_mut=0.001, # Mutation probability lb=[-1, -1], # Lower bounds for each dimension ub=[1, 1], # Upper bounds for each dimension precision=1e-7 # Precision of variables ) # Run optimization best_x, best_y = ga.run() print(f'Best solution: x = {best_x}, f(x) = {best_y}') # Output: Best solution: x = [0.0, 0.0], f(x) = [0.0] # Access optimization history for plotting import matplotlib.pyplot as plt import pandas as pd Y_history = pd.DataFrame(ga.all_history_Y) fig, ax = plt.subplots(2, 1) ax[0].plot(Y_history.index, Y_history.values, '.', color='red') ax[1].plot(Y_history.min(axis=1).cummin()) plt.show() ``` -------------------------------- ### Compare Speed of Common and Cached Modes Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/speed_up.md This example demonstrates how to compare the performance of the 'common' mode with the 'cached' mode. The 'cached' mode is beneficial when the objective function is computationally expensive and is called with the same inputs multiple times. ```python def obj_func4_1(p): time.sleep(0.1) # say that this function is very complicated and cost 0.1 seconds to run x1, x2 = p x = np.square(x1) + np.square(x2) return 0.5 + (np.square(np.sin(x)) - 0.5) / np.square(1 + 0.001 * x) def obj_func4_2(p): time.sleep(0.1) # say that this function is very complicated and cost 0.1 seconds to run x1, x2 = p x = np.square(x1) + np.square(x2) return 0.5 + (np.square(np.sin(x)) - 0.5) / np.square(1 + 0.001 * x) set_run_mode(obj_func4_2, 'cached') ga4_1 = GA(func=obj_func4_1, n_dim=2, size_pop=6, max_iter=10, lb=[-2, -2], ub=[2, 2], precision=1) ``` -------------------------------- ### GPU Acceleration for GA Source: https://context7.com/guofei9987/scikit-opt/llms.txt Utilize GPU acceleration for genetic algorithms in scikit-opt, typically by integrating with libraries like PyTorch. This example shows the basic GA initialization, implying GPU support is configured elsewhere. ```python from sko.GA import GA def demo_func(x): x1, x2, x3 = x return x1 ** 2 + (x2 - 0.05) ** 2 + x3 ** 2 ga = GA( func=demo_func, n_dim=3, size_pop=100, max_iter=500, lb=[-1, -10, -5], ub=[2, 10, 2] ) ``` -------------------------------- ### PSO for TSP (PSO_TSP) Example Source: https://context7.com/guofei9987/scikit-opt/llms.txt Demonstrates the Particle Swarm Optimization variant for the Travelling Salesman Problem (PSO_TSP). It uses permutation-based operations suitable for TSP. ```python import numpy as np from scipy import spatial from sko.PSO import PSO_TSP # Setup TSP problem num_points = 20 points_coordinate = np.random.rand(num_points, 2) distance_matrix = spatial.distance.cdist( points_coordinate, points_coordinate, metric='euclidean' ) def cal_total_distance(routine): num_points, = routine.shape return sum([ distance_matrix[routine[i % num_points], routine[(i + 1) % num_points]] for i in range(num_points) ]) pso_tsp = PSO_TSP( func=cal_total_distance, n_dim=num_points, size_pop=50, max_iter=200, w=0.8, c1=0.1, c2=0.1 ) best_x, best_y = pso_tsp.run() print(f'Best distance: {best_y}') ``` -------------------------------- ### Fast Simulated Annealing Implementation Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/more_sa.md Demonstrates the basic usage of Fast Simulated Annealing. Ensure the 'demo_func' is defined and accessible. ```python from sko.SA import SAFast sa_fast = SAFast(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150) sa_fast.run() print('Fast Simulated Annealing: best_x is ', sa_fast.best_x, 'best_y is ', sa_fast.best_y) ``` -------------------------------- ### Display Help for scikit-opt Algorithms Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/args.md Use the `help()` function to display detailed information about the input parameters and usage of various optimization algorithms in the scikit-opt library. This is useful for understanding the specific requirements and options for each algorithm. ```python import sko help(sko.GA.GA) help(sko.GA.GA_TSP) help(sko.PSO.PSO) help(sko.DE.DE) help(sko.SA.SA) help(sko.SA.SA_TSP) help(sko.ACA.ACA_TSP) help(sko.IA.IA_TSP) help(sko.AFSA.AFSA) ``` -------------------------------- ### GA for TSP with Fixed Start and End Points Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/more_ga.md Solve the Traveling Salesperson Problem (TSP) with scikit-opt's GA_TSP, specifying fixed start and end points. The objective function calculates the total distance between points, including the fixed start and end points. ```python import numpy as np from scipy import spatial import matplotlib.pyplot as plt num_points = 20 points_coordinate = np.random.rand(num_points, 2) # generate coordinate of points start_point=[[0,0]] end_point=[[1,1]] points_coordinate=np.concatenate([points_coordinate,start_point,end_point]) distance_matrix = spatial.distance.cdist(points_coordinate, points_coordinate, metric='euclidean') def cal_total_distance(routine): '''The objective function. input routine, return total distance. cal_total_distance(np.arange(num_points)) ''' num_points, = routine.shape routine = np.concatenate([[num_points], routine, [num_points+1]]) return sum([distance_matrix[routine[i], routine[i + 1]] for i in range(num_points+2-1)]) ``` ```python from sko.GA import GA_TSP ga_tsp = GA_TSP(func=cal_total_distance, n_dim=num_points, size_pop=50, max_iter=500, prob_mut=1) best_points, best_distance = ga_tsp.run() fig, ax = plt.subplots(1, 2) best_points_ = np.concatenate([[num_points],best_points, [num_points+1]]) best_points_coordinate = points_coordinate[best_points_, :] ax[0].plot(best_points_coordinate[:, 0], best_points_coordinate[:, 1], 'o-r') ax[1].plot(ga_tsp.generation_best_Y) plt.show() ``` -------------------------------- ### SA with Bounds and Different Cooling Schedules Source: https://context7.com/guofei9987/scikit-opt/llms.txt Illustrates Simulated Annealing with bound constraints and demonstrates the use of three distinct cooling schedules: Fast, Boltzmann, and Cauchy. ```python from sko.SA import SAFast, SABoltzmann, SACauchy demo_func = lambda x: x[0] ** 2 + x[1] ** 2 # Fast Annealing with bounds sa_fast = SAFast( func=demo_func, x0=[1, 1], T_max=100, T_min=1e-7, L=300, lb=[-5, -5], # Lower bounds ub=[5, 5], # Upper bounds hop=10 # Step size ) best_x, best_y = sa_fast.run() print(f'Fast SA: x = {best_x}, y = {best_y}') # Boltzmann Annealing sa_boltzmann = SABoltzmann( func=demo_func, x0=[1, 1], T_max=100, T_min=1e-7, L=300, lb=[-5, -5], ub=[5, 5], learn_rate=0.5 ) best_x, best_y = sa_boltzmann.run() print(f'Boltzmann SA: x = {best_x}, y = {best_y}') # Cauchy Annealing sa_cauchy = SACauchy( func=demo_func, x0=[1, 1], T_max=100, T_min=1e-7, L=300, lb=[-5, -5], ub=[5, 5], learn_rate=0.5 ) best_x, best_y = sa_cauchy.run() print(f'Cauchy SA: x = {best_x}, y = {best_y}') ``` -------------------------------- ### Cauchy Simulated Annealing with Bounds Implementation Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/more_sa.md Shows how to implement Cauchy Simulated Annealing with defined lower and upper bounds. Ensure 'demo_func' is defined. ```python from sko.SA import SACauchy sa_cauchy = SACauchy(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150, lb=[-1, 1, -1], ub=[2, 3, 4]) sa_cauchy.run() print('Cauchy Simulated Annealing with bounds: best_x is ', sa_cauchy.best_x, 'best_y is ', sa_cauchy.best_y) ``` -------------------------------- ### Import and Build GA Instance Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/README.md Import the GA class and initialize a Genetic Algorithm instance with a custom objective function, dimensions, population size, and other parameters. Ensure numpy is imported. ```python import numpy as np from sko.GA import GA, GA_TSP demo_func = lambda x: x[0] ** 2 + (x[1] - 0.05) ** 2 + (x[2] - 0.5) ** 2 ga = GA(func=demo_func, n_dim=3, size_pop=100, max_iter=500, prob_mut=0.001, lb=[-1, -10, -5], ub=[2, 10, 2], precision=[1e-7, 1e-7, 1]) ``` -------------------------------- ### PSO with nonlinear inequality constraints Source: https://github.com/guofei9987/scikit-opt/blob/master/README.md Demonstrates how to set up and run Particle Swarm Optimization with nonlinear inequality constraints. Constraints are provided as a tuple of lambda functions. ```python constraint_ueq = ( lambda x: (x[0] - 1) ** 2 + (x[1] - 0) ** 2 - 0.5 ** 2 , ) pso = PSO(func=demo_func, n_dim=2, pop=40, max_iter=max_iter, lb=[-2, -2], ub=[2, 2] , constraint_ueq=constraint_ueq) ``` -------------------------------- ### Fast Simulated Annealing with Bounds Implementation Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/more_sa.md Shows how to apply Fast Simulated Annealing when the search space is constrained by lower and upper bounds. Ensure 'demo_func' is defined. ```python from sko.SA import SAFast sa_fast = SAFast(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150, lb=[-1, 1, -1], ub=[2, 3, 4]) sa_fast.run() print('Fast Simulated Annealing with bounds: best_x is ', sa_fast.best_x, 'best_y is ', sa_fast.best_y) ``` -------------------------------- ### PSO with nonlinear inequality constraint Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/README.md Demonstrates how to set up and run Particle Swarm Optimization with nonlinear inequality constraints. Multiple constraints can be provided as a tuple of lambda functions. ```python constraint_ueq = ( lambda x: (x[0] - 1) ** 2 + (x[1] - 0) ** 2 - 0.5 ** 2 , ) pso = PSO(func=demo_func, n_dim=2, pop=40, max_iter=max_iter, lb=[-2, -2], ub=[2, 2] , constraint_ueq=constraint_ueq) ``` -------------------------------- ### Boltzmann Simulated Annealing Implementation Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/more_sa.md Illustrates the use of Boltzmann Simulated Annealing. Requires 'demo_func' to be defined. ```python from sko.SA import SABoltzmann sa_boltzmann = SABoltzmann(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150) sa_boltzmann.run() print('Boltzmann Simulated Annealing: best_x is ', sa_boltzmann.best_x, 'best_y is ', sa_boltzmann.best_y) ``` -------------------------------- ### GA for Travelling Salesman Problem (GA_TSP) Source: https://context7.com/guofei9987/scikit-opt/llms.txt Solve the Travelling Salesman Problem using a specialized Genetic Algorithm. This example generates random city coordinates, calculates distances, and optimizes the route to minimize total distance. ```python import numpy as np from scipy import spatial from sko.GA import GA_TSP import matplotlib.pyplot as plt # Generate random city coordinates num_points = 50 points_coordinate = np.random.rand(num_points, 2) # Calculate distance matrix distance_matrix = spatial.distance.cdist( points_coordinate, points_coordinate, metric='euclidean' ) # Define objective function: total route distance def cal_total_distance(routine): num_points, = routine.shape return sum([ distance_matrix[routine[i % num_points], routine[(i + 1) % num_points]] for i in range(num_points) ]) # Initialize and run GA_TSP ga_tsp = GA_TSP( func=cal_total_distance, n_dim=num_points, size_pop=50, max_iter=500, prob_mut=1 ) best_points, best_distance = ga_tsp.run() print(f'Best distance: {best_distance}') # Plot the best route fig, ax = plt.subplots(1, 2, figsize=(12, 5)) best_points_ = np.concatenate([best_points, [best_points[0]]]) best_points_coordinate = points_coordinate[best_points_, :] ax[0].plot(best_points_coordinate[:, 0], best_points_coordinate[:, 1], 'o-r') ax[0].set_title('Best Route') ax[1].plot(ga_tsp.generation_best_Y) ax[1].set_title('Convergence') plt.show() ``` -------------------------------- ### Boltzmann Simulated Annealing with Bounds Implementation Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/more_sa.md Demonstrates Boltzmann Simulated Annealing with specified lower and upper bounds for the search variables. Ensure 'demo_func' is defined. ```python from sko.SA import SABoltzmann sa_boltzmann = SABoltzmann(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, q=0.99, L=300, max_stay_counter=150, lb=-1, ub=[2, 3, 4]) sa_boltzmann.run() print('Boltzmann Simulated Annealing with bounds: best_x is ', sa_boltzmann.best_x, 'best_y is ', sa_boltzmann.best_y) ``` -------------------------------- ### Run Simulated Annealing (SA) for Function Optimization Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/README.md Initializes and runs the Simulated Annealing algorithm to find the minimum of a defined objective function. Requires the 'SA' class from 'sko.SA' and a callable objective function. The 'x0' parameter sets the initial guess. ```python from sko.SA import SA sa = SA(func=demo_func, x0=[1, 1, 1], T_max=1, T_min=1e-9, L=300, max_stay_counter=150) best_x, best_y = sa.run() print('best_x:', best_x, 'best_y', best_y) ``` -------------------------------- ### Run Artificial Fish Swarm Algorithm (AFSA) Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/README.md Initializes and runs the Artificial Fish Swarm Algorithm for general function optimization. Requires the 'AFSA' class from 'sko.AFSA'. Parameters include the objective function, number of dimensions, population size, max iterations, and various behavioral parameters like 'step', 'visual', 'q', and 'delta'. ```python def func(x): x1, x2 = x return 1 / x1 ** 2 + x1 ** 2 + 1 / x2 ** 2 + x2 ** 2 from sko.AFSA import AFSA afsa = AFSA(func, n_dim=2, size_pop=50, max_iter=300, max_try_num=100, step=0.5, visual=0.3, q=0.98, delta=0.5) best_x, best_y = afsa.run() print(best_x, best_y) ``` -------------------------------- ### Perform Differential Evolution Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/README.md Initialize and run the Differential Evolution algorithm with the defined objective function, dimensions, population size, iteration limit, bounds, and constraints. Print the best solution found. ```python from sko.DE import DE de = DE(func=obj_func, n_dim=3, size_pop=50, max_iter=800, lb=[0, 0, 0], ub=[5, 5, 5], constraint_eq=constraint_eq, constraint_ueq=constraint_ueq) best_x, best_y = de.run() print('best_x:', best_x, '\n', 'best_y:', best_y) ``` -------------------------------- ### Run PSO with Constraints Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/zh/more_pso.md Initializes and runs a Particle Swarm Optimization algorithm. Requires numpy and scikit-opt's PSO class. The `record_mode` should be set to True to capture optimization history for animation. ```python import numpy as np from sko.PSO import PSO def demo_func(x): x1, x2 = x return -20 * np.exp(-0.2 * np.sqrt(0.5 * (x1 ** 2 + x2 ** 2))) - np.exp( 0.5 * (np.cos(2 * np.pi * x1) + np.cos(2 * np.pi * x2))) + 20 + np.e constraint_ueq = ( lambda x: ( x[0] - 1 ) ** 2 + ( x[1] - 0 ) ** 2 - 0.5 ** 2 , ) max_iter = 50 pso = PSO(func=demo_func, n_dim=2, pop=40, max_iter=max_iter, lb=[-2, -2], ub=[2, 2] , constraint_ueq=constraint_ueq) pso.record_mode = True pso.run() print('best_x is ', pso.gbest_x, 'best_y is', pso.gbest_y) ``` -------------------------------- ### Run PSO Optimization Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/more_pso.md Sets up and runs the Particle Swarm Optimization algorithm. Requires defining the objective function, dimensions, bounds, and optionally constraints. The `record_mode` should be set to `True` to store intermediate results for animation. ```python import numpy as np from sko.PSO import PSO def demo_func(x): x1, x2 = x return -20 * np.exp(-0.2 * np.sqrt(0.5 * (x1 ** 2 + x2 ** 2))) - np.exp( 0.5 * (np.cos(2 * np.pi * x1) + np.cos(2 * np.pi * x2))) + 20 + np.e constraint_ueq = ( lambda x: ( x[0] - 1 ) ** 2 + ( x[1] - 0 ) ** 2 - 0.5 ** 2 , ) max_iter = 50 pso = PSO(func=demo_func, n_dim=2, pop=40, max_iter=max_iter, lb=[-2, -2], ub=[2, 2] , constraint_ueq=constraint_ueq) pso.record_mode = True pso.run() print('best_x is ', pso.gbest_x, 'best_y is', pso.gbest_y) ``` -------------------------------- ### Setting Initial Point for SA Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/more_ga.md Set the initial point for the Simulated Annealing (SA) algorithm using the 'x0' parameter during initialization. ```python # Assuming sa is an initialized SA object # sa = SA(x0=initial_point, **params) ``` -------------------------------- ### Run Particle Swarm Optimization Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/README.md Initializes and runs the Particle Swarm Optimization algorithm with the defined objective function, dimensions, population size, and other parameters. It prints the best solution found. ```python from sko.PSO import PSO pso = PSO(func=demo_func, n_dim=3, pop=40, max_iter=150, lb=[0, -1, 0.5], ub=[1, 1, 1], w=0.8, c1=0.5, c2=0.5) pso.run() print('best_x is ', pso.gbest_x, 'best_y is', pso.gbest_y) ``` -------------------------------- ### Perform Genetic Algorithm Optimization Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/curve_fitting.md Initializes and runs the Genetic Algorithm to find the best parameters for the curve fitting. Sets population size, maximum iterations, and parameter bounds. ```python ga = GA(func=obj_fun, n_dim=4, size_pop=100, max_iter=500, lb=[-2] * 4, ub=[2] * 4) best_params, residuals = ga.run() print('best_x:', best_params, '\n', 'best_y:', residuals) ``` -------------------------------- ### Setting Initial Population for PSO Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/more_ga.md Manually set the initial population for the Particle Swarm Optimization (PSO) algorithm by assigning a NumPy array to the `pso.X` attribute, followed by updating the global and personal best positions. ```python # Assuming pso is an initialized PSO object # pso = PSO(**params) pso.X = initial_population_array pso.cal_y() pso.update_gbest() pso.update_pbest() ``` -------------------------------- ### Continue GA Run Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/README.md Demonstrates continuing a GA run. First, run the algorithm for a specified number of iterations (e.g., 10), then continue the optimization from the current state for additional iterations (e.g., 20). ```python from sko.GA import GA func = lambda x: x[0] ** 2 ga = GA(func=func, n_dim=1) ga.run(10) ga.run(20) ``` -------------------------------- ### Artificial Fish Swarm Algorithm Source: https://context7.com/guofei9987/scikit-opt/llms.txt Demonstrates the Artificial Fish Swarm Algorithm for continuous function optimization. Configurable parameters include population size, max iterations, perception range (visual), and movement step. ```python from sko.AFSA import AFSA # Define objective function def func(x): x1, x2 = x return 1 / x1 ** 2 + x1 ** 2 + 1 / x2 ** 2 + x2 ** 2 # Initialize AFSA afsa = AFSA( func, n_dim=2, size_pop=50, # Number of fish max_iter=300, max_try_num=100, # Max prey attempts step=0.5, # Max movement ratio visual=0.3, # Perception range q=0.98, # Visual decay rate delta=0.5 # Crowding threshold ) best_x, best_y = afsa.run() print(f'Best x: {best_x}') print(f'Best y: {best_y}') ``` -------------------------------- ### Setting Initial Population for DE Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/more_ga.md Manually set the initial population for the Differential Evolution (DE) algorithm by assigning a NumPy array to the `de.X` attribute. ```python # Assuming de is an initialized DE object # de = DE(**params) de.X = initial_population_array ``` -------------------------------- ### Run Genetic Algorithm Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/README.md Execute the Genetic Algorithm's run method to find the optimal solution. The results, including the best found parameters (best_x) and objective function value (best_y), are then printed. ```python best_x, best_y = ga.run() print('best_x:', best_x, '\n', 'best_y:', best_y) ``` -------------------------------- ### Run Simulated Annealing (SA) for TSP Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/README.md Initializes and runs the Simulated Annealing algorithm specifically for the Traveling Salesperson Problem (TSP). Requires the 'SA_TSP' class from 'sko.SA'. The 'func' parameter should be a distance calculation function, and 'x0' is the initial tour. ```python from sko.SA import SA_TSP sa_tsp = SA_TSP(func=cal_total_distance, x0=range(num_points), T_max=100, T_min=1, L=10 * num_points) best_points, best_distance = sa_tsp.run() print(best_points, best_distance, cal_total_distance(best_points)) ``` -------------------------------- ### Setting Initial Population for GA Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/more_ga.md Manually set the initial population for the GA algorithm by assigning a NumPy array to the `ga.Chrom` attribute after initializing the GA object. ```python from sko.GA import GA # Assuming ga is an initialized GA object # ga = GA(**params) ga.Chrom = np.random.randint(0,2,size=(80,20)) ``` -------------------------------- ### Basic Particle Swarm Optimization (PSO) Source: https://context7.com/guofei9987/scikit-opt/llms.txt Implements the classic particle swarm algorithm for continuous optimization. Supports bound constraints and includes plotting of convergence history. ```python from sko.PSO import PSO import matplotlib.pyplot as plt # Define objective function def demo_func(x): x1, x2, x3 = x return x1 ** 2 + (x2 - 0.05) ** 2 + x3 ** 2 # Initialize PSO pso = PSO( func=demo_func, n_dim=3, # Number of dimensions pop=40, # Number of particles max_iter=150, # Maximum iterations lb=[0, -1, 0.5], # Lower bounds ub=[1, 1, 1], # Upper bounds w=0.8, # Inertia weight c1=0.5, # Cognitive parameter (personal best) c2=0.5 # Social parameter (global best) ) # Run optimization pso.run() print(f'Best x: {pso.gbest_x}') print(f'Best y: {pso.gbest_y}') # Plot convergence history plt.plot(pso.gbest_y_hist) plt.xlabel('Iteration') plt.ylabel('Best Fitness') plt.title('PSO Convergence') plt.show() ``` -------------------------------- ### Genetic Algorithm (GA) with Early Stopping Source: https://context7.com/guofei9987/scikit-opt/llms.txt Illustrates the Genetic Algorithm (GA) with early stopping. The `early_stop` parameter halts the process if the best solution remains unchanged for a specified number of iterations. ```python from sko.GA import GA def schaffer(p): x1, x2 = p return x1 ** 2 + x2 ** 2 ga = GA( func=schaffer, n_dim=2, size_pop=50, max_iter=1000, prob_mut=0.001, lb=[-1, -1], ub=[1, 1], early_stop=50 # Stop if best unchanged for 50 iterations ) best_x, best_y = ga.run() print(f'Best x: {best_x}') print(f'Best y: {best_y}') ``` -------------------------------- ### Run Immune Algorithm (IA) for TSP Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/README.md Initializes and runs the Immune Algorithm for the Traveling Salesperson Problem. Requires the 'IA_TSP' class from 'sko.IA'. Parameters include the distance function, number of dimensions, population size, max iterations, mutation probability, and temperature-related parameters. ```python from sko.IA import IA_TSP ia_tsp = IA_TSP(func=cal_total_distance, n_dim=num_points, size_pop=500, max_iter=800, prob_mut=0.2, T=0.7, alpha=0.95) best_points, best_distance = ia_tsp.run() print('best routine:', best_points, 'best_distance:', best_distance) ``` -------------------------------- ### PSO with Non-linear Inequality Constraints Source: https://context7.com/guofei9987/scikit-opt/llms.txt Demonstrates how to use PSO with non-linear inequality constraints to restrict the search space. The constraint is defined as a lambda function. ```python from sko.PSO import PSO def demo_func(x): x1, x2 = x return x1 ** 2 + x2 ** 2 # Non-linear constraint: (x1-1)^2 + x2^2 <= 0.5^2 constraint_ueq = ( lambda x: (x[0] - 1) ** 2 + (x[1] - 0) ** 2 - 0.5 ** 2, ) pso = PSO( func=demo_func, n_dim=2, pop=40, max_iter=200, lb=[-2, -2], ub=[2, 2], constraint_ueq=constraint_ueq ) pso.run() print(f'Best x: {pso.gbest_x}') print(f'Best y: {pso.gbest_y}') ``` -------------------------------- ### Run Genetic Algorithm Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/README.md Initializes and runs the Genetic Algorithm with a specified objective function, dimensions, population size, and other parameters. The `run` method returns the best found solution and its corresponding objective function value. ```python from sko.GA import GA ga = GA(func=schaffer, n_dim=2, size_pop=50, max_iter=800, prob_mut=0.001, lb=[-1, -1], ub=[1, 1], precision=1e-7) best_x, best_y = ga.run() print('best_x:', best_x, '\n', 'best_y:', best_y) ``` -------------------------------- ### Simulated Annealing for TSP Source: https://context7.com/guofei9987/scikit-opt/llms.txt Sets up and runs the Simulated Annealing algorithm to solve the Traveling Salesperson Problem. Requires defining the distance matrix and a function to calculate total route distance. ```python import numpy as np from scipy import spatial from sko.SA import SA_TSP import matplotlib.pyplot as plt # Setup TSP problem num_points = 20 points_coordinate = np.random.rand(num_points, 2) distance_matrix = spatial.distance.cdist( points_coordinate, points_coordinate, metric='euclidean' ) def cal_total_distance(routine): num_points, = routine.shape return sum([ distance_matrix[routine[i % num_points], routine[(i + 1) % num_points]] for i in range(num_points) ]) # Initialize SA_TSP sa_tsp = SA_TSP( func=cal_total_distance, x0=range(num_points), # Initial route T_max=100, T_min=1, L=10 * num_points # Chain length proportional to problem size ) best_points, best_distance = sa_tsp.run() print(f'Best distance: {best_distance}') # Plot results fig, ax = plt.subplots(1, 2, figsize=(12, 5)) best_points_ = np.concatenate([best_points, [best_points[0]]]) best_points_coordinate = points_coordinate[best_points_, :] ax[0].plot(sa_tsp.best_y_history) ax[0].set_xlabel('Iteration') ax[0].set_ylabel('Distance') ax[1].plot(best_points_coordinate[:, 0], best_points_coordinate[:, 1], 'o-b') ax[1].set_title('Best Route') plt.show() ``` -------------------------------- ### Simulated Annealing (SA) Parameters Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/en/args.md Parameters for the Simulated Annealing (SA) algorithm. ```APIDOC ## SA Parameters ### Description Parameters for the Simulated Annealing (SA) algorithm. ### Parameters #### Request Body - **func** (callable) - Required - objective function - **x0** (numpy.ndarray) - Required - initial point - **T_max** (int) - Optional - maximum temperature (default: 100) - **T_min** (float) - Optional - minimum temperature (default: 1e-7) - **L** (int) - Optional - length of chain (default: 300) - **max_stay_counter** (int) - Optional - cooldown time (default: 150) - **lb** (int/float/list) - Optional - lower bound of variables (default: -1) - **ub** (int/float/list) - Optional - upper bound of variables (default: 1) ``` -------------------------------- ### Simulated Annealing (SA) Basic Usage Source: https://context7.com/guofei9987/scikit-opt/llms.txt Performs continuous function optimization using Simulated Annealing with the default Fast Annealing cooling schedule. Includes plotting of convergence. ```python from sko.SA import SA import matplotlib.pyplot as plt import pandas as pd # Define objective function demo_func = lambda x: x[0] ** 2 + (x[1] - 0.05) ** 2 + x[2] ** 2 # Initialize SA (defaults to Fast Annealing) sa = SA( func=demo_func, x0=[1, 1, 1], # Initial solution T_max=1, # Maximum temperature T_min=1e-9, # Minimum temperature L=300, # Iterations per temperature (chain length) max_stay_counter=150 # Stop if no improvement for this many cycles ) # Run optimization best_x, best_y = sa.run() print(f'Best x: {best_x}') print(f'Best y: {best_y}') # Plot convergence plt.plot(pd.DataFrame(sa.best_y_history).cummin(axis=0)) plt.xlabel('Temperature Cycle') plt.ylabel('Best Fitness') plt.title('SA Convergence') plt.show() ``` -------------------------------- ### Animate PSO Optimization Process Source: https://github.com/guofei9987/scikit-opt/blob/master/docs/zh/more_pso.md Visualizes the PSO optimization steps using matplotlib's animation module. This requires the `pso` object from the previous step to have `record_mode=True`. The animation saves the output as 'pso.gif'. ```python import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation record_value = pso.record_value X_list, V_list = record_value['X'], record_value['V'] fig, ax = plt.subplots(1, 1) ax.set_title('title', loc='center') line = ax.plot([], [], 'b.') X_grid, Y_grid = np.meshgrid(np.linspace(-2.0, 2.0, 40), np.linspace(-2.0, 2.0, 40)) Z_grid = demo_func((X_grid, Y_grid)) ax.contour(X_grid, Y_grid, Z_grid, 30) ax.set_xlim(-2, 2) ax.set_ylim(-2, 2) t = np.linspace(0, 2 * np.pi, 40) ax.plot(0.5 * np.cos(t) + 1, 0.5 * np.sin(t), color='r') plt.ion() p = plt.show() def update_scatter(frame): i, j = frame // 10, frame % 10 ax.set_title('iter = ' + str(i)) X_tmp = X_list[i] + V_list[i] * j / 10.0 plt.setp(line, 'xdata', X_tmp[:, 0], 'ydata', X_tmp[:, 1]) return line ani = FuncAnimation(fig, update_scatter, blit=True, interval=25, frames=max_iter * 10) plt.show() ani.save('pso.gif', writer='pillow') ```