真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

遺傳算法java源代碼 遺傳算法java源代碼是什么

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

把這個地址的程序 中,山消這一句山唯老public void print(){

新林網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),新林網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為新林超過千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的新林做網(wǎng)站的公司定做!

改成public void print(){}加一個大逗升括號就可以運行了。

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

題目好像是讓你做個增強版的List ,簡單的都實現(xiàn)了 程序架子大概是這樣,排序查找什么的百度搜下 算法很多,套著每樣寫個方法就行了,測試就在main‘方法里寫

public?class?MyList?{

private?String[]?arr;

private?int?count?;

public?MyList?(int?count){

arr?=?new?String[count];

知雹塵???????this.count?=?count;

}

public?MyList?(int[]?intArr){

arr?=?new?String[intArr.length];

this.count?=?intArr.length;

for(int?i=0;iintArr.length;i++){

arr[i]?=?intArr[i]+"";

}

}

public?MyList?(String[]?stringArr){

arr?=?stringArr;

this.count?=?stringArr.length;

}

public?int?getLength(){

return?count;

}

//清空容器內(nèi)肆卜的數(shù)組。

public?void?clearAll(){

arr?=?new?String[count];

}

//通過給定元素下標來刪除某一元素

public?void?removeBySeqn(int?seqn){

搭禪???????if(seqn?=?0??seqncount){

arr[seqn]?=?null;

}

}

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

MyList??list?=?new?MyList?(40);

MyList??list1?=?new?MyList?({3,2,125,56,123});

MyList??list2?=?new?MyList?({"123",""ad});

list2.removeBySeqn(0);

list1.clearAll();

}

}

《Java遺傳算法編程》pdf下載在線閱讀全文,求百度網(wǎng)盤云資源

《Java遺傳算法編程》百度網(wǎng)盤pdf最新全集下載:

鏈接:

?pwd=xv3v 提取碼: xv3v

簡介:本書簡單、直接地介紹了遺傳算法,并且針對所討論的示例問題,給出了模襲搏Java代碼的算法實現(xiàn)禪薯。全書分為6章。第1章簡單介紹了人工智能和生物進化的知識背景,這也是遺傳算法的歷史知識背景。第2章給出了一個基本遺傳算法的實現(xiàn);第4章和第5章,分別針對機器人控制器、旅行商問題、排課問題展開分析和討論,并給出了算法實現(xiàn)。在這些章的末尾,還給出旦祥了一些練習(xí)供讀者深入學(xué)習(xí)和實踐。第6章專門討論了各種算法的優(yōu)化問題。 ?

如何用Java實現(xiàn)遺傳算法?

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

示例圖1:

示凱陪例圖2:

實現(xiàn)代碼:

import?java.util.ArrayList;

import?java.util.Collections;

import?java.util.Iterator;

import?java.util.LinkedList;

import?java.util.List;

import?java.util.Random;

/**

* 用遺傳算法走迷宮

*

* @author Orisun

*

*/

