// Unlike the base array genome, here when we write out we don't put any
// whitespace between genes.  No newline at end of it all.
template <> int
GA1DArrayAlleleGenome<char>::write(STD_OSTREAM & os) const
{
  for(unsigned int i=0; i<nx; i++)
    os << gene(i);
  return 0;
}
Пример #2
0
int
GA2DBinaryStringGenome::read(STD_ISTREAM & is)
{
  static char c;
  unsigned int i=0, j=0;
  while(!is.fail() && !is.eof() && j < ny) {
    is >> c;
    if(isdigit(c)){
      gene(i, j, ((c == '0') ? 0 : 1));
      if(++i >= nx){		// ready for next row
	i=0;
	j++;
      }
    }
  }

  _evaluated = gaFalse;

  if(is.eof() && 
     ((j < ny) ||	     // didn't get some lines
      (i < nx && i != 0))){   // stopped early on a row
    GAErr(GA_LOC, className(), "read", gaErrUnexpectedEOF);
    is.clear(STD_IOS_BADBIT | is.rdstate());
    return 1;
  }

  return 0;
}
// When we write the data to a stream we do it without any spaces.  Also, there
// is no newline at the end of the stream of digits.
int
GA1DBinaryStringGenome::write(STD_OSTREAM & os) const
{
  for(unsigned int i=0; i<nx; i++)
    os << gene(i);
  return 0;
}
Пример #4
0
int
GA3DBinaryStringGenome::read(std::istream & is)
{
  static char c;
  unsigned int i=0, j=0, k=0;
  do{
    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++;
      }
    }
  } while(!is.fail() && !is.eof() && k < nz);

  _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;
}
Пример #5
0
// Unlike the base array genome, here when we write out we don't put any
// whitespace between genes.  No newline at end of it all.
int
GA1DArrayAlleleGenome<char>::write(ostream & os) const
{
  for(unsigned int i=0; i<nx; i++)
    os << gene(i);
  return 0;
}
Пример #6
0
////////////////////////////////////////////////////////////////////////////////////////////////////
/// container and manager for a list of genes
void GeneList::finalize()
{
	if ( genes_.size() != 0 ) {
		for ( std::vector< Gene >::iterator gene( genes_.begin() );
		      gene != genes_.end(); ++gene ) {
			gene->finalize();
		}
	}
	else std::cerr << "ERROR: no genes in the list!" << std::endl;

	numseqs_ = genes_.size();
	numbps_ = 0;
	for ( std::vector< Gene >::const_iterator gene( genes_.begin() );
  gene != genes_.end(); ++gene ) {
		numbps_ += gene->sequence().size();
	}
}
Пример #7
0
void
GeneList::print( std::ostream & out ) const
{
	for ( std::vector< Gene >::const_iterator gene( genes_.begin() );
  gene != genes_.end(); ++gene ) {
		out << *gene;
	}
}
Пример #8
0
// Dump the digits to the stream with a newline between each row.  No newline
// at the end of the whole thing.
int
GA2DBinaryStringGenome::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;
}
Пример #9
0
void GAMachine::CreateStartPopulation()
{
	for (int i = 0; i < POPULATION_SIZE; i++)
	{
		Genome genome;
		for (int i = 0; i < 112; i++)
		{
			Gene gene(GNR_RANDOM_INT(2), GNR_RANDOM_INT(NUM_SECTORS - 1));
			genome.genes.push_back(gene);
		}
		genomes.push_back(genome);
	}
}
Пример #10
0
template <class T> int
GA3DArrayGenome<T>::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;
}
Пример #11
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.
int
GA3DBinaryStringGenome::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;
}
Пример #12
0
// The read specialization takes in each number and stuffs it into the array.
int
GA1DArrayAlleleGenome<float>::read(istream & is) {
  unsigned int i=0;
  float val;
  do{
    is >> val;
    if(!is.fail()) gene(i++, val);
  } while(!is.fail() && !is.eof() && i < nx);

  if(is.eof() && i < nx){
    GAErr(GA_LOC, className(), "read", gaErrUnexpectedEOF);
    is.clear(ios::badbit | is.rdstate());
    return 1;
  }
  return 0;
}
Пример #13
0
// The read specialization takes in each number and stuffs it into the array.
template <> int
GA1DArrayAlleleGenome<float>::read(STD_ISTREAM & is) {
  unsigned int i=0;
  float val;
  do{
    is >> val;
    if(!is.fail()) gene(i++, val);
  } while(!is.fail() && !is.eof() && i < nx);

  if(is.eof() && i < nx){
    GAErr(GA_LOC, className(), "read", gaErrUnexpectedEOF);
    is.clear(STD_IOS_BADBIT | is.rdstate());
    return 1;
  }
  return 0;
}
Пример #14
0
Gene Gene::generateRandomGene(int vertexCount, int verticesNumber,  bool isClosed) {
  BitSet set;

  int tmp;
  for(int i=0; i<vertexCount; ++i) {
    tmp = Random::uniformInt(0, verticesNumber - 1);

    if(set[tmp])
      --i;

    set[tmp] = true;
  }

  Gene gene(isClosed);
  gene.verticesMask = set;
  return gene;
}
// We read data from a stream as a series of 1's and 0's.  We want a continuous
// stream (ie no spaces) but if there are spaces then we can handle them.  We
// ignore all whitespace.  We ignore anything that is not a digit.  If it is a
// zero then we set to zero.  Anything else is a 1.
int
GA1DBinaryStringGenome::read(STD_ISTREAM & is)
{
  static char c;
  unsigned int i=0;

  while(!is.fail() && !is.eof() && i<nx) {
    is >> c;
    if(isdigit(c)) gene(i++, ((c == '0') ? 0 : 1));
  }

  _evaluated = gaFalse;

  if(is.eof() && i < nx){
    GAErr(GA_LOC, className(), "read", gaErrUnexpectedEOF);
    is.clear(STD_IOS_BADBIT | is.rdstate());
    return 1;
  }

  return 0;
}
Пример #16
0
int GA1DArrayAlleleGenome<char>::read(std::istream & is)
{
    unsigned int i = 0;
    char c;
    do
    {
        is.get(c);
        if(!is.fail())
        {
            gene(i++, c);
        }
    }
    while(!is.fail() && !is.eof() && i < nx);

    if(is.eof() && i < nx)
    {
        GAErr(GA_LOC, className(), "read", gaErrUnexpectedEOF);
        is.clear(std::ios::badbit | is.rdstate());
        return 1;
    }
    return 0;
}
Пример #17
0
int main(int argc, char* argv[])
{
	srand(time(NULL));

/*
	int n = 10000;  //nodes in graph
	int p = 40;	//population size of pool
	int u = 10; //utilities
	int e = 5; //elites
	float r = 0.1; //mutation rate
	int c = 25; //crossovers
	int iterations = 7500;
*/

	std::vector< char > myCode = {1,0,1,1,1,0,0,1,0,1};
	gene geneOne = gene(5,myCode);
	geneOne.printCode();

//	geneOne.printCycles();


	return 0;
}
// When we write the data to a stream we do it with spaces between elements.
// Also, there is no newline at the end of the stream of digits.
template <class T> int
GA1DArrayGenome<T>::write(STD_OSTREAM & os) const {
    for(unsigned int i=0; i<nx; i++)
        os << gene(i) << " ";
    return 0;
}
void GeneticOperators::Initializer(GAGenome& g)//todo: better initializer
{
	std::cout<<"calling Initializer\n";
	GAListGenome<Gene>& genome = (GAListGenome<Gene>&)g;


	if(_seedOriginalPosition){
	    while(genome.head()) genome.destroy(); // destroy any pre-existing list
	    for(std::vector<SparCraft::Unit>::const_iterator it=_buildings.begin();
	                   it!=_buildings.end();it++){
	        Gene gene(it->type(),BWAPI::TilePosition((it->pos().x()-it->type().dimensionLeft())/TILE_SIZE,(it->pos().y()-it->type().dimensionUp())/TILE_SIZE));
	        genome.insert(gene,GAListBASE::TAIL);
	    }
	    if(!isLegal(genome)||!isPowered(genome)){
	        std::cout<<"Repairing setup from file"<<std::endl;
	        if(!GeneticOperators::repair(genome)){
	            System::FatalError("Couldn't repair at initializer");
	        }
	    }
	    _seedOriginalPosition=false;
	}else{
	    do{
	        while(genome.head()) genome.destroy(); // destroy any pre-existing list
	        bool needsRepair=false;
	        for(std::vector<SparCraft::Unit>::const_iterator it=_buildings.begin();
	                it!=_buildings.end();it++){


	            BWAPI::TilePosition pos(_defendPlayer->getGoal().x()/TILE_SIZE,_defendPlayer->getGoal().y()/TILE_SIZE);
	            Gene gene(it->type(),pos);
	            //	        BWAPI::TilePosition offset(0,0);
	            int n=placementRetries;
	            do{
	                do{
	                    //	                gene.undo(offset);
	                    //	                offset=BWAPI::TilePosition(GARandomInt(-mutDistance,mutDistance),GARandomInt(-mutDistance,mutDistance));
	                    //	                gene.move(offset);
	                    gene.setPosition(BWAPI::TilePosition(
	                            GARandomInt(_baseLeft,_baseRight),
	                            GARandomInt(_baseTop,_baseBottom)));

	                }while(!_map->canBuildHere(gene.getType(),gene.getCenterPos()));

	                if(isLegal(genome,gene)){
	                    if(!gene.getType().requiresPsi()||isPowered(genome,gene)){
	                        genome.insert(gene,GAListBASE::TAIL);
	                        break;
	                    }
	                }
	                n--;
	            }while(n>0);
	            if(n==0){//if we reached the max amount of tries, add it anyway and try to repair later
	                genome.insert(gene,GAListBASE::TAIL);
	                needsRepair=true;
	                std::cout<<"Max amount of retries for initial location failed, will try to repair\n";
	            }

	            //		std::cout<<"building added"<<std::endl;
	        }
	        if(needsRepair||!isLegal(genome)){
	            if(!GeneticOperators::repair(genome)){
	                System::FatalError("Couldn't repair at initializer");
	            }
	        }
	    }while(!isLegal(genome));
	}
	//	Mutator(genome,0.5,20);
}
Пример #20
0
//--------------------- CreateFromFile -----------------------------------
//
//  creates and returns a genome from a data file
//------------------------------------------------------------------------
bool CGenome::CreateFromFile(const char* szFileName)
{
	ifstream in(szFileName);

	//check for error
	if (!in)
	{
		MessageBox(NULL, "Cannot find genome file!", "error", MB_OK);

		return false;
	}

	//clear any current neuron and link genes
	m_vecNeurons.clear();
	m_vecLinks.clear();

	char buffer[100];
	int  iVal;

	//discard the genomeID
	in >> buffer; in >> iVal;

	//get the network depth
	in >> buffer; in >> m_iNetDepth;

	//grab the neuron data and create the neuron genes
	int NumNeurons = 0;

	in >> buffer; in >> NumNeurons;

	for (int n=0; n<NumNeurons; ++n)
	{
		int    NeuronID, NeuronType;
		double Activation, SplitX, SplitY;
		bool   Recurrent;

		in >> buffer; in >> NeuronID;
		in >> buffer; in >> NeuronType;
		in >> buffer; in >> Recurrent;
		in >> buffer; in >> Activation;
		in >> buffer; in >> SplitX;
		in >> buffer; in >> SplitY;

		//create a neuron gene
		SNeuronGene gene((neuron_type)NeuronType,
			NeuronID,
			SplitY,
			SplitX,
			Recurrent,
			Activation);

		//add it
		m_vecNeurons.push_back(gene);

	}//grab next neuron

	//grab the link data and create the link genes
	int NumLinks = 0;
	in >> buffer; in >> NumLinks;

	int NextInnovationID = NumNeurons;

	for (int l=0; l<NumLinks; ++l)
	{
		int    from, to, ID;
		bool   recurrent, enabled;
		double weight;

		in >> buffer; in >> ID;
		in >> buffer; in >> from;
		in >> buffer; in >> to;
		in >> buffer; in >> enabled;
		in >> buffer; in >> recurrent;
		in >> buffer; in >> weight;

		//create a link gene
		SLinkGene LinkGene(from,
			to,
			enabled,
			NextInnovationID++,
			weight,
			recurrent);

		//add it
		m_vecLinks.push_back(LinkGene);


	}//next link


	return true;
}