示例#1
0
    def graph3(self):
        graph = Graph({
            'a': {
                'b': 10,
                'c': 100,
                'd': 1
            },
            'b': {
                'c': 10
            },
            'd': {
                'b': 1,
                'e': 1
            },
            'e': {
                'f': 1
            },
        })

        graph.add_node('f', {'c': 1})
        graph['f'] = {'c': 1}

        graph.add_edge('f', 'c', 1)
        graph.add_edge('g', 'b', 1)

        nodes = list(graph)
        nodes.sort()
        self.assertEqual(nodes, ['a', 'b', 'd', 'e', 'f', 'g'])

        incoming = graph.get_incoming('c')
        incoming_nodes = list(incoming.keys())
        incoming_nodes.sort()
        self.assertEqual(incoming_nodes, ['a', 'b', 'f'])

        return graph
def Init_Graph(Node_list, Edge_list):
  graph_orig = Graph()
  
  # Build Graph with specified Nodes & Edges
  for nodes in Node_list:
    graph_orig.add_node(nodes)

  for edges in Edge_list:
    n1, n2 = edges
    graph_orig.add_edge(n1, n2, {'cost':1})
    graph_orig.add_edge(n2, n1, {'cost':1})

  return graph_orig
示例#3
0
文件: prm.py 项目: rgaensler/gcode
def create_prm(
        config: List[BaseJoint],
        collider: MatlabCollisionChecker,
        constr: Constraints,
        dj: float,
        dmax: float,
        max_neighbors: Optional[int] = 30,
        max_time_s: Optional[int] = 60) -> Tuple[Graph, Tuple[List[float]]]:
    """
    Create a probabilistic road map
    :param config: List of joints containing coordinate transformations.
    :param collider: Matlab collision checking interface
    :param constr: Namedtuple containing all relevant trajectory constraints
    :param dj:
    :param dmax: Maximum allowable distance value
    :param max_neighbors: Maximum number of neighbors to be checked for a connection, defaults to 30.
    :param max_time_s: Time in seconds to be spent on creating a probabilistic roadmap
    :return:
    """
    if dj <= 0:
        raise ValueError('Joint increment must be positive.')

    # Create a new undirected graph and a list of the nodes to store the joint coordinates
    graph = Graph(undirected=True)
    nodes = []

    # Initialize the time
    t0 = time.time()
    current_time = t0
    print('\n')
    prefix = 'Creating probabilistic roadmap ...'

    while current_time - t0 < max_time_s:
        # Update the progress bar
        print_progress(int(current_time - t0), max_time_s, prefix=prefix)

        # Create a new node
        current_node_idx = len(nodes)
        try:
            j0 = generate_rand_free_node(config, collider,
                                         constr.pos_cartesian,
                                         constr.pos_joint)
        except ValueError:
            # Maximum number of iterations reached, retry if some time is left
            break

        # Log the data of the new node
        graph.add_node(current_node_idx)
        nodes.append(j0)

        # Calculate distance for all nodes
        neighbors = [(joint_node_distance(j0, jn), jn, idx)
                     for idx, jn in enumerate(nodes[:current_node_idx])]

        # Attempt to connect neighbors (smallest distance first)
        is_connected = False
        for distance, jn, n_idx in sorted(neighbors, key=lambda x: x[0]):
            # Stop searching if distance or neigbor count exceeding thresholds
            if (distance > dmax or n_idx >= max_neighbors) and is_connected:
                break

            # Node selection logic
            try:
                # Check connectivity with rest of the graph
                path_info: PathInfo = find_path(graph, current_node_idx, n_idx)
            except NoPathError:
                # Connect neighbors that are not in the same tree
                pass
            else:
                if len(path_info.nodes) <= 3:
                    # Skip nodes that have paths via one intermittent node
                    continue

            # Generate the path and check discrete nodes along it, path does not need to be saved
            path = generate_joint_path(j0, jn, dj=5)
            if all(
                    is_node_free_and_within(config, collider, ji,
                                            constr.pos_cartesian)
                    for ji in path):
                # Connect the nodes with edge 1 so that number of intermediate steps can be minimized
                graph.add_edge(current_node_idx, n_idx, edge=1)
                is_connected = True

        # Update time
        current_time = time.time()

    print_progress(max_time_s, max_time_s, prefix=prefix)
    return graph, tuple(nodes)
