summaryrefslogtreecommitdiff
path: root/main.cpp
blob: e89cfe1d2543aacd586ba18c07002f9e7a2e5d3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <Eigen/Dense>
#include <boost/program_options.hpp>
#include <list>
#include "tools/easylogging++.h"
#include "model/ranksvmtn.h"
#include "tools/fileDataProvider.h"
#include "tools/matrixIO.h"
#include <fstream>

INITIALIZE_EASYLOGGINGPP

using namespace Eigen;
namespace po = boost::program_options;

po::variables_map vm;

int train() {
    RSVM *rsvm;
    rsvm = RSVM::loadModel(vm["model"].as<std::string>());
    FileDP dp(vm["feature"].as<std::string>());

    // Generic training operations
    dp.open();
    DataList D;

    LOG(INFO)<<"Training started";
    dp.getDataSet(D);
    LOG(INFO)<<"Read "<<D.getSize()<<" entries with "<< D.getfSize()<<" features";
    rsvm->train(D);

    LOG(INFO)<<"Training finished,saving model";

    dp.close();
    rsvm->saveModel(vm["output"].as<std::string>().c_str());
    delete rsvm;
    return 0;
}

int predict() {
    RSVM *rsvm;
    rsvm = RSVM::loadModel(vm["model"].as<std::string>().c_str());
    FileDP dp(vm["feature"].as<std::string>().c_str());

    dp.open();
    DataList D;
    std::vector<double> L;
    LOG(INFO)<<"Prediction started";

    while (!dp.EOFile())
    {
        dp.getDataSet(D);
        LOG(INFO)<<"Read "<<D.getSize()<<" entries with "<< D.getfSize()<<" features";
        rsvm->predict(D,L);
    }

    LOG(INFO)<<"Training finished,saving prediction";
    std::ofstream fout(vm["output"].as<std::string>().c_str());

    for (int i=0; i<L.size();++i)
        fout<<L[i]<<std::endl;
    fout.close();

    dp.close();
    delete rsvm;
    return 0;
}

int validate()
{
    LOG(FATAL)<<"Not Implemented";
    return 0;
}

int main(int argc, char **argv) {
    // Defining program options
    po::options_description desc("Allowed options");
    desc.add_options()
            ("help,h", "produce help message")
            ("train,T", "training model")
            ("validate,V", "validate model")
            ("predict,P", "use model for prediction")
            ("model,m", po::value<std::string>(), "set input model file")
            ("output,o", po::value<std::string>(), "set output model/prediction file")
            ("feature,i", po::value<std::string>(), "set input feature file");

    // Parsing program options
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);

    /* Conjugate Gradient method test
    MatrixXd A(3,3);
    VectorXd b(3),x(3);
    A<< 1,2,3,2,2,4,3,4,1;
    b<< 1,1,1;
    x<< 0,0,0;
    cg_solve(A,b,x);
    write_stream(std::cout,x);*/

    // Print help if necessary
    if (vm.count("help") || !(vm.count("train") || vm.count("validate") || vm.count("predict"))) {
        std::cout << desc;
        return 0;
    }

    if (vm.count("train")) {
        LOG(INFO) << "Program option: training";
        train();
    }
    else if (vm.count("validate")) {
        LOG(INFO) << "Program option: validate";
        validate();
    }
    else if (vm.count("predict")) {
        LOG(INFO) << "Program option: predict";
        predict();
    }
    return 0;
}