///////////////////////////////////////////////////////////////////////////
// ASVR: Active Support Vector Regression                                //
// Copyright 2002 David R. Musicant and Alexander Feinberg.              //
// This software is free for academic use only.                          //
// For commercial use, contact dmusican@carleton.edu.                    //
///////////////////////////////////////////////////////////////////////////
// AsvrPredict.cpp: This file contains code relating to the standalone   //
// prediction application.                                               //
///////////////////////////////////////////////////////////////////////////

#include "DataSource.H"
#include "Utilities.H"
#include "StandAlone.H"
#include "Version.H"

// Functions corresponding to executable for testing ASVR output
char gTESTFILENAME[1000], gOUTPUTFILENAME[1000], gDATATYPE[10];
bool gHEADER;
bool gPREDS;

// Parse the inputs off the command line.
void ParseInputs(int argc, char *argv[]) {
  int i;
  
  // Set default values 
  gDATATYPE[0]='d';
  strcpy(gTESTFILENAME,"__none__");
  gPREDS = TRUE;
  
  // Parse the inputs
  for (i=1; i < argc && (argv[i][0]=='-'); i++) {
    
    if (!strcmp(argv[i],"-precision")) {
      i++;
      strcpy(gDATATYPE,argv[i]);
    } else if (!strcmp(argv[i],"-header"))
      gHEADER = TRUE;
    else if (!strcmp(argv[i],"-nopreds"))
      gPREDS = FALSE;
    else
      ThrowIt("Invalid option.");
    
  } // end-for
  
  if (i != argc-2) {
    PRINT("Active Support Vector Regression Tester.\n");
    PRINT("Copyright (C) 2002 David R. Musicant and Alexander Feinberg.\n");
    PRINT(VERSION);
    PRINT("\n\n");
    PRINT("Syntax: asvr_test test_file solution_file\n");
    PRINT("Possible choices are:\n");
    PRINT("-nopreds         Predicted values are not present in test file\n");
    exit(2);
  }
  
  strcpy(gTESTFILENAME,argv[i]);
  strcpy(gOUTPUTFILENAME,argv[i+1]);
}

void getData(ifstream &fileIn, DenseMatrixType<double>* &A)
{

  int rows, cols, dataCols;
  char comma;
	
  // Pull rows and columns from header if present, otherwise determine
  // dynamically
  if (gHEADER) {
    fileIn >> rows >> comma >>  dataCols;
  } else {

    ifstream inFile(gTESTFILENAME,ios::in);
    char inBuf[MAXROWLENGTH];
    inFile.getline(inBuf,MAXROWLENGTH);
    // Determine number of cols by taking number of commas and adding 1
    cols = 1;
    for (unsigned int i=0; i < strlen(inBuf); i++)
      if (inBuf[i]==',') cols++;
    if (gPREDS)
      dataCols = cols - 1;
    else
      dataCols = cols;
    rows = 1; // have read one row
    // Scan rows and count how many there are
    while (inFile.getline(inBuf,MAXROWLENGTH))
      rows++;
    inFile.close();

  }
	
  A = new DenseMatrixType<double>(rows, dataCols);
  double **data = A->dataMatrix, *dataRow, dummy;
	
  for (int i = 0; i < rows; i++)
    {
      dataRow = data[i];
      for (int j = 0; j < dataCols-1; j++)
	{	fileIn >> dataRow[j] >> comma;	}
      fileIn >> dataRow[dataCols-1];
      if (gPREDS)
	fileIn >> comma >> dummy;
		
    }

}

void loadData(DenseMatrixType<double>* &A)
{
  ifstream fileIn;
  fileIn.open(gTESTFILENAME);
  getData(fileIn, A);
  fileIn.close();

}

int main(int argc, char *argv[])
{
  try {
    ParseInputs(argc,argv);
    
    FILE *out = fopen(gOUTPUTFILENAME,"r");
    if (!out)
      ThrowIt("Unable to read from training output file.");
    int n;
    double gamma;
    fscanf(out,"%d\n%lf\n",&n,&gamma);
    DenseVector *w = new DenseVector(n);
    double *mean = new double[n];
    double *std = new double[n];
    int i;
    for (i=0; i < n; i++)
      fscanf(out,"%lf\n",&(w->dataMatrix[i]));
    for (i=0; i < n; i++)
      fscanf(out,"%lf\n",&(mean[i]));
    for (i=0; i < n; i++)
      fscanf(out,"%lf\n",&(std[i]));

    DenseMatrixType<double> *A;
    
    loadData(A);
    A->Normalize(mean,std);
    
    DenseVector *results = new DenseVector(A->rows);
    A->Predict(w,gamma,results);
    results->Display();

    delete w;
    delete A;
    delete results;
    delete [] mean;
    delete [] std;

  }
  catch(const char* text) {
    PRINT("%s\n",text);
  }

  return 0;
}