示例#4
0
class Grid:
    def __init__(self, height, width, blocks, spawner_square, exit_square,
                 lives, souls):
        self.height = height
        self.width = width
        self.blocks = blocks
        self.spawner_square = spawner_square
        self.exit_square = exit_square
        self.lives = lives
        self.souls = souls
        self.dijk_grid = Graph()
        self.route_dict = {}
        self.num_loc_list = []
        self.tower_list = []
        self.square_grid = []
        self.forbidden_squares = set()

    def initialise_dijk_grid(self):
        """Initialises the dijk_grid attribute for routing"""
        for i in range(self.height):
            for j in range(self.width):
                if i < self.height - 1:
                    self.dijk_grid.add_edge((i, j), (i + 1, j), 1)
                    self.dijk_grid.add_edge((i + 1, j), (i, j), 1)
                if j < self.width - 1:
                    self.dijk_grid.add_edge((i, j), (i, j + 1), 1)
                    self.dijk_grid.add_edge((i, j + 1), (i, j), 1)
        for node in self.blocks:
            self.dijk_grid.remove_node(node)

    def initialise_route_dict(self):
        """Initialises the route_dict attribute"""
        self.route_dict[self.spawner_square] = find_route(
            self.dijk_grid, self.spawner_square, self.exit_square)

    def initialise_square_grid(self, display):
        """Initialises the square_grid attribute"""
        screenWidth, screenHeight = display.get_size()
        squarelen = int(screenHeight /
                        self.height)  # Length of each square on the grid

        gridStartX = int(screenWidth / 2 - screenHeight / 2)
        gridStartY = 0

        for i in range(self.height):
            row = []
            for j in range(self.width):
                sqrX = gridStartX + squarelen * j
                sqrY = gridStartY + squarelen * i
                greySqr = pygame.Rect(sqrX, sqrY, squarelen, squarelen)
                sqr = Square(greySqr)
                row.append([sqr])
            self.square_grid.append(row)

    def initialise_exit(self):
        """Adds the Exit to its square_grid square"""
        try:
            sqr = self.square_grid[self.exit_square[0]][self.exit_square[1]][0]
            w, h = sqr.surface.width, sqr.surface.height
            rect = pygame.Rect(sqr.surface.x, sqr.surface.y, w, h)
            self.square_grid[self.exit_square[0]][self.exit_square[1]].append(
                Exit(rect, self.lives))
        except (IndexError):
            print(
                "Instance variable square_grid has not been initialised properly. Please call the method initialise_square_grid to initialise it."
            )

    def initialise_spawner(self):
        """Adds the Spawner to its square_grid square"""
        try:
            sqr = self.square_grid[self.spawner_square[0]][
                self.spawner_square[1]][0]
            w, h = sqr.surface.width, sqr.surface.height
            rect = pygame.Rect(sqr.surface.x, sqr.surface.y, w, h)
            self.square_grid[self.spawner_square[0]][
                self.spawner_square[1]].append(Spawner(rect))
        except (IndexError):
            print(
                "Instance variable square_grid has not been initialised properly. Please call the method initialise_square_grid to initialise it."
            )

    def initialise_blocks(self):
        """Adds the Blocks to their square_grid squares"""
        try:
            for block in self.blocks:
                sqr = self.square_grid[block[0]][block[1]][0]
                w, h = sqr.surface.width, sqr.surface.height
                rect = pygame.Rect(sqr.surface.x, sqr.surface.y, w, h)
                self.square_grid[block[0]][block[1]].append(Block(rect))
        except (IndexError):
            print(
                "Instance variable square_grid has not been initialised properly. Please call the method initialise_square_grid to initialise it."
            )

    def build_tower(self, value, operation, location):
        """Adds a new Tower to a square_grid square and blocks off its dijk_grid square"""
        sqr = self.square_grid[location[0]][location[1]][0]
        w, h = sqr.surface.width, sqr.surface.height
        rect = pygame.Rect(sqr.surface.x, sqr.surface.y, w, h)
        tower = Tower(rect, value, operation, location)
        self.square_grid[tower.location[0]][tower.location[1]].append(tower)
        self.dijk_grid.remove_node(tower.location)
        self.tower_list.append(tower)
        self.souls -= tower.cost

    def sell_tower(self, tower):
        """For selling a Tower and removing from square_grid and adding its square to dijk_grid"""
        pos = (tower.location[0], tower.location[1])
        self.tower_list.remove(tower)
        self.square_grid[pos[0]][pos[1]].remove(tower)
        for node in [(pos[0], pos[1] + 1), (pos[0], pos[1] - 1),
                     (pos[0] + 1, pos[1]), (pos[0] - 1, pos[1])]:
            if node in self.dijk_grid:
                self.dijk_grid.add_edge(pos, node, 1)
                self.dijk_grid.add_edge(node, pos, 1)
        else:
            self.dijk_grid.add_node((pos[0], pos[1]))
        self.souls += max(tower.cost // 3, 1)

    def spawn_numemy(self, start_val, speed, weight):
        """Adds a new Numemy object to the square_grid spawner_square"""
        sqr = self.square_grid[self.spawner_square[0]][
            self.spawner_square[1]][0]
        w, h = sqr.surface.width, sqr.surface.height
        rect = pygame.Rect(sqr.surface.x, sqr.surface.y, w, h)
        numemy = Numemy(rect, start_val, speed, weight)
        numemy.location = self.spawner_square
        self.square_grid[numemy.location[0]][numemy.location[1]].append(numemy)
        self.num_loc_list.append(numemy.location)

    # Should be called after build_tower() and move_numemies()
    def update_routes(self):
        """Updates all routes which pass through a new_square being built upon"""
        new_route_dict = {
            self.spawner_square:
            find_route(self.dijk_grid, self.spawner_square, self.exit_square)
        }
        for num_loc in self.num_loc_list:
            new_route_dict[num_loc] = find_route(self.dijk_grid, num_loc,
                                                 self.exit_square)
        self.route_dict = new_route_dict

    # Should be called after build_tower(), update_routes()
    def update_forbidden_squares(self):
        """Updates the list of squares that cannot be built upon without isolating a Numemy"""
        self.forbidden_squares = set(
            self.num_loc_list) | {self.spawner_square, self.exit_square}
        for num_loc in self.route_dict:
            for square in self.route_dict[num_loc]:
                test_grid = copy.deepcopy(self.dijk_grid)
                test_grid.remove_node(square)
                try:
                    find_route(test_grid, num_loc, self.exit_square)
                except NoPathError:
                    self.forbidden_squares |= {square}
示例#5
0
文件: day15.py 项目: remis87/aoc2019
class IntCodeProgram():
    def __init__(self, name, program):
        self.program = program
        self.name = name
        self.output = []
        self.switch = {
            1: self.opt1,
            2: self.opt2,
            3: self.opt3,
            4: self.opt4,
            5: self.opt5,
            6: self.opt6,
            7: self.opt7,
            8: self.opt8,
            9: self.opt9,
            99: self.opt99
        }
        self.inputp = 0
        self.halt = False
        self.relative_base = 0
        self.memory = {}
        self.retpt = 0
        self.new_position = (0, 0)
        self.position = (0, 0)
        self.area = {(0, 0): 'X'}
        self.graph = Graph()
        self.graph.add_node((0, 0))
        self.oxygen = (0, 0)
        self.unexplored = set()
        self.explored = set([(0, 0)])
        self.graph.add_node((0, 0))
        self.path = []

    def run(self, inputs):
        if not self.halt:
            pp = self.retpt
            while pp >= 0:
                # console.log("executing pp {}".format(pp))
                opt_code = program[pp] % 100
                mode = str(program[pp])[:-2].zfill(3)
                pp = self.switch[opt_code](self.program, pp, mode, inputs)
        return self.output

    def print_area(self, cl=True):
        sz = 21
        min_x = -sz
        min_y = -sz
        for point in self.unexplored:
            area_x = point[0] - min_x
            area_y = point[1] - min_y
            pad.addch(area_y, area_x, '?')
        for point in self.area.keys():
            area_x = point[0] - min_x
            area_y = point[1] - min_y
            pad.addch(area_y, area_x, self.area[point])
        cursor_x = self.position[0] - min_x
        cursor_y = self.position[1] - min_y
        pad.move(cursor_y, cursor_x)
        pad.refresh()

    def read_value(self, pp, pp_offset, mode):
        index = 0
        if mode == '0':
            index = self.program[pp + pp_offset]
        if mode == '1':
            index = pp + pp_offset
        if mode == '2':
            index = self.relative_base + self.program[pp + pp_offset]
        if index >= len(self.program):
            return self.memory.get(index, 0)
        return self.program[index]

    def write_to_memory(self, index, value, mode):
        if mode == '2':
            index = self.relative_base + index
        if index >= len(self.program):
            self.memory[index] = value
        else:
            self.program[index] = value

    def opt1(self, prog, pp, opt_mode, _):
        m1 = opt_mode[2]
        m2 = opt_mode[1]
        a = self.read_value(pp, 1, m1)
        b = self.read_value(pp, 2, m2)
        self.write_to_memory(prog[pp + 3], a + b, opt_mode[0])
        return pp + 4

    def opt2(self, prog, pp, mode, _):
        m1 = mode[2]
        m2 = mode[1]
        a = self.read_value(pp, 1, m1)
        b = self.read_value(pp, 2, m2)
        self.write_to_memory(prog[pp + 3], a * b, mode[0])
        return pp + 4

    def get_new_pos(self, direction):
        if direction == 1:
            np = self.position[0], self.position[1] + 1
        if direction == 2:
            np = self.position[0], self.position[1] - 1
        if direction == 3:
            np = self.position[0] + 1, self.position[1]
        if direction == 4:
            np = self.position[0] - 1, self.position[1]
        return np

    def get_input(self):
        p = self.position
        neighbors = [(p[0], p[1] + 1), (p[0], p[1] - 1), (p[0] + 1, p[1]),
                     (p[0] - 1, p[1])]
        choices = []
        for i in range(4):
            if self.area.get(neighbors[i], '.') != '#':
                choices.append(i + 1)
                if not neighbors[i] in self.area:
                    choices.append(i + 1)
                    choices.append(i + 1)
        c = choice(choices)
        newly_unexplored = set(neighbors).difference(self.explored)
        self.unexplored.update(newly_unexplored)
        return c

    def get_dir_to_next_pos(self, new_pos):
        pos = self.position
        x_diff = pos[0] - new_pos[0]
        y_diff = pos[1] - new_pos[1]
        out = -1
        if x_diff == -1:
            out = 3
        if x_diff == 1:
            out = 4
        if y_diff == -1:
            out = 1
        if y_diff == 1:
            out = 2
        newly_unexplored = set(self.get_neighbors(self.position)).difference(
            self.explored)
        self.unexplored.update(newly_unexplored)
        return out

    def get_path_to_unexplored(self, unex):
        paths = []
        for candidate in self.get_neighbors(unex).intersection(self.explored):
            paths.append(find_path(self.graph, self.position, candidate))
        node_seq = min(paths, key=lambda x: x.total_cost).nodes
        node_seq.append(unex)
        return node_seq[1:]

    def opt3(self, prog, pp, mode, inputs):
        if len(self.path) == 0 and len(self.unexplored) > 0:
            closest_unex = min(self.unexplored,
                               key=lambda x: abs(self.position[0] - x[0]) +
                               abs(self.position[1] - x[1]))
            path = self.get_path_to_unexplored(closest_unex)
            self.path = path
        if len(self.path) > 0:
            inp = self.get_dir_to_next_pos(self.path.pop(0))
        else:
            inp = self.get_input()
        self.new_position = self.get_new_pos(inp)
        self.unexplored = self.unexplored.difference(set(self.area.keys()))
        self.write_to_memory(prog[pp + 1], inp, mode[2])
        return pp + 2

    def get_symbol(self, code):
        code_map = {0: '#', 1: '.', 2: 'O'}
        return code_map[code]

    def get_neighbors(self, p):
        return {(p[0], p[1] + 1), (p[0], p[1] - 1), (p[0] + 1, p[1]),
                (p[0] - 1, p[1])}

    def addNodeToGraph(self, u, v):
        if u not in self.graph:
            self.graph.add_node(u)
        if v not in self.graph:
            self.graph.add_node(v)
        self.graph.add_edge(u, v, 1)
        self.graph.add_edge(v, u, 1)

    def opt4(self, prog, pp, mode, _):
        out = self.read_value(pp, 1, mode[2])
        self.output.append(out)
        self.area[self.new_position] = self.get_symbol(out)

        if out == 1:
            self.explored.add(self.new_position)
            self.addNodeToGraph(self.position, self.new_position)
            self.position = self.new_position
        if out == 2:
            self.explored.add(self.new_position)
            self.addNodeToGraph(self.position, self.new_position)
            self.oxygen = self.new_position
            self.position = self.new_position
        if len(self.unexplored) == 0 and len(self.explored) > 1:
            self.halt = True
            return -1
        self.print_area()
        return pp + 2

    def opt5(self, prog, pp, mode, _):
        m1 = mode[2]
        m2 = mode[1]
        x = self.read_value(pp, 1, m1)
        if x != 0:
            return self.read_value(pp, 2, m2)
        return pp + 3

    def opt6(self, prog, pp, mode, _):
        m1 = mode[2]
        m2 = mode[1]
        x = self.read_value(pp, 1, m1)
        if x == 0:
            return self.read_value(pp, 2, m2)
        return pp + 3

    def opt7(self, prog, pp, mode, _):
        m1 = mode[2]
        m2 = mode[1]
        x = self.read_value(pp, 1, m1)
        y = self.read_value(pp, 2, m2)
        ret = 0
        if x < y:
            ret = 1
        self.write_to_memory(prog[pp + 3], ret, mode[0])
        return pp + 4

    def opt8(self, prog, pp, mode, _):
        m1 = mode[2]
        m2 = mode[1]
        m3 = mode[0]
        x = self.read_value(pp, 1, m1)
        y = self.read_value(pp, 2, m2)
        ret = 0
        if x == y:
            ret = 1
        self.write_to_memory(prog[pp + 3], ret, m3)
        return pp + 4

    def opt9(self, prog, pp, mode, _2):
        inc = self.read_value(pp, 1, mode[2])
        self.relative_base += inc
        return pp + 2

    def opt99(self, prog, _1, _2, _3):
        self.halt = True
        return -1
示例#6
0
    'a': {
        'b': 10,
        'c': 100,
        'd': 1
    },
    'b': {
        'c': 10
    },
    'd': {
        'b': 1,
        'e': 1
    },
    'e': {
        'f': 1
    },
})
graph.add_node('f', {'c': 1})
graph['f'] = {'c': 1}

