// file: $isip_ifc/class/pr/RelevanceVectorMachine/rvm_04.cc
// version: $Id: rvm_04.cc 10424 2006-02-10 20:37:45Z raghavan $
//

// isip include files
//
#include "RelevanceVectorMachine.h"

// method: sofSize
//
// arguments: none
//
// return: size of object
//
// this method returns the size of the object in the Sof file and is
// used for binary write
//
long RelevanceVectorMachine::sofSize() const {

  // return the space required for this object
  //
  long bytes = polynomial_degree_d.sofSize() + rbf_gamma_d.sofSize() +
    sigmoid_kappa_d.sofSize() + sigmoid_delta_d.sofSize() +
    weights_d.sofSize() + targets_d.sofSize() +
    vectors_d.sofSize() + bias_d.sofSize() + kernel_d.sofSize() +
    inv_hessian_d.sofSize();

  // return the size
  //
  return bytes;
}

// method: write
//
// arguments:
//  Sof& sof: (input) sof file object
//  long tag: (input) sof object instance tag
//  const String& name: (input) sof object instance name
//
// return: a boolean value indicating status
//
// this method has the object write itself to an Sof file
//
boolean RelevanceVectorMachine::write(Sof& sof_a, long tag_a,
				      const String& name_a) const {

  // declare local variable
  //
  long obj_size;
  
  // write the instance of the object into the Sof file
  //
  if (sof_a.isText()) {

    // set the size to by dynamic
    //
    obj_size = Sof::ANY_SIZE;
  }
  else {

    // set the size to be the size of the object written to the Sof file
    //
    obj_size = sofSize();
  }

  // write the object into the sof file's index
  //
  if (!sof_a.put(name_a, tag_a, obj_size)) {
    return false;
  }
  
  // write data and exit gracefully
  //
  return writeData(sof_a);
}

// method: writeData
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//
// return: a boolean value indicating status
//
// this method has the object write itself to an Sof file. it assumes
// that the Sof file is already positioned correctly
//
boolean RelevanceVectorMachine::writeData(Sof& sof_a,
					  const String& pname_a) const {

  // write a start string if necessary
  //
  sof_a.writeLabelPrefix(pname_a);

  // write the algorithm
  //
  ALGO_MAP.writeElementData(sof_a, PARAM_ALGORITHM, (long)algorithm_d);
  
  // write the implementation
  //
  IMPL_MAP.writeElementData(sof_a, PARAM_IMPLEMENTATION, (long)implementation_d);
  // write the bias
  //
  bias_d.writeData(sof_a, PARAM_BIAS);
  
  // write the weights
  //
  weights_d.writeData(sof_a, PARAM_WEIGHTS);
  
  // write the hyperparameters
  //
  inv_hessian_d.writeData(sof_a, PARAM_INV_HESSIAN);
  
  // write the targets
  //
  targets_d.writeData(sof_a, PARAM_TARGETS);
  
  // write the relevance vectors
  //
  vectors_d.writeData(sof_a, PARAM_VECTORS);

  // write the polynomial degree
  //
  polynomial_degree_d.writeData(sof_a, PARAM_POLYNOMIAL_DEGREE);

  // write the rbf gamma
  //  
  rbf_gamma_d.writeData(sof_a, PARAM_RBF_GAMMA);

  // write the sigmoid kappa
  //  
  sigmoid_kappa_d.writeData(sof_a, PARAM_SIGMOID_KAPPA);

  // write the sigmoid delta
  //    
  sigmoid_delta_d.writeData(sof_a, PARAM_SIGMOID_DELTA);
  
  // write the kernel
  //
  kernel_d.writeData(sof_a, PARAM_KERNEL);
  
  // put an end string if necessary
  //
  sof_a.writeLabelSuffix(pname_a);
  
  // exit gracefully
  //
  return true;
}
