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

📄 ga3darraygenome.c

📁 关于遗传算法代码。比较全。希望能给大家带来帮助。
💻 C
📖 第 1 页 / 共 3 页
字号:
// $Header: /usr/people/mbwall/src/galib/ga/RCS/GA3DArrayGenome.C,v 1.6 1999/03/27 19:18:08 mbwall Exp $
/* ----------------------------------------------------------------------------
  array3.C
  mbwall 25feb95
  Copyright (c) 1995 Massachusetts Institute of Technology
                     all rights reserved

 DESCRIPTION:
  Source file for the 3D array genome.
---------------------------------------------------------------------------- */
#ifndef _ga_array3_C_
#define _ga_array3_C_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ga/garandom.h>
#include <ga/GA3DArrayGenome.h>
#include <ga/GAMask.h>


/* ----------------------------------------------------------------------------
3DArrayGenome
---------------------------------------------------------------------------- */
template <class T> const char *
GA3DArrayGenome<T>::className() const {return "GA3DArrayGenome";}
template <class T> int
GA3DArrayGenome<T>::classID() const {return GAID::ArrayGenome3D;}

template <class T> 
GA3DArrayGenome<T>::
GA3DArrayGenome(unsigned int w, unsigned int h, unsigned int d,
		GAGenome::Evaluator f,
		void * u) :
GAArray<T>(w*h*d),
GAGenome(DEFAULT_3DARRAY_INITIALIZER, 
	 DEFAULT_3DARRAY_MUTATOR,
	 DEFAULT_3DARRAY_COMPARATOR)
{
  evaluator(f);
  userData(u);
  crossover(DEFAULT_3DARRAY_CROSSOVER);
  nx=minX=maxX=w; ny=minY=maxY=h; nz=minZ=maxZ=d;
}


template <class T> 
GA3DArrayGenome<T>::
GA3DArrayGenome(const GA3DArrayGenome<T> & orig) :
GAArray<T>(orig.sz), GAGenome(){
  GA3DArrayGenome<T>::copy(orig);
}


template <class T>
GA3DArrayGenome<T>::~GA3DArrayGenome(){ }


template <class T> void
GA3DArrayGenome<T>::copy(const GAGenome & orig){
  if(&orig == this) return;
  const GA3DArrayGenome<T>* c = DYN_CAST(const GA3DArrayGenome<T>*, &orig);
  if(c) {
    GAGenome::copy(*c);
    GAArray<T>::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;
  }
}


template <class T> GAGenome *
GA3DArrayGenome<T>::clone(GAGenome::CloneMethod flag) const {
  GA3DArrayGenome<T> *cpy = new GA3DArrayGenome<T>(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;
}


template <class T> int
GA3DArrayGenome<T>::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++)
	GAArray<T>::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++)
	GAArray<T>::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++)
	GAArray<T>::move(k*h*nx+j*nx,k*ny*nx+j*nx,nx);
  }

  GAArray<T>::size(w*h*d);

  if(w > STA_CAST(int,nx) && h > STA_CAST(int,ny)){ 
    int z=GAMin(STA_CAST(int,nz),d);
    for(int k=z-1; k>=0; k--)
      for(int j=ny-1; j>=0; j--)
	GAArray<T>::move(k*h*w+j*w,k*ny*nx+j*nx,nx);
  }
  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--)
	GAArray<T>::move(k*h*w+j*w,k*h*nx+j*nx,nx);
  }
  else if(h > STA_CAST(int,ny)){
    int z=GAMin(STA_CAST(int,nz),d);
    for(int k=z-1; k>=0; k--)
      for(int j=ny-1; j>=0; j--)
	GAArray<T>::move(k*h*w+j*w,k*ny*w+j*w,w);
  }

  nx = w; ny = h; nz = d;
  _evaluated = gaFalse;
  return sz;
}


#ifndef NO_STREAMS
template <class T> int
GA3DArrayGenome<T>::read(istream &) {
  GAErr(GA_LOC, className(), "read", gaErrOpUndef);
  return 1;
}


template <class T> int
GA3DArrayGenome<T>::write(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;
}
#endif


template <class T> int
GA3DArrayGenome<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;
  }
  else if(which == DEPTH) {
    if(maxZ == minZ) val = FIXED_SIZE;
    else val = maxZ;
  }
  return val;
}


