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
use std::collections::HashMap;
use std::ffi::OsString;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use crate::lp_format::*;
use crate::solvers::{Solution, SolverProgram, SolverWithSolutionParsing, Status};
use crate::util::buf_contains;
#[derive(Debug, Clone)]
pub struct GurobiSolver {
name: String,
command_name: String,
temp_solution_file: Option<PathBuf>,
}
impl Default for GurobiSolver {
fn default() -> Self {
Self::new()
}
}
impl GurobiSolver {
pub fn new() -> GurobiSolver {
GurobiSolver {
name: "Gurobi".to_string(),
command_name: "gurobi_cl".to_string(),
temp_solution_file: None,
}
}
pub fn command_name(&self, command_name: String) -> GurobiSolver {
GurobiSolver {
name: self.name.clone(),
command_name,
temp_solution_file: self.temp_solution_file.clone(),
}
}
}
impl SolverWithSolutionParsing for GurobiSolver {
fn read_specific_solution<'a, P: LpProblem<'a>>(
&self,
f: &File,
_problem: Option<&'a P>,
) -> Result<Solution, String> {
let mut vars_value: HashMap<_, _> = HashMap::new();
let mut file = BufReader::new(f);
let mut buffer = String::new();
let _ = file.read_line(&mut buffer);
if buffer.split(' ').next().is_some() {
for line in file.lines() {
let l = line.unwrap();
if let Some('#') = l.chars().next() {
continue;
}
let result_line: Vec<_> = l.split_whitespace().collect();
if result_line.len() == 2 {
match result_line[1].parse::<f32>() {
Ok(n) => {
vars_value.insert(result_line[0].to_string(), n);
}
Err(e) => return Err(e.to_string()),
}
} else {
return Err("Incorrect solution format".to_string());
}
}
} else {
return Err("Incorrect solution format".to_string());
}
Ok(Solution::new(Status::Optimal, vars_value))
}
}
impl SolverProgram for GurobiSolver {
fn command_name(&self) -> &str {
&self.command_name
}
fn arguments(&self, lp_file: &Path, solution_file: &Path) -> Vec<OsString> {
let mut arg0: OsString = "ResultFile=".into();
arg0.push(solution_file.as_os_str());
vec![arg0, lp_file.into()]
}
fn preferred_temp_solution_file(&self) -> Option<&Path> {
self.temp_solution_file.as_deref()
}
fn parse_stdout_status(&self, stdout: &[u8]) -> Option<Status> {
if buf_contains(stdout, "Optimal solution found") {
Some(Status::Optimal)
} else if buf_contains(stdout, "infeasible") {
Some(Status::Infeasible)
} else {
None
}
}
}