1 //
2 // Copyright (c) 2009-2017 Benjamin Kaufmann
3 //
4 // This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to
8 // deal in the Software without restriction, including without limitation the
9 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 // sell copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 // IN THE SOFTWARE.
23 //
24 
25 // Add the libclasp directory to the list of
26 // include directoies of your build system.
27 #include <clasp/clasp_facade.h>
28 #include <clasp/solver.h>
29 #include "example.h"
30 
31 // This example demonstrates how user code can influence model enumeration.
32 
excludeModel(const Clasp::Solver & s,const Clasp::Model & m)33 static void excludeModel(const Clasp::Solver& s, const Clasp::Model& m) {
34 	Clasp::LitVec clause;
35 	for (uint32_t i = 1; i <= s.decisionLevel(); ++i) {
36 		clause.push_back(~s.decision(i));
37 	}
38 	m.ctx->commitClause(clause);
39 }
40 
example4()41 void example4() {
42 	Clasp::ClaspConfig config;
43 	config.solve.enumMode  = Clasp::EnumOptions::enum_user;
44 	config.solve.numModels = 0;
45 
46 	// The "interface" to the clasp library.
47 	Clasp::ClaspFacade libclasp;
48 
49 	Clasp::Asp::LogicProgram& asp = libclasp.startAsp(config);
50 	addSimpleProgram(asp);
51 
52 	libclasp.prepare();
53 
54 	// Start the actual solving process.
55 	for (Clasp::ClaspFacade::SolveHandle h = libclasp.solve(Clasp::SolveMode_t::Yield); h.next(); ) {
56 		// print the model
57 		printModel(libclasp.ctx.output, *h.model());
58 		// exclude this model
59 		excludeModel(*libclasp.ctx.solver(h.model()->sId), *h.model());
60 	}
61 	std::cout << "No more models!" << std::endl;
62 }
63