example1.cpp
Go to the documentation of this file.
00001 // 
00002 // Copyright (c) 2009, Benjamin Kaufmann
00003 // 
00004 // This file is part of Clasp. See http://www.cs.uni-potsdam.de/clasp/ 
00005 // 
00006 // Clasp is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License as published by
00008 // the Free Software Foundation; either version 2 of the License, or
00009 // (at your option) any later version.
00010 // 
00011 // Clasp is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 // 
00016 // You should have received a copy of the GNU General Public License
00017 // along with Clasp; if not, write to the Free Software
00018 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019 //
00020 #include "example.h"
00021 // Add the libclasp directory to the list of 
00022 // include directoies of your build system.
00023 #include <clasp/logic_program.h>    // for defining logic programs
00024 #include <clasp/unfounded_check.h>  // unfounded set checkers
00025 #include <clasp/model_enumerators.h>// for enumerating answer sets
00026 #include <clasp/solve_algorithms.h> // for enumerating answer sets
00027 
00028 
00029 // Compute the stable models of the program
00030 //    a :- not b.
00031 //    b :- not a.
00032 void example1() {
00033         // LogicProgram provides the interface for 
00034         // defining logic programs.
00035         // It also preprocesses the program and converts it
00036         // to the internal solver format.
00037         // See logic_program.h for details.
00038         Clasp::Asp::LogicProgram lp;
00039         
00040         // Among other things, SharedContext maintains a Solver object 
00041         // which hosts the data and functions for CDNL answer set solving.
00042         // SharedContext also contains the symbol table which stores the 
00043         // mapping between atoms of the logic program and the 
00044         // propositional literals in the solver.
00045         // See shared_context.h for details.
00046         Clasp::SharedContext ctx;
00047         
00048         // startProgram must be called once before we can add atoms/rules
00049         lp.startProgram(ctx);
00050         
00051         // Populate symbol table. Each atoms must have a unique id, the name is optional.
00052         // The symbol table then maps the ids to the propositional 
00053         // literals in the solver.
00054         lp.setAtomName(1, "a");
00055         lp.setAtomName(2, "b");
00056         
00057         // Define the rules of the program.
00058         lp.startRule(Clasp::Asp::BASICRULE).addHead(1).addToBody(2, false).endRule();
00059         lp.startRule(Clasp::Asp::BASICRULE).addHead(2).addToBody(1, false).endRule();
00060         
00061         // Once all rules are defined, call endProgram() to load the (simplified)
00062         // program into the context object.
00063         lp.endProgram();
00064         
00065         // Since we want to compute more than one
00066         // answer set, we need an enumerator.
00067         // See enumerator.h for details
00068         Clasp::ModelEnumerator enumerator;
00069         enumerator.init(ctx, 0);
00070 
00071         // We are done with problem setup. 
00072         // Prepare for solving.
00073         ctx.endInit();
00074         // BasicSolve implements a basic search for a model.
00075         // It handles the various strategies like restarts, deletion, etc.
00076         Clasp::BasicSolve solve(*ctx.master());
00077         // Prepare the solver for enumeration.
00078         enumerator.start(solve.solver());
00079         while (solve.solve() == Clasp::value_true) {
00080                 // Make the enumerator aware of the new model and
00081                 // let it compute a new constraint and/or backtracking level.
00082                 if (enumerator.commitModel(solve.solver())) { printModel(ctx.symbolTable(), enumerator.lastModel()); }
00083                 // Integrate the model into the search and thereby prepare 
00084                 // the solver for the search for the next model.
00085                 enumerator.update(solve.solver());
00086         }
00087         std::cout << "No more models!" << std::endl;
00088 }
00089 
00090 


clasp
Author(s): Benjamin Kaufmann
autogenerated on Thu Aug 27 2015 12:41:39