dlvhex  2.5.0
include/dlvhex2/EvalHeuristicShared.h
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 #ifndef EVAL_HEURISTIC_SHARED_HPP_INCLUDED__30112011
00035 #define EVAL_HEURISTIC_SHARED_HPP_INCLUDED__30112011
00036 
00037 #include "dlvhex2/EvalHeuristicBase.h"
00038 #include "dlvhex2/EvalGraphBuilder.h"
00039 
00040 #include <boost/graph/topological_sort.hpp>
00041 #include <boost/property_map/property_map.hpp>
00042 
00043 DLVHEX_NAMESPACE_BEGIN
00044 
00045 namespace evalheur
00046 {
00047 
00048     typedef ComponentGraph::Component Component;
00049     typedef ComponentGraph::ComponentIterator ComponentIterator;
00050     typedef std::vector<Component> ComponentContainer;
00051     typedef std::set<Component> ComponentSet;
00052 
00057     template<typename ComponentGraphIntOrRest, typename Sequence>
00058         void topologicalSortComponents(const ComponentGraphIntOrRest& cg, Sequence& out);
00059 
00061     struct BuildCommand
00062     {
00064         ComponentContainer collapse;
00066         ComponentContainer share;
00067     };
00068     typedef std::vector<BuildCommand> CommandVector;
00069 
00073     void executeBuildCommands(const CommandVector& commands, EvalGraphBuilder& builder);
00074 
00075     // template implementation
00076     template<typename ComponentGraphIntOrRest, typename Sequence>
00077     void topologicalSortComponents(const ComponentGraphIntOrRest& cg, Sequence& out) {
00078         // we need a hash map, as component graph is no graph with vecS-storage
00079         typedef boost::unordered_map<Component, boost::default_color_type> CompColorHashMap;
00080         typedef boost::associative_property_map<CompColorHashMap> CompColorMap;
00081 
00082         // create white hash map for topological sort
00083         CompColorHashMap ccWhiteHashMap;
00084         {
00085             typename boost::graph_traits<ComponentGraphIntOrRest>::vertex_iterator cit, cit_end;
00086             for(boost::tie(cit, cit_end) = boost::vertices(cg);
00087             cit != cit_end; ++cit) {
00088                 ccWhiteHashMap[*cit] = boost::white_color;
00089             }
00090         }
00091 
00092         assert(out.empty());
00093         typename std::back_insert_iterator<Sequence> compinserter(out);
00094         boost::topological_sort(
00095             cg,
00096             compinserter,
00097             boost::color_map(CompColorMap(ccWhiteHashMap)));
00098     }
00099 
00100 }
00101 
00102 
00103 DLVHEX_NAMESPACE_END
00104 #endif                           // EVAL_HEURISTIC_SHARED_HPP_INCLUDED__30112011
00105 
00106 // vim:expandtab:ts=4:sw=4:
00107 // mode: C++
00108 // End: