X-Git-Url: https://ruin.nu/git/?p=germs.git;a=blobdiff_plain;f=src%2Fgenesorter.cpp;h=602a6dde276318e069fa8e1496c005110e9b27d4;hp=3ce89ad493d6163ee568a0f4e3697a1face13946;hb=efe42ca948704593c847996d0ae8da71d15bb75b;hpb=f8575ea6c0982bba5ccf42771b2994d19500c0a3 diff --git a/src/genesorter.cpp b/src/genesorter.cpp index 3ce89ad..602a6dd 100644 --- a/src/genesorter.cpp +++ b/src/genesorter.cpp @@ -26,12 +26,19 @@ #include "genealgorithms.h" +#include "model.h" + #include -#include -#include using namespace std; -GeneSorter::ActionList GeneSorter::sort(const GeneOrder& go){ +struct ScoreCmp { + template + bool operator()(T s1, T s2){ + return s1.first < s2.first; + } +}; + +GeneSorter::ActionList GeneSorter::sort(const GeneOrder& go, const Model& m){ ActionList al; GeneOrder temp(go); while(inversionDistance(temp) > 0){ @@ -40,24 +47,19 @@ GeneSorter::ActionList GeneSorter::sort(const GeneOrder& go){ //cout << endl; ActionList safe = safeActions(temp); if (safe.size() > 0){ - safe[0](temp); - cout << "Action: " << safe[0].toString() << endl; - al.push_back(safe[0]); + priority_queue,vector >, ScoreCmp > pq; + for (ActionList::iterator sa = safe.begin(); sa != safe.end(); ++sa){ + pq.push(pair(m.score(*sa),*sa)); + } + SortAction sa = pq.top().second; + sa(temp); + al.push_back(sa); }else return ActionList(); //TODO: Need to handle hurdles. } - cout << "Distance: " << inversionDistance(temp) << " : "; - copy(temp.begin(), temp.end(), ostream_iterator(cout, " ")); - cout << endl; return al; } -struct ScoreCmp { - template - bool operator()(T s1, T s2){ - return s1.first < s2.first; - } -}; GeneSorter::ActionList GeneSorter::safeActions(const GeneOrder& go){ if (countCycles(go) == go.size() - 1) @@ -73,7 +75,8 @@ GeneSorter::ActionList GeneSorter::safeActions(const GeneOrder& go){ pq.push(pair(score,sa)); } } - while (pq.size() > 0){ + size_t max = pq.top().first; + while (pq.size() > 0 && pq.top().first >= max){ al.push_back(pq.top().second); pq.pop(); }