]> ruin.nu Git - germs.git/commitdiff
started a refactoring with Model class to score different sort actions
authorMichael Andreen <harv@ruin.nu>
Fri, 3 Aug 2007 11:42:25 +0000 (11:42 +0000)
committerMichael Andreen <harv@ruin.nu>
Fri, 3 Aug 2007 11:42:25 +0000 (11:42 +0000)
src/CMakeLists.txt
src/main.cpp
src/model.cpp [new file with mode: 0644]
src/model.h [new file with mode: 0644]
src/modelidentifier.cpp
src/modelidentifier.h
src/models.cpp [new file with mode: 0644]
src/models.h [new file with mode: 0644]
src/test/CMakeLists.txt
src/test/modelidentifiertest.cpp

index 88e33de6af4b433140e85868ce304a92220463a8..25cda6d76e2abb577e94e6f4cc60e04c8bfff162 100644 (file)
@@ -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)
index 1c6a85ecc85790716e6a8559e1ea86117d716922..153aff40e608bce08a91bfe77a4980f017379ca8 100644 (file)
@@ -12,15 +12,7 @@ using namespace std;
 #include "modelidentifier.h"
 #include "genesorter.h"
 #include "sortaction.h"
-
-typedef pair<ModelIdentifier::Model,double> modelpair;
-
-struct ScoreCmp {
-       template<typename T>
-       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<ModelIdentifier::Model,double> scores = mi.identify(go);
-       priority_queue<pair<double,ModelIdentifier::Model>,vector<pair<double,ModelIdentifier::Model> >, ScoreCmp > pq;
-       for (map<ModelIdentifier::Model,double>::iterator m = scores.begin();
-                       m != scores.end(); ++m){
-               if (m->second > 0){
-                       pq.push(pair<double,ModelIdentifier::Model>(m->second,m->first));
-               }
-       }
+       priority_queue<pair<double,Model> > 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 (file)
index 0000000..12bee4b
--- /dev/null
@@ -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 (file)
index 0000000..9ebcb23
--- /dev/null
@@ -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 <tr1/memory>
+#include <string>
+
+class SortAction;
+namespace Models {
+       class ModelImpl;
+}
+
+class Model{
+       public:
+               typedef std::tr1::shared_ptr<Models::ModelImpl> 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
index 56b6935c48a4050bda6be087f9899f8f2af45b40..820d75abd73bbc58a215a704652502e17388893a 100644 (file)
@@ -22,6 +22,8 @@
 #include "genealgorithms.h"
 
 #include <doublefann.h>
+#include "models.h"
+#include "model.h"
 
 using namespace std;
 
@@ -36,7 +38,7 @@ ModelIdentifier::~ModelIdentifier(){
        fann_destroy(_ann);
 }
 
-std::map<ModelIdentifier::Model,double> ModelIdentifier::identify(const GeneOrder& go){
+priority_queue<pair<double,Model> > 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::Model,double> ModelIdentifier::identify(const GeneOrde
 
        double *output = fann_run(_ann,&input[0]);
 
-       map<Model,double> scores;
-       scores[X] = output[0];
-       scores[Zipper] = output[1];
-       scores[Whirl] = output[2];
-       scores[FatX] = output[3];
-       scores[Cloud] = output[4];
+       priority_queue<pair<double,Model> > scores;
+       scores.push(pair<double,Model>(output[0],Model(new Models::X)));
+       scores.push(pair<double,Model>(output[1],Model(new Models::Zipper)));
+       scores.push(pair<double,Model>(output[2],Model(new Models::ModelImpl)));
+       scores.push(pair<double,Model>(output[3],Model(new Models::FatX)));
+       scores.push(pair<double,Model>(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";
-}
index a42e0efb7097fe05ed34a8175c9a05633baf5d18..ee163e315d278d427ce646f8a52b29ec98052271 100644 (file)
 
 #include "geneorder.h"
 
-#include <map>
+#include <queue>
 #include <string>
 #include <stdexcept>
 
 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<Model,double> identify(const GeneOrder& go);
-
-               static std::string modelName(Model m);
+               std::priority_queue<std::pair<double,Model> > identify(const GeneOrder& go);
 
                ~ModelIdentifier();
        private:
diff --git a/src/models.cpp b/src/models.cpp
new file mode 100644 (file)
index 0000000..cd89ef8
--- /dev/null
@@ -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 (file)
index 0000000..6a61506
--- /dev/null
@@ -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 <string>
+
+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
index c018f8cbc56ccaf60fb75a41bd3a62c2a93fadc8..47a09fa93b00fbf36b60f56f426bce525b27ca2e 100644 (file)
@@ -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
index 1e5563f1993ffe391a38b18cbdbfb373f4ff0db5..96c3587eae0e15dc5570d7bcf8b18c1adffd8094 100644 (file)
@@ -4,10 +4,12 @@
 #include <geneorder.h>
 #include <genealgorithms.h>
 #include <modelidentifier.h>
+#include <model.h>
 
 #include <algorithm>
 #include <queue>
 #include <iterator>
+
 using namespace std;
 
 /* 
@@ -18,13 +20,8 @@ using namespace std;
 
 #define TESTNAME ModelIdentifierTest
 
-typedef pair<ModelIdentifier::Model,double> modelpair;
+typedef pair<double, Model> 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<ModelIdentifier::Model,double> scores = mi.identify(axis);
-               priority_queue<modelpair, vector<modelpair>, ModelCmp > pq(scores.begin(),scores.end());
-               CPPUNIT_ASSERT_EQUAL(ModelIdentifier::X,pq.top().first);
+               priority_queue<modelpair> 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<modelpair, vector<modelpair>, 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<modelpair, vector<modelpair>, 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<modelpair, vector<modelpair>, 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());
        }
 };