// file: $isip/class/pr/RegressionDecisionTree/RegressionDecisionTree.h
// version: $Id: RegressionDecisionTree.h 9470 2004-05-10 15:40:08Z gao $
//

// make sure definitions are only made once
//
#ifndef ISIP_REGRESSION_DECISION_TREE
#define ISIP_REGRESSION_DECISION_TREE

// isip include files
//
#ifndef ISIP_DECISION_TREE_BASE
#include <DecisionTreeBase.h>
#endif

#ifndef ISIP_REGRESSION_DECISION_TREE_NODE
#include <RegressionDecisionTreeNode.h>
#endif

#ifndef ISIP_STATISTICAL_MODEL
#include <StatisticalModel.h>
#endif

#ifndef ISIP_GAUSSIAN_MODEL
#include <GaussianModel.h>
#endif

#ifndef ISIP_MIXTURE_MODEL
#include <MixtureModel.h>
#endif

#ifndef ISIP_BIGRAPH_ARC
#include <BiGraphArc.h>
#endif

#ifndef ISIP_BIGRAPH_VERTEX
#include <BiGraphVertex.h>
#endif

// forward class definitions:
//  we must define the RegressionDecisionTreeNode class here first
//  because the header files might be short-circuited by the ifndef.
//
class RegressionDecisionTreeNode;

// RegressionDecisionTree: a class that build a RegressionDecisionTree
// by giving statistical models and speech/nonspeech tag for
// statistical models.
//
class RegressionDecisionTree: public DecisionTreeBase<RegressionDecisionTreeNode> {

  //---------------------------------------------------------------------------
  //
  // public constants
  //
  //---------------------------------------------------------------------------
 public:
  
  // define the class name
  //
  static const String CLASS_NAME;
  
  //----------------------------------------
  //
  // other important constants
  //
  //----------------------------------------
  
  // define the algorithm choices
  //
  enum ALGORITHM { MLLR = 0, DEF_ALGORITHM = MLLR };
  
  // define the implementation choices
  //
  enum IMPLEMENTATION { MEAN = 0, VARIANCE, COMBINED,
			DEF_IMPLEMENTATION = MEAN };

  // define the adaptation supervision choices
  //
  enum SUPERVISION_MODE { SUPERVISED = 0, UNSUPERVISED,
			  DEF_SUPERVISION_MODE = SUPERVISED };

  // define the adaptation sequence choices
  //
  enum SEQUENCE_MODE { BATCH = 0, INCREMENTAL,
		       DEF_SEQUENCE_MODE = BATCH };
  
  // define the static NameMap objects
  //
  static const NameMap ALGO_MAP;
  static const NameMap IMPL_MAP;
  static const NameMap SUP_MODE_MAP;
  static const NameMap SEQ_MODE_MAP;
  
  //----------------------------------------
  //
  // i/o related constants
  //
  //----------------------------------------

  static const String DEF_PARAM;
  static const String PARAM_ALGORITHM;
  static const String PARAM_IMPLEMENTATION;
  static const String PARAM_SUPERVISION_MODE;
  static const String PARAM_SEQUENCE_MODE;  
  static const String PARAM_SPEECH_FLAG;
  static const String PARAM_NUM_TERMINALS;
  static const String PARAM_PERTURB_DEPTH;
  static const String PARAM_OCC_THRESHOLD;
  static const String PARAM_COMPONENTS_THRESHOLD;
  static const String PARAM_MIXTURE_OFFSET;
  static const String PARAM_MAP_STAT_TO_TRANS;
  
  
  static const String PARAM_SPLIT_THRESHOLD;
  static const String PARAM_MERGE_THRESHOLD;
  static const String PARAM_NUM_OCC_THRESHOLD;
  static const String PARAM_BDT;

  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------  

  // define default values for the thresholds
  //
  static const long DEF_NUM_TERMINALS = 1;
  static const boolean DEF_SPEECH_FLAG = true;
  static const double DEF_PERTURB_DEPTH = 0.2;
  static const double DEF_OCC_THRESHOLD = 1000.0;
  static const long DEF_COMPONENTS_THRESHOLD = 100;  
  static const float DEF_MERGE_THRESHOLD = 5;
  static const float DEF_NUM_OCC_THRESHOLD = 100;
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  
  
