Clingo C API
C API for clingo providing high level functions to control grounding and solving.
propagator.c

The example shows how to write a simple propagator for the pigeon hole problem. For a detailed description of what is implemented here and some background, take a look at the following paper:

https://www.cs.uni-potsdam.de/wv/publications/#DBLP:conf/iclp/GebserKKOSW16x

Output

The output is empty because the pigeon hole problem is unsatisfiable.

Code

#include <clingo.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
// state information for individual solving threads
typedef struct {
// assignment of pigeons to holes
// (hole number -> pigeon placement literal or zero)
size_t size;
} state_t;
// state information for the propagator
typedef struct {
// mapping from solver literals capturing pigeon placements to hole numbers
// (solver literal -> hole number or zero)
int *pigeons;
size_t pigeons_size;
// array of states
state_t *states;
size_t states_size;
} propagator_t;
// returns the offset'th numeric argument of the function symbol sym
bool get_arg(clingo_symbol_t sym, int offset, int *num) {
clingo_symbol_t const *args;
size_t args_size;
// get the arguments of the function symbol
if (!clingo_symbol_arguments(sym, &args, &args_size)) { return false; }
// get the requested numeric argument
if (!clingo_symbol_number(args[offset], num)) { return false; }
return true;
}
bool init(clingo_propagate_init_t *init, propagator_t *data) {
// the total number of holes pigeons can be assigned too
int holes = 0;
// stores the (numeric) maximum of the solver literals capturing pigeon placements
// note that the code below assumes that this literal is not negative
// which holds for the pigeon problem but not in general
clingo_symbolic_atom_iterator_t atoms_it, atoms_ie;
// ensure that solve can be called multiple times
// for simplicity, the case that additional holes or pigeons to assign are grounded is not handled here
if (data->states != NULL) {
// in principle the number of threads can increase between solve calls by changing the configuration
// this case is not handled (elegantly) here
if (threads > data->states_size) {
clingo_set_error(clingo_error_runtime, "more threads than states");
}
return true;
}
// allocate memory for exactly one state per thread
if (!(data->states = (state_t*)malloc(sizeof(*data->states) * threads))) {
return false;
}
memset(data->states, 0, sizeof(*data->states) * threads);
data->states_size = threads;
// the propagator monitors place/2 atoms and dectects conflicting assignments
// first get the symbolic atoms handle
if (!clingo_propagate_init_symbolic_atoms(init, &atoms)) { return false; }
// create place/2 signature to filter symbolic atoms with
if (!clingo_signature_create("place", 2, true, &sig)) { return false; }
// get an iterator after the last place/2 atom
// (atom order corresponds to grounding order (and is unpredictable))
if (!clingo_symbolic_atoms_end(atoms, &atoms_ie)) { return false; }
// loop over the place/2 atoms in two passes
// the first pass determines the maximum placement literal
// the second pass allocates memory for data structures based on the first pass
for (int pass = 0; pass < 2; ++pass) {
// get an iterator to the first place/2 atom
if (!clingo_symbolic_atoms_begin(atoms, &sig, &atoms_it)) { return false; }
if (pass == 1) {
// allocate memory for the assignemnt literal -> hole mapping
if (!(data->pigeons = (int*)malloc(sizeof(*data->pigeons) * (max + 1)))) {
return false;
}
data->pigeons_size = max + 1;
}
while (true) {
int h;
bool equal;
// stop iteration if the end is reached
if (!clingo_symbolic_atoms_iterator_is_equal_to(atoms, atoms_it, atoms_ie, &equal)) { return false; }
if (equal) { break; }
// get the solver literal for the placement atom
if (!clingo_symbolic_atoms_literal(atoms, atoms_it, &lit)) { return false; }
if (!clingo_propagate_init_solver_literal(init, lit, &lit)) { return false; }
if (pass == 0) {
// determine the maximum literal
assert(lit > 0);
if (lit > max) { max = lit; }
}
else {
// extract the hole number from the atom
if (!clingo_symbolic_atoms_symbol(atoms, atoms_it, &sym)) { return false; }
if (!get_arg(sym, 1, &h)) { return false; }
// initialize the assignemnt literal -> hole mapping
data->pigeons[lit] = h;
// watch the assignment literal
if (!clingo_propagate_init_add_watch(init, lit)) { return false; }
// update the total number of holes
if (h + 1 > holes) { holes = h + 1; }
}
// advance to the next placement atom
if (!clingo_symbolic_atoms_next(atoms, atoms_it, &atoms_it)) { return false; }
}
}
// initialize the per solver thread state information
for (size_t i = 0; i < threads; ++i) {
if (!(data->states[i].holes = (clingo_literal_t*)malloc(sizeof(*data->states[i].holes) * holes))) {
return false;
}
// initially no pigeons are assigned to any holes
// so the hole -> literal mapping is initialized with zero
// which is not a valid literal
memset(data->states[i].holes, 0, sizeof(*data->states[i].holes) * holes);
data->states[i].size = holes;
}
return true;
}
bool propagate(clingo_propagate_control_t *control, const clingo_literal_t *changes, size_t size, propagator_t *data) {
// get the thread specific state
state_t state = data->states[clingo_propagate_control_thread_id(control)];
// apply and check the pigeon assignments done by the solver
for (size_t i = 0; i < size; ++i) {
// the freshly assigned literal
clingo_literal_t lit = changes[i];
// a pointer to the previously assigned literal
clingo_literal_t *prev = state.holes + data->pigeons[lit];
// update the placement if no literal was assigned previously
if (*prev == 0) { *prev = lit; }
// create a conflicting clause and propagate it
else {
// current and previous literal must not hold together
clingo_literal_t clause[] = { -lit, -*prev };
// stores the result when adding a clause or propagationg
// if result is false propagation must stop for the solver to backtrack
bool result;
// add the clause
if (!clingo_propagate_control_add_clause(control, clause, sizeof(clause)/sizeof(*clause), clingo_clause_type_learnt, &result)) { return false; }
if (!result) { return true; }
// propagate it
if (!clingo_propagate_control_propagate(control, &result)) { return false; }
if (!result) { return true; }
// must not happen because the clause above is conflicting by construction
assert(false);
}
}
return true;
}
void undo(clingo_propagate_control_t *control, const clingo_literal_t *changes, size_t size, propagator_t *data) {
// get the thread specific state
state_t state = data->states[clingo_propagate_control_thread_id(control)];
// undo the assignments made in propagate
for (size_t i = 0; i < size; ++i) {
clingo_literal_t lit = changes[i];
int hole = data->pigeons[lit];
if (state.holes[hole] == lit) {
// undo the assignment
state.holes[hole] = 0;
}
}
}
bool print_model(clingo_model_t const *model) {
bool ret = true;
clingo_symbol_t *atoms = NULL;
size_t atoms_n;
clingo_symbol_t const *it, *ie;
char *str = NULL;
size_t str_n = 0;
// determine the number of (shown) symbols in the model
if (!clingo_model_symbols_size(model, clingo_show_type_shown, &atoms_n)) { goto error; }
// allocate required memory to hold all the symbols
if (!(atoms = (clingo_symbol_t*)malloc(sizeof(*atoms) * atoms_n))) {
clingo_set_error(clingo_error_bad_alloc, "could not allocate memory for atoms");
goto error;
}
// retrieve the symbols in the model
if (!clingo_model_symbols(model, clingo_show_type_shown, atoms, atoms_n)) { goto error; }
printf("Model:");
for (it = atoms, ie = atoms + atoms_n; it != ie; ++it) {
size_t n;
char *str_new;
// determine size of the string representation of the next symbol in the model
if (!clingo_symbol_to_string_size(*it, &n)) { goto error; }
if (str_n < n) {
// allocate required memory to hold the symbol's string
if (!(str_new = (char*)realloc(str, sizeof(*str) * n))) {
clingo_set_error(clingo_error_bad_alloc, "could not allocate memory for symbol's string");
goto error;
}
str = str_new;
str_n = n;
}
// retrieve the symbol's string
if (!clingo_symbol_to_string(*it, str, n)) { goto error; }
printf(" %s", str);
}
printf("\n");
goto out;
error:
ret = false;
out:
if (atoms) { free(atoms); }
if (str) { free(str); }
return ret;
}
bool ret = true;
clingo_model_t const *model;
// get a solve handle
if (!clingo_control_solve(ctl, clingo_solve_mode_yield, NULL, 0, NULL, NULL, &handle)) { goto error; }
// loop over all models
while (true) {
if (!clingo_solve_handle_resume(handle)) { goto error; }
if (!clingo_solve_handle_model(handle, &model)) { goto error; }
// print the model
if (model) { print_model(model); }
// stop if there are no more models
else { break; }
}
// close the solve handle
if (!clingo_solve_handle_get(handle, result)) { goto error; }
goto out;
error:
ret = false;
out:
// free the solve handle
return clingo_solve_handle_close(handle) && ret;
}
int main(int argc, char const **argv) {
char const *error_message;
int ret = 0;
clingo_control_t *ctl = NULL;
// arguments to the pigeon program part
clingo_symbol_t args[2];
// the pigeon program part having the number of holes and pigeons as parameters
clingo_part_t parts[] = {{ "pigeon", args, sizeof(args)/sizeof(*args) }};
// parameters for the pigeon part
char const *params[] = {"h", "p"};
// create a propagator with the functions above
// using the default implementation for the model check
NULL,
NULL,
};
// user data for the propagator
propagator_t prop_data = { NULL, 0, NULL, 0 };
// set the number of holes
// set the number of pigeons
// create a control object and pass command line arguments
if (!clingo_control_new(argv+1, argc-1, NULL, NULL, 20, &ctl) != 0) { goto error; }
// register the propagator
if (!clingo_control_register_propagator(ctl, &prop, &prop_data, false)) { goto error; }
// add a logic program to the pigeon part
if (!clingo_control_add(ctl, "pigeon", params, sizeof(params)/sizeof(*params),
"1 { place(P,H) : H = 1..h } 1 :- P = 1..p.")) { goto error; }
// ground the pigeon part
if (!clingo_control_ground(ctl, parts, 1, NULL, NULL)) { goto error; }
// solve using a model callback
if (!solve(ctl, &solve_ret)) { goto error; }
goto out;
error:
if (!(error_message = clingo_error_message())) { error_message = "error"; }
printf("%s\n", error_message);
out:
// free the propagator state
if (prop_data.pigeons) { free(prop_data.pigeons); }
if (prop_data.states_size > 0) {
for (size_t i = 0; i < prop_data.states_size; ++i) {
if (prop_data.states[i].holes) {
free(prop_data.states[i].holes);
}
}
free(prop_data.states);
}
if (ctl) { clingo_control_free(ctl); }
return ret;
}
clingo_propagate_control_propagate
CLINGO_VISIBILITY_DEFAULT bool clingo_propagate_control_propagate(clingo_propagate_control_t *control, bool *result)
Propagate implied literals (resulting from added clauses).
clingo_symbol_create_number
CLINGO_VISIBILITY_DEFAULT void clingo_symbol_create_number(int number, clingo_symbol_t *symbol)
Construct a symbol representing a number.
clingo_signature_create
CLINGO_VISIBILITY_DEFAULT bool clingo_signature_create(char const *name, uint32_t arity, bool positive, clingo_signature_t *signature)
Create a new signature.
clingo_symbol_number
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_number(clingo_symbol_t symbol, int *number)
Get the number of a symbol.
clingo_solve_mode_yield
@ clingo_solve_mode_yield
Yield models in calls to clingo_solve_handle_model.
Definition: clingo.h:2288
clingo_show_type_shown
@ clingo_show_type_shown
Select shown atoms and terms.
Definition: clingo.h:2098
clingo_control_t
struct clingo_control clingo_control_t
Control object holding grounding and solving state.
Definition: clingo.h:2693
clingo_control_new
CLINGO_VISIBILITY_DEFAULT bool clingo_control_new(char const *const *arguments, size_t arguments_size, clingo_logger_t logger, void *logger_data, unsigned message_limit, clingo_control_t **control)
Create a new control object.
clingo_symbolic_atoms_literal
CLINGO_VISIBILITY_DEFAULT bool clingo_symbolic_atoms_literal(clingo_symbolic_atoms_t const *atoms, clingo_symbolic_atom_iterator_t iterator, clingo_literal_t *literal)
Returns the (numeric) aspif literal corresponding to the given symbolic atom.
clingo_symbolic_atoms_symbol
CLINGO_VISIBILITY_DEFAULT bool clingo_symbolic_atoms_symbol(clingo_symbolic_atoms_t const *atoms, clingo_symbolic_atom_iterator_t iterator, clingo_symbol_t *symbol)
Get the symbolic representation of an atom.
clingo_propagate_init_symbolic_atoms
CLINGO_VISIBILITY_DEFAULT bool clingo_propagate_init_symbolic_atoms(clingo_propagate_init_t const *init, clingo_symbolic_atoms_t const **atoms)
Get an object to inspect the symbolic atoms.
clingo_symbolic_atoms_iterator_is_equal_to
CLINGO_VISIBILITY_DEFAULT bool clingo_symbolic_atoms_iterator_is_equal_to(clingo_symbolic_atoms_t const *atoms, clingo_symbolic_atom_iterator_t a, clingo_symbolic_atom_iterator_t b, bool *equal)
Check if two iterators point to the same element (or end of the sequence).
clingo_control_free
CLINGO_VISIBILITY_DEFAULT void clingo_control_free(clingo_control_t *control)
Free a control object created with clingo_control_new().
clingo_symbolic_atoms_t
struct clingo_symbolic_atoms clingo_symbolic_atoms_t
Object to inspect symbolic atoms in a program—the relevant Herbrand base gringo uses to instantiate p...
Definition: clingo.h:542
clingo_propagate_control_t
struct clingo_propagate_control clingo_propagate_control_t
This object can be used to add clauses and propagate literals while solving.
Definition: clingo.h:1265
clingo_literal_t
int32_t clingo_literal_t
Signed integer type used for aspif and solver literals.
Definition: clingo.h:121
clingo.h
clingo_propagator_init_callback_t
bool(* clingo_propagator_init_callback_t)(clingo_propagate_init_t *, void *)
Typedef for clingo_propagator::init().
Definition: clingo.h:1347
clingo_part
Struct used to specify the program parts that have to be grounded.
Definition: clingo.h:2645
clingo_solve_handle_close
CLINGO_VISIBILITY_DEFAULT bool clingo_solve_handle_close(clingo_solve_handle_t *handle)
Stops the running search and releases the handle.
clingo_symbol_to_string
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_to_string(clingo_symbol_t symbol, char *string, size_t size)
Get the string representation of a symbol.
clingo_clause_type_learnt
@ clingo_clause_type_learnt
clause is subject to the solvers deletion policy
Definition: clingo.h:1256
clingo_propagate_init_t
struct clingo_propagate_init clingo_propagate_init_t
Object to initialize a user-defined propagator before each solving step.
Definition: clingo.h:1101
clingo_control_ground
CLINGO_VISIBILITY_DEFAULT bool clingo_control_ground(clingo_control_t *control, clingo_part_t const *parts, size_t parts_size, clingo_ground_callback_t ground_callback, void *ground_callback_data)
Ground the selected parts of the current (non-ground) logic program.
clingo_control_add
CLINGO_VISIBILITY_DEFAULT bool clingo_control_add(clingo_control_t *control, char const *name, char const *const *parameters, size_t parameters_size, char const *program)
Extend the logic program with the given non-ground logic program in string form.
clingo_solve_handle_get
CLINGO_VISIBILITY_DEFAULT bool clingo_solve_handle_get(clingo_solve_handle_t *handle, clingo_solve_result_bitset_t *result)
Get the next solve result.
clingo_control_solve
CLINGO_VISIBILITY_DEFAULT bool clingo_control_solve(clingo_control_t *control, clingo_solve_mode_bitset_t mode, clingo_literal_t const *assumptions, size_t assumptions_size, clingo_solve_event_callback_t notify, void *data, clingo_solve_handle_t **handle)
Solve the currently grounded logic program enumerating its models.
clingo_model_t
struct clingo_model clingo_model_t
Object representing a model.
Definition: clingo.h:2085
clingo_solve_handle_resume
CLINGO_VISIBILITY_DEFAULT bool clingo_solve_handle_resume(clingo_solve_handle_t *handle)
Discards the last model and starts the search for the next one.
clingo_control_register_propagator
CLINGO_VISIBILITY_DEFAULT bool clingo_control_register_propagator(clingo_control_t *control, clingo_propagator_t const *propagator, void *data, bool sequential)
Register a custom propagator with the control object.
clingo_propagate_init_solver_literal
CLINGO_VISIBILITY_DEFAULT bool clingo_propagate_init_solver_literal(clingo_propagate_init_t const *init, clingo_literal_t aspif_literal, clingo_literal_t *solver_literal)
Map the given program literal or condition id to its solver literal.
clingo_symbol_to_string_size
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_to_string_size(clingo_symbol_t symbol, size_t *size)
Get the size of the string representation of a symbol (including the terminating 0).
clingo_symbolic_atoms_end
CLINGO_VISIBILITY_DEFAULT bool clingo_symbolic_atoms_end(clingo_symbolic_atoms_t const *atoms, clingo_symbolic_atom_iterator_t *iterator)
Iterator pointing to the end of the sequence of symbolic atoms.
clingo_model_symbols_size
CLINGO_VISIBILITY_DEFAULT bool clingo_model_symbols_size(clingo_model_t const *model, clingo_show_type_bitset_t show, size_t *size)
Get the number of symbols of the selected types in the model.
clingo_symbolic_atoms_next
CLINGO_VISIBILITY_DEFAULT bool clingo_symbolic_atoms_next(clingo_symbolic_atoms_t const *atoms, clingo_symbolic_atom_iterator_t iterator, clingo_symbolic_atom_iterator_t *next)
Get an iterator to the next element in the sequence of symbolic atoms.
clingo_solve_handle_t
struct clingo_solve_handle clingo_solve_handle_t
Search handle to a solve call.
Definition: clingo.h:2322
clingo_solve_result_bitset_t
unsigned clingo_solve_result_bitset_t
Definition: clingo.h:2248
clingo_solve_handle_model
CLINGO_VISIBILITY_DEFAULT bool clingo_solve_handle_model(clingo_solve_handle_t *handle, clingo_model_t const **model)
Get the next model (or zero if there are no more models).
clingo_propagator
An instance of this struct has to be registered with a solver to implement a custom propagator.
Definition: clingo.h:1362
clingo_propagate_control_add_clause
CLINGO_VISIBILITY_DEFAULT bool clingo_propagate_control_add_clause(clingo_propagate_control_t *control, clingo_literal_t const *clause, size_t size, clingo_clause_type_t type, bool *result)
Add the given clause to the solver.
clingo_error_runtime
@ clingo_error_runtime
errors only detectable at runtime like invalid input
Definition: clingo.h:143
clingo_error_code
CLINGO_VISIBILITY_DEFAULT clingo_error_t clingo_error_code()
Get the last error code set by a clingo API call.
clingo_error_message
CLINGO_VISIBILITY_DEFAULT const char * clingo_error_message()
Get the last error message set if an API call fails.
clingo_symbol_t
uint64_t clingo_symbol_t
Represents a symbol.
Definition: clingo.h:330
clingo_propagator_undo_callback_t
void(* clingo_propagator_undo_callback_t)(clingo_propagate_control_t const *, clingo_literal_t const *, size_t, void *)
Typedef for clingo_propagator::undo().
Definition: clingo.h:1353
clingo_propagator_propagate_callback_t
bool(* clingo_propagator_propagate_callback_t)(clingo_propagate_control_t *, clingo_literal_t const *, size_t, void *)
Typedef for clingo_propagator::propagate().
Definition: clingo.h:1350
clingo_propagate_control_thread_id
CLINGO_VISIBILITY_DEFAULT clingo_id_t clingo_propagate_control_thread_id(clingo_propagate_control_t const *control)
Get the id of the underlying solver thread.
clingo_propagate_init_add_watch
CLINGO_VISIBILITY_DEFAULT bool clingo_propagate_init_add_watch(clingo_propagate_init_t *init, clingo_literal_t solver_literal)
Add a watch for the solver literal in the given phase.
clingo_symbolic_atom_iterator_t
uint64_t clingo_symbolic_atom_iterator_t
Object to iterate over symbolic atoms.
Definition: clingo.h:552
clingo_error_bad_alloc
@ clingo_error_bad_alloc
memory could not be allocated
Definition: clingo.h:145
clingo_propagate_init_number_of_threads
CLINGO_VISIBILITY_DEFAULT int clingo_propagate_init_number_of_threads(clingo_propagate_init_t const *init)
Get the number of threads used in subsequent solving.
clingo_symbolic_atoms_begin
CLINGO_VISIBILITY_DEFAULT bool clingo_symbolic_atoms_begin(clingo_symbolic_atoms_t const *atoms, clingo_signature_t const *signature, clingo_symbolic_atom_iterator_t *iterator)
Get a forward iterator to the beginning of the sequence of all symbolic atoms optionally restricted t...
clingo_set_error
CLINGO_VISIBILITY_DEFAULT void clingo_set_error(clingo_error_t code, char const *message)
Set a custom error code and message in the active thread.
clingo_model_symbols
CLINGO_VISIBILITY_DEFAULT bool clingo_model_symbols(clingo_model_t const *model, clingo_show_type_bitset_t show, clingo_symbol_t *symbols, size_t size)
Get the symbols of the selected types in the model.
clingo_symbol_arguments
CLINGO_VISIBILITY_DEFAULT bool clingo_symbol_arguments(clingo_symbol_t symbol, clingo_symbol_t const **arguments, size_t *arguments_size)
Get the arguments of a symbol.
clingo_signature_t
uint64_t clingo_signature_t
Represents a predicate signature.
Definition: clingo.h:255