dlvhex  2.5.0
src/EvalHeuristicOldDlvhex.cpp
Go to the documentation of this file.
00001 /* dlvhex -- Answer-Set Programming with external interfaces.
00002  * Copyright (C) 2005-2007 Roman Schindlauer
00003  * Copyright (C) 2006-2015 Thomas Krennwallner
00004  * Copyright (C) 2009-2016 Peter Schüller
00005  * Copyright (C) 2011-2016 Christoph Redl
00006  * Copyright (C) 2015-2016 Tobias Kaminski
00007  * Copyright (C) 2015-2016 Antonius Weinzierl
00008  *
00009  * This file is part of dlvhex.
00010  *
00011  * dlvhex is free software; you can redistribute it and/or modify it
00012  * under the terms of the GNU Lesser General Public License as
00013  * published by the Free Software Foundation; either version 2.1 of
00014  * the License, or (at your option) any later version.
00015  *
00016  * dlvhex is distributed in the hope that it will be useful, but
00017  * WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  * Lesser General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU Lesser General Public
00022  * License along with dlvhex; if not, write to the Free Software
00023  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00024  * 02110-1301 USA.
00025  */
00026 
00034 #ifdef HAVE_CONFIG_H
00035 #include "config.h"
00036 #endif                           // HAVE_CONFIG_H
00037 
00038 #include "dlvhex2/EvalHeuristicOldDlvhex.h"
00039 #include "dlvhex2/EvalHeuristicShared.h"
00040 #include "dlvhex2/Logger.h"
00041 
00042 #include <boost/unordered_map.hpp>
00043 #include <boost/property_map/property_map.hpp>
00044 #include <boost/graph/properties.hpp>
00045 #include <boost/graph/depth_first_search.hpp>
00046 #include <boost/graph/reverse_graph.hpp>
00047 
00048 DLVHEX_NAMESPACE_BEGIN
00049 
00050 EvalHeuristicOldDlvhex::EvalHeuristicOldDlvhex():
00051 Base()
00052 {
00053 }
00054 
00055 
00056 EvalHeuristicOldDlvhex::~EvalHeuristicOldDlvhex()
00057 {
00058 }
00059 
00060 
00061 namespace
00062 {
00063     typedef ComponentGraph::Component Component;
00064     typedef ComponentGraph::ComponentIterator ComponentIterator;
00065     typedef ComponentGraph::SuccessorIterator SuccessorIterator;
00066     typedef ComponentGraph::PredecessorIterator PredecessorIterator;
00067     typedef std::set<Component> ComponentSet;
00068     typedef std::list<Component> ComponentList;
00069     typedef std::vector<Component> ComponentVector;
00070 }
00071 
00072 
00073 // old dlvhex approach:
00074 // "calculate all that is calculateable" then go to next set of components and continue
00075 //
00076 // 1) do a topological sort of components not yet put into eval units
00077 // 2) go through components in order and mark "take" if
00078 //    * is an external component and depends only on prior eval units, or
00079 //    * is no external component and depends only on prior eval units or "take" components
00080 // 3) build eval unit from all marked as "take"
00081 // 4) restart
00082 void EvalHeuristicOldDlvhex::build(EvalGraphBuilder& builder)
00083 {
00084     const ComponentGraph& compgraph = builder.getComponentGraph();
00085 
00086     // get internal compgraph
00087     const ComponentGraph::Graph& igraph = compgraph.getInternalGraph();
00088 
00089     ComponentList opencomps;
00090     evalheur::topologicalSortComponents(igraph, opencomps);
00091 
00092     ComponentSet finishedcompsSet;
00093 
00094     do {
00095         DBGLOG(DBG,"creating new eval unit:");
00096         DBGLOG(DBG,"open =     " << printrange(opencomps));
00097         DBGLOG(DBG,"finished = " << printrange(finishedcompsSet));
00098 
00099         ComponentSet markedcomps;
00100         for(ComponentList::const_iterator it = opencomps.begin();
00101         it != opencomps.end(); ++it) {
00102             Component comp = *it;
00103             bool extcomp = !compgraph.propsOf(comp).outerEatoms.empty();
00104             DBGLOG(DBG,"comp " << comp << " is " << (extcomp?"":"not ") << "external");
00105 
00106             // check dependencies
00107             bool mark = true;
00108             ComponentGraph::PredecessorIterator pit, pit_end;
00109             for(boost::tie(pit, pit_end) = compgraph.getDependencies(comp);
00110             pit != pit_end; ++pit) {
00111                 Component dependsOn = compgraph.targetOf(*pit);
00112                 if( extcomp ) {
00113                     if( finishedcompsSet.find(dependsOn) == finishedcompsSet.end() ) {
00114                         // this is an external component and it depends on something not yet finished
00115                         mark = false;
00116                         break;
00117                     }
00118                 }
00119                 else {
00120                     if( finishedcompsSet.find(dependsOn) == finishedcompsSet.end() &&
00121                     markedcomps.find(dependsOn) == markedcomps.end() ) {
00122                         // this is not an external component and it depends on something not yet finished or marked
00123                         mark = false;
00124                         break;
00125                     }
00126                 }
00127             }                    // go through dependencies of each component
00128             DBGLOG(DBG,"comp " << comp << " is " << (mark?"":"not ") << "marked for this eval unit");
00129 
00130             if( mark )
00131                 markedcomps.insert(comp);
00132         }                        // go through all components in order and determine marking for each of them
00133 
00134         LOG(ANALYZE,"marked = " << printrange(markedcomps));
00135 
00136         // create new component
00137         {
00138             std::list<Component> comps(markedcomps.begin(), markedcomps.end());
00139             std::list<Component> ccomps;
00140             EvalGraphBuilder::EvalUnit u = builder.createEvalUnit(comps, ccomps);
00141             Component c = builder.getComponentForUnit(u);
00142             LOG(ANALYZE,"components " << printrange(comps) << " became eval unit " << u << " and component " << c);
00143             finishedcompsSet.insert(c);
00144         }
00145 
00146         // remove marked from opencomps
00147         ComponentList::iterator it = opencomps.begin();
00148         while( it != opencomps.end() ) {
00149             if( markedcomps.find(*it) != markedcomps.end() ) {
00150                 // found marked component
00151 
00152                 // add to finished set
00153                 finishedcompsSet.insert(*it);
00154 
00155                 // remove from open components list
00156                 it = opencomps.erase(it);
00157                 continue;        // does not increment, first checks if end
00158             }
00159             // do it here, so that continue; does not increment
00160             it++;
00161         }
00162     }
00163     while(!opencomps.empty());
00164 }
00165 
00166 
00167 DLVHEX_NAMESPACE_END
00168 
00169 
00170 // vim:expandtab:ts=4:sw=4:
00171 // mode: C++
00172 // End: