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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use grb::prelude::*;
use std::fs::OpenOptions;
use std::io::{BufWriter, Write};
mod example_utils;
use example_utils::*;
fn main() -> grb::Result<()> {
let mut model = load_model_file_from_clarg();
model.set_param(param::Heuristics, 0.0)?;
let vars = model.get_vars()?.to_vec();
let mut callback = {
let mut lastiter = -INFINITY;
let mut lastnode = -INFINITY;
let file = OpenOptions::new()
.write(true)
.create(true)
.open("cb.log")
.unwrap();
let mut writer = BufWriter::new(file);
move |w: Where| {
use Where::*;
match w {
// Periodic polling callback
Polling(_) => {
// Ignore polling callback
}
// Currently performing presolve
PreSolve(ctx) => {
println!("@PreSolve");
let (coldel, rowdel) = (ctx.col_del()?, ctx.row_del()?);
if coldel > 0 || rowdel > 0 {
println!("**** {coldel} columns and {rowdel} rows removed so far. ****");
}
}
// Currently in simplex
Simplex(ctx) => {
let itrcnt = ctx.iter_cnt()?;
if itrcnt - lastiter >= 100.0 {
lastiter = itrcnt;
let ch = match ctx.is_perturbed()? {
0 => ' ',
1 => 'S',
_ => 'P',
};
println!(
"@Simplex: itrcnt={}, objval={}{}, priminf={}, dualinf={}.",
itrcnt,
ctx.obj_val()?,
ch,
ctx.prim_inf()?,
ctx.dual_inf()?
);
}
}
// Currently in MIP
MIP(ctx) => {
let (objbst, objbnd, solcnt, nodcnt) = (
ctx.obj_best()?,
ctx.obj_bnd()?,
ctx.sol_cnt()?,
ctx.node_cnt()?,
);
if nodcnt - lastnode >= 100.0 {
lastnode = nodcnt;
println!("@MIP: nodcnt={}, actnodes={}, itrcnt={}, objbst={}, objbnd={}, solcnt={}, cutcnt={}.",
nodcnt,
ctx.node_left()?,
ctx.iter_cnt()?,
objbst,
objbnd,
solcnt,
ctx.cut_cnt()?);
}
if (objbst - objbnd).abs() < 0.1 * (1.0 + objbst.abs()) {
println!("Stop early - 10% gap achived");
ctx.terminate();
}
if nodcnt >= 10000.0 && solcnt != 0 {
println!("Stop early - 10000 nodes explored");
ctx.terminate();
}
}
// Found a new MIP incumbent
MIPSol(ctx) => {
println!("@MIPSol: ");
let x = ctx.get_solution(&vars)?;
println!(
"**** New solution at node {}, obj {}, sol {}, x[0] = {} ****",
ctx.node_cnt()?,
ctx.obj()?,
ctx.sol_cnt()?,
x[0]
);
}
// Currently exploring a MIP node
MIPNode(ctx) => {
println!("@MIPNode");
println!("**** NEW NODE! ****");
let x = ctx.get_solution(&vars)?;
if ctx.status()? == Status::Optimal {
println!(" relaxation solution = {x:?}");
let obj = ctx.set_solution(vars.iter().zip(x))?;
// Should not return None - we didn't modify the solution
assert!(obj.is_some());
}
}
// Currently in barrier
Barrier(ctx) => {
println!("@Barrier: itrcnt={}, primobj={}, dualobj={}, priminf={}, dualinf={}, compl={}.",
ctx.iter_cnt()?,
ctx.prim_obj()?,
ctx.dual_obj()?,
ctx.prim_inf()?,
ctx.dual_inf()?,
ctx.compl_viol()?);
}
// Printing a log message
Message(ctx) => {
writer.write_all(ctx.message()?.as_bytes())?;
writer.write_all(b"\n")?;
}
_ => {}
}
Ok(())
}
};
model.optimize_with_callback(&mut callback)?;
println!("\nOptimization complete");
if model.get_attr(attr::SolCount)? == 0 {
println!(
"No solution found. optimization status = {:?}",
model.status()
);
} else {
println!(
"Solution found. objective = {}",
model.get_attr(attr::ObjVal)?
);
for v in model.get_vars().unwrap() {
let vname = model.get_obj_attr(attr::VarName, v)?;
let value = model.get_obj_attr(attr::X, v)?;
if value > 1e-10 {
println!(" {vname}: {value}");
}
}
}
Ok(())
}