dlvhex  2.5.0
src/ExternalAtomEvaluationHeuristics.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 
00035 #include "dlvhex2/ExternalAtomEvaluationHeuristics.h"
00036 #include "dlvhex2/Interpretation.h"
00037 
00038 #include <bm/bmalgo.h>
00039 
00040 DLVHEX_NAMESPACE_BEGIN
00041 
00042 // ============================== Always ==============================
00043 
00044 ExternalAtomEvaluationHeuristicsAlways::ExternalAtomEvaluationHeuristicsAlways(RegistryPtr reg) : ExternalAtomEvaluationHeuristics(reg)
00045 {
00046 }
00047 
00048 
00049 bool ExternalAtomEvaluationHeuristicsAlways::doEvaluate(const ExternalAtom& eatom, InterpretationConstPtr eatomMask, InterpretationConstPtr programMask, InterpretationConstPtr partialAssignment, InterpretationConstPtr assigned, InterpretationConstPtr changed)
00050 {
00051     return true;
00052 }
00053 
00054 
00055 bool ExternalAtomEvaluationHeuristicsAlways::frequent()
00056 {
00057     return true;
00058 }
00059 
00060 
00061 ExternalAtomEvaluationHeuristicsPtr ExternalAtomEvaluationHeuristicsAlwaysFactory::createHeuristics(RegistryPtr reg)
00062 {
00063     return ExternalAtomEvaluationHeuristicsPtr(new ExternalAtomEvaluationHeuristicsAlways(reg));
00064 }
00065 
00066 
00067 // ============================== Periodic ==============================
00068 
00069 ExternalAtomEvaluationHeuristicsPeriodic::ExternalAtomEvaluationHeuristicsPeriodic(RegistryPtr reg) : ExternalAtomEvaluationHeuristics(reg), counter(0)
00070 {
00071 }
00072 
00073 
00074 bool ExternalAtomEvaluationHeuristicsPeriodic::doEvaluate(const ExternalAtom& eatom, InterpretationConstPtr eatomMask, InterpretationConstPtr programMask, InterpretationConstPtr partialAssignment, InterpretationConstPtr assigned, InterpretationConstPtr changed)
00075 {
00076     static int b = 10;
00077 
00078     counter++;
00079     if (counter > b){
00080         counter = 0;
00081 //        b += 10;
00082         return true;
00083     }else{
00084         return false;
00085     }
00086 }
00087 
00088 
00089 bool ExternalAtomEvaluationHeuristicsPeriodic::frequent()
00090 {
00091     return true;
00092 }
00093 
00094 
00095 ExternalAtomEvaluationHeuristicsPtr ExternalAtomEvaluationHeuristicsPeriodicFactory::createHeuristics(RegistryPtr reg)
00096 {
00097     return ExternalAtomEvaluationHeuristicsPtr(new ExternalAtomEvaluationHeuristicsPeriodic(reg));
00098 }
00099 
00100 
00101 // ============================== InputComplete ==============================
00102 
00103 ExternalAtomEvaluationHeuristicsInputComplete::ExternalAtomEvaluationHeuristicsInputComplete(RegistryPtr reg) : ExternalAtomEvaluationHeuristics(reg)
00104 {
00105 }
00106 
00107 
00108 bool ExternalAtomEvaluationHeuristicsInputComplete::doEvaluate(const ExternalAtom& eatom, InterpretationConstPtr eatomMask, InterpretationConstPtr programMask, InterpretationConstPtr partialAssignment, InterpretationConstPtr assigned, InterpretationConstPtr changed)
00109 {
00110 
00111     eatom.updatePredicateInputMask();
00112 
00113     bool aux = true;
00114     if (eatom.auxInputPredicate != ID_FAIL) {
00115         aux = (eatom.getAuxInputMask()->getStorage() & programMask->getStorage() & assigned->getStorage()).count() == (eatom.getAuxInputMask()->getStorage() & programMask->getStorage()).count();
00116     }
00117 
00118     return !assigned ||
00119         (eatom.getPredicateInputMask()->getStorage() & programMask->getStorage() & assigned->getStorage()).count() == (eatom.getPredicateInputMask()->getStorage() & programMask->getStorage()).count() &&
00120         aux;
00121 }
00122 
00123 
00124 ExternalAtomEvaluationHeuristicsPtr ExternalAtomEvaluationHeuristicsInputCompleteFactory::createHeuristics(RegistryPtr reg)
00125 {
00126     return ExternalAtomEvaluationHeuristicsPtr(new ExternalAtomEvaluationHeuristicsInputComplete(reg));
00127 }
00128 
00129 
00130 // ============================== EAComplete ==============================
00131 
00132 ExternalAtomEvaluationHeuristicsEAComplete::ExternalAtomEvaluationHeuristicsEAComplete(RegistryPtr reg) : ExternalAtomEvaluationHeuristics(reg)
00133 {
00134 }
00135 
00136 
00137 bool ExternalAtomEvaluationHeuristicsEAComplete::doEvaluate(const ExternalAtom& eatom, InterpretationConstPtr eatomMask, InterpretationConstPtr programMask, InterpretationConstPtr partialAssignment, InterpretationConstPtr assigned, InterpretationConstPtr changed)
00138 {
00139 
00140     return !assigned ||
00141         !bm::any_sub(eatomMask->getStorage() & programMask->getStorage(), assigned->getStorage() & programMask->getStorage());
00142 }
00143 
00144 
00145 ExternalAtomEvaluationHeuristicsPtr ExternalAtomEvaluationHeuristicsEACompleteFactory::createHeuristics(RegistryPtr reg)
00146 {
00147     return ExternalAtomEvaluationHeuristicsPtr(new ExternalAtomEvaluationHeuristicsEAComplete(reg));
00148 }
00149 
00150 
00151 // ============================== Never ==============================
00152 
00153 ExternalAtomEvaluationHeuristicsNever::ExternalAtomEvaluationHeuristicsNever(RegistryPtr reg) : ExternalAtomEvaluationHeuristics(reg)
00154 {
00155 }
00156 
00157 
00158 bool ExternalAtomEvaluationHeuristicsNever::doEvaluate(const ExternalAtom& eatom, InterpretationConstPtr eatomMask, InterpretationConstPtr programMask, InterpretationConstPtr partialAssignment, InterpretationConstPtr assigned, InterpretationConstPtr changed)
00159 {
00160     return false;
00161 }
00162 
00163 
00164 ExternalAtomEvaluationHeuristicsPtr ExternalAtomEvaluationHeuristicsNeverFactory::createHeuristics(RegistryPtr reg)
00165 {
00166     return ExternalAtomEvaluationHeuristicsPtr(new ExternalAtomEvaluationHeuristicsNever(reg));
00167 }
00168 
00169 
00170 DLVHEX_NAMESPACE_END
00171 
00172 // vim:expandtab:ts=4:sw=4:
00173 // mode: C++
00174 // End: