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