X-Git-Url: https://ruin.nu/git/?a=blobdiff_plain;f=src%2Fgenesorter.cpp;h=3198543fae391f4805211406a5f8f76ccf28c889;hb=HEAD;hp=1e199843a1a5ecedcbf692fc6da5117dafa4ce8e;hpb=3401199b1ce268dd6465f09abf6264592b989140;p=germs.git diff --git a/src/genesorter.cpp b/src/genesorter.cpp index 1e19984..3198543 100644 --- a/src/genesorter.cpp +++ b/src/genesorter.cpp @@ -26,16 +26,74 @@ #include "genealgorithms.h" +#include "model.h" + +#include using namespace std; -GeneSorter::ActionList GeneSorter::sort(const GeneOrder& go){ - return ActionList(); +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); + size_t dist = inversionDistance(temp); + while(dist > 0){ + //cout << "Distance: " << inversionDistance(temp) << " : "; + //copy(temp.begin(), temp.end(), ostream_iterator(cout, " ")); + //cout << endl; + ActionList safe = safeActions(temp); + if (safe.size() > 0){ + priority_queue,vector >, ScoreCmp > pq; + for (ActionList::iterator sa = safe.begin(); sa != safe.end(); ++sa){ + pq.push(pair(m.score(*sa,temp),*sa)); + } + SortAction sa = pq.top().second; + sa(temp); + al.push_back(sa); + --dist; + }else + return ActionList(); //TODO: Need to handle hurdles. + } + return al; } + GeneSorter::ActionList GeneSorter::safeActions(const GeneOrder& go){ if (countCycles(go) == go.size() - 1) return ActionList(); ActionList al; - al.push_back(ActionPointer(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)); + } + } + size_t max = pq.top().first; + while (pq.size() > 0 && pq.top().first >= max){ + 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; +} +