//************************************ // Method: initPheromones // FullName: CAntColonySystem::initPheromones // Access: virtual public // Returns: void // Qualifier: //************************************ void CAntColonySystem::initSystem() { double best_distance = (std::numeric_limits<double>::max)(); std::vector<size_t> randomPath = m_pLocalSearch->greedyPath(m_noNodes); double dist=this->calculatePathLength(randomPath); m_BestAntToDate.setAntsTour(randomPath); if(m_LocalSearchOpt2) m_pLocalSearch->opt2(m_BestAntToDate.getAntsCityTour()); else if(m_LocalSearchOpt3) m_pLocalSearch->opt3(m_BestAntToDate.getAntsCityTour()); else if(m_LocalSearchGreed) m_pLocalSearch->three_opt_first(randomPath , m_nnList); m_BestAntToDate.setAntsTour(randomPath); m_BestAntToDate.setAntTourLength(this->calculateAntPathLength(m_BestAntToDate)); tau0 = 1.0/ ((double)m_noNodes * dist); for (size_t i=0; i<m_noNodes; i++) { for (size_t j=0; j< m_noNodes; j++) this->m_newPheromoneMatrix->set(i, j , tau0); } //added initAnts(); calculateHeuristicMatrix(); // clog.logMatrix(m_newPheromoneMatrix->getMatrix()); }
int simulateACO(){ static int iteration; int max; initAnts(); simulateAllTour(); }
int simulateACO(){ static int iteration; int max; initAnts(); simulateAllTour(); simulateAllPheromone(); max = finateIteration(); if(!parameterAdjustmentMode) printf("interation = %d\n", iteration); iteration++; return max; }
//************************************ // 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(); }
void simulateAllTour(){ initAnts(); for(int i = 0; i < swarmSize; i++){ simulateTour(i); } }
void initACO(WAY * way){ initWay(way); if(!isAlloced) allocAnts(); initAnts(); printf("initialized\n"); }
void initACO(){ initTau(); if(!isAlloced) allocAnts(); initAnts(); printf("initialized\n"); }