Answer set programming

Also known as: ASP

On this page 7

Answer set programming (ASP) is a form of declarative logic programming in which a problem is described by rules and constraints, and every stable model, also called an answer set, of that program corresponds to one solution. Within Symbolic AI & reasoning, ASP is the main practical language for non-monotonic reasoning and for hard combinatorial search problems such as scheduling, configuration and planning.

Unlike Prolog, ASP does not execute a program by following a proof strategy. A grounder first replaces variables with constants, and a solver then searches for sets of atoms that satisfy the rules, using techniques borrowed from Boolean satisfiability (SAT) solvers. The programmer states what a solution looks like, not how to find it.

How stable model semantics defines a solution

The meaning of an ASP program is given by the stable model semantics, introduced by Michael Gelfond and Vladimir Lifschitz in 1988. A candidate set of atoms is guessed, the program is reduced with respect to that guess by deleting rules whose negated conditions are violated, and the guess is accepted only if it equals the minimal model of the reduced program.

This construction gives default negation (not p) a precise reading: an atom is false unless the rules force it to be true. Each atom in an answer set therefore has a justification that does not depend on itself, which rules out circular support. A program can have zero, one or many answer sets, and each set is a separate, self-consistent world.

The following table lists the core syntactic elements of a typical ASP program and the role each one plays.

ElementExampleRole
Factnode(1).States a ground truth unconditionally
Normal rulereach(Y) :- reach(X), edge(X,Y).Derives the head when the body holds
Choice rule{ color(N,C) : col(C) } = 1 :- node(N).Generates candidate solutions
Integrity constraint:- edge(X,Y), color(X,C), color(Y,C).Eliminates candidates that violate a condition
Optimization statement#minimize { W,X : cost(X,W) }.Ranks answer sets by a cost function
Core building blocks of an ASP program

The generate, define and test methodology

Most ASP encodings follow a generate, define and test pattern. Choice rules generate the search space, normal rules define auxiliary concepts, and integrity constraints test candidates and discard every one that breaks a requirement. This separation keeps encodings short: a graph coloring or a timetabling problem often fits in ten to thirty lines.

Execution happens in two phases. The grounding phase instantiates all variables over the finite domain of constants, producing a variable-free program. The solving phase applies conflict-driven nogood learning, a close relative of the conflict-driven clause learning used in modern SAT solvers, together with propagation of unfounded sets that is specific to stable models.

The table below summarizes what each phase does and where performance problems typically arise.

PhaseTypical toolMain workCommon bottleneck
Groundinggringo, idlvReplaces variables with constantsExponential blow-up of ground rules
Solvingclasp, waspSearches for stable modelsWeak propagation on large search spaces
Optimizationclasp, waspImproves the best answer set foundProving optimality can take far longer
Multi-shot controlclingo APIAdds rules incrementally between solver callsManaging state across iterations
Phases of ASP execution

ASP sits between logic programming and constraint solving. It shares rule syntax with Prolog and Datalog, but its semantics is model-based rather than proof-based, and its solving machinery resembles SAT and satisfiability modulo theories (SMT). The comparison below shows where the differences matter in practice.

ParadigmSemanticsNegationTypical use
Answer set programmingStable modelsDefault negation, non-monotonicCombinatorial search and knowledge representation
PrologSLD resolution with backtrackingNegation as failure, order dependentGeneral-purpose logic programs
DatalogLeast modelStratified onlyRecursive queries over databases
SAT solvingPropositional modelsClassical negationHardware and software verification
Constraint programmingConstraint satisfactionNot rule basedScheduling with numeric domains
ASP compared with neighboring paradigms

Compared with a raw SAT encoding, ASP offers first-order variables, recursion and closed-world reasoning directly in the language. Reachability or transitive closure, for example, needs only two rules in ASP but requires extra auxiliary variables in pure propositional logic.

Practical applications and tooling

ASP is used where a problem combines hard constraints, preferences and incomplete knowledge. Reported applications include product configuration, crew and shift rostering, classical planning, bioinformatics, phylogenetics, robotics task planning and the decision support system for the Space Shuttle reaction control system.

The most widely used system is clingo from the Potassco project at the University of Potsdam, which combines the gringo grounder with the clasp solver and offers Python and Lua scripting. DLV and its successor DLV2 are the main alternatives, with strengths in disjunctive programs and database-style reasoning.

The decision table below helps judge whether ASP is a good fit for a given task.

SituationRecommendationReason
Many interacting discrete constraintsASP is a strong fitCompact encoding and powerful propagation
Defaults and exceptions in domain rulesASP is a strong fitDefault negation models exceptions naturally
Large numeric ranges or real numbersPrefer constraint programming or MILPGrounding numeric domains explodes in size
Huge data with simple recursive queriesPrefer DatalogPolynomial evaluation scales better
Need for explanation of every decisionASP with careJustifications exist but need extra tooling
When to choose answer set programming

Limitations and common mistakes

The main limitation of ASP is the grounding bottleneck: rules with many variables over large domains can produce billions of ground instances before solving even starts. Deciding whether a normal program has an answer set is NP-complete, and disjunctive programs reach the second level of the polynomial hierarchy, so performance depends heavily on the encoding.

  • Unsafe variables: every variable in a rule must appear in a positive body literal, or the grounder rejects it.
  • Overly general choice rules create search spaces far larger than needed; tighter domains often cut runtime by orders of magnitude.
  • Confusing default and classical negation: not p means p is not derivable, while -p asserts that p is false.
  • Ignoring symmetry: interchangeable resources produce many equivalent answer sets unless symmetry-breaking constraints are added.
  • Expecting procedural control: rule order has no effect on the result, so Prolog habits such as cuts do not apply.

The idea in brief

Answer set programming is a way to solve puzzles by describing the rules, not the steps. You list the facts. You say what a good answer must look like. You also say what is forbidden. The computer then finds every arrangement that fits.

Think of a seating plan for a wedding. You know who must sit together and who must not. You do not explain how to try each table. You only state the rules, and the solver returns valid seating plans, or tells you that none exists.

A practical scenario

A regional hospital needs a monthly nurse roster for 60 staff across three shifts. Each nurse has a contract limit of hours, no one may work a night shift followed by an early shift, every ward needs at least two senior nurses per shift, and staff have stated preferences for days off.

The planning team encodes the roster in about 40 lines of ASP. Choice rules assign each nurse to at most one shift per day, integrity constraints enforce the legal rest periods and staffing minimums, and a weighted #minimize statement penalizes ignored preferences. Running clingo produces a valid roster within minutes and then keeps improving it.

The result is a schedule that satisfies every hard rule and honors most preferences. When a new regulation arrives, the team adds a single constraint instead of rewriting a scheduling algorithm, which is the main practical advantage of the declarative approach.