X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=src%2Fgenesorter.cpp;h=d73f176eb01465b2b717283b59015167f921cc7a;hb=79451fe868d3d11c0294383a2ddab95b40a0a4ce;hp=40e62e5b8bab62ae58e55fdc85f0c2f7a486380e;hpb=3fff88637ec7e806f19e613051f31ca5178d2cf5;p=germs.git diff --git a/src/genesorter.cpp b/src/genesorter.cpp index 40e62e5..d73f176 100644 --- a/src/genesorter.cpp +++ b/src/genesorter.cpp @@ -26,16 +26,68 @@ #include "genealgorithms.h" +#include +#include +#include using namespace std; GeneSorter::ActionList GeneSorter::sort(const GeneOrder& go){ - return ActionList(); + ActionList al; + GeneOrder temp(go); + while(inversionDistance(temp) > 0){ + cout << "Distance: " << inversionDistance(temp) << " : "; + copy(temp.begin(), temp.end(), ostream_iterator(cout, " ")); + cout << endl; + ActionList safe = safeActions(temp); + if (safe.size() > 0){ + safe[0](temp); + al.push_back(safe[0]); + }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) return ActionList(); ActionList al; - al.push_back(SortAction(new ReverseAction(2,3))); + vector intervals = findIntervals(go); + priority_queue,vector >, ScoreCmp > pq; + for (size_t i = 0; i < intervals.size(); ++i){ + if (intervals[i].oriented && intervals[i].first != intervals[i].second){ + SortAction sa(new ReverseAction(intervals[i])); + size_t score = scoreAction(go,sa); + cout << "Inversion: " << min(intervals[i].first,intervals[i].second) << ":" << max(intervals[i].first,intervals[i].second)-1 << " Score: " << score << endl; + pq.push(pair(score,sa)); + } + } + while (pq.size() > 0){ + al.push_back(pq.top().second); + pq.pop(); + } return al; } + +size_t GeneSorter::scoreAction(const GeneOrder& go, SortAction& sa){ + GeneOrder temp(go); + sa(temp); + vector intervals = findIntervals(temp); + int o = 0; + for (vector::iterator in = intervals.begin(); in != intervals.end(); ++in){ + if (in->oriented) + ++o; + } + return o; +} +