void GreedyHeuristic::getGreedyHeuristicResult(UndirectedGraph* aTree, int cardinality, string ls_type) { UndirectedGraph* greedyTree = new UndirectedGraph(); bool started = false; for (list<Edge*>::iterator anEdge = ((*graph).edges).begin(); anEdge != ((*graph).edges).end(); anEdge++) { greedyTree->clear(); greedyTree->addVertex((*anEdge)->fromVertex()); greedyTree->addVertex((*anEdge)->toVertex()); greedyTree->addEdge(*anEdge); generateNeighborhoodFor(*anEdge); for (int k = 1; k < cardinality; k++) { Edge* kctn = getMinNeighbor(); Vertex* nn = determineNeighborNode(kctn,greedyTree); greedyTree->addVertex(nn); greedyTree->addEdge(kctn); adaptNeighborhoodFor(kctn,nn,greedyTree); } if (!(ls_type == "no")) { /* application of local search */ if (ls_type == "leafs") { LocalSearch lsm(graph,greedyTree); lsm.run(ls_type); } else { if (ls_type == "cycles_leafs") { //cout << *greedyTree << endl; LocalSearchB lsm; lsm.run(graph,greedyTree); } } /* end local search */ /* if (!isConnectedTree(greedyTree)) { cout << "non-connected tree" << endl; } */ } greedyTree->setWeight(weightOfSolution(greedyTree)); if (started == false) { started = true; aTree->copy(greedyTree); } else { if ((greedyTree->weight()) < (aTree->weight())) { aTree->copy(greedyTree); } } } delete(greedyTree); }
int main( int argc, char **argv ) { if ( argc < 2 ) { print_help(); exit(1); } else { read_parameters(argc,argv); } // initialize random number generator rnd = new Random((unsigned) time(&t)); // initialize and read instance from file graph = new UndirectedGraph(i_file); GreedyHeuristic gho(graph); for (int i = cardb; i <= carde; i++) { cardinality = i; if ((cardinality == cardb) || (cardinality == carde) || ((cardinality % cardmod) == 0)) { Timer timer; UndirectedGraph* greedyTree = new UndirectedGraph(); if (type == "edge_based") { gho.getGreedyHeuristicResult(greedyTree,cardinality,ls); } else { if (type == "vertex_based") { gho.getVertexBasedGreedyHeuristicResult(greedyTree,cardinality,ls); } } printf("%d\t%f\t%f\n",cardinality,greedyTree->weight(),timer.elapsed_time(Timer::VIRTUAL)); delete(greedyTree); } } delete(graph); delete rnd; }
int main( int argc, char **argv ) { if ( argc < 2 ) { cout << "something wrong" << endl; exit(1); } else { read_parameters(argc,argv); } Timer initialization_timer; rnd = new Random((unsigned) time(&t)); graph = new UndirectedGraph(i_file); init_pheromone_trail(); register int i = 0; register int j = 0; register int k = 0; register int b = 0; cout << endl; for (int card_counter = cardb; card_counter <= carde; card_counter++) { cardinality = card_counter; if ((cardinality == cardb) || (cardinality == carde) || ((cardinality % cardmod) == 0)) { printf("begin cardinality %d\n",cardinality); if (tfile_is_given) { if (times.count(cardinality) == 1) { time_limit = times[cardinality]; } } vector<double> results; vector<double> times_best_found; double biar = 0.0; int n_of_ants = (int)(((double)((*graph).edges).size()) / ((double)cardinality)); if (n_of_ants < 15) { n_of_ants = 15; } if (n_of_ants > 50) { n_of_ants = 50; } if (ants.size() > 0) { for (list<KCT_Ant*>::iterator anAnt = ants.begin(); anAnt != ants.end(); anAnt++) { delete(*anAnt); } } ants.clear(); for (i = 0; i < n_of_ants; i++) { KCT_Ant* ant = new KCT_Ant(); ant->initialize(graph,rnd,pheromone,daemon_action); ants.push_back(ant); } for (int trial_counter = 1; trial_counter <= n_of_trials; trial_counter++) { printf("begin try %d\n",trial_counter); UndirectedGraph* best = NULL; UndirectedGraph* newSol = NULL; UndirectedGraph* restartBest = NULL; double ib_weight = 0.0; double rb_weight = 0.0; double gb_weight = 0.0; Timer timer; if ((!(card_counter == cardb)) || (!(trial_counter == 1))) { reset_pheromone_trail(); for (list<KCT_Ant*>::iterator ant = ants.begin(); ant != ants.end(); ant++) { (*ant)->restart_reset(); } } int iter = 1; double cf = 0.0; bool restart = false; bool program_stop = false; bool bs_update = false; while (program_stop == false) { for (list<KCT_Ant*>::iterator ant = ants.begin(); ant != ants.end(); ant++) { for (k = 0; k < cardinality; k++) { (*ant)->step(0.8); } (*ant)->evaluate(); } if (!(newSol == NULL)) { delete(newSol); } if (elite_action == "no") { newSol = getBestDaemonSolutionInIteration(); } else { KCT_Ant* bestAnt = getBestDaemonAntInIteration(); bestAnt->eliteAction(); newSol = new UndirectedGraph(); newSol->copy(bestAnt->getCurrentDaemonSolution()); } if (iter == 1) { best = new UndirectedGraph(); printf("best %f\ttime %f\n",newSol->weight(),timer.elapsed_time(Timer::VIRTUAL)); best->copy(newSol); restartBest = new UndirectedGraph(newSol); results.push_back(newSol->weight()); times_best_found.push_back(timer.elapsed_time(Timer::VIRTUAL)); if (trial_counter == 1) { biar = newSol->weight(); } else { if (newSol->weight() < biar) { biar = newSol->weight(); } } } else { if (restart) { restart = false; restartBest->copy(newSol); if (newSol->weight() < best->weight()) { printf("best %f\ttime %f\n",newSol->weight(),timer.elapsed_time(Timer::VIRTUAL)); best->copy(newSol); results[trial_counter-1] = newSol->weight(); times_best_found[trial_counter-1] = timer.elapsed_time(Timer::VIRTUAL); if (newSol->weight() < biar) { biar = newSol->weight(); } } } else { if (newSol->weight() < restartBest->weight()) { restartBest->copy(newSol); } if (newSol->weight() < best->weight()) { printf("best %f\ttime %f\n",newSol->weight(),timer.elapsed_time(Timer::VIRTUAL)); best->copy(newSol); results[trial_counter-1] = newSol->weight(); times_best_found[trial_counter-1] = timer.elapsed_time(Timer::VIRTUAL); if (newSol->weight() < biar) { biar = newSol->weight(); } } } } cf = get_cf(newSol); //cout << "cf: " << cf << endl; if (bs_update && (cf > 0.99)) { //cout << "doing restart" << endl; bs_update = false; restart = true; cf = 0.0; reset_pheromone_trail(); } else { if (cf > 0.99) { bs_update = true; } if (!bs_update) { if (cf < 0.7) { l_rate = 0.15; ib_weight = 2.0/3.0; rb_weight = 1.0/3.0; gb_weight = 0.0; } if ((cf >= 0.7) && (cf < 0.95)) { l_rate = 0.1; ib_weight = 1.0 / 3.0; rb_weight = 2.0 / 3.0; gb_weight = 0.0; } if (cf >= 0.95) { l_rate = 0.05; ib_weight = 0.0; rb_weight = 1.0; gb_weight = 0.0; } } else { // if bs_update = TRUE we use the best_so_far solution for updating the pheromone values l_rate = 0.1; ib_weight = 0.0; rb_weight = 0.0; gb_weight = 1.0; } map<Edge*,double>* trans_best = translate_solution(best); map<Edge*,double>* trans_newSol = translate_solution(newSol); map<Edge*,double>* trans_restartBest = translate_solution(restartBest); map<Edge*,double>* new_pd = new map<Edge*,double>; for (list<Edge*>::iterator e = (graph->edges).begin(); e != (graph->edges).end(); e++) { (*new_pd)[*e] = 0.0; (*new_pd)[*e] = (*new_pd)[*e] + (ib_weight * (*trans_newSol)[*e]) + (rb_weight * (*trans_restartBest)[*e]) + (gb_weight * (*trans_best)[*e]); } for (list<Edge*>::iterator e = (graph->edges).begin(); e != (graph->edges).end(); e++) { (*pheromone)[*e] = (*pheromone)[*e] + (l_rate * ((*new_pd)[*e] - (*pheromone)[*e])); if ((*pheromone)[*e] > tau_max) { (*pheromone)[*e] = tau_max; } if ((*pheromone)[*e] < tau_min) { (*pheromone)[*e] = tau_min; } } delete(trans_best); delete(trans_newSol); delete(trans_restartBest); delete(new_pd); } for (list<KCT_Ant*>::iterator i = ants.begin(); i != ants.end(); i++) { (*i)->reset(); } iter = iter + 1; if (tfile_is_given) { if (timer.elapsed_time(Timer::VIRTUAL) > time_limit) { program_stop = true; } } else { if (time_limit_given && iter_limit_given) { if ((timer.elapsed_time(Timer::VIRTUAL) > time_limit) || (iter > n_of_iter)) { program_stop = true; } } else { if (time_limit_given) { if (timer.elapsed_time(Timer::VIRTUAL) > time_limit) { program_stop = true; } } else { if (iter > n_of_iter) { program_stop = true; } } } } } printf("end try %d\n",trial_counter); // eturetken 18.09.09. Write the best MST for this cardinality to the file. //////////////////////////////////////////////////////////////////// if( mstfile_is_given ) { string MSTFile(mst_file); best->Write2File(concatIntToString(MSTFile, card_counter) + ".mst"); } //////////////////////////////////////////////////////////////////// delete(best); delete(restartBest); delete(newSol); } double r_mean = 0.0; double t_mean = 0.0; for (int i = 0; i < results.size(); i++) { r_mean = r_mean + results[i]; t_mean = t_mean + times_best_found[i]; } r_mean = r_mean / ((double)results.size()); t_mean = t_mean / ((double)times_best_found.size()); double rsd = 0.0; double tsd = 0.0; for (int i = 0; i < results.size(); i++) { rsd = rsd + pow(results[i]-r_mean,2.0); tsd = tsd + pow(times_best_found[i]-t_mean,2.0); } rsd = rsd / ((double)(results.size()-1.0)); if (rsd > 0.0) { rsd = sqrt(rsd); } tsd = tsd / ((double)(times_best_found.size()-1.0)); if (tsd > 0.0) { tsd = sqrt(tsd); } if (output_file_given == true) { fout << cardinality << "\t" << r_mean << "\t" << rsd << "\t" << t_mean << "\t" << tsd << endl; } else { printf("statistics\t%d\t%g\t%f\t%f\t%f\t%f\n",cardinality,biar,r_mean,rsd,t_mean,tsd); } printf("end cardinality %d\n",cardinality); } } if (output_file_given == true) { fout.close(); } delete(graph); delete rnd; }