Skip to content

Commit bef123b

Browse files
committed
Add nicer methods for creating sets of super-spaces or sub-spaces.
1 parent 5147d63 commit bef123b

File tree

2 files changed

+84
-6
lines changed

2 files changed

+84
-6
lines changed

src/trap_spaces/_impl_network_colored_spaces.rs

+46-4
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,20 @@ impl NetworkColoredSpaces {
116116
pub fn to_colored_vertices(&self, ctx: &SymbolicSpaceContext) -> GraphColoredVertices {
117117
GraphColoredVertices::new(ctx.spaces_to_vertices(self.as_bdd()), ctx.inner_context())
118118
}
119+
120+
/// Produce a set of spaces that is a superset of this set, and in addition contains
121+
/// all spaces that are a super-space of *some* item in this set.
122+
pub fn with_all_super_spaces(&self, ctx: &SymbolicSpaceContext) -> NetworkColoredSpaces {
123+
let super_bdd = ctx.mk_super_spaces(self.as_bdd());
124+
self.copy(super_bdd)
125+
}
126+
127+
/// Produce a set of spaces that is a superset of this set, and in addition contains
128+
/// all spaces that are a subspace of *some* item in this set.
129+
pub fn with_all_sub_spaces(&self, ctx: &SymbolicSpaceContext) -> NetworkColoredSpaces {
130+
let sub_bdd = ctx.mk_sub_spaces(self.as_bdd());
131+
self.copy(sub_bdd)
132+
}
119133
}
120134

121135
/// Relation operations.
@@ -213,8 +227,8 @@ impl BddSet for NetworkColoredSpaces {
213227
mod tests {
214228
use crate::biodivine_std::traits::Set;
215229
use crate::symbolic_async_graph::SymbolicAsyncGraph;
216-
use crate::trap_spaces::SymbolicSpaceContext;
217-
use crate::BooleanNetwork;
230+
use crate::trap_spaces::{NetworkColoredSpaces, SymbolicSpaceContext};
231+
use crate::{BooleanNetwork, ExtendedBoolean, Space};
218232
use num_bigint::BigInt;
219233
use num_traits::One;
220234

@@ -236,12 +250,40 @@ mod tests {
236250
assert!(singleton_color.is_singleton());
237251
assert!(singleton_space.is_singleton());
238252
assert!(!unit.intersect_colors(&singleton_color).is_singleton());
239-
// There is only one color, hence this holds. Otherwise this should not hold.
253+
// There is only one color, hence this holds. Otherwise, this should not hold.
240254
assert!(unit.intersect_spaces(&singleton_space).is_singleton());
241255
assert!(unit.minus_colors(&singleton_color).is_empty());
242256
assert!(unit.minus_spaces(&singleton_space).is_subset(&unit));
243257

244-
// There are 28 network variables and we are eliminating 22 of them, so 6 should be left.
258+
let mut space = Space::new(&bn);
259+
for var in bn.variables() {
260+
space[var] = ExtendedBoolean::Zero;
261+
}
262+
263+
let all_zero = NetworkColoredSpaces::new(ctx.mk_space(&space), &ctx)
264+
.intersect_colors(stg.unit_colors());
265+
266+
// 2^28, i.e. the number of variables, since each variable can
267+
// be either fixed to zero or free.
268+
let unit_colors = stg.unit_colors();
269+
assert_eq!(
270+
unit_colors.approx_cardinality(),
271+
all_zero.with_all_sub_spaces(&ctx).approx_cardinality()
272+
);
273+
assert_eq!(
274+
268435456.0 * unit_colors.approx_cardinality(),
275+
all_zero.with_all_super_spaces(&ctx).approx_cardinality()
276+
);
277+
278+
// Adding all super-spaces will always add *^n, which has all spaces as sub-spaces.
279+
assert_eq!(
280+
ctx.mk_unit_colored_spaces(&stg),
281+
all_zero
282+
.with_all_super_spaces(&ctx)
283+
.with_all_sub_spaces(&ctx)
284+
);
285+
286+
// There are 28 network variables, and we are eliminating 22 of them, so 6 should be left.
245287
let dual_vars = ctx.inner_context().all_extra_state_variables();
246288
let project = unit.raw_projection(&dual_vars[0..44]);
247289
assert_eq!(project.iter().count(), 3_usize.pow(6));

src/trap_spaces/_impl_network_spaces.rs

+38-2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ impl NetworkSpaces {
119119
pub fn to_vertices(&self, ctx: &SymbolicSpaceContext) -> GraphVertices {
120120
GraphVertices::new(ctx.spaces_to_vertices(self.as_bdd()), ctx.inner_context())
121121
}
122+
123+
/// Produce a set of spaces that is a superset of this set, and in addition contains
124+
/// all spaces that are a super-space of *some* item in this set.
125+
pub fn with_all_super_spaces(&self, ctx: &SymbolicSpaceContext) -> NetworkSpaces {
126+
let super_bdd = ctx.mk_super_spaces(self.as_bdd());
127+
self.copy(super_bdd)
128+
}
129+
130+
/// Produce a set of spaces that is a superset of this set, and in addition contains
131+
/// all spaces that are a subspace of *some* item in this set.
132+
pub fn with_all_sub_spaces(&self, ctx: &SymbolicSpaceContext) -> NetworkSpaces {
133+
let sub_bdd = ctx.mk_sub_spaces(self.as_bdd());
134+
self.copy(sub_bdd)
135+
}
122136
}
123137

124138
impl BddSet for NetworkSpaces {
@@ -167,8 +181,8 @@ impl Iterator for SpaceIterator {
167181

168182
#[cfg(test)]
169183
mod tests {
170-
use crate::trap_spaces::SymbolicSpaceContext;
171-
use crate::BooleanNetwork;
184+
use crate::trap_spaces::{NetworkSpaces, SymbolicSpaceContext};
185+
use crate::{BooleanNetwork, ExtendedBoolean, Space};
172186
use num_bigint::BigInt;
173187
use num_traits::One;
174188

@@ -186,6 +200,28 @@ mod tests {
186200
assert_eq!(BigInt::one(), singleton.exact_cardinality());
187201
assert_eq!(1, singleton.iter().count());
188202

203+
let mut space = Space::new(&bn);
204+
for var in bn.variables() {
205+
space[var] = ExtendedBoolean::Zero;
206+
}
207+
208+
let all_zero = NetworkSpaces::new(ctx.mk_space(&space), &ctx);
209+
// 2^28, i.e. the number of variables, since each variable can
210+
// be either fixed to zero or free.
211+
assert_eq!(1.0, all_zero.with_all_sub_spaces(&ctx).approx_cardinality());
212+
assert_eq!(
213+
268435456.0,
214+
all_zero.with_all_super_spaces(&ctx).approx_cardinality()
215+
);
216+
217+
// Adding all super-spaces will always add *^n, which has all spaces as sub-spaces.
218+
assert_eq!(
219+
ctx.mk_unit_spaces(),
220+
all_zero
221+
.with_all_super_spaces(&ctx)
222+
.with_all_sub_spaces(&ctx)
223+
);
224+
189225
// There are 28 network variables and we are eliminating 22 of them, so 6 should be left.
190226
let dual_vars = ctx.inner_context().all_extra_state_variables();
191227
let project = unit.raw_projection(&dual_vars[0..44]);

0 commit comments

Comments
 (0)