]> ruin.nu Git - germs.git/commitdiff
setting up things for implementing the sorting
authorMichael Andreen <harv@ruin.nu>
Sun, 24 Jun 2007 10:26:45 +0000 (10:26 +0000)
committerMichael Andreen <harv@ruin.nu>
Sun, 24 Jun 2007 10:26:45 +0000 (10:26 +0000)
src/CMakeLists.txt
src/genesorter.cpp [new file with mode: 0644]
src/genesorter.h
src/reverseaction.h [new file with mode: 0644]
src/sortaction.h
src/test/CMakeLists.txt
src/test/genesortertest.cpp [new file with mode: 0644]

index 3f17220d404b405c58c3a1c0ec0092d05aa90a38..16e293676d7c2d98e77fb9237e9e683c3d0b0ae1 100644 (file)
@@ -5,7 +5,7 @@ SET(CMAKE_VERBOSE_MAKEFILE OFF)
 ADD_DEFINITIONS(-Wall -g)
 
 INCLUDE_DIRECTORIES(.)
 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)
 ADD_EXECUTABLE(genesort main.cpp)
 
 SET(GENELIBS doublefann GeneSort)
diff --git a/src/genesorter.cpp b/src/genesorter.cpp
new file mode 100644 (file)
index 0000000..5f8699c
--- /dev/null
@@ -0,0 +1,35 @@
+/***************************************************************************
+ *   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();
+}
index fa47b0535b188b01b14461807d335ca6f76be945..384261196060dfaa19522a7deadc68e28784e6d1 100644 (file)
 #ifndef __GENESORTER_H__
 #define __GENESORTER_H__
 
 #ifndef __GENESORTER_H__
 #define __GENESORTER_H__
 
-#include "geneorder.h"
-#include "sortaction.h"
-
 #include <vector>
 
 #include <vector>
 
+class SortAction;
+class GeneOrder;
 
 /**
 
 /**
- * Abstract baseclass for different gene sorters.
+ * Sorts genes
+ *
  * \author Michael Andreen
  */
 class GeneSorter{
  * \author Michael Andreen
  */
 class GeneSorter{
@@ -39,14 +39,14 @@ class GeneSorter{
                 * Takes a GeneOrder, finds the actions to transform it into a sorted
                 * permutation and returns the list with required actions.
                 */
                 * 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.
                 */
 
                /**
                 * Find the safe actions for this GeneOrder.
                 */
-               virtual ActionList safeActions(const GeneOrder& go1) = 0;
+               ActionList safeActions(const GeneOrder& go1);
 
 
-               virtual ~GeneSorter(){};
+               ~GeneSorter(){};
 };
 
 #endif
 };
 
 #endif
diff --git a/src/reverseaction.h b/src/reverseaction.h
new file mode 100644 (file)
index 0000000..a7db6d4
--- /dev/null
@@ -0,0 +1,59 @@
+/***************************************************************************
+ *   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
+
index b6c5df74278f85d08540678674ffb6c7d384a650..a71a533cf70ec981f10d32527053e58cdf0a609b 100644 (file)
@@ -21,8 +21,9 @@
 #ifndef __SORTACTION_H__
 #define __SORTACTION_H__
 
 #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
  */
  *
  * \author Michael Andreen
  */
@@ -32,9 +33,18 @@ class SortAction{
                virtual ~SortAction(){};
 
                /**
                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
 };
 
 #endif
index 2cd964dde1808d149001040ec1efcda21c3377ba..c018f8cbc56ccaf60fb75a41bd3a62c2a93fadc8 100644 (file)
@@ -5,7 +5,8 @@ SET(CMAKE_VERBOSE_MAKEFILE OFF)
 ADD_DEFINITIONS(-Wall -O2)
 
 
 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)
 
 ADD_EXECUTABLE(tester main ${TESTSRC})
 TARGET_LINK_LIBRARIES(tester ${GENELIBS} cppunit)
diff --git a/src/test/genesortertest.cpp b/src/test/genesortertest.cpp
new file mode 100644 (file)
index 0000000..a2c4fae
--- /dev/null
@@ -0,0 +1,87 @@
+#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