#include "genealgorithms.h"
+#include "model.h"
+
#include <queue>
-#include <iostream>
-#include <iterator>
using namespace std;
-GeneSorter::ActionList GeneSorter::sort(const GeneOrder& go){
+struct ScoreCmp {
+ template<typename T>
+ 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){
//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<pair<double,SortAction>,vector<pair<double,SortAction> >, ScoreCmp > pq;
+ for (ActionList::iterator sa = safe.begin(); sa != safe.end(); ++sa){
+ pq.push(pair<double,SortAction>(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<int>(cout, " "));
- cout << endl;
return al;
}
-struct ScoreCmp {
- template<typename T>
- bool operator()(T s1, T s2){
- return s1.first < s2.first;
- }
-};
GeneSorter::ActionList GeneSorter::safeActions(const GeneOrder& go){
if (countCycles(go) == go.size() - 1)
pq.push(pair<size_t,SortAction>(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();
}
class SortAction;
class GeneOrder;
+class Model;
/**
* Sorts genes
* Takes a GeneOrder, finds the actions to transform it into a sorted
* permutation and returns the list with required actions.
*/
- ActionList sort(const GeneOrder& go1);
+ ActionList sort(const GeneOrder& go1, const Model& m);
/**
* Find the safe actions for this GeneOrder.
#include "modelidentifier.h"
#include "genesorter.h"
#include "sortaction.h"
+#include "genealgorithms.h"
#include "model.h"
int main(int argc, char** argv){
//TODO: Identify
ModelIdentifier mi(ann);
priority_queue<pair<double,Model> > pq = mi.identify(go);
- while (pq.size() > 0){
- cout << "Model: " << pq.top().second.name() << " score: " << pq.top().first << endl;
- pq.pop();
- }
+ Model model = pq.top().second;
+ //while (pq.size() > 0){
+ cout << "Model: " << model.name() << " score: " << pq.top().first << endl;
+ //pq.pop();
+ //}
+
+ cout << "Distance: " << inversionDistance(go) << " : ";
+ copy(go.begin(), go.end(), ostream_iterator<int>(cout, " "));
+ cout << endl;
//TODO: Chose a sorter
GeneSorter so;
//TODO: Sort
- GeneSorter::ActionList al = so.sort(go);
+ GeneSorter::ActionList al = so.sort(go,model);
+
+ for (GeneSorter::ActionList::iterator sa = al.begin(); sa != al.end(); ++sa){
+ cout << "Action: " << sa->toString() << " model score: " << model.score(*sa) << endl;
+ }
//TODO: Print result
return EXIT_SUCCESS;
}
***************************************************************************/
#include "models.h"
+#include "reverseaction.h"
using namespace std;
#include "sortaction.h"
#include "genealgorithms.h"
+#include "geneorder.h"
#include <algorithm>
#include <sstream>
*
* \author Michael Andreen
*/
-class ReverseAction : public SortAction{
+class ReverseAction : public SortActionImpl{
public:
/**
* Creates a new reverse action for the interval [i,j]
*/
- ReverseAction(size_t i, size_t j): SortAction(0),_i(i),_j(j){
+ ReverseAction(size_t i, size_t j): _i(i),_j(j){
}
- ReverseAction(Interval i): SortAction(0){
+
+ ReverseAction(Interval i){
_i = std::min(i.first,i.second);
_j = std::max(i.first,i.second)-1;
}
return go;
}
- virtual bool operator==(const SortAction& sa) const{
+ virtual bool operator==(const SortActionImpl& sa) const{
if (const ReverseAction* psa = dynamic_cast<const ReverseAction*>(&sa)){
if (_i == psa->_i && _j == psa->_j)
return true;
#include <tr1/memory>
#include <string>
class GeneOrder;
+
+
+class SortActionImpl{
+ public:
+
+ virtual ~SortActionImpl(){};
+
+ /**
+ * Applies the action on the GeneOrder and returning it.
+ */
+ virtual GeneOrder& operator()(GeneOrder& go) const = 0;
+
+ /**
+ * Compares sort actions.
+ */
+ virtual bool operator==(const SortActionImpl& sa) const = 0;
+
+ /**
+ * Gives a string representation of the action, for output
+ */
+ virtual std::string toString() const = 0;
+};
+
/**
* Abstraction of a sort action, all child actions has to be immutable.
*
*/
class SortAction{
public:
- typedef std::tr1::shared_ptr<SortAction> ActionPointer;
+ typedef std::tr1::shared_ptr<SortActionImpl> ActionPointer;
/**
* Creates a new sort action, given a specified action.
* SortAction promises to remove the given action.
*/
- SortAction(SortAction* sa): _action(sa){
+ SortAction(SortActionImpl* sa): _action(sa){
}
SortAction(const SortAction& sa): _action(sa._action){
}
- virtual const SortAction& operator=(const SortAction& sa){
+ const SortAction& operator=(const SortAction& sa){
if (this != &sa)
_action = sa._action;
return *this;
}
- virtual ~SortAction(){};
+ ~SortAction(){};
/**
* Applies the action on the GeneOrder and returning it.
*/
- virtual GeneOrder& operator()(GeneOrder& go) const{
+ GeneOrder& operator()(GeneOrder& go) const{
return (*_action)(go);
}
/**
* Compares sort actions.
*/
- virtual bool operator==(const SortAction& sa) const{
- return (*_action) == (sa._action.get() == 0 ? sa : *sa._action);
+ bool operator==(const SortAction& sa) const{
+ return (*_action) == (*sa._action);
+ }
+
+ /**
+ * Compares sort actions.
+ */
+ bool operator==(const SortActionImpl& sa) const{
+ return (*_action) == sa;
}
/**
* Gives a string representation of the action, for output
*/
- virtual std::string toString() const{
+ std::string toString() const{
return _action->toString();
}
private:
#include <geneorder.h>
#include <genealgorithms.h>
#include <reverseaction.h>
+#include <models.h>
+#include <model.h>
#include <algorithm>
#include <iterator>
void testSort (){
GeneSorter so;
GeneOrder go(_validPerm.begin(),_validPerm.end());
- GeneSorter::ActionList al = so.sort(go);
+ GeneSorter::ActionList al = so.sort(go,Model(new Models::ModelImpl));
CPPUNIT_ASSERT_EQUAL((size_t)0u,al.size());
GeneOrder go2(_validPerm2.begin(),_validPerm2.end());
- al = so.sort(go2);
+ al = so.sort(go2,Model(new Models::ModelImpl));
CPPUNIT_ASSERT_EQUAL((size_t)1u,al.size());
CPPUNIT_ASSERT(al[0] == ReverseAction(2,3));
CPPUNIT_ASSERT(equal(go.begin(),go.end(),go2.begin()));
GeneOrder go3(_validPerm3.begin(),_validPerm3.end());
- al = so.sort(go3);
+ al = so.sort(go3,Model(new Models::ModelImpl));
CPPUNIT_ASSERT_EQUAL((size_t)5u,al.size());
for (size_t i = 0; i < al.size(); ++i)
al[i](go3);
CPPUNIT_ASSERT(equal(go3.begin(),go3.end(),perm));
GeneOrder go4(_validPerm4.begin(),_validPerm4.end());
- al = so.sort(go4);
+ al = so.sort(go4,Model(new Models::ModelImpl));
CPPUNIT_ASSERT_EQUAL((size_t)13u,al.size());
for (size_t i = 0; i < al.size(); ++i)
al[i](go3);