#include <iostream>
#include <fstream>

#include <armadillo>
#include "matReader.h"
#include "Boost.h"
#include "matWriter.h"

using namespace arma;
using namespace std;

fstream logfile;
fstream resultfile;
my_param para;
//matWriter	writer("result/data.mat");
int main(int args, char*argv[])
{
  if(args>1)
    para.read(argv[1]);
  else 
    para.read("config.xml");
  logfile.open(para.log.c_str(),std::fstream::out);
  if(logfile==false){
	cerr<<"fail to open logfile!\n";exit(-1);
  }
  resultfile.open(para.result.c_str(),std::fstream::out);
  if(resultfile==false){
	cerr<<"fail to open resultfile!\n";exit(-1);
  }
  matReader	reader(para.data);
  Boost		ensemble("softmax");
  mat		train_data,test_data;
  mat		train_label,test_label;
  mat		train_pl_labels;
  size_t	T = para.T;

  reader.read(train_data, "train_data"); 
  reader.read(train_pl_labels, "train_p_target");inplace_trans(train_pl_labels);
  reader.read(test_data,"test_data");
  reader.read(test_label,"test_target");inplace_trans(test_label);

  //train
  ensemble.boosting(train_data, train_pl_labels,train_label,T);
  //predict
  pair<uvec,double> result = ensemble.predict(test_data, test_label);
  printf("####################################################"
	 "\n correct rate:%f\n",result.second);
  resultfile<<"correct rate:"<<result.second<<"\n"
    <<"####################################################"
    <<"\npredict result for each instances:\n"
    <<result.first;
  return 0;
}
