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
use crate::math::distances::WeightedDistances;
use ebi_objects::{
anyhow::{Context, Result, anyhow},
ebi_arithmetic::{
MaybeExact, One, Zero,
fraction::{fraction_exact::FractionExact, signed::Numerator},
malachite::{Integer, base::num::basic::traits::One as MOne},
},
};
use ebi_optimisation::network_simplex::NetworkSimplex;
use rayon::iter::ParallelIterator;
use rayon::prelude::*;
/// Authored by Leonhard Mühlmeyer (2024)
/// Implementation of the Earth Movers Stochastic Conformance Cheching (EMSC) described in
/// Leemans et al. *Earth movers’ stochastic conformance checking.* BPM Forum 2019.
/// Leemans et al. *Stochastic process mining: Earth movers’ stochastic conformance.* Information Systems 102 2021.
impl dyn WeightedDistances {
/// # Algorithm
/// 1. **Compute all pairwise distances** between the traces of the two languages (parallelized, see `DistanceMatrix`).
///
/// 2. **Is exact arithmetic required?**<br>
/// a. Calculate the Least Common Multiple (LCM) of all denominators of distances (i.e., the elements in the `DistanceMatrix`).<br>
/// b. Calculate the Least Common Multiple (LCM) of all denominators of trace probabilities from both stochastic languages.<br>
/// c. If the LCMs are within the range of `i64`, use `i64` for the `NetworkSimplex` computation (remains exact but faster).
/// If the LCMs are within the range of `i128`, use `i128` for the `NetworkSimplex` computation.
/// Otherwise, use `BigInt`.<br>
/// d. Scale the distances and probabilities by the respective identified LCM to retrieve integer values.<br>
/// e. Create a network graph with the scaled distances and probabilities:<br>
/// i. For each trace in the first language, create a supply node with the corresponding trace probability as supply.<br>
/// ii. For each trace in the second language, create a demand node with the corresponding trace probability as demand (i.e., negative supply).<br>
/// iii. Create an edge between each pair of traces with the respective scaled distance as cost.<br>
/// f. Run the `NetworkSimplex` algorithm to find the optimal flow between the supply and demand nodes.<br>
/// g. Calculate the EMSC value as `1 - (result / (LCM of distances * LCM of probabilities))` (i.e., undo the scaling trick).<br>
///
/// 3. **If exact arithmetic is not required**, use `f64` for the `NetworkSimplex` computation.<br>
/// a. Create a network graph with the scaled distances and probabilities:<br>
/// i. For each trace in the first language, create a supply node with the corresponding trace probability as supply.<br>
/// ii. For each trace in the second language, create a demand node with the corresponding trace probability as demand (i.e., negative supply).<br>
/// iii. Create an edge between each pair of traces with the respective distance as cost.<br>
/// b. Run the `NetworkSimplex` algorithm to find the optimal flow between the supply and demand nodes.<br>
/// c. Calculate the EMSC value as `1 - result`.
pub fn earth_movers_stochastic_conformance(&self) -> Result<FractionExact> {
if self.len_a() == 0 || self.len_b() == 0 {
return Err(anyhow!("One of the languages is empty."));
}
// 2. Is exact arithmetic required?
log::info!("Calculating exact EMSC value");
// 2a. Calculate the Least Common Multiple (LCM) of all denominators of distances (i.e. the elements in the DistanceMatrix).
let lcm_distances = self.lowest_common_multiple_denominators_distances()?;
let lcm_probabilities = self.lowest_common_multiple_denominators_weights()?;
// 2b. Calculate the Least Common Multiple (LCM) of all denominators of trace probabilities from both stochastic languages.
let n = self.len_a();
let m = self.len_b();
let lcm_distance_fraction = FractionExact::try_from(lcm_distances.clone())?;
let lcm_probability_fraction = FractionExact::try_from(lcm_probabilities.clone())?;
log::debug!(
"LCM of distances: {:?} \n LCM of probabilities {:?}",
lcm_distances,
lcm_probabilities
);
// 2c. If the LCMs are within the range of i64, use i64 for the NetworkSimplex computation (remains exact but faster). Otherwise use BigInt.
if lcm_probabilities <= Integer::from(i64::MAX)
&& (Into::<Integer>::into(lcm_distances.clone()) + Integer::ONE) * Integer::from(n + m)
<= Integer::from(i64::MAX)
{
log::info!("Using i64 for NetworkSimplex computation.");
// (i64) 2e. Create a network graph with the scaled distances and probabilities:
// (i64) 2e(i). For each trace in the first language, create a supply node with the corresponding trace probability as supply.
// (i64) 2e(ii). For each trace in the second language, create a demand node with the corresponding trace probability as demand (i.e. negative supply).
let mut supply = vec![0i64; n + m];
supply
.par_chunks_mut(1024)
.enumerate()
.for_each(|(chunk_idx, chunk)| {
chunk.iter_mut().enumerate().for_each(|(i, s)| {
let idx = chunk_idx * 1024 + i;
*s = if idx < n {
(self.weight_a(idx) * &lcm_probability_fraction)
.exact_ref()
.unwrap()
.numerator_ref()
.try_into()
.unwrap()
} else if idx < n + m {
-TryInto::<i64>::try_into(
(self.weight_b(idx - n) * &lcm_probability_fraction)
.exact_ref()
.unwrap()
.numerator_ref(),
)
.unwrap()
} else {
0
};
});
});
// (i64) 2d. Scale the distances and probabilities by the respective identified LCM to retrieve integer values.
// (i64) 2e(iii). Create an edge between each pair of traces with the respective scaled distance as cost.
let mut graph_and_costs = vec![vec![None; n + m]; n + m];
for i in 0..n {
for j in 0..m {
let product = self.distance(i, j) * &lcm_distance_fraction;
let i64 = product
.exact_ref()
.unwrap()
.numerator_ref()
.try_into()
.unwrap();
graph_and_costs[i][j + n] = Some(i64);
}
}
// (i64) 2f. Run the NetworkSimplex algorithm to find the optimal flow between the supply and demand nodes.
log::info!("Starting Network Simplex.");
let mut ns = NetworkSimplex::new(&graph_and_costs, &supply, false, false);
ns.run(false);
let ns_result = ns
.get_bigint_result()
.context("NetworkSimplex did not return a result, cannot calculate EMSC")?;
log::debug!("NetworkSimplex result: {:?}", ns_result);
// (i64) 2g. Calculate the EMSC value as 1 - (result / (LCM of distances * LCM of probabilities)) (i.e. undo the scaling trick).
let mut result = FractionExact::from(1);
let mut distance = FractionExact::try_from(ns_result)?;
distance /= FractionExact::try_from(lcm_distances)?;
distance /= FractionExact::try_from(lcm_probabilities)?;
result -= distance;
return Ok(result);
} else if lcm_probabilities <= Integer::from(i128::MAX)
&& (Into::<Integer>::into(lcm_distances.clone()) + Integer::one())
* Integer::from(n + m)
<= Integer::from(i128::MAX)
{
log::info!("Using i128 for NetworkSimplex computation.");
// (i128) 2e. Create a network graph with the scaled distances and probabilities:
// (i128) 2e(i). For each trace in the first language, create a supply node with the corresponding trace probability as supply.
// (i128) 2e(ii). For each trace in the second language, create a demand node with the corresponding trace probability as demand (i.e. negative supply).
let mut supply = vec![0i128; n + m];
supply
.par_chunks_mut(1024)
.enumerate()
.for_each(|(chunk_idx, chunk)| {
chunk.iter_mut().enumerate().for_each(|(i, s)| {
let idx = chunk_idx * 1024 + i;
*s = if idx < n {
(self.weight_a(idx) * &lcm_probability_fraction)
.exact_ref()
.unwrap()
.numerator_ref()
.try_into()
.unwrap()
} else if idx < n + m {
-TryInto::<i128>::try_into(
(self.weight_b(idx - n) * &lcm_probability_fraction)
.exact_ref()
.unwrap()
.numerator_ref(),
)
.unwrap()
} else {
0
};
});
});
// (i128) 2d. Scale the distances and probabilities by the respective identified LCM to retrieve integer values.
// (i128) 2e(iii). Create an edge between each pair of traces with the respective scaled distance as cost.
let mut graph_and_costs = vec![vec![None; n + m]; n + m];
for i in 0..n {
for j in 0..m {
let product = self.distance(i, j) * &lcm_distance_fraction;
let i128 = product
.exact_ref()
.unwrap()
.numerator_ref()
.try_into()
.unwrap();
graph_and_costs[i][j + n] = Some(i128);
}
}
// 2f. Run the NetworkSimplex algorithm to find the optimal flow between the supply and demand nodes.
log::info!("Starting Network Simplex.");
let mut ns = NetworkSimplex::new(&graph_and_costs, &supply, false, false);
ns.run(false);
let ns_result = ns
.get_bigint_result()
.context("NetworkSimplex did not return a result, cannot calculate EMSC")?;
log::debug!("NetworkSimplex result: {:?}", ns_result);
// (i128) 2g. Calculate the EMSC value as 1 - (result / (LCM of distances * LCM of probabilities)) (i.e. undo the scaling trick).
let mut result = FractionExact::from(1);
let mut distance = FractionExact::try_from(ns_result)?;
distance /= FractionExact::try_from(lcm_distances)?;
distance /= FractionExact::try_from(lcm_probabilities)?;
result -= distance;
return Ok(result);
} else {
log::info!("Using BigInt for NetworkSimplex computation.");
// (BigInt) 2e. Create a network graph with the scaled distances and probabilities:
// (BigInt) 2e(i). For each trace in the first language, create a supply node with the corresponding trace probability as supply.
// (BigInt) 2e(ii). For each trace in the second language, create a demand node with the corresponding trace probability as demand (i.e. negative supply).
let mut supply = vec![Integer::zero(); n + m];
supply
.par_chunks_mut(1024)
.enumerate()
.for_each(|(chunk_idx, chunk)| {
chunk.iter_mut().enumerate().for_each(|(i, s)| {
let idx = chunk_idx * 1024 + i;
*s = if idx < n {
(self.weight_a(idx) * &lcm_probability_fraction)
.exact_ref()
.unwrap()
.numerator_ref()
.into()
} else if idx < n + m {
-Into::<Integer>::into(
(self.weight_b(idx - n) * &lcm_probability_fraction)
.exact_ref()
.unwrap()
.numerator_ref(),
)
} else {
Integer::zero()
};
});
});
// 2d(BigInt). Scale the distances and probabilities by the respective identified LCM to retrieve integer values.
// (BigInt) 2e(iii). Create an edge between each pair of traces with the respective scaled distance as cost.
let mut graph_and_costs = vec![vec![None; n + m]; n + m];
for index_a in 0..n {
for index_b in 0..m {
let product = self.distance(index_a, index_b) * &lcm_distance_fraction;
let bigint = product.exact_ref().unwrap().signed_numerator();
graph_and_costs[index_a][index_b + n] = Some(bigint);
}
}
// 2f. Run the NetworkSimplex algorithm to find the optimal flow between the supply and demand nodes.
log::info!("Starting Network Simplex.");
let mut ns = NetworkSimplex::new(&graph_and_costs, &supply, false, false);
ns.run(false);
let ns_result = ns
.get_result()
.context("NetworkSimplex did not return a result, cannot calculate EMSC")?;
log::debug!("NetworkSimplex result: {:?}", ns_result);
// 2g. Calculate the EMSC value as 1 - (result / (LCM of distances * LCM of probabilities)) (i.e. undo the scaling trick).
let mut result = FractionExact::one();
let mut distance = FractionExact::try_from(ns_result)?;
distance /= FractionExact::try_from(lcm_distances)?;
distance /= FractionExact::try_from(lcm_probabilities)?;
result -= distance;
return Ok(result);
}
}
}
#[cfg(test)]
mod tests {
use crate::{
ebi_traits::ebi_trait_finite_stochastic_language::EbiTraitFiniteStochasticLanguage,
techniques::earth_movers_stochastic_conformance::EarthMoversStochasticConformance,
};
use ebi_objects::{
FiniteStochasticLanguage,
ebi_arithmetic::{Fraction, One, Zero},
};
use std::fs;
#[test]
fn emsc_one() {
let fin1 = fs::read_to_string("testfiles/aa.slang").unwrap();
let mut slang1: Box<dyn EbiTraitFiniteStochasticLanguage> =
Box::new(fin1.parse::<FiniteStochasticLanguage>().unwrap());
let fin2 = fs::read_to_string("testfiles/aa.slang").unwrap();
let mut slang2 = fin2.parse::<FiniteStochasticLanguage>().unwrap();
let emsc = slang1
.earth_movers_stochastic_conformance(&mut slang2)
.unwrap();
assert_eq!(emsc, Fraction::one());
}
#[test]
fn emsc_zero() {
let fin1 = fs::read_to_string("testfiles/aa.slang").unwrap();
let mut slang1: Box<dyn EbiTraitFiniteStochasticLanguage> =
Box::new(fin1.parse::<FiniteStochasticLanguage>().unwrap());
let fin2 = fs::read_to_string("testfiles/bb.slang").unwrap();
let mut slang2 = fin2.parse::<FiniteStochasticLanguage>().unwrap();
let emsc = slang1
.earth_movers_stochastic_conformance(&mut slang2)
.unwrap();
assert_eq!(emsc, Fraction::zero());
}
}