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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
extern crate gurobi_sys as ffi;

use std::ptr::{null, null_mut};
use std::ffi::CString;
use error::{Error, Result};
use model::Model;
use util;
use types;


pub mod param {
  pub use ffi::{IntParam, DoubleParam, StringParam};

  // re-exports
  pub use self::IntParam::*;
  pub use self::DoubleParam::*;
  pub use self::StringParam::*;
}


/// Gurobi environment object
pub struct Env {
  env: *mut ffi::GRBenv
}

impl Env {
  /// create an environment with log file
  pub fn new(logfilename: &str) -> Result<Env> {
    let mut env = null_mut::<ffi::GRBenv>();
    let logfilename = try!(util::make_c_str(logfilename));
    let error = unsafe { ffi::GRBloadenv(&mut env, logfilename.as_ptr()) };
    if error != 0 {
      return Err(Error::FromAPI(util::get_error_msg_env(env), error));
    }
    Ok(Env { env: env })
  }

  /// create an empty model object associted with the environment.
  pub fn new_model(&self, modelname: &str) -> Result<Model> {
    let modelname = try!(util::make_c_str(modelname));
    let mut model = null_mut::<ffi::GRBmodel>();
    let error = unsafe {
      ffi::GRBnewmodel(self.env,
                       &mut model,
                       modelname.as_ptr(),
                       0,
                       null(),
                       null(),
                       null(),
                       null(),
                       null())
    };
    if error != 0 {
      return Err(self.error_from_api(error));
    }

    Ok(Model::new(self, model))
  }

  /// Query the value of a parameter.
  pub fn get<P: Param>(&self, param: P) -> Result<P::Out> { Param::get(self, param) }

  /// Set the value of a parameter.
  pub fn set<P: Param>(&mut self, param: P, value: P::Out) -> Result<()> { Param::set(self, param, value) }

  pub fn error_from_api(&self, error: ffi::c_int) -> Error { Error::FromAPI(util::get_error_msg_env(self.env), error) }
}

impl Drop for Env {
  fn drop(&mut self) {
    unsafe { ffi::GRBfreeenv(self.env) };
    self.env = null_mut();
  }
}


/// provides function to query/set the value of parameters.
pub trait Param: Sized + Into<CString> {
  type Out;
  type Buf: types::Init + types::Into<Self::Out> + types::AsRawPtr<Self::RawFrom>;
  type RawFrom;
  type RawTo: types::FromRaw<Self::Out>;

  #[allow(unused_imports)]
  fn get(env: &Env, param: Self) -> Result<Self::Out> {
    use types::{AsRawPtr, Into};

    let mut value: Self::Buf = types::Init::init();
    let error = unsafe { Self::get_param(env.env, param.into().as_ptr(), value.as_rawptr()) };
    if error != 0 {
      return Err(env.error_from_api(error));
    }

    Ok(types::Into::into(value))
  }

  fn set(env: &mut Env, param: Self, value: Self::Out) -> Result<()> {
    let error = unsafe { Self::set_param(env.env, param.into().as_ptr(), types::FromRaw::from(value)) };
    if error != 0 {
      return Err(env.error_from_api(error));
    }

    Ok(())
  }

  #[inline(always)]
  unsafe fn get_param(env: *mut ffi::GRBenv, paramname: ffi::c_str, value: Self::RawFrom) -> ffi::c_int;

  #[inline(always)]
  unsafe fn set_param(env: *mut ffi::GRBenv, paramname: ffi::c_str, value: Self::RawTo) -> ffi::c_int;
}


impl Param for param::IntParam {
  type Out = i32;
  type Buf = ffi::c_int;
  type RawFrom = *mut ffi::c_int;
  type RawTo = ffi::c_int;

  unsafe fn get_param(env: *mut ffi::GRBenv, paramname: ffi::c_str, value: *mut ffi::c_int) -> ffi::c_int {
    ffi::GRBgetintparam(env, paramname, value)
  }

  unsafe fn set_param(env: *mut ffi::GRBenv, paramname: ffi::c_str, value: ffi::c_int) -> ffi::c_int {
    ffi::GRBsetintparam(env, paramname, value)
  }
}

impl Param for param::DoubleParam {
  type Out = f64;
  type Buf = ffi::c_double;
  type RawFrom = *mut ffi::c_double;
  type RawTo = ffi::c_double;

  unsafe fn get_param(env: *mut ffi::GRBenv, paramname: ffi::c_str, value: *mut ffi::c_double) -> ffi::c_int {
    ffi::GRBgetdblparam(env, paramname, value)
  }

  unsafe fn set_param(env: *mut ffi::GRBenv, paramname: ffi::c_str, value: ffi::c_double) -> ffi::c_int {
    ffi::GRBsetdblparam(env, paramname, value)
  }
}


impl Param for param::StringParam {
  type Out = String;
  type Buf = Vec<ffi::c_char>;
  type RawFrom = *mut ffi::c_char;
  type RawTo = *const ffi::c_char;

  unsafe fn get_param(env: *mut ffi::GRBenv, paramname: ffi::c_str, value: *mut ffi::c_char) -> ffi::c_int {
    ffi::GRBgetstrparam(env, paramname, value)
  }

  unsafe fn set_param(env: *mut ffi::GRBenv, paramname: ffi::c_str, value: *const ffi::c_char) -> ffi::c_int {
    ffi::GRBsetstrparam(env, paramname, value)
  }
}


// #[test]
// fn env_with_logfile() {
//   use std::path::Path;
//   use std::fs::remove_file;
//
//   let path = Path::new("test_env.log");
//
//   if path.exists() {
//     remove_file(path).unwrap();
//   }
//
//   {
//     let env = Env::new(path.to_str().unwrap()).unwrap();
//   }
//
//   assert!(path.exists());
//   remove_file(path).unwrap();
// }

#[cfg(test)]
mod test {
  use env::param;
  use env::Env;

  #[test]
  fn param_accesors_should_be_valid() {
    let mut env = Env::new("").unwrap();
    env.set(param::IISMethod, 1).unwrap();
    let iis_method = env.get(param::IISMethod).unwrap();
    assert_eq!(iis_method, 1);
  }
}