#include <queue>
#include <iterator>
#include <fstream>
+#include <cstdlib>
using namespace std;
#include "geneorder.h"
#include "modelidentifier.h"
#include "genesorter.h"
+#include "threadgenesorter.h"
#include "sortaction.h"
#include "genealgorithms.h"
#include "model.h"
//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);
case 'n':
ann = optarg;
break;
+ case 't':
+ threads = atoi(optarg);
+ break;
case 'i':
onlyIdentify = true;
break;
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;
}
* Compares sort actions.
*/
virtual bool operator==(const SortActionImpl& sa) const = 0;
-
+
+ /**
+ * Compares sort actions.
+ */
+ virtual bool operator<(const SortActionImpl& sa) const = 0;
+
/**
* Gives a string representation of the action, for output
*/
return (*_action) == (*sa._action);
}
+ /**
+ * Compares sort actions.
+ */
+ bool operator<(const SortAction& sa) const{
+ return (*_action) < (*sa._action);
+ }
+
/**
* Compares sort actions.
*/
#include "threadgenesorter.h"
#include "sortaction.h"
+#include "genealgorithms.h"
+
+#include <set>
using namespace std;
/**
}
void ThreadGeneSorter::worker(){
+ try{
while(!_done){
Mutex m(&_queuelock);
SortUnit su = _queue.top();
_queue.pop();
m.unlock();
+
+ size_t dist = inversionDistance(su._go);
+ if (dist == 0){
+ Mutex m(&_solutionslock);
+ _solutions.push(pair<double,ActionList>(su._score,su._al));
+ pthread_cond_broadcast(&_addedSolution);
+ continue;;
+ }
+
+ ActionList act = safeActions(su._go);
+ if (act.size() > 0){
+ set<SortAction> safe(act.begin(), act.end());
+ for (set<SortAction>::iterator sa = safe.begin(); sa != safe.end(); ++sa){
+ GeneOrder go(su._go);
+ ActionList al(su._al);
+ al.push_back(*sa);
+ (*sa)(go);
+ double score = su._score + _model.score(*sa,su._go);
+ Mutex m(&_queuelock);
+ _queue.push(SortUnit(score,go,al));
+ }
+ pthread_cond_broadcast(&_addedTask);
+ } //TODO: Hurdles..
+
+ }
+ }catch(const bad_alloc& e){
+ _done = true;
}
}