dlvhex  2.1.0
include/dlvhex2/EvalHeuristicShared.h
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, 2011 Thomas Krennwallner
00004  * Copyright (C) 2009, 2010, 2011 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 #ifndef EVAL_HEURISTIC_SHARED_HPP_INCLUDED__30112011
00032 #define EVAL_HEURISTIC_SHARED_HPP_INCLUDED__30112011
00033 
00034 #include "dlvhex2/EvalHeuristicBase.h"
00035 #include "dlvhex2/EvalGraphBuilder.h"
00036 
00037 #include <boost/graph/topological_sort.hpp>
00038 #include <boost/property_map/property_map.hpp>
00039 
00040 DLVHEX_NAMESPACE_BEGIN
00041 
00042 namespace evalheur
00043 {
00044 
00045 typedef ComponentGraph::Component Component;
00046 typedef ComponentGraph::ComponentIterator ComponentIterator;
00047 typedef std::vector<Component> ComponentContainer;
00048 typedef std::set<Component> ComponentSet;
00049 
00050 // topological sort of all components in graph into vector
00051 //
00052 // takes either internal component graph or component graph rest
00053 template<typename ComponentGraphIntOrRest>
00054 void topologicalSortComponents(const ComponentGraphIntOrRest& cg, ComponentContainer& out);
00055 
00056 struct BuildCommand
00057 {
00058     // components to collapse to unit
00059     ComponentContainer collapse;
00060     // components to share into unit (constraint components)
00061     ComponentContainer share;
00062 };
00063 typedef std::vector<BuildCommand> CommandVector;
00064 
00065 void executeBuildCommands(const CommandVector& commands, EvalGraphBuilder& builder);
00066 
00067 }
00068 
00069 namespace evalheur
00070 {
00071 
00072 // we need a hash map, as component graph is no graph with vecS-storage
00073 typedef boost::unordered_map<Component, boost::default_color_type> CompColorHashMap;
00074 typedef boost::associative_property_map<CompColorHashMap> CompColorMap;
00075 
00076 template<typename ComponentGraphIntOrRest>
00077 void topologicalSortComponents(const ComponentGraphIntOrRest& cg, ComponentContainer& out)
00078 {
00079     // create white hash map for topological sort
00080     CompColorHashMap ccWhiteHashMap;
00081     {
00082     typename boost::graph_traits<ComponentGraphIntOrRest>::vertex_iterator cit, cit_end;
00083     for(boost::tie(cit, cit_end) = boost::vertices(cg);
00084         cit != cit_end; ++cit)
00085     {
00086       ccWhiteHashMap[*cit] = boost::white_color;
00087         }
00088     }
00089 
00090   assert(out.empty());
00091   std::back_insert_iterator<ComponentContainer> compinserter(out);
00092   boost::topological_sort(
00093       cg,
00094       compinserter,
00095       boost::color_map(CompColorMap(ccWhiteHashMap)));
00096 }
00097 
00098 }
00099 
00100 DLVHEX_NAMESPACE_END
00101 
00102 #endif // EVAL_HEURISTIC_SHARED_HPP_INCLUDED__30112011