『壹』 遺傳演算法的模擬 數據結構題目
我這里給出了一個簡單的模板如果需要編代碼填代碼的地方已經有提示了
/*package otherFile;
import java.util.Random;
import tGraph.TdcppGraph;
import shuffP.*;
*/
/**************
*
* @author vaqeteart
* 這里是遺傳演算法的核心框架遺傳演算法的步驟:
* 遺傳演算法核心部分的演算法描述
* 演算法步驟:
* 1、初始化
* 1.1、生成初始種群編碼
* 1.2、計算每個個體的適配值。
* 1.3、記錄當前最優適配值和最優個體
* 2、選擇和遺傳,
* 2.0、若當前最優適配值多次小於已有的最優適配值(或相差不大)很多次,或者進化的次數超過設定的限制,轉4。
* 2.1、按照與每個個體的適配值成正比的概率選擇個體並復制,復制之後個體的數目和原始種群數目一樣。
* 2.2、(最好先打亂復制後種群的個體次序)對復制後個體進行兩兩配對交叉,生成相同數目的的下一代種群。
* 2.3、對下一代種群按照一定的概率進行變異
* 2.4、計算每個個體的適配值。
* 2.5、記錄當前最優適配值和最優個體
* 2.6、轉2
* 3、返回當前最優適配值以及其對應的編碼,結束。
*
* 注意:
* 1.這里的內容相當於一個模板,編寫具體的遺傳演算法的時候,可以按照這個模板的形式編寫。
* 2.應該填寫代碼的地方都有提示的標記。
*/
public class GAKernel
{
//number of population
int popNum;//set the number to 20 in constructor
//current evolution times
int evolutionTim;
//limit of the evolution times
int evolutionLim;//set the number to 20 in constructor
//unaccepted times
//int eliminTim;
//limit of unaccepted times
//int eliminLim;
//current best euler code
//int curBestCode[];
//current best fitness
int curBestFitness;
//fitness of every indivial
int iFitness[];
//fator of compute the fitness
int factor;
//..................other members.............................................
//the graph
//public TdcppGraph tpGraph;
//the eula code group
//int codes[][];
//every population
//
//constructor
GAKernel(TdcppGraph tG,int eulerCode[])
{
popNum = 32;//2*2*2*2*2
//factor = Integer.MAX_VALUE / popNum;//to avoid overflow when select,for every fitness
evolutionTim = 0;/////
evolutionLim = 15;///////
//this.tpGraph=new TdcppGraph(tG);
//eliminTim = 0;
//eliminLim
curBestFitness = 0;
//curBestCode = new int[eulerCode.length];
//for(int i = 0; i < curBestCode.length; ++i)
//{
// curBestCode[i] = eulerCode[i];
//}
//??curBestFitness
iFitness = new int[popNum];
//codes = new int[popNum][];//lines
for(int i = 0; i < popNum; ++i)
{
//codes[i] = new int[eulerCode.length];
iFitness[i] = 0;
}
System.out.println("構造函數,需要填入代碼");
}
//initialize the originalpopulation
void initPopulation()
{
//.......................初始化種群........................................
//int tmpCode[] = new int[curBestCode.length];
//get the initial indivial
//for(int i = 0; i < curBestCode.length; ++i)
//{
// tmpCode[i] = curBestCode[i];
// codes[0][i] = tmpCode[i];
//}
//ShuffEP s = new ShuffEP(this.tpGraph);
//for(int i = 1; i < popNum; ++i)
//{
// s.shuff(tmpCode);
// for(int j = 0; j < tmpCode.length; ++j)
// {
// codes[i][j] = tmpCode[j];
// }
//}
System.out.println("初始化種群,需要填入代碼");
//get the initial fitness to the member iFitness
computeFitness();
//get the initial best indivial and fitness
recordBest();
}
//compute the fitness of every indivial in current population
void computeFitness()
{
//........................計算每個個體適應度.......................
//int time = 0;
//for(int i = 0; i < popNum; ++i)
//{
// time = 0;
// for(int j = 0; j < codes[i].length - 1; ++j)
// {
// time += tpGraph.Edge(codes[i][j], codes[i][j + 1]).getCost(time);
// }
// iFitness[i] = factor - time;
// if(iFitness[i] < 0)
// {
// System.out.println("錯誤,某個個體適應度過小使得適配值出現負數");//lkdebug
// System.exit(1);
// }
//}
System.out.println("計算每個個體適應度,需要填入代碼");
}
//record the current best fitness and the according indivial
void recordBest()
{
int bestIndex = -1;
for(int i = 0; i < popNum; ++i)
{
if(curBestFitness < iFitness[i])
{
curBestFitness = iFitness[i];
bestIndex = i;
}
}
//............................記錄最優個體.............................
if(bestIndex > -1)
{
// for(int i = 0; i < curBestCode.length; ++i)
// {
// curBestCode[i] = codes[bestIndex][i];
// }
}
System.out.println("記錄最優個體,需要填入代碼");
}
//selection and reproce indivial in population
void selIndivial()
{
int tmpiFitness[] = new int[iFitness.length];
tmpiFitness[0] = iFitness[0];
//建立臨時群體用於選擇交換
//.................................復制個體...............................
//清除原來的群體
//int tmpCode[][] = new int[popNum][];
//for(int i = 0; i < codes.length; ++i)
//{
// tmpCode[i] = new int[codes[i].length];//???
// for(int j = 0; j < codes[i].length; ++j)
// {// to tmpCode and reset codes
// tmpCode[i][j] = codes[i][j];
// codes[i][j] = -1;
// }
//}
System.out.println("復制個體,需要填入代碼");
for(int i = 1; i < tmpiFitness.length; ++i)
{
tmpiFitness[i] = tmpiFitness[i - 1] + iFitness[i];
//iFitness[i] = 0;
}
//輪盤賭選擇個體
for(int i = 0; i < popNum; ++i)
{
int rFit = new Random().nextInt(tmpiFitness[tmpiFitness.length - 1]);
for(int j = 0; j < tmpiFitness.length; ++j)
{
if(rFit < tmpiFitness[j])
{
rFit = j;//record the index of the indivial
break;
}
}
if(rFit == 0)
{
iFitness[i] = tmpiFitness[rFit];
}
else
{
iFitness[i] = tmpiFitness[rFit] - tmpiFitness[rFit - 1];// fitness
}
//....................................選擇個體...........................
//for(int j = 0; j < tmpCode[rFit].length; ++j)
//{
// codes[i][j] =tmpCode[rFit][j];
//}
System.out.println("選擇個體,需要填入代碼");
}
//get the copied fitness in iFitness
}
//match every two indivial and cross the code
void matchCross()
{
//........................需要填入代碼................................
System.out.println("配對交叉,需要填入代碼");
}
//mutate by a specifical probability
void mutate()
{
//........................按照一定的概率進行變異.......................
System.out.println("按照一定的概率進行變異,需要填入代碼");
}
//evolve current population
void evolve()
{
selIndivial();
matchCross();
mutate();
}
//compute the approximative best value by GA
//find approximative best solution by GA
public void compute()
{
initPopulation();
//while((evolutionTim < evolutionLim) && (eliminTim < eliminLim))
while(evolutionTim < evolutionLim)
{
evolve();
//get the initial fitness to the member iFitness
computeFitness();
//get the initial best indivial and fitness
recordBest();
++evolutionTim;
}
}
}
『貳』 用JAVA實現遺傳演算法求最小值的問題,一直報錯,如下: 應該是越界拋的異常,如何解決呢
具體遺傳演算法我沒研究過,但是這個異常是數組下標越界引起的,數組里沒有數據,你去索引了第一個,肯定是哪裡不細心了,如果邏輯沒問題的話,在這一行(GeneticAlgorithmMin.java:102)加個判斷,數組長度為0就不索引,這樣就不會報錯。 不過我估計涉及到邏輯性的其他地方了,就算不報錯,程序也會有邏輯性問題,你給的資料不夠,我盡力了
『叄』 《Java遺傳演算法編程》pdf下載在線閱讀全文,求百度網盤雲資源
《Java遺傳演算法編程》網路網盤pdf最新全集下載:
鏈接: https://pan..com/s/1l6_14X1Yhcgv8kYwHqyY2g
『肆』 使用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編程遺傳演算法怎樣記錄每一代的值呢
在實例化一個數組
沒循環一次往數組里添加一個值
這樣就可以了
『陸』 遺傳演算法代碼出錯
函數minwucha(a,b,c)的參數改為長度為3的向量,如minwucha(p),p為長度為3的向量。
『柒』 求基於遺傳演算法的多目標優化代碼 用C,C++或java實現。最好能夠運行
好高深
『捌』 急求java代碼:遺傳演算法解決車輛路徑問題。。
把這個地址的程序http://..com/question/340500887.html 中,這一句public void print(){
改成public void print(){}加一個大括弧就可以運行了。
『玖』 如何用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 遺傳演算法實現排課功能(控制台程序)的代碼
關於交叉的疑問,不就是父親和母親隨機位上的基因進行交換得到孩子的基因,後面一句」然後選擇所有基因位上的數總和最大的染色體C1「就不明白了。