<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    waysun一路陽(yáng)光

    不輕易服輸,不輕言放棄.--心是夢(mèng)的舞臺(tái),心有多大,舞臺(tái)有多大。踏踏實(shí)實(shí)做事,認(rèn)認(rèn)真真做人。

      BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 ::  :: 管理 ::
      167 隨筆 :: 1 文章 :: 64 評(píng)論 :: 0 Trackbacks
    http://hi.baidu.com/litertiger/blog/item/3798546625f86224aa184c30.html

    書(shū)上給的的一個(gè)算法,實(shí)現(xiàn)了一下

    無(wú)約束條件 max f(x1,x2)=21.5+x1*sin(4*pi*x1)+x2sin(20*pi*x2)

    -3.0<x1<12.1

    4.1<x2<5.8

    1%的變異

    25%交叉

    旋轉(zhuǎn)轉(zhuǎn)輪選擇

    /**
     * 實(shí)現(xiàn)Michalewicz
     * 
     * @author not attributable
     * @version 1.0
     */

     
    public class JGA {
     
     bestindival bd = null;
     
     String[] ipop = new String[10];
     
     int gernation = 0;
     
     public JGA() {
      this.ipop = inialPops();
     }
     
     double calculatefitnessvalue(String str) { // str為染色體,前面18個(gè)為x1表示部分,后面15個(gè)為x2表示部分
      String str1 = str.substring(0, 18);
      // System.out.println(str1);
      String str2 = str.substring(18);
      // System.out.println(str2);
      int b1 = Integer.parseInt(str1, 2);
      // System.out.println(b1);
      int b2 = Integer.parseInt(str2, 2);
      // System.out.println(b2);
      double x1 = -3.0 + b1 * (12.1 - (-3.0)) / (Math.pow(2, 18) - 1);
      // System.out.println(x1);
      double x2 = 4.1 + b2 * (5.8 - 4.1) / (Math.pow(2, 15) - 1);
      // System.out.println(x2);
      double fitness = 21.5 + x1 * Math.sin(4 * 3.1415926 * x1) + x2
        * Math.sin(20 * 3.1415926 * x2);
      //System.out.println("eval=f(" + x1 + "," + x2 + ")=" + fitness);
      return fitness;
     }
     
     String inialPop() { // 初始化10個(gè)字符串
      String res = "";
      for (int i = 0; i < 33; i++) {
       if (Math.random() > 0.5) {
        res += "0";
       } else {
        res += "1";
       }
     
      }
      return res;
     }
     
     String[] inialPops() {
      String[] ipop = new String[10];
      for (int i = 0; i < 10; i++) {
       ipop[i] = inialPop();
      }
      return ipop;
     
     }
     
     void select() {
      double evals[] = new double[10];// 所有染色體適應(yīng)值
      double p[] = new double[10];// 各染色體選擇概率
      double q[] = new double[10];// 累計(jì)概率
      double F = 0;
      for (int i = 0; i < 10; i++) {
       evals[i] = calculatefitnessvalue(ipop[i]);
       if (bd == null) {
        bd = new bestindival();
        bd.fitness = evals[i];
        bd.generations = 0;
        bd.str = ipop[i];
       } else {
        if (evals[i] > bd.fitness)// 最好的記錄下來(lái)
        {
         bd.fitness = evals[i];
         bd.generations = gernation;
         bd.str = ipop[i];
        }
     
       }
       F = F + evals[i];// 所有染色體適應(yīng)值總和
     
      }
      for (int i = 0; i < 10; i++) {
       p[i] = evals[i] / F;
       if (i == 0)
        q[i] = p[i];
       else {
        q[i] = q[i - 1] + p[i];
       }
      }
      for (int i = 0; i < 10; i++) {
     
       double r = Math.random();
       if (r <= q[0]) {
        ipop[i] = ipop[0];
     
       } else {
        for (int j = 1; j < 10; j++) {
         if (r < q[j]) {
          ipop[i] = ipop[j];
          break;
         }
        }
       }
      }
     
     }
     
     void cross() { // 交叉率為25%,平均為25%的染色體進(jìn)行交叉
      String temp1, temp2;
      for (int i = 0; i < 10; i++) {
       if (Math.random() < 0.25) {
        double r = Math.random();
        int pos = (int) (Math.round(r * 1000)) % 33;
        if (pos == 0) {
         pos = 1;
        }
        temp1 = ipop[i].substring(0, pos)
          + ipop[(i + 1) % 10].substring(pos);
        temp2 = ipop[(i + 1) % 10].substring(0, pos)
          + ipop[i].substring(pos);
        ipop[i] = temp1;
        ipop[(i + 1) / 10] = temp2;
     
       }
     
      }
     }
     
     void mutation() {
      // 1%基因變異m*pop_size 共330個(gè)基因,為了使每個(gè)基因都相投機(jī)會(huì)發(fā)生變異,需要產(chǎn)生[1--330]上均勻分布的
      for (int i = 0; i < 4; i++) {
       int num = (int) (Math.random() * 330 + 1);
       int chromosomeNum = (int) (num / 33) + 1; // 染色體號(hào)
       int mutationNum = num - (chromosomeNum - 1) * 33; // 基因號(hào)
       if (mutationNum == 0)
        mutationNum = 1;
       //System.out.println(num + "," + chromosomeNum + "," + mutationNum);
       chromosomeNum = chromosomeNum - 1;
       if(chromosomeNum>=10)
        chromosomeNum=9;
       //System.out.println("變異前" + ipop[chromosomeNum]);
       String temp;
       if (ipop[chromosomeNum].charAt(mutationNum - 1) == '0') {
        if (mutationNum == 1) {
         temp = "1" + ipop[chromosomeNum].substring(mutationNum);
        } else {
         if (mutationNum != 33) {
          temp = ipop[chromosomeNum]
            .substring(0, mutationNum - 1)
            + "1"
            + ipop[chromosomeNum].substring(mutationNum);
         } else {
          temp = ipop[chromosomeNum]
            .substring(0, mutationNum - 1)
            + "1";
         }
        }
       } else {
        if (mutationNum == 1) {
         temp = "0" + ipop[chromosomeNum].substring(mutationNum);
        } else {
         if (mutationNum != 33) {
          temp = ipop[chromosomeNum]
            .substring(0, mutationNum - 1)
            + "0"
            + ipop[chromosomeNum].substring(mutationNum);
         } else {
          temp = ipop[chromosomeNum]
            .substring(0, mutationNum - 1)
            + "1";
         }
        }
     
       }
       ipop[chromosomeNum] = temp;
       //System.out.println("變異后" + ipop[chromosomeNum]);
     
      }
     
     }
     
     void process()
     {
      for(int i=0;i<1000000;i++)
      {
       select();
       cross();
       mutation();
       gernation=i;
       
      }
      System.out.println("最優(yōu)值"+bd.fitness+",代數(shù)"+bd.generations);
     }
     
     public static void main(String args[]) {
      JGA j = new JGA();
      // System.out.println(j.calculatefitnessvalue("000001010100101001101111011111110"));
      j.process();
     
     }
    }
     
    class bestindival { // 存儲(chǔ)最佳的
     public int generations;
     
     public String str;
     
     public double fitness;
     
    }
     

    litertiger
    2006-12-09
    posted on 2009-04-15 23:19 weesun一米陽(yáng)光 閱讀(280) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): JAVA源碼總結(jié)備用
    主站蜘蛛池模板: 免费在线黄色网址| 亚洲国产精品人久久电影| 免费精品国产自产拍在| a免费毛片在线播放| 亚洲中文字幕一二三四区| 亚洲国产成人久久精品动漫| 亚洲国产成人久久一区久久| 黄色片在线免费观看| 亚洲视频在线观看免费| 久久WWW免费人成—看片| 亚洲AV无码专区在线厂| 久久国产亚洲精品| 亚洲成aⅴ人片在线观| 亚洲国产精品国自产电影| 精品亚洲一区二区三区在线播放| 日本特黄特黄刺激大片免费| h在线观看视频免费网站| 亚洲午夜免费视频| 久久久久免费精品国产| 中文字幕在线免费播放| 国产精品亚洲专区无码不卡| 亚洲另类无码专区丝袜| 亚洲中文字幕无码一去台湾 | 亚洲精品国产肉丝袜久久| 国产亚洲精品一品区99热| 国产成人99久久亚洲综合精品 | 亚洲日韩国产一区二区三区在线| 亚洲国产综合在线| 亚洲激情电影在线| 亚洲精品国产肉丝袜久久| 亚洲小说区图片区| 亚洲人6666成人观看| 亚洲一区精品视频在线| 国产精品亚洲精品观看不卡| 亚洲粉嫩美白在线| 亚洲人成自拍网站在线观看| 亚洲狠狠婷婷综合久久蜜芽| 亚洲AV无码专区国产乱码不卡| WWW亚洲色大成网络.COM| 女人裸身j部免费视频无遮挡| 一级黄色免费毛片|