Skip to content

Commit

Permalink
support for clingo 5.5
Browse files Browse the repository at this point in the history
  • Loading branch information
pauleve committed Aug 26, 2021
1 parent 3d7f9f8 commit abddf5d
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion caspo/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."

Expand Down
4 changes: 2 additions & 2 deletions caspo/core/clamping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion caspo/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 5 additions & 3 deletions caspo/core/hypergraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 7 additions & 4 deletions caspo/core/logicalnetwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions caspo/core/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 10 additions & 8 deletions caspo/design.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."

Expand Down Expand Up @@ -143,23 +143,25 @@ 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__)
else:
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']
Expand Down
2 changes: 1 addition & 1 deletion caspo/learn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ requirements:
- pyparsing
- pydot
- joblib
- clingo 5*
- clingo 5.5.*

test:
commands:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pyparsing
scikit-learn
scipy
seaborn
clingo

0 comments on commit abddf5d

Please sign in to comment.