]> ruin.nu Git - germs.git/blob - src/genesorter.cpp
Small optimization in sort()
[germs.git] / src / genesorter.cpp
1 /***************************************************************************
2  *   Copyright (C) 2006 by Michael Andreen                                 *
3  *   andreen@student.chalmers.se                                           *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA          *
19  ***************************************************************************/
20
21 #include "genesorter.h"
22
23 #include "geneorder.h"
24 #include "sortaction.h"
25 #include "reverseaction.h"
26
27 #include "genealgorithms.h"
28
29 #include "model.h"
30
31 #include <queue>
32 using namespace std;
33
34 struct ScoreCmp {
35         template<typename T>
36         bool operator()(T s1, T s2){
37                 return s1.first < s2.first;
38         }
39 };
40
41 GeneSorter::ActionList GeneSorter::sort(const GeneOrder& go, const Model& m){
42         ActionList al;
43         GeneOrder temp(go);
44         size_t dist = inversionDistance(temp);
45         while(dist > 0){
46                 //cout << "Distance: " << inversionDistance(temp) << " : ";
47                 //copy(temp.begin(), temp.end(), ostream_iterator<int>(cout, " "));
48                 //cout << endl;
49                 ActionList safe = safeActions(temp);
50                 if (safe.size() > 0){
51                         priority_queue<pair<double,SortAction>,vector<pair<double,SortAction> >, ScoreCmp > pq;
52                         for (ActionList::iterator sa = safe.begin(); sa != safe.end(); ++sa){
53                                 pq.push(pair<double,SortAction>(m.score(*sa,temp),*sa));
54                         }
55                         SortAction sa = pq.top().second;
56                         sa(temp);
57                         al.push_back(sa);
58                         --dist;
59                 }else
60                         return ActionList(); //TODO: Need to handle hurdles.
61         }
62         return al;
63 }
64
65
66 GeneSorter::ActionList GeneSorter::safeActions(const GeneOrder& go){
67         if (countCycles(go) == go.size() - 1)
68                 return ActionList();
69         ActionList al;
70         vector<Interval> intervals = findIntervals(go);
71         priority_queue<pair<size_t,SortAction>,vector<pair<size_t,SortAction> >, ScoreCmp > pq;
72         for (size_t i = 0; i < intervals.size(); ++i){
73                 if (intervals[i].oriented && intervals[i].first != intervals[i].second){
74                         SortAction sa(new ReverseAction(intervals[i]));
75                         size_t score = scoreAction(go,sa);
76                         //cout << "Inversion: " << min(intervals[i].first,intervals[i].second) << ":" << max(intervals[i].first,intervals[i].second)-1 << " Score: " << score <<  endl;
77                         pq.push(pair<size_t,SortAction>(score,sa));
78                 }                               
79         }
80         size_t max = pq.top().first;
81         while (pq.size() > 0 && pq.top().first >= max){
82                 al.push_back(pq.top().second);
83                 pq.pop();
84         }
85         return al;
86 }
87
88 size_t GeneSorter::scoreAction(const GeneOrder& go, SortAction& sa){
89         GeneOrder temp(go);
90         sa(temp);
91         vector<Interval> intervals = findIntervals(temp);
92         int o = 0;
93         for (vector<Interval>::iterator in = intervals.begin(); in != intervals.end(); ++in){
94                 if (in->oriented)
95                         ++o;
96         }
97         return o;
98 }
99