欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

遺傳算法java代碼,java遺傳算法經(jīng)典實(shí)例

用JAVA實(shí)現(xiàn)遺傳算法求最小值的問(wèn)題,一直報(bào)錯(cuò),如下: 應(yīng)該是越界拋的異常,如何解決呢

具體遺傳算法我沒(méi)研究過(guò),但是這個(gè)異常是數(shù)組下標(biāo)越界引起的,數(shù)組里沒(méi)有數(shù)據(jù),你去索引了第一個(gè),肯定是哪里不細(xì)心了,如果邏輯沒(méi)問(wèn)題的話,在這一行(GeneticAlgorithmMin.java:102)加個(gè)判斷,數(shù)組長(zhǎng)度為0就不索引,這樣就不會(huì)報(bào)錯(cuò)。 不過(guò)我估計(jì)涉及到邏輯性的其他地方了,就算不報(bào)錯(cuò),程序也會(huì)有邏輯性問(wèn)題,你給的資料不夠,我盡力了

站在用戶(hù)的角度思考問(wèn)題,與客戶(hù)深入溝通,找到澄城網(wǎng)站設(shè)計(jì)與澄城網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶(hù)體驗(yàn)好的作品,建站類(lèi)型包括:成都做網(wǎng)站、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名與空間、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋澄城地區(qū)。

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

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

示例圖1:

示例圖2:

實(shí)現(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;?// 基因長(zhǎng)度

int?chrom_len;?// 染色體長(zhǎng)度

int?population;?// 種群大小

double?cross_ratio;?// 交叉率

double?muta_ratio;?// 變異率

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

Listboolean[] individuals;?// 存儲(chǔ)當(dāng)代種群的染色體

Labyrinth labyrinth;

int?width;??????//迷宮一行有多少個(gè)格子

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;?// 存儲(chǔ)每一代中最優(yōu)秀的個(gè)體

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?ListBI 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;

}

// 輪盤(pán)法選擇下一代,并返回當(dāng)代最高的適應(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;

// 尋找當(dāng)代的最優(yōu)個(gè)體

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]));

// 把當(dāng)代的最優(yōu)個(gè)體及其適應(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);

// 新一代作為當(dāng)前代

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;

}

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

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

int?length = individual.length;

// 記錄當(dāng)前的位置,入口點(diǎn)是(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;

}

// 運(yù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("沒(méi)有找到走出迷宮的路徑.");

//????? } 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();

}

}

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

在實(shí)例化一個(gè)數(shù)組

沒(méi)循環(huán)一次往數(shù)組里添加一個(gè)值

這樣就可以了

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

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

鏈接:

?pwd=xv3v 提取碼: xv3v

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

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

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

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];

}

//通過(guò)給定元素下標(biāo)來(lái)刪除某一元素

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寫(xiě)的遺傳算法

package baseclass;

import java.awt.BorderLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextArea;

import javax.swing.JTextField;

/**

* 編寫(xiě)者: 賴(lài)志環(huán)

* 標(biāo)準(zhǔn)遺傳算法求解函數(shù)

* 編寫(xiě)日期: 2007-12-2

*/

class Best {

public int generations; //最佳適應(yīng)值代號(hào)

public String str; //最佳染色體

public double fitness; //最佳適應(yīng)值

}

