def helper_getUnitOutput(inputBoxWithoutInterceptTerm, weightVector, bias, activationFunctionName="tanh"): #V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V # Below requires basically copied from inputBoxToActivationFunctionOutputContainingInterval # after some variable renaming. #--------------------------------------------------------------- requires(isinstance(weightVector, np.ndarray)); requires(len(weightVector.shape) == 1); requires(all(isinstance(x, float) for x in weightVector)); requires(isProperBox(inputBoxWithoutInterceptTerm,)); requires(getDimensionOfBox(inputBoxWithoutInterceptTerm) == weightVector.shape[0]); requires(isinstance(activationFunctionName, str)); requires(activationFunctionName in {"tanh", "linear"}); #^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^ requires(isinstance(bias, float)); inputBoxWithInterceptTerm = getJointBox([inputBoxWithoutInterceptTerm, \ getBox(np.array([1]), np.array([1]))]); assert(isProperBox(inputBoxWithInterceptTerm)); weightAndBias = np.concatenate([weightVector, np.array([bias])]); assert(len(weightAndBias.shape) == 1); assert(weightAndBias.shape[0] == weightVector.shape[0] + 1); # Checking that I placed the terms necessary to make the bias work as expected in the # appropraite locations assert(weightAndBias[-1] == bias); assert(np.all(inputBoxWithInterceptTerm[-1, :] == np.array([1.0, 1.0]))); return inputBoxToActivationFunctionOutputContainingInterval(\ inputBoxWithInterceptTerm, weightAndBias, activationFunctionName=activationFunctionName);
def inputBoxToActivationFunctionOutputContainingInterval(thisBox, weightVector, activationFunctionName ="tanh"): requires(isinstance(weightVector, np.ndarray)); requires(len(weightVector.shape) == 1); requires(all(isinstance(x, float) for x in weightVector)); requires(isProperBox(thisBox)); requires(getDimensionOfBox(thisBox) == weightVector.shape[0]); requires(isinstance(activationFunctionName, str)); requires(activationFunctionName in {"tanh", "linear"}); maximizingInputValues = np.where(weightVector > 0, thisBox[:,1], thisBox[:,0]); minimizingInputValues = np.where(weightVector <= 0, thisBox[:,1], thisBox[:,0]); # ssh-rsa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DNXxQ9TTfDAWQw7ZAyzb0UR8kzQmeKrFbcPQ7uaIqV+HC4hj8COCqb/50xy6ZMwKVccw0mhVSt1NXZgoa6mx6cx251G9crWvxfPpvuYLH2NqnceoeADP8hTiia6N6iN3e4kBzDXHIrsgI6NFd6qW9p9HrFnDmHdakv3qfCJSY8acYdEe9ukRXvheyKGtvqmbMnS2RNDLcMwSQo9aypSPNpHMEXtvVp+vIuiWCR1fjgz8uY1f1Pa0SETX9jrLXfqq1zGeQTmFPR1/ANUbEz25nFIkwSUTr5YduvbFIruZ5cW8CySfKyiun+KclIwKhZVbHXcALjAOc//45HV0gdJfEEnhbUkQ+asWdf3Guyo6Eqd8g40X6XsJiFY5ah7Mc4IacNBzp3cHU3f0ODVjP9xTMMH+cNxq9IYvvhlVp38e8GydYCGoQ79jvKWHLbtsF+Z1j98o7xAxdBRKnCblSOE4anny07LCgm3U18Qft0HFEpIFATnLb3Yfjsjw1sE8Rdj9FBFApVvA3SvjGafvq5b7J9QnTWy80TjwL5zrix6vwxxClT/zjDNX+3PPXVr1FMF+Rhel58tJ8pMQ3TrzC1961GAp5eiYA1zGSyDPz+w== abc@defg activationFunction = None; if(activationFunctionName == "tanh"): activationFunction = tanhActivationFunction; elif(activationFunctionName == "linear"): activationFunction = linearActivationFunction; # for the output layer. else: raise Exception("In the current version of the code, control should never reach here."); assert(activationFunction != None); minimumValue = activationFunction(minimizingInputValues, weightVector); maximumValue = activationFunction(maximizingInputValues, weightVector); boxContainingOutputs = getBox(np.array([minimumValue]), np.array([maximumValue])); ensures(isProperBox(boxContainingOutputs)); ensures(getDimensionOfBox(boxContainingOutputs) == 1); return boxContainingOutputs;
def splitBox_randomNumberOfUniformSplits(thisBox): requires(isProperBox(thisBox)) if (np.all(thisBox[:, 0] == thisBox[:, 1])): # The box is completely flat, nothing can be split... return None indexOfAxisToSplitOn = np.argmax(thisBox[:, 1] - thisBox[:, 0]) # 4/5 probability of numberOfRandomValuesToUse == 2, 1/5 probability of numberOfRandomValuesToUse == 3 numberOfRandomValuesToUse = int(np.random.randint(0, 5) >= 4) + 2 newCutOffs = \ [ ((float(x) / numberOfRandomValuesToUse) * (thisBox[indexOfAxisToSplitOn,1] - thisBox[indexOfAxisToSplitOn,0]) + thisBox[indexOfAxisToSplitOn,0]) \ for x in range(1, numberOfRandomValuesToUse)] newCutoffs = [thisBox[indexOfAxisToSplitOn, 0] ] + list(newCutOffs) + [thisBox[indexOfAxisToSplitOn, 1]] boxesToReturn = [] assert (len(newCutoffs) > 1) for thisIndex in range(0, len(newCutoffs) - 1): newBox = thisBox.copy() newBox[indexOfAxisToSplitOn, :] = np.array( [newCutoffs[thisIndex], newCutoffs[thisIndex + 1]]) boxesToReturn.append(newBox) return boxesToReturn
def recordBoxStats(listOfBoxes, phaseLabel): requires(isinstance(phaseLabel, str)) requires(len(phaseLabel) > 0) requires(isinstance(listOfBoxes, list)) requires(all([isProperBox(x) for x in listOfBoxes])) labelForThisPhaseInQuestionAnswering = "Question_DomainOfVariablesInResponce:" + phaseLabel #V~~VV~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V~V # Below is largely copied from descriptionState.py # TODO: add this sort of code as a funtion in the utils to call upon.... #========================================================================= generalSummaryFunctionsAndLabelsForThem = [\ ( (lambda A : np.prod(np.diff(A, axis=1)) ), "bvolume"), \ ( (lambda A : np.min(np.diff(A, axis=1)) ), "bminSideLength"), \ ( (lambda A : np.max(np.diff(A, axis=1)) ), "bmaxSideLength"), \ ( (lambda A : np.sum(np.diff(A, axis=1)) ), "bsumSideLengths"), \ ] for thisFunctAndLabel in generalSummaryFunctionsAndLabelsForThem: theseValues = [thisFunctAndLabel[0](x) for x in listOfBoxes] resultValue = distributionStatics(theseValues) specificLabel = labelForThisPhaseInQuestionAnswering + ":" + thisFunctAndLabel[ 1] for thisKey in resultValue: commandToExecute = \ "INSERT INTO QAStateValues ( QAStateUUID , fieldName, fieldValue) VALUES ('" + \ ObjDatabaseValueTracker.get_QAStateUUID_mostRecentBeingComputed() + "', '" + (specificLabel + ":" + thisKey) + "', ? );" objDatabaseInterface.interfaceBleed_insertValuesForBlob(\ commandToExecute, [resultValue[thisKey]] ) objDatabaseInterface.commit() #^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^_^ return
def getRandomVectorBetweenScaledExtensionBoxes( thisBox, minInfinityNormPorportionOfDistanceFromBox, maxInfinityNormPorportionOfDistanceFromBox): requires(isProperBox(thisBox)) requires(boxSize(thisBox) > 0.0) # Does not work with flat boxes.... requires(isinstance(minInfinityNormPorportionOfDistanceFromBox, float)) requires(minInfinityNormPorportionOfDistanceFromBox >= 0) requires(isinstance(maxInfinityNormPorportionOfDistanceFromBox, float)) requires(maxInfinityNormPorportionOfDistanceFromBox >= 0) requires(maxInfinityNormPorportionOfDistanceFromBox >= minInfinityNormPorportionOfDistanceFromBox) center = getBoxCenter(thisBox) scalarToPushOut = np.random.rand() * (maxInfinityNormPorportionOfDistanceFromBox - minInfinityNormPorportionOfDistanceFromBox) + \ minInfinityNormPorportionOfDistanceFromBox assert (scalarToPushOut >= minInfinityNormPorportionOfDistanceFromBox) # assuming precision limits don't break this assert (scalarToPushOut <= maxInfinityNormPorportionOfDistanceFromBox) parametersForShiftAboutCenter = 1 - ( 2 * np.random.rand(getDimensionOfBox(thisBox), 1)) assert (np.all(parametersForShiftAboutCenter >= -1)) assert (np.all(parametersForShiftAboutCenter <= 1)) parametersForShiftAboutCenter = parametersForShiftAboutCenter * \ (scalarToPushOut / np.max(np.abs(parametersForShiftAboutCenter))) vectorToReturn = (0.5 * (thisBox[:, 1] - thisBox[:, 0]).reshape( (getDimensionOfBox(thisBox), 1)) * parametersForShiftAboutCenter) + center return vectorToReturn
def convertBoxToFormulaConstraints(self, thisBox): requires(isProperBox(thisBox)) F = z3.And([ \ z3.And( float(thisBox[index, 0]) <= self.listMappingPositionToVariableName[index], \ self.listMappingPositionToVariableName[index] <= float(thisBox[index, 1]) \ ) \ for index in range(0, self.expectedNumberOfDimensions) ]) return F
def _helper_getFunctionToCheckWhetherNoPointsInTheBoxStatisfyCondition_convertBoxToFormulaConstraints(listMappingAxisIndexToVariableInQuestion, thisBox): requires(isProperBox(thisBox)); requires(getDimensionOfBox(thisBox) == len(listMappingAxisIndexToVariableInQuestion)); F = z3.And([ \ z3.And( float(thisBox[index, 0]) <= listMappingAxisIndexToVariableInQuestion[index], \ listMappingAxisIndexToVariableInQuestion[index] <= float(thisBox[index, 1]) \ ) \ for index in range(0, len(listMappingAxisIndexToVariableInQuestion)) ]); return F;
def _helper_getFunctionToCheckWhetherNoPointsInTheBoxStatisfyCondition_convertBoxToFormulaConstraints( self, thisBox): requires(isProperBox(thisBox)) requires( getDimensionOfBox(thisBox) == len( self.variablesConditionMayInclude)) F = z3.And([ \ z3.And( float(thisBox[index, 0]) <= self.variablesConditionMayInclude[index], \ self.variablesConditionMayInclude[index] <= float(thisBox[index, 1]) \ ) \ for index in range(0, len(self.variablesConditionMayInclude)) ]) return F
def convertBoxToFormulaConstraints(self, thisBox): requires(isProperBox(thisBox)) thisBox = self.handleCaseOfJointBox(thisBox) # hacky to put this before a requires... TODO: fix this.... requires( getDimensionOfBox(thisBox) == self.expectedNumberOfDimensions) F = z3.And([ \ z3.And( float(thisBox[index, 0]) <= self.listMappingPositionToVariableName[index], \ self.listMappingPositionToVariableName[index] <= float(thisBox[index, 1]) \ ) \ for index in range(0, self.expectedNumberOfDimensions) \ if self.listMappingPositionToVariableName[index] in self.relaventVariables()]) return F
def existsMemberOfBoxSatifyingCondition(self, thisBox): requires(isProperBox(thisBox)) thisBox = self.handleCaseOfJointBox(thisBox) # hacky to put this before a requires... TODO: fix this.... requires( getDimensionOfBox(thisBox) == self.expectedNumberOfDimensions) self.z3Solver.reset() # this might be the expensive.... I have to check formulaToCheck = \ z3.Exists(self.listMappingPositionToVariableName, \ z3.And(\ self.convertBoxToFormulaConstraints(thisBox), \ self.z3FormattedCondition \ ) \ ) self.z3Solver.add(formulaToCheck) verdict = (self.z3Solver.check() == z3.z3.sat) return verdict
def splitBox_halfLongestAxis(thisBox): requires(isProperBox(thisBox)) if (np.all(thisBox[:, 0] == thisBox[:, 1])): # The box is completely flat, nothing can be split... return None indexOfAxisToSplitOn = np.argmax(thisBox[:, 1] - thisBox[:, 0]) middleOfAxisValue = np.mean(thisBox[indexOfAxisToSplitOn, :]) boxA = thisBox.copy() boxB = thisBox.copy() boxA[indexOfAxisToSplitOn, :] = np.array( [thisBox[indexOfAxisToSplitOn, 0], middleOfAxisValue]) boxB[indexOfAxisToSplitOn, :] = np.array( [middleOfAxisValue, thisBox[indexOfAxisToSplitOn, 1]]) return [boxA, boxB]
def writeBox(fileHandle, thisBox, metaData): requires(isProperBox(thisBox)); requires(isinstance(metaData, list)); requires(len(metaData) == 2); requires(isinstance(metaData[0], int)); requires(isinstance(metaData[1], int)); requires(metaData[0] >= 0); requires(metaData[1] >= 0); requires(metaData[0] <= 255); requires(metaData[1] <= 255); # Below we write out the metadata for thisMetaDataInt in metaData: fileHandle.write(struct.pack( "B", thisMetaDataInt)); numberOfBytesThatAFloatHasOnThisSystem = 4; # makes assumptions # about the system running this code, unless the library # in question standardized it.... numberOfDimensions = getDimensionOfBox(thisBox); boundIndex = 0; while(boundIndex < 2): variableIndex = 0; while(variableIndex < numberOfDimensions): valueToWriteOut = thisBox[variableIndex, boundIndex]; assert(isinstance(valueToWriteOut, float)); numberOfBytesWritten = fileHandle.write(struct.pack( "f", valueToWriteOut)); assert(numberOfBytesWritten == numberOfBytesThatAFloatHasOnThisSystem); assert(variableIndex < variableIndex + 1); # weak overflow check... should not # be necessary in python, but always good practice to include. variableIndex = variableIndex + 1; assert(boundIndex in {0, 1}); boundIndex = boundIndex + 1 return;
def existsMemberOfBoxSatifyingCondition(self, thisBox): requires(isProperBox(thisBox)) self.z3Solver.reset() conditionToCheck = \ z3.And([\ z3.And(\ x.convertBoxToFormulaConstraints(thisBox), \ x.z3FormattedCondition \ ) \ for x in self.listOfConditionsToConjunct \ ] ) setOfVariablesToQuantifyOver = set() for thisCondition in self.listOfConditionsToConjunct: setOfVariablesToQuantifyOver.update( thisCondition.listMappingPositionToVariableName) formulaToCheck = \ z3.Exists(list(setOfVariablesToQuantifyOver), \ conditionToCheck \ ) self.z3Solver.add(formulaToCheck) verdict = (self.z3Solver.check() == z3.z3.sat) return verdict
def getInitialAbstraction_boxesBySign(universeBox): requires(isProperBox(universeBox)) requires(np.all(universeBox[:, 0] < universeBox[:, 1])) numberOfBoxes = 2**(universeBox.shape[0]) listToReturn = [] for thisboxIndex in range(0, numberOfBoxes): thisBox = universeBox.copy() binaryRepresentationOfIndex = np.binary_repr( thisboxIndex, width=getDimensionOfBox(universeBox)) assert (isinstance(binaryRepresentationOfIndex, str)) assert (all([(x in {"1", "0"}) for x in binaryRepresentationOfIndex])) for thisVariableIndex in range(0, getDimensionOfBox(universeBox)): thisBox[ thisVariableIndex, int(binaryRepresentationOfIndex[thisVariableIndex])] = np.mean( thisBox[thisVariableIndex, :]) listToReturn.append(thisBox) return listToReturn
def getConsistentConditions(thisBox, listOfConditions, thisState): requires(isinstance(listOfConditions, list)) requires( all([ isinstance(x, CharacterizationConditionsBaseClass) for x in listOfConditions ])) requires(isProperBox(thisBox)) requires(isinstance(thisState, DescriptionState)) numberOfSamplesToTry = thisState.readParameter("numberOfSamplesToTry") assert (isinstance(numberOfSamplesToTry, int)) assert (numberOfSamplesToTry >= 0) indicesOfCandidateConditions = list(range(0, len(listOfConditions))) samplesToTry = getSampleVectorsToCheckAgainst(thisBox, 0.0, 1.0, numberOfSamplesToTry) indicesOfCandidateConditionsAfterFeasibilityCheck = [] for thisConditionIndex in indicesOfCandidateConditions: success = True for thisSample in samplesToTry: verdict = listOfConditions[ thisConditionIndex].pythonFormatEvaluation(thisSample) if (not verdict): success = False break if (success): indicesOfCandidateConditionsAfterFeasibilityCheck.append( thisConditionIndex) listOfConsistentCondtions = [] for thisConditionIndex in indicesOfCandidateConditionsAfterFeasibilityCheck: if (listOfConditions[thisConditionIndex]. allMembersOfBoxSatisfyCondition(thisBox)): listOfConsistentCondtions.append(thisConditionIndex) return listOfConsistentCondtions
def readBoxFromFile(fhToRead, dimensionOfBox, numberOfBytesThatAFloatHasOnThisSystem): requires(isinstance(dimensionOfBox, int)); requires(dimensionOfBox > 0); boxToReturn = np.zeros((dimensionOfBox, 2)); valueToUnpack = [fhToRead.read(1), fhToRead.read(1)]; if(len(valueToUnpack[0]) == 0): return None; metaData = [struct.unpack( "B", valueToUnpack[x])[0] for x in [0,1]]; # somewhat hacky way to write this..... boundIndex = 0; # ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAIAQC/RPJH+HUB5ZcSOv61j5AKWsnP6pwitgIsRHKQ5PxlrinTbKATjUDSLFLIs/cZxRb6Op+aRbssiZxfAHauAfpqoDOne5CP7WGcZIF5o5o+zYsJ1NzDUWoPQmil1ZnDCVhjlEB8ufxHaa/AFuFK0F12FlJOkgVT+abIKZ19eHi4C+Dck796/ON8DO8B20RPaUfetkCtNPHeb5ODU5E5vvbVaCyquaWI3u/uakYIx/OZ5aHTRoiRH6I+eAXxF1molVZLr2aCKGVrfoYPm3K1CzdcYAQKQCqMp7nLkasGJCTg1QFikC76G2uJ9QLJn4TPu3BNgCGwHj3/JkpKMgUpvS6IjNOSADYd5VXtdOS2xH2bfpiuWnkBwLi9PLWNyQR2mUtuveM2yHbuP13HsDM+a2w2uQwbZgHC2QVUE6QuSQITwY8RkReMKBJwg6ob2heIX+2JQUniF8GKRD7rYiSm7dJrYhQUBSt4T7zN4M5EDg5N5wAiT5hLumVqpAkU4JeJo5JopIohEBW/SknViyiXPqBfrsARC9onKSLp5hJMG1FAACezPAX8ByTOXh4r7rO0UPbZ1mqX1P6hMEkqb/Ut9iEr7fR/hX7WD1fpcOBbwksBidjs2rzwurVERQ0EQfjfw1di1uPR/yzLVfZ+FR2WfL+0FJX/sCrfhPU00y5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+mmstw901weMrcK1cZwxM8GY2VGUErV3dG16h7CqRJpTLn0GxDkxaEiMItcPauV0g10VWNziTaP/wU3SOY5jV0z2WbmcZCLP40IaXXPL67qE3q1x/a18geSFKIM8vIHG8xNlllfJ60THP9X/Kj8GDpQIBvsaSiGh8z3XpxyuwbQIt/tND+i2FndrM0pBSqP8U3n7EzJfbYwEzqU9fJazWFoT4Lpv/mENaFGFe3pgUBv/qIoGqv2/G5u0RqdtToUA6gR9bIdiQpK3ZSNRMM2WG/rYs1c6FDP8ZGKBh+vzfA1zVEOKmJsunG0RU9yinFhotMlix14KhZMM6URZpDGN+zZ9lWMs6UMbfAwHMM+2MqTo6Se7var7uY5GDNXxQ9TTfDAWQw7ZAyzb0UR8kzQmeKrFbcPQ7uaIqV+HC4hj8COCqb/50xy6ZMwKVccw0mhVSt1NXZgoa6mx6cx251G9crWvxfPpvuYLH2NqnceoeADP8hTiia6N6iN3e4kBzDXHIrsgI6NFd6qW9p9HrFnDmHdakv3qfCJSY8acYdEe9ukRXvheyKGtvqmbMnS2RNDLcMwSQo9aypSPNpHMEXtvVp+vIuiWCR1fjgz8uY1f1Pa0SETX9jrLXfqq1zGeQTmFPR1/ANUbEz25nFIkwSUTr5YduvbFIruZ5cW8CySfKyiun+KclIwKhZVbHXcALjAOc//45HV0gdJfEEnhbUkQ+asWdf3Guyo6Eqd8g40X6XsJiFY5ah7Mc4IacNBzp3cHU3f0ODVjP9xTMMH+cNxq9IYvvhlVp38e8GydYCGoQ79jvKWHLbtsF+Z1j98o7xAxdBRKnCblSOE4anny07LCgm3U18Qft0HFEpIFATnLb3Yfjsjw1sE8Rdj9FBFApVvA3SvjGafvq5b7J9QnTWy80TjwL5zrix6vwxxClT/zjDNX+3PPXVr1FMF+Rhel58tJ8pMQ3TrzC1961GAp5eiYA1zGSyDPz+w== abc@defg while(boundIndex < 2): variableIndex = 0; while(variableIndex < dimensionOfBox): valueToUnpack = fhToRead.read(numberOfBytesThatAFloatHasOnThisSystem); if(len(valueToUnpack) == 0): assert(boundIndex == 0 and variableIndex == 0); # in other words, not just # part of the way through reading a box. return None; unpackedValue = struct.unpack( "f", valueToUnpack)[0]; assert(isinstance(unpackedValue, float)); assert(not np.isnan(unpackedValue)); boxToReturn[variableIndex , boundIndex] = unpackedValue; assert(variableIndex < variableIndex + 1); # weak overflow check... should not # be necessary in python, but always good practice to include. variableIndex = variableIndex + 1; assert(boundIndex in {0, 1}); boundIndex = boundIndex + 1 ensures(isProperBox(boxToReturn)); return (boxToReturn, metaData);
def splitBox_randomSplittingLongestAxis(thisBox): requires(isProperBox(thisBox)) if (np.all(thisBox[:, 0] == thisBox[:, 1])): # The box is completely flat, nothing can be split... return None indexOfAxisToSplitOn = np.argmax(thisBox[:, 1] - thisBox[:, 0]) # middleOfAxisValue = np.mean(thisBox[indexOfAxisToSplitOn, :]); numberOfRandomValuesToUse = np.random.randint(2, 5) newCutOffs = \ np.sort(np.random.rand(numberOfRandomValuesToUse) * (thisBox[indexOfAxisToSplitOn,1] - thisBox[indexOfAxisToSplitOn,0]) + thisBox[indexOfAxisToSplitOn,0]) newCutoffs = [thisBox[indexOfAxisToSplitOn, 0] ] + list(newCutOffs) + [thisBox[indexOfAxisToSplitOn, 1]] boxesToReturn = [] assert (len(newCutoffs) > 1) for thisIndex in range(0, len(newCutoffs) - 1): newBox = thisBox.copy() newBox[indexOfAxisToSplitOn, :] = np.array( [newCutoffs[thisIndex], newCutoffs[thisIndex + 1]]) boxesToReturn.append(newBox) return boxesToReturn
def __init__(self, z3Solver, thisBox, listMappingAxisIndexToVariableInQuestion): requires(isProperBox(thisBox)) requires(isinstance(listMappingAxisIndexToVariableInQuestion, list)) requires( all([ isinstance(x, z3.z3.ArithRef) for x in listMappingAxisIndexToVariableInQuestion ])) requires( len(listMappingAxisIndexToVariableInQuestion) == getDimensionOfBox( thisBox)) self.expectedNumberOfDimensions = getDimensionOfBox(thisBox) self.listMappingPositionToVariableName = listMappingAxisIndexToVariableInQuestion self.personalBox = thisBox self.z3Solver = z3Solver # Including for uniformity of interface so that the # metaConditions can use it, not that it is used in this class itself. self.z3FormattedCondition = self.convertBoxToFormulaConstraints( thisBox) # None; self.setID() return
def getInitialListOfConditionsConsistentWithBoxes(\ listOfBoxes, listOfConditions, listMappingAxisIndexToVariableInQuestion, thisState): requires(isinstance(thisState, DescriptionState)) requires(isinstance(listOfConditions, list)) requires( all([ isinstance(x, CharacterizationConditionsBaseClass) for x in listOfConditions ])) requires(isinstance(listOfBoxes, list)) requires(len(listOfBoxes) > 0) requires(all([isProperBox(thisBox) for thisBox in listOfBoxes])) requires(isinstance(listMappingAxisIndexToVariableInQuestion, list)) requires( all([ isinstance(x, z3.z3.ArithRef) for x in listMappingAxisIndexToVariableInQuestion ])) requires( len(listMappingAxisIndexToVariableInQuestion) == getDimensionOfBox( listOfBoxes[0])) listOfConditions_after = listOfConditions.copy() # see the condition where "if(len(consistentConditions) == 0):" # below for why this was necessary... dictMappingConditionToBoxesItIsConsistentWith = dict() listOfSetsCoveringBox = [] boxIndex = 0 for thisBox in listOfBoxes: consistentConditions = [listOfConditions[x] for x in \ getConsistentConditions(thisBox, listOfConditions, thisState) ] # below chunk is important for removing redundant (in respect to logical implication) predicates # at the end. for thisCondition in consistentConditions: thisConditionID = thisCondition.getID() if (thisConditionID not in dictMappingConditionToBoxesItIsConsistentWith): dictMappingConditionToBoxesItIsConsistentWith[ thisConditionID] = set() dictMappingConditionToBoxesItIsConsistentWith[thisConditionID].add( boxIndex) assert (boxIndex + 1 > boxIndex) # weak overflow check.... boxIndex = boxIndex + 1 subIndicesOfMostSpecificConsistentConditions = getMostSpecificCondition(\ thisBox, \ consistentConditions, \ thisState) if ((len(consistentConditions) == 0) or (subIndicesOfMostSpecificConsistentConditions == None)): setOfConditionsCoveringThisBox = set() produceGreaterAbstraction = thisState.readParameter( "produceGreaterAbstraction") assert (isinstance(produceGreaterAbstraction, bool)) if ((not produceGreaterAbstraction) or (len(consistentConditions) == 0)): thisConditionForBox = Condition_TheBoxItself(\ listOfConditions[0].z3Solver,thisBox, listMappingAxisIndexToVariableInQuestion) listOfConditions_after.append(thisConditionForBox) # In the line below, we have boxIndex -1 because boxIndex was INCREMENTED prior to reaching # here - the boxIndex corresponding to thisBox is thus (boxIndex -1) dictMappingConditionToBoxesItIsConsistentWith[ thisConditionForBox.getID()] = set([boxIndex - 1]) assert (boxIndex - 1 >= 0) assert (boxIndex - 1 < boxIndex) # though technically not possible in python integers, checking # integer arithmetic behavior.... though, given the assert prior, underflow should be impossible... setOfConditionsCoveringThisBox.update( [thisConditionForBox.getID()]) else: setOfConditionsCoveringThisBox.update( {x.getID() for x in consistentConditions}) listOfSetsCoveringBox.append(setOfConditionsCoveringThisBox) else: mostSpecificConsistentConditions = [consistentConditions[x] for x in \ subIndicesOfMostSpecificConsistentConditions] listOfSetsCoveringBox.append( {x.getID() for x in mostSpecificConsistentConditions}) return (listOfConditions_after, dictMappingConditionToBoxesItIsConsistentWith, listOfSetsCoveringBox)
def getBoxCenter(thisBox): requires(isProperBox(thisBox)) # below, we add first because we are more concerned about precision loss / under flow than overflow. return (thisBox[:, 0].reshape( (getDimensionOfBox(thisBox), 1)) + thisBox[:, 1].reshape( (getDimensionOfBox(thisBox), 1))) * 0.5
def generateDescription(listOfBoxes, listOfConditions, listMappingAxisIndexToVariableInQuestion, thisState): requires(isinstance(listOfConditions, list)) requires( all([ isinstance(x, CharacterizationConditionsBaseClass) for x in listOfConditions ])) requires(isinstance(listOfBoxes, list)) requires(all([isProperBox(thisBox) for thisBox in listOfBoxes])) requires(isinstance(listMappingAxisIndexToVariableInQuestion, list)) requires( all([ isinstance(x, z3.z3.ArithRef) for x in listMappingAxisIndexToVariableInQuestion ])) requires( len(listOfBoxes) == 0 or \ len(listMappingAxisIndexToVariableInQuestion) == getDimensionOfBox(listOfBoxes[0])) requires(isinstance(thisState, DescriptionState)) if (len(listOfBoxes) == 0): raise NotImplementedError("There is No Situation where the State of Affairs Asked-About Occurs "+ \ "(in terms of mechanics, this means no boxes where found containing any elements "+\ "that are applicable to your question).") assert (len(listOfBoxes) > 0) (listOfConditions_after, dictMappingConditionToBoxesItIsConsistentWith, listOfSetsCoveringBox) = \ getInitialListOfConditionsConsistentWithBoxes(\ listOfBoxes, listOfConditions, listMappingAxisIndexToVariableInQuestion, thisState) # Below two variables are for use in the set coverings. dimensionOfSpace = getDimensionOfBox(listOfBoxes[0]) dictMappingBElementsToConditions = { x.getID(): x for x in listOfConditions_after } for thisElem in dictMappingBElementsToConditions: if ( isinstance(thisElem, frozenset) ): # occurs in the case of an instance of MetaCondition_Conjunction for thisSubElem in thisElem: dictMappingBElementsToConditions[thisSubElem] = [ x for x in listOfConditions_after if x.getID() == thisSubElem ][0] coveringDescriptionsInitial = getApproximateMultivariateSetCover( listOfSetsCoveringBox, dimensionOfSpace, dictMappingBElementsToConditions) assert (isinstance(coveringDescriptionsInitial, list)) coveringDescriptionsFiltered = set() # coveringDescriptionsInitial.copy(); coveringDescriptionsInitial = list(coveringDescriptionsInitial) listOfConditions_after = listOfConditions_after + list( coveringDescriptionsInitial) handleNewInstancesOf_MetaCondition_Conjunction( coveringDescriptionsInitial, dictMappingConditionToBoxesItIsConsistentWith) listMappingFromBoxIndexToSetOfChosenConditionsCoveringIt = [ set() for x in range(0, len(listOfBoxes)) ] dictMappingBElementsToConditions = dict() forCheckPurposes_setOfBoxesCovered = set() for thisConditionIndex in range(0, len(coveringDescriptionsInitial)): thisConditionID = coveringDescriptionsInitial[ thisConditionIndex].getID() dictMappingBElementsToConditions[ thisConditionID] = coveringDescriptionsInitial[thisConditionIndex] indicesOfBoxesCoveredByCondition = dictMappingConditionToBoxesItIsConsistentWith[ thisConditionID] assert (len(indicesOfBoxesCoveredByCondition) > 0) # otherwise it should not be in the minimal set covering # description... assert (isinstance(indicesOfBoxesCoveredByCondition, set)) forCheckPurposes_setOfBoxesCovered.update( indicesOfBoxesCoveredByCondition) assert (forCheckPurposes_setOfBoxesCovered.issuperset( indicesOfBoxesCoveredByCondition)) for thisBoxIndex in indicesOfBoxesCoveredByCondition: listMappingFromBoxIndexToSetOfChosenConditionsCoveringIt[ thisBoxIndex].update([thisConditionID]) assert ( thisConditionID in listMappingFromBoxIndexToSetOfChosenConditionsCoveringIt[ thisBoxIndex]) assert (forCheckPurposes_setOfBoxesCovered == { x for x in range(0, len(listOfBoxes)) }) # ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAIAQC/RPJH+HUB5ZcSOv61j5AKWsnP6pwitgIsRHKQ5PxlrinTbKATjUDSLFLIs/cZxRb6Op+aRbssiZxfAHauAfpqoDOne5CP7WGcZIF5o5o+zYsJ1NzDUWoPQmil1ZnDCVhjlEB8ufxHaa/AFuFK0F12FlJOkgVT+abIKZ19eHi4C+Dck796/ON8DO8B20RPaUfetkCtNPHeb5ODU5E5vvbVaCyquaWI3u/uakYIx/OZ5aHTRoiRH6I+eAXxF1molVZLr2aCKGVrfoYPm3K1CzdcYAQKQCqMp7nLkasGJCTg1QFikC76G2uJ9QLJn4TPu3BNgCGwHj3/JkpKMgUpvS6IjNOSADYd5VXtdOS2xH2bfpiuWnkBwLi9PLWNyQR2mUtuveM2yHbuP13HsDM+a2w2uQwbZgHC2QVUE6QuSQITwY8RkReMKBJwg6ob2heIX+2JQUniF8GKRD7rYiSm7dJrYhQUBSt4T7zN4M5EDg5N5wAiT5hLumVqpAkU4JeJo5JopIohEBW/SknViyiXPqBfrsARC9onKSLp5hJMG1FAACezPAX8ByTOXh4r7rO0UPbZ1mqX1P6hMEkqb/Ut9iEr7fR/hX7WD1fpcOBbwksBidjs2rzwurVERQ0EQfjfw1di1uPR/yzLVfZ+FR2WfL+0FJX/sCrfhPU00y5Q4Te8XqrJwqkbVMZ8fuSBk+wQA5DZRNJJh9pmdoDBi/hNfvcgp9m1D7Z7bUbp2P5cQTgay+Af0P7I5+myCscLXefKSxXJHqRgvEDv/zWiNgqT9zdR3GoYVHR/cZ5XpZhyMpUIsFfDoWfAmHVxZNXF0lKzCEH4QXcfZJgfiP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M0pBSqP8U3n7EzJfbYwEzqU9fJazWFoT4Lpv/mENaFGFe3pgUBv/qIoGqv2/G5u0RqdtToUA6gR9bIdiQpK3ZSNRMM2WG/rYs1c6FDP8ZGKBh+vzfA1zVEOKmJsunG0RU9yinFhotMlix14KhZMM6URZpDGN+zZ9lWMs6UMbfAwHMM+2MqTo6Se7var7uY5GDNXxQ9TTfDAWQw7ZAyzb0UR8kzQmeKrFbcPQ7uaIqV+HC4hj8COCqb/50xy6ZMwKVccw0mhVSt1NXZgoa6mx6cx251G9crWvxfPpvuYLH2NqnceoeADP8hTiia6N6iN3e4kBzDXHIrsgI6NFd6qW9p9HrFnDmHdakv3qfCJSY8acYdEe9ukRXvheyKGtvqmbMnS2RNDLcMwSQo9aypSPNpHMEXtvVp+vIuiWCR1fjgz8uY1f1Pa0SETX9jrLXfqq1zGeQTmFPR1/ANUbEz25nFIkwSUTr5YduvbFIruZ5cW8CySfKyiun+KclIwKhZVbHXcALjAOc//45HV0gdJfEEnhbUkQ+asWdf3Guyo6Eqd8g40X6XsJiFY5ah7Mc4IacNBzp3cHU3f0ODVjP9xTMMH+cNxq9IYvvhlVp38e8GydYCGoQ79jvKWHLbtsF+Z1j98o7xAxdBRKnCblSOE4anny07LCgm3U18Qft0HFEpIFATnLb3Yfjsjw1sE8Rdj9FBFApVvA3SvjGafvq5b7J9QnTWy80TjwL5zrix6vwxxClT/zjDNX+3PPXVr1FMF+Rhel58tJ8pMQ3TrzC1961GAp5eiYA1zGSyDPz+w== abc@defg for thisElem in listOfConditions: dictMappingBElementsToConditions[thisElem.getID()] = thisElem coveringDescriptionsFiltered = list(getApproximateMultivariateSetCover(\ listMappingFromBoxIndexToSetOfChosenConditionsCoveringIt, dimensionOfSpace, dictMappingBElementsToConditions)) listOfConditions_after = listOfConditions_after + list( coveringDescriptionsFiltered) handleNewInstancesOf_MetaCondition_Conjunction( coveringDescriptionsFiltered, dictMappingConditionToBoxesItIsConsistentWith) dictMappingConditionIDToVolumeCoveredAndUniqueVolumeCovered_initial = \ getVolumesCoveredInformation(listOfBoxes, coveringDescriptionsFiltered, \ dictMappingConditionToBoxesItIsConsistentWith) coveringDescriptionsFiltered = removePredicatesImpliedByOthers(\ coveringDescriptionsFiltered, dictMappingConditionToBoxesItIsConsistentWith, \ listOfBoxes, listMappingAxisIndexToVariableInQuestion, dictMappingConditionIDToVolumeCoveredAndUniqueVolumeCovered_initial) (coveringDescriptionsFiltered, listOfConditions_after, dictMappingConditionToBoxesItIsConsistentWith) = \ handleNewInstancesOf_BoxItself( \ coveringDescriptionsFiltered, listOfConditions_after, listMappingAxisIndexToVariableInQuestion, dictMappingConditionToBoxesItIsConsistentWith) dictMappingConditionIDToVolumeCoveredAndUniqueVolumeCovered = \ getVolumesCoveredInformation(listOfBoxes, coveringDescriptionsFiltered, dictMappingConditionToBoxesItIsConsistentWith) assert (set(listOfConditions_after).issuperset(listOfConditions)) ensures({ x.getID() for x in coveringDescriptionsFiltered if not isinstance(x, MetaCondition_Conjunction) }.issubset({x.getID() for x in listOfConditions_after})) ensures({x.getID() for x in coveringDescriptionsFiltered}.union(set(["totalVolumeOfBoxesInList"])) == \ set(dictMappingConditionIDToVolumeCoveredAndUniqueVolumeCovered.keys())) return {"description" : coveringDescriptionsFiltered, \ "listOfConditions_after" : listOfConditions_after ,\ "dictMappingConditionIDToVolumeCoveredAndUniqueVolumeCovered" : dictMappingConditionIDToVolumeCoveredAndUniqueVolumeCovered}
def splitBox(thisBox, stringSpecifyingStyleOfSplit, scalingFactors=None): requires(isProperBox(thisBox)) requires(isinstance(stringSpecifyingStyleOfSplit, str)) requires((isinstance(scalingFactors, type(None))) or (isinstance(scalingFactors, np.ndarray))) requires((isinstance(scalingFactors, type(None))) or (scalingFactors.shape == tuple([getDimensionOfBox(thisBox)]))) if (isinstance(scalingFactors, type(None))): raise Exception("While scalingFactors = None is supported in splitBox currently, use of " + \ "None as the scalling axis has been disabled elsewhere in the code. Thus, this is a " +\ "sign that the caller is in error - which is why this function (splitBox) is raising an " + \ "exception to alert the user to something that should not be occuring if the code is in " + \ "sync on the latest revisions.") dictMappingStyleOfSplitToFunction = {\ "halvingAllAxis" : splitBox_halvingAllAxis ,\ "halfLongestAxis" : splitBox_halfLongestAxis, \ "randomSplittingLongestAxis" : splitBox_randomSplittingLongestAxis, \ "randomNumberOfUniformSplits_old" : splitBox_randomNumberOfUniformSplits_old, \ "randomNumberOfUniformSplits" : splitBox_randomNumberOfUniformSplits \ } if (stringSpecifyingStyleOfSplit not in dictMappingStyleOfSplitToFunction.keys()): raise Exception( "splitBox: stringSpecifyingStyleOfSplit not in dictMappingStyleOfSplitToFunction.keys()" ) boxToSplit = thisBox if (not isinstance(scalingFactors, type(None))): boxToSplit = thisBox / scalingFactors.reshape( (getDimensionOfBox(thisBox), 1)) boxToSplit[np.isnan(boxToSplit)] = 0 rawReturn = dictMappingStyleOfSplitToFunction[ stringSpecifyingStyleOfSplit](boxToSplit) valueToReturn = [] if (not isinstance(scalingFactors, type(None))): for thisTempBox in rawReturn: valueToReturn.append(thisTempBox * scalingFactors.reshape( (getDimensionOfBox(thisBox), 1))) valueToReturn[-1][np.isnan(scalingFactors)] = \ thisBox[np.isnan(scalingFactors)] else: valueToReturn = rawReturn # The value valueToReturn is allowed to be None when the splitting # procedure values to produce a new box - that is, the point is a # fixed point for the splitting procedure. Letting the calling # function now this by returning None explicitly can be helpful. ensures((valueToReturn == None) or (isinstance(valueToReturn, list))) ensures((valueToReturn == None) or (len(valueToReturn) >= 2)) ensures((valueToReturn == None) or all([isProperBox(x) for x in valueToReturn])) ensures((valueToReturn == None) or np.all(np.isclose(getContainingBox(valueToReturn), thisBox))) # Below basically says that if valueToReturn is not-None and # the user did not specify a scaling factor, than equality should hold.... this is probably # a skectchy guarentee to even try to make considering we want to allow # more advanced splitting, but for now it looks like it will hold, and it is nice to know. ensures( (valueToReturn == None) or (isinstance(scalingFactors, type(None)) or np.all(np.isclose(getContainingBox(valueToReturn), thisBox)))) return valueToReturn
def getMostSpecificCondition(thisBox, listOfConditions, thisState): """ Given a list of predicates that are consistent with the box provided, randomly samples AROUND (not inside) the box provided to determine which of the predicates is most specific. In particular, we sample vectors at increasing distances from the box (based on l_{infinity} norm from the closest box side) and whichever predicates become false first (i.e., at a certain "raduis" of sampling, a subset of the predicates is no longer consistent with the data sampled) are considered more specifically consistent with the box in question. Bear in mind that this is a randomized approach, so results may vary. """ requires(isinstance(listOfConditions, list)) requires( all([ isinstance(x, CharacterizationConditionsBaseClass) for x in listOfConditions ])) # Below is a sanity check- we want all the members of listOfConditions to be true over every element in # thisBox, so they should at least be true for the vector at the center of the box.... requires( all([ x.pythonFormatEvaluation(getBoxCenter(thisBox)) for x in listOfConditions ])) requires(isProperBox(thisBox)) requires(isinstance(thisState, DescriptionState)) numberOfSamplesToTry = thisState.readParameter("numberOfSamplesToTry") assert (isinstance(numberOfSamplesToTry, int)) assert (numberOfSamplesToTry >= 0) exponentialComponent = thisState.readParameter("exponentialComponent") assert (isinstance(exponentialComponent, float)) assert (exponentialComponent >= 0.0) numberOfDimensionToCover = getDimensionOfBox(thisBox) if (len(listOfConditions) == 0): return None dimensionsCovered = set() minAndMaxInfinityNormPorportionOfDistanceFromBox = [\ [1.0, 1.01], \ [1.01,1.05], \ [1.05, 1.10], \ [1.10, 1.20], \ [1.20, 1.40], \ [1.40, 1.80], \ [1.80, 2.60] ] for thisRowIndex in range( 0, len(minAndMaxInfinityNormPorportionOfDistanceFromBox)): for columnIndex in [0, 1]: thisValue = minAndMaxInfinityNormPorportionOfDistanceFromBox[ thisRowIndex][columnIndex] minAndMaxInfinityNormPorportionOfDistanceFromBox[thisRowIndex][columnIndex] = \ thisValue * np.exp(exponentialComponent * thisValue) setOfIndicesOfCandidateMostSpecificValues = set() success = False for thisMinAndMaxPorpDistance in minAndMaxInfinityNormPorportionOfDistanceFromBox: assert (thisMinAndMaxPorpDistance[0] >= 1.0) assert (thisMinAndMaxPorpDistance[0] < thisMinAndMaxPorpDistance[1]) samples = getSampleVectorsToCheckAgainst(thisBox, \ thisMinAndMaxPorpDistance[0], thisMinAndMaxPorpDistance[1], numberOfSamplesToTry) temp_newVariablesCovered = set() for thisIndex in range(0, len(listOfConditions)): if (thisIndex in setOfIndicesOfCandidateMostSpecificValues): continue if (set(listOfConditions[thisIndex].relaventVariables()).issubset( dimensionsCovered)): continue for thisSample in samples: if ((not listOfConditions[thisIndex].pythonFormatEvaluation( thisSample))): # and \ setOfIndicesOfCandidateMostSpecificValues.add(thisIndex) temp_newVariablesCovered.update( listOfConditions[thisIndex].relaventVariables()) # notice that we append in the new variables covered only AFTER we are done evaluating for candidate # conditions based on thisMinAndMaxPorpDistance. That way, conditions covering the same variables # but in the same partition are both drawn in, as desired. dimensionsCovered.update(temp_newVariablesCovered) if (len(dimensionsCovered) == numberOfDimensionToCover): assert (len(setOfIndicesOfCandidateMostSpecificValues) > 0) return setOfIndicesOfCandidateMostSpecificValues assert (len(dimensionsCovered) < numberOfDimensionToCover) if (len(setOfIndicesOfCandidateMostSpecificValues) != 0): assert (len(dimensionsCovered) > 0) return setOfIndicesOfCandidateMostSpecificValues assert (setOfIndicesOfCandidateMostSpecificValues == set()) return None
def getRelaventInputBoxes(self, thisInstanceOfModelBoxProgatorManager, thisState): requires(isinstance(thisState, DescriptionState)) functionToStatisfy = self.helper_getRelaventInputBoxes_get_functionToStatisfy( ) functionToCheckWhetherNoPointsInTheBoxStatisfyCondition = self.getFunctionToCheckWhetherNoPointsInTheBoxStatisfyCondition( ) epsilonForBoxSize = thisState.readParameter( "floatValueForBoxDivisionCutoff") splitOnlyOnRelaventVariables = thisState.readParameter( "splitOnlyOnRelaventVariables") assert (isinstance(epsilonForBoxSize, float)) assert (epsilonForBoxSize > 0) assert (isinstance(splitOnlyOnRelaventVariables, bool)) axisToSplitOn = list( range( 0, getDimensionOfBox(self.domainInfo.getInputSpaceUniverseBox()))) if (splitOnlyOnRelaventVariables): setOfRelaventVariables = set() for thisCondition in self.conditionsToBeConsistentWith: setOfRelaventVariables.update( thisCondition.relaventVariables()) tempAxisToSplitOn=[\ thisIndex for thisIndex in axisToSplitOn \ if (self.domainInfo.inputSpaceVariables()[thisIndex] in setOfRelaventVariables)] if (len(tempAxisToSplitOn) > 0): #This would fail to happen, for instanc, when # self.conditionsToBeConsistentWith specify conditions over the output space # as oppossed to the input space. axisToSplitOn = tempAxisToSplitOn assert (len(axisToSplitOn) > 0) # The below assert is most easily understood from converting (p or q) to (if (not p) then q). # That is, if axisToSplitOn does not include the whole input space, it is because # splitOnlyOnRelaventVariables has been enabled. Note that this does NOT say the converse, # since even if splitOnlyOnRelaventVariables is True, axisToSplitOn might not reduce in # size for a number of reasons. assert( (len(axisToSplitOn) == len(self.domainInfo.inputSpaceVariables())) or \ splitOnlyOnRelaventVariables) assert (len(axisToSplitOn) <= len( self.domainInfo.inputSpaceVariables())) assert (set(axisToSplitOn).issubset( range(0, len(self.domainInfo.inputSpaceVariables())))) # Below checks that the elements of axisToSplitOn are unique assert (len(set(axisToSplitOn)) == len(axisToSplitOn)) axisScaling = self.domainInfo.getInputSpaceUniverseBox( )[:, 1] - self.domainInfo.getInputSpaceUniverseBox()[:, 0] assert (all(axisScaling >= 0.0)) indicesWhereAxisNotFlat = np.where(axisScaling[axisToSplitOn] > 0)[-1] assert (np.all( axisScaling[axisToSplitOn][indicesWhereAxisNotFlat] > 0)) def functionToDetermineWhenToGiveUpOnBox_axisSmallAfterScalingByUniverseSize( thisBox): return np.max( (thisBox[axisToSplitOn,:][indicesWhereAxisNotFlat,1] - thisBox[axisToSplitOn,:][indicesWhereAxisNotFlat,0]) /\ axisScaling[axisToSplitOn][indicesWhereAxisNotFlat]) <= epsilonForBoxSize thisCEGARFileWrittingManagerInstance = analysis(self.domainInfo.getInputSpaceUniverseBox(), thisInstanceOfModelBoxProgatorManager, \ functionToStatisfy, functionToDetermineWhenToGiveUpOnBox_axisSmallAfterScalingByUniverseSize, \ limitSplittingToAxisWithIndicesInThisList=axisToSplitOn, \ functionToCheckWhetherNoPointsInTheBoxStatisfyCondition=functionToCheckWhetherNoPointsInTheBoxStatisfyCondition) fileNameHoldingDesiredBoxes = \ thisCEGARFileWrittingManagerInstance.dictMappingFileTypeToFileHandleToUse["boxes"].name tempFH = open(fileNameHoldingDesiredBoxes, "rb") boxesToReturn = [ x[0] for x in readBoxes(tempFH) if ((x[1][1] & labelsForBoxes.LOWESTLEVEL_FALSESOMEWHEREANDEXHAUSTEDLOOKING ) > 0) ] #<---------------- NOTICE THE NOT (i.e., the check for falsity).... tempFH.close() # Note that the second disjunct in the below depends on the specifics # of the splitting strategy and stop-criteria we use, and as such may # need to be adjusted if we employ different functions later. ensures(splitOnlyOnRelaventVariables or \ all([ np.all((x[:,1] - x[:,0]) <= (epsilonForBoxSize * axisScaling)) \ for x in boxesToReturn ]) \ ) ensures(isinstance(boxesToReturn, list)) ensures(all([isProperBox(x) for x in boxesToReturn])) return boxesToReturn