Exemple #1
0
    def __blind_search__(self, max_steps=5000):
        '''
        This method implements the main search loop and must be implemented with the particularities of each search agent
        '''
        # Dummy node for initialization, done = false
        current = Node()

        while not current.done and len(
                self.frontier.nodes) > 0 and self.step <= max_steps:
            current = self.frontier.__get_one_left__()
            expanded = self.__expand_node__(current)

            # Algorithm agnostic implementation on what filter to apply on expanded nodes (such as a depth limit for DLS)
            expanded = self.frontier.expanded_post_processing_function(
                expanded)

            # Algorithm agnostic implementation on how to insert expanded nodes in frontier
            self.frontier.frontier_insertion_function(expanded)

            self.reporting.__append__(
                current.done, len(expanded),
                self.frontier.__get_frontier_max_depth__(),
                len(self.frontier.nodes), current.depth, current.cost)
            self.__printlog__(print_every=1)

            self.step += 1

        if current.done:
            self.final_node = current
Exemple #2
0
    def __expand_node__(self, node: Node) -> list:
        '''

        Parameters
        ----------
        node

        Returns
        -------
        a list of Nodes consequence of expanding node

        Note
        -----
        action_space must be an openai gym Space accepted by environment.step(action) for every action in action_space
        Note that not all openai gym environments implement the __is_applicable__ internal method.
        If so __expand_node__ should be overwritten accordingly :)

        '''

        action_space = node.env.action_space
        assert isinstance(action_space, spaces.Space)

        expanded = list()

        # We need this hack to work with Wrapped environments. Do not pay close attention
        if self.wrapped_env:
            __is_applicable__ = node.env.env.env.__is_applicable__
        else:
            __is_applicable__ = node.env.env.__is_applicable__

        if isinstance(action_space, spaces.Discrete):
            for action in range(action_space.n):
                if __is_applicable__(action):
                    env = deepcopy(node.env)
                    observation, reward, done, info = env.step(action)
                    new_node = Node(env, observation, reward, done, info,
                                    self.__heuristic__(env),
                                    node.cost + reward,
                                    node.action_history + [action],
                                    node.depth + 1)
                    expanded.append(new_node)
        else:
            raise ("Box / Tuple spaces not yet implemented")

        return expanded
Exemple #3
0
# Task 1
# First of all analyze the BFS procedure (UDAI)

#---------------------------
# River Crossing environment
#---------------------------
import gym
from UDAI.frontier.Node import Node
from UDAI.agent.Blind_UCS_Tree_Agent import Blind_UCS_Tree_Agent
from UDAI.agent.Blind_UCS_Graph_Agent import Blind_UCS_Graph_Agent
import pandas as pd
import matplotlib.pyplot as plt

env = gym.make('gym_RiverCrossing:RiverCrossing-v0')
first_observation = env.reset()
first_node = Node(env, first_observation)
env.render()

# Bling Tree Agent Search
blind_ucs_tree_agent = Blind_UCS_Tree_Agent()
blind_ucs_tree_agent.blind_search(first_node)

# If solution is found we can explore the solution :)
blind_ucs_tree_agent.final_node.depth
blind_ucs_tree_agent.final_node.action_history
blind_ucs_tree_agent.final_node.observation
blind_ucs_tree_agent.final_node.env.render()
# etc

# we can also analyze the search process using the reporting class inside the agent
blind_ucs_tree_agent.reporting.log
Exemple #4
0
env.reset()
env.render()
new_observation, reward, done, info = env.step(2)
[new_observation, reward, done, info]

new_observation, reward, done, info = env.step(3)
[new_observation, reward, done, info]


# So once we have analyzed the environment in detail, it is time to start exploring the UDAI library
# We will start analyzing the method to expand nodes
# the node class is a wrapper for the environment to save extra information

env.reset()
from UDAI.frontier.Node import Node
first_node = Node(env)

# We have our initial node to start the search process
# We can also call openai gym commands through the node env variable, as the environment is saved there :)
first_node.env.render()
first_node.env.env.state


# Define a frontier with nodes pending of "expansion"
from UDAI.frontier.Frontier import Frontier
frontier = Frontier()
frontier.nodes.append(first_node)

# For the first node in the frontier, we have to extract it and check if it is a final state.
# if not, we have to check which of the actions are applicable
Exemple #5
0
from UDAI.agent.Blind_DFS_Graph_Agent import Blind_DFS_Graph_Agent
from UDAI.agent.Blind_DLS_Tree_Agent import Blind_DLS_Tree_Agent
from UDAI.agent.Blind_DLS_Graph_Agent import Blind_DLS_Graph_Agent
from UDAI.agent.Blind_IDS_Tree_Agent import Blind_IDS_Tree_Agent
from UDAI.agent.Blind_IDS_Graph_Agent import Blind_IDS_Graph_Agent
from UDAI.agent.Blind_UCS_Tree_Agent import Blind_UCS_Tree_Agent
from UDAI.agent.Blind_UCS_Graph_Agent import Blind_UCS_Graph_Agent

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Environment set up
env = gym.make('gym_RiverCrossing:RiverCrossing-v0')
first_observation = env.reset()
first_node = Node(env, first_observation)

# Agent set up
blind_bfs_tree_agent = Blind_BFS_Tree_Agent()
blind_bfs_graph_agent = Blind_BFS_Graph_Agent()
blind_dfs_tree_agent = Blind_DFS_Tree_Agent()
blind_dfs_graph_agent = Blind_DFS_Graph_Agent()
blind_dls_tree_agent = Blind_DLS_Tree_Agent()
blind_dls_graph_agent = Blind_DLS_Graph_Agent()
blind_ids_tree_agent = Blind_IDS_Tree_Agent()
blind_ids_graph_agent = Blind_IDS_Graph_Agent()
blind_ucs_tree_agent = Blind_UCS_Tree_Agent()
blind_ucs_graph_agent = Blind_UCS_Graph_Agent()

# Search for results
blind_bfs_tree_agent.blind_search(first_node)
Exemple #6
0
 def __to_absolute__(self, x: Node) -> Node:
     x.cost = math.abs(x.cost)