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})})
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})})
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)
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})})
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})})
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})})
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
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})})
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)})