量子算法QGA 优化 函数 c++语言 绝对原创.doc_第1页
量子算法QGA 优化 函数 c++语言 绝对原创.doc_第2页
量子算法QGA 优化 函数 c++语言 绝对原创.doc_第3页
量子算法QGA 优化 函数 c++语言 绝对原创.doc_第4页
量子算法QGA 优化 函数 c++语言 绝对原创.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

ALL RIGHTS RESERVED BY inevernuts (baidu. kongjian) JUN_K*/*量子遗传算法*cpp*/#include #include faiheader.husing namespace std;#include void main()int nm=14;int mn=2;double * test=INITIAL_Ofai(nm,mn);double * stest=INITIAL_Ofai(nm,mn);CHSC* p=FAI2Ch(nm,mn,test,stest);CH_X * pch_x=XCalculate(nm,mn,p);Pnum_MaxV pnm;pnm=FindBestFit(pch_x,nm,mn);coutBest pnm.best_p pnm.cos_in pnm.maxendl;int Maxiter=550;for(int iter=0;iterMaxiter;iter+)Evolvo(nm,mn,test,stest,p,pnm); p=FAI2Ch(nm,mn,test,stest); pch_x=XCalculate(nm,mn,p); pnm=FindBestFit(pch_x,nm,mn); Mutation(nm,mn,test,stest,pnm,0.12);p=FAI2Ch(nm,mn,test,stest);pch_x=XCalculate(nm,mn,p);pnm=FindBestFit(pch_x,nm,mn);if (iter%100)=0)coutiter 500Best pnm.best_p pnm.cos_in pnm.maxendl;for(int n=0;n6;n+)/out: the chromXcout(*(pch_x+n).x1 (*(pch_x+n).x2 (*(pch_x+n).x3endl;/(*pch_x)+1)for(int i(0);i3;i+)double& x2=(*(pch_x+i).x2;/ print x2coutx2endl;cout(*(p+1+1).Ch_ss1 (*(p+1).Ch_ss1 (*(p+3).Ch_ss1 endl;/sin cos for(int tem=0;temmn;tem+)coutrand() rand()endl;cout*madendl;int* ad=*mad; ad=fun(/*mad12,*/ad);Chrom_x f1(pi/4,0.314159);f1.Calculate();f1.SHOW();/f1.FAI2Ch();f1.SHOW();/*class shape public: shape(); shape(); virtual char* getclassname(); int getnumberofshapes(); private: static int numberofshapes; ;int shape:numberofshapes = 0; shape:shape() numberofshapes+; shape:shape() numberofshapes-; shape:getnumberofshapes() return numberofshapes; char* shape:getclassname() return NULL; class circle : public shape public: / same stuff goes here as in original circle/ add some calls here circle(const double* cert,double radius)coutthist center getnumberofshapes()-1 radius radiusendl;char* getclassname(); ;/ this member defines the virtual member of shape char* circle:getclassname() return (char *)this; void print( shape &s) cout s.getclassname() endl; void print( shape *s) cout getclassname() endl; void main() int a,b,ab;double d_ans;a=93;b=99;ab=90;d_ans=static_cast(a-ab)/(b-ab);const double center2 = 5.0, 3.14159 ;circle * mycircle; / define a circle pointer mycircle = new circle( center, 5.123); / build itcout getnumberofshapes() endl; print(mycircle); circle yourcircle(center, 1.111); cout yourcircle.getnumberofshapes() endl; char* g= yourcircle.getclassname();print(yourcircle); */*.h*#include #include math.h#include stdlib.h#include using namespace std;#define SIZE 20#define MAX 100#define MIN -100/*-CLASStruct:O_fai-*/*-faisita-*/_ _ SEE THE MATLAB BQGA warning the bloch qga()/*FUNCTION:O_fai()-origanal-*/*class*/struct O_fai/public: double fai ; double sita; /O_fai();/*O_fai:O_fai()fai=11;/rand();sita=31;/rand();*/*-CLASS:Chrom_f-*/*CLASS O_fai F,Chrom_ss1,Chrom_sc2,Chrom_c3,*/*FUNCTION:FAI2Ch(),SHOW()*/class Chrom_f/: public O_faipublic:O_fai F; double Chrom_ss1; double Chrom_sc2; double Chrom_c3 ;/*Chrom_f&*/ void FAI2Ch();void SHOW(); Chrom_f(double s, double c); Chrom_f();/*Chrom_f &*/void Chrom_f:FAI2Ch()this-Chrom_ss1 = sin(this-F.fai)*cos(this-F.sita);/cos this-Chrom_sc2 = sin(this-F.fai)*sin(this-F.sita);/sinsin this-Chrom_c3 = cos(this-F.sita);/cos /*return *this;*/Chrom_f:Chrom_f() Chrom_f:Chrom_f(double s, double c) this-F.fai=s;this-F.sita=c;coutF.fai=s;sF.sita=c;c/*Chrom_sc2Chrom_c3*/endl;void Chrom_f:SHOW()coutss1Chrom_ss1Chrom_sc2Chrom_sc2Chrom_sc2Chrom_c3F.fai=s; this-F.sita=c;void Chrom_x:Calculate()this-FAI2Ch();x1=MAX*(1-this-Chrom_ss1)+MIN*(1+this-Chrom_ss1);/Ch_f.x2=MAX*(1-this-Chrom_sc2)+MIN*(1+this-Chrom_sc2);x3=MAX*(1-this-Chrom_c3)+MIN*(1+this-Chrom_c3);x4=MAX*(1-this-Chrom_sc2)+MIN*(1+this-Chrom_sc2);void Chrom_x:FITNESS()void Chrom_x:SHOW()coutx1: x1 x2: x2endl;coutx3: x3 x4: x4SORT();coutafter sort() test: testendl;/ test: testx1 this-x2 ? this-x2 : this-x1);return temp;void Chrom_x:EVOLUTION()double *f;/*初始化角度*/double* INITIAL_Ofai(int nc,int mr)/mr: varial 变量f=(double *)malloc(sizeof(double)*nc*mr);for(int i_c=0; i_cnc; i_c+) for(int i_r=0; i_rmr; i_r+) / char_randdn 1;/rand()/RAND_MAX;*(f+i_c*mr+i_r)=(double)rand()/RAND_MAX;/set in the sequenced order coutfai i_c i_r *(f+i_c*mr+i_r)endl;return f;struct CHSCdouble Ch_ss1;double Ch_sc2;double Ch_c3;CHSC* pCHSC;/100;/teydef/*相位角度变cosin 行数 列数 相1 相2返回 p 编码1*/CHSC* FAI2Ch(const int& nc,const int& mr,const double* fai,const double* sita)pCHSC=(CHSC *)malloc(sizeof(CHSC)*nc*mr);/*=new (CHSC );fif(!pCHSC) return;*/for(int i_c=0;i_cnc; i_c+)/pCHSC=new CHSC mr; i_cfor(int i_r=0; i_rmr; i_r+) (*(pCHSC+i_c*mr+i_r).Ch_ss1=sin(*(fai+i_c*mr+i_r) * sin(*(sita+i_c*mr+i_r); (*(pCHSC+i_c*mr+i_r).Ch_sc2=sin(*(fai+i_c*mr+i_r) * cos(*(sita+i_c*mr+i_r); (*(pCHSC+i_c*mr+i_r).Ch_c3=cos(*(sita+i_c*mr+i_r); return pCHSC;struct CH_Xdouble x1;double x2;double x3;/对应3条基因链/*解码2相位(cosin) 行数 列数 编码1*/CH_X * XCalculate(const int nc,const int mr,CHSC *pCHSC)CH_X * pXCH; pXCH=(CH_X *)malloc(sizeof(CH_X)*nc*mr);/02 for(int i_c=0;i_cnc; i_c+)/03 for(int i_r=0; i_rmr; i_r+)double& Ch_ss1=(*(pCHSC+i_c*mr+i_r).Ch_ss1;double& Ch_sc2=(*(pCHSC+i_c*mr+i_r).Ch_sc2;double& Ch_c3 =(*(pCHSC+i_c*mr+i_r).Ch_c3;double& x1=(*(pXCH+i_c*mr+i_r).x1;double& x2=(*(pXCH+i_c*mr+i_r).x2;double& x3=(*(pXCH+i_c*mr+i_r).x3;/double& x4=(*(pXCH+i_c*mr+i_r).x4;x1=MAX*(1-Ch_ss1)+MIN*(1+Ch_ss1);/x2=MAX*(1-Ch_sc2)+MIN*(1+Ch_sc2);x3=MAX*(1-Ch_c3)+MIN*(1+Ch_c3); /02return (pXCH);/ttp;/*适应度函数定义*/inline double fit(double * p,int mr)/double *:double total(0);/for(int y=0;ymr;y+)/total+=*(p+ y);double t1(0),t2(0),t;t1=*(p);t2=*(p+(mr-1);/注意指针的步进,为数组下标t=t1*t1+t2*t2;t1=sin(sqrt(t);total=0.5-(t1*t1-0.5)/(1+0.00*(t*t); /%return total;struct Pnum_MaxVint cos_in;/enum (1,2,3)int best_p;/数组下标形式double max;/*-求种群中适应函数的最大 返回此种群最优个体的标号,最优质*/struct Pnum_MaxV FindBestFit(CH_X * pCx, int nc,int mr)double f1,f2,f3;double maxium(0);int order(0);int best_p(0);Pnum_MaxV cm,bm;bm.best_p=0;bm.cos_in=0;bm.max=0;double* p_d=new doublemr;/for()for(int i_c=0;i_cnc; i_c+)/ for(int i_r=0; i_rmaxium)maxium=f1;best_p=i_c;maxium=0;for(i_r=0; i_rmaxium)maxium=f2;maxium=0;for( i_r=0; i_rmaxium)maxium=f3;/coutf1 f1 f2 f2 f3 f3=f2) ? 1 : 2;if(f1=f2)maxium=f1;elsemaxium=f2;order= (maxiumf3) ? order : 3;maxium= (maxiumf3) ? maxium : f3;cm.max=maxium;cm.cos_in=order;cm.best_p=i_c;if(bm.maxcm.max);else bm.best_p=cm.best_p;bm.cos_in=cm.cos_in;bm.max=cm.max;delete p_d;return bm;/2012*03*23not OK/*-QUANTUM GATE步长evol_stepnc:populationmr:varial numberbest:*/bool booll=0;const double evol_step=pi*0.05;void Evolvo(int nc,int mr,double* p_fai,double* p_sita,const CHSC* p_cosin,const Pnum_MaxV best/*CHSC* p_best_cosin*/)double p1(0),p2(0),p3(0);double b_p1(0);double b_p2(0);double b_p3(0);int best_p=best.best_p;for(int i_c=0; i_cnc; i_c+) for(int i_r=0; i_r b_p1*p2) if(booll=0) cout*(p_fai+i_c*mr+i_r)endl; *(p_fai+i_c*mr+i_r)+=evol_step; if(booll=0) cout

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论