forked from potassco/potassco.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpropagator_8c-example.html
83 lines (81 loc) · 41.3 KB
/
propagator_8c-example.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
---
layout: plain
---
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Clingo C API: propagator.c</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="clingo.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">Clingo C API
</div>
<div id="projectbrief">C API for clingo providing high level functions to control grounding and solving.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
initMenu('',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="headertitle">
<div class="title">propagator.c</div> </div>
</div><!--header-->
<div class="contents">
<p>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:</p>
<p><a href="{{ site.publicationurl }}/#DBLP:conf/iclp/GebserKKOSW16x">{{ site.publicationurl }}/#DBLP:conf/iclp/GebserKKOSW16x</a></p>
<h2>Output</h2>
<p>The output is empty because the pigeon hole problem is unsatisfiable.</p>
<h2>Code</h2>
<div class="fragment"><div class="line"><span class="preprocessor">#include <<a class="code" href="clingo_8h.html">clingo.h</a>></span></div><div class="line"><span class="preprocessor">#include <stdlib.h></span></div><div class="line"><span class="preprocessor">#include <stdio.h></span></div><div class="line"><span class="preprocessor">#include <string.h></span></div><div class="line"><span class="preprocessor">#include <assert.h></span></div><div class="line"></div><div class="line"><span class="comment">// state information for individual solving threads</span></div><div class="line"><span class="keyword">typedef</span> <span class="keyword">struct </span>{</div><div class="line"> <span class="comment">// assignment of pigeons to holes</span></div><div class="line"> <span class="comment">// (hole number -> pigeon placement literal or zero)</span></div><div class="line"> <a class="code" href="group__BasicTypes.html#gaa95dd19334e536397bbad174c8fa4ff8">clingo_literal_t</a> *holes;</div><div class="line"> <span class="keywordtype">size_t</span> size;</div><div class="line">} state_t;</div><div class="line"></div><div class="line"><span class="comment">// state information for the propagator</span></div><div class="line"><span class="keyword">typedef</span> <span class="keyword">struct </span>{</div><div class="line"> <span class="comment">// mapping from solver literals capturing pigeon placements to hole numbers</span></div><div class="line"> <span class="comment">// (solver literal -> hole number or zero)</span></div><div class="line"> <span class="keywordtype">int</span> *pigeons;</div><div class="line"> <span class="keywordtype">size_t</span> pigeons_size;</div><div class="line"> <span class="comment">// array of states</span></div><div class="line"> state_t *states;</div><div class="line"> <span class="keywordtype">size_t</span> states_size;</div><div class="line">} propagator_t;</div><div class="line"></div><div class="line"><span class="comment">// returns the offset'th numeric argument of the function symbol sym</span></div><div class="line"><span class="keywordtype">bool</span> get_arg(<a class="code" href="group__Symbols.html#ga6c75c60fa57c3b97505265ff08f6f951">clingo_symbol_t</a> sym, <span class="keywordtype">int</span> offset, <span class="keywordtype">int</span> *num) {</div><div class="line"> <a class="code" href="group__Symbols.html#ga6c75c60fa57c3b97505265ff08f6f951">clingo_symbol_t</a> <span class="keyword">const</span> *args;</div><div class="line"> <span class="keywordtype">size_t</span> args_size;</div><div class="line"></div><div class="line"> <span class="comment">// get the arguments of the function symbol</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a0"></a><a class="code" href="group__Symbols.html#gaa3cb5dc221406dddf849e167d4efa43a">clingo_symbol_arguments</a>(sym, &args, &args_size)) { <span class="keywordflow">return</span> <span class="keyword">false</span>; }</div><div class="line"> <span class="comment">// get the requested numeric argument</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a1"></a><a class="code" href="group__Symbols.html#ga1be9e455607b75d3381edd26b4777435">clingo_symbol_number</a>(args[offset], num)) { <span class="keywordflow">return</span> <span class="keyword">false</span>; }</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">bool</span> init(<a class="code" href="group__Propagator.html#ga640db8ba73adaddd5dbe50b572b77f25">clingo_propagate_init_t</a> *init, propagator_t *data) {</div><div class="line"> <span class="comment">// the total number of holes pigeons can be assigned too</span></div><div class="line"> <span class="keywordtype">int</span> holes = 0;</div><div class="line"> <span class="keywordtype">size_t</span> threads = <a name="a2"></a><a class="code" href="group__Propagator.html#gaf1c203c2a09e8a9da37b87c7bb0378be">clingo_propagate_init_number_of_threads</a>(init);</div><div class="line"> <span class="comment">// stores the (numeric) maximum of the solver literals capturing pigeon placements</span></div><div class="line"> <span class="comment">// note that the code below assumes that this literal is not negative</span></div><div class="line"> <span class="comment">// which holds for the pigeon problem but not in general</span></div><div class="line"> <a class="code" href="group__BasicTypes.html#gaa95dd19334e536397bbad174c8fa4ff8">clingo_literal_t</a> max = 0;</div><div class="line"> <a class="code" href="group__SymbolicAtoms.html#ga48f4026c13a49553efce213e76ab32aa">clingo_symbolic_atoms_t</a> *atoms;</div><div class="line"> <a class="code" href="group__Symbols.html#ga3485f06e63e12f967ef6420e132f7376">clingo_signature_t</a> sig;</div><div class="line"> <a class="code" href="group__SymbolicAtoms.html#ga0a773e6f798cb2395d0cdfab5b277ca2">clingo_symbolic_atom_iterator_t</a> atoms_it, atoms_ie;</div><div class="line"> <span class="comment">// ensure that solve can be called multiple times</span></div><div class="line"> <span class="comment">// for simplicity, the case that additional holes or pigeons to assign are grounded is not handled here</span></div><div class="line"> <span class="keywordflow">if</span> (data->states != NULL) {</div><div class="line"> <span class="comment">// in principle the number of threads can increase between solve calls by changing the configuration</span></div><div class="line"> <span class="comment">// this case is not handled (elegantly) here</span></div><div class="line"> <span class="keywordflow">if</span> (threads > data->states_size) {</div><div class="line"> <a name="a3"></a><a class="code" href="group__BasicTypes.html#ga5c2b2943475239e151eb783d30548b38">clingo_set_error</a>(<a name="a4"></a><a class="code" href="group__BasicTypes.html#gga5adba1f5e366e7489ac4a3fb5266dba6af43eebc9503fef8d1b2b85f99bb63221">clingo_error_runtime</a>, <span class="stringliteral">"more threads than states"</span>);</div><div class="line"> }</div><div class="line"> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div><div class="line"> }</div><div class="line"> <span class="comment">// allocate memory for exactly one state per thread</span></div><div class="line"> <span class="keywordflow">if</span> (!(data->states = (state_t*)malloc(<span class="keyword">sizeof</span>(*data->states) * threads))) {</div><div class="line"> <a class="code" href="group__BasicTypes.html#ga5c2b2943475239e151eb783d30548b38">clingo_set_error</a>(<a name="a5"></a><a class="code" href="group__BasicTypes.html#gga5adba1f5e366e7489ac4a3fb5266dba6aa14a0926eb3e653fcc13299b33d8d348">clingo_error_bad_alloc</a>, <span class="stringliteral">"allocation failed"</span>);</div><div class="line"> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div><div class="line"> }</div><div class="line"> memset(data->states, 0, <span class="keyword">sizeof</span>(*data->states) * threads);</div><div class="line"> data->states_size = threads;</div><div class="line"></div><div class="line"> <span class="comment">// the propagator monitors place/2 atoms and dectects conflicting assignments</span></div><div class="line"> <span class="comment">// first get the symbolic atoms handle</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a6"></a><a class="code" href="group__Propagator.html#ga7f5f0a59fe5db93356598918878a9605">clingo_propagate_init_symbolic_atoms</a>(init, &atoms)) { <span class="keywordflow">return</span> <span class="keyword">false</span>; }</div><div class="line"> <span class="comment">// create place/2 signature to filter symbolic atoms with</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a7"></a><a class="code" href="group__Symbols.html#ga512e995fab2f2dfd440814777fc90247">clingo_signature_create</a>(<span class="stringliteral">"place"</span>, 2, <span class="keyword">true</span>, &sig)) { <span class="keywordflow">return</span> <span class="keyword">false</span>; }</div><div class="line"> <span class="comment">// get an iterator after the last place/2 atom</span></div><div class="line"> <span class="comment">// (atom order corresponds to grounding order (and is unpredictable))</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a8"></a><a class="code" href="group__SymbolicAtoms.html#ga48994504ec789ee1725a9761ddbb7dec">clingo_symbolic_atoms_end</a>(atoms, &atoms_ie)) { <span class="keywordflow">return</span> <span class="keyword">false</span>; }</div><div class="line"></div><div class="line"> <span class="comment">// loop over the place/2 atoms in two passes</span></div><div class="line"> <span class="comment">// the first pass determines the maximum placement literal</span></div><div class="line"> <span class="comment">// the second pass allocates memory for data structures based on the first pass</span></div><div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> pass = 0; pass < 2; ++pass) {</div><div class="line"> <span class="comment">// get an iterator to the first place/2 atom</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a9"></a><a class="code" href="group__SymbolicAtoms.html#ga6b2d6c33ad725bd86dd417574ab63aa3">clingo_symbolic_atoms_begin</a>(atoms, &sig, &atoms_it)) { <span class="keywordflow">return</span> <span class="keyword">false</span>; }</div><div class="line"> <span class="keywordflow">if</span> (pass == 1) {</div><div class="line"> <span class="comment">// allocate memory for the assignemnt literal -> hole mapping</span></div><div class="line"> <span class="keywordflow">if</span> (!(data->pigeons = (<span class="keywordtype">int</span>*)malloc(<span class="keyword">sizeof</span>(*data->pigeons) * (max + 1)))) {</div><div class="line"> <a class="code" href="group__BasicTypes.html#ga5c2b2943475239e151eb783d30548b38">clingo_set_error</a>(<a class="code" href="group__BasicTypes.html#gga5adba1f5e366e7489ac4a3fb5266dba6aa14a0926eb3e653fcc13299b33d8d348">clingo_error_bad_alloc</a>, <span class="stringliteral">"allocation failed"</span>);</div><div class="line"> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div><div class="line"> }</div><div class="line"> data->pigeons_size = max + 1;</div><div class="line"> }</div><div class="line"> <span class="keywordflow">while</span> (<span class="keyword">true</span>) {</div><div class="line"> <span class="keywordtype">int</span> h;</div><div class="line"> <span class="keywordtype">bool</span> equal;</div><div class="line"> <a class="code" href="group__BasicTypes.html#gaa95dd19334e536397bbad174c8fa4ff8">clingo_literal_t</a> lit;</div><div class="line"> <a class="code" href="group__Symbols.html#ga6c75c60fa57c3b97505265ff08f6f951">clingo_symbol_t</a> sym;</div><div class="line"></div><div class="line"> <span class="comment">// stop iteration if the end is reached</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a10"></a><a class="code" href="group__SymbolicAtoms.html#ga47a855856f74608dd0a28234060b2cbc">clingo_symbolic_atoms_iterator_is_equal_to</a>(atoms, atoms_it, atoms_ie, &equal)) { <span class="keywordflow">return</span> <span class="keyword">false</span>; }</div><div class="line"> <span class="keywordflow">if</span> (equal) { <span class="keywordflow">break</span>; }</div><div class="line"></div><div class="line"> <span class="comment">// get the solver literal for the placement atom</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a11"></a><a class="code" href="group__SymbolicAtoms.html#gaa11e6e579cbd4c51f2551373c9700459">clingo_symbolic_atoms_literal</a>(atoms, atoms_it, &lit)) { <span class="keywordflow">return</span> <span class="keyword">false</span>; }</div><div class="line"> <span class="keywordflow">if</span> (!<a name="a12"></a><a class="code" href="group__Propagator.html#ga24ffe5c89ab1629271e53be60c7cff9d">clingo_propagate_init_solver_literal</a>(init, lit, &lit)) { <span class="keywordflow">return</span> <span class="keyword">false</span>; }</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (pass == 0) {</div><div class="line"> <span class="comment">// determine the maximum literal</span></div><div class="line"> assert(lit > 0);</div><div class="line"> <span class="keywordflow">if</span> (lit > max) { max = lit; }</div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span> {</div><div class="line"> <span class="comment">// extract the hole number from the atom</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a13"></a><a class="code" href="group__SymbolicAtoms.html#ga6005aad96aaa2f63efc4d7fd05369dda">clingo_symbolic_atoms_symbol</a>(atoms, atoms_it, &sym)) { <span class="keywordflow">return</span> <span class="keyword">false</span>; }</div><div class="line"> <span class="keywordflow">if</span> (!get_arg(sym, 1, &h)) { <span class="keywordflow">return</span> <span class="keyword">false</span>; }</div><div class="line"></div><div class="line"> <span class="comment">// initialize the assignemnt literal -> hole mapping</span></div><div class="line"> data->pigeons[lit] = h;</div><div class="line"></div><div class="line"> <span class="comment">// watch the assignment literal</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a14"></a><a class="code" href="group__Propagator.html#gab4a0f0767529ad000f5d80b56b74104f">clingo_propagate_init_add_watch</a>(init, lit)) { <span class="keywordflow">return</span> <span class="keyword">false</span>; }</div><div class="line"></div><div class="line"> <span class="comment">// update the total number of holes</span></div><div class="line"> <span class="keywordflow">if</span> (h + 1 > holes) { holes = h + 1; }</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">// advance to the next placement atom</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a15"></a><a class="code" href="group__SymbolicAtoms.html#gaaf06576071eec031638dc609d205b8bd">clingo_symbolic_atoms_next</a>(atoms, atoms_it, &atoms_it)) { <span class="keywordflow">return</span> <span class="keyword">false</span>; }</div><div class="line"> }</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">// initialize the per solver thread state information</span></div><div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i < threads; ++i) {</div><div class="line"> <span class="keywordflow">if</span> (!(data->states[i].holes = (<a class="code" href="group__BasicTypes.html#gaa95dd19334e536397bbad174c8fa4ff8">clingo_literal_t</a>*)malloc(<span class="keyword">sizeof</span>(*data->states[i].holes) * holes))) {</div><div class="line"> <a class="code" href="group__BasicTypes.html#ga5c2b2943475239e151eb783d30548b38">clingo_set_error</a>(<a class="code" href="group__BasicTypes.html#gga5adba1f5e366e7489ac4a3fb5266dba6aa14a0926eb3e653fcc13299b33d8d348">clingo_error_bad_alloc</a>, <span class="stringliteral">"allocation failed"</span>);</div><div class="line"> <span class="keywordflow">return</span> <span class="keyword">false</span>;</div><div class="line"> }</div><div class="line"> <span class="comment">// initially no pigeons are assigned to any holes</span></div><div class="line"> <span class="comment">// so the hole -> literal mapping is initialized with zero</span></div><div class="line"> <span class="comment">// which is not a valid literal</span></div><div class="line"> memset(data->states[i].holes, 0, <span class="keyword">sizeof</span>(*data->states[i].holes) * holes);</div><div class="line"> data->states[i].size = holes;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">bool</span> propagate(<a class="code" href="group__Propagator.html#ga1cf43bea08101abb8b8cc7711c4c7165">clingo_propagate_control_t</a> *control, <span class="keyword">const</span> <a class="code" href="group__BasicTypes.html#gaa95dd19334e536397bbad174c8fa4ff8">clingo_literal_t</a> *changes, <span class="keywordtype">size_t</span> size, propagator_t *data) {</div><div class="line"> <span class="comment">// get the thread specific state</span></div><div class="line"> state_t state = data->states[<a name="a16"></a><a class="code" href="group__Propagator.html#gafc162fc6dd12dbbf1c39c0562e3533c3">clingo_propagate_control_thread_id</a>(control)];</div><div class="line"></div><div class="line"> <span class="comment">// apply and check the pigeon assignments done by the solver</span></div><div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i < size; ++i) {</div><div class="line"> <span class="comment">// the freshly assigned literal</span></div><div class="line"> <a class="code" href="group__BasicTypes.html#gaa95dd19334e536397bbad174c8fa4ff8">clingo_literal_t</a> lit = changes[i];</div><div class="line"> <span class="comment">// a pointer to the previously assigned literal</span></div><div class="line"> <a class="code" href="group__BasicTypes.html#gaa95dd19334e536397bbad174c8fa4ff8">clingo_literal_t</a> *prev = state.holes + data->pigeons[lit];</div><div class="line"></div><div class="line"> <span class="comment">// update the placement if no literal was assigned previously</span></div><div class="line"> <span class="keywordflow">if</span> (*prev == 0) { *prev = lit; }</div><div class="line"> <span class="comment">// create a conflicting clause and propagate it</span></div><div class="line"> <span class="keywordflow">else</span> {</div><div class="line"> <span class="comment">// current and previous literal must not hold together</span></div><div class="line"> <a class="code" href="group__BasicTypes.html#gaa95dd19334e536397bbad174c8fa4ff8">clingo_literal_t</a> clause[] = { -lit, -*prev };</div><div class="line"> <span class="comment">// stores the result when adding a clause or propagationg</span></div><div class="line"> <span class="comment">// if result is false propagation must stop for the solver to backtrack</span></div><div class="line"> <span class="keywordtype">bool</span> result;</div><div class="line"></div><div class="line"> <span class="comment">// add the clause</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a17"></a><a class="code" href="group__Propagator.html#ga45c65882cf55edcf8782f2ce13d66e2b">clingo_propagate_control_add_clause</a>(control, clause, <span class="keyword">sizeof</span>(clause)/<span class="keyword">sizeof</span>(*clause), <a name="a18"></a><a class="code" href="group__Propagator.html#ggab158fba808ea7d1eee7f955858aca36ca35c9cfe61e2130b0292866860218ee58">clingo_clause_type_learnt</a>, &result)) { <span class="keywordflow">return</span> <span class="keyword">false</span>; }</div><div class="line"> <span class="keywordflow">if</span> (!result) { <span class="keywordflow">return</span> <span class="keyword">true</span>; }</div><div class="line"></div><div class="line"> <span class="comment">// propagate it</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a19"></a><a class="code" href="group__Propagator.html#ga1e5504a8332ed8c453197046d201cd4b">clingo_propagate_control_propagate</a>(control, &result)) { <span class="keywordflow">return</span> <span class="keyword">false</span>; }</div><div class="line"> <span class="keywordflow">if</span> (!result) { <span class="keywordflow">return</span> <span class="keyword">true</span>; }</div><div class="line"></div><div class="line"> <span class="comment">// must not happen because the clause above is conflicting by construction</span></div><div class="line"> assert(<span class="keyword">false</span>);</div><div class="line"> }</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">bool</span> undo(<a class="code" href="group__Propagator.html#ga1cf43bea08101abb8b8cc7711c4c7165">clingo_propagate_control_t</a> *control, <span class="keyword">const</span> <a class="code" href="group__BasicTypes.html#gaa95dd19334e536397bbad174c8fa4ff8">clingo_literal_t</a> *changes, <span class="keywordtype">size_t</span> size, propagator_t *data) {</div><div class="line"> <span class="comment">// get the thread specific state</span></div><div class="line"> state_t state = data->states[<a class="code" href="group__Propagator.html#gafc162fc6dd12dbbf1c39c0562e3533c3">clingo_propagate_control_thread_id</a>(control)];</div><div class="line"></div><div class="line"> <span class="comment">// undo the assignments made in propagate</span></div><div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i < size; ++i) {</div><div class="line"> <a class="code" href="group__BasicTypes.html#gaa95dd19334e536397bbad174c8fa4ff8">clingo_literal_t</a> lit = changes[i];</div><div class="line"> <span class="keywordtype">int</span> hole = data->pigeons[lit];</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (state.holes[hole] == lit) {</div><div class="line"> <span class="comment">// undo the assignment</span></div><div class="line"> state.holes[hole] = 0;</div><div class="line"> }</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> <span class="keyword">true</span>;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">bool</span> print_model(<a class="code" href="group__Model.html#gaaf9a93819f023f3cb8aa80598c46556b">clingo_model_t</a> *model) {</div><div class="line"> <span class="keywordtype">bool</span> ret = <span class="keyword">true</span>;</div><div class="line"> <a class="code" href="group__Symbols.html#ga6c75c60fa57c3b97505265ff08f6f951">clingo_symbol_t</a> *atoms = NULL;</div><div class="line"> <span class="keywordtype">size_t</span> atoms_n;</div><div class="line"> <a class="code" href="group__Symbols.html#ga6c75c60fa57c3b97505265ff08f6f951">clingo_symbol_t</a> <span class="keyword">const</span> *it, *ie;</div><div class="line"> <span class="keywordtype">char</span> *str = NULL;</div><div class="line"> <span class="keywordtype">size_t</span> str_n = 0;</div><div class="line"></div><div class="line"> <span class="comment">// determine the number of (shown) symbols in the model</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a20"></a><a class="code" href="group__Model.html#ga19a05934acb060994eed7cd079d1a0fb">clingo_model_symbols_size</a>(model, <a name="a21"></a><a class="code" href="group__Model.html#ggaa943fe81f0874ea63820b627bc19845ca82a698ef41cd919cb370d22fc0b44826">clingo_show_type_shown</a>, &atoms_n)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> <span class="comment">// allocate required memory to hold all the symbols</span></div><div class="line"> <span class="keywordflow">if</span> (!(atoms = (<a class="code" href="group__Symbols.html#ga6c75c60fa57c3b97505265ff08f6f951">clingo_symbol_t</a>*)malloc(<span class="keyword">sizeof</span>(*atoms) * atoms_n))) {</div><div class="line"> <a class="code" href="group__BasicTypes.html#ga5c2b2943475239e151eb783d30548b38">clingo_set_error</a>(<a class="code" href="group__BasicTypes.html#gga5adba1f5e366e7489ac4a3fb5266dba6aa14a0926eb3e653fcc13299b33d8d348">clingo_error_bad_alloc</a>, <span class="stringliteral">"could not allocate memory for atoms"</span>);</div><div class="line"> <span class="keywordflow">goto</span> error;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">// retrieve the symbols in the model</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a22"></a><a class="code" href="group__Model.html#ga7ffb82aac782e7c623a2009b76830cdd">clingo_model_symbols</a>(model, <a class="code" href="group__Model.html#ggaa943fe81f0874ea63820b627bc19845ca82a698ef41cd919cb370d22fc0b44826">clingo_show_type_shown</a>, atoms, atoms_n)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> printf(<span class="stringliteral">"Model:"</span>);</div><div class="line"></div><div class="line"> <span class="keywordflow">for</span> (it = atoms, ie = atoms + atoms_n; it != ie; ++it) {</div><div class="line"> <span class="keywordtype">size_t</span> n;</div><div class="line"> <span class="keywordtype">char</span> *str_new;</div><div class="line"></div><div class="line"> <span class="comment">// determine size of the string representation of the next symbol in the model</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a23"></a><a class="code" href="group__Symbols.html#ga6963e11f6d22a5c04a0e50d1afe1f4f5">clingo_symbol_to_string_size</a>(*it, &n)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (str_n < n) {</div><div class="line"> <span class="comment">// allocate required memory to hold the symbol's string</span></div><div class="line"> <span class="keywordflow">if</span> (!(str_new = (<span class="keywordtype">char</span>*)realloc(str, <span class="keyword">sizeof</span>(*str) * n))) {</div><div class="line"> <a class="code" href="group__BasicTypes.html#ga5c2b2943475239e151eb783d30548b38">clingo_set_error</a>(<a class="code" href="group__BasicTypes.html#gga5adba1f5e366e7489ac4a3fb5266dba6aa14a0926eb3e653fcc13299b33d8d348">clingo_error_bad_alloc</a>, <span class="stringliteral">"could not allocate memory for symbol's string"</span>);</div><div class="line"> <span class="keywordflow">goto</span> error;</div><div class="line"> }</div><div class="line"></div><div class="line"> str = str_new;</div><div class="line"> str_n = n;</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="comment">// retrieve the symbol's string</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a24"></a><a class="code" href="group__Symbols.html#gac292526ba129269eec3c64510deec3b0">clingo_symbol_to_string</a>(*it, str, n)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> printf(<span class="stringliteral">" %s"</span>, str);</div><div class="line"> }</div><div class="line"></div><div class="line"> printf(<span class="stringliteral">"\n"</span>);</div><div class="line"> <span class="keywordflow">goto</span> out;</div><div class="line"></div><div class="line">error:</div><div class="line"> ret = <span class="keyword">false</span>;</div><div class="line"></div><div class="line">out:</div><div class="line"> <span class="keywordflow">if</span> (atoms) { free(atoms); }</div><div class="line"> <span class="keywordflow">if</span> (str) { free(str); }</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> ret;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">bool</span> solve(<a class="code" href="group__Control.html#gaf008e9db9dbb37b0b7ef039bb9d582f0">clingo_control_t</a> *ctl, <a class="code" href="group__Control.html#gae917a23b0591d181004ec88c4e3291c1">clingo_solve_result_bitset_t</a> *result) {</div><div class="line"> <span class="keywordtype">bool</span> ret = <span class="keyword">true</span>;</div><div class="line"> <a class="code" href="group__SolveHandle.html#ga023c1084a4c01ea6e121a8310efba045">clingo_solve_handle_t</a> *handle;</div><div class="line"> <a class="code" href="group__Model.html#gaaf9a93819f023f3cb8aa80598c46556b">clingo_model_t</a> *model;</div><div class="line"></div><div class="line"> <span class="comment">// get a solve handle</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a25"></a><a class="code" href="group__Control.html#ga0d0d3f09cbfbc2c7f92b7f0a87a13590">clingo_control_solve</a>(ctl, <a name="a26"></a><a class="code" href="group__SolveHandle.html#gga2cfdeafd2caf82e14349e639f298a35fa60a9c133be636e68a7d02680c8b9d47e">clingo_solve_mode_yield</a>, NULL, 0, NULL, NULL, &handle)) { <span class="keywordflow">goto</span> error; }</div><div class="line"> <span class="comment">// loop over all models</span></div><div class="line"> <span class="keywordflow">while</span> (<span class="keyword">true</span>) {</div><div class="line"> <span class="keywordflow">if</span> (!<a name="a27"></a><a class="code" href="group__SolveHandle.html#ga73d71df7adeee92f1d515f52cdcbbac0">clingo_solve_handle_resume</a>(handle)) { <span class="keywordflow">goto</span> error; }</div><div class="line"> <span class="keywordflow">if</span> (!<a name="a28"></a><a class="code" href="group__SolveHandle.html#ga41635407591fcad87b474a9af32aacd4">clingo_solve_handle_model</a>(handle, &model)) { <span class="keywordflow">goto</span> error; }</div><div class="line"> <span class="comment">// print the model</span></div><div class="line"> <span class="keywordflow">if</span> (model) { print_model(model); }</div><div class="line"> <span class="comment">// stop if there are no more models</span></div><div class="line"> <span class="keywordflow">else</span> { <span class="keywordflow">break</span>; }</div><div class="line"> }</div><div class="line"> <span class="comment">// close the solve handle</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a29"></a><a class="code" href="group__SolveHandle.html#gae4270b7d4d0174a479307438680007c5">clingo_solve_handle_get</a>(handle, result)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> <span class="keywordflow">goto</span> out;</div><div class="line"></div><div class="line">error:</div><div class="line"> ret = <span class="keyword">false</span>;</div><div class="line"></div><div class="line">out:</div><div class="line"> <span class="comment">// free the solve handle</span></div><div class="line"> <span class="keywordflow">return</span> <a name="a30"></a><a class="code" href="group__SolveHandle.html#ga488b76e7240d625bd4066e68a33ab23e">clingo_solve_handle_close</a>(handle) && ret;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> <span class="keyword">const</span> **argv) {</div><div class="line"> <span class="keywordtype">char</span> <span class="keyword">const</span> *error_message;</div><div class="line"> <span class="keywordtype">int</span> ret = 0;</div><div class="line"> <a class="code" href="group__Control.html#gae917a23b0591d181004ec88c4e3291c1">clingo_solve_result_bitset_t</a> solve_ret;</div><div class="line"> <a class="code" href="group__Control.html#gaf008e9db9dbb37b0b7ef039bb9d582f0">clingo_control_t</a> *ctl = NULL;</div><div class="line"> <span class="comment">// arguments to the pigeon program part</span></div><div class="line"> <a class="code" href="group__Symbols.html#ga6c75c60fa57c3b97505265ff08f6f951">clingo_symbol_t</a> args[2];</div><div class="line"> <span class="comment">// the pigeon program part having the number of holes and pigeons as parameters</span></div><div class="line"> <a name="_a31"></a><a class="code" href="structclingo__part.html">clingo_part_t</a> parts[] = { { <span class="stringliteral">"pigeon"</span>, args, <span class="keyword">sizeof</span>(args)/<span class="keyword">sizeof</span>(*args) } };</div><div class="line"> <span class="comment">// parameters for the pigeon part</span></div><div class="line"> <span class="keywordtype">char</span> <span class="keyword">const</span> *params[] = {<span class="stringliteral">"h"</span>, <span class="stringliteral">"p"</span>};</div><div class="line"> <span class="comment">// create a propagator with the functions above</span></div><div class="line"> <span class="comment">// using the default implementation for the model check</span></div><div class="line"> <a name="_a32"></a><a class="code" href="structclingo__propagator.html">clingo_propagator_t</a> prop = {</div><div class="line"> (bool (*) (<a class="code" href="group__Propagator.html#ga640db8ba73adaddd5dbe50b572b77f25">clingo_propagate_init_t</a> *, <span class="keywordtype">void</span> *))init,</div><div class="line"> (bool (*) (<a class="code" href="group__Propagator.html#ga1cf43bea08101abb8b8cc7711c4c7165">clingo_propagate_control_t</a> *, <a class="code" href="group__BasicTypes.html#gaa95dd19334e536397bbad174c8fa4ff8">clingo_literal_t</a> <span class="keyword">const</span> *, size_t, <span class="keywordtype">void</span> *))propagate,</div><div class="line"> (bool (*) (clingo_propagate_control_t *, clingo_literal_t <span class="keyword">const</span> *, size_t, <span class="keywordtype">void</span> *))undo,</div><div class="line"> NULL</div><div class="line"> };</div><div class="line"> <span class="comment">// user data for the propagator</span></div><div class="line"> propagator_t prop_data = { NULL, 0, NULL, 0 };</div><div class="line"></div><div class="line"> <span class="comment">// set the number of holes</span></div><div class="line"> <a name="a33"></a><a class="code" href="group__Symbols.html#gaf7394a2d7be515f00873ba260643c2e2">clingo_symbol_create_number</a>(8, &args[0]);</div><div class="line"> <span class="comment">// set the number of pigeons</span></div><div class="line"> <a class="code" href="group__Symbols.html#gaf7394a2d7be515f00873ba260643c2e2">clingo_symbol_create_number</a>(9, &args[1]);</div><div class="line"></div><div class="line"> <span class="comment">// create a control object and pass command line arguments</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a34"></a><a class="code" href="group__Control.html#ga7ec38c676e45447bab71278b51e090c6">clingo_control_new</a>(argv+1, argc-1, NULL, NULL, 20, &ctl) != 0) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> <span class="comment">// register the propagator</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a35"></a><a class="code" href="group__Control.html#ga5f1c3f9cbd1d7b2936038f05d3110747">clingo_control_register_propagator</a>(ctl, &prop, &prop_data, <span class="keyword">false</span>)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> <span class="comment">// add a logic program to the pigeon part</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a36"></a><a class="code" href="group__Control.html#gada7b7f54331abfedf60fac33d742c0dc">clingo_control_add</a>(ctl, <span class="stringliteral">"pigeon"</span>, params, <span class="keyword">sizeof</span>(params)/<span class="keyword">sizeof</span>(*params),</div><div class="line"> <span class="stringliteral">"1 { place(P,H) : H = 1..h } 1 :- P = 1..p."</span>)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> <span class="comment">// ground the pigeon part</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a37"></a><a class="code" href="group__Control.html#gaf85b77055668171e6a85f9d729719b57">clingo_control_ground</a>(ctl, parts, 1, NULL, NULL)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> <span class="comment">// solve using a model callback</span></div><div class="line"> <span class="keywordflow">if</span> (!solve(ctl, &solve_ret)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> <span class="keywordflow">goto</span> out;</div><div class="line"></div><div class="line">error:</div><div class="line"> <span class="keywordflow">if</span> (!(error_message = <a name="a38"></a><a class="code" href="group__BasicTypes.html#ga72da180a7e7300f5f38003450cce900a">clingo_error_message</a>())) { error_message = <span class="stringliteral">"error"</span>; }</div><div class="line"></div><div class="line"> printf(<span class="stringliteral">"%s\n"</span>, error_message);</div><div class="line"> ret = <a name="a39"></a><a class="code" href="group__BasicTypes.html#gab6fae458db566efb7d6684ffda376aa8">clingo_error_code</a>();</div><div class="line"></div><div class="line">out:</div><div class="line"> <span class="comment">// free the propagator state</span></div><div class="line"> <span class="keywordflow">if</span> (prop_data.pigeons) { free(prop_data.pigeons); }</div><div class="line"> <span class="keywordflow">if</span> (prop_data.states_size > 0) {</div><div class="line"> <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i < prop_data.states_size; ++i) {</div><div class="line"> <span class="keywordflow">if</span> (prop_data.states[i].holes) {</div><div class="line"> free(prop_data.states[i].holes);</div><div class="line"> }</div><div class="line"> }</div><div class="line"> free(prop_data.states);</div><div class="line"> }</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (ctl) { <a name="a40"></a><a class="code" href="group__Control.html#ga849604bd7107e6948a0e08e1dc10178a">clingo_control_free</a>(ctl); }</div><div class="line"></div><div class="line"> <span class="keywordflow">return</span> ret;</div><div class="line">}</div><div class="line"></div></div><!-- fragment --> </div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Wed Nov 8 2017 09:57:36 for Clingo C API by  <a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
</body>
</html>