Đorđe Marković - Ђорђе Марковић
About | Posts | Presentations | Contact
📅 Last updated: Apr 14. 2025
Programming Contest - International Conference on Logic Programming (2024)
👥 Authors: Bart Bogaerts, Robbe Van den Eede, Djordje Markovic
📧 Contact: bart [dot] bogaerts [at] vub [dot] be, robbe [dot] vandeneede [at] kuleuven [dot] be, dorde [dot] markovic [at] kuleuven [dot] be
📅 Created: Jan 8. 2025
⏳ Estimated reading time: ~30min
Content
- Introduction
- Problems
- Challenge 1: Gates to truth
- Challenge 2: Tile them all
- Challenge 3: Area fifty won
- Challenge 4: Fence for fans
- Conclusion
Introduction
- Challenge 1: Gates to truth
- Challenge 2: Tile them all
- Challenge 3: Area fifty won
- Challenge 4: Fence for fans
This post aims to present, analyze, and solve the four challenges from the programming contest held at the International Conference on Logic Programming (2024). Short specifications of the problems are elaborated on and accompanied by graphical visualizations for better understanding. For solving the challenges IDP3 system [De Cat, 2018] is used (for technical details consult the [IDP manual, 2020]).
About the contest
The Programming Contest at the 2024 International Conference on Logic Programming was organized by Prof. Martin Gebser. Participants faced four challenges, each of which could be solved using any declarative programming language, such as Prolog, ASP, or IDP, within a time limit of one and a half hours. The competition was conducted in teams of up to three members.
Problems
Challenge 1: Gates to truth
Problem description from the assignment:
In this problem, we are given the layout of a Boolean circuit with unary and binary gates, yet we have to decide which logical operations the gates should perform. For each unary gate, we have to choose between the identity function and negation, and between logical AND and OR for the binary gates. A partial specification requires specific output values for some truth assignments of the input variables, while the output can be arbitrary for the remaining truth assignments.
The following figure illustrates the problem by displaying the given layout of the logical circuit (first block), four entries of desired input/output values for the circuit (second block), and finally the unique assignment of the functionalities of each of the gates in the circuit such that entries from the previous block are satisfied (third block).
We should focus on the underlying knowledge of this domain in order to provide a good declarative solution to this problem. The main building block of this domain is the logical gate. These are further composed into logical circuits and constrained by certain entities that are provided. However, the main law in this example is the law of the behavior of different logical gates. The following illustration depicts the different logical gates and how they map their input variables to the output in terms of logical operations (block one), and provides a tabular definition for each of the logical operations (block two).
Taking everything into account, the idea is to declaratively define (formally specify) the behavior of each of the gates by refining their output in terms of the input variables. Additionally, this definition should support multiple entries. Finally, we should be able to search for gate functionalities given the layout of the circuit (i.e., connections of inputs and outputs of different gates) and the list of entries.
Solwing the problem with IDP3
The first step towards the solution is to consider the format of the input. We are given specification of the Boolean circuit in a Prolog stile fact list using the following predicates:
- \(variable/1\) - set of logical variables that are input of the circuit.
- \(result/1\) - variable representing the output of the circuit.
- \(unary/1\) - set of unary gates.
- \(binary/1\) - set of binary gates.
- \(input/3\) - set of input variables of a gate per slot (if unary only one, if binary then two).
- \(output/2\) - output variable per gate.
- \(entry/3\) - value of input variables per iteration.
- \(value/3\) - value of the output variable per iteration.
To solw the problem we should model the behaviour of the circuit depending on the gates functionalities. The behaviour of the circuite is modeled in a composite way as definition of the output of each gate in terms of it's imput. For example, let input of a binary gate \(G_1\) be variables \(x_1\) and \(x_2\), and output variable \(x_3\), then:
- If gate is OR, value of \(x_3\) is logical "or" of values \(x_1\) and \(x_2\).
- If gate is AND, value of \(x_3\) is logical "and" of values \(x_1\) and \(x_2\).
Note that even though the goal is to find functionalities of the gates, we are defining the value of variables in terms of gates. This is because IDP system can reason in any direction, and hence can be used to find functionalities of gates such that partialy specified values of variables are sensible (i.e., satisfying the value definition).
The main code of the solution is contained in the "Theory" block in the two definitions:
- "Definition of Gate operations" defines the logical operations negation (\(neg\)), conjunction (\(and\)), and disjunction (\(or\)).
- "Definition of a cValue of a variable" defines the computed value (\(cValue\)) of each variable (input or output of any gate) per iteration.
Following is the code containing the full solution of the problem. It is possible to run the code in the online editor by clicking "Try in online editor!".
Gates-to-truth-webide.idp <Try in online editor!>
Challenge 2: Tile them all
Problem description from the assignment:
Consider a rectangular \(4 \times 5\) grid such that each of its cells contains a number: \(0, 1, 2,\) or \(3\). The goal is to group horizontally or vertically adjacent cells into ten disjoint pairs such that all ten of the combinations \(\{0, 1, 2, 3\} \times \{0, 1, 2, 3\}\) are represented by a pair, also called tile.
The following figure illustrates the problem. On the left is presented an example input matrix, in the middle are pairs of all possible combinations of numbers, on the right these pairs are uniquely identified in the matrix.
To solve this problem, we will declaratively specify what it means for two cells to be a pair in the matrix, and then add the constraint that each possible pair has to exist.
Solwing the problem with IDP3
The format of the input for this problem is the Prolog stile fact list specifying the value for each cell in the matrix:
- \(cell/3\) - set of values per cell, where cell is specified as X and Y
Following is the code containing the full solution of the problem. The solution contains axioms that are explained with comments. It is possible to run the code in the online editor by clicking "Try in online editor!".
Tile-them-all.idp <Try in online editor!>
Challenge 3: Area fifty won
Problem description from the assignment:
Consider a \(5 \times 5\) grid such that some of its cells contain positive numbers and the remaining cells are empty. The goal is to partition the grid into as many disjoint areas as there cells with positive numbers, where each area consists of exactly one cell with a positive number \(N\) and \(N − 1\) empty cells. Moreover, the cells of each area must form a connected region, i.e., there is a path via horizontally or vertically adjacent neighbors between any two cells belonging to the same area.
The problem is illustrated in the following figure. On the left, we can see the input \(5 \times 5\) table where each cell contains a number (\(0\) cells are empty). In the middle is a table with identified cells that represent the roots of the partitions. Finally, the table on the right represents the unique solution where each cell with number \(n \neq 0\) is associated with exactly \(n\) other cells all mutually reachable (this is represented by the same color of the cells around cells with the number).
The main property of this problem is the definition of what it means for a group of cells to be connected in an island, i.e., each cell in the group is reachable from each other. Once this property is formalized, all that is needed is to specify constraints that in each island there is one non-zero cell and that size of the island is exactly that number.
Solwing the problem with IDP3
The format of the input for this problem is the Prolog stile fact list specifying the numbers for each cell in the matrix:
- \(\mathit{cell}/3\) - number per cell, where cell is specified as Row and Col(umn)
- \(\mathit{Pos}\) - A type representing positions and constructed as the set of pairs from existing types \(\mathit{Row}\) and \(\mathit{Col}\).
- \(\mathit{content}/1:\) - Function mapping positions to the number, functionalization of relation \(\mathit{cell}/3\).
- \(\mathit{adjacent}/2\) - Relation of neighbouring cells (Manhattan distance at most 1)
- \(\mathit{path}/3\) - The relation between two points and area. It holds true iff the second position is reachable from the first only through the cells of the same area.
Following is the code containing the full solution of the problem. The solution contains axioms that are explained with comments. It is possible to run the code in the online editor by clicking "Try in online editor!".
Area-fifty-won.idp <Try in online editor!>
Challenge 4: Fence for fans
Problem description from the assignment:
Consider a \(W \times H\) grid of width W and height H such that, for each cell \(c\), a non-negative cost \(cost(c)\) and a (possibly negative) reward \(reward(c)\) is given. The goal is to set up a fence, forming a non-empty and non-simple cycle, which must not include any cell with a negative reward (\(reward(c) < 0\) is not permitted for cells \(c\) belonging to the cycle) and maximizes the following quality function: \[\sum_{\text{cell } c \text{ belongs to the cycle}} (reward(c) - cost(c))\] Moreover, given limits on the length of the cycle, i.e., the number of contained cells, and the budget for setting up the cycle, i.e., the sum of costs \(cost(c)\) over the cells \(c\) belonging to the cycle, must not be exceeded.
The following figure is presenting an input table (top left) with it components reward (top right) and cost (bottom left) scores. The same figure provides an example of a non-empty cycle (bottom right), the arrows are there to help us ilustrate how such a cycle can be defined.
The core of this problem is definition of a valid non-empty non-trivial cycle of fences, the remaining of the probelm is about adding couple of constraints and searching for an optimal solution. A valid cycle is a set of cells that can be grouped into pairs of cells forming a linear closed path. This means that such a path has no branching nor merging, and every cell in the path has to be reachable from every other cell. On the figure above, bottom right ilustration depicts the cycle by colored cells and the described relation with arrows.
The following figure presents two optimal solution for the given input matrix from the previous figure. The purple cycle (top right) has total score of \(8\) (\(20\) reward minus \(12\) cost, recall that each cell cost \(1\) in this example). The orange cycle (bottom left) has total score of \(8\) too (\(18\) reward minus \(10\) cost). Both solutions are valid.
Solwing the problem with IDP3
The format of the input for this problem is the Prolog stile fact list specifying the reward/cost for each cell in the matrix:
- \(\mathit{reward}/3\) - reward per cell, where cell is specified as Row and Col(umn)
- \(\mathit{cost}/3\) - cost per cell, where cell is specified as Row and Col(umn)
- \(\mathit{width}/1\) - width of the matrix
- \(\mathit{height}/1\) - height of the matrix
- \(\mathit{length}/1\) - maximum length of the fences
- \(\mathit{budget}/1\) - maximum cost of the fences
Following is the code containing the full solution of the problem. The solution contains axioms that are explained with comments. It is possible to run the code in the online editor by clicking "Try in online editor!".
Area-fifty-won.idp <Try in online editor!>
Conclusion
The IDP3 system proved to be suitable for modeling and solving problems from the 2024 ICLP Programming Contest. To conclude this story, we point to two important observations. First, IDP3 provides a natural way to model domains independently of the specific problem. A good example is the first challenge, where the defined concept is given as input and we search for parameters of the definition. This power of the IDP3 system comes from the precise model semantics of the language, allowing clear and declarative specifications of the domain. Secondly, it is important to mention that the challenges were on the edge of the computational capacity of the IDP3 system. Hence, for some problems, finding the solution can take up to a few minutes (e.g., the last problem).
The source code for all examples is available in this Git repository.
References
[De Cat, 2018] De Cat, B. a. (2018). Predicate logic as a modeling language: the IDP system. In Declarative Logic Programming: Theory, Systems, and Applications (pp. 273--323).
[IDP manual, 2020] KU Leuven Knowledge Representation and Reasoning research group (2020), The IDP framework reference manual