ProbLog Documentation

repository·master·Indexed 19 days ago

https://github.com/ml-kuleuven/problog

ProbLog is a Probabilistic Logic Programming toolbox for handling complex interactions and uncertainties. It converts probabilistic logic programs into weighted Boolean formulas for inference via weighted model counting. The toolkit supports computing marginals, sampling, Most Probable Explanation (MPE), Learning from Interpretations (LFI), and decision-theoretic (dt) mode. It provides a Python API and a CLI for managing logic formulas, execution engines, and knowledge compilation tools.

Tokens
22.6K
Snippets
83
Records
100
Agent score
64%

What's inside ProbLog

  1. What is ProbLog?

    master

    ProbLog 2 is a Probabilistic Logic Programming toolbox. It allows you to build programs that encode complex interactions between heterogeneous components while accounting for inherent uncertainties.

    Core Concepts:

    • Probabilistic Logic Programs: Logic programs where some facts are annotated with probabilities.
    • Inference Engine: Converts programs, queries, and evidence into weighted Boolean formulas. This reduces inference tasks to weighted model counting, utilizing methods from graphical model and knowledge compilation literature.
    • Supported Tasks: Computing marginals given evidence and learning from (partial) interpretations.
    • Data Integration: Knowledge bases can be represented as Prolog/Datalog facts, CSV files, SQLite database tables, or via custom functions implemented in the host environment.
  2. Explore the ProbLog Python API modules

    master

    The ProbLog Python API is organized into several specialized modules. Depending on your task, you will interact with different parts of the library:

    • Logic and Formulas: problog.logic, problog.formula, problog.cnf_formula, problog.ddnnf_formula, problog.dd_formula, problog.bdd_formula, and problog.sdd_formula for defining and manipulating probabilistic logic and various formula representations (CNF, DNNF, DD, BDD, SDD).
    • Core Engine and Execution: problog.engine, problog.engine_builtin, problog.engine_stack, problog.engine_unify, problog.evaluator, and problog.forward for running inference and managing the execution engine.
    • Program Management: problog.program and problog.parser for parsing and handling ProbLog programs.
    • Constraints and Cycles: problog.constraint and problog.cycles for managing logical constraints and cyclic dependencies.
    • Specialized Solvers and Utilities: problog.maxsat for MaxSAT problems, problog.kbest for k-best queries, problog.extern for external functions, problog.clausedb for clause databases, and problog.util for general utilities.
  3. Use the Aggregate library for LDL++ style aggregation

    master

    The aggregate library allows for LDL++ style aggregation. To use it, you must first import the module: :- use_module(library(aggregate))..

    An aggregating clause follows this syntax: FUNCTOR(*GroupArgs, AggFunc<AggVar>) :- BODY.

    • FUNCTOR: The predicate name.
    • GroupArgs: (Optional) Arguments used for 'group by' logic.
    • AggFunc: A binary predicate (e.g., sum, avg, min, max) that maps a list to a term.
    • AggVar: The variable over which aggregation is computed.
    • BODY: The clause body.

    User-defined aggregation predicates must be /2 (taking a list as input and returning a result). These aggregates also support probabilistic data.

    :- use_module(library(aggregate)).
    
    person(a).
    person(b).
    salary(a, 1000).
    salary(b, 1200).
    dept(a, dept_a).
    dept(b, dept_a).
    
    % Average salary per department
    % Syntax: dept_salary(Dept, avg<Salary>) :- person(X), salary(X, Salary), dept(X, Dept).
  4. Manage multiple theories using Scopes

    master

    Scopes allow you to manage several ProbLog theories within a single model using the :/2 operator.

    • Defining a scope: scope(Name):Predicate (e.g., scope(1):knowledge(1).).
    • Unifying scopes: Use the ;/2 operator to generate the union of scopes.
    • Querying scopes: Use the scope name in a query (e.g., query(scope(1):a).).
    • Temporary unions: You can query a list of scopes simultaneously: query([scope(1), scope(2)]:b)..
    • Global predicates: Predicates defined outside any scope are considered part of all scopes.
    • Conjunction reasoning: You can perform conjunctions within a scope: query(scope(1):(a, b))..
    scope(1):a.
    scope(2):b.
    % Querying the union of scope 1 and 2
    query([scope(1), scope(2)]:b).
  5. Understand the difference between ProbLog and Prolog

    master
    ProbLog is a logic representation language, not a complete programming language. While it supports a subset of Prolog (based on Yap Prolog), it lacks many programming-related features found in standard Prolog, such as complex control constructs, input/output, and message handling. Use ProbLog to express probabilistic models rather than for general-purpose logic programming.
  6. Annotated disjunctions for non-binary choices

    master

    Annotated disjunctions allow you to model scenarios where multiple mutually exclusive outcomes are possible. The sum of probabilities in a disjunction represents the total probability of any of those outcomes occurring; the remaining probability is assigned to an implicit 'null' state where none of the options are true.

    % Modeling a 6-sided die
    1/6::die(D, 1); 1/6::die(D, 2); 1/6::die(D, 3);
    1/6::die(D, 4); 1/6::die(D, 5); 1/6::die(D, 6).
  7. Probabilistic clauses and head annotations

    master

    ProbLog allows probabilities to be placed in the head of a clause. This means that if the body of the clause is true, the head will be true with the specified probability.

    Note: Any program using probabilistic clauses can be transformed into an equivalent program using only probabilistic facts and standard Prolog clauses.

    % If burglary is true, alarm is true with 90% probability
    0.1::burglary.
    0.9::alarm :- burglary.
  8. Use the Collect library and the => operator

    master

    The collect library provides the => operator, which generalizes the all/3 operator for complex aggregations.

    Syntax: ( CODEBLOCK ) => GroupBy / AggFunc(Arg1, Arg2, ..., ArgK)

    • CODEBLOCK: A Prolog-parseable block of code.
    • AggFunc: The aggregation function to apply to the results of the CODEBLOCK.
    • Arg1...K: Arguments for the aggregation function.
    • GroupBy: (Optional) An expression to group the results.

    To implement a custom collection, define a predicate with the shape: collect_AggFunc(CodeBlock, GroupBy, Arg1, ..., ArgK, Result)

    % Example: Getting average per column for integer cell values
    column_average(Column, Avg) :- (
        cell(Row, Column, Value),
        type(cell(Row, Column, 'int'))
    ) => Column / avg(Value, Avg).
  9. Probabilistic modeling syntax in ProbLog

    master

    ProbLog extends standard Prolog syntax with the :: operator to allow for probabilistic modeling. You can define probabilistic facts, clauses, and annotated disjunctions.

    Syntax Overview

    • Probabilistic Fact: P::fact. (e.g., 0.5::a.)
    • Probabilistic Clause: P::head :- body. (e.g., 0.5::a :- x.)
    • Annotated Disjunction: P1::choice1; P2::choice2. (e.g., 0.5::a; 0.5::b.). This expresses that at most one of these choices is true. There is an implicit null choice (none of the options are taken) which has a probability equal to 1 - sum(P_i).

    Probabilistic Facts

    % A single probabilistic choice
    0.5::heads.
    
    % Two separate probabilistic choices
    0.5::heads1.
    0.5::heads2.
    
    % Generalizing with variable arguments
    0.5::heads(C).
  10. Decision-theoretic modeling with DTProbLog

    master

    DTProbLog is an extension for decision-theoretic modeling. It differs from standard ProbLog in that it does not use query or evidence. Instead, it focuses on finding optimal choices.

    • Decision Facts: Annotate facts as decisions to determine the optimal choice. Use ?::a. or decision(a)..
    • Utilities: Define the contribution of atoms to the final score using the utility(Atom, Value) predicate.
    % Decision fact
    ?::a.
    
    % Utility definition
    utility(win, 10).
    utility(buy, -1).
  11. Using tabling for efficiency and cyclic programs

    master

    ProbLog uses tabling (memoization) for all computations. This provides two main benefits:

    1. Efficiency: It caches results of computations, turning exponential-time recursive algorithms (like Fibonacci) into linear-time ones.
    2. Handling Cycles: It allows for the definition of cyclic relations (like ancestor or path in a graph) that would cause infinite recursion in standard Prolog.
    % Path in a potentially cyclic graph
    path(X, Y) :- edge(X, Y).
    path(X, Y) :- edge(X, Z), path(Z, Y).