public?class?GA {

int?gene_len;?// 基因長度

int?chrom_len;?// 染色體長度

int?population;?// 種群磨孫嫌大小

double?cross_ratio;?// 交叉率

double?muta_ratio;?// 變異率

int?iter_limit;?// 最多進化的代數(shù)

Listboolean[] individuals;?// 存儲當代種群的染色體

Labyrinth labyrinth;

int?width;??????//迷宮一行有多少個格子

int?height;?????//迷宮有多少行

public?class?BI {

double?fitness;

boolean[] indv;

public?BI(double?f,?boolean[] ind) {

fitness = f;

indv = ind;

}

public?double?getFitness() {

return?fitness;

}

public?boolean[] getIndv() {

return?indv;

}

}

ListBI best_individual;?// 存儲每一代中最優(yōu)秀的個體

public?GA(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;

individuals =?new?ArrayListboolean[](population);

best_individual =?new?ArrayListBI(iter_limit);

}

public?int?getWidth() {

return?width;

}

public?void?setWidth(int?width) {

this.width = width;

}

public?double?getCross_ratio() {

return?cross_ratio;

}

public?List瞎手BI getBest_individual() {

return?best_individual;

}

public?Labyrinth getLabyrinth() {

return?labyrinth;

}

public?void?setLabyrinth(Labyrinth labyrinth) {

this.labyrinth = labyrinth;

}

public?void?setChrom_len(int?chrom_len) {

this.chrom_len = chrom_len;

}

public?void?setPopulation(int?population) {

this.population = population;

}

public?void?setCross_ratio(double?cross_ratio) {

this.cross_ratio = cross_ratio;

}

public?void?setMuta_ratio(double?muta_ratio) {

this.muta_ratio = muta_ratio;

}

public?void?setIter_limit(int?iter_limit) {

this.iter_limit = iter_limit;

}

// 初始化種群

public?void?initPopulation() {

Random r =?new?Random(System.currentTimeMillis());

for?(int?i =?0; i population; i++) {

int?len = gene_len * chrom_len;

boolean[] ind =?new?boolean[len];

for?(int?j =?0; j len; j++)

ind[j] = r.nextBoolean();

individuals.add(ind);

}

}

// 交叉

public?void?cross(boolean[] arr1,?boolean[] arr2) {

Random r =?new?Random(System.currentTimeMillis());

int?length = arr1.length;

int?slice =?0;

do?{

slice = r.nextInt(length);

}?while?(slice ==?0);

if?(slice length /?2) {

for?(int?i =?0; i slice; i++) {

boolean?tmp = arr1[i];

arr1[i] = arr2[i];

arr2[i] = tmp;

}

}?else?{

for?(int?i = slice; i length; i++) {

boolean?tmp = arr1[i];

arr1[i] = arr2[i];

arr2[i] = tmp;

}

}

}

// 變異

public?void?mutation(boolean[] individual) {

int?length = individual.length;

Random r =?new?Random(System.currentTimeMillis());

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

}

// 輪盤法選擇下一代,并返回當代最高的適應(yīng)度值

public?double?selection() {

boolean[][] next_generation =?new?boolean[population][];?// 下一代

int?length = gene_len * chrom_len;

for?(int?i =?0; i population; i++)

next_generation[i] =?new?boolean[length];

double[] cumulation =?new?double[population];

int?best_index =?0;

double?max_fitness = getFitness(individuals.get(best_index));

cumulation[0] = max_fitness;

for?(int?i =?1; i population; i++) {

double?fit = getFitness(individuals.get(i));

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

// 尋找當代的最優(yōu)個體

if?(fit max_fitness) {

best_index = i;

max_fitness = fit;

}

}

Random rand =?new?Random(System.currentTimeMillis());

for?(int?i =?0; i population; i++)

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

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

// 把當代的最優(yōu)個體及其適應(yīng)度放到best_individual中

BI bi =?new?BI(max_fitness, individuals.get(best_index));

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

//System.out.println(max_fitness);

best_individual.add(bi);

// 新一代作為當前代

for?(int?i =?0; i population; i++)

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

return?max_fitness;

}

// 折半查找

public?int?findByHalf(double[] arr,?double?find) {

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

return?-1;

int?min =?0;

int?max = arr.length -?1;

int?medium = min;

do?{

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

break;

medium = (min + max) /?2;

if?(arr[medium] find)

min = medium;

else?if?(arr[medium] find)

max = medium;

else

return?medium;

}?while?(min max);

return?max;

}

// 計算適應(yīng)度

public?double?getFitness(boolean[] individual) {

int?length = individual.length;

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

int?x =?1;

int?y =?0;

// 根據(jù)染色體中基因的指導(dǎo)向前走

for?(int?i =?0; i length; i++) {

boolean?b1 = individual[i];

boolean?b2 = individual[++i];

// 00向左走

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

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

x--;

}

}

// 01向右走

else?if?(b1 ==?false? b2 ==?true) {

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

x++;

}

}

// 10向上走

else?if?(b1 ==?true? b2 ==?false) {

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

y--;

}

}

// 11向下走

else?if?(b1 ==?true? b2 ==?true) {

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

y++;

}

}

}

int?n = Math.abs(x - labyrinth.x_end) + Math.abs(y -labyrinth.y_end) +?1;

//????? if(n==1)

//????????? printPath(individual);

return?1.0?/ n;

}

// 運行遺傳算法

public?boolean?run() {

// 初始化種群

initPopulation();

Random rand =?new?Random(System.currentTimeMillis());

boolean?success =?false;

while?(iter_limit-- ?0) {

// 打亂種群的順序

Collections.shuffle(individuals);

for?(int?i =?0; i population -?1; i +=?2) {

// 交叉

if?(rand.nextDouble() cross_ratio) {

cross(individuals.get(i), individuals.get(i +?1));

}

// 變異

if?(rand.nextDouble() muta_ratio) {

mutation(individuals.get(i));

}

}

// 種群更替

if?(selection() ==?1) {

success =?true;

break;

}

}

return?success;

}

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

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

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

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

//????? } else {

//????????? int gen = ga.best_individual.size();

//????????? boolean[] individual = ga.best_individual.get(gen - 1).indv;

//????????? System.out.println(ga.getPath(individual));

//????? }

//? }

// 根據(jù)染色體打印走法

public?String getPath(boolean[] individual) {

int?length = individual.length;

int?x =?1;

int?y =?0;

LinkedListString stack=new?LinkedListString();

for?(int?i =?0; i length; i++) {

boolean?b1 = individual[i];

boolean?b2 = individual[++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("左");

}

}?else?if?(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("右");

}

}?else?if?(b1 ==?true? b2 ==?false) {

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

y--;

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

stack.poll();

else

stack.push("上");

}

}?else?if?(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=new?StringBuilder(length/4);

IteratorString iter=stack.descendingIterator();

while(iter.hasNext())

sb.append(iter.next());

return?sb.toString();

}

}


網(wǎng)站欄目:遺傳算法java源代碼 遺傳算法java源代碼是什么
本文網(wǎng)址:http://weahome.cn/article/ddpipeo.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部