|
本帖最后由 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优点就是表格处理强大。
|
|