君子不器分享 http://blog.sciencenet.cn/u/foreverph

博文

演化计算:以变异为主导的演化(6)

已有 1835 次阅读 2021-7-4 12:17 |个人分类:算法|系统分类:科研笔记

20世纪60年代,I.Rechenberg和H.P.Schwefel等提出了利用生物变异的思想随机改变风洞实验参数,获得了较好的效果。并进一步发展,形成了演化计算的分支----演化策略(Evolutionary Strategy,ES)。

本节按照前面的思路:以个体、群体、主类构成演化程序;这里的演化策略与传统的稍有不同,不是对实数编码进行变异,而是对二进制编码,以便保持与前面的GA保持连贯。

我们的观点:算法如果与经典著作描述的一致,称为实现,否则称为改进。评价的关键应该是结果。

主要演化思想是:对群体的每个个体均进行试探变异,变异位置随机产生,若变异后个体适度值更好,则选择变异个体,否则保留原来个体。

完整代码如下:

class Point
{
    int[] x;
    static double xmax;
    static double xmin;
    static int size;
    
    public Point()
    {
        x=new int[size];
        init();
    }
    
    public static void set(int isize,double max,double min)
    {
        size=isize;
        xmax=max;
        xmin=min;
    }
    
    public void init()
    {
        for(int i=0;i<size;i++)
            if(Math.random()>0.5)
                x[i]=1;
            else
                x[i]=0;
    }
    
    public double decode()
    {
        double va=0.0;
        for(int i=0;i<size;i++)
            va=va+x[i]*Math.pow(2, size-i-1);
        va=xmin+(xmax-xmin)*(va)/(Math.pow(2, size)-1.0);
        return va;
    }
    
    public double getFitness()
    {
        double va=decode();
        double y=va*Math.sin(va*10*Math.PI)+2.0;
        return y;
    }
    
    public String toString()
    {
        String str="";
        str=str+this.decode()+","+this.getFitness();
        return str;
    }
}
class ES
{
    Point[] obj;       //父代
    int size;         //种群规模
    
    public ES(int isize)
    {
        size=isize;
        obj=new Point[size];
        init();
    }
    
    public void init()
    {
        for(int i=0;i<size;i++)
        {
            obj[i]=new Point();
        }
    }
    
    public void mut(Point a)
    {
        int pos=0;
        pos=(int)(Math.random()*Point.size);
        a.x[pos]=(a.x[pos]+1)%2;
    }
    
    public void copy(Point a,Point b)
    {
        System.arraycopy(a.x, 0, b.x, 0, Point.size);
    }
    
    public int getBestPos()
    {
        int pos=0; 
        Point temp=new Point();
        System.arraycopy(obj[0].x, 0, temp.x, 0, Point.size);
        for(int i=1;i<size;i++)
        {
            if(temp.getFitness()<obj[i].getFitness())
            {
                System.arraycopy(obj[i].x, 0, temp.x, 0, Point.size);
                pos=i;
            }
        }
        return pos;
    }
    
    public void doAll()
    {
        Point temp=new Point();
        for(int i=0;i<size;i++)
        {
            System.arraycopy(obj[i].x, 0, temp.x, 0, Point.size);
            this.mut(temp);
            if(temp.getFitness()>obj[i].getFitness())
                System.arraycopy(temp.x, 0, obj[i].x, 0, Point.size);
        }
    }
}
public class TestES 
{
    public static void main(String[] args) 
    {
        Point.set(22,2.0,-1.0);
        ES s=new ES(40);
        int n=0;
        int bestPOS;
        while(n<500)
        {
            s.doAll();
            n++;
        }
        bestPOS=s.getBestPos();
        for(int i=0;i<s.size;i++)
             System.out.println(s.obj[i]);
        System.out.println("Best:No."+bestPOS);
        System.out.println(s.obj[bestPOS]);
    } 
}

运行结果:

run:

1.0625004917384366,2.981616176135558

0.4522091036341438,2.4511205071961424

1.4374998658895173,3.3280743863560422

1.8505479933137878,3.850273766514116

1.249999821186023,3.2499998211662997

1.249999821186023,3.2499998211662997

0.21874957531680472,2.1215283258630158

0.828124720603161,2.640144454365155

1.8593756817282872,3.7793000288607077

1.8476562136784107,3.8426497540259517

0.45312534645208036,2.450942940622897

1.0625004917384366,2.981616176135558

0.4523929244024574,2.4511151947630827

1.249999821186023,3.2499998211662997

1.8593756817282872,3.7793000288607077

-0.8476562136784109,2.845359381290698

1.249999821186023,3.2499998211662997

0.4522091036341438,2.4511205071961424

1.8476562136784107,3.8426497540259517

1.0625004917384366,2.981616176135558

0.45312534645208036,2.450942940622897

1.0625004917384366,2.981616176135558

0.45312534645208036,2.450942940622897

1.0625004917384366,2.981616176135558

1.249999821186023,3.2499998211662997

1.249999821186023,3.2499998211662997

1.8476562136784107,3.8426497540259517

0.21874957531680472,2.1215283258630158

1.8593756817282872,3.7793000288607077

0.4522319918231945,2.4511206740065674

1.249999821186023,3.2499998211662997

1.249999821186023,3.2499998211662997

1.4374998658895173,3.3280743863560422

1.85054727805788,3.8502737667358073

0.21874957531680472,2.1215283258630158

-0.8511352184141203,2.850593989322364

0.4522091036341438,2.4511205071961424

1.249999821186023,3.2499998211662997

1.8593756817282872,3.7793000288607077

0.828124720603161,2.640144454365155

Best:No. 33

1.85054727805788,3.8502737667358073

成功构建 (总时间: 0 秒)

与基本遗传算法相比,程序更简洁,没有变异率、交叉率,总体结果比GA好。程序在NetBeans IDE下测试。




https://blog.sciencenet.cn/blog-260510-1294008.html

上一篇:演化计算:基于实践的认知(5)
下一篇:演化计算:工具箱的设计(7)
收藏 IP: 111.121.88.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-25 22:17

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部