Modeling with Answer Set Programming
Introduction
This lecture introduces Answer Set Programming (ASP) as a powerful tool for modeling complex problems. We will explore its theoretical foundations, focusing on:
The application of ASP in modeling real-world scenarios.
The non-monotonic nature of common-sense reasoning and how ASP addresses it through stable models.
The computational complexity of ASP, particularly its NP-completeness.
Syntactic extensions like denials and non-deterministic choices that enhance ASP’s expressiveness and simplify modeling.
The goal is to provide a comprehensive understanding of ASP, from its theoretical underpinnings to its practical application in solving complex problems.
Non-Monotonicity and Common Sense Reasoning
The presence of negated literals in logical rules introduces non-monotonicity. This contrasts with classical logic, where adding new information never invalidates previous conclusions. Common sense reasoning is inherently non-monotonic; our beliefs and inferences can change as we gain new information.
Example 1. Consider the statement "father of Pluto." This fact, once established, remains true. However, in real-world scenarios, facts are not always static. Our knowledge is often incomplete, and we may make assumptions that later turn out to be false. For instance, if we cannot prove a statement, we might assume it is false. However, new evidence could later prove it true, requiring us to revise our understanding and previous conclusions. This is a key aspect of non-monotonic reasoning.
Stable Model Semantics
The traditional concept of minimum model semantics, which works well for definite logic programs, is not suitable when dealing with negation. In the presence of negation, the intersection of two models is not guaranteed to be a model. This necessitates a different approach to define the semantics of logic programs with negation.
Gelfond and Lifschitz introduced the concept of stable models to address this issue.
Guess and Verify Approach
The stable model semantics can be understood through a "guess and verify" approach, which provides an intuitive way to grasp the concept:
Guess a Model: Start by guessing a set of atoms that you believe might constitute a stable model.
Transform the Program (Reduct): Based on the guessed model, transform the original program into a new one called the reduct. This transformation involves two steps:
Rule Removal: Remove all rules whose body is not satisfied by the guessed model.
Literal Removal: Remove all negated literals of the form ‘not L’ if the atom ‘L’ is not in the guessed model.
Compute the Minimum Model: Compute the minimum model of the transformed (reduct) program. Since the reduct is a definite program (no negation), it has a unique minimum model.
Verify Stability: If the computed minimum model is identical to the initially guessed model, then the guessed model is a stable model.
Remark. Remark 1. While the "guess and verify" approach is useful for understanding the semantics, practical solvers cannot rely solely on this method. They employ additional strategies, such as:
Treating facts as always true, thus avoiding unnecessary checks.
Starting the search from the completion of the program, which provides a good starting point for finding stable models.
Completion and Minimal Models
The completion of a logic program is a transformation that replaces implications (\(\leftarrow\)) with logical equivalences (\(\leftrightarrow\)). This transformation is a useful starting point for computing stable models.
Theorem 1. Stable models of a logic program are always among the minimal models of its completion. This theorem highlights the relationship between the two concepts and provides a way to narrow down the search space for stable models.
Example 2. Consider the following propositional program: \[\begin{aligned} & P \leftarrow P \\ & Q \leftarrow P, \text{not } S \\ & Q \leftarrow \text{not } P, \text{not } S \\ & S \leftarrow \text{not } Q \end{aligned}\] The completion of this program is: \[\begin{aligned} & P \leftrightarrow P \\ & Q \leftrightarrow (P \land \text{not } S) \lor (\text{not } P \land \text{not } S) \\ & S \leftrightarrow \text{not } Q \end{aligned}\] Simplifying the completion of \(Q\), we get: \[\begin{aligned} & P \leftrightarrow P \\ & Q \leftrightarrow \text{not } S \\ & S \leftrightarrow \text{not } Q \end{aligned}\] The minimal models of the completion are \(\{Q\}\) and \(\{S\}\). However, the set \(\{P, Q\}\) is also a model of the completion but not a stable model. This illustrates that while stable models are minimal models of the completion, not all minimal models of the completion are stable models.
Remark. Remark 2. The completion provides a necessary but not sufficient condition for stable models. It helps in identifying candidate models but requires further verification using the stable model semantics.
Supported Models and Loops
A crucial aspect of stable models is that they must be supported. This means that for an atom to be included in a stable model, there must be a rule with that atom as its head, and the body of that rule must be true within the same model. This requirement prevents the inclusion of atoms without proper justification.
Consider a situation where there is a loop in the dependencies between atoms. For example, if we have rules like: \[\begin{aligned} P &\leftarrow Q \\ Q &\leftarrow R \\ R &\leftarrow P \end{aligned}\] In such a case, none of the atoms \(P\), \(Q\), or \(R\) can be included in a stable model unless at least one of them is supported by other rules or facts. These atoms cannot support themselves in a circular manner.
Remark. Remark 3. If there is no external reason to believe in an atom, it cannot be part of a stable model. Loops or circular dependencies cannot support themselves; at least one atom in the loop must be supported by other means (e.g., a fact or a rule with a true body based on other atoms). This ensures that stable models are grounded in valid justifications.
Using SAT Solvers
Answer Set Programming (ASP) rules can be translated into Conjunctive Normal Form (CNF) clauses, the standard input format for SAT (Boolean Satisfiability) solvers. This translation allows us to leverage the efficiency of existing SAT solvers to find models of ASP programs.
While SAT solvers can find models for the translated clauses, they do not inherently enforce the minimality requirement that is crucial for stable models. As a result, SAT solvers may return models that are not minimal and, therefore, not stable models of the original ASP program.
Remark. Remark 4. The translation of ASP rules to SAT clauses is a useful technique for leveraging existing SAT solvers. However, it is important to note that the models returned by SAT solvers need further processing to ensure they are minimal and correspond to stable models of the original ASP program. This typically involves additional checks or constraints to filter out non-minimal models.
Example with Negation
Let’s consider a simple program to illustrate the behavior of negation and stable models: \[P \leftarrow \text{not } P, D \label{eq:negation_example}\]
Analysis
Without the Fact D: If we consider the program consisting only of rule [eq:negation_example], the only stable model is the empty set. This is because, if we assume \(P\) is false, then ‘not P’ is true, and the rule becomes \(P \leftarrow D\). Since \(D\) is not known to be true, \(P\) cannot be derived, and the empty set is the stable model.
Adding the Fact D: If we add the fact \(D\) to the program, the rule becomes \(P \leftarrow \text{not } P\). In this case, there is no stable model. If we assume \(P\) is true, then ‘not P’ is false, and the rule cannot be applied. If we assume \(P\) is false, then ‘not P’ is true, and the rule becomes \(P \leftarrow \text{true}\), which means \(P\) should be true, contradicting our assumption.
Remark. Remark 5. The rule \(P \leftarrow \text{not } P, D\) can be used to indicate that \(D\) must be false in all stable models. If \(D\) is true, the program has no stable models. This is because the rule tries to define \(P\) in terms of its own negation, which leads to a contradiction when \(D\) is true.
Denials (Constraints)
Denials, also known as constraints, are a syntactic extension in Answer Set Programming (ASP) that allows us to express conditions that must not be satisfied by any stable model. They are represented as rules without a head, which can be interpreted as "if the body is true, then the program is inconsistent."
A denial is a rule of the form: \[\leftarrow B_1, B_2, \dots, B_n\] where \(B_1, B_2, \dots, B_n\) are literals. This rule states that the conjunction of literals \(B_1 \land B_2 \land \dots \land B_n\) must not be true in any stable model.
Example 3. The denial \(\leftarrow B\) is equivalent to the rule \(P \leftarrow \text{not } P, B\), where \(P\) is a new atom not present in the original program. This equivalence demonstrates that denials can be expressed using standard ASP rules with negation.
Remark. Remark 6. Denials are a convenient syntactic sugar that simplifies the expression of constraints in ASP programs. While they do not add any expressive power to the language (as they can be translated into standard rules), they are very useful for writing compact and readable code. They help in specifying conditions that must be avoided in the stable models, making the modeling process more intuitive.
Number of Stable Models
An Answer Set Program (ASP) can have zero, one, or multiple stable models. The number of stable models depends on the structure of the program and the presence of negation and denials.
Zero Stable Models:
- A program can have zero stable models when a denial (constraint) is combined with conditions that force the body of the denial to be true. This indicates that the program is inconsistent and has no valid solutions.
One Stable Model:
- Programs without negation always have exactly one stable model, which is also their minimum model. This is because the reduct of such programs is the program itself, and the minimum model of a definite program is unique.
More Than One Stable Model:
- Programs with negation can have multiple stable models. A classic example is the program: \[\begin{aligned} P &\leftarrow \text{not } Q \\ Q &\leftarrow \text{not } P \end{aligned}\] This program has two stable models: \(\{P\}\) and \(\{Q\}\).
Remark. Remark 7. The number of stable models is a crucial aspect of ASP. The existence of multiple stable models allows us to represent multiple possible solutions or scenarios, while the absence of stable models indicates an inconsistent problem specification. Understanding how to control the number of stable models is essential for effective modeling in ASP.
Non-Deterministic Choices
Answer Set Programming (ASP) provides a powerful mechanism for expressing non-deterministic choices. This feature allows us to generate multiple alternative answer sets, each representing a different possible scenario or solution.
Example 4. Consider the following set of rules: \[\begin{aligned} A &\leftarrow \text{not } NA \\ NA &\leftarrow \text{not } A \end{aligned}\] These rules create a non-deterministic choice between \(A\) and \(NA\). The program has two stable models: one containing \(A\) and the other containing \(NA\). This is because if we assume \(A\) is true, then \(NA\) is false, and vice versa.
Syntactic Sugar for Choices
To simplify the expression of non-deterministic choices, ASP provides a syntactic sugar: \[\{A\}\] This notation is equivalent to the two rules: \[\begin{aligned} A &\leftarrow \text{not } NA \\ NA &\leftarrow \text{not } A \end{aligned}\] where \(NA\) is a new atom representing the negation of \(A\).
Remark. Remark 8. While the syntactic sugar \(\{A\}\) is convenient for writing concise code, it is important to remember that it is just a shorthand for the expanded form with explicit negations. When analyzing programs with non-deterministic choices, it is often helpful to consider the expanded form to fully understand the underlying semantics and how the stable models are generated. This is particularly useful when trying to debug programs or understand the behavior of the solver.
Example: Island of Liars and Truth-Tellers
Let’s consider a classic problem to demonstrate the use of non-deterministic choices, denials, and facts in ASP. Imagine an island with four students, A, B, C, and D, each of whom can either be a truth-teller or a liar. We represent the fact that student A is a truth-teller as \(A\) and the fact that student A is a liar as \(NA\). Similarly, we use \(B\) and \(NB\) for student B, \(C\) and \(NC\) for student C, and \(D\) and \(ND\) for student D.
ASP Encoding
We can model this scenario using the following ASP rules:
Non-Deterministic Choice: We use non-deterministic choices to represent the fact that each student can be either a truth-teller or a liar: \[\begin{aligned} \{A\}, \{B\}, \{C\}, \{D\} \end{aligned}\] This is equivalent to the following rules: \[\begin{aligned} A &\leftarrow \text{not } NA \\ NA &\leftarrow \text{not } A \\ B &\leftarrow \text{not } NB \\ NB &\leftarrow \text{not } B \\ C &\leftarrow \text{not } NC \\ NC &\leftarrow \text{not } C \\ D &\leftarrow \text{not } ND \\ ND &\leftarrow \text{not } D \end{aligned}\]
Denial (Constraint): We add a denial to express the constraint that A and B cannot both be liars: \[\leftarrow NA, NB\] This rule ensures that any stable model where both A and B are liars is discarded.
Fact: We add a fact to indicate that student B is a truth-teller: \[B\]
Denial (Constraint): We add a denial to express the constraint that student B cannot be a liar: \[\leftarrow NB\] This rule ensures that any stable model where B is a liar is discarded. Note that this constraint is redundant with the fact B.
Remark. Remark 9. This example demonstrates how non-deterministic choices, denials, and facts can be combined to model complex scenarios in ASP. The non-deterministic choices generate multiple possible worlds, the denials eliminate inconsistent worlds, and the facts provide specific information about the scenario.
Computational Complexity
A fundamental result in Answer Set Programming (ASP) is that determining whether a ground ASP program admits a stable model is an NP-complete problem. This result establishes the inherent computational complexity of ASP and provides a theoretical basis for understanding its capabilities and limitations.
Theorem 2. The problem of deciding whether a ground ASP program has a stable model is NP-complete.
Membership in NP
To prove that the problem is in NP, we need to show that a given candidate solution (a set of atoms) can be verified in polynomial time. The verification process involves the following steps:
Candidate Model: A candidate model is a set of atoms that are present in the program.
Polynomial Size: The size of the candidate model is polynomially related to the size of the program. This is because the candidate model is a subset of the atoms in the program.
Polynomial Reduct Computation: Computing the reduct of the program with respect to the candidate model can be done in polynomial time. This involves scanning the rules and removing or modifying them based on the candidate model.
Polynomial Fixed Point Computation: Computing the minimum model (fixed point) of the reduct can be done in polynomial time. This is because the reduct is a definite program, and the fixed point computation involves iteratively applying rules until no new atoms are derived.
Polynomial Verification: Checking if the computed minimum model is equal to the candidate model can be done in polynomial time. This involves comparing the two sets of atoms.
Since all these steps can be performed in polynomial time, the problem is in NP.
NP-Hardness
To prove NP-hardness, we need to show that any problem in NP can be reduced to the problem of finding a stable model in polynomial time. We can achieve this by reducing the 3-SAT problem to the ASP stable model existence problem.
Example 5. Consider the following 3-SAT instance: \[(A \lor \text{not } B \lor C) \land (\text{not } A \lor B \lor \text{not } C)\] This instance can be encoded in ASP as follows: \[\begin{aligned} & \{A\}, \{B\}, \{C\} \\ & C1 \leftarrow A \\ & C1 \leftarrow \text{not } B \\ & C1 \leftarrow C \\ & C2 \leftarrow \text{not } A \\ & C2 \leftarrow B \\ & C2 \leftarrow \text{not } C \\ & \text{satisfied} \leftarrow C1, C2 \\ & \leftarrow \text{not satisfied} \end{aligned}\] In this encoding:
The non-deterministic choices \(\{A\}\), \(\{B\}\), and \(\{C\}\) generate all possible truth assignments for the variables \(A\), \(B\), and \(C\).
The rules for \(C1\) and \(C2\) encode the clauses of the 3-SAT instance.
The rule for ‘satisfied’ checks if both clauses are true.
The denial ‘\(\leftarrow \text{not satisfied}\)’ ensures that only models where the 3-SAT instance is satisfied are considered.
This reduction shows that any instance of 3-SAT can be encoded as an ASP program, and the stable models of the ASP program correspond to the satisfying assignments of the 3-SAT instance. Since 3-SAT is NP-complete, this proves that the stable model existence problem is NP-hard.
Remark. Remark 10. The NP-completeness of the stable model existence problem highlights the expressive power of ASP. It also implies that solving ASP programs can be computationally challenging in the worst case, but it also means that ASP can be used to solve a wide range of NP problems.
Grounding and Non-Ground Programs
In practice, Answer Set Programming (ASP) programs are often written using variables and predicates, which are not directly handled by the solver. The process of grounding transforms a non-ground ASP program into a ground program by replacing variables with constants from the Herbrand base. This process is crucial for making the program suitable for stable model computation.
Infinite Herbrand Base
If the Herbrand base of a program is infinite, which occurs when the program includes function symbols, then the resulting ground program will also be infinite. In such cases, we cannot compute the ground program explicitly.
Finite Herbrand Base
If the Herbrand base is finite, meaning the program only uses constants, the grounding process will produce a finite program. However, the size of the ground program can still be significant.
Exponential Growth: The size of the ground program can be exponential in the maximum number of variables in a rule. For example, if a rule has \(k\) variables and there are \(c\) constants in the Herbrand base, the number of ground instances of that rule can be \(c^k\).
Practical Considerations: In practice, the number of variables in a rule is usually small (typically 3-5). Therefore, the grounding process is often effectively polynomial in the size of the input program.
Remark. Remark 11. In some specific cases, the grounding process can still lead to exponential growth, even with a finite Herbrand base. Examples include:
Intervals: When using intervals, the number of ground instances can grow rapidly with the size of the intervals.
Special Programs: Certain programs can be designed to exhibit exponential grounding behavior.
These cases can lead to a complexity beyond NP, potentially reaching NEXPTIME. However, in most practical scenarios, the grounding process is manageable and does not cause significant performance issues.
ASP Solver Architecture
Answer Set Programming (ASP) solvers typically follow a two-stage architecture: grounding and solving. This separation allows for efficient handling of complex programs.
Grounder:
The grounder takes a non-ground ASP program as input and transforms it into a ground program. This involves replacing variables with constants from the Herbrand base.
The grounder also performs various optimizations to reduce the size of the ground program and improve the efficiency of the solving process. A prominent example of a grounder is Gringo.
Solver:
The solver takes the ground program produced by the grounder and computes its stable models.
The solver employs sophisticated algorithms to efficiently search for stable models. A popular ASP solver is Clasp.
The combination of Gringo and Clasp is often referred to as Clingo.
Grounding Bottleneck
In some cases, the grounding process can produce very large ground programs, leading to a "grounding bottleneck." This can occur when the program has many variables, constants, or complex rules. The large size of the ground program can cause performance issues and make the solving process more difficult.
Various techniques exist to mitigate the grounding bottleneck, including:
Domain Predicates and Range Restriction: Using domain predicates to restrict the range of variables, which reduces the number of ground instances generated.
Partial Grounding: Delaying the grounding of certain parts of the program until necessary.
Program Optimization: Optimizing the program structure to reduce the number of ground instances.
Domain Predicates and Range Restriction
To ensure efficient grounding and avoid generating unnecessary ground instances, it is crucial to range-restrict variables. This is typically achieved by ensuring that every variable in a rule appears as an argument of a domain predicate that appears positively in the body of the rule.
A domain predicate is a predicate that defines the possible values for a variable. By using domain predicates, we can limit the number of ground instances generated during the grounding process.
Example 6. For example, if we have a rule like ‘q(X,Y) :- r(X), not s(Y)’, the variable ‘X’ is range-restricted by the domain predicate ‘r(X)’, while ‘Y’ is not range-restricted because ‘s(Y)’ appears negatively. To range-restrict ‘Y’, we would need a positive occurrence of a domain predicate for ‘Y’ in the body of the rule.
Available ASP Systems
Several Answer Set Programming (ASP) systems have been developed over the years, each with its own strengths and features. Here is an overview of some of the most notable systems:
Smodels (with lparse):
Smodels was one of the earliest ASP solvers and played a significant role in the development of the field.
It uses the ‘lparse’ grounder.
While historically important, Smodels is generally slower than more modern solvers.
Cmodels:
Cmodels is another ASP solver that uses a SAT solver as its core engine.
It translates ASP programs into SAT clauses and then uses a SAT solver to find models.
DLV:
DLV is a powerful ASP system that supports a wide range of features, including disjunction and aggregates.
It was developed by the University of Calabria and the Technical University of Vienna.
DLV is known for its advanced grounding and solving techniques.
Clingo (Gringo + Clasp):
Clingo is a state-of-the-art ASP system that combines the ‘Gringo’ grounder and the ‘Clasp’ solver.
Gringo is known for its efficient grounding techniques, and Clasp is a highly optimized solver.
Clingo is widely used and recommended for its performance and versatility.
Remark. Remark 12. While various ASP systems are available, Clingo is the recommended choice for most practical applications due to its performance, versatility, and wide community support. It is actively maintained and provides a comprehensive set of features for modeling and solving complex problems.
Using Clingo
This section provides a practical guide on how to use the Clingo ASP system. Clingo combines the Gringo grounder and the Clasp solver, offering a powerful and efficient environment for solving ASP problems.
Installation:
Clingo can be installed using various methods, including package managers (e.g., ‘apt-get’ on Linux) or through Anaconda.
It is recommended to follow the installation instructions provided on the Potassco website (https://potassco.org/).
Running Clingo:
To run Clingo, use the following command in your terminal:
clingo <filename.lp> <number_of_solutions><filename.lp>is the name of the file containing your ASP program (typically with the ‘.lp’ extension).<number_of_solutions>specifies the number of stable models you want to find. Use ‘0’ to find all stable models.
Defining Constants:
You can define constants in your ASP program using the ‘#const’ directive:
#const n = 10.This allows you to parameterize your programs and easily change values without modifying the program logic.
Controlling Output with ‘#show’:
When your program generates a large number of predicates, you can use the ‘#show’ directive to select which predicates should be displayed in the output:
#show p/1.This will only show predicates with the name ‘p’ and arity 1 in the output, making it easier to analyze the results.
Remark. Remark 13. Clingo provides a flexible and powerful environment for developing and solving ASP programs. By understanding how to use the command-line interface, define constants, and control the output, you can effectively leverage the capabilities of Clingo to solve a wide range of problems.
Exercises
To solidify your understanding of Answer Set Programming (ASP), here are some exercises that you can try:
Simple Scenario Modeling:
Model simple scenarios using the concepts learned in this lecture, such as non-deterministic choices, denials, and facts.
Try to model situations involving decision-making, planning, or problem-solving.
Start with small examples and gradually increase the complexity.
This exercise will help you gain practical experience with the basic building blocks of ASP.
K-Coloring Problem:
Model the K-coloring problem for a given graph.
The K-coloring problem involves assigning one of K colors to each vertex of a graph such that no two adjacent vertices have the same color.
You can represent the graph using facts, such as ‘edge(a,b)’, ‘edge(b,c)’, etc.
You can use non-deterministic choices to assign colors to the vertices and denials to enforce the constraints.
This exercise will challenge you to apply your knowledge of ASP to a well-known problem in graph theory.
Remark. Remark 14. These exercises are designed to help you practice and apply the concepts learned in this lecture. By attempting these exercises, you will gain a deeper understanding of ASP and its capabilities. Feel free to experiment and explore different approaches to solving these problems.
Conclusion
This lecture has provided a comprehensive introduction to Answer Set Programming (ASP), covering its theoretical foundations, computational properties, and practical aspects of modeling. We have explored the following key concepts:
Stable Models: The core concept of ASP, providing a semantics for logic programs with negation.
Non-Monotonic Reasoning: How ASP addresses the challenges of common-sense reasoning where new information can invalidate previous conclusions.
Denials (Constraints): A syntactic sugar for expressing conditions that must not be satisfied by any stable model.
Non-Deterministic Choices: A mechanism for generating multiple alternative answer sets, representing different possible scenarios.
NP-Completeness: The inherent computational complexity of ASP, which places it within the NP complexity class.
Grounding: The process of transforming non-ground programs into ground programs suitable for solving.
ASP Solvers: An introduction to available ASP solvers, particularly Clingo, and how to use them.
The key takeaway is that ASP is a powerful and expressive language for modeling problems within the NP complexity class. It has a well-defined semantics and efficient solvers available, making it a valuable tool for solving a wide range of problems.
Future lectures will build upon these concepts, exploring more advanced modeling techniques and applications of ASP. We will delve into more complex scenarios and investigate how to leverage the full potential of ASP for solving real-world problems.
Follow-Up Questions
Here are some follow-up questions to consider as you continue to learn about ASP:
How can we model more complex scenarios, such as those involving time or uncertainty?
What are some real-world applications of ASP?
How can we optimize ASP programs for better performance?
What are the limitations of ASP, and when might other formalisms be more suitable?
These questions will guide our future discussions and help you deepen your understanding of ASP.