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

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

// isip include files
//
#ifndef ISIP_REGRESSION_DECISION_TREE_NODE
#include <RegressionDecisionTreeNode.h>
#endif

#ifndef ISIP_REGRESSION_DECISION_TREE
#include <RegressionDecisionTree.h>
#endif

#ifndef ISIP_STATISTICAL_MODEL_ADAPTATION
#include <StatisticalModelAdaptation.h>
#endif

// forward class definitions:
//
//  we must define the RegressionDecisionTreeNode,
//  RegressDecisionTreeNode, Transform, and Adapt class here first
//  because the header files might be short-circuited by the ifndef.
//
class RegressionDecisonTreeNode;
class RegressionDecisonTree;
class StatisticalModelAdaptation;

// MaximumLikelihoodLinearRegression: a class that controls the
// process of MLLR adaptation
//
class MaximumLikelihoodLinearRegression {

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

  // 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_RDT;
  static const String PARAM_SM_ADAPT;    
  static const String PARAM_DBGL;  
  //----------------------------------------
  //
  // default values and arguments
  //
  //----------------------------------------  

  // define default values for the thresholds
  //
  static const int DEF_NUM_CLASSES = 1;
  static const float DEF_MERGE_THRESHOLD = 5;
  static const float DEF_NUM_OCC_THRESHOLD = 100;
  
  //----------------------------------------
  //
  // error codes
  //
  //----------------------------------------  
  
  static const long ERR = 00100700;
  static const long ERR_UNALG_UNIMP = 00100701;

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

  // 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;

  // regression decision tree
  //
  RegressionDecisionTree rdt_d;

  // statistical model adaptation
  //
  StatisticalModelAdaptation sm_adapt_d;  
  
  // local copy of the statistical model
  //
  Vector<StatisticalModel> stat_models_d;

  // debug level
  //
  DebugLevel debug_level_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
  //
  ~MaximumLikelihoodLinearRegression() {
  }

  // method: default constructor
  //
  MaximumLikelihoodLinearRegression(ALGORITHM algorithm = DEF_ALGORITHM,
				    IMPLEMENTATION implementation = DEF_IMPLEMENTATION) {
    algorithm_d = algorithm;
    implementation_d = implementation;
  }

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

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

  // method: operator=
  //
  MaximumLikelihoodLinearRegression& operator= (const MaximumLikelihoodLinearRegression& 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 MaximumLikelihoodLinearRegression& 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 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 adapt the model
  //
  boolean adapt(Vector<StatisticalModel>& stat_models_a);  

  //---------------------------------------------------------------------------
  //
  // class-specific public methods:
  //  computational methods
  //
  //---------------------------------------------------------------------------
    
  // method to set the parser
  //
  boolean setParser(SofParser* parser);
  
  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
  
private:
  
};

// end of include file
// 
#endif
