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

The example shows how to write a simple propagator for the pigeonhole problem.

The example shows how to write a simple propagator for the pigeonhole 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 pigeonhole problem is unsatisfiable.

Code

#include <assert.h>
#include <clingo.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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
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
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),
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
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
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) {
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) {
}
return ret;
}
Single header containing the whole clingo API.
CLINGO_VISIBILITY_DEFAULT char const * clingo_error_message()
Get the last error message set if an API call fails.
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.
int32_t clingo_literal_t
Signed integer type used for aspif and solver literals.
Definition clingo.h:121
CLINGO_VISIBILITY_DEFAULT clingo_error_t clingo_error_code()
Get the last error code set by a clingo API call.
@ clingo_error_bad_alloc
memory could not be allocated
Definition clingo.h:144
@ clingo_error_runtime
errors only detectable at runtime like invalid input
Definition clingo.h:142
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_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.
unsigned clingo_solve_result_bitset_t
Corresponding type to clingo_solve_result_e.
Definition clingo.h:2488
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_VISIBILITY_DEFAULT void clingo_control_free(clingo_control_t *control)
Free a control object created with clingo_control_new().
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.
struct clingo_control clingo_control_t
Control object holding grounding and solving state.
Definition clingo.h:2973
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_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_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.
struct clingo_model clingo_model_t
Object representing a model.
Definition clingo.h:2280
@ clingo_show_type_shown
Select shown atoms and terms.
Definition clingo.h:2293
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_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.
struct clingo_propagate_control clingo_propagate_control_t
This object can be used to add clauses and propagate literals while solving.
Definition clingo.h:1373
CLINGO_VISIBILITY_DEFAULT bool clingo_propagate_control_propagate(clingo_propagate_control_t *control, bool *result)
Propagate implied literals (resulting from added clauses).
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.
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:1472
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.
struct clingo_propagate_init clingo_propagate_init_t
Object to initialize a user-defined propagator before each solving step.
Definition clingo.h:1169
bool(* clingo_propagator_init_callback_t)(clingo_propagate_init_t *, void *)
Typedef for clingo_propagator::init().
Definition clingo.h:1465
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_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.
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:1468
@ clingo_clause_type_learnt
clause is subject to the solvers deletion policy
Definition clingo.h:1362
struct clingo_solve_handle clingo_solve_handle_t
Search handle to a solve call.
Definition clingo.h:2564
CLINGO_VISIBILITY_DEFAULT bool clingo_solve_handle_close(clingo_solve_handle_t *handle)
Stops the running search and releases the handle.
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_VISIBILITY_DEFAULT bool clingo_solve_handle_get(clingo_solve_handle_t *handle, clingo_solve_result_bitset_t *result)
Get the next solve result.
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_solve_mode_yield
Yield models in calls to clingo_solve_handle_model.
Definition clingo.h:2528
uint64_t clingo_symbolic_atom_iterator_t
Object to iterate over symbolic atoms.
Definition clingo.h:556
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).
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:546
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_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_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_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_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_VISIBILITY_DEFAULT bool clingo_symbol_number(clingo_symbol_t symbol, int *number)
Get the number of a symbol.
uint64_t clingo_signature_t
Represents a predicate signature.
Definition clingo.h:254
CLINGO_VISIBILITY_DEFAULT bool clingo_signature_create(char const *name, uint32_t arity, bool positive, clingo_signature_t *signature)
Create a new signature.
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).
uint64_t clingo_symbol_t
Represents a symbol.
Definition clingo.h:330
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_VISIBILITY_DEFAULT bool clingo_symbol_to_string(clingo_symbol_t symbol, char *string, size_t size)
Get the string representation of a symbol.
CLINGO_VISIBILITY_DEFAULT void clingo_symbol_create_number(int number, clingo_symbol_t *symbol)
Construct a symbol representing a number.
Struct used to specify the program parts that have to be grounded.
Definition clingo.h:2908
An instance of this struct has to be registered with a solver to implement a custom propagator.
Definition clingo.h:1482