Ejemplo n.º 1
0
 def update(
         self, key, start_end_positions, tree,
         positive_or_negative, depth=10):
     """ Update key trail with new key against
         imprinted past in the tree
     """
     if self.active:
         found_leaf = self.leafs_history[-1].get_leafs((key,))
         if found_leaf:
             leaf = found_leaf[0]
             if positive_or_negative:
                 increment_leaf_key_count(leaf)
             else:
                 decrement_leaf_key_count(leaf)
             self.positions.append(start_end_positions)
             if not leaf.is_last_leaf():
                 self.leafs_history.append(leaf)
             else:
                 pass
                 # add finished trail into results
                 # for accounting
         else:
             # this trail did progress in past so
             # ending it now
             self.active = False
Ejemplo n.º 2
0
    def delete_key_within_range(
            self, key, start_end_positions, depth, allowed_range, sample_size):
        """ To delete keys if they are known unknown.
            e.g.: N4 with range limit 2 would delete key
            key if input key was (N2, N3, N4, N5, N6)

            which key to delete from allowed range if there are multiple keys
            available ?

            Find all keys which are available in range and then choose
            one at random to delete.
        """
        found = False
        if self.active:
            keys = self._get_keys_in_range(key, allowed_range, sample_size)
            if len(keys) > 1:
                new_key = random.choice(keys)
                keys = [new_key]
            found_leaf = self.leafs_history[-1].get_leafs(keys)
            if found_leaf:
                leaf = found_leaf[0]
                decrement_leaf_key_count(leaf)
                self.positions.append(start_end_positions)
                if not leaf.is_last_leaf():
                    self.leafs_history.append(leaf)
                else:
                    self.active = False
                found = True
            else:
                # this trail did progress in past so
                # ending it now
                self.active = False
        return found
 def delete_key(self, key, start_end_positions, depth=10):
     """ Delete key from trail if it appeared previously """
     found = False
     if self.active:
         found_leaf = self.leafs_history[-1].get_leafs((key, ))
         if found_leaf:
             leaf = found_leaf[0]
             decrement_leaf_key_count(leaf)
             self.positions.append(start_end_positions)
             if not leaf.is_last_leaf():
                 self.leafs_history.append(leaf)
             else:
                 self.active = False
             found = True
         else:
             # this trail did progress in past so
             # ending it now
             self.active = False
     return found
Ejemplo n.º 4
0
 def delete_euclidean_closest_key(
         self, key, start_end_positions,
         not_found_keys, found_keys,
         common_leaf_attributes, samples):
     found = False
     found_leaf = None
     last_leaf = self.leafs_history[-1]
     if self.active:
         # print "F last_leaf",  last_leaf
         # print "F last_leaf all", last_leaf.get_next_leafs()
         found_leaf = last_leaf.get_leafs((key,))
         if not found_leaf:
             if isinstance(key, basestring) and key.startswith("N"):
                 # print "not_found_keys", not_found_keys
                 leafs = get_next_not_found_leafs_extended(
                     last_leaf, not_found_keys)
                 # print "A leafs", leafs
                 if leafs:
                     found_leaf = (get_euclidean_closest_element(
                         # length of unknown
                         # probability of happening
                         (key, int(key[1:]), 1.0),
                         # (key, int(key[1:])),
                         leafs)[0][0],)
                 # print "A key", key, "found_leaf", found_leaf
             else:
                 leafs = get_next_found_leafs_extended(
                     key, last_leaf, found_keys,
                     common_leaf_attributes, samples)
                 # print "B leafs", leafs
                 if leafs:
                     attribute = get_leaf_atrribute_number(
                         key, common_leaf_attributes)
                     # print "attribute", attribute
                     # attribute number
                     # probability of happening
                     # correlation number
                     found_leaf = (get_euclidean_closest_element(
                         (key, attribute, 1.0, 1.0),
                         # (key, attribute),
                         leafs)[0][0],)
                 # print "B key", key, "found_leaf", found_leaf
         if found_leaf:
             leaf = found_leaf[0]
             # print "C leaf", leaf
             decrement_leaf_key_count(leaf)
             self.positions.append(start_end_positions)
             """
             if not leaf.is_last_leaf():
                 self.leafs_history.append(leaf)
             else:
                 self.active = False
             """
             self.leafs_history.append(leaf)
             if leaf.is_last_leaf():
                 # print "Last leaf :("
                 self.active = False
             found = True
         else:
             # print "End of trial", self.positions
             # this trail did progress in past so
             # ending it now
             self.active = False
     return found
 def delete_euclidean_closest_key(self, key, start_end_positions,
                                  not_found_keys, found_keys,
                                  common_leaf_attributes, samples,
                                  volatilities):
     found = False
     found_leaf_set = None
     last_leaf = self.leafs_history[-1]
     self.volatilities.append(volatilities)
     if self.active:
         # print "F last_leaf",  last_leaf
         # print "F last_leaf all", last_leaf.get_next_leafs()
         # found_leaf = last_leaf.get_leafs((key,))
         found_leaf_set = None
         # print "found_leaf ", found_leaf
         # if not found_leaf:
         if isinstance(key, basestring) and key.startswith("N"):
             # print "not_found_keys", not_found_keys
             leafs = get_next_not_found_leafs_extended(
                 last_leaf, not_found_keys)
             # print "A leafs", leafs
             if leafs:
                 current = [key, int(key[1:]), 1.0]
                 current.extend(volatilities)
                 # print "current", current
                 found_leaf_set = get_euclidean_closest_element(
                     # x0: key is not used
                     # x1: length of unknown
                     # x2: probability of happening
                     # x3: current volatilities
                     current,
                     leafs)
             # print "A key", key, "found_leaf", found_leaf_set
         else:
             leafs = get_next_found_leafs_extended(key, last_leaf,
                                                   found_keys,
                                                   common_leaf_attributes,
                                                   samples)
             # print "B leafs", leafs, key, last_leaf
             if leafs:
                 attribute = get_leaf_atrribute_number(
                     key, common_leaf_attributes)
                 # print "attribute", attribute
                 # x0: key is not used
                 # x1: sample attribute number (up, down, stale ...)
                 # x2: probability of happening
                 # x3: correlation number
                 # x4: current volatilities
                 current = [key, attribute, 1.0, 1.0]
                 current.extend(volatilities)
                 found_leaf_set = get_euclidean_closest_element(
                     # TODO: correlation number should be real one when
                     #       in matched rather than 1.0
                     current,
                     leafs)
                 # print "B key", key, "found_leaf", found_leaf_set
         if found_leaf_set:
             leaf = found_leaf_set[0][0]
             volatility_delete_index = found_leaf_set[2]
             # print "C leaf", leaf, volatility_delete_index
             # import sys; sys.exit();
             decrement_leaf_key_count(leaf)
             delete_leaf_volatilities(leaf, volatility_delete_index)
             self.positions.append(start_end_positions)
             """
             if not leaf.is_last_leaf():
                 self.leafs_history.append(leaf)
             else:
                 self.active = False
             """
             self.leafs_history.append(leaf)
             if leaf.is_last_leaf():
                 # print "Last leaf :("
                 self.active = False
             found = True
         else:
             # print "End of trial", self.positions
             # this trail did progress in past so
             # ending it now
             self.active = False
     return found