Example #1
0
 def track_increment(self, incr=1):
     """
     Returns: True if we enter the next period
     """
     U.assert_type(incr, int)
     self.value += incr
     return self._update_endpoint()
Example #2
0
 def track_absolute(self, value):
     """
     Returns: True if we enter the next period
     """
     U.assert_type(value, int)
     self.value = value
     return self._update_endpoint()
Example #3
0
 def __init__(self, tensorplex, min_update_interval):
     U.assert_type(tensorplex, TensorplexClient)
     self.tensorplex = tensorplex
     self.min_update_interval = min_update_interval
     self.history = U.AverageDictionary()
     self.lock = Lock()
     self.tracker = U.TimedTracker(self.min_update_interval)
     self.init_time = time.time()
Example #4
0
 def __init__(self, period, init_value=0, init_endpoint=0):
     """
     first: if True, triggers at the first time
     """
     U.assert_type(period, int)
     assert period > 0
     U.assert_type(init_value, int)
     self.period = period
     self.value = init_value
     self._endpoint = init_endpoint
Example #5
0
 def add(self, hash_dict, nonhash_dict):
     """
     Args:
         hash_dict: {obs_hash: [ .. can be nested .. ]}
         nonhash_dict: {reward: -1.2, done: True, ...}
     """
     U.assert_type(hash_dict, dict)
     U.assert_type(nonhash_dict, dict)
     exp = {}
     for key, values in hash_dict.items():
         assert not key.endswith('_hash'), 'do not manually append `_hash`'
         exp[key + '_hash'] = self._hash_nested(values)
     exp.update(nonhash_dict)
     self.exp_list.append(exp)
Example #6
0
def extend_config(config, default_config):
  """
    default_config must specify all the expected keys. Use the following special
    values for required placeholders:

    * _req_: require a single value (not a list or dict)
    * _req_DICT_: require a dict
    * _req_LIST_: require a list

    Returns:
        AttributeDict
        `config` filled by default values if certain keys are unspecified

    Raises:
        ConfigError if required placeholders are not satisfied
    """
  U.assert_type(config, dict)
  U.assert_type(default_config, dict)
  return Config(_fill_default_config(config, default_config, []))
Example #7
0
 def __init__(self,
              tensorplex,
              period,
              is_average=True,
              keep_full_history=False):
     """
     Args:
         tensorplex: TensorplexClient object
         period: when you call `update()`, it will only send to Tensorplex
             at the specified period.
         is_average: if True, send the averaged value over the last `period`.
         keep_full_history: if False, only keep the last `period` of history.
     """
     if tensorplex is not None:  # None to turn off tensorplex
         U.assert_type(tensorplex, TensorplexClient)
     U.assert_type(period, int)
     assert period > 0
     self._tplex = tensorplex
     self._period = period
     self._is_average = is_average
     self._keep_full_history = keep_full_history
     self._tracker = PeriodicTracker(period)
     self._history = {}
     self._max_deque_size = None if keep_full_history else period
Example #8
0
    def _sample_request_handler(self, req):
        """
    Handle requests to the learner
    https://stackoverflow.com/questions/29082268/python-time-sleep-vs-event-wait
    Since we don't have external notify, we'd better just use sleep
    """
        # batch_size = U.deserialize(req)
        batch_size = req
        U.assert_type(batch_size, int)
        while not self.start_sample_condition():
            time.sleep(0.01)
        self.cumulative_sampled_count += batch_size
        self.cumulative_request_count += 1

        with self.sample_time.time():
            while True:
                try:
                    sample = self.sample(batch_size)
                    break
                except ReplayUnderFlowException:
                    time.sleep(1e-3)

        with self.serialize_time.time():
            return sample
Example #9
0
 def extend(self, default_config):
   U.assert_type(default_config, dict)
   return _fill_default_config(self, default_config, [])