forked from potassco/potassco.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration_8c-example.html
78 lines (76 loc) · 19.1 KB
/
configuration_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
<!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: configuration.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">configuration.c</div> </div>
</div><!--header-->
<div class="contents">
<p>The example shows how to configure the solver.</p><dl class="section note"><dt>Note</dt><dd>It is also possible to loop over all configuration entries. This can be done in a similar fashion as in the <a class="el" href="statistics_8c-example.html">statistics.c</a> example. But note that, unlike with statistics entries, a configuration entry can have more than one type.</dd></dl>
<h2>Output</h2>
<div class="fragment"><div class="line">./configuration</div><div class="line">Model: a</div><div class="line">Model: b</div></div><!-- fragment --><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"></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="a0"></a><a class="code" href="group__Model.html#ga19a05934acb060994eed7cd079d1a0fb">clingo_model_symbols_size</a>(model, <a name="a1"></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 name="a2"></a><a class="code" href="group__BasicTypes.html#ga5c2b2943475239e151eb783d30548b38">clingo_set_error</a>(<a name="a3"></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="a4"></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="a5"></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="a6"></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="a7"></a><a class="code" href="group__Control.html#ga0d0d3f09cbfbc2c7f92b7f0a87a13590">clingo_control_solve</a>(ctl, <a name="a8"></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="a9"></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="a10"></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="a11"></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="a12"></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"> <a name="_a13"></a><a class="code" href="structclingo__part.html">clingo_part_t</a> parts[] = {{ <span class="stringliteral">"base"</span>, NULL, 0 }};</div><div class="line"> <a class="code" href="group__Configuration.html#gabf3ac9f48abde87a53cdba386f21bc2f">clingo_configuration_t</a> *conf;</div><div class="line"> <a class="code" href="group__BasicTypes.html#gac7aaf88deb591e91b94ff38aa1e3472e">clingo_id_t</a> root_key, sub_key;</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="a14"></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">// get the configuration object and its root key</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a15"></a><a class="code" href="group__Control.html#ga508042ea216ddfd24f4278310cc25321">clingo_control_configuration</a>(ctl, &conf)) { <span class="keywordflow">goto</span> error; }</div><div class="line"> <span class="keywordflow">if</span> (!<a name="a16"></a><a class="code" href="group__Configuration.html#ga08630e8762ae216ba84a5b52c61f78eb">clingo_configuration_root</a>(conf, &root_key)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> <span class="comment">// configure to enumerate all models</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a17"></a><a class="code" href="group__Configuration.html#ga0f7f9edc216c4d9131310457de936fa2">clingo_configuration_map_at</a>(conf, root_key, <span class="stringliteral">"solve.models"</span>, &sub_key)) { <span class="keywordflow">goto</span> error; }</div><div class="line"> <span class="keywordflow">if</span> (!<a name="a18"></a><a class="code" href="group__Configuration.html#gacf50be61b36e0f0aaa3b367ac1ad18c4">clingo_configuration_value_set</a>(conf, sub_key, <span class="stringliteral">"0"</span>)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> <span class="comment">// configure the first solver to use the berkmin heuristic</span></div><div class="line"> <span class="keywordflow">if</span> (!<a class="code" href="group__Configuration.html#ga0f7f9edc216c4d9131310457de936fa2">clingo_configuration_map_at</a>(conf, root_key, <span class="stringliteral">"solver"</span>, &sub_key)) { <span class="keywordflow">goto</span> error; }</div><div class="line"> <span class="keywordflow">if</span> (!<a name="a19"></a><a class="code" href="group__Configuration.html#ga009446256c152ed664ec426782fee7c7">clingo_configuration_array_at</a>(conf, sub_key, 0, &sub_key)) { <span class="keywordflow">goto</span> error; }</div><div class="line"> <span class="keywordflow">if</span> (!<a class="code" href="group__Configuration.html#ga0f7f9edc216c4d9131310457de936fa2">clingo_configuration_map_at</a>(conf, sub_key, <span class="stringliteral">"heuristic"</span>, &sub_key)) { <span class="keywordflow">goto</span> error; }</div><div class="line"> <span class="keywordflow">if</span> (!<a class="code" href="group__Configuration.html#gacf50be61b36e0f0aaa3b367ac1ad18c4">clingo_configuration_value_set</a>(conf, sub_key, <span class="stringliteral">"berkmin"</span>)) { <span class="keywordflow">goto</span> error; }</div><div class="line"> <span class="comment">// note that the solver entry can be used both as an array and a map</span></div><div class="line"> <span class="comment">// if used as a map, this simply sets the configuration of the first solver and</span></div><div class="line"> <span class="comment">// is equivalent to the code above</span></div><div class="line"></div><div class="line"> <span class="comment">// add a logic program to the base part</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a20"></a><a class="code" href="group__Control.html#gada7b7f54331abfedf60fac33d742c0dc">clingo_control_add</a>(ctl, <span class="stringliteral">"base"</span>, NULL, 0, <span class="stringliteral">"a :- not b. b :- not a."</span>)) { <span class="keywordflow">goto</span> error; }</div><div class="line"></div><div class="line"> <span class="comment">// ground the base part</span></div><div class="line"> <span class="keywordflow">if</span> (!<a name="a21"></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</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="a22"></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="a23"></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="keywordflow">if</span> (ctl) { <a name="a24"></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>