Debian Codegraph @

FaCT++.Python/lib/Kernel/dlDag.cpp

raw symbols SCIP
1 /* This file is part of the FaCT++ DL reasoner
2 Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester
3 Copyright (C) 2015-2017 Dmitry Tsarkov
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "dlDag.h"
21
22 #include "logging.h"
23 #include "tDataEntry.h"
24 #include "tConcept.h"
25
26 DLDag :: DLDag ( const ifOptionSet* Options )
27 : indexAnd(*this)
28 , indexAll(*this)
29 , indexLE(*this)
30 {
31 Heap.push_back ( new DLVertex (dtBad) ); // empty vertex -- bpINVALID
32 Heap.push_back ( new DLVertex (dtTop) );
33
34 readConfig ( Options );
35 }
36
37 DLDag :: ~DLDag()
38 {
39 for ( HeapType::iterator p = Heap.begin(), p_end = Heap.end(); p < p_end; ++p )
40 delete *p;
41 }
42
43 void
44 DLDag :: removeQuery ( void )
45 {
46 for ( size_t i = size()-1; i >= finalDagSize; --i )
47 {
48 DLVertex* v = Heap[i];
49 switch ( v->Type() )
50 {
51 case dtData:
52 static_cast<TDataEntry*>(v->getConcept())->setBP(bpINVALID);
53 break;
54 case dtConcept:
55 static_cast<TConcept*>(v->getConcept())->clear();
56 break;
57 default:
58 break;
59 }
60 delete v;
61 }
62 Heap.resize(finalDagSize);
63 }
64
65 void DLDag :: readConfig ( const ifOptionSet* Options )
66 {
67 fpp_assert ( Options != nullptr ); // safety check
68
69 orSortSat = Options->getText ( "orSortSat" ).c_str();
70 orSortSub = Options->getText ( "orSortSub" ).c_str();
71
72 if ( !isCorrectOption(orSortSat) || !isCorrectOption(orSortSub) )
73 throw EFaCTPlusPlus ( "DAG: wrong OR sorting options" );
74 }
75
76 /// set defaults of OR orderings
77 void
78 DLDag :: setOrderDefaults ( const char* defSat, const char* defSub )
79 {
80 // defaults should be correct
81 fpp_assert ( isCorrectOption(defSat) && isCorrectOption(defSub) );
82
83 if ( LLM.isWritable(llAlways) )
84 LL << "orSortSat: initial=" << orSortSat << ", default=" << defSat;
85 if ( orSortSat[0] == '0' )
86 orSortSat = defSat;
87 if ( LLM.isWritable(llAlways) )
88 LL << ", used=" << orSortSat << "\n"
89 << "orSortSub: initial=" << orSortSub << ", default=" << defSub;
90 if ( orSortSub[0] == '0' )
91 orSortSub = defSub;
92 if ( LLM.isWritable(llAlways) )
93 LL << ", used=" << orSortSub << "\n";
94 }
95
96 /// set OR sort flags based on given option string
97 void DLDag :: setOrderOptions ( const char* opt )
98 {
99 // 0x means not to use OR sort
100 if ( opt[0] == '0' )
101 return;
102
103 sortAscend = (opt[1] == 'a');
104 preferNonGen = (opt[2] == 'p');
105
106 // all statistics use negative version (as it is used in disjunctions)
107 iSort = opt[0] == 'S' ? DLVertex::getStatIndexSize(false)
108 : opt[0] == 'D' ? DLVertex::getStatIndexDepth(false)
109 : opt[0] == 'B' ? DLVertex::getStatIndexBranch(false)
110 : opt[0] == 'G' ? DLVertex::getStatIndexGener(false)
111 : DLVertex::getStatIndexFreq(false);
112
113 Recompute();
114 }
115
116 void
117 DLDag :: computeVertexStat ( BipolarPointer p )
118 {
119 DLVertex& v = (*this)[p];
120 bool pos = isPositive(p);
121
122 // this vertex is already processed
123 if ( v.isProcessed(pos) )
124 return;
125
126 // in case of cycle: mark concept as such
127 if ( v.isVisited(pos) )
128 {
129 v.setInCycle(pos);
130 return;
131 }
132
133 v.setVisited(pos);
134
135 // ensure that the statistic is gather for all sub-concepts of the expression
136 switch ( v.Type() )
137 {
138 case dtAnd: // check all the conjuncts
139 for ( DLVertex::const_iterator q = v.begin(), q_end = v.end(); q < q_end; ++q )
140 computeVertexStat ( *q, pos );
141 break;
142 case dtProj:
143 if ( !pos ) // ~Proj -- nothing to do
144 break;
145 // fallthrough
146 case dtName:
147 case dtForall:
148 case dtChoose:
149 case dtLE: // check a single referenced concept
150 computeVertexStat ( v.getC(), pos );
151 break;
152 default: // nothing to do
153 break;
154 }
155
156 v.setProcessed(pos);
157
158 // here all the necessary statistics is gathered -- use it in the init
159 updateVertexStat(p);
160 }
161
162 void
163 DLDag :: updateVertexStat ( BipolarPointer p )
164 {
165 DLVertex& v = (*this)[p];
166 bool pos = isPositive(p);
167
168 DLVertex::StatType d = 0, s = 1, b = 0, g = 0;
169
170 if ( !v.omitStat(pos) )
171 {
172 if ( isValid(v.getC()) )
173 updateVertexStat ( v, v.getC(), pos );
174 else
175 for ( DLVertex::const_iterator q = v.begin(), q_end = v.end(); q < q_end; ++q )
176 updateVertexStat ( v, *q, pos );
177 }
178
179 // correct values wrt POS
180 d = v.getDepth(pos);
181 switch ( v.Type() )
182 {
183 case dtAnd:
184 if ( !pos )
185 ++b; // OR is branching
186 break;
187 case dtForall:
188 ++d; // increase depth
189 if ( !pos )
190 ++g; // SOME is generating
191 break;
192 case dtLE:
193 ++d; // increase depth
194 if ( !pos )
195 ++g; // >= is generating
196 else if ( v.getNumberLE() != 1 )
197 ++b; // <= is branching
198 break;
199 case dtProj:
200 if ( pos )
201 ++b; // projection sometimes involves branching
202 break;
203 default:
204 break;
205 }
206
207 v.updateStatValues ( d, s, b, g, pos );
208 }
209
210 /// gather vertex freq statistics
211 void
212 DLDag :: computeVertexFreq ( BipolarPointer p )
213 {
214 DLVertex& v = (*this)[p];
215 bool pos = isPositive(p);
216
217 if ( v.isVisited(pos) ) // avoid cycles
218 return;
219
220 v.incFreqValue(pos); // increment frequency of current vertex
221 v.setVisited(pos);
222
223 if ( v.omitStat(pos) ) // negation of primitive concept-like
224 return;
225
226 // increment frequency of all sub-vertices
227 if ( isValid(v.getC()) )
228 computeVertexFreq ( v.getC(), pos );
229 else
230 for ( DLVertex::const_iterator q = v.begin(), q_end = v.end(); q != q_end; ++q )
231 computeVertexFreq ( *q, pos );
232 }
233
234 void
235 DLDag :: gatherStatistic ( void )
236 {
237 // gather main statistics for disjunctions
238 for ( StatVector::iterator p = listAnds.begin(), p_end = listAnds.end(); p < p_end; ++p )
239 computeVertexStat(inverse(*p));
240
241 // if necessary -- gather frequency
242 if ( orSortSat[0] != 'F' && orSortSub[0] != 'F' )
243 return;
244
245 clearDFS();
246
247 for ( BipolarPointer i = (BipolarPointer)size()-1; i > 1; --i )
248 {
249 if ( isCNameTag((*this)[i].Type()) )
250 computeVertexFreq(i);
251 }
252 }
253
254 //---------------------------------------------------
255 // change order of ADD elements with respect to Freq
256 //---------------------------------------------------
257
258 /// return true if p1 is less than p2 using chosen sort order
259
260 // the overall sorted entry structure looks like
261 // fffA..M..Zlll if sortAscend set, and
262 // fffZ..M..Alll if sortAscend cleared.
263 // here 's' means "always first" entries, like neg-primconcepts,
264 // and 'l' means "always last" entries, like looped concepts
265
266 // note that the statistics is given for disjunctions,
267 // so inverted (neg) values are taken into account
268 bool DLDag :: less ( BipolarPointer p1, BipolarPointer p2 ) const
269 {
270 # ifdef ENABLE_CHECKING
271 fpp_assert ( isValid(p1) && isValid(p2) );
272 # endif
273
274 // idea: any positive entry should go first
275 if ( preferNonGen )
276 {
277 if ( isNegative(p1) && isPositive(p2) )
278 return true;
279 if ( isPositive(p1) && isNegative(p2) )
280 return false;
281 }
282
283 const DLVertex& v1 = (*this)[p1];
284 const DLVertex& v2 = (*this)[p2];
285 /*
286 // prefer non-cyclical
287 if ( !v1.isInCycle(false) && v2.isInCycle(false) )
288 return true;
289 if ( !v2.isInCycle(false) && v1.isInCycle(false) )
290 return false;
291 */
292 DLVertex::StatType key1 = v1.getStat(iSort);
293 DLVertex::StatType key2 = v2.getStat(iSort);
294
295 // return "less" wrt sortAscend
296 if ( sortAscend )
297 return key1 < key2;
298 else
299 return key2 < key1;
300 }
301
302 #ifdef RKG_PRINT_DAG_USAGE
303 /// print usage of DAG
304 void DLDag :: PrintDAGUsage ( std::ostream& o ) const
305 {
306 unsigned int n = 0; // number of no-used DAG entries
307 unsigned int total = Heap.size()*2-2; // number of total DAG entries
308
309 for ( HeapType::const_iterator i = Heap.begin(), i_end = Heap.end(); i < i_end; ++i )
310 {
311 if ( (*i)->getUsage(true) == 0 ) // positive and...
312 ++n;
313 if ( (*i)->getUsage(false) == 0 ) // negative ones
314 ++n;
315 }
316
317 o << "There are " << n << " unused DAG entries (" << (unsigned long)(n*100/total)
318 << "% of " << total << " total)\n";
319 }
320 #endif