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

📄 ga3dbinstrgenome.cpp

📁 遗传算法工具箱C++
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// $Header$/* ----------------------------------------------------------------------------  binstr3.C  mbwall 19apr95  Copyright (c) 1995 Massachusetts Institute of Technology                     all rights reserved DESCRIPTION:  Source file for the 3D binary string genome.  See the 1D genome for comments.---------------------------------------------------------------------------- */#include <stdio.h>#include <stdlib.h>#include <ctype.h>#include <string.h>#include "gaerror.h"#include "garandom.h"#include "GA3DBinStrGenome.h"#include "GAMask.h"/* ----------------------------------------------------------------------------   Genome class definition---------------------------------------------------------------------------- */GA3DBinaryStringGenome::GA3DBinaryStringGenome(unsigned int width,		       unsigned int height,		       unsigned int depth,		       GAGenome::Evaluator f, void * u) :GABinaryString(width*height*depth),GAGenome(DEFAULT_3DBINSTR_INITIALIZER,	 DEFAULT_3DBINSTR_MUTATOR,	 DEFAULT_3DBINSTR_COMPARATOR) {  evaluator(f);  userData(u);  crossover(DEFAULT_3DBINSTR_CROSSOVER);  nx=minX=maxX=0; ny=minY=maxY=0; nz=minZ=maxZ=0;  resize(width, height, depth);}GA3DBinaryStringGenome::GA3DBinaryStringGenome(const GA3DBinaryStringGenome & orig) :GABinaryString(orig.GABinaryString::size()),GAGenome() {  nx=minX=maxX=0; ny=minY=maxY=0; nz=minZ=maxZ=0;  GA3DBinaryStringGenome::copy(orig);}GA3DBinaryStringGenome::~GA3DBinaryStringGenome() { }GAGenome *GA3DBinaryStringGenome::clone(GAGenome::CloneMethod flag) const {  GA3DBinaryStringGenome *cpy = new GA3DBinaryStringGenome(nx,ny,nz);  if(flag == CONTENTS){    cpy->copy(*this);  }  else{    cpy->GAGenome::copy(*this);    cpy->minX = minX; cpy->minY = minY; cpy->minZ = minZ;     cpy->maxX = maxX; cpy->maxY = maxY; cpy->maxZ = maxZ;  }  return cpy;}voidGA3DBinaryStringGenome::copy(const GAGenome & orig){  if(&orig == this) return;  const GA3DBinaryStringGenome* c =     DYN_CAST(const GA3DBinaryStringGenome*, &orig);  if(c) {    GAGenome::copy(*c);    GABinaryString::copy(*c);    nx = c->nx; ny = c->ny; nz = c->nz;    minX = c->minX; minY = c->minY; minZ = c->minZ;    maxX = c->maxX; maxY = c->maxY; maxZ = c->maxZ;  }}intGA3DBinaryStringGenome::resize(int w, int h, int d){  if(w == STA_CAST(int,nx) && h == STA_CAST(int,ny) && d == STA_CAST(int,nz))    return 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(d == GAGenome::ANY_SIZE)    d = GARandomInt(minZ, maxZ);  else if(d < 0)    d = nz;		// do nothing  else if(minZ == maxZ)    minZ=maxZ = d;  else{    if(d < STA_CAST(int,minZ)) d=minZ;    if(d > STA_CAST(int,maxZ)) d=maxZ;  }  if(w < STA_CAST(int,nx) && h < STA_CAST(int,ny)){    int z=GAMin(STA_CAST(int,nz),d);    for(int k=0; k<z; k++)      for(int j=0; j<h; j++)	GABinaryString::move(k*h*w+j*w,k*ny*nx+j*nx,w);  }  else if(w < STA_CAST(int,nx)){    int z=GAMin(STA_CAST(int,nz),d);    for(int k=0; k<z; k++)      for(int j=0; j<STA_CAST(int,ny); j++)	GABinaryString::move(k*ny*w+j*w,k*ny*nx+j*nx,w);  }  else if(h < STA_CAST(int,ny)){    int z=GAMin(STA_CAST(int,nz),d);    for(int k=0; k<z; k++)      for(int j=0; j<h; j++)	GABinaryString::move(k*h*nx+j*nx,k*ny*nx+j*nx,nx);  }  GABinaryString::resize(w*h*d);  if(w > STA_CAST(int,nx) && h > STA_CAST(int,ny)){ // adjust the existing bits    int z=GAMin(STA_CAST(int,nz),d);    for(int k=z-1; k>=0; k--){      int j;      for(j=ny-1; j>=0; j--){	GABinaryString::move(k*h*w+j*w,k*ny*nx+j*nx,nx);	for(int i=nx; i<w; i++)	  bit(k*h*w+j*w+i, GARandomBit());      }      for(j=ny; j<h; j++)	for(int i=0; i<w; i++)	  bit(k*h*w+j*w+i, GARandomBit());    }  }  else if(w > STA_CAST(int,nx)){    int z=GAMin(STA_CAST(int,nz),d);    for(int k=z-1; k>=0; k--){      for(int j=h-1; j>=0; j--){	GABinaryString::move(k*h*w+j*w,k*h*nx+j*nx,nx);	for(int i=nx; i<w; i++)	  bit(k*h*w+j*w+i, GARandomBit());      }    }  }  else if(h > STA_CAST(int,ny)){    int z=GAMin(STA_CAST(int,nz),d);    for(int k=z-1; k>=0; k--){      int j;      for(j=ny-1; j>=0; j--)	GABinaryString::move(k*h*w+j*w,k*ny*w+j*w,w);      for(j=ny; j<h; j++)	for(int i=0; i<w; i++)	  bit(k*h*w+j*w+i, GARandomBit());    }  }  if(d > STA_CAST(int,nz)){		// change in depth is always new bits    for(int i=w*h*nz; i<w*h*d; i++)      bit(i, GARandomBit());  }  nx = w; ny = h; nz = d;  _evaluated = gaFalse;  return sz;}#ifdef GALIB_USE_STREAMSintGA3DBinaryStringGenome::read(STD_ISTREAM & is){  static char c;  unsigned int i=0, j=0, k=0;  while(!is.fail() && !is.eof() && k < nz) {    is >> c;    if(isdigit(c)){      gene(i++, j, k, ((c == '0') ? 0 : 1));      if(i >= nx){	i=0;	j++;      }      if(j >= ny){	j=0;	k++;      }    }  }  _evaluated = gaFalse;  if(is.eof() &&      ((k < nz) ||		// didn't get some lines      (j < ny && j != 0) ||	// didn't get some lines      (i < nx && i != 0))){	// didn't get some lines    GAErr(GA_LOC, className(), "read", gaErrUnexpectedEOF);    is.clear(STD_IOS_BADBIT | is.rdstate());    return 1;  }  return 0;}// Dump the bits to the stream with a newline at the end of each row and // another at the end of each layer.  No newline at the end of the block.intGA3DBinaryStringGenome::write(STD_OSTREAM & os) const {  for(unsigned int k=0; k<nz; k++){    for(unsigned int j=0; j<ny; j++){      for(unsigned int i=0; i<nx; i++){	os << gene(i,j,k);      }      os << "\n";    }    os << "\n";  }  return 0;}#endifintGA3DBinaryStringGenome::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;  }  else if(which == DEPTH) {    if(maxZ == minZ) val = FIXED_SIZE;    else val = maxZ;  }  return val;}intGA3DBinaryStringGenome::resizeBehaviour(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) resize(upper,ny,nz);    if(nx < lower) resize(lower,ny,nz);    break;  case HEIGHT:    minY = lower; maxY = upper;    if(ny > upper) resize(nx,upper,nz);    if(ny < lower) resize(nx,lower,nz);    break;  case DEPTH:    minZ = lower; maxZ = upper;    if(nz > upper) resize(nx,ny,upper);    if(nz < lower) resize(nx,ny,lower);    break;  default:    break;  }  return resizeBehaviour(which);}voidGA3DBinaryStringGenome::copy(const GA3DBinaryStringGenome & orig,     unsigned int r, unsigned int s, unsigned int t,     unsigned int x, unsigned int y, unsigned int z,     unsigned int w, unsigned int h, unsigned int d){  if(w == 0 || x >= orig.nx || r >= nx ||     h == 0 || y >= orig.ny || s >= ny ||     d == 0 || z >= orig.nz || t >= nz) return;  if(x + w > orig.nx) w = orig.nx - x;  if(y + h > orig.ny) h = orig.ny - y;  if(z + d > orig.nz) d = orig.nz - z;  if(r + w > nx) w = nx - r;  if(s + h > ny) h = ny - s;  if(t + d > nz) d = nz - t;  for(unsigned int k=0; k<d; k++)    for(unsigned int j=0; j<h; j++)      GABinaryString::copy(orig,			   (t+k)*ny*nx + (s+j)*nx + r,			   (z+k)*orig.ny*orig.nx + (y+j)*orig.nx + x, w);  _evaluated = gaFalse;}voidGA3DBinaryStringGenome::set(unsigned int x, unsigned int y, unsigned int z,    unsigned int w, unsigned int h, unsigned int d){  if(x + w > nx) w = nx - x;  if(y + h > ny) h = ny - y;  if(z + d > nz) d = nz - z;  for(unsigned int k=0; k<d; k++)    for(unsigned int j=0; j<h; j++)      GABinaryString::set((z+k)*ny*nx + (y+j)*nx + x, w);  _evaluated = gaFalse;}voidGA3DBinaryStringGenome::unset(unsigned int x, unsigned int y, unsigned int z,      unsigned int w, unsigned int h, unsigned int d){  if(x + w > nx) w = nx - x;  if(y + h > ny) h = ny - y;  if(z + d > nz) d = nz - z;  for(unsigned int k=0; k<d; k++)    for(unsigned int j=0; j<h; j++)      GABinaryString::unset((z+k)*ny*nx + (y+j)*nx + x, w);  _evaluated = gaFalse;}voidGA3DBinaryStringGenome::randomize(unsigned int x, unsigned int y, unsigned int z,	  unsigned int w, unsigned int h, unsigned int d){

⌨️ 快捷键说明

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