X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=src%2Fmain.cpp;h=9b39eb27d67d7f619608cf90e71aedadd5171157;hb=refs%2Fheads%2Fthreads;hp=abd520f43a29f7a3ec9b4cb7cb8f893895f16cbc;hpb=69cfdf79b7597ee6133dc3add1a759bc94861fa6;p=germs.git diff --git a/src/main.cpp b/src/main.cpp index abd520f..9b39eb2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,20 +3,37 @@ #include #include #include +#include using namespace std; #include +#include #include "geneorder.h" #include "modelidentifier.h" #include "genesorter.h" +#include "threadgenesorter.h" #include "sortaction.h" #include "genealgorithms.h" #include "model.h" +static bool g_cancel = false; + +void handler(int signal) +{ + + if (signal == SIGINT){ + cerr << "Interrupted" << endl; + g_cancel = true; + } + + /* signal(SIGINT, handler); */ +} + int main(int argc, char** argv){ + signal(SIGINT, handler); string ann = "default.ann"; Model model(0); bool detectModel = true; @@ -25,7 +42,8 @@ int main(int argc, char** argv){ //Parse command line arguments int opt; - while ((opt = getopt(argc, argv, "im:n:hp")) != -1) { + int threads = 0; + while ((opt = getopt(argc, argv, "im:n:t:hp")) != -1) { switch (opt) { case 'm': model = Model::modelFactory(optarg); @@ -34,6 +52,9 @@ int main(int argc, char** argv){ case 'n': ann = optarg; break; + case 't': + threads = atoi(optarg); + break; case 'i': onlyIdentify = true; break; @@ -90,32 +111,62 @@ int main(int argc, char** argv){ } cout << "Using model: " << model.name() << endl; - cout << "Distance: " << inversionDistance(go) << endl; + size_t dist = inversionDistance(go); + cout << "Distance: " << dist << endl; //copy(go.begin(), go.end(), ostream_iterator(cout, " ")); //cout << endl; if (onlyIdentify){ return EXIT_SUCCESS; } - //Sort - GeneSorter so; - GeneSorter::ActionList al = so.sort(go,model); - - //Print the result - double score = 0; - - GeneOrder temp(go); - for (GeneSorter::ActionList::iterator sa = al.begin(); sa != al.end(); ++sa){ - cout << "Action: " << sa->toString() << " model score: " << model.score(*sa,temp) << endl; - (*sa)(temp); - score += model.score(*sa,temp); - - if (printPerm){ - copy(temp.begin(), temp.end(), ostream_iterator(cout, " ")); - cout << endl; + if (threads){ + ThreadGeneSorter so(model, threads); + so.start(go); + cout << "Press CTRL+C to stop" << endl; + while (!g_cancel && so.running()){ + double score = so.wait(5,1000); + cout << "\r" << "Solutions: " << so.size() + << " Best score: " << score / dist; + cout.flush(); + } + cout << endl << "DONE: Waiting for threads to stop." << endl; + so.stop(); + so.join(); + + ThreadGeneSorter::SolutionsQueue solutions = so.solutions(); + + for (int i = 0; i < 10 && solutions.size() > 0; ++i){ + pair s = solutions.top(); + solutions.pop(); + + cout << "Actions: "; + for (GeneSorter::ActionList::iterator sa = s.second.begin(); + sa != s.second.end(); ++sa){ + cout << sa->toString() << " "; + } + cout << endl << "Score: " << s.first / dist << endl; + } + }else{ + //Sort + GeneSorter so; + GeneSorter::ActionList al = so.sort(go,model); + + //Print the result + double score = 0; + + GeneOrder temp(go); + for (GeneSorter::ActionList::iterator sa = al.begin(); sa != al.end(); ++sa){ + cout << "Action: " << sa->toString() << " model score: " << model.score(*sa,temp) << endl; + (*sa)(temp); + score += model.score(*sa,temp); + + if (printPerm){ + copy(temp.begin(), temp.end(), ostream_iterator(cout, " ")); + cout << endl; + } } + cout << "Avg score: " << score / al.size() << endl; } - cout << "Avg score: " << score / al.size() << endl; return EXIT_SUCCESS; }