1 #if !defined(_TESTER_HH_)
20 using namespace EasyLocal::Core;
22 template <
class Input,
class State,
typename CFtype>
27 int ReadChoice(std::istream& is);
30 template <
class Input,
class State,
typename CFtype>
41 std::istringstream iss(c);
44 throw std::invalid_argument(
"Not an integer value");
48 catch (std::invalid_argument)
55 template <
class Input,
class State,
typename CFtype>
65 AddSearchEngine(*p_r);
74 template <
class Input,
class Output,
class State,
typename CFtype =
int>
84 void RunMainMenu(std::string file_name =
"");
88 void RunStateTestMenu();
89 void SetState(
const State& st){test_state = st;}
93 void ShowReducedStateMenu();
94 bool ExecuteStateChoice();
97 void ShowKickersMenu();
99 void ShowSolverMenu();
100 void ShowDebuggingMenu();
101 void ExecuteMainChoice();
102 void ExecuteMovesChoice();
103 void ExecuteKickersChoice();
104 void ExecuteRunChoice();
105 void ExecuteSolverChoice();
106 void ExecuteDebuggingMenu();
107 std::vector<ComponentTester<Input, Output, State, CFtype>* >
move_testers;
134 template <
class Input,
class Output,
class State,
typename CFtype>
138 : in(i), os(o), sm(e_sm), om(e_om),
139 test_state(i), out(i)
148 template <
class Input,
class Output,
class State,
typename CFtype>
150 { move_testers.push_back(&amt); }
157 template <
class Input,
class Output,
class State,
typename CFtype>
159 { kicker_testers.push_back(&kt); }
166 template <
class Input,
class Output,
class State,
typename CFtype>
169 searchengines.push_back(&r);
176 template <
class Input,
class Output,
class State,
typename CFtype>
183 else if (file_name ==
"random")
185 this->sm.RandomState(test_state);
189 std::ifstream is(file_name.c_str());
191 throw std::runtime_error(
"Cannot open file!");
192 om.ReadState(test_state, is);
193 om.OutputState(test_state, out);
194 os <<
"SOLUTION IMPORTED " << std::endl << out << std::endl;
195 os <<
"IMPORTED SOLUTION COST : " << sm.CostFunctionComponents(test_state) << std::endl;
201 if (this->choice != 0)
204 while (this->choice != 0);
205 os <<
"Bye bye..." << std::endl;
211 template <
class Input,
class Output,
class State,
typename CFtype>
214 os <<
"MAIN MENU:" << std::endl
215 <<
" (1) Move menu" << std::endl
216 <<
" (2) Kicker menu" << std::endl
217 <<
" (3) Run menu" << std::endl
218 <<
" (4) State menu" << std::endl
219 <<
" (0) Exit" << std::endl
221 this->choice = this->ReadChoice(std::cin);
227 template <
class Input,
class Output,
class State,
typename CFtype>
234 ExecuteMovesChoice();
238 ExecuteKickersChoice();
250 os <<
"Invalid choice" << std::endl;
257 template <
class Input,
class Output,
class State,
typename CFtype>
261 os <<
"MOVE MENU: " << std::endl;
262 for (i = 0; i < move_testers.size(); i++)
263 os <<
" (" << i+1 <<
") " << move_testers[i]->name <<
" [" << move_testers[i]->Modality() <<
"-modal]" << std::endl;
264 os <<
" (0) Return to Main Menu" << std::endl;
265 os <<
" Your choice: ";
266 this->sub_choice = this->ReadChoice(std::cin);
270 template <
class Input,
class Output,
class State,
typename CFtype>
273 os <<
"SOLVER MENU: " << std::endl;
274 os <<
" (1) Simple solver" << std::endl;
275 os <<
" (2) Token ring solver" << std::endl;
276 os <<
" (0) Return to Main Menu" << std::endl;
277 os <<
" Your choice: ";
278 this->sub_choice = this->ReadChoice(std::cin);
284 template <
class Input,
class Output,
class State,
typename CFtype>
288 os <<
"KICK MENU: " << std::endl;
289 for (i = 0; i < kicker_testers.size(); i++)
290 os <<
" (" << i+1 <<
") " << kicker_testers[i]->name << std::endl;
291 os <<
" (0) Return to Main Menu" << std::endl;
292 os <<
" Your choice: ";
293 this->sub_choice = this->ReadChoice(std::cin);
299 template <
class Input,
class Output,
class State,
typename CFtype>
305 os <<
"RUN MENU: " << std::endl;
306 for (i = 0; i < searchengines.size(); i++)
307 os <<
" (" << (i + 1) <<
") " << searchengines[i]->name << std::endl;
308 os <<
" (0) Return to Main Menu" << std::endl;
309 os <<
" Your choice: ";
310 this->sub_choice = this->ReadChoice(std::cin);
311 if (sub_choice == -1 || sub_choice >= static_cast<int>(searchengines.size()))
312 os <<
"Invalid choice" << std::endl;
314 while (sub_choice == -1 || sub_choice > static_cast<int>(searchengines.size()));
320 template <
class Input,
class Output,
class State,
typename CFtype>
323 if (sub_choice > 0 && sub_choice <= static_cast<int>(move_testers.size()))
324 move_testers[sub_choice-1]->RunMainMenu(test_state);
330 template <
class Input,
class Output,
class State,
typename CFtype>
334 kicker_testers[sub_choice-1]->RunMainMenu(test_state);
340 template <
class Input,
class Output,
class State,
typename CFtype>
353 auto to = std::chrono::milliseconds((
long long)(timeout * 1000));
355 auto start = std::chrono::high_resolution_clock::now();
357 auto end = std::chrono::high_resolution_clock::now();
359 auto duration = end - start;
360 om.OutputState(test_state, out);
362 os <<
"CURRENT SOLUTION " << std::endl << out << std::endl;
363 os <<
"CURRENT COST : " << result << std::endl;
364 os <<
"ELAPSED TIME : " << std::chrono::duration_cast<std::chrono::milliseconds>(duration).count() / 1000.0 <<
" s" << std::endl;
365 os <<
"NUMBER OF ITERATIONS : " << r.
Iteration() << std::endl;
372 template <
class Input,
class Output,
class State,
typename CFtype>
376 ShowReducedStateMenu();
377 std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
378 show_state = ExecuteStateChoice();
379 std::chrono::milliseconds duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start);
382 this->om.OutputState(test_state, this->out);
383 os <<
"INITIAL SOLUTION " << std::endl << this->out << std::endl;
384 os <<
"INITIAL COST : " << this->sm.CostFunctionComponents(test_state) << std::endl;
386 os <<
"ELAPSED TIME : " << duration.count() / 1000.0 <<
"s" << std::endl;
392 template <
class Input,
class Output,
class State,
typename CFtype>
395 os <<
"STATE MENU: " << std::endl
396 <<
" (1) Random state " << std::endl
397 <<
" (2) Read from file" << std::endl
398 <<
" (3) Greedy state " << std::endl
399 <<
" (4) Sample state" << std::endl
400 <<
" (5) Write to file" << std::endl
401 <<
" (6) Show state" << std::endl
402 <<
" (7) Show input" << std::endl
403 <<
" (8) Show cost function components" << std::endl
404 <<
" (9) Show cost elements" << std::endl
405 <<
" (10) Check state consistency" << std::endl
406 <<
" (11) Pretty print output" << std::endl
407 <<
" (0) Return to Main Menu" << std::endl
409 this->sub_choice = this->ReadChoice(std::cin);
415 template <
class Input,
class Output,
class State,
typename CFtype>
418 os <<
"INITIAL STATE MENU: " << std::endl
419 <<
" (1) Random state " << std::endl
420 <<
" (2) Read from file" << std::endl
421 <<
" (3) Greedy state " << std::endl
423 this->sub_choice = this->ReadChoice(std::cin);
433 template <
class Input,
class Output,
class State,
typename CFtype>
437 std::string file_name;
441 this->sm.RandomState(test_state);
450 os <<
"File name : ";
451 std::cin >> file_name;
452 is.open(file_name.c_str());
455 os <<
"File " << file_name <<
" does not exist!" << std::endl;
461 this->om.ReadState(test_state, is);
468 os <<
"Lenght of the restricted candidate list: ";
470 os <<
"Level of randomness (0 <= alpha <= 1): ";
471 std::cin >> randomness;
472 this->sm.GreedyState(test_state, randomness, lenght);
477 unsigned int samples;
478 os <<
"How many samples : ";
480 this->sm.SampleState(test_state, samples);
485 os <<
"File name : ";
486 std::cin >> file_name;
487 std::ofstream os(file_name.c_str());
488 this->om.WriteState(test_state, os);
493 os << test_state << std::endl;
494 os <<
"Total cost: " << this->sm.CostFunctionComponents(test_state) << std::endl;
504 os <<
"Cost Components: " << std::endl;
506 for (i = 0; i < CostComponent<Input, State, CFtype>::CostComponents(); i++)
509 os << i <<
". " << cc.
name <<
" : "
512 os <<
"Total Violations: " << cost.
violations << std::endl;
513 os <<
"Total Objective: " << cost.
objective << std::endl;
514 os <<
"Total Cost: " << cost.
total << std::endl;
519 os <<
"Detailed Violations: " << std::endl;
521 for (i = 0; i < CostComponent<Input, State, CFtype>::CostComponents(); i++)
526 os << std::endl <<
"Summary of Cost Components: " << std::endl;
527 for (i = 0; i < CostComponent<Input, State, CFtype>::CostComponents(); i++)
530 os << i <<
". " << cc.
name <<
" : "
533 os <<
"Total Violations: " << cost.
violations << std::endl;
534 os <<
"Total Objective: " << cost.
objective << std::endl;
535 os <<
"Total Cost: " << cost.
total << std::endl;
540 os <<
"Checking state consistency: " << std::endl;
541 bool consistent = this->sm.CheckConsistency(test_state);
543 os <<
"The state is consistent" << std::endl;
545 os <<
"The state is not consistent" << std::endl;
550 os <<
"File name : ";
551 std::cin >> file_name;
552 this->om.PrettyPrintOutput(test_state, file_name);
553 std::cout <<
"Output pretty-printed in file " << file_name << std::endl;
557 os <<
"Invalid choice" << std::endl;
559 return (sub_choice >= 1 && sub_choice <= 4);
566 template <
class Input,
class Output,
class State,
typename CFtype>
575 std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
576 show_state = ExecuteStateChoice();
577 std::chrono::milliseconds duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - start);
580 om.OutputState(test_state, out);
581 os <<
"CURRENT SOLUTION " << std::endl << out << std::endl;
582 os <<
"CURRENT COST : " << sm.CostFunctionComponents(test_state) << std::endl;
584 os <<
"ELAPSED TIME : " << duration.count() / 1000.0 <<
"s" << std::endl;
587 while (sub_choice != 0);
588 os <<
"Leaving state menu" << std::endl;
594 #endif // _TESTER_HH_
Tester(const Input &in, StateManager< Input, State, CFtype > &e_sm, Core::OutputManager< Input, Output, State, CFtype > &e_om, std::ostream &o=std::cout)
void AddKickerTester(ComponentTester< Input, Output, State, CFtype > &kt)
CostStructure< CFtype > SyncRun(std::chrono::milliseconds timeout, Args...args)
void AddMoveTester(ComponentTester< Input, Output, State, CFtype > &amt)
std::vector< CFtype > all_components
virtual void ReadParameters(std::istream &is=std::cin, std::ostream &os=std::cout)
bool ExecuteStateChoice()
void RunMainMenu(std::string file_name="")
Core::OutputManager< Input, Output, State, CFtype > & om
virtual ~AbstractTester()
void AddSearchEngine(Core::SearchEngine< Input, State, CFtype > &r)
std::vector< ComponentTester< Input, Output, State, CFtype > * > kicker_testers
int ReadChoice(std::istream &is)
unsigned long int Iteration() const
void SetState(const State &st)
Core::StateManager< Input, State, CFtype > & sm
The class CostComponent manages one single component of the cost, either hard or soft.
void ExecuteMovesChoice()
void ShowReducedStateMenu()
std::vector< Core::SearchEngine< Input, State, CFtype > * > searchengines
std::vector< ComponentTester< Input, Output, State, CFtype > * > move_testers
virtual void AddSearchEngine(Core::SearchEngine< Input, State, CFtype > &r)
This component is responsible for all operations on the state which are independent of the neighborho...
static const CostComponent< Input, State, CFtype > & Component(size_t i)
virtual void PrintViolations(const State &st, std::ostream &os=std::cout) const =0
void ExecuteKickersChoice()