Hello every one,
I wrote a code for HMM model that I am building, and am having a problem with this function.The code compiles fine but when I run it, there is a segmentation fault. The segmentation fault occurs in the red coded area.
Now the reason is that BLUE loop is running more than the structure of the graph in the nested loop. Hence, after the first iteration(when the graph is iterated) I get a segmentation fault.Please note that I want to iterate the same graph again and again.
To counter this, I initialized the iterator for the graph coded in Yellow.But it seems that the iterator is not initialized and I am having the same segmentation fault. Any help would be highly appreciable.
Code:
Mat<double> HMM::reverse_partition_function(sequences* s, unsigned int line){
unsigned int seq_len=int(s->seq[line].size());
Mat<double> Z(tiny, int(states.size()), int(seq_len));//in log-space
graph_type graph;
graph_type graph1;
int new_state;
int old_state;
//building the graph with required nodes and edges
std::cout << "start of the loop 1" << endl;
graph1=build_graph();
graph=inverted(graph1);
std::cout << "start of the loop 2" << endl;
for(unsigned int k=0;k<seq_len;k++){
std::cout << "start of the loop fuck" << endl;
edge_list_type::const_iterator cur_edge;
graph_type::const_iterator cur_node;
unsigned int j=seq_len-1-k;
if(j!=0){
for (graph_type::const_iterator cur_node = graph.begin() ,end_node = graph.end() ;cur_node != end_node; ++cur_node){
// cur_node->first will give the new_state
std::cout << "start of the loop 3" << endl;
// for(unsigned int s_new=0;s_new<states.size();s_new++){
vector<double> summands;
for (edge_list_type::const_iterator cur_edge = cur_node->second.begin(), end_edge = cur_node->second.end(); cur_edge != end_edge; ++cur_edge){
// *cur_edge will give the old_state
// for(unsigned int s_old=0;s_old<states.size();s_old++){
std::cout << "start of the loop 4" << endl;
for(int l=0;l<=max_len;l++){
double prior=log(prior_p[cur_node->first][*cur_edge]);//going backwards through model
// double prior=0;
if(j+l<seq_len){
cout << "Start of loop 5"<< endl;
cout << "value in new state"<< cur_node->first <<endl;
cout << "value in old state"<< *cur_edge <<endl;
// double sc=0;
double sc=states[cur_node->first]->score(s, line, j, l);
summands.push_back(prior+sc+(Z[*cur_edge][j+l]));
// summands.push_back(0);
}
}
}
for(int l=1;l<=1;l++){//end state only allows transition into background
if(j+l==seq_len){
if(end_tr[cur_node->first]>0){
double prior=log(end_tr[cur_node->first]);
double sc=states[cur_node->first]->score(s, line, j, l);
summands.push_back(sc+prior);
}
}
}
if(summands.size()>0){
Z[cur_node->first][j]=add_in_log_space(summands);
}
else{
Z[cur_node->first][j]=tiny;
}
}
}
else{
for(unsigned int s_new=0;s_new<states.size();s_new++){
if(start_tr[s_new]>0){
double start_prior=log(start_tr[s_new]);
vector<double> summands;
for(unsigned int s_old=0;s_old<states.size();s_old++){
for(int l=0;l<=max_len;l++){
if(connectivity[s_new][s_old]>0){
double prior=log(prior_p[s_new][s_old]);
if(j+l<seq_len){
double sc=states[s_new]->score(s, line, j, l);
summands.push_back(start_prior+prior+sc+(Z[s_old][j+l]));
}
if(j+l==seq_len){
if(end_tr[s_new]>0){
double prior=log(end_tr[s_new]);
double sc=states[s_new]->score(s, line, j, l);
summands.push_back(sc+prior+start_prior);
}
}
}
}
}
if(summands.size()>0){
Z[s_new][j]=add_in_log_space(summands);
}
else{
Z[s_new][j]=tiny;
}
}
}
}
}
return Z;
}
I am using this for building the graph.
Code:
using namespace std;
typedef std::vector<int> edge_list_type;
typedef std::map<int, edge_list_type> graph_type;
graph_type HMM::build_graph(){
graph_type graph;
// Enter the nodes of the graph
graph.insert(std::make_pair(1, std::vector<int>()));
graph.insert(std::make_pair(2, std::vector<int>()));
graph.insert(std::make_pair(3, std::vector<int>()));
graph.insert(std::make_pair(4, std::vector<int>()));
// Enter the edges of the graph
// Note that the edges are directed
// graph[Edge_from].push_back(Edge_to);
graph[1].push_back(1);
graph[1].push_back(2);
graph[2].push_back(4);
graph[3].push_back(1);
graph[4].push_back(3);
return graph;
}