From: Michael Andreen Date: Sun, 24 Jun 2007 10:26:45 +0000 (+0000) Subject: setting up things for implementing the sorting X-Git-Tag: v0.1~53 X-Git-Url: https://ruin.nu/git/?p=germs.git;a=commitdiff_plain;h=d0abe1592fcbb10f4ac303e7b66c384624d4d439 setting up things for implementing the sorting --- diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3f17220..16e2936 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,7 +5,7 @@ SET(CMAKE_VERBOSE_MAKEFILE OFF) 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) diff --git a/src/genesorter.cpp b/src/genesorter.cpp new file mode 100644 index 0000000..5f8699c --- /dev/null +++ b/src/genesorter.cpp @@ -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(); +} diff --git a/src/genesorter.h b/src/genesorter.h index fa47b05..3842611 100644 --- a/src/genesorter.h +++ b/src/genesorter.h @@ -21,14 +21,14 @@ #ifndef __GENESORTER_H__ #define __GENESORTER_H__ -#include "geneorder.h" -#include "sortaction.h" - #include +class SortAction; +class GeneOrder; /** - * Abstract baseclass for different gene sorters. + * Sorts genes + * * \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. */ - 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 diff --git a/src/reverseaction.h b/src/reverseaction.h new file mode 100644 index 0000000..a7db6d4 --- /dev/null +++ b/src/reverseaction.h @@ -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(&sa)){ + if (_i == psa->_i && _j == psa->_j) + return true; + } + return false; + } + private: + size_t _i; + size_t _j; +}; + +#endif + diff --git a/src/sortaction.h b/src/sortaction.h index b6c5df7..a71a533 100644 --- a/src/sortaction.h +++ b/src/sortaction.h @@ -21,8 +21,9 @@ #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 */ @@ -32,9 +33,18 @@ class 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 diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 2cd964d..c018f8c 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -5,7 +5,8 @@ SET(CMAKE_VERBOSE_MAKEFILE OFF) 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) diff --git a/src/test/genesortertest.cpp b/src/test/genesortertest.cpp new file mode 100644 index 0000000..a2c4fae --- /dev/null +++ b/src/test/genesortertest.cpp @@ -0,0 +1,87 @@ +#include +#include + +#include +#include +#include +#include + +#include +#include +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 _validPerm; + vector _validPerm2; + vector _validPerm3; + vector _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