-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample1.cpp
More file actions
89 lines (56 loc) · 1.59 KB
/
example1.cpp
File metadata and controls
89 lines (56 loc) · 1.59 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
#include <ilcplex/ilocplex.h>
ILOSTLBEGIN
static void
populatebyrow (IloModel model, IloNumVarArray var, IloRangeArray con);
int main (void){
// Setting up the CPLEX environment
IloEnv env;
// CPLEX solution block
try {
// Declaring the objects
IloModel model(env);
IloNumVarArray var (env);
IloRangeArray con(env);
populatebyrow(model, var, con);
// Solving using CPLEX
IloCplex cplex(model);
cplex.solve();
// Output solutions
env.out() << "Solution status = " << cplex.getStatus() << endl;
env.out() << "Solution value = " << cplex.getObjValue() << endl;
IloNumArray vals(env);
cplex.getValues(vals, var);
env.out() << "Values = " << vals << endl;
IloNumArray slacks(env);
cplex.getSlacks(slacks, con);
env.out() << "Slacks = " << slacks << endl;
// Write model to file
cplex.exportModel("ipex1.lp");
}
// Error handling
catch (IloException e){
cerr << "Concert exception caught: " << e << endl;
}
catch (...){
cerr << "Unknown exception caught." << endl;
}
// End environment
env.out() << "Press return to exit..." << endl;
std::getchar();
env.end();
return 0;
}
static void
populatebyrow (IloModel model, IloNumVarArray x, IloRangeArray c){
// retrieve model environment
IloEnv env = model.getEnv();
// Define variables
x.add(IloNumVar(env, 0.0, IloInfinity, ILOINT));
x.add(IloNumVar(env, 0.0, IloInfinity, ILOINT));
// Add objective
model.add(IloMaximize(env, 1.00 * x[0] + 0.64 * x[1] ));
// Add constraints
c.add( 50 * x[0] + 31 * x[1] <= 250);
c.add( 3 * x[0] - 2 * x[1] >= -4);
model.add(c);
}