dlvhex  2.5.0
testsuite/TestEvalHeuristic.cpp
Go to the documentation of this file.
00001 /* dlvhex -- Answer-Set Programming with external interfaces.
00002  * Copyright (C) 2005, 2006, 2007 Roman Schindlauer
00003  * Copyright (C) 2006, 2007, 2008, 2009, 2010 Thomas Krennwallner
00004  * Copyright (C) 2009, 2010 Peter Schüller
00005  * 
00006  * This file is part of dlvhex.
00007  *
00008  * dlvhex is free software; you can redistribute it and/or modify it
00009  * under the terms of the GNU Lesser General Public License as
00010  * published by the Free Software Foundation; either version 2.1 of
00011  * the License, or (at your option) any later version.
00012  *
00013  * dlvhex is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with dlvhex; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00021  * 02110-1301 USA.
00022  */
00023 
00031 #ifdef HAVE_CONFIG_H
00032 #include "config.h"
00033 #endif // HAVE_CONFIG_H
00034 
00035 #include "dlvhex2/EvalGraphBuilder.h"
00036 #include "dlvhex2/EvalHeuristicOldDlvhex.h"
00037 #include "dlvhex2/HexParser.h"
00038 #include "dlvhex2/ProgramCtx.h"
00039 #include "dlvhex2/Printer.h"
00040 #include "dlvhex2/Registry.h"
00041 #include "dlvhex2/PluginInterface.h"
00042 #include "dlvhex2/DependencyGraph.h"
00043 #include "dlvhex2/ComponentGraph.h"
00044 #include "dlvhex2/ASPSolverManager.h"
00045 
00046 // this must be included before dummytypes!
00047 #define BOOST_TEST_MODULE __FILE__
00048 #include <boost/test/unit_test.hpp>
00049 
00050 #include "fixturesExt1.h"
00051 #include "fixturesMCS.h"
00052 #include "graphviz.h"
00053 
00054 #include <iostream>
00055 #include <fstream>
00056 #include <cstdlib>
00057 
00058 #define LOG_REGISTRY_PROGRAM(ctx) \
00059   ctx.registry()->logContents(); \
00060     RawPrinter printer(std::cerr, ctx.registry()); \
00061     LOG(INFO,"edb"); \
00062     printer.printmany(ctx.edb,"\n"); \
00063     std::cerr << std::endl; \
00064     LOG(INFO,"edb end"); \
00065     LOG(INFO,"idb"); \
00066     printer.printmany(ctx.idb,"\n"); \
00067     std::cerr << std::endl; \
00068     LOG(INFO,"idb end");
00069 
00070 LOG_INIT(Logger::ERROR | Logger::WARNING)
00071 
00072 DLVHEX_NAMESPACE_USE
00073 
00074 BOOST_FIXTURE_TEST_CASE(testEvalHeuristicExt1,ProgramExt1ProgramCtxDependencyGraphComponentGraphFixture) 
00075 {
00076   // eval graph
00077   FinalEvalGraph eg;
00078 
00079   // write to dotfile and create pdf
00080   {
00081     const char* fnamev = "testEvalHeurExt1CGVerbose.dot";
00082     LOG(INFO,"dumping verbose graph to " << fnamev);
00083     std::ofstream filev(fnamev);
00084     compgraph.writeGraphViz(filev, true);
00085     makeGraphVizPdf(fnamev);
00086 
00087     const char* fnamet = "testEvalHeurExt1CGTerse.dot";
00088     LOG(INFO,"dumping terse graph to " << fnamet);
00089     std::ofstream filet(fnamet);
00090     compgraph.writeGraphViz(filet, false);
00091     makeGraphVizPdf(fnamet);
00092   }
00093 
00094   //
00095   // now the real testing starts
00096   //
00097 
00098   // create "final" (for a lack of a better name) eval graph
00099   {
00100     LOG(INFO,"starting to build eval graph");
00101 
00102     // create builder that supervises the construction of eg
00103     ASPSolverManager::SoftwareConfigurationPtr extEvalConfig;
00104     EvalGraphBuilder egbuilder(ctx, compgraph, eg, extEvalConfig);
00105 
00106     {
00107       // create heuristic, which sends commands to egbuilder
00108       EvalHeuristicOldDlvhex heuristicOldDlvhex;
00109       heuristicOldDlvhex.build(egbuilder);
00110       LOG(INFO,"building eval graph finished");
00111 
00112       // log the (changed) component graph
00113       {
00114         const char* fnamev = "testEvalHeurExt1Verbose.dot";
00115         LOG(INFO,"dumping verbose graph to " << fnamev);
00116         std::ofstream filev(fnamev);
00117         compgraph.writeGraphViz(filev, true);
00118         makeGraphVizPdf(fnamev);
00119 
00120         const char* fnamet = "testEvalHeurExt1Terse.dot";
00121         LOG(INFO,"dumping terse graph to " << fnamet);
00122         std::ofstream filet(fnamet);
00123         compgraph.writeGraphViz(filet, false);
00124         makeGraphVizPdf(fnamet);
00125       }
00126 
00127       LOG(INFO,"eval heuristic going out of scope");
00128     }
00129     LOG(INFO,"eval graph builder going out of scope");
00130   }
00131 
00132   // TODO check eval graph
00133 }
00134 
00135 // example using MCS-IE encoding from KR 2010 for calculation of equilibria in medical example
00136 BOOST_FIXTURE_TEST_CASE(testEvalHeuristicMCSMedEQ,ProgramMCSMedEQProgramCtxDependencyGraphComponentGraphFixture) 
00137 {
00138   // eval graph
00139   FinalEvalGraph eg;
00140 
00141   // write ComponentGraph to dotfile and create pdf
00142   {
00143     const char* fnamev = "testEvalHeurMCSMedEqCGVerbose.dot";
00144     LOG(INFO,"dumping verbose graph to " << fnamev);
00145     std::ofstream filev(fnamev);
00146     compgraph.writeGraphViz(filev, true);
00147     makeGraphVizPdf(fnamev);
00148 
00149     const char* fnamet = "testEvalHeurMCSMedEqCGTerse.dot";
00150     LOG(INFO,"dumping terse graph to " << fnamet);
00151     std::ofstream filet(fnamet);
00152     compgraph.writeGraphViz(filet, false);
00153     makeGraphVizPdf(fnamet);
00154   }
00155 
00156   //
00157   // now the real testing starts
00158   //
00159 
00160   // create "final" (for a lack of a better name) eval graph
00161   {
00162     LOG(INFO,"starting to build eval graph");
00163 
00164     // create builder that supervises the construction of eg
00165     ASPSolverManager::SoftwareConfigurationPtr extEvalConfig;
00166     EvalGraphBuilder egbuilder(ctx, compgraph, eg, extEvalConfig);
00167 
00168     {
00169       // create heuristic, which sends commands to egbuilder
00170       EvalHeuristicOldDlvhex heuristicOldDlvhex;
00171       heuristicOldDlvhex.build(egbuilder);
00172       LOG(INFO,"building eval graph finished");
00173 
00174       // log the (changed) component graph
00175       {
00176         const char* fnamev = "testEvalHeurMCSMedEqVerbose.dot";
00177         LOG(INFO,"dumping verbose graph to " << fnamev);
00178         std::ofstream filev(fnamev);
00179         compgraph.writeGraphViz(filev, true);
00180         makeGraphVizPdf(fnamev);
00181 
00182         const char* fnamet = "testEvalHeurMCSMedEqTerse.dot";
00183         LOG(INFO,"dumping terse graph to " << fnamet);
00184         std::ofstream filet(fnamet);
00185         compgraph.writeGraphViz(filet, false);
00186         makeGraphVizPdf(fnamet);
00187       }
00188 
00189       LOG(INFO,"eval heuristic going out of scope");
00190     }
00191     LOG(INFO,"eval graph builder going out of scope");
00192   }
00193 
00194   // TODO check eval graph
00195 }
00196 
00197 // example using MCS-IE encoding from KR 2010 for calculation of diagnoses in medical example
00198 BOOST_FIXTURE_TEST_CASE(testEvalHeuristicMCSMedD,ProgramMCSMedDProgramCtxDependencyGraphComponentGraphFixture) 
00199 {
00200   // eval graph
00201   FinalEvalGraph eg;
00202 
00203   // write ComponentGraph to dotfile and create pdf
00204   {
00205     const char* fnamev = "testEvalHeurMCSMedDCGVerbose.dot";
00206     LOG(INFO,"dumping verbose graph to " << fnamev);
00207     std::ofstream filev(fnamev);
00208     compgraph.writeGraphViz(filev, true);
00209     makeGraphVizPdf(fnamev);
00210 
00211     const char* fnamet = "testEvalHeurMCSMedDCGTerse.dot";
00212     LOG(INFO,"dumping terse graph to " << fnamet);
00213     std::ofstream filet(fnamet);
00214     compgraph.writeGraphViz(filet, false);
00215     makeGraphVizPdf(fnamet);
00216   }
00217 
00218   //
00219   // now the real testing starts
00220   //
00221 
00222   // create "final" (for a lack of a better name) eval graph
00223   {
00224     LOG(INFO,"starting to build eval graph");
00225 
00226     // create builder that supervises the construction of eg
00227     ASPSolverManager::SoftwareConfigurationPtr extEvalConfig;
00228     EvalGraphBuilder egbuilder(ctx, compgraph, eg, extEvalConfig);
00229 
00230     {
00231       // create heuristic, which sends commands to egbuilder
00232       EvalHeuristicOldDlvhex heuristicOldDlvhex;
00233       heuristicOldDlvhex.build(egbuilder);
00234       LOG(INFO,"building eval graph finished");
00235 
00236       // log the (changed) component graph
00237       {
00238         const char* fnamev = "testEvalHeurMCSMedDVerbose.dot";
00239         LOG(INFO,"dumping verbose graph to " << fnamev);
00240         std::ofstream filev(fnamev);
00241         compgraph.writeGraphViz(filev, true);
00242         makeGraphVizPdf(fnamev);
00243 
00244         const char* fnamet = "testEvalHeurMCSMedDTerse.dot";
00245         LOG(INFO,"dumping terse graph to " << fnamet);
00246         std::ofstream filet(fnamet);
00247         compgraph.writeGraphViz(filet, false);
00248         makeGraphVizPdf(fnamet);
00249       }
00250 
00251       LOG(INFO,"eval heuristic going out of scope");
00252     }
00253     LOG(INFO,"eval graph builder going out of scope");
00254   }
00255 
00256   // TODO check eval graph
00257 }
00258