Skip to content

Commit

Permalink
Timeout option and more help doc
Browse files Browse the repository at this point in the history
  • Loading branch information
susuhahnml committed Aug 5, 2024
1 parent 52a07b0 commit a649aa3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
{
"serverUrl": "http://localhost",
"serverPort": "8000"
}
{"serverPort": 8000, "serverUrl": "http://localhost"}
40 changes: 34 additions & 6 deletions clinguin/server/application/backends/clingo_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Module that contains the ClingoMultishotBackend.
"""
import logging
import time
import textwrap
from functools import cached_property
from pathlib import Path
import functools
Expand Down Expand Up @@ -111,6 +113,18 @@ def register_options(cls, parser):
metavar="",
)

parser.add_argument(
"--opt-timeout",
help=textwrap.dedent(
"""\
Optional timeout for searching for optimal models.
The timeout is not exactly enforced (might take longer) but only checked after each solution is found.
"""
),
type=int,
metavar="",
)

# ---------------------------------------------
# Properties
# ---------------------------------------------
Expand Down Expand Up @@ -249,6 +263,8 @@ def _init_command_line(self):

self._default_opt_mode = self._args.default_opt_mode

self._opt_timeout = self._args.opt_timeout

def _init_interactive(self):
"""
Initializes the attributes that will change during the interaction.
Expand Down Expand Up @@ -895,25 +911,37 @@ def next_solution(self, opt_mode="ignore"):

self._iterator = iter(self._handler)
try:
start = time.time()
model = next(self._iterator)
self._clear_cache(["_ds_model"])
self._on_model(model)
self._model = model.symbols(shown=True, atoms=True, theory=True)
while optimizing and not model.optimality_proven:
if len(model.cost) == 0:
self._messages.append(
(
"Browsing Error",
"Browsing Warning",
"No optimization provided",
"error",
"warning",
)
)
self._logger.error(
self._logger.warning(
"No optimization statement provided in encoding but optimization condition provided in 'next_solution' operation. Exiting browsing."
)
raise StopIteration
break
if (
self._opt_timeout is not None
and time.time() - start > self._opt_timeout
):
self._logger.warning(
"Timeout for finding optimal model was reached. Returning model without proving optimality."
)
break
self._logger.debug("Skipping non-optimal model!")
model = next(self._iterator)
self._clear_cache(["_ds_model"])
self._on_model(model)

self._clear_cache(["_ds_model"])
self._on_model(model)
self._model = model.symbols(shown=True, atoms=True, theory=True)
except StopIteration:
self._logger.info("No more solutions")
Expand Down
25 changes: 13 additions & 12 deletions docs/clinguin/help.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,27 @@
If the problem is finding an optimal model you can pass a timeout to the server in the command line with the flag ``--out-timeout 10``.
This will make the server stop searching for models with a bette cost after ``10`` seconds. Note that the response time might be larget than this timeout
if the solver is already searching when the timeout is reached. If you pass a timeout of ``0`` the server will only get the first model, you can then keep calling the operation ``next_solution(optN)`` to improve the cost, one at a time.
Check out the `placement_optimized example <https://github.com/potassco/clinguin/tree/master/examples/angular/placement_optimized>`_.


.. admonition:: **Can't manage to place or style things in my UI**
:class: tip

If you are having trouble placing or styling elements in your UI, first make sure that they are part of your UI state in the command line.
If you are having trouble placing or styling elements in your UI, first make sure that they are part of your UI state by checking the logs in the command line.

make sure you are using the correct attributes.
The ``grid_row`` and ``grid_column`` attributes are used to place elements in a grid layout.
The ``width`` and ``height`` attributes are used to set the size of the element.
The ``class`` attribute is used to set the style of the element.
*Not in my ui-state*
If your expected attribute is not in the :ref:`ui-state`, then you have to check your :ref:`ui-files` to make sure the atom is generated.
If you are using the ``;`` operator for the ``class`` attribute, make sure you did not make a mistake and have ``attr(elem,class,(c1,c2);`` instead of ``attr(elem,class,(c1;c2).``.

If you are using the ``class`` attribute, make sure you are using the correct syntax for the style you want to apply.
The syntax is a list of strings, each string is a class that will be applied to the element.
For example, ``class,("border-dark";"bg-primary")`` will apply the classes ``border-dark`` and ``bg-primary`` to the element.
*Is part of my ui-state*
Then we recomend you use the browser's developer tools to inspect the elements and see if the styles are being applied.
You can also move the HTML directly in your browser to find the right settings. Once you found them you can copy them to your :ref:`ui-files`.

If you are using the ``class`` attribute to apply a style that is not working, make sure the style is defined in the ``ui.lp`` file.
If you are using a style that is not defined in the ``ui.lp`` file, the element will not be styled as expected.
- Inspect in the browser

- Raise an issue in github

.. admonition:: **Still need help**
:class: warning

If none of the above tips help you solve your problem, please open an issue in the `clinguin repository <https://github.com/potassco/clinguin/issues>`_.


3 changes: 2 additions & 1 deletion examples/angular/placement_optimized/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

This example show how to use consequences with optimization statements to have user feedback on optimal models.

The option ` --opt-timeout 0` makes sure that one model is computed at a time to try to find the optimal one.

### Usage

```
clinguin client-server --domain-files examples/angular/placement_optimized/instance.lp examples/angular/placement_optimized/encoding.lp --ui-files examples/angular/placement_optimized/ui.lp
clinguin client-server --domain-files examples/angular/placement_optimized/instance.lp examples/angular/placement_optimized/encoding.lp --ui-files examples/angular/placement_optimized/ui.lp --opt-timeout 0
```

![](out1.png)
Expand Down
12 changes: 7 additions & 5 deletions examples/angular/placement_optimized/ui.lp
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,13 @@ attr(window, flex_direction, row).
when(menu_bar_select, click, call, select).

elem(menu_bar_next_opt, button, menu_bar).
attr(menu_bar_next_opt, label, "Next Optimal").
attr(menu_bar_next_opt, label, "Next").
attr(menu_bar_next_opt, icon, "fa-forward-fast").
when(menu_bar_next_opt, click, call, next_solution(optN)).

elem(menu_bar_next, button, menu_bar).
attr(menu_bar_next, label, "Next").
attr(menu_bar_next, icon, "fa-forward-step").
when(menu_bar_next, click, call, next_solution).
elem(m, message, window):-_clinguin_optimizing.
attr(m, title, "Optimality information"):-_clinguin_optimizing.
attr(m, message, "Optimal!"):- _clinguin_optimizing, _clinguin_optimal.
attr(m, message, "Optimality not proven"):- _clinguin_optimizing, not _clinguin_optimal.
attr(m, type, success):- _clinguin_optimizing, _clinguin_optimal.
attr(m, type, warning):- _clinguin_optimizing, not _clinguin_optimal.

0 comments on commit a649aa3

Please sign in to comment.