コード例 #1
0
    def get_all_successor_nodes(self, astar_state):
        """
        Fetches all successor nodes from a given CSP state
        In this spesific problem that means all states with a domain
        length greater than 1 for a random node
        :return: The generated successor nodes
        """
        csp_state = astar_state.state
        successor_nodes = []

        for node, domains in csp_state.nodes.items():
            if len(domains) > 1:
                for d in range(len(domains)):
                    child_state = deepcopy(csp_state)
                    child_state.nodes[node] = [list(domains)[d]]

                    if DEBUG:
                        print("Domain for %s is now %s" % (node, str(child_state.nodes[node])))

                    self.gac.csp_state = child_state
                    self.gac.run_again(node)

                    if not child_state.contradiction:
                        astar_state = AStarState()
                        astar_state.state = child_state
                        successor_nodes.append(astar_state)

                return successor_nodes
コード例 #2
0
ファイル: vc.py プロジェクト: myth/trashcan
    def get_all_successor_nodes(self, astar_state):
        """
        Fetches all successor nodes from a given CSP state
        In this spesific problem that means all states with a domain
        length greater than 1 for a random node
        :return: The generated successor nodes
        """
        csp_state = astar_state.state
        successor_nodes = []

        for node, domains in csp_state.nodes.items():
            if len(domains) > 1:
                for d in range(len(domains)):

                    child_state = deepcopy(csp_state)
                    child_state.nodes[node] = {list(domains)[d]}

                    if DEBUG:
                        print("Domain for %s is now %s" %
                              (node, str(child_state.nodes[node])))

                    self.gac.csp_state = child_state
                    self.gac.run_again(node)

                    if not child_state.contradiction:
                        astar_state = AStarState()
                        astar_state.state = child_state
                        successor_nodes.append(astar_state)

                return successor_nodes
コード例 #3
0
 def get_node(self, x, y):
     """
     Returnes a given node in the grid representation
     It used the x and y coordinates to achieve this
     :param x: x-coordinate
     :param y: y-coordinate
     :return:
     """
     a = AStarState(index=0, x=x, y=y)
     a.state = self.grid[y][x]
     return a
コード例 #4
0
 def get_node(self, x, y):
     """
     Returnes a given node in the grid representation
     It used the x and y coordinates to achieve this
     :param x: x-coordinate
     :param y: y-coordinate
     :return:
     """
     a = AStarState(index=0, x=x, y=y)
     a.state = self.grid[y][x]
     return a
コード例 #5
0
ファイル: vc.py プロジェクト: myth/trashcan
    def __init__(self, nodes, edges, cf=lambda x, y: x != y):
        """
        Constructor for VCProblem
        :param nodes: nodes in the VC-problem
        :param edges: edges between the nodes in the VC-problem
        """
        self.constraints = {}
        for from_node, to_node in edges:
            if from_node not in self.constraints:
                self.constraints[from_node] = []
            self.constraints[from_node].append(to_node)
            if to_node not in self.constraints:
                self.constraints[to_node] = []
            self.constraints[to_node].append(from_node)

        self.gac = GAC(csp_state=CSPState(nodes), cnet=self.constraints, cf=cf)

        self.gac.initialize()
        self.gac.domain_filtering_loop()
        self.initial_state = AStarState()
        self.initial_state.state = self.gac.csp_state
        self.goal_node = None

        # TODO: Check for contradiction or solution
        h = self.heuristic(self.initial_state)
        if h == 0:
            log("Found solution for VCProblem after first domain filtering loop"
                )
            print(
                "Found solution for VCProblem after first domain filtering loop"
            )  # Should add debug flag here!
コード例 #6
0
    def __init__(self, path):
        """
        Constructor for the NonogramProblem
        Will set up the grid and create all nodes with all possible permutations as domains
        """

        self.nodes = {}
        with open(path) as f:
            cols, rows = map(int, f.readline().split())

            self.grid = [[False] * cols] * rows
            self.total_rows = rows
            self.total_cols = cols

            r_reversed = []
            for row in range(rows):
                r_reversed.append(list(map(int, f.readline().split())))
            for row, counts in enumerate(reversed(r_reversed)):
                self.nodes[row] = [(row, p)
                                   for p in self.gen_patterns(counts, cols)]
            for col in range(cols):
                counts = list(map(int, f.readline().split()))
                self.nodes[rows + col] = [
                    (col, p) for p in self.gen_patterns(counts, rows)
                ]

        if DEBUG:
            for x in range(rows + cols):
                print(self.nodes[x])

        self.constraints = {}
        self.generate_constraints()

        def cf(a, b):
            r, domain_a = a
            c, domain_b = b
            return domain_a[c] == domain_b[r]

        self.gac = GAC(cnet=self.constraints,
                       csp_state=CSPState(self.nodes),
                       cf=cf)
        self.gac.initialize()
        self.gac.domain_filtering_loop()
        self.initial_state = AStarState()
        self.initial_state.state = self.gac.csp_state

        log('NonogramProblem initialized with %dx%d grid' % (rows, cols))
コード例 #7
0
    def init_grid_from_file(self):
        """
        Reads and parses all the data from the text file representing the board
        """
        with open(self.board_path) as f:
            width, height = map(int, f.readline().split())
            self.grid = [[
                AStarState(index=(y * x + x), x=x, y=y) for x in range(width)
            ] for y in range(height)]
            sx, sy, gx, gy = map(int, f.readline().split())
            self.start_node = self.get_node(sx, sy)
            self.start_node.is_start = True
            self.goal_node = self.get_node(gx, gy)
            self.goal_node.is_goal = True

            for line in f.readlines():
                ox, oy, ow, oh = map(int, line.split())
                for y in range(oh):
                    for x in range(ow):
                        obstacle_node = self.get_node(ox + x, oy + y)
                        obstacle_node.walkable = False