| 1 |
/* |
| 2 |
* Copyright (C) 2013-2015, 2019 by the Konclude Developer Team. |
| 3 |
* |
| 4 |
* This file is part of the reasoning system Konclude. |
| 5 |
* For details and support, see <http://konclude.com/>. |
| 6 |
* |
| 7 |
* Konclude is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of version 3 of the GNU Lesser General Public |
| 9 |
* License (LGPLv3) as published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* Konclude is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU (Lesser) General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU (Lesser) General Public |
| 17 |
* License along with Konclude. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "CAnsweringSplitPropagationTestControllingItem.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Reasoner { |
| 27 |
|
| 28 |
namespace Answerer { |
| 29 |
|
| 30 |
|
| 31 |
CAnsweringSplitPropagationTestControllingItem::CAnsweringSplitPropagationTestControllingItem(cint64 testingItemId, COptimizedComplexVariableAbsorptionBasedHandlingQueryPartData* absorptionData) { |
| 32 |
mTestingItemId = testingItemId; |
| 33 |
mAbsorptionData = absorptionData; |
| 34 |
mIntegrationOrderedVariableDataCount = 0; |
| 35 |
mNextIntegrationId = 0; |
| 36 |
mBaseSplittingVarData = nullptr; |
| 37 |
mExpectedIntegrationCount = 0; |
| 38 |
mTestActualIntegrationCount = 0; |
| 39 |
mIntendedIntegrationLimit = 0; |
| 40 |
mAllIntegrationTested = false; |
| 41 |
mLastIntegrationTestedVarData = nullptr; |
| 42 |
mLastIntegrationTestedVar = nullptr; |
| 43 |
mNextTestingId = 0; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
CAnsweringSplitPropagationTestControllingItem::~CAnsweringSplitPropagationTestControllingItem() { |
| 48 |
for (QHash<CVariable*, CAnsweringSplitPropagationTestingVariableData*>::const_iterator it = mVariableDataHash.constBegin(), itEnd = mVariableDataHash.constEnd(); it != itEnd; ++it) { |
| 49 |
CAnsweringSplitPropagationTestingVariableData* data = it.value(); |
| 50 |
delete data; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
cint64 CAnsweringSplitPropagationTestControllingItem::getTestingItemId() { |
| 56 |
return mTestingItemId; |
| 57 |
} |
| 58 |
|
| 59 |
CAnsweringSplitPropagationTestControllingItem* CAnsweringSplitPropagationTestControllingItem::setIntegrationLimit(cint64 limit) { |
| 60 |
mIntendedIntegrationLimit = limit; |
| 61 |
return this; |
| 62 |
} |
| 63 |
|
| 64 |
cint64 CAnsweringSplitPropagationTestControllingItem::getNextVariableDataId() { |
| 65 |
return mVariableDataHash.size(); |
| 66 |
} |
| 67 |
|
| 68 |
CAnsweringSplitPropagationTestControllingItem* CAnsweringSplitPropagationTestControllingItem::addVariableData(CAnsweringSplitPropagationTestingVariableData* varData, bool splittingBase) { |
| 69 |
mVariableDataHash.insert(varData->getVariable(), varData); |
| 70 |
mVariableExpressionDataHash.insert(varData->getVariableExpression(), varData); |
| 71 |
|
| 72 |
if (splittingBase && !mBaseSplittingVarData) { |
| 73 |
mBaseSplittingVarData = varData; |
| 74 |
varData->setVariableBindingsIntegrationLimit(-1); |
| 75 |
} else { |
| 76 |
mVariableDataList.append(varData); |
| 77 |
} |
| 78 |
return this; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
CAnsweringSplitPropagationTestControllingItem* CAnsweringSplitPropagationTestControllingItem::updateIntegrationOrderedVariableDataList() { |
| 84 |
if (mIntegrationOrderedVariableDataCount < mVariableDataList.size()) { |
| 85 |
for (CAnsweringSplitPropagationTestingVariableData* variableData : mVariableDataList) { |
| 86 |
if (!variableData->hasVariableDataIntegrationId() && variableData->getVariableBindingsIntegrationCount() > 0) { |
| 87 |
variableData->setVariableDataIntegrationId(mNextIntegrationId++); |
| 88 |
mIntegrationOrderedVariableDataList.append(variableData); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
return this; |
| 93 |
} |
| 94 |
|
| 95 |
CAnsweringSplitPropagationTestingVariableData* CAnsweringSplitPropagationTestControllingItem::getBaseVariableData() { |
| 96 |
return mBaseSplittingVarData; |
| 97 |
} |
| 98 |
|
| 99 |
QList<CAnsweringSplitPropagationTestingVariableData*> CAnsweringSplitPropagationTestControllingItem::getIntegrationOrderedVariableDataList() { |
| 100 |
updateIntegrationOrderedVariableDataList(); |
| 101 |
return mIntegrationOrderedVariableDataList; |
| 102 |
} |
| 103 |
|
| 104 |
cint64 CAnsweringSplitPropagationTestControllingItem::getExpectedIntegrationCount() { |
| 105 |
return mExpectedIntegrationCount; |
| 106 |
} |
| 107 |
|
| 108 |
cint64 CAnsweringSplitPropagationTestControllingItem::getTestActualIntegrationCount() { |
| 109 |
return mTestActualIntegrationCount; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
cint64 CAnsweringSplitPropagationTestControllingItem::getIntendedIntegrationLimit() { |
| 114 |
return mIntendedIntegrationLimit; |
| 115 |
} |
| 116 |
|
| 117 |
bool CAnsweringSplitPropagationTestControllingItem::tryExtendVariableIntegrationLimits(CVariable** integrationExtendedVariable) { |
| 118 |
if (mTestActualIntegrationCount < mIntendedIntegrationLimit / 2) { |
| 119 |
updateIntegrationOrderedVariableDataList(); |
| 120 |
|
| 121 |
bool allVariableIntegration = mVariableDataList.size() == mIntegrationOrderedVariableDataList.size(); |
| 122 |
cint64 collIntegrationCount = 0; |
| 123 |
for (auto rit = mIntegrationOrderedVariableDataList.crbegin(); rit != mIntegrationOrderedVariableDataList.crend(); ++rit) { |
| 124 |
CAnsweringSplitPropagationTestingVariableData* varData = *rit; |
| 125 |
if (varData->hasVariableBiningsIntegrationLimitReached()) { |
| 126 |
cint64 varDataIntCount = varData->getVariableBindingsIntegrationCount(); |
| 127 |
cint64 varDataIntLimit = varData->getVariableBindingsIntegrationLimit(); |
| 128 |
cint64 varDataIntReq = varData->getVariableBindingsRemainingIntegrationRequirementCount(); |
| 129 |
double integratedIndiCountPerBinding = 1; |
| 130 |
collIntegrationCount += varDataIntCount; |
| 131 |
|
| 132 |
integratedIndiCountPerBinding = (double)collIntegrationCount / (double)varDataIntCount; |
| 133 |
double additionalPossibleIntegrationIndiCount = mIntendedIntegrationLimit - mTestActualIntegrationCount; |
| 134 |
|
| 135 |
double varIntegrationLimitExtensibleSize = additionalPossibleIntegrationIndiCount / integratedIndiCountPerBinding; |
| 136 |
if (!allVariableIntegration) { |
| 137 |
varIntegrationLimitExtensibleSize /= 2.; |
| 138 |
} |
| 139 |
cint64 varIntegrationLimitExtensibleCount = varIntegrationLimitExtensibleSize; |
| 140 |
if (varIntegrationLimitExtensibleCount <= 0) { |
| 141 |
varIntegrationLimitExtensibleCount = 1; |
| 142 |
} |
| 143 |
if (integrationExtendedVariable) { |
| 144 |
*integrationExtendedVariable = varData->getVariable(); |
| 145 |
} |
| 146 |
varData->setVariableBindingsIntegrationLimit(varDataIntLimit + varIntegrationLimitExtensibleCount); |
| 147 |
return true; |
| 148 |
} else { |
| 149 |
collIntegrationCount += varData->getVariableBindingsIntegrationCount(); |
| 150 |
if (varData->getVariableBindingsIntegrationCount() <= 0) { |
| 151 |
allVariableIntegration = false; |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
} |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
bool CAnsweringSplitPropagationTestControllingItem::reconfigureVariableIntegrationLimits(cint64 newIntededIntegrationLimit) { |
| 163 |
updateIntegrationOrderedVariableDataList(); |
| 164 |
bool allVariableIntegration = mVariableDataList.size() == mIntegrationOrderedVariableDataList.size(); |
| 165 |
bool previousLimitReached = mTestActualIntegrationCount >= mIntendedIntegrationLimit; |
| 166 |
|
| 167 |
double sufficientChangeFactor = 1.; |
| 168 |
double insufficientChangeFactor = 1.; |
| 169 |
if (mTestActualIntegrationCount > 0) { |
| 170 |
insufficientChangeFactor = (double)newIntededIntegrationLimit / (double)mTestActualIntegrationCount; |
| 171 |
sufficientChangeFactor = insufficientChangeFactor * 1.1; |
| 172 |
} else { |
| 173 |
return false; |
| 174 |
} |
| 175 |
|
| 176 |
double consideringTestIntegrationCount = mTestActualIntegrationCount; |
| 177 |
double collExpectedIntegrationCount = 0; |
| 178 |
for (auto rit = mIntegrationOrderedVariableDataList.crbegin(); rit != mIntegrationOrderedVariableDataList.crend(); ++rit) { |
| 179 |
CAnsweringSplitPropagationTestingVariableData* varData = *rit; |
| 180 |
collExpectedIntegrationCount += varData->getVariableBindingsIntegrationCount(); |
| 181 |
if (!varData->hasVariableBiningsIntegrationLimitReached()) { |
| 182 |
cint64 varDataIntCount = varData->getVariableBindingsIntegrationCount(); |
| 183 |
cint64 varDataIntLimit = varData->getVariableBindingsIntegrationLimit(); |
| 184 |
double integratedIndiCountPerBinding = (double)collExpectedIntegrationCount / (double)varDataIntCount; |
| 185 |
|
| 186 |
varData->setVariableBindingsIntegrationLimit(varDataIntCount * sufficientChangeFactor); |
| 187 |
|
| 188 |
} else { |
| 189 |
|
| 190 |
cint64 varDataIntCount = varData->getVariableBindingsIntegrationCount(); |
| 191 |
cint64 varDataIntLimit = varData->getVariableBindingsIntegrationLimit(); |
| 192 |
cint64 varDataIntReq = varData->getVariableBindingsRemainingIntegrationRequirementCount(); |
| 193 |
double integratedIndiCountPerBinding = (double)collExpectedIntegrationCount / (double)varDataIntCount; |
| 194 |
//double newCollExpectedIntegrationCount = integratedIndiCountPerBinding * (varDataIntCount + varDataIntReq); |
| 195 |
|
| 196 |
double additionalNewIntCount = (newIntededIntegrationLimit - (consideringTestIntegrationCount - collExpectedIntegrationCount)); |
| 197 |
double remainIntIncFactor = additionalNewIntCount / collExpectedIntegrationCount; |
| 198 |
varData->setVariableBindingsIntegrationLimit(varDataIntCount * remainIntIncFactor); |
| 199 |
|
| 200 |
|
| 201 |
double newVarDataIntCount = varDataIntCount * remainIntIncFactor; |
| 202 |
if (newVarDataIntCount > varDataIntCount + varDataIntReq) { |
| 203 |
remainIntIncFactor = qMin(remainIntIncFactor, (double)(varDataIntCount + varDataIntReq) / (double)varDataIntCount); |
| 204 |
newVarDataIntCount = varDataIntCount * remainIntIncFactor; |
| 205 |
|
| 206 |
double newCollExpectedIntegrationCount = collExpectedIntegrationCount * remainIntIncFactor; |
| 207 |
|
| 208 |
double additionalCollExpectedIntegrationCount = newCollExpectedIntegrationCount - collExpectedIntegrationCount; |
| 209 |
collExpectedIntegrationCount = newCollExpectedIntegrationCount; |
| 210 |
consideringTestIntegrationCount += additionalCollExpectedIntegrationCount; |
| 211 |
insufficientChangeFactor = (double)newIntededIntegrationLimit / (double)consideringTestIntegrationCount; |
| 212 |
sufficientChangeFactor = insufficientChangeFactor * 1.1; |
| 213 |
|
| 214 |
varData->setVariableBindingsIntegrationLimit(newVarDataIntCount * insufficientChangeFactor); |
| 215 |
|
| 216 |
} else { |
| 217 |
break; |
| 218 |
} |
| 219 |
|
| 220 |
} |
| 221 |
} |
| 222 |
mIntendedIntegrationLimit = newIntededIntegrationLimit; |
| 223 |
mTestActualIntegrationCount = 0; |
| 224 |
return true; |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
cint64 CAnsweringSplitPropagationTestControllingItem::getNextTestingId() { |
| 230 |
return mNextTestingId++; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
bool CAnsweringSplitPropagationTestControllingItem::hasCheckedAllVariableIntegrations() { |
| 235 |
return mAllIntegrationTested; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
bool CAnsweringSplitPropagationTestControllingItem::disableCheckedVariableIntegrations() { |
| 240 |
mBaseSplittingVarData->resetIntegrations(); |
| 241 |
updateIntegrationOrderedVariableDataList(); |
| 242 |
CAnsweringSplitPropagationTestingVariableData* lastRemainingIntReqVarData = nullptr; |
| 243 |
for (auto rit = mIntegrationOrderedVariableDataList.crbegin(); rit != mIntegrationOrderedVariableDataList.crend(); ++rit) { |
| 244 |
CAnsweringSplitPropagationTestingVariableData* varData = *rit; |
| 245 |
if (varData->getVariableBindingsRemainingIntegrationRequirementCount() > 0) { |
| 246 |
lastRemainingIntReqVarData = varData; |
| 247 |
break; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
if (lastRemainingIntReqVarData) { |
| 252 |
bool varDataIsBeforeLastReachedIntLimit = true; |
| 253 |
for (auto rit = mIntegrationOrderedVariableDataList.crbegin(); rit != mIntegrationOrderedVariableDataList.crend(); ++rit) { |
| 254 |
CAnsweringSplitPropagationTestingVariableData* varData = *rit; |
| 255 |
if (lastRemainingIntReqVarData == varData) { |
| 256 |
// disable bindings |
| 257 |
varData->disableIntegrations(); |
| 258 |
varDataIsBeforeLastReachedIntLimit = false; |
| 259 |
if (varData->hasAllVariableBindingsDisabled()) { |
| 260 |
varData->resetIntegrations(); |
| 261 |
return disableCheckedVariableIntegrations(); |
| 262 |
} |
| 263 |
} else if (!varDataIsBeforeLastReachedIntLimit) { |
| 264 |
// reset bindings |
| 265 |
varData->resetIntegrations(); |
| 266 |
} |
| 267 |
} |
| 268 |
return true; |
| 269 |
} else { |
| 270 |
mAllIntegrationTested = true; |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
CAnsweringSplitPropagationTestingVariableData* CAnsweringSplitPropagationTestControllingItem::getVariableData(CVariable* var) { |
| 278 |
if (mLastIntegrationTestedVar != var) { |
| 279 |
mLastIntegrationTestedVarData = mVariableDataHash.value(var); |
| 280 |
mLastIntegrationTestedVar = var; |
| 281 |
} |
| 282 |
return mLastIntegrationTestedVarData; |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
bool CAnsweringSplitPropagationTestControllingItem::canIntegrateVariableBinding(CVariable* var, const CIndividualReference& indiRef) { |
| 287 |
if (mBaseSplittingVarData->getVariable() == var) { |
| 288 |
return mBaseSplittingVarData->canIntegrateBinding(indiRef, &mTestActualIntegrationCount); |
| 289 |
} else { |
| 290 |
CAnsweringSplitPropagationTestingVariableData* varData = mLastIntegrationTestedVarData; |
| 291 |
if (mLastIntegrationTestedVar != var) { |
| 292 |
mLastIntegrationTestedVarData = mVariableDataHash.value(var); |
| 293 |
mLastIntegrationTestedVar = var; |
| 294 |
} |
| 295 |
if (mLastIntegrationTestedVarData) { |
| 296 |
return mLastIntegrationTestedVarData->canIntegrateBinding(indiRef, &mTestActualIntegrationCount); |
| 297 |
} |
| 298 |
} |
| 299 |
return false; |
| 300 |
} |
| 301 |
|
| 302 |
CAnsweringPropagationExtendedSteeringController* CAnsweringSplitPropagationTestControllingItem::incVariableBindingsRemainingIntegrationRequirementCount(CVariable* var, cint64 count) { |
| 303 |
if (mBaseSplittingVarData->getVariable() != var) { |
| 304 |
CAnsweringSplitPropagationTestingVariableData* varData = mLastIntegrationTestedVarData; |
| 305 |
if (mLastIntegrationTestedVar != var) { |
| 306 |
mLastIntegrationTestedVarData = mVariableDataHash.value(var); |
| 307 |
mLastIntegrationTestedVar = var; |
| 308 |
} |
| 309 |
if (mLastIntegrationTestedVarData) { |
| 310 |
mLastIntegrationTestedVarData->incVariableBindingsRemainingIntegrationRequirementCount(count); |
| 311 |
} |
| 312 |
} |
| 313 |
return this; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
bool CAnsweringSplitPropagationTestControllingItem::finalizeWithClashing() { |
| 318 |
return false; |
| 319 |
} |
| 320 |
|
| 321 |
bool CAnsweringSplitPropagationTestControllingItem::finalizeWithBindingExtraction() { |
| 322 |
return true; |
| 323 |
} |
| 324 |
|
| 325 |
bool CAnsweringSplitPropagationTestControllingItem::isPreparationBindingNominalIndividual(CVariable* variable, CIndividual* indi) { |
| 326 |
return isPreparationBindingNominalIndividual(variable, indi->getIndividualID()); |
| 327 |
} |
| 328 |
|
| 329 |
bool CAnsweringSplitPropagationTestControllingItem::isPreparationBindingNominalIndividual(CVariable* variable, cint64 indiId) { |
| 330 |
CIndividualReference indiRef(indiId); |
| 331 |
return canIntegrateVariableBinding(variable, indiRef); |
| 332 |
} |
| 333 |
|
| 334 |
bool CAnsweringSplitPropagationTestControllingItem::isPreparationBindingAllIndividuals(CVariable* variable) { |
| 335 |
CExpressionVariable* expVariable = mAbsorptionData->getExpressionVariableVariableHash()->value(variable); |
| 336 |
if (mAbsorptionData->getPrepareVariableSet()->contains(expVariable)) { |
| 337 |
return false; |
| 338 |
} |
| 339 |
return false; |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
CAnsweringSplitPropagationTestControllingItem* CAnsweringSplitPropagationTestControllingItem::reset() { |
| 344 |
for (CAnsweringSplitPropagationTestingVariableData* variableData : mVariableDataList) { |
| 345 |
variableData->resetIntegrations(); |
| 346 |
} |
| 347 |
mIntegrationOrderedVariableDataCount = 0; |
| 348 |
mAllIntegrationTested = false; |
| 349 |
mNextTestingId = 0; |
| 350 |
|
| 351 |
return this; |
| 352 |
} |
| 353 |
|
| 354 |
}; // end namespace Answerer |
| 355 |
|
| 356 |
}; // end namespace Reasoner |
| 357 |
|
| 358 |
}; // end namespace Konclude |