Пример #1
0
	def __init__(self, policy, start_state, **kwargs):

		"""
		Constructor for Agent base class

		Parameters:
			policy (Policy): defines the way this agent will behave. Must be a derived class of type Policy
			start_state (list): starting state of this agent (must be valid based on WorldSpace inside Policy)
			kwargs (dict):
				ID (int): unique number to identify this agent in a multi-agent Game
				is_training (bool): flag to track if the current agent is static or training

		Returns:
			Agent: A newly constructed Agent
		"""
		self._policy = policy

		if self._policy.IsValidState(start_state):
			self.curr_state = start_state
		else:
			ERROR(f"start_state [{start_state}] was invalid")
			raise ValueError

		if not isinstance(policy, Policy):
			ERROR(f"policy must be of type {type(Policy)}, got [{type(policy)}]")
			raise TypeError
	
		self._id = kwargs.get("ID", np.random.randint(2**31-1))
		self._is_training = kwargs.get("is_training", True)
Пример #2
0
	def TrainAgent(self, id, episodes = None, steps_per_episode = None):
		if episodes == None:
			episodes = RLGame.DEFAULT_NUM_EPS

		if steps_per_episode == None:
			steps_per_episode = RLGame.DEFAULT_NUM_STEPS_PER_EP
		
		try:
			agent_to_train = self._agents[id]
		
		except KeyError:
			ERROR(f"Invalid key passed to StepAgent, {id} is not a valid agent id")
			return False

		for _, agent in self._agents.items():
			if agent is agent_to_train:
				agent.SetTrainable(True)
			else:
				agent.SetTrainable(False)

		for ii in range(episodes):
			INFO(f"Running episode {ii}")
			steps, _ = self.RunEpisode(steps_per_episode)
			INFO(f"Ran for {steps} steps")

		INFO("Training complete")
Пример #3
0
	def GetTargetEstimate(self, packet):
		S_list, A_list, R_list = packet.Get()
		try:
			next_val = self.GetStateVal(S_list[1], A_list[1])
		except IndexError as e:
			ERROR(f"Bad ExpPacket sent to GetTargetEstimate. \nS_list and A_list must be at least length 2\nEach S,A pair must be valid. Got:\nS: {S_list}\nA: {A_list}")
			raise IndexError
		return R_list[0] + self.gamma*next_val
Пример #4
0
	def StepAgentByID(self, id):
		try:
			agent = self._agents[id]
		except KeyError:
			ERROR(f"Invalid key passed to StepAgent, {id} is not a valid agent id")
			return False

		self._StepAgent(agent)
Пример #5
0
def convert_to_regexp(patterns):
    """Return list of regexp objects
    """
    regexp_list = []
    for pattern in patterns:
        try:
            regexp_list.append(re.compile(pattern, re.I))
        except Exception:
            ERROR("Invalid pattern: %s", pattern)
            emergency_exit()

    return regexp_list
Пример #6
0
def json_load(data, default=None, critical=False):
    """Load JSON from string or file
    """
    try:
        return (json.loads if not hasattr(data, 'read') else json.load)(data)

    except ValueError:
        if critical:
            CRITICAL("Can't parse JSON data")
            EXCEPTION("")
            emergency_exit()

        else:
            ERROR("Can't parse JSON data")
            return default
Пример #7
0
def json_dump(data, fhandler=None, critical=False, default=None):
    """Dump data to JSON and write to string or file
    """
    try:
        if fhandler:
            json.dump(data, fhandler, indent=4)

        else:
            return json.dumps(data, indent=4)

    except ValueError:
        if critical:
            CRITICAL("Can't dump data to JSON")
            EXCEPTION("")
            emergency_exit()

        else:
            ERROR("Can't dump data to JSON")
            return default
Пример #8
0
            except Exception, e:
                ret["status"] = "Could not open the file: %s" % e

        if f is not None:
            ret["status"] = "Reading file..."
            try:
                ret["contents"] = f.read().decode(u'utf-8')
                ret["status"] = "File has been read"
            except Exception, e:
                ret["status"] = "Could not read the file: %s" % e

        if f is not None:
            try:
                f.close()
            except Exception, e:
                ERROR("Could not close the file: %s" % e)

    return ret


def writeCode(code, library, filePath):
    """
    Write the code to a given file.

    @param code: the source code, as a big string
    @param library: qname of the library
    @param filePath: path of the filen to write
    """

    f = None
Пример #9
0
def emergency_exit():
    """Immediately exit from script
    """
    ERROR("Script terminated")
    sys.exit(1)
Пример #10
0
def create_folder(path, erase=False, quiet=False):
    """Create folder at @path.
    - @erase - erase existing folder
    - @quiet - don't ask user about particular actions
    - if @quiet is False, new folder with name @path[i]
      will be created
    - @erase has more priority than @quiet
    """
    # >:( a lot of returns - not good style

    DEBUG("Creating '%s' folder", path)

    try:
        os.makedirs(path)

    except OSError as ex:

        # we can't support other errors, except 'Folder already exists'
        if ex.errno != 17:
            CRITICAL("Can't create folder %s", path)
            EXCEPTION("")
            emergency_exit()

    else:
        DEBUG("Folder '%s' created", path)
        return path

    # Looks like folder already exists
    # lets try to erase it or create new
    # at different path

    ERROR("Can't create '%s' folder", path)
    if erase:
        try:
            erase_dir(path)

        except Exception:
            CRITICAL("Folder '%s' can't be erased")

        else:
            INFO("Folder erased: '{}'".format(path))
            return path

    # Well, erase == False or folder can't be erased
    if not quiet:
        answ = ''

        while not answ:
            answ = raw_input(("Type (E) to erase existing folder, "
                              "type (Q) to exit the script "
                              "or enter new folder name: ")).lower()

            if answ == "e":
                return create_folder(path, erase=True, quiet=quiet)

            elif answ == "q":
                script_exit()

            elif answ:
                return create_folder(answ, erase=False, quiet=quiet)

    else:
        return create_folder(find_unic_path(path), erase=erase, quiet=quiet)