def replay(self, batch_size, postask=None): #print("BEGIN: REPLAYING........................................................") p_size = min(self.psize, batch_size) n_size = min(self.nsize, batch_size) nt_size = min(self.ntsize, batch_size) minibatch = [] if (p_size > 0): minibatch += sample(self.positive_memory, p_size) if (n_size > 0): minibatch += sample(self.negative_memory, n_size) if (nt_size > 0): minibatch += sample(self.neutral_memory, nt_size) random.shuffle(minibatch) batch_size = len(minibatch) states = np.zeros((batch_size, self.state_size[0], self.state_size[1], self.skip_frames)) next_states = np.zeros((batch_size, self.state_size[0], self.state_size[1], self.skip_frames)) actions = [] rewards = [] dones = [] targets = np.zeros((batch_size, )) idx = 0 for idx, val in enumerate(minibatch): states[idx] = val[0] next_states[idx] = val[3] actions.append(val[1]) rewards.append(val[2]) dones.append(val[4]) actions_mask = np.ones((batch_size, self.action_size)) next_Q_values = self.back_model.predict([next_states, actions_mask]) for i in range(batch_size): if dones[i]: targets[i] = -1 #targets[i] = rewards[i] else: targets[i] = rewards[i] + self.gamma * np.amax( next_Q_values[i]) action_one_hot = get_one_hot(actions, self.action_size) target_one_hot = action_one_hot * targets[:, None] h = self.back_model.fit([states, action_one_hot], target_one_hot, epochs=1, batch_size=batch_size, verbose=0) self.replay_is_running = False #print("END: REPLAYING........................................................") self.last_loss = h.history['loss'][0] #print("CURRENT LOSS: %f" % (self.last_loss)) if postask: postask(self, self.last_loss) return self.last_loss
def return_server(self): actions = [] for step in self.queue: for action in step: actions.append(action.name) return actions
def estimate_empowerment(self): """ Estimate the empowerment of the current state. Returns: empowerment (int) """ states = [] for i in range(self.num_sequences): actions = [] for action in self.random_action_sequence(): actions.append(action) action.apply() state = self.sense() self.add_new_reading(state, states) actions.reverse() for action in actions: action.rollback() if self.sensing_threshold is None: average_embedded = 0 # does not matter else: if type(states[0]) == int: average_embedded = np.mean(states) else: average_embedded = np.mean([a.size for a in states]) return np.log(len(states)), average_embedded
def available_actions(self): """ Iterate over all loaded modules, and retrieve actions (ie functions with the @action decorator). """ actions = [] path = sys.modules["robots.actions"].__path__ for loader, module_name, is_pkg in pkgutil.walk_packages(path): m = sys.modules["robots.actions." + module_name] for member in [getattr(m, fn) for fn in dir(m)]: if hasattr(member, "_action"): actions.append(RobotAction(member, m)) return actions
def emit_default(self): ''' emit the action taken when we did not hit a valid hash table entry ''' actions = [] for action in self.default_actions: if action.is_return(): s = "return %s" % action.get_str_value() actions.append(s) if action.is_field_binding(): val = action.get_str_value() fb = action.field_name.lower() s = "%s_set_%s(%s,%s)" % (self.strings_dict['op_accessor'], fb, self.strings_dict['obj_str'], val) actions.append(s) return actions
def main(): #print '' log.setDebug(util.argv(_cmds['Debug'])) actions.supress(util.argv(_cmds['Supress'])) args = sys.argv if len(args) == 1: log.comment( "No arguments specified, listing your Gists. Try '%s help' if you need help." % util.fileName) print('') del args[0] # Delete the filename cmd = None log.debug("Arguments " + str(args)) #-------------------------------------------- # If args[0] is a command. We remove it from the list. args now contains only the command arguments # else we keep as is and try to evaluate the command #-------------------------------------------- if _hasCmd(args): cmd = args[0] del args[ 0] # Delete the command. Arguments remaining are the options for each command else: cmd = _deriveCmd(args) log.debug("Adjusted cmd: " + str(cmd)) log.debug("Adjusted arguments " + str(args)) #-------------------------------------------- # Handle commands #-------------------------------------------- if cmd == None: _printNoMatch() elif cmd in (_cmds['Help']): actions.help() elif cmd in (_cmds['List']): actions.list() elif cmd in (_cmds['Token']): actions.updateCredentials() elif cmd in (_cmds['Open']): if len(args) == 1: actions.open(args[0]) else: _printNoMatch() elif cmd in (_cmds['View']): if len(args) == 1: actions.view(args[0]) elif len(args) == 2: actions.view(args[0], fileName=args[1]) elif cmd in (_cmds['Download']): if len(args) == 2: actions.get(args[0], path=args[1]) elif len(args) == 3: actions.get(args[0], fileName=args[1], path=args[2]) elif cmd in (_cmds['New']): # Each option will prompt for public/pvt and description. In silent mode, assumes private and no description. if len(args) == 0: actions.new() elif len(args) == 1: # create File # create Content if util.isFileOrDir(args[0]) == True: actions.new(filename=args[0]) else: actions.new(content=args[0]) elif len(args) == 2: # create Boolean and File # create Boolean and Content # create Description and File # create Description and Content if util.parseBool(args[0]) != None: if util.isFileOrDir(args[1]) == True: actions.new(public=util.parseBool(args[0]), filename=args[1]) else: actions.new(public=util.parseBool(args[0]), content=args[1]) else: if util.isFileOrDir(args[1]) == True: actions.new(description=args[0], filename=args[1]) else: actions.new(description=args[0], content=args[1]) elif len(args) == 3 and util.parseBool(args[0]) != None: # create Boolean, Description and File # create Boolean, Description and Content if util.isFileOrDir(args[2]) == True: actions.new(public=util.parseBool(args[0]), description=args[1], filename=args[2]) else: actions.new(public=util.parseBool(args[0]), description=args[1], content=args[2]) else: _printNoMatch() elif cmd in (_cmds['Append']): # Each option will prompt for public/pvt and description. if len(args) == 0: _printNoMatch() elif len(args) == 2: # append: id File # append: id Content if util.isFileOrDir(args[1]) == True: actions.append(args[0], filename=args[1]) else: actions.append(args[0], content=args[1]) elif len(args) == 3: # append: id Description File # append: id Description Content if util.isFileOrDir(args[2]) == True: actions.append(args[0], description=args[1], filename=args[2]) else: actions.append(args[0], description=args[1], content=args[2]) else: actions.append(args[0]) elif cmd in (_cmds['Update']): # Each option will prompt for public/pvt and description. if len(args) == 0: _printNoMatch() elif len(args) == 2: # append: id File # append: id Content if util.isFileOrDir(args[1]) == True: actions.update(args[0], filename=args[1]) else: actions.update(args[0], content=args[1]) elif len(args) == 3: # append: id Description File # append: id Description Content if util.isFileOrDir(args[2]) == True: actions.update(args[0], description=args[1], filename=args[2]) else: actions.update(args[0], description=args[1], content=args[2]) else: actions.update(args[0]) elif cmd in (_cmds['Delete']): actions.delete(args[0]) elif cmd in (_cmds['Backup']): _printNoImpl() actions.backup() elif cmd in (_cmds['Search']): _printNoImpl() actions.search() else: _printNoMatch() log.debug("Done.") print('')
def main ( ): #print '' log.setDebug( util.argv( _cmds['Debug'] ) ) actions.supress( util.argv( _cmds['Supress']) ) args = sys.argv if len(args) == 1: log.comment ("No arguments specified, listing your Gists. Try '%s help' if you need help." % util.fileName) print '' del args[0] # Delete the filename cmd = None log.debug ("Arguments " + str( args )) #-------------------------------------------- # If args[0] is a command. We remove it from the list. args now contains only the command arguments # else we keep as is and try to evaluate the command #-------------------------------------------- if _hasCmd( args ): cmd = args[0] del args[0] # Delete the command. Arguments remaining are the options for each command else: cmd = _deriveCmd( args ) log.debug ("Adjusted cmd: " + str(cmd)) log.debug ("Adjusted arguments " + str( args )) #-------------------------------------------- # Handle commands #-------------------------------------------- if cmd == None: _printNoMatch() elif cmd in (_cmds['Help']): actions.help() elif cmd in (_cmds['List']): actions.list() elif cmd in (_cmds['Token']): actions.updateCredentials() elif cmd in (_cmds['View']): if len(args) == 1: actions.view( args[0] ) elif len(args) == 2: actions.view( args[0], fileName=args[1]) elif cmd in (_cmds['Download']): if len(args) == 2: actions.get( args[0], path=args[1] ) elif len(args) == 3: actions.get( args[0], fileName=args[1], path=args[2] ) elif cmd in (_cmds['New']): # Each option will prompt for public/pvt and description. In silent mode, assumes private and no description. if len(args) == 0: actions.new() elif len(args) == 1: # create File # create Content if util.isFileOrDir(args[0]) == True: actions.new( filename = args[0] ) else: actions.new( content = args[0] ) elif len(args) == 2: # create Boolean and File # create Boolean and Content # create Description and File # create Description and Content if util.parseBool( args[0] ) != None: if util.isFileOrDir(args[1]) == True: actions.new( public=util.parseBool( args[0] ), filename=args[1] ) else: actions.new( public=util.parseBool( args[0] ), content=args[1] ) else: if util.isFileOrDir(args[1]) == True: actions.new( description=args[0], filename=args[1] ) else: actions.new( description=args[0], content=args[1] ) elif len(args) == 3 and util.parseBool( args[0] ) != None: # create Boolean, Description and File # create Boolean, Description and Content if util.isFileOrDir(args[2]) == True: actions.new( public=util.parseBool( args[0] ), description=args[1], filename=args[2] ) else: actions.new( public=util.parseBool( args[0] ), description=args[1], content=args[2] ) else: _printNoMatch() elif cmd in (_cmds['Append']): # Each option will prompt for public/pvt and description. if len(args) == 0: _printNoMatch() elif len(args) == 2: # append: id File # append: id Content if util.isFileOrDir(args[1]) == True: actions.append( args[0], filename=args[1] ) else: actions.append( args[0], content=args[1] ) elif len(args) == 3: # append: id Description File # append: id Description Content if util.isFileOrDir(args[2]) == True: actions.append( args[0], description=args[1], filename=args[2] ) else: actions.append( args[0], description=args[1], content=args[2] ) else: actions.append( args[0] ) elif cmd in (_cmds['Update']): # Each option will prompt for public/pvt and description. if len(args) == 0: _printNoMatch() elif len(args) == 2: # append: id File # append: id Content if util.isFileOrDir(args[1]) == True: actions.update( args[0], filename=args[1] ) else: actions.update( args[0], content=args[1] ) elif len(args) == 3: # append: id Description File # append: id Description Content if util.isFileOrDir(args[2]) == True: actions.update( args[0], description=args[1], filename=args[2] ) else: actions.update( args[0], description=args[1], content=args[2] ) else: actions.update( args[0] ) elif cmd in (_cmds['Delete']): actions.delete( args[0] ) elif cmd in (_cmds['Backup']): _printNoImpl() actions.backup( ) elif cmd in (_cmds['Search']): _printNoImpl() actions.search( ) else: _printNoMatch() log.debug ("Done.") print ''