Clingo C API
C API for clingo providing high level functions to control grounding and solving.
|
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 <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
typedef struct {
size_t size;
} state_t;
typedef struct {
int *pigeons;
size_t pigeons_size;
state_t *states;
size_t states_size;
} propagator_t;
size_t args_size;
return true;
}
int holes = 0;
if (data->states != NULL) {
if (threads > data->states_size) {
}
return true;
}
if (!(data->states = (state_t*)malloc(sizeof(*data->states) * threads))) {
return false;
}
memset(data->states, 0, sizeof(*data->states) * threads);
data->states_size = threads;
for (int pass = 0; pass < 2; ++pass) {
if (pass == 1) {
if (!(data->pigeons = (int*)malloc(sizeof(*data->pigeons) * (max + 1)))) {
return false;
}
data->pigeons_size = max + 1;
}
while (true) {
int h;
bool equal;
if (equal) { break; }
if (pass == 0) {
assert(lit > 0);
if (lit > max) { max = lit; }
}
else {
if (!get_arg(sym, 1, &h)) { return false; }
data->pigeons[lit] = h;
if (h + 1 > holes) { holes = h + 1; }
}
}
}
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;
}
memset(data->states[i].holes, 0, sizeof(*data->states[i].holes) * holes);
data->states[i].size = holes;
}
return true;
}
for (size_t i = 0; i < size; ++i) {
if (*prev == 0) { *prev = lit; }
else {
bool result;
if (!result) { return true; }
if (!result) { return true; }
assert(false);
}
}
return true;
}
for (size_t i = 0; i < size; ++i) {
int hole = data->pigeons[lit];
if (state.holes[hole] == lit) {
state.holes[hole] = 0;
}
}
}
bool ret = true;
size_t atoms_n;
char *str = NULL;
size_t str_n = 0;
goto error;
}
printf("Model:");
for (it = atoms, ie = atoms + atoms_n; it != ie; ++it) {
size_t n;
char *str_new;
if (str_n < n) {
if (!(str_new = (char*)realloc(str, sizeof(*str) * n))) {
goto error;
}
str = str_new;
str_n = n;
}
printf(" %s", str);
}
printf("\n");
goto out;
error:
ret = false;
out:
if (atoms) { free(atoms); }
if (str) { free(str); }
return ret;
}
bool ret = true;
while (true) {
if (model) { print_model(model); }
else { break; }
}
goto out;
error:
ret = false;
out:
}
int main(int argc, char const **argv) {
char const *error_message;
int ret = 0;
clingo_part_t parts[] = {{
"pigeon", args,
sizeof(args)/
sizeof(*args) }};
char const *params[] = {"h", "p"};
NULL,
NULL,
};
propagator_t prop_data = { NULL, 0, NULL, 0 };
"1 { place(P,H) : H = 1..h } 1 :- P = 1..p.")) { goto error; }
if (!solve(ctl, &solve_ret)) { goto error; }
goto out;
error:
printf("%s\n", error_message);
out:
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);
}
return ret;
}
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 void clingo_symbol_create_number(int number, clingo_symbol_t *symbol)
Construct a symbol representing a number.
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_number(clingo_symbol_t symbol, int *number)
Get the number of a symbol.
@ clingo_solve_mode_yield
Yield models in calls to clingo_solve_handle_model.
Definition: clingo.h:2288
@ clingo_show_type_shown
Select shown atoms and terms.
Definition: clingo.h:2098
struct clingo_control clingo_control_t
Control object holding grounding and solving state.
Definition: clingo.h:2693
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 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_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_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_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_VISIBILITY_DEFAULT void clingo_control_free(clingo_control_t *control)
Free a control object created with clingo_control_new().
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
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
int32_t clingo_literal_t
Signed integer type used for aspif and solver literals.
Definition: clingo.h:121
bool(* clingo_propagator_init_callback_t)(clingo_propagate_init_t *, void *)
Typedef for clingo_propagator::init().
Definition: clingo.h:1347
Struct used to specify the program parts that have to be grounded.
Definition: clingo.h:2645
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_symbol_to_string(clingo_symbol_t symbol, char *string, size_t size)
Get the string representation of a symbol.
@ clingo_clause_type_learnt
clause is subject to the solvers deletion policy
Definition: clingo.h:1256
struct clingo_propagate_init clingo_propagate_init_t
Object to initialize a user-defined propagator before each solving step.
Definition: clingo.h:1101
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_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_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_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.
struct clingo_model clingo_model_t
Object representing a model.
Definition: clingo.h:2085
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_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_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_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_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_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_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.
struct clingo_solve_handle clingo_solve_handle_t
Search handle to a solve call.
Definition: clingo.h:2322
unsigned clingo_solve_result_bitset_t
Definition: clingo.h:2248
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).
An instance of this struct has to be registered with a solver to implement a custom propagator.
Definition: clingo.h:1362
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
errors only detectable at runtime like invalid input
Definition: clingo.h:143
CLINGO_VISIBILITY_DEFAULT clingo_error_t clingo_error_code()
Get the last error code set by a clingo API call.
CLINGO_VISIBILITY_DEFAULT const char * clingo_error_message()
Get the last error message set if an API call fails.
uint64_t clingo_symbol_t
Represents a symbol.
Definition: clingo.h:330
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
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_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_add_watch(clingo_propagate_init_t *init, clingo_literal_t solver_literal)
Add a watch for the solver literal in the given phase.
uint64_t clingo_symbolic_atom_iterator_t
Object to iterate over symbolic atoms.
Definition: clingo.h:552
@ clingo_error_bad_alloc
memory could not be allocated
Definition: clingo.h:145
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_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 void clingo_set_error(clingo_error_t code, char const *message)
Set a custom error code and message in the active thread.
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_symbol_arguments(clingo_symbol_t symbol, clingo_symbol_t const **arguments, size_t *arguments_size)
Get the arguments of a symbol.
uint64_t clingo_signature_t
Represents a predicate signature.
Definition: clingo.h:255