From f8575ea6c0982bba5ccf42771b2994d19500c0a3 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Fri, 3 Aug 2007 11:42:25 +0000 Subject: [PATCH] started a refactoring with Model class to score different sort actions --- src/CMakeLists.txt | 8 +++- src/main.cpp | 21 ++-------- src/model.cpp | 37 +++++++++++++++++ src/model.h | 49 ++++++++++++++++++++++ src/modelidentifier.cpp | 27 ++++-------- src/modelidentifier.h | 11 ++--- src/models.cpp | 66 ++++++++++++++++++++++++++++++ src/models.h | 70 ++++++++++++++++++++++++++++++++ src/test/CMakeLists.txt | 5 ++- src/test/modelidentifiertest.cpp | 29 +++++-------- 10 files changed, 259 insertions(+), 64 deletions(-) create mode 100644 src/model.cpp create mode 100644 src/model.h create mode 100644 src/models.cpp create mode 100644 src/models.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 88e33de..25cda6d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -2,14 +2,18 @@ PROJECT(GeneSort) SET(CMAKE_VERBOSE_MAKEFILE OFF) -ADD_DEFINITIONS(-Wall -pedantic -g -D_GLIBCXX_DEBUG) +#ADD_DEFINITIONS(-Wall -O2) + +#ADD_DEFINITIONS(-Wall -pedantic -g -D_GLIBCXX_DEBUG) +ADD_DEFINITIONS(-Wall -pedantic -g) #INCLUDE(CheckCXXSourceCompiles) #CheckCXXSourceCompiles(test HAVE_TR1) INCLUDE_DIRECTORIES(.) -ADD_LIBRARY(GeneSort geneorder genealgorithms modelidentifier genesorter) +ADD_LIBRARY(GeneSort geneorder genealgorithms modelidentifier genesorter model + models) ADD_EXECUTABLE(genesort main.cpp) SET(GENELIBS doublefann GeneSort) diff --git a/src/main.cpp b/src/main.cpp index 1c6a85e..153aff4 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,15 +12,7 @@ using namespace std; #include "modelidentifier.h" #include "genesorter.h" #include "sortaction.h" - -typedef pair modelpair; - -struct ScoreCmp { - template - bool operator()(T s1, T s2){ - return s1.first < s2.first; - } -}; +#include "model.h" int main(int argc, char** argv){ @@ -67,16 +59,9 @@ int main(int argc, char** argv){ //TODO: Identify ModelIdentifier mi(ann); - map scores = mi.identify(go); - priority_queue,vector >, ScoreCmp > pq; - for (map::iterator m = scores.begin(); - m != scores.end(); ++m){ - if (m->second > 0){ - pq.push(pair(m->second,m->first)); - } - } + priority_queue > pq = mi.identify(go); while (pq.size() > 0){ - cout << "Model: " << mi.modelName(pq.top().second) << " score: " << pq.top().first << endl; + cout << "Model: " << pq.top().second.name() << " score: " << pq.top().first << endl; pq.pop(); } diff --git a/src/model.cpp b/src/model.cpp new file mode 100644 index 0000000..12bee4b --- /dev/null +++ b/src/model.cpp @@ -0,0 +1,37 @@ +/*************************************************************************** + * 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 "model.h" +#include "models.h" + +Model::Model(Models::ModelImpl* model): _impl(model){ +} + +double Model::score(const SortAction& sa) const{ + return _impl->score(sa); +} + +std::string Model::name() const{ + return _impl->name(); +} + +bool operator<(const Model& lh,const Model& rh){ + return &lh < &rh; +} diff --git a/src/model.h b/src/model.h new file mode 100644 index 0000000..9ebcb23 --- /dev/null +++ b/src/model.h @@ -0,0 +1,49 @@ +/*************************************************************************** + * 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 __MODEL_H__ +#define __MODEL_H__ + +#include +#include + +class SortAction; +namespace Models { + class ModelImpl; +} + +class Model{ + public: + typedef std::tr1::shared_ptr ModelPointer; + + Model(Models::ModelImpl* model); + + double score(const SortAction& sa) const; + + std::string name() const; + + private: + ModelPointer _impl; + +}; + +bool operator<(const Model& lh,const Model& rh); + +#endif diff --git a/src/modelidentifier.cpp b/src/modelidentifier.cpp index 56b6935..820d75a 100644 --- a/src/modelidentifier.cpp +++ b/src/modelidentifier.cpp @@ -22,6 +22,8 @@ #include "genealgorithms.h" #include +#include "models.h" +#include "model.h" using namespace std; @@ -36,7 +38,7 @@ ModelIdentifier::~ModelIdentifier(){ fann_destroy(_ann); } -std::map ModelIdentifier::identify(const GeneOrder& go){ +priority_queue > ModelIdentifier::identify(const GeneOrder& go){ int pos = 0; int neg = 0; for (GeneOrder::iterator g = go.begin(); g != go.end(); ++g){ @@ -78,22 +80,11 @@ std::map ModelIdentifier::identify(const GeneOrde double *output = fann_run(_ann,&input[0]); - map scores; - scores[X] = output[0]; - scores[Zipper] = output[1]; - scores[Whirl] = output[2]; - scores[FatX] = output[3]; - scores[Cloud] = output[4]; + priority_queue > scores; + scores.push(pair(output[0],Model(new Models::X))); + scores.push(pair(output[1],Model(new Models::Zipper))); + scores.push(pair(output[2],Model(new Models::ModelImpl))); + scores.push(pair(output[3],Model(new Models::FatX))); + scores.push(pair(output[4],Model(new Models::Cloud))); return scores; } - -string ModelIdentifier::modelName(Model m){ - switch (m){ - case Whirl : return "Whirl"; - case X : return "X"; - case FatX : return "FatX"; - case Zipper : return "Zipper"; - case Cloud : return "Cloud"; - } - return "Unknown model"; -} diff --git a/src/modelidentifier.h b/src/modelidentifier.h index a42e0ef..ee163e3 100644 --- a/src/modelidentifier.h +++ b/src/modelidentifier.h @@ -23,11 +23,13 @@ #include "geneorder.h" -#include +#include #include #include struct fann; +class Model; + /** * Identifies the model this gene order belongs to @@ -35,9 +37,6 @@ struct fann; */ class ModelIdentifier{ public: - //TODO: Make a Model class with functions for scoring SortActions - //one subclass for each model. Possibly storing name as string too. - enum Model{Whirl,X,FatX,Zipper,Cloud}; /** * Creates a new identifier given an artificial neural network @@ -54,9 +53,7 @@ class ModelIdentifier{ * * \returns a map with the model as key and the score between -1 and 1 */ - std::map identify(const GeneOrder& go); - - static std::string modelName(Model m); + std::priority_queue > identify(const GeneOrder& go); ~ModelIdentifier(); private: diff --git a/src/models.cpp b/src/models.cpp new file mode 100644 index 0000000..cd89ef8 --- /dev/null +++ b/src/models.cpp @@ -0,0 +1,66 @@ +/*************************************************************************** + * 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 "models.h" + +using namespace std; + +namespace Models{ + +ModelImpl::~ModelImpl(){} + +double ModelImpl::score(const SortAction& sa){ + return 1; +} +string ModelImpl::name(){ + return "Standard"; +} + + + +double X::score(const SortAction& sa){ + return 1; +} +string X::name(){ + return "X"; +} + +double Zipper::score(const SortAction& sa){ + return 1; +} +string Zipper::name(){ + return "Zipper"; +} + +double Cloud::score(const SortAction& sa){ + return 1; +} +string Cloud::name(){ + return "Cloud"; +} + +double FatX::score(const SortAction& sa){ + return 1; +} +string FatX::name(){ + return "FatX"; +} + +} diff --git a/src/models.h b/src/models.h new file mode 100644 index 0000000..6a61506 --- /dev/null +++ b/src/models.h @@ -0,0 +1,70 @@ +/*************************************************************************** + * 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 __MODELS_H__ +#define __MODELS_H__ + +#include + +class SortAction; + +namespace Models { + +class ModelImpl { + public: + virtual ~ModelImpl(); + + virtual double score(const SortAction& sa); + + virtual std::string name(); +}; + + +class X : public ModelImpl { + public: + double score(const SortAction& sa); + + std::string name(); +}; + +class Zipper : public ModelImpl { + public: + double score(const SortAction& sa); + + std::string name(); +}; + +class Cloud : public ModelImpl { + public: + double score(const SortAction& sa); + + std::string name(); +}; + +class FatX : public ModelImpl { + public: + double score(const SortAction& sa); + + std::string name(); +}; + +} + +#endif diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index c018f8c..47a09fa 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -2,7 +2,10 @@ PROJECT(GeneSort) SET(CMAKE_VERBOSE_MAKEFILE OFF) -ADD_DEFINITIONS(-Wall -O2) +#ADD_DEFINITIONS(-Wall -O2) + +#ADD_DEFINITIONS(-Wall -pedantic -g -D_GLIBCXX_DEBUG) + SET(TESTSRC geneordertest genealgorithmstest modelidentifiertest diff --git a/src/test/modelidentifiertest.cpp b/src/test/modelidentifiertest.cpp index 1e5563f..96c3587 100644 --- a/src/test/modelidentifiertest.cpp +++ b/src/test/modelidentifiertest.cpp @@ -4,10 +4,12 @@ #include #include #include +#include #include #include #include + using namespace std; /* @@ -18,13 +20,8 @@ using namespace std; #define TESTNAME ModelIdentifierTest -typedef pair modelpair; +typedef pair modelpair; -struct ModelCmp { - bool operator()(modelpair m1, modelpair m2){ - return m1.second < m2.second; - } -}; class TESTNAME : public CPPUNIT_NS::TestFixture { @@ -65,24 +62,20 @@ protected: ModelIdentifier mi("default.ann"); GeneOrder axis(_1axis.begin(),_1axis.end()); - map scores = mi.identify(axis); - priority_queue, ModelCmp > pq(scores.begin(),scores.end()); - CPPUNIT_ASSERT_EQUAL(ModelIdentifier::X,pq.top().first); + priority_queue pq = mi.identify(axis); + CPPUNIT_ASSERT_EQUAL(string("X"),pq.top().second.name()); GeneOrder uniform(_uniform.begin(),_uniform.end()); - scores = mi.identify(uniform); - pq = priority_queue, ModelCmp >(scores.begin(),scores.end()); - CPPUNIT_ASSERT_EQUAL(ModelIdentifier::Whirl,pq.top().first); + pq = mi.identify(uniform); + CPPUNIT_ASSERT_EQUAL(string("Standard"),pq.top().second.name()); GeneOrder zipper(_zipper.begin(),_zipper.end()); - scores = mi.identify(zipper); - pq = priority_queue, ModelCmp >(scores.begin(),scores.end()); - CPPUNIT_ASSERT_EQUAL(ModelIdentifier::Zipper,pq.top().first); + pq = mi.identify(zipper); + CPPUNIT_ASSERT_EQUAL(string("Zipper"),pq.top().second.name()); GeneOrder transpos(_transpos.begin(),_transpos.end()); - scores = mi.identify(transpos); - pq = priority_queue, ModelCmp >(scores.begin(),scores.end()); - CPPUNIT_ASSERT_EQUAL(ModelIdentifier::Cloud,pq.top().first); + pq = mi.identify(transpos); + CPPUNIT_ASSERT_EQUAL(string("Cloud"),pq.top().second.name()); } }; -- 2.39.2