From abddf5de71938398009c32df36536c1564ccae27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Paulev=C3=A9?= Date: Fri, 27 Aug 2021 00:41:36 +0200 Subject: [PATCH] support for clingo 5.5 --- caspo/control.py | 2 +- caspo/core/clamping.py | 4 ++-- caspo/core/dataset.py | 3 ++- caspo/core/hypergraph.py | 8 +++++--- caspo/core/logicalnetwork.py | 11 +++++++---- caspo/core/setup.py | 6 +++--- caspo/design.py | 18 ++++++++++-------- caspo/learn.py | 2 +- recipe/meta.yaml | 2 +- requirements.txt | 1 + 10 files changed, 33 insertions(+), 24 deletions(-) diff --git a/caspo/control.py b/caspo/control.py index a596cf8..d607c60 100644 --- a/caspo/control.py +++ b/caspo/control.py @@ -114,7 +114,7 @@ def __init__(self, networks, scenarios): fs = networks.to_funset().union(scenarios.to_funset()) for v in (n for n in networks.hg.nodes if n not in scenarios.exclude): - fs.add(clingo.Function("candidate", [v])) + fs.add(clingo.Function("candidate", [clingo.String(v)])) self.instance = ". ".join(map(str, fs)) + ". #show intervention/2." diff --git a/caspo/core/clamping.py b/caspo/core/clamping.py index 1c9cbc6..10f8c89 100644 --- a/caspo/core/clamping.py +++ b/caspo/core/clamping.py @@ -53,7 +53,7 @@ def to_funset(self, lname="clamping", cname="clamped"): """ fs = set() for i, clamping in enumerate(self): - fs.add(clingo.Function(lname, [i])) + fs.add(clingo.Function(lname, [clingo.Number(i)])) fs = fs.union(clamping.to_funset(i, cname)) return fs @@ -368,7 +368,7 @@ def to_funset(self, index, name="clamped"): """ fs = set() for var, sign in self: - fs.add(clingo.Function(name, [index, var, sign])) + fs.add(clingo.Function(name, [clingo.Number(index), clingo.String(var), clingo.Number(sign)])) return fs diff --git a/caspo/core/dataset.py b/caspo/core/dataset.py index 95b07c6..3500076 100644 --- a/caspo/core/dataset.py +++ b/caspo/core/dataset.py @@ -159,6 +159,7 @@ def to_funset(self, discrete): for i, row in self.readouts.iterrows(): for var, val in row.items(): if not np.isnan(val): - fs.add(clingo.Function('obs', [i, var, discrete(val)])) + fs.add(clingo.Function('obs', [clingo.Number(i), + clingo.String(var), clingo.Number(discrete(val))])) return fs diff --git a/caspo/core/hypergraph.py b/caspo/core/hypergraph.py index 1b7156d..ea9aee1 100644 --- a/caspo/core/hypergraph.py +++ b/caspo/core/hypergraph.py @@ -174,12 +174,14 @@ def to_funset(self): """ fs = set() for i, n in self.nodes.items(): - fs.add(clingo.Function('node', [n, i])) + fs.add(clingo.Function('node', [clingo.String(n), clingo.Number(i)])) for j, i in self.hyper.items(): - fs.add(clingo.Function('hyper', [i, j, len(self.edges[self.edges.hyper_idx == j])])) + fs.add(clingo.Function('hyper', [clingo.Number(i), clingo.Number(j), + clingo.Number(len(self.edges[self.edges.hyper_idx == j]))])) for j, v, s in self.edges.itertuples(index=False): - fs.add(clingo.Function('edge', [j, v, s])) + fs.add(clingo.Function('edge', [clingo.Number(j), clingo.String(v), + clingo.Number(s)])) return fs diff --git a/caspo/core/logicalnetwork.py b/caspo/core/logicalnetwork.py index 2cdb921..730e979 100644 --- a/caspo/core/logicalnetwork.py +++ b/caspo/core/logicalnetwork.py @@ -287,7 +287,7 @@ def to_funset(self): .. _clingo.Function: https://potassco.github.io/clingo/python-api/current/clingo.html#-Function """ - fs = set((clingo.Function("variable", [var]) for var in self.hg.nodes)) + fs = set((clingo.Function("variable", [clingo.String(var)]) for var in self.hg.nodes)) formulas = set() for network in self: @@ -297,15 +297,18 @@ def to_funset(self): for i, network in enumerate(self): for v, f in network.formulas_iter(): - fs.add(clingo.Function("formula", [i, v, int(formulas[formulas == f].index[0])])) + fs.add(clingo.Function("formula", [clingo.Number(i), + clingo.String(v), clingo.Number(int(formulas[formulas == f].index[0]))])) for formula_idx, formula in formulas.items(): for clause in formula: clause_idx = self.hg.clauses_idx[clause] - fs.add(clingo.Function("dnf", [formula_idx, clause_idx])) + fs.add(clingo.Function("dnf", [clingo.Number(formula_idx), + clingo.Number(clause_idx)])) for variable, sign in clause: - fs.add(clingo.Function("clause", [clause_idx, variable, sign])) + fs.add(clingo.Function("clause", [clingo.Number(clause_idx), + clingo.String(variable), clingo.Number(sign)])) return fs diff --git a/caspo/core/setup.py b/caspo/core/setup.py index 586508a..7ad6a4f 100644 --- a/caspo/core/setup.py +++ b/caspo/core/setup.py @@ -101,9 +101,9 @@ def to_funset(self): .. _clingo.Function: https://potassco.github.io/clingo/python-api/current/clingo.html#-Function """ - fs = set((clingo.Function('stimulus', [str(var)]) for var in self.stimuli)) - fs = fs.union((clingo.Function('inhibitor', [str(var)]) for var in self.inhibitors)) - fs = fs.union((clingo.Function('readout', [str(var)]) for var in self.readouts)) + fs = set((clingo.Function('stimulus', [clingo.String(str(var))]) for var in self.stimuli)) + fs = fs.union((clingo.Function('inhibitor', [clingo.String(str(var))]) for var in self.inhibitors)) + fs = fs.union((clingo.Function('readout', [clingo.String(str(var))]) for var in self.readouts)) return fs diff --git a/caspo/design.py b/caspo/design.py index 1a243b6..b0ab416 100644 --- a/caspo/design.py +++ b/caspo/design.py @@ -59,9 +59,9 @@ def __init__(self, networks, setup, candidates=None): fs = networks.to_funset().union(setup.to_funset()) if candidates: fs = fs.union(self.candidates.to_funset("listing", "listed")) - fs.add(clingo.Function("mode", [2])) + fs.add(clingo.Function("mode", [clingo.Number(2)])) else: - fs.add(clingo.Function("mode", [1])) + fs.add(clingo.Function("mode", [clingo.Number(1)])) self.instance = ". ".join(map(str, fs)) + ". #show clamped/3." @@ -143,8 +143,8 @@ def design(self, max_stimuli=-1, max_inhibitors=-1, max_experiments=10, relax=Fa solver.ground([("base", [])]) if relax: - parts = [("step", [step]) for step in range(1, max_experiments+1)] - parts.append(("diff", [max_experiments + 1])) + parts = [("step", [clingo.Number(step)]) for step in range(1, max_experiments+1)] + parts.append(("diff", [clingo.Number(max_experiments + 1)])) solver.ground(parts) solver.solve(on_model=self.__save__) @@ -152,14 +152,16 @@ def design(self, max_stimuli=-1, max_inhibitors=-1, max_experiments=10, relax=Fa step, sat = 0, False while step <= max_experiments and not sat: parts = [] - parts.append(("check", [step])) + parts.append(("check", [clingo.Number(step)])) if step > 0: - solver.release_external(clingo.Function("query", [step-1])) - parts.append(("step", [step])) + solver.release_external(clingo.Function("query", + [clingo.Number(step-1)])) + parts.append(("step", [clingo.Number(step)])) solver.cleanup() solver.ground(parts) - solver.assign_external(clingo.Function("query", [step]), True) + solver.assign_external(clingo.Function("query", + [clingo.Number(step)]), True) sat, step = solver.solve(on_model=self.__save__).satisfiable, step + 1 self.stats['time_optimum'] = solver.statistics['summary']['times']['solve'] diff --git a/caspo/learn.py b/caspo/learn.py index fdca767..a043e34 100644 --- a/caspo/learn.py +++ b/caspo/learn.py @@ -75,7 +75,7 @@ def __init__(self, graph, dataset, length, discrete, factor): self.hypergraph = core.HyperGraph.from_graph(self.graph, length) fs = self.dataset.to_funset(self.discrete).union(self.hypergraph.to_funset()) - fs.add(clingo.Function('dfactor', [self.factor])) + fs.add(clingo.Function('dfactor', [clingo.Number(self.factor)])) self.instance = ". ".join(map(str, fs)) + ". #show dnf/2." self.optimum = None diff --git a/recipe/meta.yaml b/recipe/meta.yaml index 226adec..d2f8387 100644 --- a/recipe/meta.yaml +++ b/recipe/meta.yaml @@ -28,7 +28,7 @@ requirements: - pyparsing - pydot - joblib - - clingo 5* + - clingo 5.5.* test: commands: diff --git a/requirements.txt b/requirements.txt index 045f662..f0c4d9c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,3 +8,4 @@ pyparsing scikit-learn scipy seaborn +clingo