]> ruin.nu Git - germs.git/commitdiff
Added progress information for the search threads
authorMichael Andreen <harv@ruin.nu>
Thu, 30 Oct 2008 21:13:44 +0000 (22:13 +0100)
committerMichael Andreen <harv@ruin.nu>
Thu, 30 Oct 2008 21:13:44 +0000 (22:13 +0100)
src/main.cpp
src/threadgenesorter.cpp
src/threadgenesorter.h

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;
index 2276a2fdd271f05a04ae286831fb30ce9d0a20fc..d4fe06995d2d850298cb011346c3700668d88dce 100644 (file)
@@ -23,6 +23,9 @@
 
 #include "genealgorithms.h"
 
+#include <sys/time.h>
+#include <errno.h>
+
 #include <set>
 using namespace std;
 
@@ -72,15 +75,41 @@ void ThreadGeneSorter::join(){
 }
 
 void ThreadGeneSorter::stop(){
+       Mutex m(&_queuelock);
        _done = true;
+       pthread_cond_signal(&_waiting);
 }
 
 size_t ThreadGeneSorter::size(){
-       //TODO: thread safety..
+       Mutex m(&_solutionslock);
        return _solutions.size();
 }
 
-double progress(int time, int solutions = 1){
+bool ThreadGeneSorter::running(){
+       return !_done;
+}
+
+double ThreadGeneSorter::wait(time_t time, size_t solutions){
+
+       timeval now;
+       gettimeofday(&now, NULL);
+       timespec timeout;
+       timeout.tv_sec = now.tv_sec + time;
+       timeout.tv_nsec = 0;
+
+       int err = 0;
+
+       Mutex m(&_solutionslock);
+       size_t n = _solutions.size();
+
+       while (!_done && err != ETIMEDOUT
+                       && _solutions.size() - n < solutions){
+               err = pthread_cond_timedwait(&_addedSolution,&_solutionslock
+                       , &timeout);
+       }
+       if (_solutions.size() == 0)
+               return 0.0;
+       return _solutions.top().first;
 }
 
 ThreadGeneSorter::SolutionsQueue ThreadGeneSorter::solutions(){
index 57d10520611b4e702f53860ffff8364d74d0ff98..b002aa1b8dd68d117527dad0fb93529d762b7984 100644 (file)
@@ -81,6 +81,11 @@ class ThreadGeneSorter : public GeneSorter{
                 */
                void join();
 
+               /**
+                * Returns true while the sorter is running.
+                */
+               bool running();
+
                /**
                 * Tell the sorter to stop execution.
                 */
@@ -94,10 +99,11 @@ class ThreadGeneSorter : public GeneSorter{
                /**
                 * Waits for progress.
                 *
-                * \param time Max time to wait for.
+                * \param time Max time to wait for, in seconds.
                 * \param solutions The minimum number of solutions we'll wait for.
+                * \return The best score so far.
                 */
-               double progress(int time, int solutions = 1);
+               double wait(time_t time, size_t solutions = 1);
 
                /**
                 * Get the full list of solutions.