ToPS
ProbabilisticModel.hpp
00001 /*
00002  *       ProbabilisticModel.hpp
00003  *
00004  *       Copyright 2011 Andre Yoshiaki Kashiwabara <akashiwabara@usp.br>
00005  *                      Ígor Bonádio <ibonadio@ime.usp.br>
00006  *                      Vitor Onuchic <vitoronuchic@gmail.com>
00007  *                      Alan Mitchell Durham <aland@usp.br>
00008  *
00009  *       This program is free software; you can redistribute it and/or modify
00010  *       it under the terms of the GNU  General Public License as published by
00011  *       the Free Software Foundation; either version 3 of the License, or
00012  *       (at your option) any later version.
00013  *
00014  *       This program is distributed in the hope that it will be useful,
00015  *       but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *       GNU General Public License for more details.
00018  *
00019  *       You should have received a copy of the GNU General Public License
00020  *       along with this program; if not, write to the Free Software
00021  *       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00022  *       MA 02110-1301, USA.
00023  */
00024 
00025 #ifndef PROBABILISTIC_MODEL_HPP
00026 #define PROBABILISTIC_MODEL_HPP
00027 
00028 #include "crossplatform.hpp"
00029 
00030 #include <cmath>
00031 #include <iostream>
00032 
00033 #include <vector>
00034 #include <cstdarg>
00035 #include <boost/shared_ptr.hpp>
00036 
00037 #include "util.hpp"
00038 #include "Sequence.hpp"
00039 #include "Alphabet.hpp"
00040 #include "ProbabilisticModelParameter.hpp"
00041 #include "GHMMStates.hpp"
00042 
00043 namespace tops {
00044   class DLLEXPORT DecodableModel;
00045   class DLLEXPORT PairHiddenMarkovModel;
00046   class DLLEXPORT FactorableModel;
00047   class DLLEXPORT InhomogeneousFactorableModel;
00048   class DLLEXPORT ProbabilisticModelCreator;
00049   typedef boost::shared_ptr<ProbabilisticModelCreator> ProbabilisticModelCreatorPtr;
00050 
00052   class DLLEXPORT ProbabilisticModel
00053   {
00054   private:
00055     AlphabetPtr _alphabet;
00056     Sequence  _last;
00057     double _dummy;
00058   public:
00059     ProbabilisticModel () {
00060       _alphabet = AlphabetPtr(new Alphabet());
00061     }
00062     ProbabilisticModel( AlphabetPtr alpha) : _alphabet(alpha)   {    };
00063     virtual ~ProbabilisticModel () {}
00064 
00066     virtual double evaluate(const Sequence & s, unsigned int begin, unsigned int end) const {
00067       not_implemented("evaluate()");
00068       return 0;
00069     }
00070 
00072     virtual double evaluatePosition(const Sequence & s, unsigned int i) const {
00073       not_implemented("evaluatePosition()");
00074       return 0;
00075     }
00076 
00077 
00079       virtual double evaluatePosition(const Sequence & s,  int i,  int t) const {
00080           evaluatePosition(s, i);
00081           return 0;
00082       }
00083 
00084 
00086     virtual double choose() const {
00087       not_implemented("choose()");
00088       return 0;
00089     }
00090     virtual Sequence & choose(Sequence & h, int size) const {
00091       not_implemented("choose(Sequence &h, int size)");
00092       return h;
00093     }
00094 
00095     virtual Sequence & choose(Sequence &h, int initial_phase, int size) const{
00096       choose(h, size);
00097       not_implemented("choose(Sequence &h, int initial_phase, int size)");
00098       return h;
00099     }
00100 
00101     virtual Sequence & choose(Sequence & h, Sequence & path,  int size) const
00102     {
00103       choose(h, size);
00104       //      not_implemented("choose(Sequence &h, Sequence & path, int size)");
00105       return h;
00106     }
00107 
00108 
00109     virtual Sequence & choose(Sequence & h, Sequence & path,  int i, int size) const
00110     {
00111       not_implemented("choose(Sequence &h, Sequence & path, int size)");
00112       return h;
00113     }
00114 
00115     virtual Sequence & chooseWithHistory(Sequence & h, int i,  int size) const
00116     {
00117       choose(h,size);
00118       return h;
00119     }
00120 
00121     virtual Sequence & chooseWithHistory(Sequence & h, int i,  int phase, int size) const{
00122       chooseWithHistory(h,i, size);
00123       return h;
00124     }
00125 
00127     virtual AlphabetPtr alphabet() const
00128     {
00129       return _alphabet;
00130     }
00132     virtual void setAlphabet(AlphabetPtr alphabet)
00133     {
00134       _alphabet = alphabet;
00135     }
00137     virtual std::string str() const {
00138       std::stringstream out;
00139       not_implemented("str()");
00140       return out.str();
00141     }
00142 
00144     virtual std::string model_name() const{
00145       std::stringstream out;
00146       not_implemented("model_name()");
00147       return out.str();
00148     }
00149 
00151     virtual int size() const {
00152       not_implemented("size()");
00153       return 0;
00154     }
00155 
00156     virtual ProbabilisticModelParameters parameters() const {
00157       ProbabilisticModelParameters p;
00158       std::cerr << "ERROR: The " << model_name() << " does not implement parameters" << std::endl;
00159       exit(-1);
00160       return p;
00161     }
00162 
00163     virtual void initialize(const ProbabilisticModelParameters & p) {
00164       std::cerr << "ERROR: The " << model_name() << " does not implement initialize" << std::endl;
00165       exit(-1);
00166     }
00167 
00168     virtual void not_implemented (std::string method ) const {
00169       std::cerr << "ERROR: The " << model_name() << " does not implement " << method << std::endl;
00170     }
00172     virtual double  log_probability_of(int s) const {
00173       not_implemented("log_probability_of(int s) const");
00174       return 0.0;
00175     }
00176 
00178     virtual double probability_of(int s, double new_value) {
00179       not_implemented("probability_of(int s, double new_value)");
00180       return _dummy;
00181     }
00182     virtual double evaluate(const Sequence & s, unsigned int begin, unsigned int end, int phase) const {
00183       //      not_implemented("evaluate(const Sequence & s int begin, int end, int phase)");
00184       return evaluate(s, begin, end);
00185 
00186     }
00187 
00188     virtual double prefix_sum_array_compute(int begin , int end) {
00189         std::cerr << "WARNING: " << model_name() << " does not implement prefix_sum_array " << std::endl;
00190       return evaluate(_last, begin, end);
00191     }
00192 
00193     virtual double prefix_sum_array_compute(int begin , int end, int phase) {
00194       return prefix_sum_array_compute(begin,end);
00195     }
00196 
00197     virtual bool initialize_prefix_sum_array(const Sequence & s, int phase) {
00198       return initialize_prefix_sum_array(s);
00199     }
00200     virtual bool initialize_prefix_sum_array(const Sequence & s) {
00201       if(_last == s)
00202         return true;
00203       _last = s;
00204       return false;
00205     }
00206     virtual void removeSequenceFromModel(const Sequence & s, int phase) {
00207       not_implemented("removeSequenceFromModel()");
00208     }
00209     virtual AlphabetPtr getStateNames() const {
00210       AlphabetPtr n;
00211       not_implemented("getStateNames()");
00212       return n;
00213     }
00214 
00216     virtual void trainBaumWelch (SequenceList & training_set, int maxiterations, double diff) {
00217       not_implemented("trainBaumWelch");
00218     }
00219 
00220 
00221     virtual InhomogeneousFactorableModel* inhomogeneous() {
00222         //      not_implemented("inhomogeneous()");
00223       return NULL;
00224     }
00225     virtual DecodableModel* decodable() {
00226         //      not_implemented("decodable()");
00227       return NULL;
00228     }
00229 
00230     virtual PairHiddenMarkovModel* pairDecodable() {
00231       not_implemented("pairDecodable()");
00232       return NULL;
00233     }
00234 
00235     virtual FactorableModel* factorable() {
00236         //      not_implemented("factorable()");
00237       return NULL;
00238     }
00239     virtual std::string print_graph () const {
00240       not_implemented("print_graph()");
00241       return "";
00242     }
00243   };
00244 
00245   typedef boost::shared_ptr<ProbabilisticModel> ProbabilisticModelPtr;
00246 };
00247 #endif