-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.cpp
More file actions
102 lines (90 loc) · 2.88 KB
/
Copy pathModel.cpp
File metadata and controls
102 lines (90 loc) · 2.88 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
96
97
98
99
100
101
102
#include "Model.h"
#include <stdio.h>
#include <string>
using namespace std;
void Model::allocVar()
{
int N = my_input_->N_VERTICES;
int E = my_input_->N_ARCS;
cout <<"The model has "<< N << " vertices and "<< E << " arcs "<< endl;
H_ = IloNumVarArray(env_, E + 1);
h_ = IloNumVarArray(env_, E + 1);
int k = 0;
for (int i = 0; i < E ; ++i)
{
string name;
name = "H_" + to_string((my_input_->arcs[i]).tail) + "_" + to_string((my_input_->arcs[i]).head);
H_[k] = IloNumVar(env_, 0, IloInfinity, IloNumVar::Int, name.c_str());
name = "h_" + to_string((my_input_->arcs[i]).tail) + "_" + to_string((my_input_->arcs[i]).head);
h_[k] = IloNumVar(env_, 0, IloInfinity, IloNumVar::Int, name.c_str());
++k;
}
}
void Model::buildModel(int CAP, int TMAX)
{
// First kind of constraints: conservation of flow H
for(int i = 0; i < my_input_->N_VERTICES; ++i)
{
int counter = 0;
IloExpr expr1(env_);
for(int j = 0; j < my_input_->in_arcs[i].size(); ++j)
{
expr1 -= H_[ (my_input_->arcs[my_input_->in_arcs[i][j]]).index];
counter++;
}
for(int j = 0; j < my_input_->out_arcs[i].size();++j)
{
expr1 += H_[ (my_input_->arcs[my_input_->out_arcs[i][j]]).index];
counter++;
}
if (counter > 0)
{
mod_.add( expr1 == 0);
}
expr1.end();
}
// Second kind of constraints: semiflow h -> sum of flows at vertex i is equal to v(i)
for(int i = 0; i < my_input_->N_VERTICES; ++i)
{
IloExpr expr2(env_);
for(int j = 0; j < my_input_->in_arcs[i].size();++j)
{
expr2 -= h_[ (my_input_->arcs[my_input_->in_arcs[i][j]]).index];
}
for(int j = 0; j < my_input_->out_arcs[i].size();++j)
{
expr2 += h_[ (my_input_->arcs[my_input_->out_arcs[i][j]]).index];
}
mod_.add( expr2 == my_input_->vertices_status[i]);
expr2.end();
}
// Third kind of constraints: h <= CAP * H
for(int j = 0; j < my_input_->N_ARCS; ++j)
{
IloExpr expr3(env_);
expr3 -= h_[ my_input_->arcs[j].index];
expr3 += CAP * H_[my_input_->arcs[j].index];
mod_.add( expr3 >= 0);
}
// Objective function
IloExpr expr3(env_);
for(int i = 0; i < my_input_->N_ARCS; ++i)
{
expr3 += (my_input_->arcs[i]).cce_cost * H_[(my_input_->arcs[i]).index];
expr3 += (my_input_->arcs[i]).cve_cost * h_[(my_input_->arcs[i]).index];
}
mod_.add(IloMinimize(env_, expr3));
expr3.end();
}
/* Export the model as an .lp file*/
void Model::writeModel()
{
cplex_.exportModel("model.lp");
}
/* Creates and solve the model */
void Model::createSolve(int CAP, int TMAX)
{
allocVar();
buildModel(CAP, TMAX);
writeModel();
}