Skip to content

Commit

Permalink
hypercube(): add {min,max}_dimension constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
pauleve committed Mar 23, 2023
1 parent 385a991 commit c2d36ab
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
12 changes: 12 additions & 0 deletions bonesis/asp_encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,18 @@ def encode_trapspace(self, cfg, mutant=None):
] + self.apply_mutant_to_mcfg(mutant, myts)
return rules

def encode_hypercube(self, h):
self.load_template_hypercube()
H = clingo_encode(h.name)
rules = [f"hypercube({H})" ]
if h.min_dimension >= 1:
rules.append(f":- #count {{ N: hypercube({H},N,2) }}"
f"{h.min_dimension-1}")
if h.max_dimension:
rules.append(f":- {max_dimension+1} #count {{ N: hypercube({H},N,2) }}")
print(rules)
return rules

def encode_in_attractor(self, cfg, mutant=None):
self.load_template_eval()

Expand Down
9 changes: 8 additions & 1 deletion bonesis/language.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,14 @@ def __str__(self):
__language_api__["obs"] = ObservationVar

class HypercubeVar(BonesisVar):
def __init__(self, obs=None):
def __init__(self, obs=None,
min_dimension=0,
max_dimension=None,
dimension=None):
if dimension is not None:
min_dimension = max_dimension = dimension
self.min_dimension = min_dimension
self.max_dimension = max_dimension
if isinstance(obs, dict):
obs = self.iface.obs(obs)
self.obs = obs
Expand Down
2 changes: 1 addition & 1 deletion bonesis/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def register_configuration(self, cfg):
def register_hypercube(self, h):
name = f"_h{len(self.hypercubes)}"
h.name = name
self.push_term("hypercube", name)
self.push_term("hypercube", h)
if h.obs:
self.register_predicate("bind_hypercube", name, h.obs.name)

Expand Down
19 changes: 19 additions & 0 deletions tests/test_fixed.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def setUp(self):
"000": {"a": 0, "b": 0, "c": 0},
"111": {"a": 1, "b": 1, "c": 1},
}
self.dom2 = bonesis.InfluenceGraph.complete("abc", -1)

def test_fixpoints(self):
bo = bonesis.BoNesis(self.dom1, self.data1)
Expand All @@ -19,3 +20,21 @@ def test_trapspace(self):
bo = bonesis.BoNesis(self.dom1, self.data1)
bo.fixed(bo.obs("000"))
self.assertEqual(bo.boolean_networks().count(), 6859)

def test_mintrap(self):
bo = bonesis.BoNesis(self.dom1)
h = bo.hypercube(min_dimension=1)
bo.fixed(h)
self.assertEqual(len(list(h.assignments(limit=1))), 0)

bo = bonesis.BoNesis(self.dom2)
h = bo.hypercube(min_dimension=2)
bo.fixed(h)
val = next(iter(h.assignments()))
self.assertGreaterEqual(sum((1 for v in val.values() if v == '*')), 2)

bo = bonesis.BoNesis(self.dom2)
h = bo.hypercube(max_dimension=0)
bo.fixed(h)
val = next(iter(h.assignments()))
self.assertEqual(sum((1 for v in val.values() if v == '*')), 0)

0 comments on commit c2d36ab

Please sign in to comment.