⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ga2darraygenome.cpp

📁 遗传算法工具箱C++
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// $Header$/* ----------------------------------------------------------------------------  array2.C  mbwall 25feb95  Copyright (c) 1995 Massachusetts Institute of Technology                     all rights reserved DESCRIPTION:  Source file for the 2D array genome.---------------------------------------------------------------------------- */#ifndef _ga_array2_C_#define _ga_array2_C_#include <stdio.h>#include <stdlib.h>#include <string.h>#include "garandom.h"#include "GA2DArrayGenome.h"#include "GAMask.h"/* ----------------------------------------------------------------------------2DArrayGenome---------------------------------------------------------------------------- */template <class T> const char *GA2DArrayGenome<T>::className() const {return "GA2DArrayGenome";}template <class T> intGA2DArrayGenome<T>::classID() const {return GAID::ArrayGenome2D;}template <class T> GA2DArrayGenome<T>::GA2DArrayGenome(unsigned int width, unsigned int height, 		GAGenome::Evaluator f,		void * u) :GAArray<T>(width*height),GAGenome(DEFAULT_2DARRAY_INITIALIZER, 	 DEFAULT_2DARRAY_MUTATOR,	 DEFAULT_2DARRAY_COMPARATOR){  evaluator(f);  userData(u);  crossover(DEFAULT_2DARRAY_CROSSOVER);  nx=minX=maxX=width; ny=minY=maxY=height;}template <class T> GA2DArrayGenome<T>::GA2DArrayGenome(const GA2DArrayGenome<T> & orig) : GAArray<T>(orig.sz){  GA2DArrayGenome<T>::copy(orig);}template <class T>GA2DArrayGenome<T>::~GA2DArrayGenome() { }template <class T> voidGA2DArrayGenome<T>::copy(const GAGenome & orig){  if(&orig == this) return;  const GA2DArrayGenome<T>* c = DYN_CAST(const GA2DArrayGenome<T>*, &orig);  if(c) {    GAGenome::copy(*c);    GAArray<T>::copy(*c);    nx = c->nx; ny = c->ny;     minX = c->minX; minY = c->minY;     maxX = c->maxX; maxY = c->maxY;  } }template <class T> GAGenome *GA2DArrayGenome<T>::clone(GAGenome::CloneMethod flag) const {  GA2DArrayGenome<T> *cpy = new GA2DArrayGenome<T>(nx,ny);  if(flag == CONTENTS){    cpy->copy(*this);  }  else{    cpy->GAGenome::copy(*this);    cpy->minX = minX; cpy->minY = minY;     cpy->maxX = maxX; cpy->maxY = maxY;   }  return cpy;}template <class T> intGA2DArrayGenome<T>::resize(int w, int h){  if(w == STA_CAST(int,nx) && h == STA_CAST(int,ny)) return this->sz;  if(w == GAGenome::ANY_SIZE)    w = GARandomInt(minX, maxX);  else if(w < 0)    w = nx;		// do nothing  else if(minX == maxX)    minX=maxX = w;  else{    if(w < STA_CAST(int,minX)) w=minX;    if(w > STA_CAST(int,maxX)) w=maxX;  }  if(h == GAGenome::ANY_SIZE)    h = GARandomInt(minY, maxY);  else if(h < 0)    h = ny;		// do nothing  else if(minY == maxY)    minY=maxY = h;  else{    if(h < STA_CAST(int,minY)) h=minY;    if(h > STA_CAST(int,maxY)) h=maxY;  }  if(w < STA_CAST(int,nx)){    int y=GAMin(STA_CAST(int,ny),h);    for(int j=0; j<y; j++)      GAArray<T>::move(j*w,j*nx,w);  }  GAArray<T>::size(w*h);  if(w > STA_CAST(int,nx)){		// adjust the existing chunks of bits    int y=GAMin(STA_CAST(int,ny),h);    for(int j=y-1; j>=0; j--)      GAArray<T>::move(j*w,j*nx,nx);  }  nx = w; ny = h;  _evaluated = gaFalse;  return this->sz;}#ifdef GALIB_USE_STREAMStemplate <class T> intGA2DArrayGenome<T>::read(STD_ISTREAM &) {  GAErr(GA_LOC, className(), "read", gaErrOpUndef);  return 1;}template <class T> intGA2DArrayGenome<T>::write(STD_OSTREAM & os) const {  for(unsigned int j=0; j<ny; j++){    for(unsigned int i=0; i<nx; i++){      os << gene(i,j) << " ";    }    os << "\n";  }  return 0;}#endiftemplate <class T> intGA2DArrayGenome<T>::resizeBehaviour(GAGenome::Dimension which) const {  int val = 0;  if(which == WIDTH) {    if(maxX == minX) val = FIXED_SIZE;    else val = maxX;  }  else if(which == HEIGHT) {    if(maxY == minY) val = FIXED_SIZE;    else val = maxY;  }  return val;}template <class T> intGA2DArrayGenome<T>::resizeBehaviour(GAGenome::Dimension which, 		unsigned int lower, unsigned int upper){  if(upper < lower){    GAErr(GA_LOC, className(), "resizeBehaviour", gaErrBadResizeBehaviour);    return resizeBehaviour(which);  }  switch(which){  case WIDTH:    minX = lower; maxX = upper;    if(nx > upper) GA2DArrayGenome<T>::resize(upper,ny);    if(nx < lower) GA2DArrayGenome<T>::resize(lower,ny);    break;  case HEIGHT:    minY = lower; maxY = upper;    if(ny > upper) GA2DArrayGenome<T>::resize(nx,upper);    if(ny < lower) GA2DArrayGenome<T>::resize(nx,lower);    break;  default:    break;  }  return resizeBehaviour(which);}template <class T> voidGA2DArrayGenome<T>::copy(const GA2DArrayGenome<T> & orig,			 unsigned int r, unsigned int s,			 unsigned int x, unsigned int y,			 unsigned int w, unsigned int h){  if(w == 0 || x >= orig.nx || r >= nx ||     h == 0 || y >= orig.ny || s >= ny) return;  if(x + w > orig.nx) w = orig.nx - x;  if(y + h > orig.ny) h = orig.ny - y;  if(r + w > nx) w = nx - r;  if(s + h > ny) h = ny - s;  for(unsigned int j=0; j<h; j++)    GAArray<T>::copy(orig, (s+j)*nx+r, (y+j)*orig.nx+x, w);  _evaluated = gaFalse; }template <class T> int GA2DArrayGenome<T>::equal(const GAGenome & c) const{  if(this == &c) return 1;  GA2DArrayGenome<T> & b = (GA2DArrayGenome<T> &)c;  if(nx != b.nx || ny != b.ny) return 0;  int val=0;  for(unsigned int j=0; j<ny && val==0; j++)    val = GAArray<T>::equal(b,j*nx,j*nx,nx) ? 0 : 1;  return(val ? 0 : 1);}/* ----------------------------------------------------------------------------2DArrayAlleleGenome---------------------------------------------------------------------------- */template <class T> const char *GA2DArrayAlleleGenome<T>::className() const {return "GA2DArrayAlleleGenome";}template <class T> intGA2DArrayAlleleGenome<T>::classID() const {return GAID::ArrayAlleleGenome2D;}template <class T> GA2DArrayAlleleGenome<T>::GA2DArrayAlleleGenome(unsigned int width, unsigned int height, 		      const GAAlleleSet<T> & s,		      GAGenome::Evaluator f, void * u) :GA2DArrayGenome<T>(width,height,f,u){  naset = 1;  aset = new GAAlleleSet<T>[1];  aset[0] = s;  initializer(GA2DArrayAlleleGenome<T>::DEFAULT_2DARRAY_ALLELE_INITIALIZER);  mutator(GA2DArrayAlleleGenome<T>::DEFAULT_2DARRAY_ALLELE_MUTATOR);  comparator(GA2DArrayAlleleGenome<T>::DEFAULT_2DARRAY_ALLELE_COMPARATOR);  crossover(GA2DArrayAlleleGenome<T>::DEFAULT_2DARRAY_ALLELE_CROSSOVER);}template <class T> GA2DArrayAlleleGenome<T>::GA2DArrayAlleleGenome(unsigned int width, unsigned int height, 		      const GAAlleleSetArray<T> & sa,		      GAGenome::Evaluator f, void * u) :GA2DArrayGenome<T>(width,height, f, u) {  naset = sa.size();  aset = new GAAlleleSet<T>[naset];  for(int i=0; i<naset; i++)    aset[i] = sa.set(i);  initializer(GA2DArrayAlleleGenome<T>::DEFAULT_2DARRAY_ALLELE_INITIALIZER);  mutator(GA2DArrayAlleleGenome<T>::DEFAULT_2DARRAY_ALLELE_MUTATOR);  comparator(GA2DArrayAlleleGenome<T>::DEFAULT_2DARRAY_ALLELE_COMPARATOR);  crossover(GA2DArrayAlleleGenome<T>::DEFAULT_2DARRAY_ALLELE_CROSSOVER);}template <class T> GA2DArrayAlleleGenome<T>::GA2DArrayAlleleGenome(const GA2DArrayAlleleGenome<T> & orig) : GA2DArrayGenome<T>(orig.nx, orig.ny) {  naset = 0;  aset = (GAAlleleSet<T>*)0;  GA2DArrayAlleleGenome<T>::copy(orig);}template <class T>GA2DArrayAlleleGenome<T>::~GA2DArrayAlleleGenome(){  delete [] aset;}template <class T> GAGenome *GA2DArrayAlleleGenome<T>::clone(GAGenome::CloneMethod) const {  return new GA2DArrayAlleleGenome<T>(*this);}template <class T> void GA2DArrayAlleleGenome<T>::copy(const GAGenome& orig){  if(&orig == this) return;  const GA2DArrayAlleleGenome<T>* c =     DYN_CAST(const GA2DArrayAlleleGenome<T>*, &orig);  if(c) {    GA2DArrayGenome<T>::copy(*c);    if(naset != c->naset){      delete [] aset;      naset = c->naset;      aset = new GAAlleleSet<T>[c->naset];    }    for(int i=0; i<naset; i++)      aset[i].link(c->aset[i]);  }}template <class T> intGA2DArrayAlleleGenome<T>::resize(int x, int y){  unsigned int oldx = this->nx;  unsigned int oldy = this->ny;  GA2DArrayGenome<T>::resize(x,y);  if(this->nx > oldx){		// adjust the existing chunks of bits    int y=GAMin(oldy,this->ny);    for(int j=y-1; j>=0; j--){      for(unsigned int i=oldx; i<this->nx; i++)	this->a[j * this->nx+i] = aset[(j * this->nx+i) % naset].allele();    }  }  if(this->ny > oldy){		// change in height is always new bits    for(unsigned int i=this->nx*oldy; i<this->nx * this->ny; i++)      this->a[i] = aset[i % naset].allele();  }  return this->sz;}#ifdef GALIB_USE_STREAMStemplate <class T> intGA2DArrayAlleleGenome<T>::read(STD_ISTREAM& is){  return GA2DArrayGenome<T>::read(is);}template <class T> intGA2DArrayAlleleGenome<T>::write(STD_OSTREAM& os) const {  return GA2DArrayGenome<T>::write(os);}#endiftemplate <class T> intGA2DArrayAlleleGenome<T>::equal(const GAGenome & c) const {  return GA2DArrayGenome<T>::equal(c);}/* ----------------------------------------------------------------------------   Operator definitions---------------------------------------------------------------------------- */// this does not handle genomes with multiple allele sets!template <class ARRAY_TYPE> void GA2DArrayAlleleGenome<ARRAY_TYPE>::UniformInitializer(GAGenome & c){  GA2DArrayAlleleGenome<ARRAY_TYPE> &child=    DYN_CAST(GA2DArrayAlleleGenome<ARRAY_TYPE> &, c);  child.resize(GAGenome::ANY_SIZE,GAGenome::ANY_SIZE);   for(int i=child.width()-1; i>=0; i--)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -