導航:首頁 > 源碼編譯 > 遺傳演算法java源代碼

遺傳演算法java源代碼

發布時間:2023-03-10 05:35:49

㈠ 急求java代碼:遺傳演算法解決車輛路徑問題。。

把這個地址的程序http://..com/question/340500887.html 中,這一句public void print(){
改成public void print(){}加一個大括弧就可以運行了。

㈡ 求用java實現多目標遺傳演算法的代碼

給分吧,我給你發一段。

㈢ 如何用Java實現遺傳演算法

通過遺傳演算法走迷宮。雖然圖1和圖2均成功走出迷宮,但是圖1比圖2的路徑長的多,且復雜,遺傳演算法可以計算出有多少種可能性,並選擇其中最簡潔的作為運算結果。

示例圖1:

實現代碼:

importjava.util.ArrayList;

importjava.util.Collections;

importjava.util.Iterator;

importjava.util.LinkedList;

importjava.util.List;

importjava.util.Random;

/**

* 用遺傳演算法走迷宮

*

* @author Orisun

*

*/

publicclassGA {

intgene_len;// 基因長度

intchrom_len;// 染色體長度

intpopulation;// 種群大小

doublecross_ratio;// 交叉率

doublemuta_ratio;// 變異率

intiter_limit;// 最多進化的代數

List<boolean[]> indivials;// 存儲當代種群的染色體

Labyrinth labyrinth;

intwidth;//迷宮一行有多少個格子

intheight;//迷宮有多少行

publicclassBI {

doublefitness;

boolean[] indv;

publicBI(doublef,boolean[] ind) {

fitness = f;

indv = ind;

}

publicdoublegetFitness() {

returnfitness;

}

publicboolean[] getIndv() {

returnindv;

}

}

List<BI> best_indivial;// 存儲每一代中最優秀的個體

publicGA(Labyrinth labyrinth) {

this.labyrinth=labyrinth;

this.width = labyrinth.map[0].length;

this.height = labyrinth.map.length;

chrom_len =4* (width+height);

gene_len =2;

population =20;

cross_ratio =0.83;

muta_ratio =0.002;

iter_limit =300;

indivials =newArrayList<boolean[]>(population);

best_indivial =newArrayList<BI>(iter_limit);

}

publicintgetWidth() {

returnwidth;

}

publicvoidsetWidth(intwidth) {

this.width = width;

}

publicdoublegetCross_ratio() {

returncross_ratio;

}

publicList<BI> getBest_indivial() {

returnbest_indivial;

}

publicLabyrinth getLabyrinth() {

returnlabyrinth;

}

publicvoidsetLabyrinth(Labyrinth labyrinth) {

this.labyrinth = labyrinth;

}

publicvoidsetChrom_len(intchrom_len) {

this.chrom_len = chrom_len;

}

publicvoidsetPopulation(intpopulation) {

this.population = population;

}

publicvoidsetCross_ratio(doublecross_ratio) {

this.cross_ratio = cross_ratio;

}

publicvoidsetMuta_ratio(doublemuta_ratio) {

this.muta_ratio = muta_ratio;

}

publicvoidsetIter_limit(intiter_limit) {

this.iter_limit = iter_limit;

}

// 初始化種群

publicvoidinitPopulation() {

Random r =newRandom(System.currentTimeMillis());

for(inti =0; i < population; i++) {

intlen = gene_len * chrom_len;

boolean[] ind =newboolean[len];

for(intj =0; j < len; j++)

ind[j] = r.nextBoolean();

indivials.add(ind);

}

}

// 交叉

publicvoidcross(boolean[] arr1,boolean[] arr2) {

Random r =newRandom(System.currentTimeMillis());

intlength = arr1.length;

intslice =0;

do{

slice = r.nextInt(length);

}while(slice ==0);

if(slice < length /2) {

for(inti =0; i < slice; i++) {

booleantmp = arr1[i];

arr1[i] = arr2[i];

arr2[i] = tmp;

}

}else{

for(inti = slice; i < length; i++) {

booleantmp = arr1[i];

arr1[i] = arr2[i];

arr2[i] = tmp;

}

}

}

// 變異

publicvoidmutation(boolean[] indivial) {

intlength = indivial.length;

Random r =newRandom(System.currentTimeMillis());

indivial[r.nextInt(length)] ^=false;

}

// 輪盤法選擇下一代,並返回當代最高的適應度值

publicdoubleselection() {

boolean[][] next_generation =newboolean[population][];// 下一代

intlength = gene_len * chrom_len;

for(inti =0; i < population; i++)

next_generation[i] =newboolean[length];

double[] cumulation =newdouble[population];

intbest_index =0;

doublemax_fitness = getFitness(indivials.get(best_index));

cumulation[0] = max_fitness;

for(inti =1; i < population; i++) {

doublefit = getFitness(indivials.get(i));

cumulation[i] = cumulation[i -1] + fit;

// 尋找當代的最優個體

if(fit > max_fitness) {

best_index = i;

max_fitness = fit;

}

}

Random rand =newRandom(System.currentTimeMillis());

for(inti =0; i < population; i++)

next_generation[i] = indivials.get(findByHalf(cumulation,

rand.nextDouble() * cumulation[population -1]));

// 把當代的最優個體及其適應度放到best_indivial中

BI bi =newBI(max_fitness, indivials.get(best_index));

// printPath(indivials.get(best_index));

//System.out.println(max_fitness);

best_indivial.add(bi);

// 新一代作為當前代

for(inti =0; i < population; i++)

indivials.set(i, next_generation[i]);

returnmax_fitness;

}

// 折半查找

publicintfindByHalf(double[] arr,doublefind) {

if(find <0|| find ==0|| find > arr[arr.length -1])

return-1;

intmin =0;

intmax = arr.length -1;

intmedium = min;

do{

if(medium == (min + max) /2)

break;

medium = (min + max) /2;

if(arr[medium] < find)

min = medium;

elseif(arr[medium] > find)

max = medium;

else

returnmedium;

}while(min < max);

returnmax;

}

// 計算適應度

publicdoublegetFitness(boolean[] indivial) {

intlength = indivial.length;

// 記錄當前的位置,入口點是(1,0)

intx =1;

inty =0;

// 根據染色體中基因的指導向前走

for(inti =0; i < length; i++) {

booleanb1 = indivial[i];

booleanb2 = indivial[++i];

// 00向左走

if(b1 ==false&& b2 ==false) {

if(x >0&& labyrinth.map[y][x -1] ==true) {

x--;

}

}

// 01向右走

elseif(b1 ==false&& b2 ==true) {

if(x +1< width && labyrinth.map[y][x +1] ==true) {

x++;

}

}

// 10向上走

elseif(b1 ==true&& b2 ==false) {

if(y >0&& labyrinth.map[y -1][x] ==true) {

y--;

}

}

// 11向下走

elseif(b1 ==true&& b2 ==true) {

if(y +1< height && labyrinth.map[y +1][x] ==true) {

y++;

}

}

}

intn = Math.abs(x - labyrinth.x_end) + Math.abs(y -labyrinth.y_end) +1;

// if(n==1)

// printPath(indivial);

return1.0/ n;

}

// 運行遺傳演算法

publicbooleanrun() {

// 初始化種群

initPopulation();

Random rand =newRandom(System.currentTimeMillis());

booleansuccess =false;

while(iter_limit-- >0) {

// 打亂種群的順序

Collections.shuffle(indivials);

for(inti =0; i < population -1; i +=2) {

// 交叉

if(rand.nextDouble() < cross_ratio) {

cross(indivials.get(i), indivials.get(i +1));

}

// 變異

if(rand.nextDouble() < muta_ratio) {

mutation(indivials.get(i));

}

}

// 種群更替

if(selection() ==1) {

success =true;

break;

}

}

returnsuccess;

}

// public static void main(String[] args) {

// GA ga = new GA(8, 8);

// if (!ga.run()) {

// System.out.println("沒有找到走出迷宮的路徑.");

// } else {

// int gen = ga.best_indivial.size();

// boolean[] indivial = ga.best_indivial.get(gen - 1).indv;

// System.out.println(ga.getPath(indivial));

// }

// }

// 根據染色體列印走法

publicString getPath(boolean[] indivial) {

intlength = indivial.length;

intx =1;

inty =0;

LinkedList<String> stack=newLinkedList<String>();

for(inti =0; i < length; i++) {

booleanb1 = indivial[i];

booleanb2 = indivial[++i];

if(b1 ==false&& b2 ==false) {

if(x >0&& labyrinth.map[y][x -1] ==true) {

x--;

if(!stack.isEmpty() && stack.peek()=="右")

stack.poll();

else

stack.push("左");

}

}elseif(b1 ==false&& b2 ==true) {

if(x +1< width && labyrinth.map[y][x +1] ==true) {

x++;

if(!stack.isEmpty() && stack.peek()=="左")

stack.poll();

else

stack.push("右");

}

}elseif(b1 ==true&& b2 ==false) {

if(y >0&& labyrinth.map[y -1][x] ==true) {

y--;

if(!stack.isEmpty() && stack.peek()=="下")

stack.poll();

else

stack.push("上");

}

}elseif(b1 ==true&& b2 ==true) {

if(y +1< height && labyrinth.map[y +1][x] ==true) {

y++;

if(!stack.isEmpty() && stack.peek()=="上")

stack.poll();

else

stack.push("下");

}

}

}

StringBuilder sb=newStringBuilder(length/4);

Iterator<String> iter=stack.descendingIterator();

while(iter.hasNext())

sb.append(iter.next());

returnsb.toString();

}

}

