Beispiel #1
0
void CGeneticSystem::stepGeneration(){
	//stepGeneration2();
	//return;
	std::vector<CChromo> newPopulation(m_populationSize*2);
	for(int i = 0; i < m_populationSize*2; i++)
	{
		CChromo p(m_noCitys , m_distMatrix);
		newPopulation[i] = p;
	}
    int newPopulationSize = 0;
    SortPopulation(m_ChromoPopulation,false);
    computeFitness();
    for(int i = 0; i < m_populationSize; i++)
	{
	  for(int j = 0; j < m_noCitys; j++)
		{
			int test = m_ChromoPopulation[i].getGene(j);
            newPopulation[i].setGene(j,test );
        }
        newPopulationSize++;
    }
    while(newPopulationSize < 2*m_populationSize)
	{
        int idx1 =0; //tournamentSelection();
		int idx2 =0; //tournamentSelection();
		while(idx1 == idx2)
		{
			idx2= tournamentSelection();
			idx1= tournamentSelection();
		}
		CChromo &pfather=m_ChromoPopulation[idx2];
		CChromo &pMother=m_ChromoPopulation[idx1];
        CChromo p_offspring1(m_noCitys,m_distMatrix) , p_offspring2(m_noCitys,m_distMatrix);
		pMother.crossover(&pfather, &p_offspring1, &p_offspring2);
		newPopulation[newPopulationSize] = p_offspring1;
        newPopulationSize++;
		if(newPopulationSize >= newPopulation.size())
            break;
        newPopulation[newPopulationSize] = p_offspring2;
        newPopulationSize++;

    }
    mutatePopulation(newPopulation);
    SortPopulation(newPopulation , true);  //ass
    for(int i = 0; i < m_populationSize-2; i++) //keep last best
	{
		m_ChromoPopulation[i] = newPopulation[i];
	}
    SortPopulation(m_ChromoPopulation , true);
    updateBestSoFarPath();
}
//************************************
// Method:    constructSolutions
// FullName:  CAntColonySystem::constructSolutions
// Access:    virtual public 
// Returns:   void
// Qualifier:
//************************************
void CAntColonySystem::constructSolutions()
{
	m_tourTime.startTimer();
	//clear ants memory and shuffle start pos
	initAnts();
	//place ants in ramdom citys for starting
	for (size_t k = 0; k <  m_Ants.size(); k++ )
	{
		m_Ants[k].setAntCity(0,m_randomPath[k]);
		m_Ants[k].setCityVisited(m_randomPath[k]);
	}
	uniform_real<double> rndSelTrsh(0.0, 1.0);
	uniform_real<double> m_ChoiceExplore(0.0 , 1.0);

	for(size_t step = 1 ; step < m_noNodes; step++)
	{
		for(size_t k = 0; k < m_Ants.size(); k++)
		{
			decisionRule(k,step, rndSelTrsh, m_ChoiceExplore);
		//	localPheromoneUpdate(k,m_Ants[k].getCity(step));
		}
	}

	for(size_t k = 0; k <  m_Ants.size(); k++)
	{
		int tourstart=m_Ants[k].getCity(0);
		m_Ants[k].setAntCity(m_noNodes,tourstart);
		m_Ants[k].setAntTourLength((int)this->calculateAntPathLength(m_Ants[k]));
	//	size_t idx1 = this->m_Ants[k].getCity(m_noNodes);
	//	size_t idx2 = m_Ants[k].getCity(m_noNodes-1);

	//	m_newPheromoneMatrix->local_pheromone_update(m_noNodes-1, m_Ants[k].getAntsCityTour()[0] ,xi, tau0);
	//	localPheromoneEndIndexUpdate(idx1, idx2);
	}
	
	for(size_t k = 0; k < m_Ants.size(); k++)
		for(size_t i=0;i<m_Ants[k].getAntsCityTour().size();i++) 
		{
			if(i==0)
				m_newPheromoneMatrix->local_pheromone_update(m_noNodes-1, m_Ants[k].getAntsCityTour()[i] ,xi, tau0);
			else 
				m_newPheromoneMatrix->local_pheromone_update(m_Ants[k].getAntsCityTour()[i-1], m_Ants[k].getAntsCityTour()[i],xi , tau0 );
		}
	
	m_tourStats.tourAverageTime = (m_tourTime.getElapsedTime())/  (m_Ants.size()-1);
	m_tourTime.stopTimer();
	updateBestSoFarPath();
}
Beispiel #3
0
void CGeneticSystem::localSearch(){
    if(m_bOpt2Search)
	{
        for(int i = 0; i < m_populationSize; i++)
		{
            m_pLocalSearch->opt2(m_ChromoPopulation[i].getPath());
        }
    }
    if(m_bOpt3Search)
	{
        for(int i = 0; i < m_populationSize; i++)
		{
            m_pLocalSearch->opt3(m_ChromoPopulation[i].getPath());
        }
    }
    updateBestSoFarPath();
}
Beispiel #4
0
void CGeneticSystem::stepGeneration2()
{
	std::vector<CChromo> newPopulation(m_populationSize*2);
	for(int i = 0; i < m_populationSize*2; i++)
	{
		CChromo p(m_noCitys , m_distMatrix);
		newPopulation[i] = p;
	}

	int newPopulationSize = 0;
	SortPopulation(m_ChromoPopulation , false);
	computeFitness();

	for(int i = 0; i < m_populationSize; i++)
	{
		for(int j = 0; j < m_noCitys; j++)
		{
			int test = m_ChromoPopulation[i].getGene(j);
			newPopulation[i].setGene(j,test );
		}
		newPopulationSize++;
	}
     

	while (newPopulationSize < 2*m_populationSize) 
	{
		int idx1 = tournamentSelection();
		int idx2 = tournamentSelection();
		CChromo offspring = m_ChromoPopulation[idx1].CrossOver2(&m_ChromoPopulation[idx2]);
		newPopulation[newPopulationSize] = offspring;
		newPopulationSize++;
	}
  	mutatePopulation(newPopulation);
	SortPopulation(newPopulation , true);
	for(int i = 0; i < m_populationSize; i++)
	{
		m_ChromoPopulation[i] = newPopulation[i];
	}
	SortPopulation(m_ChromoPopulation,true);
	updateBestSoFarPath();
}