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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
use fnv::FnvHashMap;
use std::fmt::Debug;
use std::hash::Hash;

use crate::Model;
use crate::{Error, Result};
use grb_sys2 as ffi;

mod private_traits {
    use super::*;

    pub trait ModelObjectPrivate: Sized + Hash + Eq + Copy {
        fn from_raw(id: u32, model_id: u32) -> Self;
        fn idx_manager_mut(model: &mut Model) -> &mut IdxManager<Self>;
        fn idx_manager(model: &Model) -> &IdxManager<Self>;
        unsafe fn gurobi_remove(m: *mut ffi::GRBmodel, inds: &[i32]) -> ffi::c_int;
        fn model_id(&self) -> u32;
    }
}

use private_traits::ModelObjectPrivate;

/// This trait encompasses all Gurobi model objects: [`Var`], [`Constr`], [`QConstr`] and [`SOS`].
/// Each `ModelObject` is associated with a particular model, and can only be used with that model.
/// Each `ModelObject` also has a unique, fixed 32-bit ID.  Gurobi itself uses an `i32` to index objects
/// (only positive indices are used), so the 32-bit limitation is already there.  Note that IDs are only
/// guaranteed to be unique if the concrete types of the `ModelObject` are the same and the objects
/// belong to the same model.  For example, if `v` is a `Var` and `c` is a `Constr`, then `v` and `c` may
/// have the same ID.  Additionally, if `s` is also a `Var`, but doesn't belong to the same `Model` as `v`,
/// `s` and `v` may have the same ID.
pub trait ModelObject: ModelObjectPrivate + Debug {
    /// Retrieve the object's ID.
    fn id(&self) -> u32;
}

macro_rules! create_model_obj_ty {
    ($t:ident, $model_attr:ident, $delfunc:path, $doc:literal) => {
      #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
      #[doc = $doc]
      pub struct $t {
        pub(crate) id : u32,
        pub(crate) model_id: u32,
      }

      impl ModelObjectPrivate for $t {
        fn from_raw(id: u32, model_id: u32) -> $t {
          Self{ id, model_id }
        }

        fn idx_manager_mut(model: &mut Model) -> &mut IdxManager<$t> {
          &mut model.$model_attr
        }

        fn idx_manager(model: &Model) -> &IdxManager<$t> {
          &model.$model_attr
        }

        unsafe fn gurobi_remove(m: *mut ffi::GRBmodel, inds: &[i32]) -> ffi::c_int {
          $delfunc(m, inds.len() as i32, inds.as_ptr())
        }

        fn model_id(&self) -> u32 { self.model_id }
      }

      impl ModelObject for $t {
        fn id(&self) -> u32 { self.id }
      }

    };
}

create_model_obj_ty!(Var, vars, ffi::GRBdelvars,
  "A Gurobi variable.

  To interact with the attributes of a variable, use [`Model::get_obj_attr`] and [`Model::set_obj_attr`]"
  );
create_model_obj_ty!(Constr, constrs, ffi::GRBdelconstrs,
  "A linear constraint added to a [`Model`]

  To interact with the attributes of a constraint, use [`Model::get_obj_attr`] and [`Model::set_obj_attr`]"
);
create_model_obj_ty!(QConstr, qconstrs, ffi::GRBdelqconstrs,
"A quadratic constraint added to a [`Model`]

  To interact with the attributes of a constraint, use [`Model::get_obj_attr`] and [`Model::set_obj_attr`]"
);
create_model_obj_ty!(SOS, sos, ffi::GRBdelsos,
"An SOS constraint added to a [`Model`]

 To interact with the attributes of a constraint, use [`Model::get_obj_attr`] and [`Model::set_obj_attr`]"
);

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
enum IdxState {
    Present(i32),
    // has been processed with a call to update()
    Build(i32),
    // has an index and can be used for building, and setting but not querying attributes
    Pending,
    // hasn't got an index yet, cannot be used for building, setting, or querying attributes
    Removed(i32), // object has been removed, but can still be used to build and set attributes.
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
enum UpdateAction {
    Noop = 0,
    Fix = 1,
    // Sweep from the back of the ordering, mapping Build(idx) to Present(idx)
    Rebuild = 2, // Rebuild the whole lookup
}

/// A struct to keep track of the index state of model objects such as `Var`, `Constr`, `QConstr` and `SOS`
/// It also maintains the absolute order of variables, with respect to the order
/// If variables have been removed, it is necessary to update to rebuild the lookup (see the `update` method).
/// The `update_action` field is an optimisation to avoid having to do this for "appends" (only adding new variables)
#[derive(Debug)]
#[doc(hidden)]
pub struct IdxManager<T: Hash + Eq> {
    update_model: bool,
    update_action: UpdateAction,
    next_id: u32,
    model_id: u32,
    order: Vec<T>,
    lookup: FnvHashMap<T, IdxState>,
}

impl<T: ModelObject> IdxManager<T> {
    pub(crate) fn new_with_existing_obj(model_id: u32, nobj: usize) -> IdxManager<T> {
        let mut im = IdxManager::new(model_id);
        for id in 0..nobj {
            let v = T::from_raw(id as u32, model_id);
            im.order.push(v);
            im.lookup.insert(v, IdxState::Present(id as i32));
        }
        im.next_id = nobj as u32;
        im
    }