㈣ 用java編程遺傳演算法怎樣記錄每一代的值呢

在實例化一個數組
沒循環一次往數組里添加一個值
這樣就可以了

㈤ 《Java遺傳演算法編程》pdf下載在線閱讀全文,求百度網盤雲資源

《Java遺傳演算法編程》網路網盤pdf最新全集下載:
鏈接: https://pan..com/s/1l6_14X1Yhcgv8kYwHqyY2g

?pwd=xv3v 提取碼: xv3v
簡介:本書簡單、直接地介紹了遺傳演算法,並且針對所討論的示例問題,給出了Java代碼的演算法實現。全書分為6章。第1章簡單介紹了人工智慧和生物進化的知識背景,這也是遺傳演算法的歷史知識背景。第2章給出了一個基本遺傳演算法的實現;第4章和第5章,分別針對機器人控制器、旅行商問題、排課問題展開分析和討論,並給出了演算法實現。在這些章的末尾,還給出了一些練習供讀者深入學習和實踐。第6章專門討論了各種演算法的優化問題。

㈥ 求基於遺傳演算法的多目標優化代碼 用C,C++或java實現。最好能夠運行

好高深

㈦ 急求java 遺傳演算法實現排課功能(控制台程序)的代碼

關於交叉的疑問,不就是父親和母親隨機位上的基因進行交換得到孩子的基因,後面一句」然後選擇所有基因位上的數總和最大的染色體C1「就不明白了。

