📄 mfgaview.cpp
字号:
// MFGAView.cpp : implementation of the CMFGAView class
//
#include "stdafx.h"
#include "MFGA.h"
#include "MFGADoc.h"
#include "MFGAView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMFGAView
IMPLEMENT_DYNCREATE(CMFGAView,CScrollView)
BEGIN_MESSAGE_MAP(CMFGAView, CScrollView)
//{{AFX_MSG_MAP(CMFGAView)
ON_COMMAND(ID_SGA_DRAW, OnSgaDraw)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMFGAView construction/destruction
CMFGAView::CMFGAView()
{
// TODO: add construction code here
x1=700;
y1=-1300;
x2=700;
y2=-2800;
PdsBtEn=0;
maxruns=1;
popsize=250;
if((popsize%2)!=0)
popsize++;
lchrom=20;
maxgen=2000;
pcross=0.8;
pmutation=0.1;
randomseed=0.4;
N=4;
wd=22;
chromsize=(lchrom/(8*sizeof(unsigned)));
if(lchrom%(8*sizeof(unsigned))) chromsize++;
}
CMFGAView::~CMFGAView()
{
}
BOOL CMFGAView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMFGAView drawing
void CMFGAView::OnDraw(CDC* pDC)
{
CMFGADoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//使点距为0.1毫米
pDC->SetMapMode(MM_LOMETRIC);
CPen pen1,pen2;
//创建画笔
pen1.CreatePen(PS_SOLID,6,RGB(0,0,0));
pen2.CreatePen(PS_SOLID,2,RGB(0,0,255));
CPen *pOldPen;
//选入画笔pen1
pOldPen=pDC->SelectObject(&pen1);
SetXYAxis(pDC,x1,y1);
SetXYAxis(pDC,x2,y2);
//选入画笔pen2
pDC->SelectObject(&pen2);
//显示图
pDC->TextOut(700,-50,"CGA Optimizer");
pDC->TextOut(1000,-50,"s11");
pDC->TextOut(1000,-1500,"s21");
if(PdsBtEn)
{
Display(pDC,ps1,x1,y1);
Display(pDC,ps2,x2,y2);
}
}
/////////////////////////////////////////////////////////////////////////////
// CMFGAView printing
BOOL CMFGAView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMFGAView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMFGAView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMFGAView diagnostics
#ifdef _DEBUG
void CMFGAView::AssertValid() const
{
CScrollView::AssertValid();
}
void CMFGAView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CMFGADoc* CMFGAView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFGADoc)));
return (CMFGADoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMFGAView message handlers
void CMFGAView::advance_random()
{
int j1;
double new_random;
for(j1=0;j1<24;j1++)
{
new_random=oldrand[j1]-oldrand[j1+31];
if(new_random<0.0) new_random=new_random+1.0;
oldrand[j1]=new_random;
}
for(j1=24;j1<55;j1++)
{
new_random=oldrand[j1]-oldrand[j1-24];
if(new_random<0.0) new_random=new_random+1.0;
oldrand[j1]=new_random;
}
}
void CMFGAView::initpop()
{
int i,j,j1,k,stop;
unsigned mask=1;
for(j=0;j<popsize;j++)
{
for(i=0;i<N*N+1;i++)
{
for(k=0;k<chromsize;k++)
{
oldpop[j].chrom[i][k]=0;
if(k==(chromsize-1))
stop=lchrom-(k*(8*sizeof(unsigned)));
else
stop=8*sizeof(unsigned);
for(j1=1;j1<=stop;j1++)
{
oldpop[j].chrom[i][k]=oldpop[j].chrom[i][k]<<1;
if(flip(0.5))
oldpop[j].chrom[i][k]=oldpop[j].chrom[i][k]|mask;
}
}
oldpop[j].xsite[i]=0;
}
oldpop[j].parent[0]=0; /*initialize parent individul information*/
oldpop[j].parent[1]=0;
objfunc(&(oldpop[j])); /*compute initialized fitness*/
}
}
void CMFGAView::generation()
{
int mate1,mate2,j,k;
int *jcross;
jcross=new int [N*N+1];
/*preelection before every generation operated*/
preselect();
/*selection crossover mutation*/
for(j=0;j<popsize;j+=2)
{
/*selection crossover partnership*/
mate1=select();
mate2=select();
/*crossover mutation*/
crossover(oldpop[mate1].chrom,oldpop[mate2].chrom,newpop[j].chrom,newpop[j+1].chrom,jcross);
mutation(newpop[j].chrom);
mutation(newpop[j+1].chrom);
/*decoding,compute fitness*/
objfunc(&(newpop[j]));
for(k=0;k<N*N+1;k++)
newpop[j].xsite[k]=jcross[k];
objfunc(&(newpop[j+1]));
for(k=0;k<N*N+1;k++)
newpop[j+1].xsite[k]=jcross[k];
newpop[j].parent[0]=mate1+1;
newpop[j].parent[1]=mate2+1;
newpop[j+1].parent[0]=mate1+1;
newpop[j+1].parent[1]=mate2+1;
}
delete [] jcross;
jcross=NULL;
}
void CMFGAView::initmalloc()
{
int j,i;
/*diatribute memory space to current and new population*/
oldpop=new individual [popsize];
newpop=new individual [popsize];
/*distribute memory space for chromosome*/
for(j=0;j<popsize;j++)
{
oldpop[j].varible=new double [N*N+1];
if(oldpop[j].varible==NULL) AfxMessageBox("OK");
newpop[j].varible=new double [N*N+1];
if(newpop[j].varible==NULL) AfxMessageBox("OK");
oldpop[j].xsite=new int [N*N+1];
if(oldpop[j].xsite==NULL) AfxMessageBox("OK");
newpop[j].xsite=new int [N*N+1];
if(newpop[j].xsite==NULL) AfxMessageBox("OK");
oldpop[j].chrom = new unsigned *[N*N+1];
for(i=0;i<N*N+1;i++)
oldpop[j].chrom[i] = new unsigned [lchrom];
newpop[j].chrom = new unsigned *[N*N+1];
for(i=0;i<N*N+1;i++)
newpop[j].chrom[i] = new unsigned [lchrom];
}
bestfit.chrom = new unsigned *[N*N+1];
for(i=0;i<N*N+1;i++)
bestfit.chrom[i] = new unsigned [lchrom];
bestfit.varible=new double [N*N+1];
bestone.chrom = new unsigned *[N*N+1];
for(i=0;i<N*N+1;i++)
bestone.chrom[i] = new unsigned [lchrom];
}
void CMFGAView::freeall()
{
int i,j;
for(i=0;i<popsize;i++)
{
delete [] oldpop[i].varible;
oldpop[i].varible=NULL;
delete [] newpop[i].varible;
newpop[i].varible=NULL;
delete [] oldpop[i].xsite;
oldpop[i].xsite=NULL;
delete [] newpop[i].xsite;
newpop[i].xsite=NULL;
for(j=0;j<N*N+1;j++)
{
delete [] oldpop[i].chrom[j];
oldpop[i].chrom[j]=NULL;
delete [] newpop[i].chrom[j];
newpop[i].chrom[j]=NULL;
}
delete [] oldpop[i].chrom;
oldpop[i].chrom=NULL;
delete [] newpop[i].chrom;
newpop[i].chrom=NULL;
}
for(j=0;j<N*N+1;j++)
{
delete [] bestfit.chrom[j];
bestfit.chrom[j]=NULL;
}
delete [] oldpop;
oldpop=NULL;
delete [] newpop;
newpop=NULL;
delete [] bestfit.chrom;
bestfit.chrom=NULL;
delete [] bestfit.varible;
bestfit.varible=NULL;
delete [] bestone.chrom;
bestone.chrom=NULL;
}
void CMFGAView::preselect()
{
int j;
sumfitness=0;
for(j=0;j<popsize;j++) sumfitness+=oldpop[j].fitness;
}
void CMFGAView::statistics(individual *pop)
{
int i,j,k;
sumfitness=0.0;
min=pop[0].fitness;
max=pop[0].fitness;
/*compute max,min,accumulative fitness*/
for(j=0;j<popsize;j++)
{
sumfitness=sumfitness+pop[j].fitness;
if(pop[j].fitness>max) max=pop[j].fitness;
if(pop[j].fitness<min) min=pop[j].fitness;
/* new global best-fit individual */
if(pop[j].fitness>bestfit.fitness)
{
for(k=0;k<N*N+1;k++)
{
for(i=0;i<chromsize;i++)
bestfit.chrom[k][i]=pop[j].chrom[k][i];
bestfit.varible[k]=pop[j].varible[k];
}
bestfit.fitness=pop[j].fitness;
bestfit.generation=gen+1;
}
}
/*compute average fitness*/
avg=sumfitness/popsize;
}
void CMFGAView::varible(individual *critter) /*compute fitness function value*/
{
unsigned mask=1;
unsigned bitpos;
unsigned tp;
double bitpow;
int i,j,k,stop;
for(i=0;i<N*N+1;i++)
{
critter->varible[i]=0.0;
for(k=0;k<chromsize;k++)
{
if(k==(chromsize-1))
stop=lchrom-(k*(8*sizeof(unsigned)));
else
stop=8*sizeof(unsigned);
tp=critter->chrom[i][k];
for(j=0;j<stop;j++)
{
bitpos=j+(8*sizeof(unsigned))*k;
if((tp&mask)==1)
{
bitpow=pow(2.0,(double)bitpos);
critter->varible[i]=critter->varible[i]+bitpow;
}
tp=tp>>1;
}
}
//double xmax[17]={0.15,1.1,0,0.3,1.1,0.0,0.6,0.9,0,0.6,-0.5,0.9,0.3,0.9,0.9,0.15,1.5};
//double xmin[17]={0.0,0.8,0,0.1,0.8,-0.1,0.2,0.4,0,0.2,-1.0,0.5,0.1,0.4,0.5,0.0,1.0};
switch(i)
{
case(0):critter->varible[i]=0.0+critter->varible[i]*0.15/(pow(2.0,(double)lchrom)-1);break;
case(1):critter->varible[i]=0.8+critter->varible[i]*0.3/(pow(2.0,(double)lchrom)-1);break;
case(3):critter->varible[i]=0.1+critter->varible[i]*0.2/(pow(2.0,(double)lchrom)-1);break;
case(4):critter->varible[i]=0.8+critter->varible[i]*0.3/(pow(2.0,(double)lchrom)-1);break;
case(5):critter->varible[i]=-0.1+critter->varible[i]*0.1/(pow(2.0,(double)lchrom)-1);break;
case(6):critter->varible[i]=0.2+critter->varible[i]*0.4/(pow(2.0,(double)lchrom)-1);break;
case(7):critter->varible[i]=0.4+critter->varible[i]*0.5/(pow(2.0,(double)lchrom)-1);break;
case(9):critter->varible[i]=0.2+critter->varible[i]*0.4/(pow(2.0,(double)lchrom)-1);break;
case(10):critter->varible[i]=-1+critter->varible[i]*0.5/(pow(2.0,(double)lchrom)-1);break;
case(11):critter->varible[i]=0.5+critter->varible[i]*0.4/(pow(2.0,(double)lchrom)-1);break;
case(12):critter->varible[i]=0.1+critter->varible[i]*0.2/(pow(2.0,(double)lchrom)-1);break;
case(13):critter->varible[i]=0.4+critter->varible[i]*0.5/(pow(2.0,(double)lchrom)-1);break;
case(14):critter->varible[i]=0.5+critter->varible[i]*0.4/(pow(2.0,(double)lchrom)-1);break;
case(15):critter->varible[i]=0.0+critter->varible[i]*0.15/(pow(2.0,(double)lchrom)-1);break;
case(16):critter->varible[i]=1+critter->varible[i]*0.5/(pow(2.0,(double)lchrom)-1);break;
default:critter->varible[i]=0;break;
}
}
}
void CMFGAView::Zinversion(class individual *crit1,complex<double> *cmatZ1,complex<double> *cmatZ2,complex<double> *ca,complex<double> *cb,double *rr)
{
int k=0;
int n;
int i,j;
double *mm;
mm=new double [N*N];
//static double wz[4]={-0.9027,-0.2415,0.5171,0.9447};
//static double wp[2]={2.6791,4.0904};
static double wz[4]={-0.8743,-0.1122,0.6262,0.9625};
static double wp[2]={1.5267,2.2685};
matrix< complex<double> > cmatZ(N,N);
for( i=0;i<N;i++)
{
for( n=0;n<N;n++)
{
cmatZ(i,n)=complex<double> (0,0);
}
}
varible(crit1);
for(j=0;j<N*N;j++)
mm[j]=crit1->varible[j];
*rr=crit1->varible[N*N];
matrix< complex<double> > matM(N,N);
for( i=0;i<N;i++)
{
for( n=0;n<N;n++)
{
matM(i,n)=complex<double>(mm[k],0);
k++;
}
}
matrix< complex<double> > cmatR(N,N);
for(i=0;i<N;i++)
{
for(n=0;n<N;n++)
{
if((i==0)&&(n==0))
cmatR(i,n)=complex<double> (0,-*rr);
else if((i==N-1)&&(n==N-1))
cmatR(i,n)=complex<double> (0,-*rr);
else
cmatR(i,n)=complex<double> (0,0);
}
}
matrix< complex<double> > matU(N,N);
for(i=0;i<N;i++)
{
for(n=0;n<N;n++)
{
if(i==n)
matU(i,n)=complex<double> (1,0);
else
matU(i,n)=complex<double> (0,0);
}
}
for(i=0;i<N;i++)
{
cmatZ=wz[i]*matU-(-cmatR)-(-matM);
MatrixInversionGS(cmatZ);
cmatZ1[i]=cmatZ(0,0);
}
for(i=0;i<N-2;i++)
{
cmatZ=wp[i]*matU-(-cmatR)-(-matM);
MatrixInversionGS(cmatZ);
cmatZ2[i]=cmatZ(N-1,0);
}
cmatZ=matU-(-cmatR)-(-matM);
MatrixInversionGS(cmatZ);
*ca=cmatZ(0,0);
cmatZ=-matU-(-cmatR)-(-matM);
MatrixInversionGS(cmatZ);
*cb=cmatZ(0,0);
delete [] mm;
mm=NULL;
}
void CMFGAView::parameters(individual *crit2,double *s1,double *s2,double *s3,double *s4)
{
int i;
complex<double> *cA1,*cA2;
cA1=new complex<double> [N];
cA2=new complex<double> [N-2];
complex<double> cas;
complex<double> cA3;
complex<double> cA4;
double r;
*s1=0;*s2=0;*s3=0;*s4=0;
Zinversion(crit2,cA1,cA2,&cA3,&cA4,&r);
complex<double> ss2(0,-2*r);
complex<double> ss1(0,2*r);
complex<double> cA(1,0);
for(i=0;i<N;i++)
{
cas=cA+ss1*cA1[i];
*s1+=norm(cas);
}
for(i=0;i<N-2;i++)
*s2+=norm(ss2*cA2[i]);
*s3=abs(cA+cA3);
*s4=abs(cA+cA4);
delete [] cA1;
cA1=NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -