-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrderSeperationCutGenerator.cpp
102 lines (92 loc) · 2.92 KB
/
OrderSeperationCutGenerator.cpp
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
#include "Global.h"
#include "OrderSeperationCutGenerator.h"
#include "OptModel.h"
#include "DataModel.h"
OrderSeperationCutGenerator::OrderSeperationCutGenerator(OptModel* _model)
{
model = _model;
}
OrderSeperationCutGenerator::~OrderSeperationCutGenerator(void)
{
}
bool OrderSeperationCutGenerator::seperateCurrentSolution()
{
/* Initilize model and create variables */
IloEnv env = IloEnv();
IloModel SeperationModel(env, "Seperation");
IloCplex SeperationSolver(SeperationModel);
int dimension = DataModel::n;
IloNumVarArray alpha(env, dimension, 0, IloInfinity, ILOFLOAT);
for (int i = 0; i < alpha.getSize(); i++)
{
string name = "a" + ToString(i);
alpha[i].setName(name.c_str());
}
IloNumVar beta(env, 0, IloInfinity);
beta.setName("b");
// set Speration objective
double* x = new double[DataModel::n];
CPXgetx(model->cpxenv, model->cpxlp, x, 0, DataModel::n -1);
IloExpr expr(env);
for (int i = 0; i < dimension; i++)
{
expr += x[i]*alpha[i];
}
SeperationModel.add(IloMinimize(env, expr - beta));
delete []x;
IloNumVarArray betaArray(env, DataModel::m, 0, IloInfinity, ILOFLOAT);
IloNumVarArray z(env, DataModel::m,0, 1, IloNumVar::Bool);
for (int i = 0; i < DataModel::m; i++)
{
string strBetai= "b" + ToString(i);
betaArray[i].setName(strBetai.c_str());
string strZi = "z" + ToString(i);
z[i].setName(strZi.c_str());
}
/************************** build model ******************************/
DataModel*dm = model->dm_p;
for (int i = 0 ; i < DataModel::m ; i++)
{
double b_i = dm->rhs[i];
for (int j = 0; j < DataModel::n; j++)
{
double a_ij = dm->Arows[i][j].second;
SeperationModel.add(betaArray[i]<=(b_i/a_ij)*alpha[j]);
}
}
int BigM = 1/SMALL_DECIMAL;
for (int i = 0; i < DataModel::m; i ++)
{
SeperationModel.add(beta <= betaArray[i] + BigM*z[i]);
}
SeperationModel.add(IloSum(z)==DataModel::m - DataModel::k -1);
SeperationModel.add(IloSum(alpha) == 1);
SeperationSolver.exportModel("seperation.lp");
SeperationSolver.solve();
if(!SeperationSolver.getCplexStatus() == IloCplex::Optimal)
cout << "Seperation is not solved"<<endl;
if (SeperationSolver.getObjValue() >= - SMALL_DECIMAL)
{
return false;
}
IloNumArray val_alpha(env);
IloNum val_beta;
IloNumArray val_betaArray(env);
IloNumArray val_z(env);
SeperationSolver.getValues(val_z, z);
SeperationSolver.getValues(val_betaArray, betaArray);
SeperationSolver.getValues(val_alpha, alpha);
val_beta = SeperationSolver.getValue(beta);
cout << "beta: " << val_beta<<endl;
cout << "beta_i" << val_betaArray<<endl;
cout << "z: " << val_z <<endl;
cout << "alpha" << val_alpha<<endl;
CUT_COEFICIENTS coefs; CUT_RHS rhs = val_beta;
for (int i = 0; i < val_alpha.getSize(); i++)
{
coefs.push_back(make_pair(i, val_alpha[i]));
}
CUT cut(coefs, rhs);
// model->addAConstToCPXModel(cut, string("test"), 0);
return true;
}