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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
use crate::biodivine_std::traits::Set;
use crate::symbolic_async_graph::{GraphColoredVertices, SymbolicAsyncGraph};
use crate::VariableId;
use biodivine_lib_bdd::Bdd;
/// Basic symbolic graph operators. For convenience, there is a wide variety of different
/// operators fulfilling different needs. Here, all operators only analyse transitions with
/// respect to a single network variable and every operation implemented using one symbolic
/// operation.
///
/// The general recommendation is to use `var_post` / `var_pre` for most tasks
/// (implementing additional logic using extra symbolic operations), and once the algorithm is
/// tested and stable, replace these functions with the more efficient "fused" operations.
///
/// We provide the following variable-specific operations:
/// - `var_post` / `var_pre`: General successors or predecessors.
/// - `var_post_out` / `var_pre_out`: Successors or predecessors, but only *outside* of the
/// given initial set.
/// - `var_post_within` / `var_pre_within`: Successors or predecessors, but only *inside* the
/// given initial set.
/// - `var_can_post` / `var_can_pre`: Subset of the initial set that has some
/// successors / predecessors.
/// - `var_can_post_out` / `var_can_pre_out`: Subset of the initial set that can perform
/// a transition leading *outside* of the initial set.
/// - `var_can_post_within` / `var_can_pre_within`: Subset of the initial set that can perform
/// a transition leading *into* the initial set.
///
/// Note that the output of some of these functions is technically equivalent (e.g.
/// `var_post_within` and `var_can_pre_within`) but we nevertheless provided all for completeness.
///
impl SymbolicAsyncGraph {
/// Compute the `GraphColoredVertices` representing the successors of the vertices in the
/// given `initial` set that are reached by updating the given `variable`. Formally:
///
/// $$
/// \texttt{VarPost}(v, X) = \\{~(y, c) \mid \exists x.~(x, c) \in X \land x \xrightarrow{v}_c y~\\}
/// $$
pub fn var_post(
&self,
variable: VariableId,
initial: &GraphColoredVertices,
) -> GraphColoredVertices {
// flip(initial & can_apply_function)
let symbolic_var = self.symbolic_context.state_variables[variable.0];
let output = Bdd::fused_binary_flip_op(
(&initial.bdd, None),
(&self.update_functions[variable.0], None),
Some(symbolic_var),
biodivine_lib_bdd::op_function::and,
);
GraphColoredVertices::new(output, &self.symbolic_context)
}
/// Compute the `GraphColoredVertices` representing the predecessors of the vertices in the
/// given `initial` set that are reached by updating the given `variable`. Formally:
///
/// $$
/// \texttt{VarPre}(v, X) = \\{~(x, c) \mid \exists y.~(y, c) \in X \land x \xrightarrow{v}_{c} y~\\}
/// $$
pub fn var_pre(
&self,
variable: VariableId,
initial: &GraphColoredVertices,
) -> GraphColoredVertices {
// flip(set) & can_apply_function
let symbolic_var = self.symbolic_context.state_variables[variable.0];
let output = Bdd::fused_binary_flip_op(
(&initial.bdd, Some(symbolic_var)),
(&self.update_functions[variable.0], None),
None,
biodivine_lib_bdd::op_function::and,
);
GraphColoredVertices::new(output, &self.symbolic_context)
}
/// Compute the `GraphColoredVertices` representing the successors of the vertices in the
/// given `initial` set that are reached by updating the given `variable`, but are *not* in
/// the `initial` set. Formally:
///
/// $$
/// \texttt{VarPostOut}(v, X) = \\{~(y, c) \mid (y, c) \notin X \land \exists x.~(x, c) \in X \land x \xrightarrow{v}_{c} y~\\}
/// $$
pub fn var_post_out(
&self,
variable: VariableId,
initial: &GraphColoredVertices,
) -> GraphColoredVertices {
// flip(set & !flip(set) & can_apply_function)
let symbolic_var = self.symbolic_context.state_variables[variable.0];
let output = Bdd::fused_ternary_flip_op(
(&initial.bdd, None),
(&initial.bdd, Some(symbolic_var)),
(&self.update_functions[variable.0], None),
Some(symbolic_var),
|a, b, c| {
// a & !b & c
match (a, b, c) {
(Some(false), _, _) => Some(false),
(_, Some(true), _) => Some(false),
(_, _, Some(false)) => Some(false),
(Some(true), Some(false), Some(true)) => Some(true),
_ => None,
}
},
);
GraphColoredVertices::new(output, &self.symbolic_context)
}
/// Compute the `GraphColoredVertices` representing the predecessors of the vertices in the
/// given `initial` set that are reached by updating the given `variable`, but are *not* in
/// the initial set. Formally:
///
/// $$
/// \texttt{VarPreOut}(v, X) = \\{~(x, c) \mid (x, c) \notin X \land \exists y.~(y, c) \in X \land x \xrightarrow{v}_{c} y~\\}
/// $$
pub fn var_pre_out(
&self,
variable: VariableId,
initial: &GraphColoredVertices,
) -> GraphColoredVertices {
// !set & flip(set) & can_apply_function
let symbolic_var = self.symbolic_context.state_variables[variable.0];
let output = Bdd::fused_ternary_flip_op(
(&initial.bdd, None),
(&initial.bdd, Some(symbolic_var)),
(&self.update_functions[variable.0], None),
None,
|a, b, c| {
// !a & b & c
match (a, b, c) {
(Some(true), _, _) => Some(false),
(_, Some(false), _) => Some(false),
(_, _, Some(false)) => Some(false),
(Some(false), Some(true), Some(true)) => Some(true),
_ => None,
}
},
);
GraphColoredVertices::new(output, &self.symbolic_context)
}
/// Compute the `GraphColoredVertices` representing the successors of the vertices in the
/// given `initial` set that are reached by updating the given `variable`, but are *within*
/// the `initial` set. Formally:
///
/// $$
/// \texttt{VarPostWithin}(v, X) = \\{~(y, c) \mid (y, c) \in X \land \exists x.~(x, c) \in X \land x \xrightarrow{v}_{c} y~\\}
/// $$
pub fn var_post_within(
&self,
variable: VariableId,
initial: &GraphColoredVertices,
) -> GraphColoredVertices {
// flip(set & flip(set) & can_apply_function)
let symbolic_var = self.symbolic_context.state_variables[variable.0];
let output = Bdd::fused_ternary_flip_op(
(&initial.bdd, None),
(&initial.bdd, Some(symbolic_var)),
(&self.update_functions[variable.0], None),
Some(symbolic_var),
|a, b, c| {
// a & b & c
match (a, b, c) {
(Some(false), _, _) => Some(false),
(_, Some(false), _) => Some(false),
(_, _, Some(false)) => Some(false),
(Some(true), Some(true), Some(true)) => Some(true),
_ => None,
}
},
);
GraphColoredVertices::new(output, &self.symbolic_context)
}
/// Compute the `GraphColoredVertices` representing the predecessors of the vertices in the
/// given `initial` set that are reached by updating the given `variable`, but are *within*
/// the initial set. Formally:
///
/// $$
/// \texttt{VarPreWithin}(v, X) = \\{~(x, c) \mid (x, c) \in X \land \exists y.~(y, c) \in X \land x \xrightarrow{v}_{c} y~\\}
/// $$
pub fn var_pre_within(
&self,
variable: VariableId,
initial: &GraphColoredVertices,
) -> GraphColoredVertices {
// set & flip(set) & can_apply_function
let symbolic_var = self.symbolic_context.state_variables[variable.0];
let output = Bdd::fused_ternary_flip_op(
(&initial.bdd, None),
(&initial.bdd, Some(symbolic_var)),
(&self.update_functions[variable.0], None),
None,
|a, b, c| {
// a & b & c
match (a, b, c) {
(Some(false), _, _) => Some(false),
(_, Some(false), _) => Some(false),
(_, _, Some(false)) => Some(false),
(Some(true), Some(true), Some(true)) => Some(true),
_ => None,
}
},
);
GraphColoredVertices::new(output, &self.symbolic_context)
}
/// Compute the `GraphColoredVertices` representing the subset of the `initial` set for which
/// there exists an outgoing transition using the given `variable`. Formally:
///
/// $$
/// \texttt{VarCanPost}(v, X) = \\{~ (x, c) \in X \mid \exists y.~ x \xrightarrow{v}_{c} y ~\\}
/// $$
pub fn var_can_post(
&self,
variable: VariableId,
initial: &GraphColoredVertices,
) -> GraphColoredVertices {
// set & can_apply_function
GraphColoredVertices::new(
initial.bdd.and(&self.update_functions[variable.0]),
&self.symbolic_context,
)
}
/// Compute the `GraphColoredVertices` representing the subset of the `initial` set for which
/// there exists an incoming transition using the given `variable`. Formally:
///
/// $$
/// \texttt{VarCanPre}(v, X) = \\{~ (y, c) \in X \mid \exists x.~ x \xrightarrow{v}_{c} y ~\\}
/// $$
pub fn var_can_pre(
&self,
variable: VariableId,
initial: &GraphColoredVertices,
) -> GraphColoredVertices {
// flip(flip(set) & can_apply_function)
let symbolic_var = self.symbolic_context.state_variables[variable.0];
let output = Bdd::fused_binary_flip_op(
(&initial.bdd, Some(symbolic_var)),
(&self.update_functions[variable.0], None),
Some(symbolic_var),
biodivine_lib_bdd::op_function::and,
);
GraphColoredVertices::new(output, &self.symbolic_context)
}
/// Compute the `GraphColoredVertices` representing the subset of the `initial` set for which
/// there exists an outgoing transition using the given `variable` that leads *outside* of the
/// `initial` set. Formally:
///
/// $$
/// \texttt{VarCanPostOut}(v, X) = \\{~ (x, c) \in X \mid \exists y.~(y, c) \notin X \land x \xrightarrow{v}_{c} y ~\\}
/// $$
pub fn var_can_post_out(
&self,
variable: VariableId,
initial: &GraphColoredVertices,
) -> GraphColoredVertices {
// set & !flip(set) & can_apply_function
let symbolic_var = self.symbolic_context.state_variables[variable.0];
let output = Bdd::fused_ternary_flip_op(
(&initial.bdd, None),
(&initial.bdd, Some(symbolic_var)),
(&self.update_functions[variable.0], None),
None,
|a, b, c| {
// a & !b & c
match (a, b, c) {
(Some(false), _, _) => Some(false),
(_, Some(true), _) => Some(false),
(_, _, Some(false)) => Some(false),
(Some(true), Some(false), Some(true)) => Some(true),
_ => None,
}
},
);
GraphColoredVertices::new(output, &self.symbolic_context)
}
/// Compute the `GraphColoredVertices` representing the subset of the `initial` set for which
/// there exists an incoming transition using the given `variable` that originates *outside* of the
/// `initial` set. Formally:
///
/// $$
/// \texttt{VarCanPreOut}(v, X) = \\{~ (y, c) \in X \mid \exists x.~(x, c) \notin X \land x \xrightarrow{v}_{c} y ~\\}
/// $$
pub fn var_can_pre_out(
&self,
variable: VariableId,
initial: &GraphColoredVertices,
) -> GraphColoredVertices {
// flip(!set & flip(set) & can_apply_function)
let symbolic_var = self.symbolic_context.state_variables[variable.0];
let output = Bdd::fused_ternary_flip_op(
(&initial.bdd, None),
(&initial.bdd, Some(symbolic_var)),
(&self.update_functions[variable.0], None),
Some(symbolic_var),
|a, b, c| {
// !a & b & c
match (a, b, c) {
(Some(true), _, _) => Some(false),
(_, Some(false), _) => Some(false),
(_, _, Some(false)) => Some(false),
(Some(false), Some(true), Some(true)) => Some(true),
_ => None,
}
},
);
GraphColoredVertices::new(output, &self.symbolic_context)
}
/// Compute the `GraphColoredVertices` representing the subset of the `initial` set for which
/// there exists an outgoing transition using the given `variable` that leads *into* the
/// `initial` set. Formally:
///
/// $$
/// \texttt{VarCanPostWithin}(v, X) = \\{~ (x, c) \in X \mid \exists y.~(y, c) \in X \land x \xrightarrow{v}_{c} y ~\\}
/// $$
pub fn var_can_post_within(
&self,
variable: VariableId,
initial: &GraphColoredVertices,
) -> GraphColoredVertices {
// set & flip(set) & can_apply_function
let symbolic_var = self.symbolic_context.state_variables[variable.0];
let output = Bdd::fused_ternary_flip_op(
(&initial.bdd, None),
(&initial.bdd, Some(symbolic_var)),
(&self.update_functions[variable.0], None),
None,
|a, b, c| {
// a & b & c
match (a, b, c) {
(Some(false), _, _) => Some(false),
(_, Some(false), _) => Some(false),
(_, _, Some(false)) => Some(false),
(Some(true), Some(true), Some(true)) => Some(true),
_ => None,
}
},
);
GraphColoredVertices::new(output, &self.symbolic_context)
}
/// Compute the `GraphColoredVertices` representing the subset of the `initial` set for which
/// there exists an incoming transition using the given `variable` that originates *inside* the
/// `initial` set. Formally:
///
/// $$
/// \texttt{VarCanPreWithin}(v, X) = \\{~ (y, c) \in X \mid \exists x.~(x, c) \in X \land x \xrightarrow{v}_{c} y ~\\}
/// $$
pub fn var_can_pre_within(
&self,
variable: VariableId,
initial: &GraphColoredVertices,
) -> GraphColoredVertices {
// flip(set & flip(set) & can_post_function)
let symbolic_var = self.symbolic_context.state_variables[variable.0];
let output = Bdd::fused_ternary_flip_op(
(&initial.bdd, None),
(&initial.bdd, Some(symbolic_var)),
(&self.update_functions[variable.0], None),
Some(symbolic_var),
|a, b, c| {
// a & b & c
match (a, b, c) {
(Some(false), _, _) => Some(false),
(_, Some(false), _) => Some(false),
(_, _, Some(false)) => Some(false),
(Some(true), Some(true), Some(true)) => Some(true),
_ => None,
}
},
);
GraphColoredVertices::new(output, &self.symbolic_context)
}
}
/// Here, we give several operators derived from the variable-specific operators. These have
/// complexity `O(|vars|)` since they are internally represented using the variable-specific
/// operators.
///
/// We provide the following functions:
/// - `post` / `pre`: General successors and predecessors functions.
/// - `can_post` / `can_pre`: Subsets of the initial states that have some successors
/// or predecessors.
/// - `can_post_within` / `can_pre_within`: Subsets of initial states that have some successors
/// / predecessors within the initial set.
/// - `will_post_within` / `will_pre_within`: Subsets of initial states that have all successors
/// / predecessors withing the initial set.
/// - `can_post_out` / `can_pre_out`: Subsets of initial states that have some successors
/// / predecessors outside of the initial set.
/// - `will_post_out` / `will_pre_out`: Subsets of initial states that have all successors
/// / predecessors outside of the initial set.
///
impl SymbolicAsyncGraph {
/// Compute the result of applying `post` with *all* update functions to the `initial` set.
pub fn post(&self, initial: &GraphColoredVertices) -> GraphColoredVertices {
self.network
.variables()
.fold(self.mk_empty_vertices(), |r, v| {
r.union(&self.var_post(v, initial))
})
}
/// Compute the result of applying `pre` with *all* update functions to the `initial` set.
pub fn pre(&self, initial: &GraphColoredVertices) -> GraphColoredVertices {
self.network
.variables()
.fold(self.mk_empty_vertices(), |r, v| {
r.union(&self.var_pre(v, initial))
})
}
/// Compute the subset of `set` that can perform *some* `post` operation.
pub fn can_post(&self, set: &GraphColoredVertices) -> GraphColoredVertices {
self.network
.variables()
.fold(self.mk_empty_vertices(), |r, v| {
r.union(&self.var_can_post(v, set))
})
}
/// Compute the subset of `set` that can perform *some* `pre` operation.
pub fn can_pre(&self, set: &GraphColoredVertices) -> GraphColoredVertices {
self.network
.variables()
.fold(self.mk_empty_vertices(), |r, v| {
r.union(&self.var_can_pre(v, set))
})
}
/// Compute the subset of `set` that can perform *some* `post` operation which leads
/// to a state within `set`.
pub fn can_post_within(&self, set: &GraphColoredVertices) -> GraphColoredVertices {
self.network
.variables()
.fold(self.mk_empty_vertices(), |r, v| {
r.union(&self.var_can_post_within(v, set))
})
}
/// Compute the subset of `set` such that *every admissible* `post` operation leads to a state
/// within the same `set`.
///
/// Note that this also includes states which cannot perform any `post` operation.
pub fn will_post_within(&self, set: &GraphColoredVertices) -> GraphColoredVertices {
self.network
.variables()
.fold(set.clone(), |r, v| r.minus(&self.var_can_post_out(v, set)))
}
/// Compute the subset of `set` that can perform *some* `pre` operation which leads
/// to a state within `set`.
pub fn can_pre_within(&self, set: &GraphColoredVertices) -> GraphColoredVertices {
self.network
.variables()
.fold(self.mk_empty_vertices(), |r, v| {
r.union(&self.var_can_pre_within(v, set))
})
}
/// Compute the subset of `set` such that *every admissible* `pre` operation leads to a state
/// within the same `set`.
///
/// Note that this also includes states which cannot perform any `pre` operation.
pub fn will_pre_within(&self, set: &GraphColoredVertices) -> GraphColoredVertices {
self.network
.variables()
.fold(set.clone(), |r, v| r.minus(&self.var_can_pre_out(v, set)))
}
/// Compute the subset of `set` that can perform *some* `post` operation which leads
/// to a state outside of `set`.
pub fn can_post_out(&self, set: &GraphColoredVertices) -> GraphColoredVertices {
self.network
.variables()
.fold(self.mk_empty_vertices(), |r, v| {
r.union(&self.var_can_post_out(v, set))
})
}
/// Compute the subset of `set` such that *every admissible* `post` operation leads
/// to a state outside the `set`.
///
/// Note that this also includes states which cannot perform any `post` operation.
pub fn will_post_out(&self, set: &GraphColoredVertices) -> GraphColoredVertices {
self.network.variables().fold(set.clone(), |r, v| {
r.minus(&self.var_can_post_within(v, set))
})
}
/// Compute the subset of `set` that can perform *some* `pre` operation which leads
/// to a state outside of `set`.
pub fn can_pre_out(&self, set: &GraphColoredVertices) -> GraphColoredVertices {
self.network
.variables()
.fold(self.mk_empty_vertices(), |r, v| {
r.union(&self.var_can_pre_out(v, set))
})
}
/// Compute the subset of `set` such that *every admissible* `pre` operation leads
/// to a state outside of `set`.
///
/// Note that this also includes states which cannot perform any `pre` operation.
pub fn will_pre_out(&self, set: &GraphColoredVertices) -> GraphColoredVertices {
self.network.variables().fold(set.clone(), |r, v| {
r.minus(&self.var_can_pre_within(v, set))
})
}
}
#[cfg(test)]
mod tests {
/* Basically a copy from of example from tutorial, but tutorials don't count towards coverage. */
use crate::symbolic_async_graph::SymbolicAsyncGraph;
use crate::BooleanNetwork;
use std::convert::TryFrom;
#[test]
fn basic_graph_test() {
let bn = BooleanNetwork::try_from(
r"
A -> B
C -|? B
$B: A
C -> A
B -> A
A -| A
$A: C | f(A, B)
",
)
.unwrap();
let stg = SymbolicAsyncGraph::new(bn).unwrap();
let id_b = stg.as_network().as_graph().find_variable("B").unwrap();
let b_is_true = stg.fix_network_variable(id_b, true);
let b_is_false = stg.fix_network_variable(id_b, false);
assert_eq!(
stg.var_can_pre(id_b, &b_is_true),
stg.var_post(id_b, &b_is_false)
);
assert_eq!(
stg.var_can_post(id_b, &b_is_false),
stg.var_pre(id_b, &b_is_true)
);
assert_eq!(4.0, stg.can_pre(&b_is_true).vertices().approx_cardinality());
assert_eq!(
4.0,
stg.can_post(&b_is_false).vertices().approx_cardinality()
);
let some_color = stg.unit_colors().pick_singleton();
let b_is_true_with_color = b_is_true.intersect_colors(&some_color);
let b_is_false_with_color = b_is_false.intersect_colors(&some_color);
assert_eq!(
3.0,
stg.can_pre(&b_is_true_with_color)
.vertices()
.approx_cardinality()
);
assert_eq!(
4.0,
stg.can_post(&b_is_false_with_color)
.vertices()
.approx_cardinality()
);
assert_ne!(stg.can_pre(&b_is_true), stg.post(&b_is_false));
assert_ne!(stg.can_post(&b_is_false), stg.pre(&b_is_true));
}
}