Type Alias minion_rs::Callback

source ·
pub type Callback = fn(solution_set: HashMap<VarName, Constant>) -> bool;
Expand description

The callback function used to capture results from Minion as they are generated.

This function is called by Minion whenever a solution is found. The input to this function is aHashMap of all named variables along with their value.

Callbacks should return true if search is to continue, false otherwise.

§Examples

Consider using a global mutex (or other static variable) to use returned solutions elsewhere.

For example:

  use minion_rs::ast::*;
  use minion_rs::run_minion;
  use std::{
      collections::HashMap,
      sync::{Mutex, MutexGuard},
  };

  // More elaborate data-structures are possible, but for sake of example store
  // a vector of solution sets.
  static ALL_SOLUTIONS: Mutex<Vec<HashMap<VarName,Constant>>>  = Mutex::new(vec![]);
   
  fn callback(solutions: HashMap<VarName,Constant>) -> bool {
      let mut guard = ALL_SOLUTIONS.lock().unwrap();
      guard.push(solutions);
      true
  }
    
  // Build and run the model.
  let mut model = Model::new();

  // ... omitted for brevity ...
  
  let res = run_minion(model, callback);
  res.expect("Error occurred");

  // Get solutions
  let guard = ALL_SOLUTIONS.lock().unwrap();
  let solution_set_1 = &(guard.get(0).unwrap());

  let x1 = solution_set_1.get("x").unwrap();
  let y1 = solution_set_1.get("y").unwrap();
  let z1 = solution_set_1.get("z").unwrap();