ADD_DEFINITIONS(-Wall -g)
INCLUDE_DIRECTORIES(.)
-ADD_LIBRARY(GeneSort geneorder genealgorithms modelidentifier)
+ADD_LIBRARY(GeneSort geneorder genealgorithms modelidentifier genesorter)
ADD_EXECUTABLE(genesort main.cpp)
SET(GENELIBS doublefann GeneSort)
--- /dev/null
+/***************************************************************************
+ * Copyright (C) 2006 by Michael Andreen *
+ * andreen@student.chalmers.se *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
+ ***************************************************************************/
+
+#include "genesorter.h"
+
+#include "geneorder.h"
+#include "sortaction.h"
+#include "reverseaction.h"
+
+using namespace std;
+
+GeneSorter::ActionList GeneSorter::sort(const GeneOrder& go1){
+ return ActionList();
+}
+
+GeneSorter::ActionList GeneSorter::safeActions(const GeneOrder& go1){
+ return ActionList();
+}
#ifndef __GENESORTER_H__
#define __GENESORTER_H__
-#include "geneorder.h"
-#include "sortaction.h"
-
#include <vector>
+class SortAction;
+class GeneOrder;
/**
- * Abstract baseclass for different gene sorters.
+ * Sorts genes
+ *
* \author Michael Andreen
*/
class GeneSorter{
* Takes a GeneOrder, finds the actions to transform it into a sorted
* permutation and returns the list with required actions.
*/
- virtual ActionList sort(const GeneOrder& go1) = 0;
+ ActionList sort(const GeneOrder& go1);
/**
* Find the safe actions for this GeneOrder.
*/
- virtual ActionList safeActions(const GeneOrder& go1) = 0;
+ ActionList safeActions(const GeneOrder& go1);
- virtual ~GeneSorter(){};
+ ~GeneSorter(){};
};
#endif
--- /dev/null
+/***************************************************************************
+ * Copyright (C) 2006 by Michael Andreen *
+ * andreen@student.chalmers.se *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
+ ***************************************************************************/
+
+#ifndef __REVERSEACTION_H__
+#define __REVERSEACTION_H__
+
+#include "sortaction.h"
+/**
+ * Reverses an interval
+ *
+ * \author Michael Andreen
+ */
+class ReverseAction : public SortAction{
+ public:
+
+ /**
+ * Creates a new reverse action for the interval [i,j]
+ */
+ ReverseAction(size_t i, size_t j):_i(i),_j(j){}
+
+ /**
+ * Sort SortActions by score
+ */
+ virtual GeneOrder& operator()(GeneOrder& go) const{
+ go.reverse(_i,_j);
+ return go;
+ }
+
+ virtual bool operator==(const SortAction& sa) const{
+ if (const ReverseAction* psa = dynamic_cast<const ReverseAction*>(&sa)){
+ if (_i == psa->_i && _j == psa->_j)
+ return true;
+ }
+ return false;
+ }
+ private:
+ size_t _i;
+ size_t _j;
+};
+
+#endif
+
#ifndef __SORTACTION_H__
#define __SORTACTION_H__
+class GeneOrder;
/**
- * Abstraction of a sort action, keeping track of score
+ * Abstraction of a sort action
*
* \author Michael Andreen
*/
virtual ~SortAction(){};
/**
- * Sort SortActions by score
+ * Applies the action on the GeneOrder and returning it.
*/
- virtual operator<(const SortAction& sa) const;
+ virtual GeneOrder& operator()(GeneOrder& go) const{
+ return go;
+ }
+
+ /**
+ * Compares sort actions.
+ */
+ virtual bool operator==(const SortAction& sa) const{
+ return false;
+ }
};
#endif
ADD_DEFINITIONS(-Wall -O2)
-SET(TESTSRC geneordertest genealgorithmstest modelidentifiertest)
+SET(TESTSRC geneordertest genealgorithmstest modelidentifiertest
+ genesortertest)
ADD_EXECUTABLE(tester main ${TESTSRC})
TARGET_LINK_LIBRARIES(tester ${GENELIBS} cppunit)
--- /dev/null
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <genesorter.h>
+#include <geneorder.h>
+#include <genealgorithms.h>
+#include <reverseaction.h>
+
+#include <algorithm>
+#include <iterator>
+using namespace std;
+
+/*
+ * A test case that is designed to produce
+ * example errors and failures.
+ *
+ */
+
+#define TESTNAME GeneSorterTest
+
+
+class TESTNAME : public CPPUNIT_NS::TestFixture
+{
+ CPPUNIT_TEST_SUITE( TESTNAME );
+ CPPUNIT_TEST( testCreate );
+ CPPUNIT_TEST( testSafeActions );
+ CPPUNIT_TEST( testSort );
+ CPPUNIT_TEST_SUITE_END();
+
+protected:
+ vector<int> _validPerm;
+ vector<int> _validPerm2;
+ vector<int> _validPerm3;
+ vector<int> _validPerm4;
+
+public:
+
+ void setUp (){
+ int validPerm[] = {1,2,3,4};
+ _validPerm.assign(validPerm,validPerm+4);
+ int validPerm2[] = {1,-3,-2,4};
+ _validPerm2.assign(validPerm2,validPerm2+4);
+ int validPerm3[] = {0,-2,-1,4,3,5,-8,6,7,9};
+ _validPerm3.assign(validPerm3,validPerm3+10);
+ int validPerm4[] = {-3,1,2,4,6,5,7,-15,-13,-14,-12,-10,-11,-9,8};
+ _validPerm4.assign(validPerm4,validPerm4+15);
+
+ }
+
+protected:
+
+ void testCreate (){
+ GeneSorter so;
+ so = so;
+ }
+ void testSort (){
+ GeneSorter so;
+ GeneOrder go(_validPerm.begin(),_validPerm.end());
+ GeneSorter::ActionList al = so.sort(go);
+ CPPUNIT_ASSERT_EQUAL(0ul,al.size());
+
+ GeneOrder go2(_validPerm2.begin(),_validPerm2.end());
+ al = so.sort(go2);
+ CPPUNIT_ASSERT_EQUAL(1ul,al.size());
+ CPPUNIT_ASSERT(al[0] == ReverseAction(2,3));
+
+ GeneOrder go3(_validPerm3.begin(),_validPerm3.end());
+ al = so.sort(go3);
+ CPPUNIT_ASSERT_EQUAL(5ul,al.size());
+ }
+ void testSafeActions (){
+ GeneSorter so;
+ GeneOrder go(_validPerm.begin(),_validPerm.end());
+ GeneSorter::ActionList al = so.safeActions(go);
+ CPPUNIT_ASSERT_EQUAL(0ul,al.size());
+
+ GeneOrder go2(_validPerm2.begin(),_validPerm2.end());
+ al = so.safeActions(go2);
+ CPPUNIT_ASSERT_EQUAL(1ul,al.size());
+ CPPUNIT_ASSERT(al[0] == ReverseAction(2,3));
+ }
+
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TESTNAME );
+
+#undef TESTNAME