// file: dtn_question_3.cc
//

// isip include files
//
#include "dt_node.h"
#include "dt_node_constants.h"

// method: find_opt_question_cc
//
// arguments:
//   Question** question: (input) questions to ask
//   Hash_table* answer: (input) Hash_table with all the answers
//   State** states: (input) all the states
//   float_4* occupy: (input) state occupancies
//   int_4** l_context: (input) the left contexts
//   int_4** r_context: (input) the right contexts
//   float_4 occ_threshold : minimum occupancy required for children nodes
//   float_4& l_child_num_occ: the number of states occupancy of left child
//   float_4& r_child_num_occ: the number of states occupancy of right child
//   int_4 num_feat_a: (input) number of features
//   int_4 num_mix_a:  (input) number of mixtures
//
// return: a logical_1 indicating status
//
// this methods finds the optimal question for the node which can give the
// maximum likelihood increase
//
logical_1 Dt_node::find_opt_question_cc(Question** questions_a,
					Hash_table* answers_a,
					State** states_a, float_4* occupy_a,
					int_4** l_context_a,
					int_4** r_context_a,
					float_4 occ_threshold_a,
					float_4& l_child_num_occ_a,
					float_4& r_child_num_occ_a,
					int_4 num_questions_a,
					int_4 num_feat_a, int_4 num_mix_a) {
  
  // local variables
  //
  Dt_node* tmp_l_node = new Dt_node();
  Dt_node* tmp_r_node = new Dt_node();
  
  float_4 l_likelihood;
  float_4 r_likelihood;
  float_4 new_likelihood;
  float_4 l_temp_occ = 0;
  float_4 r_temp_occ = 0;
  float_4 max_likelihood = MIN_LIKELIHOOD;
  int_4 opt_q_ind = -1;
  int_4 i;
  
  // loop all the questions, find the one that gives the maximum likelihood
  // increase when splitting
  //
  for (i=0; i<num_questions_a; i++) {
    
    if(split_node_cc(questions_a[i], answers_a, tmp_l_node, tmp_r_node,
		     l_context_a, r_context_a) == ISIP_TRUE) {
      
      l_likelihood = tmp_l_node->compute_likelihood_cc(states_a, occupy_a,
						       num_feat_a, num_mix_a);
      r_likelihood = tmp_r_node->compute_likelihood_cc(states_a, occupy_a,
						       num_feat_a, num_mix_a);
      new_likelihood = l_likelihood + r_likelihood;
      
      // count the number of states occupancy belonging to each child
      //
      l_temp_occ = tmp_l_node->compute_occupancy_cc(occupy_a);
      r_temp_occ = tmp_r_node->compute_occupancy_cc(occupy_a);
      if ((l_temp_occ > occ_threshold_a &&
	   r_temp_occ > occ_threshold_a) &&
	  new_likelihood > max_likelihood) {
	max_likelihood = new_likelihood;
	opt_q_ind = i;
	l_child_num_occ_a = l_temp_occ;
	r_child_num_occ_a = r_temp_occ;	
      }      
    }
    
    // free memory
    //
    tmp_l_node->clear_cc();
    tmp_r_node->clear_cc();
  }
  
  // set the optimal question
  //
  if (opt_q_ind >= 0) {
    opt_question_d = questions_a[opt_q_ind];
  }
  else {
    opt_question_d = (Question*)NULL;
  }
  
  // free memory
  //
  delete tmp_l_node;
  delete tmp_r_node;
  
  //exit gracefully
  //
  return ISIP_TRUE;   
}
