]> ruin.nu Git - germs.git/blobdiff - src/main.cpp
Basic implementation of threaded sorting
[germs.git] / src / main.cpp
index abd520f43a29f7a3ec9b4cb7cb8f893895f16cbc..85569e1c295dfa614245be5e6c759f534d1cd525 100644 (file)
@@ -3,6 +3,7 @@
 #include <queue>
 #include <iterator>
 #include <fstream>
+#include <cstdlib>
 
 using namespace std;
 
@@ -11,6 +12,7 @@ using namespace std;
 #include "geneorder.h"
 #include "modelidentifier.h"
 #include "genesorter.h"
+#include "threadgenesorter.h"
 #include "sortaction.h"
 #include "genealgorithms.h"
 #include "model.h"
@@ -25,7 +27,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 +37,9 @@ int main(int argc, char** argv){
                        case 'n':
                                ann = optarg;
                                break;
+                       case 't':
+                               threads = atoi(optarg);
+                               break;
                        case 'i':
                                onlyIdentify = true;
                                break;
@@ -97,25 +103,46 @@ int main(int argc, char** argv){
        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<int>(cout, " "));
-                       cout << endl;
+       if (threads){
+               ThreadGeneSorter so(model, threads);
+               so.start(go);
+               so.join();
+
+               ThreadGeneSorter::SolutionsQueue solutions = so.solutions();
+
+               while (solutions.size() > 0){
+                       pair<double,GeneSorter::ActionList> 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 << 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<int>(cout, " "));
+                               cout << endl;
+                       }
                }
+               cout << "Avg score: " << score / al.size() << endl;
        }
-       cout << "Avg score: " << score / al.size() << endl;
 
        return EXIT_SUCCESS;
 }