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:
The output is empty because the pigeonhole problem is unsatisfiable.
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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 false;
}
return false;
}
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;
return false;
}
return false;
}
return false;
}
for (int pass = 0; pass < 2; ++pass) {
return false;
}
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;
return false;
}
if (equal) {
break;
}
return false;
}
return false;
}
if (pass == 0) {
assert(lit > 0);
if (lit > max) {
max = lit;
}
} else {
return false;
}
if (!get_arg(sym, 1, &h)) {
return false;
}
data->pigeons[lit] = h;
return false;
}
if (h + 1 > holes) {
holes = h + 1;
}
}
return false;
}
}
}
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;
return false;
}
if (!result) {
return true;
}
return false;
}
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;
}
goto error;
}
goto error;
}
printf("Model:");
for (it = atoms, ie = atoms + atoms_n; it != ie; ++it) {
size_t n;
char *str_new;
goto error;
}
if (str_n < n) {
if (!(str_new = (char *)realloc(str, sizeof(*str) * n))) {
goto error;
}
str = str_new;
str_n = 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;
goto error;
}
while (true) {
goto error;
}
goto error;
}
if (model) {
print_model(model);
}
else {
break;
}
}
goto error;
}
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};
goto error;
}
goto error;
}
"1 { place(P,H) : H = 1..h } 1 :- P = 1..p.")) {
goto error;
}
goto error;
}
if (!solve(ctl, &solve_ret)) {
goto error;
}
goto out;
error:
error_message = "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);
}
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