]> ruin.nu Git - germs.git/blobdiff - src/main.cpp
Added progress information for the search
[germs.git] / src / main.cpp
index 85569e1c295dfa614245be5e6c759f534d1cd525..9b39eb27d67d7f619608cf90e71aedadd5171157 100644 (file)
@@ -8,6 +8,7 @@
 using namespace std;
 
 #include <unistd.h>
+#include <signal.h>
 
 #include "geneorder.h"
 #include "modelidentifier.h"
@@ -17,8 +18,22 @@ using namespace std;
 #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;
@@ -96,7 +111,8 @@ 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<int>(cout, " "));
        //cout << endl;
 
@@ -106,22 +122,30 @@ int main(int argc, char** argv){
        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();
 
-               while (solutions.size() > 0){
+               for (int i = 0; i < 10 && solutions.size() > 0; ++i){
                        pair<double,GeneSorter::ActionList> s = solutions.top();
                        solutions.pop();
 
                        cout << "Actions: ";
-                       for (GeneSorter::ActionList::iterator sa = s.second.begin(); sa != s.second.end(); ++sa){
+                       for (GeneSorter::ActionList::iterator sa = s.second.begin();
+                                       sa != s.second.end(); ++sa){
                                cout << sa->toString() << " ";
-
                        }
-                       cout << endl << "Score: " << s.first << endl;
+                       cout << endl << "Score: " << s.first / dist << endl;
                }
-               
        }else{
                //Sort
                GeneSorter so;