Пример #1
0
hyrise::storage::atable_ptr_t MutableVerticalTable::copy() const {
  // copy containers
  std::vector< hyrise::storage::atable_ptr_t > cs;

  for (size_t i = 0; i < containers.size(); ++i) {
    cs.push_back(containers[i]->copy());
  }

  auto new_table = std::make_shared<MutableVerticalTable>(cs);
  new_table->setGeneration(this->generation());

  return new_table;
}
/**
* desc: create a brand new population using the current population
* param: parent1 - binary string of parent 1
*        parent2 - binary string of parent 2
*        generation - generation id of new generation
*        mutationRate - rate int which mutation mill effect children. (e.g. 0.05 = 5%)
* pre-cond: fitness values for current generation must already be set
* post-cond: current generation will be incremented
* ret: 0 if successful
*     -1 if an error occured
*/
int CGeneticAlgorithm::addNewPopulation(std::string parent1, std::string parent2, 
	                                    int generation, double mutationRate)
{
	// check that generation list has been initialized
	if (mGenerationList == nullptr)
	{
		return -1;
	}

	// check that passed strings are same length
	if (parent1.length() != parent2.length())
	{
		return -1;
	}

	// create new population node
	PopulationNode* node (new PopulationNode);

	// set up node
	node->generation = generation;
	node->population = new CPopulation;
	node->data = new CPopulationData;
	node->next = nullptr;
	node->prev = nullptr;

	// initialize each organism in the population
	for (int i = 0; i < LIST_SIZE; ++i)
	{
		COrganism* pChild = new COrganism;
		OrganismParameters* pParam = new OrganismParameters;
		std::string childString;
		int crosspnt = crosspoint(parent1.length());
		
		// create new child and check for errors
		if (createChild(parent1, parent2, childString, crosspnt) < 0)
		{
			return -1;
		}

		// mutate the child string
		std::string mutatedChild = mutateString(childString, mutationRate);

		// set up new child to be added to population
		pChild->setBinaryString(mutatedChild);
		pChild->setParameters(pParam);
		if (pChild->ConvertStringToParameters() < 0)
		{
			return -1;
		}
	
		// add child to population
		if (node->population->addOrganism(i, pChild) < 0)
		{   // failed to add organism
			return -1;
		}
	}

	// add new generation to end of population list
	PopulationNode* current = mGenerationList;

	// TODO: add pointer to last node in GeneticAlgorithm class
	while (current != nullptr)
	{
		// check if current generation matches current
		if ((current->generation) == (node->generation))
		{   // not a unique generation id
			return -1;
		}

		// check if at end of list
		if (current->next == nullptr)
		{   // add new node to list
			current->next = node;
			current->prev = current;
			current = nullptr;  // set NULL to end loop
		}
		else
		{   // move to next node
			current = current->next;
		}
	}

	// newest generation added
	setGeneration(generation);
	return 0;
}