  static const long ERR = 00100500;
  static const long ERR_ADAPT_NO_GAUSSIAN = 00100501;
  static const long ERR_UNALG_UNIMP = 00100502;  

  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:

  // define the structures
  //
  typedef Triple<Long, Long, Long> RDataPoint;
  typedef SingleLinkedList<RDataPoint> RData;
  
  typedef BiGraphVertex<RegressionDecisionTreeNode> RTreeNode;
  
  // algorithm name
  //
  ALGORITHM algorithm_d;

  // implementation name
  //
  IMPLEMENTATION implementation_d;

  // adaptation supervision mode
  //
  SUPERVISION_MODE supervision_mode_d;

  // adaptation sequence mode
  //
  SEQUENCE_MODE sequence_mode_d;
  
  // data on the root node
  //
  RegressionDecisionTreeNode rdt_rootnode_d;

  // flag to indicate if adapt non-speech sound
  //
  Boolean speech_flag_d;
  
  // total number of terminals
  //
  Long num_terminals_d;

  // perturb depth
  // 
  Double perturb_depth_d;

  // minimum occupation count for adaptation
  //
  Double occ_threshold_d;
  
  // minimal components for each node to split
  //
  Long components_threshold_d;
  
  // variables use to get the transform id from statistical model
  // index and gaussian model index
  //
  VectorLong mixture_offset_d;
  VectorLong map_stat_to_trans_d;
    
  // local copy of the statistical model
  //
  Vector<StatisticalModel> stat_models_d;

  // static memory manager
  //
  static MemoryManager mgr_d;

  //---------------------------------------------------------------------------
  //
  // required public methods
  //
  //---------------------------------------------------------------------------
public:
    
  // method: name
  //
  static const String& name() {
    return CLASS_NAME;
  }

  // other static methods
  //
  static boolean diagnose(Integral::DEBUG debug_level);
  
  // debug methods:
  //  setDebug is inherited from the base class
  //
  boolean debug(const unichar* msg) const;

  // method: destructor
  //
  ~RegressionDecisionTree() {
  }

  // method: default constructor
  //
  RegressionDecisionTree(ALGORITHM algorithm = DEF_ALGORITHM,
			 IMPLEMENTATION implementation = DEF_IMPLEMENTATION) {

    algorithm_d = algorithm;
    implementation_d = implementation;
    num_terminals_d = DEF_NUM_TERMINALS;
  }

  // method: copy constructor
  //
  RegressionDecisionTree(const RegressionDecisionTree& arg) {
    assign(arg);
  }

  // assign methods
  //
  boolean assign(const RegressionDecisionTree& arg);

  // method: operator=
  //
  RegressionDecisionTree& operator= (const RegressionDecisionTree& arg) {
    assign(arg);
    return *this;
  }
  
  // i/o methods
  //
  long sofSize() const;
  
  // method: read
  //
  boolean read(Sof& sof, long tag) {
    return read(sof, tag, name());
  }
  boolean read(Sof& sof, long tag, const String& name);

  // method: write
  //
  boolean write(Sof& sof, long tag) const {
    return write(sof, tag, name());
  }
  boolean write(Sof& sof, long tag, const String& name) const;

  // method: readData
  //
  boolean readData(Sof& sof, const String& pname = DEF_PARAM,
		   long size = SofParser::FULL_OBJECT, boolean param = true,
                   boolean nested = false);

  // method: writeData
  //
  boolean writeData(Sof& sof, const String& pname = DEF_PARAM) const;
   
  // equality methods
  //
  boolean eq(const RegressionDecisionTree& arg) const;
  
  // method: new
  //
  static void* operator new(size_t size) {
    return mgr_d.get();
  }

  // method: new[]
  //
  static void* operator new[](size_t size) {
    return mgr_d.getBlock(size);
  }

  // method: delete
  //
  static void operator delete(void* ptr) {
    mgr_d.release(ptr);
  }

  // method: delete[]
  //
  static void operator delete[](void* ptr) {
    mgr_d.releaseBlock(ptr);
  }

  // method: setGrowSize
  //
  static boolean setGrowSize(long grow_size) {
    return mgr_d.setGrow(grow_size);
  }