public class SGAFrame extends JFrame {

private JTextArea textArea;

private String str = "";

private Best best = null; //最佳染色體

private String[] ipop = new String[10]; //染色體

private int gernation = 0; //染色體代號(hào)

public static final int GENE = 22; //基因數(shù)

/**

* Launch the application

* @param args

*/

public static void main(String args[]) {

try {

SGAFrame frame = new SGAFrame();

frame.setVisible(true);

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* Create the frame

*/

public SGAFrame() {

super();

this.ipop = inialPops();

getContentPane().setLayout(null);

setBounds(100, 100, 461, 277);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JLabel label = new JLabel();

label.setText("X的區(qū)間:");

label.setBounds(23, 10, 88, 15);

getContentPane().add(label);

final JLabel label_1 = new JLabel();

label_1.setText("[-255,255]");

label_1.setBounds(92, 10, 84, 15);

getContentPane().add(label_1);

final JButton button = new JButton();

button.addActionListener(new ActionListener() {

public void actionPerformed(final ActionEvent e) {

SGAFrame s = new SGAFrame();

str = str + s.process() + "\n";

textArea.setText(str);

}

});

button.setText("求最小值");

button.setBounds(323, 27, 99, 23);

getContentPane().add(button);

final JLabel label_2 = new JLabel();

label_2.setText("利用標(biāo)準(zhǔn)遺傳算法求解函數(shù)f(x)=(x-5)*(x-5)的最小值:");

label_2.setBounds(23, 31, 318, 15);

getContentPane().add(label_2);

final JPanel panel = new JPanel();

panel.setLayout(new BorderLayout());

panel.setBounds(23, 65, 399, 164);

getContentPane().add(panel);

final JScrollPane scrollPane = new JScrollPane();

panel.add(scrollPane, BorderLayout.CENTER);

textArea = new JTextArea();

scrollPane.setViewportView(textArea);

//

}

/**

* 初始化一條染色體(用二進(jìn)制字符串表示)

* @return 一條染色體

*/

private String inialPop() {

String res = "";

for (int i = 0; i GENE; i++) {

if (Math.random() 0.5) {

res += "0";

} else {

res += "1";

}

}

return res;

}

/**

* 初始化一組染色體

* @return 染色體組

*/

private String[] inialPops() {

String[] ipop = new String[10];

for (int i = 0; i 10; i++) {

ipop[i] = inialPop();

}

return ipop;

}

/**

* 將染色體轉(zhuǎn)換成x的值

* @param str 染色體

* @return 染色體的適應(yīng)值

*/

private double calculatefitnessvalue(String str) {

int b = Integer.parseInt(str, 2);

//String str1 = "" + "/n";

double x = -255 + b * (255 - (-255)) / (Math.pow(2, GENE) - 1);

//System.out.println("X = " + x);

double fitness = -(x - 5) * (x - 5);

//System.out.println("f(x)=" + fitness);

//str1 = str1 + "X=" + x + "/n"

//+ "f(x)=" + "fitness" + "/n";

//textArea.setText(str1);

return fitness;

}

/**

* 計(jì)算群體上每個(gè)個(gè)體的適應(yīng)度值;

* 按由個(gè)體適應(yīng)度值所決定的某個(gè)規(guī)則選擇將進(jìn)入下一代的個(gè)體;

*/

private void select() {

double evals[] = new double[10]; // 所有染色體適應(yīng)值

double p[] = new double[10]; // 各染色體選擇概率

double q[] = new double[10]; // 累計(jì)概率

double F = 0; // 累計(jì)適應(yīng)值總和

for (int i = 0; i 10; i++) {

evals[i] = calculatefitnessvalue(ipop[i]);

if (best == null) {

best = new Best();

best.fitness = evals[i];

best.generations = 0;

best.str = ipop[i];

} else {

if (evals[i] best.fitness) // 最好的記錄下來(lái)

{

best.fitness = evals[i];

best.generations = gernation;

best.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;

}

}

}

}

}

/**

* 交叉操作

* 交叉率為25%,平均為25%的染色體進(jìn)行交叉

*/

private void cross() {

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)) % GENE;

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;

}

}

}

/**

* 基因突變操作

* 1%基因變異m*pop_size 共180個(gè)基因,為了使每個(gè)基因都有相同機(jī)會(huì)發(fā)生變異,

* 需要產(chǎn)生[1--180]上均勻分布的

*/

private void mutation() {

for (int i = 0; i 4; i++) {

int num = (int) (Math.random() * GENE * 10 + 1);

int chromosomeNum = (int) (num / GENE) + 1; // 染色體號(hào)

int mutationNum = num - (chromosomeNum - 1) * GENE; // 基因號(hào)

if (mutationNum == 0)

mutationNum = 1;

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 != GENE) {

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 != GENE) {

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]);

}

}

/**

* 執(zhí)行遺傳算法

*/

public String process() {

String str = "";

for (int i = 0; i 10000; i++) {

this.select();

this.cross();

this.mutation();

gernation = i;

}

str = "最小值" + best.fitness + ",第" + best.generations + "個(gè)染色體"+best.str;

return str;

}

}

當(dāng)前名稱(chēng):遺傳算法java代碼,java遺傳算法經(jīng)典實(shí)例
本文來(lái)源:http://chinadenli.net/article22/dsgdojc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站網(wǎng)站改版企業(yè)網(wǎng)站制作微信小程序小程序開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開(kāi)發(fā)