ToPS
|
00001 /* 00002 * InhomogeneousFactorableModel.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 "InhomogeneousFactorableModel.hpp" 00026 #include "Symbol.hpp" 00027 namespace tops { 00028 00029 double InhomogeneousFactorableModel::evaluate(const Sequence & s, unsigned int begin, unsigned int end) const 00030 { 00031 double result = 0.0; 00032 int t = 0; 00033 int maximumTime = maximumTimeValue(); 00034 for (int i = (int) begin; (i <= (int) end); i++) { 00035 if (((i - (int) begin) > maximumTime) 00036 && (!isPhased())) 00037 break; 00038 result += evaluatePosition(s, i, t); 00039 t = mod(t + 1, maximumTime + 1); 00040 } 00041 return result; 00042 } 00043 00044 00045 double InhomogeneousFactorableModel::evaluate(const Sequence & s, unsigned int begin, unsigned int end, int phase) const 00046 { 00047 double result = 0.0; 00048 if(isPhased()) { 00049 int t = phase; 00050 int maximumTime = maximumTimeValue(); 00051 for (int i = (int) begin; (i <= (int) end); i++) { 00052 if (((i - (int) begin) > maximumTime) 00053 && (!isPhased())) 00054 break; 00055 result += evaluatePosition(s, i, t); 00056 t = mod(t + 1, maximumTime + 1); 00057 } 00058 return result; 00059 } 00060 else 00061 return evaluate(s, begin, end); 00062 } 00063 00064 00065 Sequence & InhomogeneousFactorableModel::choose(Sequence & h, int size) const 00066 { 00067 chooseWithHistory(h,0,size); 00068 return h; 00069 } 00070 00071 Sequence & InhomogeneousFactorableModel::chooseWithHistory(Sequence & h, int i, int size) const 00072 { 00073 chooseWithHistory(h,i,0,size); 00074 return h; 00075 } 00076 00077 double InhomogeneousFactorableModel::prefix_sum_array_compute(int begin, int end, int phase) { 00078 if (isPhased()) { 00079 int nphase = maximumTimeValue()+1; 00080 int p = mod(begin, nphase); 00081 int k = 0; 00082 if(begin < 0) 00083 return -HUGE; 00084 while ((phase >= 0) && (mod(p + k, nphase) != phase)) 00085 k++; 00086 p = k; 00087 00088 if ((end + 1) >= (int) _precision[p].size()) 00089 end = _precision[p].size() - 2; 00090 double t = _precision[p][end + 1] - _precision[p][begin]; 00091 if ((t > 0) || (begin > end)) 00092 return -HUGE; 00093 if(_alpha[p][end + 1] - _alpha[p][begin] > 0) 00094 { 00095 std::cerr << "ERROR: InhomogeneousFactorableModel " << begin << " " << end << " " << _alpha[p][end + 1] << " " << _alpha[p][begin] << " " << t << std::endl; 00096 } 00097 assert(_alpha[p][end + 1] - _alpha[p][begin] <= 0); 00098 00099 return _alpha[p][end + 1] - _alpha[p][begin]; 00100 } else { 00101 if ((begin < (int) _scores.size()) && (begin >= 0)) 00102 return _scores[begin]; 00103 return -HUGE; 00104 } 00105 00106 } 00107 double InhomogeneousFactorableModel::prefix_sum_array_compute(int begin, int end) 00108 { 00109 return prefix_sum_array_compute(begin, end, 0); 00110 } 00111 00112 bool InhomogeneousFactorableModel::initialize_prefix_sum_array(const Sequence & s, int phase) 00113 { 00114 if(ProbabilisticModel::initialize_prefix_sum_array(s)) 00115 return true; 00116 00117 if (isPhased()) 00118 { 00119 00120 00121 _phase = phase; 00122 int nphases = maximumTimeValue()+1; 00123 _alpha.resize(nphases); 00124 _precision.resize(nphases); 00125 for (int k = 0; k < nphases; k++) { 00126 _alpha[k].resize(s.size() + 1); 00127 _precision[k].resize(s.size() + 1); 00128 } 00129 for (int k = 0; k < nphases; k++) { 00130 _alpha[k][0] = 0; 00131 _precision[k][0] = 0; 00132 for (int i = 0; i < (int) s.size(); i++) { 00133 int p = mod(i + k, nphases); 00134 double prob = evaluatePosition(s, i, p); 00135 if ((prob != prob) || close(prob, -HUGE, 1e-10)) { 00136 _precision[k][i + 1] = _precision[k][i] + 1; 00137 _alpha[k][i + 1] = 0; 00138 } else { 00139 _precision[k][i + 1] = _precision[k][i]; 00140 _alpha[k][i + 1] = _alpha[k][i] + prob; 00141 } 00142 #if 0 00143 std::cerr << i << " " << alphabet()->getSymbol(s[i])->name() << " " << k << " " << p << " " << prob << " " << _alpha[k][i] << " " << _precision[k][i] << " " << std::endl; 00144 #endif 00145 } 00146 00147 } 00148 00149 } 00150 else { 00151 _scores.resize(s.size()); 00152 for (int i = 0; i < (int) s.size(); i++) 00153 _scores[i] = evaluate(s, i, s.size() - 1); 00154 } 00155 return true; 00156 } 00157 00158 Sequence & InhomogeneousFactorableModel::chooseWithHistory(Sequence & h, int i, int initial_phase, int size) const{ 00159 int maximumTime = maximumTimeValue(); 00160 int t = initial_phase; 00161 if(t < 0) t = 0; 00162 if(isPhased()) 00163 t = mod(initial_phase, maximumTime+1); 00164 if((size > maximumTime) && !isPhased()) 00165 size = maximumTime+1; 00166 00167 for(int k = i; (k < (size+i)); k++) 00168 { 00169 if( (t > maximumTime) && (!isPhased())) 00170 break; 00171 if(k < (int)h.size()) 00172 h[k] = choosePosition(h, k, t); 00173 else 00174 h.push_back(choosePosition(h, k,t)); 00175 t = mod(t+1, maximumTime+1); 00176 } 00177 return h; 00178 00179 } 00180 }