Skip to content

Commit

Permalink
Samples::states_mut
Browse files Browse the repository at this point in the history
  • Loading branch information
termoshtt committed Dec 24, 2024
1 parent d560d3a commit 7e6de9e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
17 changes: 8 additions & 9 deletions rust/ommx/src/convert/sample_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ impl Samples {
})
}

pub fn states_mut(&mut self) -> impl Iterator<Item = &mut State> {
self.entries.iter_mut().map(|v| {
v.state
.as_mut()
.expect("ommx.v1.Samples.Entry must has state. Broken Data.")
})
}

/// Transpose `sample_id -> decision_variable_id -> value` to `decision_variable_id -> sample_id -> value`
pub fn transpose(&self) -> HashMap<u64, SampledValues> {
let mut map: HashMap<u64, HashMap<OrderedFloat<f64>, Vec<u64>>> = HashMap::new();
Expand Down Expand Up @@ -113,15 +121,6 @@ impl Samples {
.collect::<Result<_>>()?,
})
}

pub fn map_state(&mut self, mut f: impl FnMut(&mut State) -> Result<()>) -> Result<()> {
for v in &mut self.entries {
f(v.state
.as_mut()
.context("ommx.v1.Samples.Entry must has state. Broken Data.")?)?;
}
Ok(())
}
}

impl SampleSet {
Expand Down
7 changes: 3 additions & 4 deletions rust/ommx/src/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,10 @@ impl Evaluate for Instance {

// Reconstruct decision variable values
let mut samples = samples.clone();
samples.map_state(|s| {
let mut new = eval_dependencies(&self.decision_variable_dependency, s)?;
for state in samples.states_mut() {
let mut new = eval_dependencies(&self.decision_variable_dependency, state)?;
used_ids.append(&mut new);
Ok(())
})?;
}
let mut transposed = samples.transpose();
let decision_variables: Vec<SampledDecisionVariable> = self
.decision_variables
Expand Down

0 comments on commit 7e6de9e

Please sign in to comment.