    pub(crate) fn new(model_id: u32) -> IdxManager<T> {
        let order = Vec::new();
        let lookup = FnvHashMap::default();
        IdxManager {
            order,
            lookup,
            model_id,
            next_id: 0,
            update_action: UpdateAction::Noop,
            update_model: false,
        }
    }

    fn mark_update_action(&mut self, a: UpdateAction) {
        if a > self.update_action {
            self.update_action = a
        }
    }

    pub(crate) fn get_index(&self, o: &T) -> Result<i32> {
        if let Some(state) = self.lookup.get(o) {
            match *state {
                IdxState::Removed(_) => Err(Error::ModelObjectRemoved),
                IdxState::Pending | IdxState::Build(_) => Err(Error::ModelObjectPending),
                IdxState::Present(idx) => Ok(idx),
            }
        } else if o.model_id() == self.model_id {
            Err(Error::ModelObjectRemoved)
        } else {
            Err(Error::ModelObjectMismatch)
        }
    }

    pub(crate) fn get_index_build(&self, o: &T) -> Result<i32> {
        if let Some(state) = self.lookup.get(o) {
            match *state {
                IdxState::Pending => Err(Error::ModelObjectPending),
                IdxState::Present(idx) | IdxState::Build(idx) | IdxState::Removed(idx) => Ok(idx),
            }
        } else if o.model_id() == self.model_id {
            Err(Error::ModelObjectRemoved)
        } else {
            Err(Error::ModelObjectMismatch)
        }
    }

    pub(crate) fn model_update_needed(&self) -> bool {
        self.update_model
    }

    pub(crate) fn objects(&self) -> &[T] {
        assert!(!self.update_model);
        self.order.as_slice()
    }

    pub(crate) fn remove(&mut self, o: T, _update_lazy: bool) -> Result<()> {
        if o.model_id() != self.model_id {
            return Err(Error::ModelObjectMismatch);
        }

        let state = self.lookup.get_mut(&o).ok_or(Error::ModelObjectRemoved)?;
        match *state {
            IdxState::Build(_) | IdxState::Pending => return Err(Error::ModelObjectPending),
            IdxState::Present(idx) => *state = IdxState::Removed(idx),
            IdxState::Removed(_) => return Err(Error::ModelObjectRemoved),
        }
        self.update_model = true;
        self.mark_update_action(UpdateAction::Rebuild);
        debug_assert_eq!(self.lookup.len(), self.order.len());
        Ok(())
    }

    pub fn add_new(&mut self, update_lazy: bool) -> T {
        debug_assert_eq!(self.lookup.len(), self.order.len());
        let o = T::from_raw(self.next_id, self.model_id);
        self.next_id += 1;
        self.mark_update_action(UpdateAction::Fix);
        let state = if update_lazy {
            IdxState::Pending
        } else {
            IdxState::Build(self.lookup.len() as i32)
        };
        self.update_model = true;
        #[cfg(debug_assertions)]
        {
            // can't do vec![self.add_new(_); 100], since this just clones a bunch of shit
            if let Some(other) = self.order.last() {
                let s = self.lookup[other];
                if s != IdxState::Pending {
                    assert_ne!(s, state);
                }
            }
        }

        self.lookup.insert(o, state);
        self.order.push(o);
        o
    }

