abs遺傳
1. 如何用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();
}
}
2. matlab 遺傳演算法
function m_main()
clear
clc
Max_gen=100;% 運行代數
pop_size=100;%種群大小
chromsome=10;%染色體的長度
pc=0.9;%交叉概率
pm=0.25;%變異概率
gen=0;%統計代數
%初始化
init=40*rand(pop_size,chromsome)-20;
pop=init;
fit=obj_fitness(pop);
[max_fit,index_max]=max(fit);maxfit=max_fit;
[min_fit,index_min]=min(fit);best_indiv=pop(index_max,:);
%迭代操作
while gen<Max_gen
gen=gen+1; bt(gen)=max_fit;
if maxfit<max_fit;maxfit=max_fit;pop(index_min,:)=pop(index_max,:);best_indiv=pop(index_max,:);end
best_indiv_tmp(gen)=pop(index_max);
newpop=ga(pop,pc,pm,chromsome,fit);
fit=obj_fitness(newpop);
[max_fit,index_max]=max(fit);
[min_fit,index_min]=min(fit);
pop=newpop;
trace(1,gen)=max_fit;
trace(2,gen)=sum(fit)./length(fit);
end
%運行結果
[f_max gen_ct]=max(bt)%求的最大值以及代數
maxfit
best_indiv
%畫圖
% bt
hold on
plot(trace(1,:),'.g:');
plot( trace(2,:),'.r-');
title('實驗結果圖')
xlabel('迭代次數/代'),ylabel('最佳適應度(最大值)');%坐標標注
plot(gen_ct-1,0:0.1:f_max+1,'c-');%畫出最大值
text(gen_ct,f_max+1, '最大值')
hold off
function [fitness]=obj_fitness(pop)
%適應度計算函數
[r c]=size(pop);
x=pop;
fitness=zeros(r,1);
for i=1:r
for j=1:c
fitness(i,1)=fitness(i,1)+sin(sqrt(abs(40*x(i))))+1-abs(x(i))/20.0;
end
end
function newpop=ga(pop,pc,pm,chromsome,fit);
pop_size=size(pop,1);
%輪盤賭選擇
ps=fit/sum(fit);
pscum=cumsum(ps);%size(pscum)
r=rand(1,pop_size);qw=pscum*ones(1,pop_size);
selected=sum(pscum*ones(1,pop_size)<ones(pop_size,1)*r)+1;
newpop=pop(selected,:);
%交叉
if pop_size/2~=0
pop_size=pop_size-1;
end
for i=1:2:pop_size-1
while pc>rand
c_pt=round(8*rand+1);
pop_tp1=newpop(i,:);pop_tp2=newpop(i+1,:);
newpop(i+1,1:c_pt)=pop_tp1(1,1:c_pt);
newpop(i,c_pt+1:chromsome)=pop_tp2(1,c_pt+1:chromsome);
end
end
% 變異
for i=1:pop_size
if pm>rand
m_pt=1+round(9*rand);
newpop(i,m_pt)=40*rand-20;
end
end
3. matlab遺傳演算法工具箱可以自己先賦初值嗎
可以的,你應該在自定義函數中體現。例如:
function y=ga_main(x)
y=max(x)+floor(abs(x(1))/abs(x(2)))
end
然後,在遺傳演算法工具箱中調用。
4. 請問怎麼不使用matlab的遺傳演算法工具箱用matlab遺傳演算法求極值
【1】先看看圖形。
subplot(2,1,1)
ezplot('abs(4*sin(x))*(exp(-0.1*x))')
subplot(2,1,2)
ezplot('abs(4*sin(x))*(exp(-0.1*x))',[-4*pi 2*pi])
圖形見:
http://hi..com/chemical%5Fengineering/album/item/0289d0165bb4ed1c962b43e7.html
【2】用fminbnd函數最簡單,遺傳演算法俺不會啊
fun=inline('-abs(4*sin(x))*(exp(-0.1*x))')%最大值的負數就是最小值
x = fminbnd(fun,0,2*pi) %求最小值
maxval=-subs(fun) %最小值的負數就是最大值
運行結果:
fun =
Inline function:
fun(x) = -abs(4*sin(x))*(exp(-0.1*x))
x = 1.4711
maxval = 3.4357
【3】這個是遺傳演算法,您試試。
http://..com/question/8207100.html?fr=qrl3
5. 格林-巴利綜合征是怎麼引起的
到目前為止GBS的病因仍不十分清楚。最早認為感染與中毒是病因基礎。40%病人有前驅感染。其前驅因素有以下幾種。
1.病毒與GBS:1983年Hurroiz報告1034例GBS,70%有前驅因素。2/3為病毒感染。主要表現為上感或腹瀉。普遍認為巨細胞病毒,EB病毒、流感病毒等與GBS有很大關系。也有報告與肝炎病毒有密切關系。劉秀梅1988年報告4例急性肝炎合並GBS。另外徐賢豪報告乙肝病人中GBS發病6/500,明顯高於對照組。乙肝ABSAg陽性100例中,GBS發病17例,對照組45例中GBS發病1例。
2.空腸彎麴菌與GBS:1982年Rhodes報告1例GBS,經便培養及抗體檢測證實為空腸彎麴菌繼發GBS。1984年又一學者報告56例GBS有空腸彎麴菌感染21例。國內唐健等1993年首先報告空腸彎麴菌感染是我國GBS主要前驅因素。報告17例GBS中71%有胃腸道感染症狀,且血清空腸彎麴菌Igm抗體陽性率53%。均高於國外文獻。目前空腸彎麴菌與GBS的關系已成為GBS研究的焦點。
3.疫苗接種與GBS:一組資料1034例GBS中有4.5%疫苗接種後發病。多見於流感疫苗,肝炎疫苗、麻疹疫苗接種後發病。我科於1995年收治一例甲肝疫苗接種後發病的GBS。
4.遺傳與GBS:有人報告GBS病人A3和B8基因頻率明顯增高,認為GBS與遺傳有一定關系。
5.微量元素與GBS:張祥建等報告GBS病人存在微量元素Zn、Cu、Fe代謝異常。認為微量元素異常,可能在GBS發病中起一定作用。
6. 運行遺基於遺傳演算法的BP神經網路MATLAB代碼程序時總是出錯!!!
請確抄認自己安裝了goat(下載-解壓-File-set path-add with subfolders-save(別忘了襲)-close)。
按照錯誤提示來說是系統無法找到nitializega等函數,說明沒有將函數所在目錄添加進path
7. HIV陰性 RPR陰性<1:1-1:32陰性> FTAABS-IGG陰性 還會得梅毒嗎
梅毒是可以治療的,我同學用了覓淋卿這個葯60天徹底治療了
再看看別人怎麼說的。
8. 遺傳演算法工具箱,用來識別滯回環參數,怎麼擬合結果是直線,這怎麼回事啊
首先將問題抽象成規劃問題的標准形式(如果你不懂什麼是標准形式的版話,建議你權去翻閱運籌學那本書,上告訴你什麼是標准形式),然後用矩陣語言寫出來,最後將矩陣的系數填寫到線性不等約束和線性相等約束中,同時定義所求變數x的上界和下界(記住有多少個變數就有多少列,如果你發現有些條件中沒有出現某些變數,那麼就應該用0補足,這個是matlab解決規劃問題與lingo想比較麻煩的一個地方,)。
9. Antley-Bixler綜合征伴生殖異常與
Antley-Bixler綜合征(Antley-Bixler syndrome,ABS)是一種罕見的兒童骨骼畸形綜合征,部分患者還伴有生殖器畸形、類固醇激素合成障礙等特徵。目前臨床上對於ABS的診斷缺乏確切標准,但是顱縫早閉、面中部發育不全和肘部骨性聯結等症狀是臨床診斷最基本的依據。ABS的發病原因復雜,包括FGFR2基因突變導致的常染色體顯性遺傳和POR基因突變導致的常染色體隱性遺傳等遺傳學病因,以及母親孕期服用高劑量氟康唑葯物等。上呼吸道阻塞所致的呼吸困難往往成為危及ABS患者生命的主要原因之一。本文針對ABS的臨床特徵、病因、鑒別診斷、治療和預防等研究進展作一綜述。
10. 請問遺傳演算法的變異操作的問題
可以使用白雜訊之類的演算法加入噪點
補充:白雜訊是指功率譜密度在整個頻域內均勻分布的雜訊,比較適合在遺傳演算法中處理變異現象。
#include "msp.h"
float randnu(long *iseed)
{
float z;
*iseed=2045*(*iseed)+1;
*iseed=*iseed-(*iseed/1048576)*1048576;
z=(float)((*iseed+1)/1048577.0);
return(z);
}
/*--------------------------------------------------------------------*/
void meavar(float u[],int *n,float *pum,float *puv)
{
int i,k;
*pum=0.0;
for(k=0;k<*n;k++)
*pum=*pum+u[k];
*pum=*pum/(*n);
*puv=0.0;
for(i=0;i<*n;i++)
*puv=*puv+pow((u[i]-*pum),2);
*puv=*puv+pow((u[i]-*pum),2);
*puv=*puv/(*n-1.);
return;
}
/*---------------------------------------------------------------------
Routine mrandom : To generate the random number(pseudo-white noise).
input Parameters:
n : the random data number requested; integer .
iseed: the seed for pseudo-random data generation.it must be
initialized by main program(suggested value is ISEED=12357),
and the random number is cycled,the cycle length=1,048,576
itype: random data distribution type, see below:
itype=1: Uniform distributed,from 0.0 to 1.0
itype=2: Uniform distributed,Mean=0.0, Variance(Power) p=1.0
itype=3: Uniform distributed,Mean=0.0, Variance(Power) p=p.
itype=4: Gaussian distributed,Mean=0.0, Variance(Power) p=1.0
itype=5: Gaussian distributed,Mean=0.0, Variance(Power) p=p.
p :variance(Power) of random, only used when itype=3 or itype=5.
out parameters:
u :n dimensioned real array, data is stored in u(0) to u(n-1).
in Chapter 1
---------------------------------------------------------------------*/
void mrandom(float u[],int *n,long *piseed,int itype,float p)
{
int k,ns,ksection,ks,j;
float a,v,umean,uvari;
float *pum,*puv;
pum=&umean;
puv=&uvari;
if(itype >6 |r |r itype <1)
return;
for(k=0;k<*n;k++)
u[k]=randnu(piseed);
if(itype==2 |r |r itype==3)
{
meavar(u,n,pum,puv);
/* to obtain a zero mean and P-power random sequence u[k]).*/
a=12.;
if(itype==2)
p=sqrt(a);
if(itype==3)
p=sqrt(p*a);
for(k=0;k<*n;k++)
u[k]=(u[k]-umean)*p;
}
if(itype==4 |r |ritype==5)
{
/* to generate the Gaussian randow sequence u[k],k=0,1,2,...,ns-1*/
ksection=12;
ns=*n/ksection;
ks=0;
if (itype==4) p=1;
p=sqrt(p);
for(k=0;k<ns;k++)
{
v=0.0;
for(j=0;j<k;j++)
{ v+=p*(u[j+ks]-.5);
u[k]=v;
ks=ks+ksection;
}
*n=ns;
}
meavar(u,n,pum,puv);
printf(" The mean of u[n]=%f\n",umean);
printf(" The variance of u[n]=%f\n",uvari);
return;
}
其中msp.h頭文件:
#define abs_error 1.e-10
#ifndef _MSP_H_
#define _MSP_H_
typedef struct {float real,imag;} complex;
/*-------------------------------------------------------------------*/
float mabs(complex a)
{
float m;
m=a.real*a.real+a.imag*a.imag;
m=sqrt(m);
return(m);
}
/*-------------------------------------------------------------------*/
float msign(float a,float b)
{
float z;
if(b>=0) z=sqrt(pow(a,2));
else z=-sqrt(pow(a,2));
return(z);
}
/*-------------------------------------------------------------------*/
complex cexp(complex a)
{
complex z;
z.real=exp(a.real)*cos(a.imag);
z.imag=exp(a.real)*sin(a.imag);
return(z);
}
/*-------------------------------------------------------------------*/
void maftodf(float d[],float c[],int ln,int iband,float fln,float fhn,
float b[],float a[],int *ierror);
void mampres(complex h[],float amp[],int n,float fs,int iamp,char filename[]);
void mar1psd(complex a[],int ip,int mfre,float *ep,float ts);
void marburg(complex x[],complex a[],complex ef[],complex eb[],
int n,int ip,float *ep,int *ierror);
void marmach(complex x[],complex ef[],int n,complex a[],
complex b[],int ip,int iq,int m,float *ep,float ts);
void maryuwa(complex x[],complex a[],complex r[],int n,int ip,
float *ep, int *ierror);
void mbiline(float d[],float c[],int ln,float b[],float a[],int *ierror);
void mbutwcf(int l,int k,int ln,float d[],float c[],int *ierror);
void mchebsh(int l,int k,int ln,float d[],float c[],float phi2,
int *ierror);
void mcholsk(complex a[],complex b[],int n,float eps,int *iflag);
void mcmpdft(complex x[],complex y[],int n,int isign);
void mcmpfft(complex x[],int n,int isign);
void mconvo1(float x[],float h[],float y[],int n,int m,int L);
void mconvo2(complex x[],complex h[],complex y[],int n1,int n2,int n);
void mcorpsd(complex x[],complex r[],int n,int lag,int iwindow,float t);
void mcorre1(complex x[],complex y[],complex r[],int n,int lag);
void mcorre2(complex x[],complex y[],int m,int n,int icorre);
void mcztfft(complex x[],int n,int m,int maxnm,float dltomg,
float omg0,float fs,int *ierror);
void mdecint(float x[],float h[],float y[],int nh,int ny,int m,
int l,int *k);
void mdefir1(int l,int iband,float fl,float fh,float fs,int iwindow,
float b[],float w[],int *ierror);
void mdefir2(int l,int iband,float fl,float fh,complex b[],
float trans,float fs,int *ierror);
void mdefir3(int nfilt,int nbands,float edge[],float fx[],
float wtx[],float h[]);
void mdesiir(float *f1,float *f2,float *f3,float *f4,float fs,
float alpha1,float alpha2,int iband,int itype);
void mfirres(float b[],int lb,int n,complex h[]);
void mfitout(float b[],float a[],int lb,int la,float x[],
int n,float y[]);
void miirres(float a[],float b[],int lb,int la,complex h[],int n);
void mlattic(float b[],float a[],int l,float k[],
float c[],int itype ,int *ierror);
void mmayuwa(complex x[],int n,complex a[],int ip,complex b[],int iq,
complex r[],float *ep, float ts,int *ierror);
void mmvseps(complex x[],complex ef[],complex eb[],int n,complex a[],
int ip,int *ierror,float ts);
void morderb(float *f1,float *f2,float *f3,float *f4,float fs,float alpha1,
float alpha2,int *l,int iband,int itype,int *ierror);
void mperpsd(complex x[],int n,int nshift,int nsamp,int iwidow,float ts);
void mphares(complex h[],float phase[],int n,float fs,char filename[]);
void mprgfft(complex x[],int n,int l,int lf,int k1,int isign);
void mpsplot(float psdr[],float psdi[],int mfre,float ts);
float randnu(long *iseed);
void meavar(float u[],int *n,float *pum,float *puv);
void mrandom(float u[],int *n,long *piseed,int ITYPE,float p);
void mrelfft(float xr[],float xi[],int n,int isign);
float d(int k,int n,int m);
float gee(int k,int n);
void mremez1();
void msplfft(complex x[],int n,int isign);
void munwrap(float phase[],int n);
void mwindow(float w[],int n,int iwindow,int *ierror);
int mspbfct(int i1,int i2);
/*-------------------------------------------------------------------*/
#endif