欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现遗传算法实例分享(打印城市信息)

程序员文章站 2024-02-21 13:32:22
复制代码 代码如下:import java.util.*;public class tsp {      private strin...

复制代码 代码如下:

import java.util.*;
public class tsp { 
    private string cityname[]={"北京","上海","天津","重庆","哈尔滨","长春","沈阳","呼和浩特","石家庄","太原","济南","郑州","西安","兰州","银川","西宁","乌鲁木齐","合肥","南京","杭州","长沙","南昌","武汉","成都","贵州","福建","台北","广州","海口","南宁","昆明","拉萨","香港","澳门"};
    //private string cityend[]=new string[34];
    private int citynum=cityname.length;     //城市个数
    private int popsize = 50;               //种群数量
    private int maxgens = 20000;            //迭代次数
    private double pxover = 0.8;            //交叉概率
    private double pmultation = 0.05;       //变异概率
    private long[][] distance = new long[citynum][citynum];
    private int range = 2000;               //用于判断何时停止的数组区间
    private class genotype {
        int city[] = new int[citynum];      //单个基因的城市序列
        long fitness;                      //该基因的适应度
        double selectp;                        //选择概率
        double exceptp;                        //期望概率
        int isselected;                        //是否被选择
    }
    private genotype[] citys = new genotype[popsize];
    /**
     *    构造函数,初始化种群
     */
    public tsp() {
        for (int i = 0; i < popsize; i++) {
            citys[i] = new genotype();
            int[] num = new int[citynum];
            for (int j = 0; j < citynum; j++)
                num[j] = j;
            int temp = citynum;
            for (int j = 0; j < citynum; j++) {
                int r = (int) (math.random() * temp);
                citys[i].city[j] = num[r];
                num[r] = num[temp - 1];
                temp--;
            }
            citys[i].fitness = 0;
            citys[i].selectp = 0;
            citys[i].exceptp = 0;
            citys[i].isselected = 0;
        }
        initdistance();
    }
    /**
     *  计算每个种群每个基因个体的适应度,选择概率,期望概率,和是否被选择。
     */
    public void calall(){
        for( int i = 0; i< popsize; i++){
            citys[i].fitness = 0;
            citys[i].selectp = 0;
            citys[i].exceptp = 0;
            citys[i].isselected = 0;
        }
        calfitness();
        calselectp();
        calexceptp();
        calisselected();
    }
    /**
     *    填充,将多选的填充到未选的个体当中
     */
    public void pad(){
        int best = 0;
        int bad = 0;
        while(true){          
            while(citys[best].isselected <= 1 && best<popsize-1)
                best ++;
            while(citys[bad].isselected != 0 && bad<popsize-1)
                bad ++;
            for(int i = 0; i< citynum; i++)
                citys[bad].city[i] = citys[best].city[i];
                citys[best].isselected --;
                citys[bad].isselected ++;
                bad ++;  
            if(best == popsize ||bad == popsize)
                break;
        }
    }
    /**
     *    交叉主体函数
     */
    public void crossover() {
        int x;
        int y;
        int pop = (int)(popsize* pxover /2);
        while(pop>0){
            x = (int)(math.random()*popsize);
            y = (int)(math.random()*popsize);
            executecrossover(x,y);//x y 两个体执行交叉
            pop--;
        }
    }
    /**
     * 执行交叉函数
     * @param 个体x
     * @param 个体y
     * 对个体x和个体y执行佳点集的交叉,从而产生下一代城市序列
     */
    private void executecrossover(int x,int y){
        int dimension = 0;
        for( int i = 0 ;i < citynum; i++)
            if(citys[x].city[i] != citys[y].city[i]){
                dimension ++;
            } 
        int diffitem = 0;
        double[] diff = new double[dimension];
        for( int i = 0 ;i < citynum; i++){
            if(citys[x].city[i] != citys[y].city[i]){
                diff[diffitem] = citys[x].city[i];
                citys[x].city[i] = -1;
                citys[y].city[i] = -1;
                diffitem ++;
            } 
        }
        arrays.sort(diff);
        double[] temp = new double[dimension];
        temp = gp(x, dimension);
        for( int k = 0; k< dimension;k++)
            for( int j = 0; j< dimension; j++)
                if(temp[j] == k){
                    double item = temp[k];
                    temp[k] = temp[j];
                    temp[j] = item;
                    item = diff[k];
                    diff[k] = diff[j];
                    diff[j] = item; 
                }
        int tempdimension = dimension;
        int tempi = 0;
        while(tempdimension> 0 ){
            if(citys[x].city[tempi] == -1){
                citys[x].city[tempi] = (int)diff[dimension - tempdimension];
                tempdimension --;
            } 
            tempi ++;
        }
        arrays.sort(diff);
        temp = gp(y, dimension);
        for( int k = 0; k< dimension;k++)
            for( int j = 0; j< dimension; j++)
                if(temp[j] == k){
                    double item = temp[k];
                    temp[k] = temp[j];
                    temp[j] = item;
                    item = diff[k];
                    diff[k] = diff[j];
                    diff[j] = item; 
                }
        tempdimension = dimension;
        tempi = 0;
        while(tempdimension> 0 ){
            if(citys[y].city[tempi] == -1){
                citys[y].city[tempi] = (int)diff[dimension - tempdimension];
                tempdimension --;
            } 
            tempi ++;
        }
    }
    /**
     * @param individual 个体
     * @param dimension    维数
     * @return 佳点集   (用于交叉函数的交叉点)    在executecrossover()函数中使用
     */
    private double[] gp(int individual, int dimension){
        double[] temp = new double[dimension];
        double[] temp1 = new double[dimension];
        int p = 2 * dimension + 3;
        while(!issushu(p))
            p++;
        for( int i = 0; i< dimension; i++){
            temp[i] = 2*math.cos(2*math.pi*(i+1)/p) * (individual+1);
            temp[i] = temp[i] - (int)temp[i];
            if( temp [i]< 0)
                temp[i] = 1+temp[i];
        }
        for( int i = 0; i< dimension; i++)
            temp1[i] = temp[i];
        arrays.sort(temp1);
        //排序
        for( int i = 0; i< dimension; i++)
            for( int j = 0; j< dimension; j++)
                if(temp[j]==temp1[i])
                    temp[j] = i;
        return temp;
    }
    /**
     *    变异
     */
    public void mutate(){
        double random;
        int temp;
        int temp1;
        int temp2;
        for( int i = 0 ; i< popsize; i++){
            random = math.random();
            if(random<=pmultation){
                temp1 = (int)(math.random() * (citynum));
                temp2 = (int)(math.random() * (citynum));
                temp = citys[i].city[temp1];
                citys[i].city[temp1] = citys[i].city[temp2];
                citys[i].city[temp2] = temp;
            }
        }     
    }
    /**
     * 打印当前代数的所有城市序列,以及其相关的参数
     */
    public void print(){
    /**
     * 初始化各城市之间的距离
     */
    private void initdistance(){
        for (int i = 0; i < citynum; i++) {
            for (int j = 0; j < citynum; j++){
                distance[i][j] = math.abs(i-j);
            }
        }
    }
    /**
     * 计算所有城市序列的适应度
     */
    private void calfitness() {
        for (int i = 0; i < popsize; i++) {
            for (int j = 0; j < citynum - 1; j++)
                citys[i].fitness += distance[citys[i].city[j]][citys[i].city[j + 1]];
            citys[i].fitness += distance[citys[i].city[0]][citys[i].city[citynum - 1]];
        }
    }
    /**
     * 计算选择概率
     */
    private void calselectp(){
        long sum = 0;
        for( int i = 0; i< popsize; i++)
            sum += citys[i].fitness;
        for( int i = 0; i< popsize; i++)
            citys[i].selectp = (double)citys[i].fitness/sum;
    }
    /**
     * 计算期望概率
     */
    private void calexceptp(){
        for( int i = 0; i< popsize; i++)
            citys[i].exceptp = (double)citys[i].selectp * popsize;
    }
    /**
     * 计算该城市序列是否较优,较优则被选择,进入下一代
     */
    private void calisselected(){
        int needselecte = popsize;
        for( int i = 0; i< popsize; i++)
            if( citys[i].exceptp<1){
                citys[i].isselected++;
                needselecte --;
            }
        double[] temp = new double[popsize];
        for (int i = 0; i < popsize; i++) {
//          temp[i] = citys[i].exceptp - (int) citys[i].exceptp;
//          temp[i] *= 10;
            temp[i] = citys[i].exceptp*10;
        }
        int j = 0;
        while (needselecte != 0) {
            for (int i = 0; i < popsize; i++) {
                if ((int) temp[i] == j) {
                    citys[i].isselected++;
                    needselecte--;
                    if (needselecte == 0)
                        break;
                }
            }
            j++;
        }
    }
    /**
     * @param x
     * @return 判断一个数是否是素数的函数
     */
    private boolean issushu( int x){
           if(x<2) return false;
           for(int i=2;i<=x/2;i++)
           if(x%i==0&&x!=2) return false;
           return true;
        }
    /**
     * @param x 数组
     * @return x数组的值是否全部相等,相等则表示x.length代的最优结果相同,则算法结束
     */
    private boolean issame(long[] x){
        for( int i = 0; i< x.length -1; i++)
            if(x[i] !=x[i+1])
                return false;
        return true;
    }
    /**
     * 打印任意代最优的路径序列
     */
    private void printbestroute(){
        calall();
        long temp = citys[0].fitness;
        int index = 0;
        for (int i = 1; i < popsize; i++) {
            if(citys[i].fitness<temp){
                temp = citys[i].fitness;
                index = i;
            }
        }
        system.out.println();
        system.out.println("最佳路径的序列:");
        for (int j = 0; j < citynum; j++)
        {
            string cityend[]={cityname[citys[index].city[j]]};
            for(int m=0;m<cityend.length;m++)
            {
                system.out.print(cityend[m] + " ");
            }
        }
            //system.out.print(citys[index].city[j] + cityname[citys[index].city[j]] + "  ");
            //system.out.print(cityname[citys[index].city[j]]);
        system.out.println();
    }
    /**
     * 算法执行
     */
    public void run(){
        long[] result = new long[range];
        //result初始化为所有的数字都不相等
        for( int i  = 0; i< range; i++)
            result[i] = i;
        int index = 0;       //数组中的位置
        int num = 1;     //第num代
        while(maxgens>0){
            system.out.println("-----------------  第  "+num+" 代  -------------------------");
            calall();
            print();
            pad();
            crossover();
            mutate();
            maxgens --;
            long temp = citys[0].fitness;
            for ( int i = 1; i< popsize; i++)
                if(citys[i].fitness<temp){
                    temp = citys[i].fitness;
                }
            system.out.println("最优的解:"+temp);
            result[index] = temp;
            if(issame(result))
                break;
            index++;
            if(index==range)
                index = 0;
            num++;
        }
        printbestroute();
    }
    /**
     * @param a 开始时间
     * @param b   结束时间
     */
    public void caltime(calendar a,calendar b){
        long x = b.gettimeinmillis() - a.gettimeinmillis();
        long y = x/1000;
        x = x - 1000*y;
        system.out.println("算法执行时间:"+y+"."+x+" 秒");
    }
    /**
     *    程序入口
     */
    public static void main(string[] args) {
        calendar a = calendar.getinstance(); //开始时间
        tsp tsp = new tsp();
        tsp.run();
        calendar b = calendar.getinstance(); //结束时间
        tsp.caltime(a, b);
    }
}