Example

Here is a working example of how to execute the PSO library using a benchmark function as fitness function.

 1# -*- coding: utf-8 -*-
 2"""
 3Created on Wed Nov 17 11:35:46 2021
 4
 5@author: Bruno Veiga
 6"""
 7from pyticleswarm import run_pso
 8import numpy as np
 9#import benchmark_functions as bf
10
11# Example 1
12
13# Define fitness function
14def func(x):
15    # hypersphere function
16    f = np.sum(x**2)
17
18    return f
19# or a benchmark can also be used
20#func = bf.Hypersphere(n_dimensions=2)
21
22# Create variables
23n_vars= 2
24low_bounds = -5.12
25up_bounds = 5.12
26# or for the benchmark function
27# n_vars=func.n_dimensions()
28# m = func.suggested_bounds()
29# low_bounds = m[0]
30# up_bounds = m[1]
31
32""" 
33Stantard parameters values:
34
35initial_solution        = []    Matrix or array containing the initial solutions or solution
36generate_int_pop        = 1     Type of initial population generation
37brm_function            = 4     Function to handle boundary constraint violation
38penalty                 = 10    Brick wall penalty value or adaptive penalty offset
39n_jobs                  = -2    Number of concurrently running jobs
40direct_repair           = None  The direct repair function
41perc_repair             = 0     Value between 0 and 1 that determines the percentage of iterations starting from the end where a repair function is applied
42wmax                    = 0.5   The maximum value of the inertia weight
43wmin                    = 0.1   The minimum value of the itertia weight
44c1min                   = 0     Minimum value of the acceleration coefficient c1
45c1max                   = 0.4   Maximum value of the acceleration coefficient c1
46c2min                   = 0.1   Minimum value of the acceleration coefficient c2
47c2max                   = 2     Maximum value of the acceleration coefficient c1 
48n_iterations            = 100   The total number of iterations
49n_particles             = 10    The total number of particles
50n_trials                = 30    The total number of trials
51show_fitness_graphic    = False Boolean that indicates if the fitness graphic is to be shown or not
52show_particle_graphics  = False Boolean that indicates if the particles graphics are to be shown or not (Only works with solutions of 2 dimensions)
53"""
54# Run PSO
55res = run_pso(n_vars, fitness_function=func, low_bounds=low_bounds, up_bounds= up_bounds, c1min = 0.4, c1max = 0.6, c2min = 0.4, generate_int_pop=1,penalty=1000000,
56            c2max = 0.6, wmax = 0.6, wmin = 0.4, n_iterations=100,n_particles=100, brm_function=4, show_fitness_grapic=True,n_jobs=-2, )
57
58# Print results
59"""
60Results:
61
62res.fitness_value is a float correspending to the best fitness value with size = 1 
63res.it_fitness_value is a array with all the fitness values of the best execution with size = n_iterations
64res.solution is the solution correspoding to best fitness value with size = n_vars
65res.tot_exec_time is the total of time of execution of the algorithm with size = 1 
66res.avg_exec_time is the average of time of execution of the algorithm with size = 1
67"""
68