From 868e1b08fbbd49e489dc1349cb3657521c5c1dd9 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Thu, 21 Jun 2007 11:57:34 +0000 Subject: [PATCH] initial commit of the modelidentifier files --- src/CMakeLists.txt | 6 ++- src/default.ann | 33 ++++++++++++ src/modelidentifier.cpp | 52 ++++++++++++++++++ src/modelidentifier.h | 24 ++++++++- src/test/CMakeLists.txt | 4 +- src/test/modelidentifiertest.cpp | 91 ++++++++++++++++++++++++++++++++ 6 files changed, 204 insertions(+), 6 deletions(-) create mode 100644 src/default.ann create mode 100644 src/modelidentifier.cpp create mode 100644 src/test/modelidentifiertest.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 42c41ad..3f17220 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,9 +5,11 @@ SET(CMAKE_VERBOSE_MAKEFILE OFF) ADD_DEFINITIONS(-Wall -g) INCLUDE_DIRECTORIES(.) -ADD_LIBRARY(GeneSort geneorder genealgorithms) +ADD_LIBRARY(GeneSort geneorder genealgorithms modelidentifier) ADD_EXECUTABLE(genesort main.cpp) -TARGET_LINK_LIBRARIES(genesort doublefann GeneSort) +SET(GENELIBS doublefann GeneSort) + +TARGET_LINK_LIBRARIES(genesort ${GENELIBS}) SUBDIRS(test) diff --git a/src/default.ann b/src/default.ann new file mode 100644 index 0000000..6e9206a --- /dev/null +++ b/src/default.ann @@ -0,0 +1,33 @@ +FANN_FLO_2.0 +num_layers=3 +learning_rate=0.700000 +connection_rate=1.000000 +shortcut_connections=0 +learning_momentum=0.000000 +training_algorithm=2 +train_error_function=1 +train_stop_function=0 +cascade_output_change_fraction=0.010000 +quickprop_decay=-0.000100 +quickprop_mu=1.750000 +rprop_increase_factor=1.200000 +rprop_decrease_factor=0.500000 +rprop_delta_min=0.000000 +rprop_delta_max=50.000000 +rprop_delta_zero=0.500000 +cascade_output_stagnation_epochs=12 +cascade_candidate_change_fraction=0.010000 +cascade_candidate_stagnation_epochs=12 +cascade_max_out_epochs=150 +cascade_max_cand_epochs=150 +cascade_num_candidate_groups=2 +bit_fail_limit=3.49999999999999977796e-01 +cascade_candidate_limit=1.00000000000000000000e+03 +cascade_weight_multiplier=4.00000000000000022204e-01 +cascade_activation_functions_count=6 +cascade_activation_functions=3 5 7 8 10 11 +cascade_activation_steepnesses_count=4 +cascade_activation_steepnesses=2.50000000000000000000e-01 5.00000000000000000000e-01 7.50000000000000000000e-01 1.00000000000000000000e+00 +layer_sizes=9 5 6 +neurons (num_inputs, activation_function, activation_steepness)=(0, 0, 0.00000000000000000000e+00) (0, 0, 0.00000000000000000000e+00) (0, 0, 0.00000000000000000000e+00) (0, 0, 0.00000000000000000000e+00) (0, 0, 0.00000000000000000000e+00) (0, 0, 0.00000000000000000000e+00) (0, 0, 0.00000000000000000000e+00) (0, 0, 0.00000000000000000000e+00) (0, 0, 0.00000000000000000000e+00) (9, 5, 5.00000000000000000000e-01) (9, 5, 5.00000000000000000000e-01) (9, 5, 5.00000000000000000000e-01) (9, 5, 5.00000000000000000000e-01) (0, 5, 0.00000000000000000000e+00) (5, 5, 5.00000000000000000000e-01) (5, 5, 5.00000000000000000000e-01) (5, 5, 5.00000000000000000000e-01) (5, 5, 5.00000000000000000000e-01) (5, 5, 5.00000000000000000000e-01) (0, 5, 0.00000000000000000000e+00) +connections (connected_to_neuron, weight)=(0, 2.03811730880741492911e+00) (1, 4.29227407164857588384e+00) (2, 1.27250709602987077673e+00) (3, -1.76596975000495759467e+00) (4, 3.85914896546326318116e+01) (5, -3.00983940273785286479e+01) (6, -7.96132130230115421909e+02) (7, -4.68709878623485565186e-02) (8, -1.02986344328384955560e+01) (0, -1.85886473910201281967e+01) (1, 3.71411728621473230305e+02) (2, 9.46622874940555192325e+01) (3, -2.86025537441835194841e+03) (4, 2.27850830770983137086e+01) (5, 6.64439332631556567321e+01) (6, 2.46896007119450829923e+07) (7, 7.01853856444358825684e-02) (8, 4.84953313466055959680e+01) (0, 2.42410213439175663552e+01) (1, 3.52906066564778697625e+01) (2, -1.10666632819873314020e+02) (3, -1.23484738420633021860e+02) (4, 5.14221097448539765651e+00) (5, -6.09301726182952485544e+00) (6, 2.47879743895786665380e+07) (7, -1.40448063611984252930e-02) (8, 2.72800598549792709946e+01) (0, -1.03913769741883038478e+03) (1, 8.22926396165223995922e+04) (2, -1.20923923873409075895e+04) (3, -4.29786470480338466587e+04) (4, 6.18227443813255467830e+03) (5, 3.11856237314064537713e+03) (6, -1.76605114589305147529e+07) (7, 1.45326405763626098633e-02) (8, 1.51457226823164569396e+03) (9, -1.51841644893375189440e+01) (10, -2.21120163092979371555e+00) (11, -1.04160582284450686785e+01) (12, 2.92637258000222155729e+00) (13, -9.06434407331133407126e+00) (9, 1.13078795369507815849e+02) (10, 2.94291835521026825973e+02) (11, -1.43862450017754355258e+02) (12, 2.38945419786671124029e+02) (13, -2.06256373069649725949e+02) (9, 1.96134688551125968559e+01) (10, -1.58980488912551436442e+01) (11, 1.39469037281984817866e+01) (12, -3.25858792460640422917e+00) (13, -7.01826611437134229021e+00) (9, 2.24440210628802372383e+00) (10, -1.82417606243704675251e+01) (11, -2.70634025528663357818e+00) (12, -1.92915267246852621952e+01) (13, -4.16325598148192170811e+01) (9, -1.28000950717861684325e+02) (10, 3.25220271406730487840e+00) (11, 2.71302886058511603551e+02) (12, -3.48001821040458366951e+02) (13, -7.05740716607723754805e+01) diff --git a/src/modelidentifier.cpp b/src/modelidentifier.cpp new file mode 100644 index 0000000..561eea3 --- /dev/null +++ b/src/modelidentifier.cpp @@ -0,0 +1,52 @@ +/*************************************************************************** + * 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 "modelidentifier.h" +#include "genealgorithms.h" + +#include + +using namespace std; + +ModelIdentifier::ModelIdentifier(std::string ann){ + _ann = fann_create_from_file(ann.c_str()); + if(!_ann){ + throw invalid_argument("Could not create network"); + } +} + +ModelIdentifier::~ModelIdentifier(){ + fann_destroy(_ann); +} + +std::map ModelIdentifier::identify(const GeneOrder& go){ + int pos = 0; + int neg = 0; + for (GeneOrder::iterator g = go.begin(); g != go.end(); ++g){ + if (*g >= 0) + ++pos; + else + ++neg; + } + map scores; + scores[X] = 1; + scores[Whirl] = -1; + return scores; +} diff --git a/src/modelidentifier.h b/src/modelidentifier.h index bbf3037..1ed4809 100644 --- a/src/modelidentifier.h +++ b/src/modelidentifier.h @@ -24,7 +24,10 @@ #include "geneorder.h" #include +#include +#include +struct fann; /** * Identifies the model this gene order belongs to @@ -34,13 +37,30 @@ class ModelIdentifier{ public: enum Model{Whirl,X,FatX,Zipper,Cloud}; + /** + * Creates a new identifier given an artificial neural network + * + * \param ann filename, including path, to the neural network + * + * \throws std::invalid_argument exception if the ann could not be + * opened + */ + ModelIdentifier(std::string ann); + /** * Identifies the model for a given gene order. - * \returns a map with the model as key and the score between 0 and 1 + * + * \returns a map with the model as key and the score between -1 and 1 */ std::map identify(const GeneOrder& go); - virtual ~ModelIdentifier(){}; + ~ModelIdentifier(); + private: + fann* _ann; + + //disable for the moment + void operator=(const ModelIdentifier&){} + }; #endif diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 5540c09..2cd964d 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -5,10 +5,10 @@ SET(CMAKE_VERBOSE_MAKEFILE OFF) ADD_DEFINITIONS(-Wall -O2) -SET(TESTSRC geneordertest genealgorithmstest) +SET(TESTSRC geneordertest genealgorithmstest modelidentifiertest) ADD_EXECUTABLE(tester main ${TESTSRC}) -TARGET_LINK_LIBRARIES(tester GeneSort cppunit) +TARGET_LINK_LIBRARIES(tester ${GENELIBS} cppunit) #find_package(Qt3) diff --git a/src/test/modelidentifiertest.cpp b/src/test/modelidentifiertest.cpp new file mode 100644 index 0000000..1e5563f --- /dev/null +++ b/src/test/modelidentifiertest.cpp @@ -0,0 +1,91 @@ +#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 ModelIdentifierTest + +typedef pair modelpair; + +struct ModelCmp { + bool operator()(modelpair m1, modelpair m2){ + return m1.second < m2.second; + } +}; + +class TESTNAME : public CPPUNIT_NS::TestFixture +{ + CPPUNIT_TEST_SUITE( TESTNAME ); + CPPUNIT_TEST( testCreate ); + CPPUNIT_TEST( testIdentify ); + CPPUNIT_TEST_SUITE_END(); + +protected: + vector _transpos; + vector _zipper; + vector _uniform; + vector _1axis; + +public: + + void setUp (){ + int transpos[] = {1,2,3,367,5,6,7,8,9,10,476,132,13,60,15,16,17,18,286,81,21,22,23,24,25,421,70,28,29,30,389,32,33,34,35,36,37,38,39,160,41,42,489,44,45,46,47,48,123,451,51,106,53,54,55,56,57,58,59,14,61,96,63,64,65,66,244,68,69,460,71,72,73,74,75,76,77,78,79,80,20,82,377,84,408,442,87,88,67,90,91,92,257,94,250,62,97,98,401,320,101,102,103,104,105,52,484,108,109,110,111,223,322,114,115,116,95,118,119,317,121,122,49,124,125,248,127,128,129,130,301,12,133,134,135,136,137,131,177,140,19,142,143,144,145,146,483,328,149,150,85,405,490,154,155,156,171,158,159,40,456,162,163,164,165,300,167,168,169,464,157,172,173,174,175,176,139,178,179,180,181,182,243,184,185,351,187,188,189,190,191,192,193,345,195,196,197,198,199,200,201,202,203,204,205,206,207,208,235,210,211,212,414,214,215,216,217,218,219,220,221,222,112,224,225,470,227,228,416,310,231,232,233,234,209,236,237,238,258,240,241,242,183,89,394,290,247,117,249,126,251,252,253,254,255,113,93,239,259,260,261,262,263,264,265,266,267,354,371,270,271,272,273,274,428,276,277,278,279,280,469,282,283,284,285,107,268,288,289,246,291,292,293,294,295,296,297,298,299,166,449,302,303,304,360,306,307,308,309,230,311,312,313,314,315,316,120,318,319,100,321,256,323,324,325,326,327,148,329,330,331,402,333,334,335,336,337,338,339,340,341,342,343,344,194,346,347,348,349,350,186,352,353,287,355,440,357,358,359,305,361,362,363,364,365,366,4,383,369,370,269,372,373,374,375,376,83,467,379,380,381,382,368,384,385,501,387,388,31,390,391,392,393,245,425,396,397,398,399,400,99,332,403,404,152,406,407,151,409,410,411,412,413,213,415,229,417,418,441,420,26,422,423,448,395,426,427,275,482,430,431,432,433,434,435,436,437,438,439,356,419,86,443,444,445,446,447,424,138,450,50,452,453,454,455,161,457,458,459,27,461,462,463,170,465,466,378,468,281,226,471,472,473,474,475,11,477,478,479,480,481,429,147,141,485,486,487,488,43,153,491,492,493,494,495,496,497,498,499,500,386}; + _transpos.assign(transpos,transpos+501); + int zipper[] = {1,2,3,-29,-28,-27,-26,-25,-24,-23,-22,-21,-20,-19,-18,-17,-16,-15,-14,-13,-12,-11,-10,-9,-8,-7,-6,-5,-4,30,31,32,33,34,35,36,61,62,63,64,43,44,-74,48,49,50,-82,-81,-80,-91,-90,-59,-58,-77,-76,-75,85,86,87,88,89,38,57,78,79,92,93,-47,-46,-45,-84,-83,51,52,53,54,55,56,39,40,41,42,65,66,67,68,69,-37,-60,70,71,72,73,94,95,96,97,98,99,-117,-116,-115,-114,-111,-110,136,137,100,101,102,103,104,105,150,151,152,153,-131,-130,162,163,164,147,148,149,106,107,112,113,-133,-132,154,155,156,157,158,159,160,161,-129,-128,-127,-126,-125,-124,-123,-122,-121,-120,-119,-118,138,207,208,209,210,211,212,213,194,195,196,141,142,143,134,199,200,197,198,135,-179,-178,-203,-202,-201,-140,-139,-206,-205,-204,-187,-186,-185,193,214,215,216,217,218,219,220,-246,-245,-244,-243,177,188,189,190,-146,-259,-258,-247,221,222,223,224,169,170,171,172,173,174,175,-165,191,192,-184,-183,-182,-181,-180,-109,-108,144,145,260,-242,-241,-240,-239,-238,-237,-226,-225,-168,-167,-166,-231,-230,-229,-228,-227,-236,-235,-234,-233,-232,176,261,262,263,264,265,266,267,268,253,254,255,256,257,248,249,250,251,252,269,270,271,272,285,286,287,288,289,290,291,292,293,294,319,320,321,-281,-280,-279,329,-309,-308,-307,-306,-305,-304,-317,-316,-315,-352,-351,-350,-355,-354,278,-328,-327,-326,-325,-324,-323,-322,282,283,284,273,274,275,276,277,-353,-314,-313,-312,342,343,344,345,346,347,348,349,356,357,358,359,-333,-332,-331,-300,-299,-376,-375,-374,-373,-372,-371,-370,310,311,-341,-378,-377,-298,-297,-296,-295,-318,-303,-302,-301,-330,-369,-368,-367,-404,-403,-406,-405,-366,-365,-364,-363,-362,-361,-360,334,335,-427,-426,-425,431,434,435,-409,-408,-407,-402,-401,-400,-399,-384,-419,-418,-455,-454,-453,-398,-397,-396,-395,-394,388,389,386,387,-393,-432,-433,-392,336,337,338,-430,-429,-428,382,383,420,421,422,423,468,469,470,471,472,449,450,451,452,385,390,-414,480,481,-417,-446,-339,-424,-467,-466,-465,-464,-463,-462,-461,415,416,447,436,-458,-457,-456,482,483,484,485,486,487,488,489,490,491,492,-442,-441,-440,-439,-438,-437,459,460,391,-381,-380,-379,-340,-445,-444,-443,493,494,495,496,497,-477,-476,-475,-474,-473,-448,410,411,412,413,-479,-478,498,499,500,501}; + _zipper.assign(zipper,zipper+501); + + int uniform[] = {1,2,3,4,5,6,-321,-320,-319,-318,-317,-316,-315,-314,156,157,158,-234,-233,-232,-231,494,495,496,-311,-310,-309,483,484,485,486,487,488,489,490,363,364,365,38,-26,444,249,250,251,252,253,254,255,-164,-163,-162,-161,-160,-159,235,236,237,238,239,240,-386,-385,-292,-291,-290,-289,-288,-287,-286,-285,-284,-283,-282,-281,-280,-173,-172,-171,-170,-169,-168,-167,-166,-165,256,257,78,138,139,140,-108,-199,117,118,119,120,121,200,201,202,203,-353,-352,-351,-350,-349,-348,-347,-346,-345,-344,-343,-342,-341,-340,-339,-338,24,-126,-125,-124,-123,-122,155,-313,-312,497,-373,410,411,412,413,205,206,-447,-446,426,427,428,429,430,431,-305,-304,182,-45,-44,-43,-42,-41,-40,-39,27,28,29,30,31,32,33,34,-10,-9,-8,-7,322,323,324,325,326,327,328,329,330,-135,-134,-133,-132,-96,-95,-94,-93,-92,-91,-90,-89,-88,-87,224,225,226,227,228,229,230,-493,-492,-491,389,390,391,392,393,394,395,396,397,398,399,400,401,-97,382,383,384,293,294,295,296,297,298,299,300,301,464,465,466,467,468,469,470,471,472,221,222,223,-86,-85,-84,-83,-82,77,258,259,260,261,262,-276,-275,-274,-273,-272,-271,-270,-269,-268,-267,-266,-265,-264,-263,277,-336,-335,-334,-333,-423,-422,-421,-52,-51,-50,-107,-106,-105,-104,-103,-102,-101,-100,-99,-98,402,449,450,451,452,-220,-356,-355,-354,20,21,178,179,180,-23,-22,-177,-176,-175,-174,-279,-278,337,181,359,360,361,362,-388,-387,241,242,243,244,245,246,-248,-247,445,-425,-424,-332,-331,136,137,79,80,81,-76,-75,-74,-73,-72,-71,-70,-69,-443,-442,-212,-211,-210,-209,-208,-207,448,403,404,405,406,407,408,409,-372,-371,-370,-369,-368,-367,-366,53,54,-481,-480,-479,-478,-477,-476,-475,-474,-473,453,454,455,-113,-112,-111,-110,-109,380,381,-131,-130,-129,-128,-127,25,302,303,-358,-357,-219,-218,-217,-216,-215,-214,-213,-441,-440,-439,-438,-437,-61,-60,-59,-58,-57,-56,-55,482,-308,-307,-306,432,433,434,435,436,62,63,64,65,66,67,68,-19,-18,-17,-16,-15,-14,-13,-12,-11,35,36,37,-420,-419,-418,-417,-416,-415,-414,-204,-463,-462,-461,-460,-459,-458,-457,-456,114,115,116,-198,-197,-196,-195,-194,-193,-192,-191,-190,-189,-188,-187,-186,-185,-184,-183,46,47,48,49,-154,-153,-152,-151,-150,-149,-148,-147,-146,-145,-144,-143,-142,-141,-379,-378,-377,-376,-375,-374,498,499,500,501}; + _uniform.assign(uniform,uniform+501); + + int axis[] = {300,-298,302,303,-295,305,306,307,308,309,310,311,312,313,314,-284,-283,-282,-281,-280,-279,-278,-277,-276,-275,-274,-273,327,328,-270,-269,331,-267,-266,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,-250,-249,-248,-247,-246,-245,355,356,357,358,359,360,361,-237,-236,-235,365,-233,367,368,369,370,371,372,373,374,-224,376,377,378,-220,-219,-218,-217,383,-215,-214,-213,-212,-211,-210,-209,-208,-207,-206,-205,-204,-203,-202,398,-200,400,-198,-197,-196,-195,-194,-193,-192,-191,-190,-189,-188,-187,-186,-185,-184,-183,-182,418,419,420,421,422,-176,-175,-174,426,427,428,429,-169,-168,432,433,434,435,436,437,438,439,440,-158,-157,443,444,445,-153,447,448,449,450,-148,-147,-146,-145,455,456,457,458,-140,-139,-138,-137,-136,464,465,466,467,468,469,-129,-128,-127,-126,-125,-124,-123,-122,-121,-120,480,481,482,483,484,485,486,-112,-111,489,490,491,492,493,-105,-104,496,-102,-101,499,500,501,-97,-96,-95,-94,-93,-92,7,8,9,10,-87,12,13,14,15,16,17,18,19,20,21,-76,23,24,25,26,27,28,29,30,31,-66,-65,-64,-63,36,37,38,39,40,-57,-56,-55,-54,-53,-52,47,48,-49,50,51,-46,-45,-44,-43,-42,-41,58,59,60,61,62,-35,-34,-33,-32,67,68,69,70,71,72,73,74,75,-22,77,78,79,80,81,82,83,84,85,86,-11,88,89,90,91,-6,-5,-4,-3,-2,-1,98,99,100,-498,-497,103,-495,-494,106,107,108,109,110,-488,-487,113,114,115,116,117,118,119,-479,-478,-477,-476,-475,-474,-473,-472,-471,-470,130,131,132,133,134,135,-463,-462,-461,-460,-459,141,142,143,144,-454,-453,-452,-451,149,150,151,152,-446,154,155,156,-442,-441,159,160,161,162,163,164,165,166,167,-431,-430,170,171,172,173,-425,-424,-423,177,178,179,180,181,-417,-416,-415,-414,-413,-412,-411,-410,-409,-408,-407,-406,-405,-404,-403,-402,-401,199,-399,201,-397,-396,-395,-394,-393,-392,-391,-390,-389,-388,-387,-386,-385,-384,216,-382,-381,-380,-379,221,222,223,-375,225,226,227,228,229,230,231,232,-366,234,-364,-363,-362,238,239,240,241,242,243,244,-354,-353,-352,-351,-350,-349,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,-333,-332,268,-330,-329,271,272,-326,-325,-324,-323,-322,-321,-320,-319,-318,-317,-316,-315,285,286,287,288,289,290,291,292,293,294,-304,296,297,-301,299}; + _1axis.assign(axis,axis+501); + } + +protected: + + void testCreate (){ + CPPUNIT_ASSERT_NO_THROW(ModelIdentifier mi("default.ann")); + CPPUNIT_ASSERT_THROW(ModelIdentifier mi("\\adfalkj234208934jfwldrj"),invalid_argument); + CPPUNIT_ASSERT_THROW(ModelIdentifier mi("/etc/passwd"),invalid_argument); + } + void testIdentify (){ + 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); + + 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); + + 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); + + 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); + } +}; + +CPPUNIT_TEST_SUITE_REGISTRATION( TESTNAME ); + +#undef TESTNAME -- 2.39.2