Skip to content

Commit

Permalink
relaxer all-in-one function, combine Control2
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanzwicknagl committed Apr 10, 2023
1 parent 13b846c commit c1fd287
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 76 deletions.
2 changes: 1 addition & 1 deletion backend/src/viasp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
mark_from_clingo_model, mark_from_file, mark_from_string,
relax_constraints, show, unmark_from_clingo_model,
unmark_from_file, unmark_from_string, register_transformer)
from .wrapper import Control, Control2
from .wrapper import Control
11 changes: 10 additions & 1 deletion backend/src/viasp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,16 @@ def relax_constraints(*args, **kwargs) -> str:
r"""
Relax constraints in the marked models.
:param kwargs:
* *head_name* (``str``) --
default="unsat", name of head literal
* *collect_variables* (``bool``) --
default=True, collect variables from
body as a tuple in the head literal
* *viasp_backend_url* (``str``) --
url of the viasp backend
* *_viasp_client* (``ClingoClient``) --
a viasp client object
"""
connector = _get_connector()
return connector.relax_constraints(*args, **kwargs)
Expand Down
81 changes: 34 additions & 47 deletions backend/src/viasp/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,43 @@ def clear(self):
def register_function_call(self, name, sig, args, kwargs):
self._database.register_function_call(name, sig, args, kwargs)

