ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 1188|回复: 0

[分享] 差分进化算法VBA版

[复制链接]

TA的精华主题

TA的得分主题

发表于 2019-8-17 19:43 | 显示全部楼层 |阅读模式
本帖最后由 weixing1531 于 2019-8-17 20:08 编辑

'Differential Evolution for Optimal Control Problems差分进化算法
'DE算法是一种基于群体进化的算法,具有记忆个体最优解和种群内信息共享的特点
'通过种群内个体间的合作与竞争来实现对优化问题的求解
'本质是一种基于实数编码的具有保优思想的贪婪遗传算法
'原理:采用对个体进行方向扰动,以达到对个体的函数值进行下降的目的
'不利用函数的梯度信息,因此对函数的可导性甚至连续性没有要求,适用性很强
'翻译自Dr. Feng-Sheng Wang的源程序DE_Fortran90.f90(MATLAB=>FORTRAN90=>VBA)


该程序不是我的原创,我只是把Fortran版本翻译成VBA版本
Fortran版本源代码网址:http://www1.icsi.berkeley.edu/~storn/DE_FORTRAN90.f90
我只是翻译家

使用方法
1.设置参数
在sheet1设置参数  绿色为输入  黄色为输出  
MaxOrMin是我增加的功能 1代表求最大值  -1代表求最小值
2.设置函数
在模块函数Sub FTN(x() As Double, objval As Double)手动更改函数
数组x() 为自变量  objval为目标函数或适应度
3.运行
Alt+F8启用宏main 至于按钮控件这些花哨东西我就不想搞了


我在主程序中采用以下两种方法提高运行速度
  Application.ScreenUpdating = False '取消屏幕更新
  Application.Calculation = xlCalculationManual '手动计算

本程序提供了三个算例
'算例1求最大值 -3.0<x(1)<12.1 4.1<x(2)<5.8  在(11.6255,5.7250)处有一个全局最大值38.8503
'算例2Easom函数求最小值 -100<x(i)<100  在(π,π)处有一个全局最小值-1
'算例3六驼峰返回函数求最小值 -3.0<x(1)<0.0 0.0<x(2)<2.0  在(-0.0898,0.7126)处有一个全局最小值-1.0316

差分进化算法.zip (25.7 KB, 下载次数: 22)

以下是源代码相关说明
!.......................................................................
!
! Differential Evolution for Optimal Control Problems
!
!.......................................................................
!  This Fortran 90 program translates from the original MATLAB
!  version of differential evolution (DE). This FORTRAN 90 code
!  has been tested on Compaq Visual Fortran v6.1.
!  Any users new to the DE are encouraged to read the article of Storn and Price.
!
!  Refences:
!  Storn, R., and Price, K.V., (1996). Minimizing the real function of the
!    ICEC'96 contest by differential evolution. IEEE conf. on Evolutionary
!    Comutation, 842-844.
!
!  This Fortran 90 program written by Dr. Feng-Sheng Wang
!  Department of Chemical Engineering, National Chung Cheng University,
!  Chia-Yi 621, Taiwan, e-mail: chmfsw@ccunix.ccu.edu.tw
!.........................................................................
!                obj : The user provided file for evlauting the objective function.
!                      subroutine obj(xc,fitness)
!                      where "xc" is the real decision parameter vector.(input)
!                            "fitness" is the fitness value.(output)
!             Dim_XC : Dimension of the real decision parameters.
!      XCmin(Dim_XC) : The lower bound of the real decision parameters.
!      XCmax(Dim_XC) : The upper bound of the real decision parameters.
!                VTR : The expected fitness value to reach.
!                 NP : Population size.
!            itermax : The maximum number of iteration.
!               F_XC : Mutation scaling factor for real decision parameters.
!              CR_XC : Crossover factor for real decision parameters.
!           strategy : The strategy of the mutation operations is used in HDE.
!            refresh : The intermediate output will be produced after "refresh"
!                      iterations. No intermediate output will be produced if
!                      "refresh < 1".
!             iwrite : The unit specfier for writing to an external data file.
! bestmen_XC(Dim_XC) : The best real decision parameters.
!              bestval : The best objective function.
!             nfeval : The number of function call.
!         method(1) = 0, Fixed mutation scaling factors (F_XC)
!                   = 1, Random mutation scaling factors F_XC=[0, 1]
!                   = 2, Random mutation scaling factors F_XC=[-1, 1]
!         method(2) = 1, Random combined factor (F_CR) used for strategy = 6
!                        in the mutation operation
!                   = other, fixed combined factor provided by the user
!         method(3) = 1, Saving results in a data file.
!                   = other, displaying results only.


总结:
VBA进行科学计算还是有点不方便,比如数组不能像Fortran数组那样提取片段,程序中我采用的是笨办法。
VBA优点就是表格处理强大。


您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-3-28 18:10 , Processed in 0.038840 second(s), 10 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表