Example #1
0
def constructScaffold(trace,
                      setsOfPNodes,
                      useDeltaKernels=False,
                      deltaKernelArgs=None,
                      hardBorder=None,
                      updateValues=False):
    if hardBorder is None:
        hardBorder = []
    assert len(hardBorder) <= 1

    cDRG, cAbsorbing, cAAA = OrderedSet(), OrderedSet(), OrderedSet()
    indexAssignments = OrderedDict()
    assert isinstance(setsOfPNodes, list)
    for i in range(len(setsOfPNodes)):
        assert isinstance(setsOfPNodes[i], OrderedFrozenSet)
        extendCandidateScaffold(trace, setsOfPNodes[i], cDRG, cAbsorbing, cAAA,
                                indexAssignments, i, hardBorder)

    brush = findBrush(trace, cDRG)
    drg, absorbing, aaa = removeBrush(cDRG, cAbsorbing, cAAA, brush)
    border = findBorder(trace, drg, absorbing, aaa)
    regenCounts = computeRegenCounts(trace, drg, absorbing, aaa, border, brush,
                                     hardBorder)
    for node in hardBorder:
        assert node in border
    lkernels = loadKernels(trace, drg, aaa, useDeltaKernels, deltaKernelArgs)
    borderSequence = assignBorderSequnce(border, indexAssignments,
                                         len(setsOfPNodes))
    scaffold = Scaffold(setsOfPNodes, regenCounts, absorbing, aaa,
                        borderSequence, lkernels, brush, drg)

    if updateValues:
        updateValuesAtScaffold(trace, scaffold, OrderedSet())

    return scaffold
Example #2
0
def findBrush(trace, cDRG):
    disableCounts = OrderedDict()
    disabledRequests = OrderedSet()
    brush = OrderedSet()
    for node in cDRG:
        if isRequestNode(node):
            disableRequests(trace, node, disableCounts, disabledRequests,
                            brush)
    return brush
Example #3
0
 def makeConsistent(self):
     weight = 0
     for node, val in self.unpropagatedObservations.iteritems():
         appNode = self.getConstrainableNode(node)
         #      print "PROPAGATE", node, appNode
         scaffold = constructScaffold(self, [OrderedSet([appNode])])
         rhoWeight, _ = detachAndExtract(self, scaffold)
         scaffold.lkernels[appNode] = DeterministicLKernel(
             self.pspAt(appNode), val)
         xiWeight = regenAndAttach(self, scaffold, False, OmegaDB(),
                                   OrderedDict())
         # If xiWeight is -inf, we are in an impossible state, but that might be ok.
         # Finish constraining, to avoid downstream invariant violations.
         node.observe(val)
         constrain(self, appNode, node.observedValue)
         weight += xiWeight
         weight -= rhoWeight
     self.unpropagatedObservations.clear()
     if not math.isnan(weight):
         # Note: +inf weight is possible at spikes in density against
         # Lebesgue measure.
         return weight
     else:
         # How could we get a NaN here?
         # If one observation made the state inconsistent, the rhoWeight
         # of another might conceivably be infinite, possibly leading to
         # a nan weight.  I want to normalize these to indicating that
         # the resulting state is impossible.
         return float("-inf")
Example #4
0
 def randomChoicesInExtent(self, nodes, scope, block):
     # The scope and block, if present, limit the computed dynamic
     # extent to everything that is not explicitly excluded from them.
     pnodes = OrderedSet()
     for node in nodes:
         self.addRandomChoicesInExtent(node, scope, block, pnodes)
     return pnodes
Example #5
0
 def getAllNodesInScope(self, scope):
     blocks = [
         self.getNodesInBlock(scope, block)
         for block in self.getScope(scope).keys()
     ]
     if len(blocks) == 0:  # Guido, WTF?
         return OrderedSet()
     else:
         return OrderedSet.union(*blocks)
Example #6
0
    def __init__(self):
        self.tableCounts = OrderedDict()
        self.nextTable = 1
        self.freeTables = OrderedSet()
        self.numTables = 0
        self.numCustomers = 0

        # application node ---> most recently unincorporated table
        self.cachedTables = WeakKeyDictionary()
Example #7
0
 def registerRandomChoiceInScope(self, scope, block, node, unboxed=False):
     if not unboxed:
         (scope,
          block) = self._normalizeEvaluatedScopeAndBlock(scope, block)
     if scope not in self.scopes: self.scopes[scope] = SamplableMap()
     if block not in self.scopes[scope]:
         self.scopes[scope][block] = OrderedSet()
     assert node not in self.scopes[scope][block]
     self.scopes[scope][block].add(node)
     assert scope != "default" or len(self.scopes[scope][block]) == 1
Example #8
0
 def __init__(self, address):
     assert address is not None
     self.address = address
     self.value = None
     self.children = OrderedSet()
     self.isObservation = False
     self.observedValue = None
     self.madeSPRecord = None
     self.aaaMadeSPAux = None
     self.numRequests = 0
     self.esrParents = []
     self.isFrozen = False