def relax_constraints(self, *args, **kwargs):
r"""This method relaxes integrity constraints and returns a modified program.
def get_relaxed_program(self, head_name:str = "unsat", collect_variables:bool = True) -> str:
r"""This method relaxes integrity constraints and returns
the relaxed program as a string.
Parameters
----------
**kwargs:
Valid keyword arguments are:
:param head_name: ``str``
default="unsat" (name of the head literal)
:param collect_variables: ``bool``
default=True (collect variables from body as a tuple in the head literal)
"""
kwargs = {"head_name": head_name, "collect_variables": collect_variables}
return self._database.get_relaxed_program(**kwargs)

def relax_constraints(self, head_name:str = "unsat", collect_variables:bool = True):
r"""This method relaxes integrity constraints and returns
a new viaspControl object with the relaxed program loaded
and stable models marked.
head_name: str
default="unsat" (name of the head literal)
collect_variables: bool
default=True (collect variables from body as a tuple in the head literal)
Parameters
----------
:param head_name: ``str``
default="unsat" (name of the head literal)
:param collect_variables: ``bool``
default=True (collect variables from body as a tuple in the head literal)
"""
return self._database.relax_constraints(*args, **kwargs)
self.show()
kwargs = {"head_name": head_name, "collect_variables": collect_variables}

relaxed_prg = self._database.relax_constraints(**kwargs)
ctl = Control()
ctl.add("base", [], relaxed_prg)
ctl.ground([("base", [])])
with ctl.solve(yield_=True) as handle:
for m in handle:
ctl.viasp.mark(m)
return ctl


def clingraph(self, viz_encoding, engine="dot"):
Expand All @@ -69,44 +93,7 @@ def register_transformer(self, transformer, imports="", path=""):
self._database._register_transformer(transformer, imports, path)



class Control(InnerControl):

def __init__(self, *args, **kwargs):
self.viasp = ShowConnector(**kwargs)
if "_viasp_client" in kwargs:
del kwargs["_viasp_client"]
if "viasp_backend_url" in kwargs:
del kwargs["viasp_backend_url"]
self.viasp.register_function_call("__init__", signature(super().__init__), args, kwargs)
super().__init__(*args, **kwargs)

def load(self, path: str) -> None:
if path == "-":
path = STDIN_TMP_STORAGE_PATH
tmp = sys.stdin.readlines()
with open(path, "w", encoding="utf-8") as f:
f.writelines(tmp)
else:
shutil.copy(path, STDIN_TMP_STORAGE_PATH)
path = STDIN_TMP_STORAGE_PATH
self.viasp.register_function_call("load", signature(self.load), [], kwargs={"path": path})
super().load(path=str(path))

def __getattribute__(self, name):
attr = InnerControl.__getattribute__(self, name)
if is_non_cython_function_call(attr) and name != "load":
def wrapper_func(*args, **kwargs):
self.viasp.register_function_call(attr.__name__, signature(attr), args, kwargs)
result = attr(*args, **kwargs)
return result

return wrapper_func
else:
return attr


class Control2:
class Control:


def __init__(self, *args, **kwargs):
Expand Down
6 changes: 3 additions & 3 deletions backend/test/test_clingraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@


def test_instanciations():
_ = wrapper.Control2()
_ = wrapper.Control2(["0"])
_ = wrapper.Control()
_ = wrapper.Control(["0"])


class DebugClient(ViaspClient):
Expand All @@ -38,7 +38,7 @@ def __init__(self, internal_client: FlaskClient):

def test_load_from_stdin(client):
debug_client = DebugClient(client)
ctl = wrapper.Control2(_viasp_client=debug_client)
ctl = wrapper.Control(_viasp_client=debug_client)
sys.stdin = io.StringIO("1{person(a);person(b)}1.person(c) :- person(a).person(d) :- person(b).")
ctl.load("-")
# Check that the calls were received
Expand Down
8 changes: 4 additions & 4 deletions backend/test/test_viasp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@


def test_instanciations():
_ = wrapper.Control2()
_ = wrapper.Control2(["0"])
_ = wrapper.Control()
_ = wrapper.Control(["0"])


class DebugClient(ViaspClient):
Expand All @@ -36,7 +36,7 @@ def __init__(self, internal_client: FlaskClient):

def test_load_from_file(client):
debug_client = DebugClient(client)
ctl = wrapper.Control2(_viasp_client=debug_client)
ctl = wrapper.Control(_viasp_client=debug_client)
sample_encoding = pathlib.Path(__file__).parent.resolve() / "resources" / "sample_encoding.lp"
ctl.load(sample_encoding)
# Check that the calls were received
Expand All @@ -54,7 +54,7 @@ def test_load_from_file(client):

def test_load_from_stdin(client):
debug_client = DebugClient(client)
ctl = wrapper.Control2(_viasp_client=debug_client)
ctl = wrapper.Control(_viasp_client=debug_client)
sys.stdin = io.StringIO("sample.{encoding} :- sample.")
ctl.load("-")
# Check that the calls were received
Expand Down
20 changes: 4 additions & 16 deletions examples/App.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import sys
from clingo.application import clingo_main, Application
from clingo.script import enable_python
from viasp import Control2
from viasp import Control
from viasp.server import startup

enable_python()

class App(Application):

def main(self, ctl, files):
ctl = Control2(control=ctl, files=files)
ctl = Control(control=ctl, files=files)

for path in files:
ctl.load(path)
Expand All @@ -20,25 +20,13 @@ def main(self, ctl, files):
for m in handle:
ctl.viasp.mark(m)
print(handle.get())
unsat=handle.get().unsatisfiable
if handle.get().unsatisfiable:
ctl = ctl.viasp.relax_constraints()
ctl.viasp.show()
# ctl.viasp.clingraph(
# viz_encoding="clingraph/example5_viz.lp", #"clingraph/queens/viz.lp",#"
# engine="dot"
# )


if unsat:
relaxed_prg = ctl.viasp.relax_constraints()
print(relaxed_prg)
ctl = Control2()
ctl.add("base", [], relaxed_prg)
ctl.ground([("base", [])])
with ctl.solve(yield_ = True) as handle:
for m in handle:
ctl.viasp.mark(m)
print(handle.get())
ctl.viasp.show()


app = startup.run()
Expand Down
4 changes: 2 additions & 2 deletions examples/quickstart.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import sys
from viasp import Control2
from viasp import Control
from viasp.server import startup


def main():
options = ['0']

ctl = Control2(options, viasp_backend_url="http://localhost:5050")
ctl = Control(options, viasp_backend_url="http://localhost:5050")
for path in sys.argv[1:]:
ctl.load(path)
if not sys.argv[1:]:
Expand Down
4 changes: 2 additions & 2 deletions viasp_jupyter/viasp_jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from jupyter_dash import JupyterDash
from jupyter_dash.comms import _jupyter_config
import viasp_dash
from viasp import Control2
from viasp import Control
from viasp.server import startup
from .html import display_refresh_button

Expand Down Expand Up @@ -44,7 +44,7 @@
def load(argv):
options = ["0"]

ctl = Control2(options, viasp_backend_url="http://localhost:5050")
ctl = Control(options, viasp_backend_url="http://localhost:5050")
for path in argv:
ctl.load(path)
if not argv:
Expand Down

0 comments on commit c1fd287

Please sign in to comment.