• matlab练习程序(演化策略ES)


    % 算法步骤如下:
    % 1.设定种群个体数和需要迭代的次数。
    % 2.选择父代中的个体按照公式:
    % z1=sqrt(-2*ln(u1))*sin(2*pi*u2)*m,
    % z2=sqrt(-2*ln(u1))*cos(2*pi*u2)*m
    % 这里u1,u2都是随机值,m是控制因子,演化次数越多m,
    % m越小,父代通过与z1,z2相加得到后代。
    % 3.计算后代的适应性。
    % 4.选择后代中最优的适应性作为全局最优适应性。


    %% matlab 代码开始
    clc;
    clear all;
    close all;
    %% 绘出所求函数图形
    [x,y] = meshgrid(-100:100,-100:100);
    sigma = 50;
    img = (1/(2*pi*sigma^2))*exp(-(x.^2 + y.^2)/(2*sigma^2));
    mesh(img);
    axis([-100,100,-100,100,2e-5,8e-5]);
    hold on
    %% 初始化种群
    pop_size = 20;%种群规模
    pop = struct([]);
    iterative = 100;
    for i = 1:pop_size
    pop(i).x = -100 + 200*rand();
    pop(i).y = -100 + 200*rand();
    pop(i).fit = compute_fit(pop(i));
    end
    pop_best = pop(1);

    %% 演化开始
    p1 = plot3(pop_best.x +100,pop_best.y+100,pop_best.fit,'k.','markersize',30);
    for i = 1:iterative
    set(p1, 'xdata',pop_best.x+100,'ydata',pop_best.y+100,'zdata',pop_best.fit)
    drawnow
    pause(.1)
    [pop,pop_best] = select_and_combin(pop,pop_best,pop_size,i,iterative);
    end

    function [new_pop,pop_best] = select_and_combin(pop,pop_best,pop_size,k,iterative)
    mul = (iterative - k) / iterative;
    new_pop = pop;
    % z1=sqrt(-2*ln(u1))*sin(2*pi*u2)*m,
    % z2=sqrt(-2*ln(u1))*cos(2*pi*u2)*m
    for i = 1:pop_size
    %随机数
    u1 = rand();
    u2 = rand();
    z1 = sqrt(-2*log(u1))*sin(2*pi*u2)*mul;
    z2 = sqrt(-2*log(u1))*cos(2*pi*u2)*mul;
    %变异
    new_pop(i).x = pop(i).x + z1;
    new_pop(i).y = pop(i).y + z2;
    %计算适应值
    new_pop(i).fit = compute_fit(new_pop(i));
    if new_pop(i).fit < pop(i).fit
    new_pop(i) = pop(i);
    end
    if pop_best.fit < new_pop(i).fit
    pop_best = pop(i);
    end
    end
    end

    function re = compute_fit(pop)
    x = pop.x;
    y = pop.y;
    sigma = 50;
    if x < -100 || x > 100 || y < -100 || y > 100
    re = 0;
    else
    re =(1/(2*pi*sigma^2))*exp(-(x^2 + y ^2)/(2 * sigma^2));
    end

     http://www.cnblogs.com/tiandsp/p/3165388.html

  • 相关阅读:
    Android开发 View_自定义快速索引侧边栏 SideBarView
    Android开发 PopupWindow开发的一些例子
    Android开发 Fragment里监听返回键
    AndroidStudio 清除项目里无用的资源
    Android开发 SingleLiveEvent解决LiveData或者MutableLiveData多次回调的问题
    字母排列城市列表资源
    Android开发 GridView详解
    Android开发 NestedScrollView嵌套RecyclerView导致的UI加载慢的问题
    Android开发 跳转指定应用商城评分
    Android开发 Activity生命周期详解
  • 原文地址:https://www.cnblogs.com/Kermit-Li/p/4051915.html
Copyright © 2020-2023  润新知