㈧ 使用java來實現在智能組卷中的遺傳演算法(急急急)

題目好像是讓你做個增強版的List ,簡單的都實現了 程序架子大概是這樣,排序查找什麼的網路搜下 演算法很多,套著每樣寫個方法就行了,測試就在main『方法里寫

publicclassMyList{
privateString[]arr;
privateintcount;
publicMyList(intcount){
arr=newString[count];
this.count=count;
}
publicMyList(int[]intArr){
arr=newString[intArr.length];
this.count=intArr.length;
for(inti=0;i<intArr.length;i++){
arr[i]=intArr[i]+"";
}
}

publicMyList(String[]stringArr){
arr=stringArr;
this.count=stringArr.length;
}
publicintgetLength(){
returncount;
}
//清空容器內的數組。
publicvoidclearAll(){
arr=newString[count];
}
//通過給定元素下標來刪除某一元素
publicvoidremoveBySeqn(intseqn){
if(seqn>=0&&seqn<count){
arr[seqn]=null;
}
}
publicstaticvoidmain(String[]args){
MyListlist=newMyList(40);
MyListlist1=newMyList({3,2,125,56,123});
MyListlist2=newMyList({"123",""ad});
list2.removeBySeqn(0);
list1.clearAll();
}
}

㈨ 用JAVA實現遺傳演算法求最小值的問題,一直報錯,如下: 應該是越界拋的異常,如何解決呢

具體遺傳演算法我沒研究過,但是這個異常是數組下標越界引起的,數組里沒有數據,你去索引了第一個,肯定是哪裡不細心了,如果邏輯沒問題的話,在這一行(GeneticAlgorithmMin.java:102)加個判斷,數組長度為0就不索引,這樣就不會報錯。 不過我估計涉及到邏輯性的其他地方了,就算不報錯,程序也會有邏輯性問題,你給的資料不夠,我盡力了

閱讀全文

與遺傳演算法java源代碼相關的資料

熱點內容
光遇安卓如何一個人拍視頻 瀏覽:277
怨女pdf 瀏覽:708
扭曲伺服器什麼時候開 瀏覽:23
加密貨幣換平台 瀏覽:609
手機內存壓縮軟體 瀏覽:33
生成樹是否與遍歷演算法有關 瀏覽:727
python強化學習迷宮 瀏覽:450
老包子解壓視頻 瀏覽:885
伺服器注冊是什麼意思 瀏覽:418
程序員群體焦慮如何破局 瀏覽:585
程序員在廣州上班 瀏覽:803
androidlinuxadt 瀏覽:512
廣聯達軟體加密鎖原裝晶元 瀏覽:338
如何打開資料庫伺服器 瀏覽:310
kppm是什麼app 瀏覽:538
python多個數組命名 瀏覽:192
a演算法csdn 瀏覽:24
r720伺服器什麼年代 瀏覽:975
本地電腦怎麼設置傳奇伺服器 瀏覽:1002
安卓10框架怎麼製作 瀏覽:959