forked from litany-of-boredom/svm_wrappers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcplex.hpp
More file actions
95 lines (82 loc) · 3.35 KB
/
cplex.hpp
File metadata and controls
95 lines (82 loc) · 3.35 KB
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
#ifndef SVMWRAPPERS_cplex_hpp
#define SVMWRAPPERS_cplex_hpp
#include "problem.hpp"
#include <iostream>
#include <ilcplex/ilocplex.h>
namespace SVMWrappers
{
namespace CPLEX
{
using namespace std;
template<typename T, unsigned int DIM>
Eigen::Hyperplane<T, Eigen::Dynamic> solve(Problem<T, DIM> p)
{
using Matrix = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
using Vector = Eigen::Matrix<T, Eigen::Dynamic, 1>;
using RowVector = Eigen::Matrix<T, 1, Eigen::Dynamic>;
using Index = Eigen::Index;
using Hyperplane = Eigen::Hyperplane<T, Eigen::Dynamic>;
IloEnv env;
try
{
int numPoints = p.length();
IloModel model(env);
IloNumVar w[3];
for(int i = 0; i < DIM; i++)
{
w[i] = IloNumVar(env, -IloInfinity, IloInfinity, ILOFLOAT);
}
for(int i = DIM; i < 3; i++)
{
w[i] = IloNumVar(env, 0, 0, ILOFLOAT);
}
IloNumVar b(env, -IloInfinity, IloInfinity, ILOFLOAT);
model.add(IloMinimize(env, w[0]*w[0] + w[1]*w[1] + w[2]*w[2]/* + w[3]*w[3] + w[4]*w[4] + w[5]*w[5] + w[6]*w[6] + w[7]*w[7] + w[8]*w[8] + w[9]*w[9]*/));
for(int i = 0; i < numPoints; i++)
{
model.add(p.get_class(i)*(
w[0]*p.get_feature(i,0) +
w[1]*p.get_feature(i,1) +
w[2]*p.get_feature(i,2)/* +
w[3]*p.get_feature(i,3) +
w[4]*p.get_feature(i,4) +
w[5]*p.get_feature(i,5) +
w[6]*p.get_feature(i,6) +
w[7]*p.get_feature(i,7) +
w[8]*p.get_feature(i,8) +
w[9]*p.get_feature(i,9)*/ - b) >= 1);
}
// Optimize
IloCplex cplex(model);
cplex.setOut(env.getNullStream());
cplex.setWarning(env.getNullStream());
cplex.solve();
if (cplex.getStatus() == IloAlgorithm::Infeasible)
env.out() << "No Solution" << endl;
// env.out() << "Solution status: " << cplex.getStatus() << endl;
// env.out() << "Optimal value: " << cplex.getObjValue() << endl;
// env.out() << "w1: " << cplex.getValue(w[0]) << endl;
// env.out() << "w2: " << cplex.getValue(w[1]) << endl;
// env.out() << "w3: " << cplex.getValue(w[2]) << endl;
// env.out() << "b: " << cplex.getValue(b) << endl;
RowVector w_vector(1,DIM);
for(int i = 0; i < DIM; i++)
{
w_vector(0,i) = cplex.getValue(w[i]);
}
auto b_store = cplex.getValue(b);
env.end();
return Hyperplane(w_vector, b_store);
}
catch (IloException& ex) {
cerr << "Error: " << ex << endl;
}
catch (...) {
cerr << "Error" << endl;
}
Hyperplane hp;
return hp;
}
}
}
#endif