satelite_test.cpp
Go to the documentation of this file.
00001 // 
00002 // Copyright (c) 2006, 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 "test.h"
00021 #include <algorithm>
00022 #include <clasp/satelite.h>
00023 #include <clasp/program_builder.h>
00024 #include <clasp/solver.h>
00025 #include <clasp/clause.h>
00026 #include <clasp/parser.h>
00027 #ifdef _MSC_VER
00028 #pragma warning (disable : 4267) //  conversion from 'size_t' to unsigned int
00029 #pragma once
00030 #endif
00031 
00032 
00033 namespace Clasp { namespace Test {
00034 
00035 class SatEliteTest : public CppUnit::TestFixture {
00036   CPPUNIT_TEST_SUITE(SatEliteTest);
00037         
00038         CPPUNIT_TEST(testDontAddSatClauses);
00039         CPPUNIT_TEST(testSimpleSubsume);
00040         CPPUNIT_TEST(testSimpleStrengthen);
00041         
00042         CPPUNIT_TEST(testClauseCreatorAddsToPreprocessor);
00043         
00044         CPPUNIT_TEST(testDimacs);
00045         CPPUNIT_TEST(testFreeze);
00046 
00047         CPPUNIT_TEST(testElimPureLits);
00048         CPPUNIT_TEST(testDontElimPureLits);
00049         CPPUNIT_TEST(testExtendModel);
00050         CPPUNIT_TEST(testExtendModel2);
00051         CPPUNIT_TEST_SUITE_END();       
00052 public:
00053         SatEliteTest(){
00054                 for (int i = 0; i < 10; ++i) {
00055                         ctx.addVar(Var_t::atom_var);
00056                 }
00057                 opts.satPre.type = SatPreParams::sat_pre_ve_bce;
00058                 ctx.startAddConstraints();
00059         }
00060         
00061         void testDontAddSatClauses() {
00062                 LitVec cl;
00063                 cl.push_back(posLit(1)); cl.push_back(posLit(2));
00064                 pre.addClause(cl);
00065                 ctx.addUnary(posLit(1));
00066                 CPPUNIT_ASSERT_EQUAL(true, pre.preprocess(ctx, opts.satPre));
00067                 CPPUNIT_ASSERT_EQUAL(0u, ctx.numConstraints());
00068         }
00069 
00070         void testSimpleSubsume() {
00071                 LitVec cl;
00072                 cl.push_back(posLit(1)); cl.push_back(posLit(2));
00073                 pre.addClause(cl);
00074                 cl.push_back(posLit(3));
00075                 pre.addClause(cl);
00076                 CPPUNIT_ASSERT_EQUAL(true, pre.preprocess(ctx, opts.satPre));
00077                 CPPUNIT_ASSERT_EQUAL(0u, ctx.numConstraints());
00078         }
00079 
00080         void testSimpleStrengthen() {
00081                 LitVec cl;
00082                 cl.push_back(posLit(1)); cl.push_back(posLit(2));
00083                 pre.addClause(cl);
00084                 cl[0] = negLit(1);
00085                 pre.addClause(cl);
00086                 CPPUNIT_ASSERT_EQUAL(true, pre.preprocess(ctx, opts.satPre));
00087                 CPPUNIT_ASSERT_EQUAL(0u, ctx.numConstraints());
00088         }
00089 
00090         void testClauseCreatorAddsToPreprocessor() {
00091                 ctx.setConfiguration(&opts, false);
00092                 ctx.setPreserveModels();
00093                 ClauseCreator nc(ctx.master());
00094                 nc.start().add(posLit(1)).add(posLit(2)).end();
00095                 CPPUNIT_ASSERT_EQUAL(0u, ctx.numConstraints());
00096                 ctx.endInit();
00097                 CPPUNIT_ASSERT_EQUAL(1u, ctx.numConstraints());
00098                 CPPUNIT_ASSERT_EQUAL(1u, ctx.numBinary());
00099         }
00100 
00101         void testDimacs() {
00102                 std::stringstream prg;
00103                 SharedContext ctx2; 
00104                 opts.satPre.disableBce();
00105                 ctx2.setConfiguration(&opts, false);
00106                 SatBuilder api;
00107                 api.startProgram(ctx2);
00108                 
00109                 prg << "c simple test case\n"
00110                                 << "p cnf 2 4\n"
00111                           << "1 2 2 1 0\n"
00112                                 << "1 2 1 2 0\n"
00113                                 << "-1 -2 -1 0\n"
00114                                 << "-2 -1 -2 0\n";
00115                 CPPUNIT_ASSERT(api.parseProgram(prg) && api.endProgram());
00116                 CPPUNIT_ASSERT(ctx2.numVars() == 2 && ctx2.master()->numAssignedVars() == 0);
00117                 CPPUNIT_ASSERT(ctx2.endInit());
00118                 CPPUNIT_ASSERT(ctx2.eliminated(2));
00119         }
00120 
00121         void testFreeze() {
00122                 std::stringstream prg;
00123                 SharedContext ctx2;
00124                 ctx2.setConfiguration(&opts, false);
00125                 SatBuilder api;
00126                 api.startProgram(ctx2);
00127                 prg << "c simple test case\n"
00128                                 << "p cnf 2 2\n"
00129                           << "1 2 0\n"
00130                                 << "-1 -2 0\n";
00131                 CPPUNIT_ASSERT(api.parseProgram(prg));
00132                 ctx2.setFrozen(1, true);
00133                 ctx2.setFrozen(2, true);
00134                 CPPUNIT_ASSERT(ctx2.numVars() == 2);
00135                 CPPUNIT_ASSERT(ctx2.endInit());
00136                 CPPUNIT_ASSERT(ctx2.numConstraints() == 2);
00137                 CPPUNIT_ASSERT(ctx2.numBinary() == 2);
00138         }
00139 
00140         void testElimPureLits() {
00141                 LitVec cl;
00142                 cl.push_back(posLit(1)); cl.push_back(posLit(2));
00143                 pre.addClause(cl);
00144                 opts.satPre.disableBce();
00145                 opts.satPre.mode = SatPreParams::prepro_preserve_sat;
00146                 pre.preprocess(ctx, opts.satPre);
00147                 CPPUNIT_ASSERT(0u == ctx.numConstraints());
00148                 CPPUNIT_ASSERT(ctx.eliminated(1) == true);
00149         }
00150         void testDontElimPureLits() {
00151                 LitVec cl;
00152                 cl.push_back(posLit(1)); cl.push_back(posLit(2));
00153                 pre.addClause(cl);
00154                 opts.satPre.mode = SatPreParams::prepro_preserve_models;
00155                 pre.preprocess(ctx, opts.satPre);
00156                 CPPUNIT_ASSERT(1u == ctx.numConstraints());
00157                 CPPUNIT_ASSERT(ctx.eliminated(1) == false);
00158         }
00159         
00160         void testExtendModel() {
00161                 SharedContext ctx2; 
00162                 opts.satPre.disableBce();
00163                 opts.satPre.mode = SatPreParams::prepro_preserve_models;
00164                 ctx2.setConfiguration(&opts, false);
00165                 ctx2.addVar(Var_t::atom_var);
00166                 ctx2.addVar(Var_t::atom_var);
00167                 ctx2.addVar(Var_t::atom_var);
00168                 ctx2.startAddConstraints();
00169                 ClauseCreator nc(ctx2.master());
00170                 nc.start().add(negLit(1)).add(posLit(2)).end();
00171                 nc.start().add(posLit(1)).add(negLit(2)).end();
00172                 nc.start().add(posLit(2)).add(negLit(3)).end();
00173                 ctx2.endInit();
00174                 CPPUNIT_ASSERT(1u == ctx2.numConstraints());
00175                 CPPUNIT_ASSERT(ctx2.eliminated(1) == true && ctx2.numEliminatedVars() == 1);
00176                 // Eliminated vars are initially assigned
00177                 CPPUNIT_ASSERT(value_free != ctx2.master()->value(1));
00178                 ctx2.master()->assume(negLit(2)) && ctx2.master()->propagate();
00179                 ctx2.master()->search(-1, -1, 1.0);
00180                 // negLit(2) -> negLit(1) -> 1 == F
00181                 CPPUNIT_ASSERT_EQUAL(value_false, ctx2.master()->model[1]);
00182         }
00183 
00184         void testExtendModel2() {
00185                 SharedContext ctx2; 
00186                 opts.satPre.disableBce();
00187                 opts.satPre.mode = SatPreParams::prepro_preserve_models;
00188                 ctx2.setConfiguration(&opts, false);
00189                 ctx2.addVar(Var_t::atom_var);
00190                 ctx2.addVar(Var_t::atom_var);
00191                 ctx2.addVar(Var_t::atom_var);
00192                 ctx2.addVar(Var_t::atom_var);
00193                 ctx2.startAddConstraints();
00194                 ClauseCreator nc(ctx2.master());
00195         
00196                 nc.start().add(posLit(1)).add(posLit(3)).add(posLit(2)).end();
00197                 nc.start().add(negLit(1)).add(posLit(3)).add(negLit(2)).end();
00198                 
00199                 nc.start().add(posLit(2)).add(posLit(3)).add(posLit(4)).end();
00200                 nc.start().add(negLit(2)).add(posLit(3)).add(negLit(4)).end();
00201                 
00202                 ctx2.endInit();
00203                 ctx2.master()->assume(posLit(3));
00204                 ctx2.master()->search(-1, -1, 1.0);
00205                 uint32 n = 1;
00206                 LitVec sym = ctx2.master()->symmetric();
00207                 while (!sym.empty()) {
00208                         ++n;
00209                         ctx2.satPrepro->extendModel(ctx2.master()->model, sym);
00210                 }
00211                 CPPUNIT_ASSERT(n == 4);
00212         }
00213 private:
00214         SharedContext       ctx;
00215         SatElite::SatElite      pre;
00216         BasicSatConfig      opts;
00217 };
00218 CPPUNIT_TEST_SUITE_REGISTRATION(SatEliteTest);
00219 } } 


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