Example #9
0
 def getNodesInBlock(self, scope, block, do_copy=True):
     #import pdb; pdb.set_trace()
     #scope, block = self._normalizeEvaluatedScopeAndBlock(scope, block)
     nodes = self.scopes[scope][block]
     # Defensively copying the set of nodes, because
     # unregisterRandomChoice will mutate them.
     if scope == "default":
         if do_copy:
             return OrderedSet(list(nodes))
         else:
             return nodes
     else:
         return self.randomChoicesInExtent(nodes, scope, block)
Example #10
0
    def __init__(self, seed):

        self.globalEnv = VentureEnvironment()
        for name, val in builtInValues().iteritems():
            self.bindPrimitiveName(name, val)
        for name, sp in builtInSPsIter():
            self.bindPrimitiveSP(name, sp)
        self.sealEnvironment()  # New frame so users can shadow globals

        self.rcs = OrderedSet()
        self.ccs = OrderedSet()
        self.aes = OrderedSet()
        self.unpropagatedObservations = OrderedDict()  # {node:val}
        self.families = OrderedDict()
        self.scopes = OrderedDict()  # :: {scope-name:smap{block-id:set(node)}}

        self.profiling_enabled = False
        self.stats = []

        assert seed is not None
        rng = random.Random(seed)
        self.np_rng = npr.RandomState(rng.randint(1, 2**31 - 1))
        self.py_rng = random.Random(rng.randint(1, 2**31 - 1))
Example #11
0
 def getSetsOfPNodes(self, trace):
     if self.block == "one":
         self.true_block = trace.sampleBlock(self.scope)
         setsOfPNodes = [trace.getNodesInBlock(self.scope, self.true_block)]
     elif self.block == "all":
         setsOfPNodes = [trace.getAllNodesInScope(self.scope)]
     elif self.block == "none":
         setsOfPNodes = [OrderedSet()]
     elif self.block == "ordered":
         setsOfPNodes = trace.getOrderedSetsInScope(self.scope)
     elif self.block == "ordered_range":
         assert self.interval
         setsOfPNodes = trace.getOrderedSetsInScope(self.scope,
                                                    self.interval)
     else:
         setsOfPNodes = [trace.getNodesInBlock(self.scope, self.block)]
     return setsOfPNodes
Example #12
0
    def checkInvariants(self):
        # print "Begin invariant check"
        assert len(self.unpropagatedObservations) == 0, \
          "Don't checkInvariants with unpropagated observations"
        rcs = copy.copy(self.rcs)
        ccs = copy.copy(self.ccs)
        aes = copy.copy(self.aes)
        scopes = OrderedDict()
        for (scope_name, scope) in self.scopes.iteritems():
            new_scope = SamplableMap()
            for (block_name, block) in scope.iteritems():
                new_scope[block_name] = copy.copy(block)
            scopes[scope_name] = new_scope

        scaffold = BlockScaffoldIndexer("default", "all").sampleIndex(self)
        rhoWeight, rhoDB = detachAndExtract(self, scaffold)

        assert len(
            self.rcs) == 0, "Global detach left random choices registered"
        # TODO What if an observed random choice had registered an
        # AEKernel?  Will that be left here?
        assert len(self.aes) == 0, "Global detach left AEKernels registered"
        assert len(
            self.scopes
        ) == 1, "Global detach left random choices in non-default scope %s" % self.scopes
        assert len(
            self.scopes['default']
        ) == 0, "Global detach left random choices in default scope %s" % self.scopes[
            'default']

        xiWeight = regenAndAttach(self, scaffold, True, rhoDB, OrderedDict())

        # XXX Apparently detach/regen sometimes has the effect of changing
        # the order of rcs.
        assert set(rcs) == set(
            self.rcs
        ), "Global detach/restore changed the registered random choices from %s to %s" % (
            rcs, self.rcs)
        assert ccs == self.ccs, "Global detach/restore changed the registered constrained choices from %s to %s" % (
            ccs, self.ccs)
        assert aes == self.aes, "Global detach/restore changed the registered AEKernels from %s to %s" % (
            aes, self.aes)

        for scope_name in OrderedSet(scopes.keys()).union(self.scopes.keys()):
            if scope_name in scopes and scope_name not in self.scopes:
                assert False, "Global detach/restore destroyed scope %s with blocks %s" % (
                    scope_name, scopes[scope_name])
            if scope_name not in scopes and scope_name in self.scopes:
                assert False, "Global detach/restore created scope %s with blocks %s" % (
                    scope_name, self.scopes[scope_name])
            scope = scopes[scope_name]
            new_scope = self.scopes[scope_name]
            for block_name in OrderedSet(scope.keys()).union(new_scope.keys()):
                if block_name in scope and block_name not in new_scope:
                    assert False, "Global detach/restore destroyed block %s, %s with nodes %s" % (
                        scope_name, block_name, scope[block_name])
                if block_name not in scope and block_name in new_scope:
                    assert False, "Global detach/restore created block %s, %s with nodes %s" % (
                        scope_name, block_name, new_scope[block_name])
                assert scope[block_name] == new_scope[
                    block_name], "Global detach/restore changed the addresses in block %s, %s from %s to %s" % (
                        scope_name, block_name, scope[block_name],
                        new_scope[block_name])

        assert_allclose(
            rhoWeight,
            xiWeight,
            err_msg="Global restore gave different weight from detach")
Example #13
0
 def enumerateValues(self, args):
     return list(OrderedSet(args.operandValues()))