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
mod logical_memory;
use super::error::SubstitutionError;
use crate::{
check_self_assignment, decision_variable::VariableID, substitute_acyclic_via_one, v1::State,
ATol, Evaluate, Function, Substitute, VariableIDSet,
};
use fnv::FnvHashMap;
use petgraph::algo;
use petgraph::prelude::DiGraphMap;
use proptest::prelude::*;
/// Represents a set of assignment rules (`VariableID` -> `Function`)
/// that has been validated to be free of any circular dependencies.
#[derive(Debug, Clone, Default)]
pub struct AcyclicAssignments {
assignments: FnvHashMap<VariableID, Function>,
// The directed graph representing dependencies between assignments, assigned -> required.
dependency: DiGraphMap<VariableID, ()>,
}
impl AcyclicAssignments {
pub fn new(
iter: impl IntoIterator<Item = (VariableID, Function)>,
) -> Result<Self, SubstitutionError> {
let assignments: FnvHashMap<VariableID, Function> = iter.into_iter().collect();
let mut dependency = DiGraphMap::new();
// Add all variables being assigned to as nodes
for &var_id in assignments.keys() {
dependency.add_node(var_id);
}
// Add edges for dependencies
for (&assigned_var, linear) in &assignments {
for required_var in linear.required_ids() {
if required_var == assigned_var {
return Err(SubstitutionError::RecursiveAssignment {
var_id: assigned_var,
});
}
// Add edge from assigned variable to required variable
// to keep the order of topological sorting correct
dependency.add_edge(assigned_var, required_var, ());
}
}
// Check if the dependency graph is acyclic
if algo::is_cyclic_directed(&dependency) {
return Err(SubstitutionError::CyclicAssignmentDetected);
}
Ok(Self {
assignments,
dependency,
})
}
pub fn is_empty(&self) -> bool {
self.assignments.is_empty()
}
pub fn len(&self) -> usize {
self.assignments.len()
}
pub fn get(&self, id: &VariableID) -> Option<&Function> {
self.assignments.get(id)
}
pub fn iter(&self) -> impl Iterator<Item = (&VariableID, &Function)> {
self.assignments.iter()
}
fn sorted_ids(&self) -> Vec<VariableID> {
algo::toposort(&self.dependency, None).expect("Graph should be acyclic by construction")
}
/// Get the assignments in substitution order (variables that need to be replaced first).
///
/// This order is used when performing substitution operations where variables
/// that are depended upon by others should be substituted first.
///
/// # Example
///
/// ```rust
/// # use ommx::{assign, linear, coeff, AcyclicAssignments};
/// let assignments = assign! {
/// 1 <- linear!(2) + linear!(3), // x1 <- x2 + x3
/// 4 <- linear!(1) + coeff!(2.0) // x4 <- x1 + 2
/// };
///
/// let order: Vec<_> = assignments.substitution_order_iter()
/// .map(|(id, _)| id.into_inner())
/// .collect();
/// /// // x4 comes before x1 in substitution order because x4 has deeper dependencies
/// assert_eq!(order, vec![4, 1]);
/// ```
pub fn substitution_order_iter(&self) -> impl Iterator<Item = (VariableID, &Function)> {
self.sorted_ids()
.into_iter()
.filter_map(move |var_id| self.assignments.get(&var_id).map(|linear| (var_id, linear)))
}
/// Get the assignments in evaluation order (variables that should be evaluated first).
///
/// This order is used when evaluating assignments where variables that are
/// required by others should be evaluated first.
///
/// # Example
///
/// ```rust
/// # use ommx::{assign, linear, coeff, AcyclicAssignments, v1::State, ATol, Evaluate};
/// let assignments = assign! {
/// 1 <- linear!(2) + linear!(3), // x1 <- x2 + x3
/// 4 <- linear!(1) + coeff!(2.0) // x4 <- x1 + 2
/// };
///
/// let order: Vec<_> = assignments.evaluation_order_iter()
/// .map(|(id, _)| id.into_inner())
/// .collect();
/// /// // x1 comes before x4 in evaluation order because x1 must be evaluated before x4
/// assert_eq!(order, vec![1, 4]);
///
/// // When evaluating with state {x2: 1, x3: 2}:
/// let state = State::from_iter(vec![(2, 1.0), (3, 2.0)]);
/// let result = assignments.evaluate(&state, ATol::default()).unwrap();
///
/// // First x1 = x2 + x3 = 1 + 2 = 3 is computed
/// // Then x4 = x1 + 2 = 3 + 2 = 5 is computed
/// assert_eq!(result.entries[&1], 3.0);
/// assert_eq!(result.entries[&4], 5.0);
/// ```
pub fn evaluation_order_iter(&self) -> impl Iterator<Item = (VariableID, &Function)> {
self.sorted_ids()
.into_iter()
.rev()
.filter_map(move |var_id| self.assignments.get(&var_id).map(|linear| (var_id, linear)))
}
pub fn keys(&self) -> impl Iterator<Item = VariableID> + '_ {
self.assignments.keys().copied()
}
}
impl PartialEq for AcyclicAssignments {
fn eq(&self, other: &Self) -> bool {
// First check if assignments are equal
if self.assignments != other.assignments {
return false;
}
// Check if dependency graphs have the same nodes
let self_nodes: std::collections::BTreeSet<_> = self.dependency.nodes().collect();
let other_nodes: std::collections::BTreeSet<_> = other.dependency.nodes().collect();
if self_nodes != other_nodes {
return false;
}
// Check if dependency graphs have the same edges
let self_edges: std::collections::BTreeSet<_> = self.dependency.all_edges().collect();
let other_edges: std::collections::BTreeSet<_> = other.dependency.all_edges().collect();
self_edges == other_edges
}
}
impl IntoIterator for AcyclicAssignments {
type Item = (VariableID, Function);
type IntoIter = <FnvHashMap<VariableID, Function> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.assignments.into_iter()
}
}
impl Arbitrary for AcyclicAssignments {
type Parameters = AcyclicAssignmentsParameters;
type Strategy = BoxedStrategy<Self>;
fn arbitrary_with(p: Self::Parameters) -> Self::Strategy {
// Generate a random acyclic graph of assignments
proptest::collection::vec(
(
(0..=p.function_parameters.max_id().into_inner()).prop_map(VariableID::from),
Function::arbitrary_with(p.function_parameters),
),
0..=p.max_assignments,
)
.prop_filter_map("Acyclic", |assignments| {
AcyclicAssignments::new(assignments).ok()
})
.boxed()
}
}
#[derive(Debug, Clone, Copy)]
pub struct AcyclicAssignmentsParameters {
pub max_assignments: usize,
pub function_parameters: <Function as Arbitrary>::Parameters,
}
impl Default for AcyclicAssignmentsParameters {
fn default() -> Self {
Self {
max_assignments: 10,
function_parameters: <Function as Arbitrary>::Parameters::default(),
}
}
}
impl Evaluate for AcyclicAssignments {
type Output = State;
type SampledOutput = FnvHashMap<VariableID, crate::Sampled<f64>>;
fn evaluate(&self, state: &State, atol: ATol) -> crate::Result<Self::Output> {
let mut extended_state = state.clone();
// Evaluate assignments in topological order
//
// When the assignment is x1 <- x2 + x3, x4 <- x1 + 2, and state is {x2: 1, x3: 2},
// we first evaluate x1 = 3, then x4 = 5. Finally returns extended state {x1: 3, x2: 1, x3: 2, x4: 5}.
for (var_id, function) in self.evaluation_order_iter() {
let value = function.evaluate(&extended_state, atol)?;
extended_state.entries.insert(var_id.into_inner(), value);
}
Ok(extended_state)
}
fn partial_evaluate(&mut self, state: &State, atol: ATol) -> crate::Result<()> {
// Create new assignments with partial evaluation applied
let mut new_assignments = Vec::new();
for (var_id, function) in self.assignments.iter() {
let mut function_clone = function.clone();
function_clone.partial_evaluate(state, atol)?;
new_assignments.push((*var_id, function_clone));
}
// Rebuild using new method to ensure acyclicity is maintained
*self = Self::new(new_assignments)?;
Ok(())
}
fn evaluate_samples(
&self,
samples: &crate::Sampled<State>,
atol: ATol,
) -> crate::Result<Self::SampledOutput> {
let mut result = FnvHashMap::default();
// For each assignment in topological order
for (var_id, function) in self.substitution_order_iter() {
let sampled_values = function.evaluate_samples(samples, atol)?;
result.insert(var_id, sampled_values);
}
Ok(result)
}
fn required_ids(&self) -> VariableIDSet {
self.assignments
.values()
.flat_map(|function| function.required_ids())
.collect()
}
}
impl Substitute for AcyclicAssignments {
type Output = Self;
fn substitute_acyclic(
self,
acyclic: &crate::AcyclicAssignments,
) -> Result<Self::Output, crate::SubstitutionError> {
// If self is empty, just return a clone of acyclic (the new assignments)
if self.is_empty() {
return Ok(acyclic.clone());
}
// If acyclic is empty, nothing to substitute
if acyclic.is_empty() {
return Ok(self);
}
substitute_acyclic_via_one(self, acyclic)
}
fn substitute_one(
self,
assigned: VariableID,
function: &Function,
) -> Result<Self::Output, SubstitutionError> {
check_self_assignment(assigned, function)?;
// Apply substitution to each assignment function
let mut new_assignments = Vec::new();
for (var_id, func) in self.assignments {
let substituted_func = func.substitute_one(assigned, function)?;
new_assignments.push((var_id, substituted_func));
}
new_assignments.push((assigned, function.clone()));
// Create new AcyclicAssignments with substituted functions
// This will rebuild the dependency graph and check for cycles
Self::new(new_assignments)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{assign, coeff, linear};
#[test]
fn test_substitute_acyclic_success() {
// Create initial assignments: x1 <- x2 + x3
let initial = assign! {
1 <- linear!(2) + linear!(3)
};
// Substitute x3 <- x4 + 1
let substitution = assign! {
3 <- linear!(4) + coeff!(1.0)
};
// Expected result: x1 <- x2 + x4 + 1, x3 <- x4 + 1
let expected = assign! {
1 <- linear!(2) + linear!(4) + coeff!(1.0),
3 <- linear!(4) + coeff!(1.0)
};
let result = initial.substitute_acyclic(&substitution).unwrap();
assert_eq!(result, expected);
}
#[test]
fn test_substitute_acyclic_fail() {
// Create initial assignments: x1 <- x2 + x3
let initial = assign! {
1 <- linear!(2) + linear!(3)
};
// Substitute x3 <- x1 + 1
// This causes a cyclic dependency
let substitution = assign! {
3 <- linear!(1) + coeff!(1.0)
};
insta::assert_snapshot!(
initial.substitute_acyclic(&substitution).unwrap_err(),
@"Recursive assignment detected: variable 1 cannot be assigned to a function that depends on itself"
);
}
#[test]
fn test_evaluate_topological_order() {
// Test case based on the comment in evaluate method:
// When the assignment is x1 <- x2 + x3, x4 <- x1 + 2, and state is {x2: 1, x3: 2},
// we first evaluate x1 = 3, then x4 = 5. Finally returns extended state {x1: 3, x2: 1, x3: 2, x4: 5}.
let assignments = assign! {
1 <- linear!(2) + linear!(3), // x1 <- x2 + x3
4 <- linear!(1) + coeff!(2.0) // x4 <- x1 + 2
};
let state = State::from_iter(vec![(2, 1.0), (3, 2.0)]); // {x2: 1, x3: 2}
let result = assignments.evaluate(&state, ATol::default()).unwrap();
// Expected extended state: {x1: 3, x2: 1, x3: 2, x4: 4}
assert_eq!(result.entries[&1], 3.0); // x1 = x2 + x3 = 1 + 2 = 3
assert_eq!(result.entries[&2], 1.0); // x2 = 1 (original)
assert_eq!(result.entries[&3], 2.0); // x3 = 2 (original)
assert_eq!(result.entries[&4], 5.0); // x4 = x1 + 2 = 3 + 2 = 5
}
}