//#include //#include #include #include //#include #include #include //#include #include #include #include //#include #include "SA_SeqSolver.h" #include "util_salib.h" #include "SA_SeqEasyScheduler.h" #include "SA_SeqAartsScheduler.h" const int MaxStringLength = 1024; // bound for the stringlength in the logfile SA_SeqSolver::SA_SeqSolver(SA_Problem &prb) : SA_Solver::SA_Solver(prb) { Cooler = new SA_SeqEasyScheduler; } SA_SeqSolver::~SA_SeqSolver() { delete Cooler; } SA_Output *SA_SeqSolver::GetSolverOutput() { return SA_Solver::GetSolverOutput(); } // \****************************************************** // Methode ReadConfig - Liest die Konfigurations- // daten ( geklammert mit '{' '}' ) aus dem istream // \****************************************************** int SA_SeqSolver::ReadConfig(std::istream & config) {//debug(ClusteringSolver::ReadConfig, myrank); char text[BUFLEN]; config >> text; if ( strcmp(text,"{") != 0) { std::cout << "Config file section for SA_SeqSolver does not begin with {!\n"; return FALSE; } while (config >> text && ( strcmp(text,"}") != 0 )) { if (strcmp(text, "SA_Solver") == 0) { if ( ! SA_Solver::ReadConfig(config) ) { std::cout <<"ReadConfing for SA_Solver failed!\n"; return 0; } } else if (strcmp(text, "SA_Scheduler") == 0) { // Instantiierung des Schedulers config >> text; if (strcmp(text, "SA_SeqEasyScheduler") == 0) { Cooler = new SA_SeqEasyScheduler(); } else if (strcmp(text, "SA_SeqAartsScheduler") == 0) { Cooler = new SA_SeqAartsScheduler(); } // TimeScheduler only implemented in parallel // else if (strcmp(text, "SA_TimeScheduler") == 0) // { // Cooler = new SA_TimeScheduler(); // } else { std::cout <<"Unknown Scheduler "<ReadConfig(config) ) { std::cout <<"ReadConfing for Scheduler failed!\n"; return 0; } } else if (strcmp(text, "algorithm") == 0) { config >> text; delete [] Algorithm; Algorithm = new char[strlen(text)+1]; (strcpy(Algorithm, text) == 0); } else { std::cout <<"In SeqEasySolver::ReadConfig unrecognized keyword \""<ShowConfig(std::cout); } void SA_SeqSolver::InitStates() { time_t starttime, endtime; int things_done=0; // Initialloesung erzeugen if( State == NULL) { State = P->CreateSolution(); things_done = 1; } if (BestSolution == NULL ) BestSolution = P->CreateSolution(); InitialSolutionTime = time(NULL); (*P.*GetInitialSolution_fp)(*State); InitialSolutionTime = time(NULL)- InitialSolutionTime; Cooler->SetStartE(P->GetCost(*State)); Cooler->ResetBestE(); P->Copy(*State,*BestSolution); if( things_done) { if ( VerboseMode ) { std::cout << "Initial solution"<<":"<< "\n"; std::cout << *State <GetCost(*State)<GetLocalN(); if( Length <= 0 ) { std::cout << "Bad value "<GetNeighbor(*State,1); std::cout<<"\tSingle GetNeighbor : done\n"; P->UpdateSolution(*State); std::cout<<"\tUpdateSolution : done\n"; do { Cost = P->GetCost(*State); P->Copy(*State,*BestSolution); P->GetNeighbor(*State,1); P->ResetSolution(*State); if ( (Cost - P->GetCost(*State)) > EPSILON ) { status = 0; std::cout<< "Error: the solution \n"<< (*BestSolution) <<" went after GetNeigbor and ResetSoltuion into\n" << (*State) <<" and the costvalue has changed!\n"; } P->GetNeighbor(*State,1); Cost = P->GetCost(*State); P->UpdateSolution(*State); if ( (Cost - P->GetCost(*State)) > EPSILON ) { status = 0; std::cout<< "Error: the solution \n"<< (*State)<<"changed the costvalue after UpdateSolution!\n"; } } while (status && (i++)StartClock(); SA_Output *output; // Initialisierung InitStates(); //Setzen der Cooling-Parameter Cooler->WarmingUp(*P,*State,*BestSolution); output = GetSolverOutput(); // ShowConfig(); time_t starttime, endtime; // P->GetInitialSolution(*State); starttime = time(NULL); output->OutputInit(); do { output->OutputFrozenNo(); do { output->OutputEquiNo(); P->GetNeighbor(*State,Cooler->GetRelativeT()); output->OutputNeighbor(); if ( Cooler->Accept( P->GetCost(*State) )) { output->OutputAccept(); P->UpdateSolution(*State); if ( Cooler->BestFound() ) P->Copy(*State,*BestSolution); } else { output->OutputNotAccept(); P->ResetSolution(*State); } } while ( ! Cooler->Equilibrium()); output->OutputEquiYes(); if ( VerboseMode ) { // Cooler->OutputSubchainStatistics(std::cout); std::cout <UpdateTemperature(); //MPE_Log_event(4,0,"End UpdateTemperature"); } while (! Cooler->Frozen()); if (Cooler->TimeExceeded()) { std::cout << "TIME LIMIT EXPIRED!!! \n Annealing process stoppt! \n"; } output->OutputFrozenYes(); endtime = time(NULL); SATime = endtime - starttime; PostprocTime = time(NULL); P->Postprocessing(*BestSolution); PostprocTime = time(NULL) - PostprocTime; WriteLog(1); WriteSolution(); return Cooler->GetBestE(); } float SA_SeqSolver::RunAnnealing() {//debug(SA_Solver::RunAnnealing, myrank); std::cout << "RunAnnealing " << Algorithm <<" should be started!\n"; if ( Algorithm == NULL ) { std::cout << "No algorithm specified !\n"; return -1; } else if ( strcmp(Algorithm, "SeqAlg") == 0 ) { return SeqSimAnn(); } else if ( strcmp(Algorithm, "TestProblem") == 0 ) { return TestProblemImplementation(); } else { std::cout << "Unknown algorithm "<