    // debug helper
    #[cfg(debug_assertions)]
    #[allow(dead_code)]
    fn print_vars(&self) {
        println!("----------------------------------------------------");
        for o in &self.order {
            print!("{:?} ", self.lookup[o]);
        }
        println!();
    }

    pub(crate) fn update(&mut self) {
        debug_assert_eq!(self.lookup.len(), self.order.len());
        use std::collections::hash_map::Entry;

        match self.update_action {
            UpdateAction::Noop => {}

            UpdateAction::Fix => {
                // O(k) where k is the number of elements that need to be updated
                let mut k = self.order.len() as i32 - 1;
                for var in self.order.iter().rev() {
                    let state = self.lookup.get_mut(var).unwrap();
                    match *state {
                        IdxState::Removed(_) => unreachable!(),
                        IdxState::Pending => {
                            *state = IdxState::Present(k);
                        }
                        IdxState::Build(idx) => {
                            debug_assert_eq!(idx, k);
                            *state = IdxState::Present(k);
                        }
                        IdxState::Present(_) => break,
                    };
                    k -= 1;
                }
            }

            UpdateAction::Rebuild => {
                // O(n) where n is the total number of elements.
                let mut k = 0i32;
                let order = &mut self.order;
                let lookup = &mut self.lookup;
                order.retain(|&o| match lookup.entry(o) {
                    Entry::Vacant(_) => unreachable!("bug, should always have an entry in lookup"),
                    Entry::Occupied(mut e) => {
                        let state = *e.get();
                        match state {
                            IdxState::Present(_) | IdxState::Build(_) | IdxState::Pending => {
                                e.insert(IdxState::Present(k));
                                k += 1;
                                true
                            }
                            IdxState::Removed(_) => {
                                e.remove();
                                false
                            }
                        }
                    }
                });
                debug_assert_eq!(k as usize, self.lookup.len());
            }
        }

        debug_assert_eq!(self.lookup.len(), self.lookup.len());
        self.update_model = false;
        self.update_action = UpdateAction::Noop;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;

    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
    enum Action {
        SwitchUpdateMode,
        AddVar(u8),
        RemoveVar(u8),
    }

    fn action_strat(num: usize) -> impl Strategy<Value = Vec<Action>> {
        let s = prop_oneof![
            Just(Action::SwitchUpdateMode),
            any::<u8>().prop_map(Action::AddVar),
            any::<u8>().prop_map(Action::RemoveVar),
        ];
        proptest::collection::vec(s, ..num)
    }

    fn state_machine(actions: Vec<Action>) {
        let mut update_mode_lazy = true;
        let mut vars: Vec<Option<Var>> = vec![None; u8::MAX as usize + 1];
        let mut idx_manager = IdxManager::new(0);
        for a in actions {
            match a {
                Action::SwitchUpdateMode => {
                    update_mode_lazy = !update_mode_lazy;
                    if !update_mode_lazy {
                        idx_manager.update(); // purge old pending states
                    }
                }
                Action::AddVar(i) => {
                    let i = i as usize;
                    let v = vars[i];
                    match v {
                        Some(v) => {
                            if !update_mode_lazy {
                                idx_manager.get_index_build(&v).unwrap();
                            }
                        }
                        None => {
                            vars[i] = Some(idx_manager.add_new(update_mode_lazy));
                        }
                    }
                }
                Action::RemoveVar(i) => {
                    let i = i as usize;
                    let v = vars[i];
                    match v {
                        Some(v) => match idx_manager.remove(v, update_mode_lazy) {
                            Ok(()) => vars[i] = None,
                            Err(e) => assert_eq!(e, Error::ModelObjectPending),
                        },
                        None => {}
                    }
                }
            }
        }
    }

    proptest! {
      #[test]
      fn fuzz(actions in action_strat(100)) {
        state_machine(actions);
      }
    }

    #[test]
    fn regressions() {
        use Action::*;
        state_machine(vec![AddVar(4), SwitchUpdateMode, AddVar(4)]);
        state_machine(vec![
            AddVar(0),
            AddVar(1),
            AddVar(2),
            RemoveVar(1),
            SwitchUpdateMode,
            AddVar(1),
        ]);
    }
}