[Fortran] 纯文本查看 复制代码
program main
use GA
implicit none
integer(kind=IB)::x=6,y
type(genetic),allocatable :: process
ALLOCATE(process)
process%func => Fitness_func ! 适应度函数 vector
process%seed = 623456 ! 随机数种子
process%iter_num = 200 ! 迭代次数
process%population = 200 ! 种群大小
process%numerical = 7 ! 有效数字
process%retain_rate = 0.35_RP ! 存活率
process%random_rate = 0.35_RP ! 随机率
process%mutation_rate = 0.005_RP ! 变异率
process%x_num = 2 ! 自变量个数
!process%IsGray = .true. ! .true.代表格雷编码
!process%IsMin = .true. ! .true.代表求最小值
process%upperb = (/12.1_RP,5.8_RP/) ! 自变量上限
process%lowerb = (/-3.0_RP,4.1_RP/) ! 自变量下限
call process%Evolve() ! 演化
call process%Output() ! 输出
!以下验证编码转换函数
call process%binary2gray(x,y) !二进制编码转换为格雷码
write(*,*)y
call process%gray2binary(x,y) !格雷码转换为二进制编码
write(*,*)y
DEALLOCATE(process)
stop
contains
!----------------------------------------------------------------------------!
real(kind=RP) function Fitness_func(x) ! sufficiency function 适应度函数
implicit none
!process%x_num明确了自变量个数
real(kind=RP), parameter :: PI=3.141592653589793_RP
real(kind=RP), intent(in) :: x(:)
!算例 -3.0<x(1)<12.1 4.1<x(2)<5.8 在(11.6255,5.7250)处有一个全局最大值38.8503
Fitness_func = 21.5_RP + x(1) * Sin(4.0_RP * PI * x(1)) + x(2) * Sin(20.0_RP * PI * x(2))
end function Fitness_func
!----------------------------------------------------------------------------!
end program main