def _update_observation(self): return_obs = Observation() return_obs.doubleArray = np.zeros(MDPState.nfeatures) for effector, mapping in MDPState.description.iteritems(): pos = NaoWorldModel().get_effector_pos(effector) for key, axis in mapping.iteritems(): return_obs.doubleArray[axis] = pos[MDPState.key_to_index(key)] return return_obs
def init(self, taskspec): super(PenaltyKickAgent, self).init(taskspec) ts = TaskSpecParser(taskspec) if ts.valid: extra = ts.get_extra() v = ['FEATUREREP', 'SUPPORTLEG', 'STATEDESCR', 'ACTIONDESCR', 'COPYRIGHT'] pos = [] for i, id_ in enumerate(list(v)): try: pos.append(extra.index(id_)) except: v.remove(id_) sorted_v = sorted(zip(pos, v)) v = [s[1] for s in sorted_v] for i, id_ in enumerate(v): val = ts.get_value(i, extra, v) if id_ == 'FEATUREREP': self._feature_rep = val if id_ == 'SUPPORTLEG': if val == 'left': self._ankle_roll = "RAnkleRoll" self._hip_roll = "RHipRoll" min_hip_roll, max_hip_roll = NaoWorldModel().get_robot_info(self._hip_roll) leg_length = NaoWorldModel().get_robot_info("TibiaLength") + NaoWorldModel().get_robot_info("ThighLength") MDPState.dtype = MDPState.DTYPE_INT if self._feature_rep == 'rl': try: max_location = NaoWorldModel().get_object("ball").resolution[0] except AttributeError: max_location = 0 MDPState.set_minmax_features([0, max_location], [math.floor(leg_length * math.sin(min_hip_roll)), math.ceil(leg_length * math.sin(max_hip_roll))]) MDPState.set_states_per_dim([int((MDPState.max_features[0] - MDPState.min_features[0]) / 2), int(math.ceil((MDPState.max_features[1] - MDPState.min_features[1]) / 4))]) # noinspection PyShadowingNames def is_valid(self): real_state = True if MDPState.min_features is not None: for (feature, min_feature, max_feature) in zip(self, MDPState.min_features, MDPState.max_features): if feature < (min_feature - eps) or feature > (max_feature + eps): real_state = False self._logger.debug("\t\t\t\tNext state is not valid (feature %d out of range)", feature) break return real_state MDPState.is_valid = is_valid else: MDPState.set_minmax_features([math.floor(leg_length * math.sin(min_hip_roll)), math.ceil(leg_length * math.sin(max_hip_roll))]) MDPState.set_nfeatures( int(math.ceil((MDPState.max_features - MDPState.min_features + 1) / self._bin_width))) # noinspection PyShadowingNames def is_valid(self): num_ones = len(np.where(self.get()[0:len(self)] == 1)[0]) if num_ones > 1 or num_ones < 1 or not all(i == 0 or i == 1 for i in self.get()): return False return True # noinspection PyShadowingNames def encode(self): return np.where(self.get()[0:len(self)] == 1)[0] def decode(cls, state_repr): decoded = [0] * cls.nfeatures bin_num = 0 if isinstance(state_repr[0], int): bin_num = state_repr[0] elif isinstance(state_repr[0], float): bin_num = int(math.floor((state_repr[0] - cls.min_features) / self._bin_width)) if 0 <= bin_num <= cls.nfeatures - 1: decoded[bin_num] = 1 elif bin_num < 0: decoded[0] = 1 else: decoded[cls.nfeatures - 1] = 1 return cls(decoded) MDPState.is_valid = is_valid MDPState.encode = encode MDPState.decode = decode