-
-
Save ubnt-intrepid/beb69d252993e97b31e0a7cc8f5cddd6 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn main() { | |
let env = Env::new("highlevel.log").unwrap(); | |
let model = define_model! { | |
env, | |
Find: { | |
"x" @ x => Binary, | |
"y" @ y => Continuous(1.0, 2.0), | |
"z" @ z => Integer(100, 200) | |
}, | |
Minimize: { x + y + z^2 }, | |
SubjectTo: { | |
"c0": a <= x <= b; | |
"c1": x + y + z <= 2; | |
}, | |
}; | |
model.optimize().unwrap(); | |
model.write("model.mps").unwrap(); | |
model.write("model.sol").unwrap(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
use std::iter::FromIterator; | |
#[derive(Copy,Clone,PartialEq, Debug)] | |
enum VarRep { | |
Attached(i32), | |
NotAttached, | |
Removed(i32), | |
} | |
use VarRep::*; | |
#[derive(Copy, Clone, PartialEq, Eq, Hash)] | |
struct Var(usize); | |
struct Model { | |
vars: HashMap<Var, VarRep>, | |
varcnt: usize, | |
} | |
impl Model { | |
fn new() -> Model { | |
Model { | |
vars: HashMap::new(), | |
varcnt: 0, | |
} | |
} | |
fn add_var(&mut self) -> Var { | |
let var = Var(self.varcnt); | |
self.vars.insert(var, NotAttached); | |
self.varcnt += 1; | |
var | |
} | |
fn update(&mut self) { | |
let newvars: Vec<_> = { | |
let (added, removed): (Vec<_>, Vec<_>) = | |
self.vars.iter().partition(|&(_, &rep)| match rep { | |
Removed(_) => false, | |
_ => true, | |
}); | |
// call GRBdelvars to delete `removed`. | |
added.iter() | |
.enumerate() | |
.map(|(newidx, &(&v, _))| (v, Attached(newidx as i32))) | |
.collect() | |
}; | |
self.vars = HashMap::from_iter(newvars.into_iter()); | |
} | |
} | |
fn main() { | |
let mut model = Model::new(); | |
let x = model.add_var(); | |
let y = model.add_var(); | |
assert_eq!(*model.vars.get(&x).unwrap(), NotAttached); | |
assert_eq!(*model.vars.get(&y).unwrap(), NotAttached); | |
model.update(); | |
assert_eq!(*model.vars.get(&x).unwrap(), Attached(0)); | |
assert_eq!(*model.vars.get(&y).unwrap(), Attached(1)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment