当前位置: 首页 > news >正文

遗传算法与粒子群算法的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 +\x4CV = np.array([ - x2 + x1 ,-0.001 + (x4-3)**2])   return f, CVproblem = 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 npdef opt(x):x1 = x[0]x2 = x[1]x3 = x[2]x4 = x[3]return (x1 + 2)**2 +\(x2 - 3)**2 +\(x3 + 2)**2 +\x4def 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

http://www.lryc.cn/news/168159.html

相关文章:

  • 无涯教程-JavaScript - ASINH函数
  • ActiveMQ面试题(一)
  • node:glob语法以及常用的文件查找库glob、fast-glob
  • 饲料添加剂 微生物 屎肠球菌
  • 二叉搜索树经典笔试题【力扣、牛客】
  • docker系列(1) - docker环境篇
  • web安全漏洞-SQL注入攻击实验
  • 直接插入排序(C++实现)
  • 【k8s】Pod 的钩子
  • MCU软核 3. Xilinx Artix7上运行cortex-m3软核
  • 基于SpringbootShiro实现的CAS单点登录
  • SocketTool V4.0 使用说明
  • Jenkins结合allure生成测试报告
  • 【Linux】缓冲区/回车换行
  • Java手写插入排序和算法案例拓展
  • Python Opencv实践 - 视频文件操作
  • HCS 中的一些概念(二)
  • Scanner类用法(学习笔记)
  • idea2021.1.3版本双击启动,没反应
  • MC-4/11/01/400 ELAU 软件允许用户完全访问相机设置
  • Error contacting service. It is probably not running.问题解决
  • 01_网络编程_传统IO
  • vue 检查指定路由是否存在
  • 自动化办公更简单了:新版python-office,有哪些更新?
  • windows flask服务卡死的问题
  • 项目上线部署--》服务器部署流程(一)
  • Python:函数调用的实参
  • 174. 地下城游戏 -- 动规
  • js实现websocket服务端和客户端
  • qt qml RadioButton如何设置字体颜色,style提示找不到怎么办?