遗传算法与粒子群算法的Python实现

  • 遗传算法本文应用的是 python geatpy module
  • 粒子群算法本文应用的是 python pyswarm module

遗传算法 

  • 它的不等约束是...<=0
import geatpy as ea
import numpy as np


@ea.Problem.single
def evalVars(Vars): 
    x1 = Vars[0]
    x2 = Vars[1]
    x3 = Vars[2]
    x4 = Vars[3]
    
    f = (x1 + 2)**2 +\
        (x2 - 3)**2 +\
        (x3 + 2)**2 +\
         x4
    
    CV = np.array([ - x2 + x1 ,
                    -0.001 + (x4-3)**2])   
    return f, CV

problem = ea.Problem(name='test',
                     M=1,  
                     maxormins=[1], 
                     Dim=4,                         
                     varTypes=[0, 0, 0, 0],
                     lb=[-5, -5, -5, -5],
                     ub=[ 5,  5,  5,  5],
                     evalVars=evalVars)

algorithm = ea.soea_SEGA_templet(problem,
                ea.Population(Encoding='RI', NIND=20),
                MAXGEN=500,              
                logTras=1,              
                trappedValue=1e-8,   
                maxTrappedCount=10)     

res = ea.optimize(algorithm, seed=1, verbose=True, \
                  drawing=1, outputMsg=True, drawLog=True, \
                  saveFlag=True, dirName='result')
==================================================================================
gen|  eval  |    f_opt    |    f_max    |    f_avg    |    f_min    |    f_std    
----------------------------------------------------------------------------------
 34|  700   | 1.02093E+01 | 1.02093E+01 | 1.02093E+01 | 1.02093E+01 | 0.00000E+00 
 35|  720   | 1.02093E+01 | 1.02093E+01 | 1.02093E+01 | 1.02093E+01 | 0.00000E+00 
 36|  740   | 1.01760E+01 | 2.07075E+01 | 1.15175E+01 | 1.01760E+01 | 3.47351E+00 
 37|  760   | 1.01760E+01 | 2.07636E+01 | 1.24590E+01 | 1.01760E+01 | 3.94129E+00 
 38|  780   | 3.07598E+00 | 1.02096E+01 | 9.84393E+00 | 3.07598E+00 | 1.55274E+00 
 39|  800   | 3.07497E+00 | 1.02093E+01 | 8.79644E+00 | 3.07497E+00 | 2.70962E+00 
 40|  820   | 3.07497E+00 | 1.01723E+01 | 6.33722E+00 | 3.07497E+00 | 3.20984E+00 
 41|  840   | 3.05616E+00 | 3.72432E+00 | 3.30975E+00 | 3.05616E+00 | 2.94966E-01 
 42|  860   | 3.03355E+00 | 3.07598E+00 | 3.07030E+00 | 3.03355E+00 | 1.05715E-02 
 43|  880   | 3.03355E+00 | 3.07497E+00 | 3.06473E+00 | 3.03355E+00 | 1.05250E-02 
 44|  900   | 3.03354E+00 | 3.06010E+00 | 3.05215E+00 | 3.03354E+00 | 9.52747E-03 
 45|  920   | 3.03236E+00 | 3.05616E+00 | 3.04503E+00 | 3.03236E+00 | 1.05466E-02 
 46|  940   | 3.03236E+00 | 3.05163E+00 | 3.03547E+00 | 3.03236E+00 | 5.48001E-03 
 47|  960   | 3.03078E+00 | 3.03355E+00 | 3.03308E+00 | 3.03078E+00 | 7.69107E-04 
 48|  980   | 3.03078E+00 | 3.03354E+00 | 3.03243E+00 | 3.03078E+00 | 8.50674E-04 
 49|  1000  | 3.03060E+00 | 3.03236E+00 | 3.03177E+00 | 3.03060E+00 | 6.64341E-04 
 50|  1020  | 3.03060E+00 | 3.03187E+00 | 3.03109E+00 | 3.03060E+00 | 5.18200E-04 
 51|  1040  | 3.03034E+00 | 3.03080E+00 | 3.03069E+00 | 3.03034E+00 | 1.24733E-04 
 52|  1060  | 3.02960E+00 | 3.03074E+00 | 3.03050E+00 | 3.02960E+00 | 3.41333E-04 
 53|  1080  | 3.02931E+00 | 3.03060E+00 | 3.03028E+00 | 3.02931E+00 | 4.18144E-04 
 54|  1100  | 3.02931E+00 | 3.03056E+00 | 3.02998E+00 | 3.02931E+00 | 4.08041E-04 
 55|  1120  | 3.02931E+00 | 3.03001E+00 | 3.02960E+00 | 3.02931E+00 | 2.06836E-04 
 56|  1140  | 3.02910E+00 | 3.02960E+00 | 3.02942E+00 | 3.02910E+00 | 1.40018E-04 
 57|  1160  | 3.00978E+00 | 3.02951E+00 | 3.02829E+00 | 3.00978E+00 | 4.24883E-03 
 58|  1180  | 3.00978E+00 | 3.02931E+00 | 3.02719E+00 | 3.00978E+00 | 5.80743E-03 
 59|  1200  | 3.00978E+00 | 3.02910E+00 | 3.02684E+00 | 3.00978E+00 | 5.71712E-03 
 60|  1220  | 3.00977E+00 | 3.02876E+00 | 3.02437E+00 | 3.00977E+00 | 7.36375E-03 
 61|  1240  | 3.00913E+00 | 3.02828E+00 | 3.01725E+00 | 3.00913E+00 | 8.26062E-03 
 62|  1260  | 3.00913E+00 | 3.01029E+00 | 3.00973E+00 | 3.00913E+00 | 2.63957E-04 
Execution time: 0.03466796875 s
Evaluation number: 1260
The best objective value is: 3.009134825271758
The best variables are: 
-1.9990825653076172	3.0016613006591797	-2.030458450317383	3.0082035064697266

粒子群算法

  • 它的不等约束是...>=0
from pyswarm import pso
import numpy as np

def opt(x):
    x1 = x[0]
    x2 = x[1]
    x3 = x[2]
    x4 = x[3]

    return (x1 + 2)**2 +\
           (x2 - 3)**2 +\
           (x3 + 2)**2 +\
           x4

def con(x):
    x1 = x[0]
    x2 = x[1]
    x3 = x[2]
    x4 = x[3]

    return [ x2 - x1 ,0.001-(x4-3)**2]

lb = [-5, -5, -5, -5]
ub = [ 5,  5,  5,  5]

xopt, fopt = pso(opt, lb, ub, f_ieqcons=con)
print("xopt:", xopt)
print("fopt:", fopt)
Stopping search: Swarm best objective change less than 1e-08
xopt: [-2.00094176  3.00064191 -1.99431492  2.96837868]
fopt: 2.9684122988614785

你可能感兴趣的:(#,科学计算案例,算法,数学建模,抽象代数,numpy,python)