template <class T> int
GA3DArrayGenome<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) GA3DArrayGenome<T>::resize(upper,ny,nz);
    if(nx < lower) GA3DArrayGenome<T>::resize(lower,ny,nz);
    break;

  case HEIGHT:
    minY = lower; maxY = upper;
    if(ny > upper) GA3DArrayGenome<T>::resize(nx,upper,nz);
    if(ny < lower) GA3DArrayGenome<T>::resize(nx,lower,nz);
    break;

  case DEPTH:
    minZ = lower; maxZ = upper;
    if(nz > upper) GA3DArrayGenome<T>::resize(nx,ny,upper);
    if(nz < lower) GA3DArrayGenome<T>::resize(nx,ny,lower);
    break;

  default:
    break;
  }

  return resizeBehaviour(which);
}


template <class T> void
GA3DArrayGenome<T>::
copy(const GA3DArrayGenome<T> & 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++)
      GAArray<T>::copy(orig,
		       (t+k)*ny*nx + (s+j)*nx + r,
		       (z+k)*orig.ny*orig.nx + (y+j)*orig.nx + x, w);

  _evaluated = gaFalse;
}


template <class T> int 
GA3DArrayGenome<T>::equal(const GAGenome & c) const
{
  if(this == &c) return 1;
  GA3DArrayGenome<T> & b = (GA3DArrayGenome<T> &)c;
  if(nx != b.nx || ny != b.ny || nz != b.nz) return 0;
  int val=0;
  for(unsigned int k=0; k<nz && val==0; k++)
    for(unsigned int j=0; j<ny && val==0; j++)
      val = GAArray<T>::equal(b,k*ny*nx,k*ny*nx,nx) ? 0 : 1;
  return(val ? 0 : 1);
}














/* ----------------------------------------------------------------------------
3DArrayAlleleGenome
---------------------------------------------------------------------------- */
template <class T> const char *
GA3DArrayAlleleGenome<T>::className() const {return "GA3DArrayAlleleGenome";}
template <class T> int
GA3DArrayAlleleGenome<T>::classID() const {return GAID::ArrayAlleleGenome3D;}

template <class T> 
GA3DArrayAlleleGenome<T>::
GA3DArrayAlleleGenome(unsigned int w, unsigned int h, unsigned int d,
		      const GAAlleleSet<T> & s,
		      GAGenome::Evaluator f, void * u) :
GA3DArrayGenome<T>(w,h,d,f,u) {
  naset = 1;
  aset = new GAAlleleSet<T>[1];
  aset[0] = s;

  initializer(DEFAULT_3DARRAY_ALLELE_INITIALIZER);
  mutator(DEFAULT_3DARRAY_ALLELE_MUTATOR);
  comparator(DEFAULT_3DARRAY_ALLELE_COMPARATOR);
  crossover(DEFAULT_3DARRAY_ALLELE_CROSSOVER);
}

template <class T> 
GA3DArrayAlleleGenome<T>::
GA3DArrayAlleleGenome(unsigned int w, unsigned int h, unsigned int d,
		      const GAAlleleSetArray<T> & sa,
		      GAGenome::Evaluator f, void * u) :
GA3DArrayGenome<T>(w,h,d, f, u) {
  naset = sa.size();
  aset = new GAAlleleSet<T>[naset];
  for(int i=0; i<naset; i++)
    aset[i] = sa.set(i);

  initializer(DEFAULT_3DARRAY_ALLELE_INITIALIZER);
  mutator(DEFAULT_3DARRAY_ALLELE_MUTATOR);
  comparator(DEFAULT_3DARRAY_ALLELE_COMPARATOR);
  crossover(DEFAULT_3DARRAY_ALLELE_CROSSOVER);
}


template <class T> 
GA3DArrayAlleleGenome<T>::
GA3DArrayAlleleGenome(const GA3DArrayAlleleGenome<T> & orig) : 
GA3DArrayGenome<T>(orig.nx, orig.ny, orig.nz) {
  naset = 0;
  aset = (GAAlleleSet<T>*)0;
  GA3DArrayAlleleGenome<T>::copy(orig);
}


template <class T>
GA3DArrayAlleleGenome<T>::~GA3DArrayAlleleGenome(){

⌨️ 快捷键说明

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