#include"ranksvm.h"
#include"ranksvmtn.h"
#include"../tools/matrixIO.h"
#include<iostream>
#include<fstream>
#include<string>

using namespace Eigen;
using namespace std;

int RSVM::saveModel(const string fname){

    std::ofstream fout(fname.c_str());
    fout<<this->getName()<<endl;
    fout<<this->fsize<<endl;
    Eigen::write_stream(fout, this->model.weight);
    fout<<this->model.beta<<endl;
    return 0;
}

RSVM* RSVM::loadModel(const string fname){
    std::ifstream fin(fname.c_str());
    std::string type;
    int fsize;
    fin>>type;
    fin>>fsize;

    RSVM* rsvm;

    if (type=="TN")
        rsvm = new RSVMTN();

    rsvm->fsize=fsize;
    SVMModel model;
    Eigen::read_stream(fin, model.weight);
    fin>>model.beta;
    rsvm->setModel(model);

    return rsvm;
}

int RSVM::setModel(const SVMModel &model) {
    if (model.weight.cols()!=fsize)
        LOG(FATAL) << "Feature size mismatch: "<<fsize<<" "<<model.weight.cols();
    this->model.weight=model.weight;
    this->model.beta=model.beta;
    return 0;
}