  // other memory management methods
  //
  boolean clear(Integral::CMODE ctype = Integral::DEF_CMODE);

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  set methods
  //
  //---------------------------------------------------------------------------

  // method: setAlgorithm
  //
  boolean setAlgorithm(ALGORITHM algorithm) {
    algorithm_d = algorithm;
    return true;
  }

  // method: setImplementation
  //
  boolean setImplementation(IMPLEMENTATION implementation) {
    implementation_d = implementation;
    return true;  
  }

  // method: set
  //
  boolean set(ALGORITHM algorithm = DEF_ALGORITHM,
	      IMPLEMENTATION implementation = DEF_IMPLEMENTATION) {

    algorithm_d = algorithm;
    implementation_d = implementation;
    return true;
  }

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  get methods
  //
  //---------------------------------------------------------------------------

  // method: getAlgorithm
  //
  ALGORITHM getAlgorithm() const {
    return algorithm_d;
  }  

  // method: getImplementation
  //
  IMPLEMENTATION getImplementation() const {
    return implementation_d;
  }

  // method: get
  //
  boolean get(ALGORITHM& algorithm, IMPLEMENTATION& implementation) {

    algorithm = algorithm_d;
    implementation = implementation_d;
    return true;
  }

  // method: getMixtureOffset
  //
  boolean getMixtureOffset(VectorLong& mixture_offset) const {
    mixture_offset.assign(mixture_offset_d);
    return true;
  }

  // method: getStatToTrans
  //
  boolean getStatToTrans(VectorLong& stat_to_trans) const {
    stat_to_trans.assign(map_stat_to_trans_d);
    return true;
  }
  
  // method to init the regression tree
  //
  boolean initRegressionTree(Vector<StatisticalModel>& stat_models_a,
			     Vector <Boolean>& speech_tag_a);

  // method to create transform
  //
  boolean createTransform(Vector<StatisticalModel>& stat_models_a);

  // method to create transform
  //
  boolean createTransforms(RTreeNode*& root_node_a,
			   Vector<StatisticalModel>& stat_models_a);

  // method to get the mean value for the gaussian model
  //
  boolean getMean(long sm_index_a, long gm_index_a, VectorFloat& mean_a);

  // method to get the GaussianModel
  //
  boolean getGaussianModel(long sm_index_a, long gm_index_a,
			   GaussianModel& gm_a);  

  // updateTransformID
  //
  boolean updateTransformID(Vector<StatisticalModel>& stat_models_a);

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  computational methods
  //
  //---------------------------------------------------------------------------
  
  // runDecisionTree method
  //
  boolean runDecisionTree();

    
  // method to set the parser
  //
  boolean setParser(SofParser* parser);
  
  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
  
private:
  
  // method to find best terminal
  //
  RTreeNode* findBestTerminal(RTreeNode*& node_a, RTreeNode*& best_a,
			      float& score_a);

  RTreeNode* findBestTerminal(RTreeNode*& node_a, RTreeNode*& best_a,
			      long& components_a);

  BiGraphVertex<RegressionDecisionTreeNode>* findTerminal(RTreeNode*& root_node_a, long index_a);
  
  // method to build regression cluster
  //
  boolean buildRegressionCluster(RTreeNode*& root_node);

  // method to perturb mean
  //
  boolean perturbMean(RegressionDecisionTreeNode*& child_node_a,
		      double perturbDepth);

  // method to cluster the children
  //
  boolean clusterChildren(RegressionDecisionTreeNode*& root_node_a,
			  RegressionDecisionTreeNode*& left_child_node_a,
			  RegressionDecisionTreeNode*& right_child_a);  
  
  // method to calculate the distance
  //
  boolean calculateDistance(RegressionDecisionTreeNode*& root_node_a,
			    RegressionDecisionTreeNode*& left_child_node_a,
			    RegressionDecisionTreeNode*& right_child_a);  
  
  // method to create the childre node
  //
  boolean createChildNode(RegressionDecisionTreeNode*&
			  root_node_a,
			  RegressionDecisionTreeNode*&
			  left_child_node_a,
			  RegressionDecisionTreeNode*&
			  right_child_a);    
  
};

// end of include file
// 
#endif


