ToPS
FactorableModel.cpp
00001 /*
00002  *       FactorableModel.cpp
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 #include "FactorableModel.hpp"
00026 
00027 namespace tops {
00028     double FactorableModel::evaluate(const Sequence & s, unsigned int begin, unsigned int end) const
00029     {
00030         double result = 0.0;
00031         if (end >= s.size())
00032             return -HUGE;
00033         if(begin < 0)
00034             return -HUGE;
00035         for(unsigned int i = begin; i <= end; i++)
00036             {
00037                 result += evaluatePosition(s, i);
00038             }
00039         return result;
00040     }
00041 
00042     Sequence & FactorableModel::choose(Sequence & h, int size) const
00043     {
00044         chooseWithHistory(h,0,size);
00045         return h;
00046     }
00047 
00048 
00049     Sequence & FactorableModel::chooseWithHistory(Sequence & h, int i,  int size) const
00050     {
00051         for (int k = i; k < (size+i); k++)
00052             {
00053                 if (k < (int)h.size())
00054                     h[k] = choosePosition(h, k);
00055                 else
00056                     h.push_back(choosePosition(h, k));
00057             }
00058         return h;
00059     }
00060 
00061     double FactorableModel::prefix_sum_array_compute(int begin, int end)
00062     {
00063         if(begin>end)
00064             return -HUGE;
00065 
00066         if((begin >= 0) && ((end + 1) < (int) _alpha.size()))
00067             {
00068                 if((_precision[end+1] - _precision[begin]) > 0)
00069                     return -HUGE;
00070                 return _alpha[end+1] - _alpha[begin];
00071             }
00072         else
00073             return -HUGE;
00074 
00075     }
00076 
00077     bool FactorableModel::initialize_prefix_sum_array(const Sequence & s)
00078     {
00079         if(ProbabilisticModel::initialize_prefix_sum_array(s))
00080             {
00081                 return true;
00082             }
00083         _alpha.resize(s.size() + 1);
00084         _precision.resize(s.size() + 1);
00085         _alpha[0] = 0;
00086         for(int i = 0 ; i < (int) s.size() ; i++) {
00087             double prob = evaluate(s, i, i);
00088             if(close(prob, -HUGE, 1e-1))
00089                 {
00090                     _precision[i+1] = _precision[i]+1;
00091                     _alpha[i+1] = evaluate(s,i,i);
00092                 }
00093             else
00094                 {
00095                     _alpha[i+1] = _alpha[i] +   prob;
00096                     _precision[i+1] = _precision[i];
00097                 }
00098         }
00099 #if 0
00100         for(int i = 0; i < s.size(); i++)
00101             std::cerr << " "  << i << " " <<  _alpha[i] << " " << _precision[i] << std::endl;
00102 #endif
00103         return true;
00104     }
00105 
00106 
00107 }