graph.add_edge('f', 'c', 1)
graph.add_edge('g', 'b', 1)

nodes = list(graph)
nodes.sort()

incoming = graph.get_incoming('c')
incoming_nodes = list(incoming.keys())
incoming_nodes.sort()

paths = single_source_shortest_paths(graph, 'a')
print(paths)
示例#7
0
文件: Main.py 项目: mthsnts/Grafo
    elif int(menu) == 3:
        print('\n\n')
        g.printArestas()
        print('\n\n')
        g.listAdjacencia(tipo, valor)
        g.adj(valor, tipo)
        g.incidencia(tipo)

    elif int(menu) == 4:
        nodes = []
        edges = []
        nos = input(
            'Digite o conjunto V separado por vírgula,(Apenas números): ')
        for n in nos.split(','):
            nodes.append(int(n))
            graph.add_node(int(n))
        if tipo == 'S':
            q = 'S'
            while q == 'S':
                q = input('Deseja inserir aresta ?')
                if q == 'S':
                    v1 = input('digite vertice 1:   ')
                    v2 = input('digite vertice 2:   ')
                    vl = input('digite o valor:     ')

                    graph.add_edge(int(v1), int(v2), {'cost': int(vl)})

        elif tipo == 'N':
            q = 'S'
            while q == 'S':
                q = input('Deseja inserir aresta ?(S/N)')