예제 #1
0
def setToFeatureMatrix(key,otherKey,pct=1,shift=0):
    """
    :type otherKey: str
    :returns: a dynamics matrix setting the given keyed value to a percentage of another keyed value plus a constant shift (default is 100% with shift of 0)
    :rtype: L{KeyedMatrix}
    """
    return KeyedMatrix({makeFuture(key): KeyedVector({otherKey: pct,CONSTANT: shift})})
예제 #2
0
def addFeatureMatrix(key,otherKey,pct=1):
    """
    :type otherKey: str
    :returns: a dynamics matrix adding a percentage of another feature value to the given feature value (default percentage is 100%)
    :rtype: L{KeyedMatrix}
    """
    return KeyedMatrix({makeFuture(key): KeyedVector({key: 1,otherKey: pct})})
예제 #3
0
def collapseDynamics(tree,effects,variables={}):
    effects.reverse()
    present = tree.getKeysIn()
    tree.makeFuture(present)
    for stage in effects:
        subtree = None
        for key,dynamics in stage.items():
            if dynamics and makeFuture(key) in tree.getKeysIn():
                assert len(dynamics) == 1
                if subtree is None:
                    subtree = dynamics[0]
                else:
                    subtree += dynamics[0]
        if subtree:
            if tree is None:
                tree = subtree
            else:
                for key in tree.getKeysIn():
                    if not key in subtree.getKeysOut():
                        fun = lambda m: KeyedMatrix(list(m.items())+[(key,KeyedVector({key: 1.}))])
                        subtree = subtree.map(fun)
                tree = tree*subtree
    future = [key for key in tree.getKeysIn() if isFuture(key)]
    if future:
        tree.makePresent(future)
    return tree.prune(variables=variables)
예제 #4
0
def setToConstantMatrix(key,value):
    """
    :type value: float
    :returns: a dynamics matrix setting the given keyed value to the constant value
    :rtype: L{KeyedMatrix}
    """
    return KeyedMatrix({makeFuture(key): KeyedVector({CONSTANT: value})})
예제 #5
0
def incrementMatrix(key,delta):
    """
    :param delta: the constant value to add to the state feature
    :type delta: float
    :returns: a dynamics matrix incrementing the given keyed value by the constant delta
    :rtype: L{KeyedMatrix}
    """
    return KeyedMatrix({makeFuture(key): KeyedVector({key: 1,CONSTANT: delta})})
예제 #6
0
def approachMatrix(key,weight,limit,limitKey=CONSTANT):
    """
    :param weight: the percentage by which you want the feature to approach the limit
    :type weight: float
    :param limit: the value you want the feature to approach
    :type limit: float
    :returns: a dynamics matrix modifying the given keyed value by approaching the given limit by the given weighted percentage of distance
    :rtype: L{KeyedMatrix}
    :param limitKey: the feature whose value to approach (default is CONSTANT)
    :type limitKey: str
    """
    return KeyedMatrix({makeFuture(key): KeyedVector({key: 1-weight,
                                                      limitKey: weight*limit})})
예제 #7
0
 def ceil(self,key,hi):
     """
     Modify this tree to make sure the new computed value never goes higher than the given ceiling
     @warning: may introduce redundant checks
     """
     if self.isLeaf():
         fMatrix = self.children[None]
         assert len(fMatrix) == 1,'Unable to handle dynamics of more than one feature'
         assert makeFuture(key) in fMatrix,'Are you sure you should be ceiling me on a key I don\'t have?'
         del self.children[None]
         tMatrix = setToConstantMatrix(key,hi)
         branch = KeyedPlane(KeyedVector(fMatrix[makeFuture(key)]),hi)
         self.makeBranch(branch,{True: KeyedTree(tMatrix),
                                 False: KeyedTree(fMatrix)})
     elif self.branch:
         for child in self.children.values():
             child.ceil(key,hi)
     else:
         for child in self.children.domain():
             prob = self.children[child]
             del self.children[child]
             self[child.ceil(key,hi)] = prob
     return self
예제 #8
0
def scaleMatrix(key,weight):
    """
    :returns: a dynamics matrix modifying the given keyed value by scaling it by the given weight
    :rtype: L{KeyedMatrix}
    """
    return KeyedMatrix({makeFuture(key): KeyedVector({key: weight})})
예제 #9
0
def dynamicsMatrix(key,vector):
    """
    :returns: a dynamics matrix setting the given key to be equal to the given weighted sum
    :rtype: L{KeyedMatrix}
    """
    return KeyedMatrix({makeFuture(key): KeyedVector(vector)})