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:
The output is empty because the pigeon hole problem is unsatisfiable.
#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;
}
}
return true;
}
(void)data;
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");
*goon = true;
goto out;
error:
ret = false;
out:
if (atoms) { free(atoms); }
if (str) { free(str); }
return ret;
}
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"};
(bool (*) (clingo_propagate_control_t *, clingo_literal_t const *, size_t, void *))undo,
NULL
};
propagator_t prop_data = { NULL, 0, NULL, 0 };
"1 { place(P,H) : H = 1..h } 1 :- P = 1..p.")) { 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;
}