Exemple #1
0
    def run(self):

        print('K VALUE: ', self.k)
        domain = [x for x in range(1, self.k + 1)]
        function = make_function(['x, y'], 'x != y')
        gac = GAC()
        nodes = {}
        constraints = {}

        for node in sorted(self.board.graph.nodes()):
            id = str(node)
            var = Variable(id)
            nodes[id] = var
            var.domain = domain
#            gac.variables.append(var)

        for key in nodes.keys():
            constraints[key] = self.board.graph.neighbors(key)

        constraint = Constraint()
        constraint.function = function

        state = CSPState()
        state.constraints = constraints
        state.constraint = constraint
        state.variables = nodes
        state.gac = gac
        gac.state = state
        astar_csp = AstarCSP()
        astar_csp.csp_state = state
        astar_csp.initialize()

        for r in astar_csp.run():
            if r[0]:
                s = r[0][-1]
                self.redraw_nodes_with_color(s)
                #self.color_node(s.colored_node)

                unsatisfied = [(len(x.domain) - 1)
                               for x in gac.state.variables.values()]
                without_assignments = [
                    1 for x in gac.state.variables.values()
                    if len(x.domain) > 1
                ]
                print('UNSATISFIED CONSTRAINTS: {}'.format(sum(unsatisfied)))
                print('Vertices without assignments: {}'.format(
                    sum(without_assignments)))
                print('Nodes in seachtree: {}'.format(len(r[1])))
                print('Number of expanded: {}'.format(len(r[2])))
Exemple #2
0
    def run(self):

        print('K VALUE: ', self.k)
        domain = [x for x in range(1, self.k + 1)]
        function = make_function(['x, y'], 'x != y')
        gac = GAC()
        nodes = {}
        constraints = {}

        for node in sorted(self.board.graph.nodes()):
            id = str(node)
            var = Variable(id)
            nodes[id] = var
            var.domain = domain
#            gac.variables.append(var)

        for key in nodes.keys():
            constraints[key] = self.board.graph.neighbors(key)

        constraint = Constraint()
        constraint.function = function

        state = CSPState()
        state.constraints = constraints
        state.constraint = constraint
        state.variables = nodes
        state.gac = gac
        gac.state = state
        astar_csp = AstarCSP()
        astar_csp.csp_state = state
        astar_csp.initialize()

        for r in astar_csp.run():
            if r[0]:
                s = r[0][-1]
                self.redraw_nodes_with_color(s)
                #self.color_node(s.colored_node)

                unsatisfied = [(len(x.domain)-1) for x in gac.state.variables.values()]
                without_assignments = [1 for x in gac.state.variables.values() if  len(x.domain) > 1]
                print('UNSATISFIED CONSTRAINTS: {}'.format(sum(unsatisfied)))
                print('Vertices without assignments: {}'.format(sum(without_assignments)))
                print('Nodes in seachtree: {}'.format(len(r[1])))
                print('Number of expanded: {}'.format(len(r[2])))
Exemple #3
0
    def run(self):

        gac = NonogramGAC()
        nodes = {}
        function = make_function(['x', 'y'], 'x == y') #x[1][x[0]]
        constraints = {}

        for row in range(len(self.board.rows)):
            constraints[(0, row)] = [(1, x) for x in range(len(self.board.cols))]
            node = Variable((0, row))
            node.domain = generate_permutations(self.board.rows[row], len(self.board.cols))
            print("xxxxx", self.board.rows[row])
            print(node.domain)
            nodes[(0, row)] = node

        for col in range(len(self.board.cols)):
            constraints[(1, col)] = [(0, x) for x in range(len(self.board.rows))]
            node = Variable((1, col))
            node.domain = generate_permutations(self.board.cols[col], len(self.board.rows))
            nodes[(1, col)] = node


        constraint = Constraint()
        constraint.function = function
        state = CSPState()
        state.constraints = constraints
        state.constraint = constraint
        state.variables = nodes
        state.gac = gac
        gac.state = state
        astar_csp = AstarCSP()
        astar_csp.csp_state = state
        astar_csp.initialize()
        pprint(state.constraints)


        for r in astar_csp.run():
            if r[0]:
                s = r[0][-1]
                self.generate_matrix(s)
                self.draw_map()
                #self.update_map()
                print("This is s: ", s)
Exemple #4
0
    def run(self):

        gac = NonogramGAC()
        nodes = {}
        function = make_function(["x", "y"], "x == y")  # x[1][x[0]]
        constraints = {}

        for row in range(len(self.board.rows)):
            constraints[(0, row)] = [(1, x) for x in range(len(self.board.cols))]
            node = Variable((0, row))
            node.domain = generate_permutations(self.board.rows[row], len(self.board.cols))
            print("xxxxx", self.board.rows[row])
            print(node.domain)
            nodes[(0, row)] = node

        for col in range(len(self.board.cols)):
            constraints[(1, col)] = [(0, x) for x in range(len(self.board.rows))]
            node = Variable((1, col))
            node.domain = generate_permutations(self.board.cols[col], len(self.board.rows))
            nodes[(1, col)] = node

        constraint = Constraint()
        constraint.function = function
        state = CSPState()
        state.constraints = constraints
        state.constraint = constraint
        state.variables = nodes
        state.gac = gac
        gac.state = state
        astar_csp = AstarCSP()
        astar_csp.csp_state = state
        astar_csp.initialize()
        pprint(state.constraints)

        for r in astar_csp.run():
            if r[0]:
                s = r[0][-1]
                self.generate_matrix(s)
                self.draw_map()
                # self.update_map()
                print("This is s: ", s)