Example #1
0
def revise(C, state):
	if show_gui_revise:
		import gui
	if C[0].get_is_row():
		index_d0 = state.get_row(C[0].get_index()).get_index()
		index_d1 = state.get_col(C[1].get_index()).get_index()
		#
		is_possible = True
		#
		if len(state.get_row(C[0].get_index()).get_domain()) == 0 or len(state.get_col(C[1].get_index()).get_domain()) < 2:
			return len(state.get_col(C[1].get_index()).get_domain())
		elif len(state.get_row(C[0].get_index()).get_domain()) > 1:
			for n in xrange(1, len(state.get_row(C[0].get_index()).get_domain())):
				check_val = state.get_row(C[0].get_index()).get_domain()[0][index_d1]
				if check_val != state.get_row(C[0].get_index()).get_domain()[n][index_d1]:
					is_possible = False
		if not is_possible:
			return len(state.get_col(C[1].get_index()).get_domain())
		else:
			check_val = state.get_row(C[0].get_index()).get_domain()[0][index_d1]
			for domain in state.get_col(C[1].get_index()).get_domain():
				if domain[index_d0] != check_val:
					state.get_col(C[1].get_index()).get_domain().remove( domain )
					if show_gui_revise:
						temp, board = state.make_board()
						gui.rectMatrix = gui.generate_rectMatrix(gui.generate_color_matrix(board))
						gui.app.processEvents()

		return len(state.get_col(C[1].get_index()).get_domain())
	else:
		##-- c[0] == kolonne --##
		index_d0 = state.get_col(C[0].get_index()).get_index()
		index_d1 = state.get_row(C[1].get_index()).get_index()
		#
		is_possible = True
		#
		if len(state.get_col(C[0].get_index()).get_domain()) == 0 or len(state.get_row(C[1].get_index()).get_domain()) < 2:
			return len(state.get_row(C[1].get_index()).get_domain())
		elif len(state.get_col(C[0].get_index()).get_domain()) > 1:
			for n in xrange(1, len(state.get_col(C[0].get_index()).get_domain())):
				check_val = state.get_col(C[0].get_index()).get_domain()[0][index_d1]
				if check_val != state.get_col(C[0].get_index()).get_domain()[n][index_d1]:
					is_possible = False
		if not is_possible:
			return len(state.get_row(C[1].get_index()).get_domain())
		else:
			check_val = state.get_col(C[0].get_index()).get_domain()[0][index_d1]
			for domain in state.get_row(C[1].get_index()).get_domain():
				if domain[index_d0] != check_val:
					state.get_row(C[1].get_index()).get_domain().remove( domain )
					if show_gui_revise:
						temp, board = state.make_board()
						gui.rectMatrix = gui.generate_rectMatrix(gui.generate_color_matrix(board))
						gui.app.processEvents()
		return len(state.get_row(C[1].get_index()).get_domain())
Example #2
0
def Filter(state,
           queue):  #Iterates through the GAC_queue -> runs revice on them
    if show_gui_filter:
        import gui
    while queue:
        q = queue.popleft()  #popper constraint fra ko
        length_pre_revise = len(q[1].domain)
        length_post_revice = revise(
            q, state)  #kjorer revice paa constrainten som ble poppet
        #
        if length_pre_revise > length_post_revice:  #hvis domenet har blitt forkortet maa nye constarints inn i ko
            if show_gui_filter:
                if len(queue) % 5 == 0:
                    #print "Showing gui from filter"
                    temp, board = state.make_board()
                    gui.rectMatrix = gui.generate_rectMatrix(
                        gui.generate_color_matrix(board))
                    gui.app.processEvents()

            if q[1].get_is_row():
                queue.extend(
                    extend_queue(state, state.get_row(q[1].get_index())))
            else:
                queue.extend(
                    extend_queue(state, state.get_col(q[1].get_index())))
Example #3
0
def Filter(state, queue):#Iterates through the GAC_queue -> runs revice on them
	if show_gui_filter:
		import gui
	while queue:
		q                  = queue.popleft()   	  #popper constraint fra ko
		length_pre_revise  = len(q[1].domain)
		length_post_revice = revise(q,state)			  #kjorer revice paa constrainten som ble poppet
		#
		if length_pre_revise > length_post_revice:  #hvis domenet har blitt forkortet maa nye constarints inn i ko
			if show_gui_filter:
				if len(queue)% 5 == 0:
					#print "Showing gui from filter"
					temp, board = state.make_board()
					gui.rectMatrix = gui.generate_rectMatrix(gui.generate_color_matrix(board))
					gui.app.processEvents()

			if q[1].get_is_row():
				queue.extend( extend_queue(state, state.get_row(q[1].get_index())))
			else:
				queue.extend( extend_queue(state, state.get_col(q[1].get_index())))
Example #4
0
def Astar(start_state, constraints_rows, constraints_columns):
    if show_gui:
        import gui

    print "press any key to continue"
    raw_input()
    start_time2 = time.time()
    print "Astar running..."
    ##
    all_states = create_dictionary(start_state.get_h())
    ##
    if show_gui:
        temp, board = start_state.make_board()
        gui.rectMatrix = gui.generate_rectMatrix(
            gui.generate_color_matrix(board))
        gui.app.processEvents()
    print "for philter"
    time.sleep(0.5 + algorithm_delay)

    Filter(start_state, make_all_constraints(start_state))

    if show_gui:
        temp, board = start_state.make_board()
        gui.rectMatrix = gui.generate_rectMatrix(
            gui.generate_color_matrix(board))
        gui.app.processEvents()
        time.sleep(0.2 + algorithm_delay)

    temp, board = start_state.make_board()
    children = generate_child_states(start_state)
    #
    nodes_generated = 0
    nodes_expanded = 1
    while True:
        time.sleep(algorithm_delay)
        #for xxx in xrange(2):
        if children:
            nodes_generated += len(children)
            valid_children = []
            for child in children:
                if show_gui:
                    gui.app.processEvents()
                    temp, board = child.make_board()
                    gui.rectMatrix = gui.generate_rectMatrix(
                        gui.generate_color_matrix(board))
                    gui.app.processEvents()
                    time.sleep(algorithm_delay)
                #
                Filter(child, create_GAC_queue(child))

                if show_gui:
                    temp, board = child.make_board()
                    gui.rectMatrix = gui.generate_rectMatrix(
                        gui.generate_color_matrix(board))
                    gui.app.processEvents()
                #
                temp, board = child.make_board()
                #
                if temp and is_valid_state(board, constraints_rows,
                                           constraints_columns):
                    #print "H for filter: ",child.get_h()
                    child.set_h(child.calculate_h())
                    #print "H etter filter: ",child.get_h(),"\n"
                    valid_children.append(child)
                    ##-- check if if child is a solution --##
                    if is_board_done(board) or is_done(child):
                        print("--- Solved in %s seconds ---" %
                              (time.time() - start_time2))
                        print "ER I MAAL!!!\n\n"
                        print "Number of nodes generated", nodes_generated
                        print "Number of nodes expanded", nodes_expanded
                        print "Antall steg til maal: ", child.get_g()
                        print "\n\nBRETTTET"
                        for b in board:
                            print b
                        gui.app.processEvents()
                        temp, board = child.make_board()
                        gui.rectMatrix = gui.generate_rectMatrix(
                            gui.generate_color_matrix(board))
                        gui.app.processEvents()

                        print "Press ENTER to close gui, input 'n' to keep it open"
                        stri = str(raw_input(""))
                        if not (stri == "n" or stri == "N"):
                            import subprocess
                            subprocess.call("taskkill /F /IM python.exe",
                                            shell=True)
                        time.sleep(10)
                        return True
            #

            all_states = add_states_to_dict(valid_children, all_states)
            current_state = get_best_state(all_states)
            nodes_expanded += 1
            gui.app.processEvents()
            temp, board = current_state.make_board()
            gui.rectMatrix = gui.generate_rectMatrix(
                gui.generate_color_matrix(board))
            gui.app.processEvents()
            time.sleep(algorithm_delay)

            children = generate_child_states(current_state)
            all_states[current_state.get_h()].remove(current_state)
            #
        else:
            current_state = get_best_state(all_states)
            children = generate_child_states(current_state)
            all_states[current_state.get_h() +
                       current_state.get_g()].remove(current_state)
Example #5
0
def draw_gui(state):
    import gui
    print "drawing gui"
    temp, board = state.make_board()
    gui.rectMatrix = gui.generate_rectMatrix(gui.generate_color_matrix(board))
    gui.app.processEvents()
Example #6
0
def revise(C, state):
    if show_gui_revise:
        import gui
    if C[0].get_is_row():
        index_d0 = state.get_row(C[0].get_index()).get_index()
        index_d1 = state.get_col(C[1].get_index()).get_index()
        #
        is_possible = True
        #
        if len(state.get_row(C[0].get_index()).get_domain()) == 0 or len(
                state.get_col(C[1].get_index()).get_domain()) < 2:
            return len(state.get_col(C[1].get_index()).get_domain())
        elif len(state.get_row(C[0].get_index()).get_domain()) > 1:
            for n in xrange(1,
                            len(state.get_row(C[0].get_index()).get_domain())):
                check_val = state.get_row(
                    C[0].get_index()).get_domain()[0][index_d1]
                if check_val != state.get_row(
                        C[0].get_index()).get_domain()[n][index_d1]:
                    is_possible = False
        if not is_possible:
            return len(state.get_col(C[1].get_index()).get_domain())
        else:
            check_val = state.get_row(
                C[0].get_index()).get_domain()[0][index_d1]
            for domain in state.get_col(C[1].get_index()).get_domain():
                if domain[index_d0] != check_val:
                    state.get_col(C[1].get_index()).get_domain().remove(domain)
                    if show_gui_revise:
                        temp, board = state.make_board()
                        gui.rectMatrix = gui.generate_rectMatrix(
                            gui.generate_color_matrix(board))
                        gui.app.processEvents()

        return len(state.get_col(C[1].get_index()).get_domain())
    else:
        ##-- c[0] == kolonne --##
        index_d0 = state.get_col(C[0].get_index()).get_index()
        index_d1 = state.get_row(C[1].get_index()).get_index()
        #
        is_possible = True
        #
        if len(state.get_col(C[0].get_index()).get_domain()) == 0 or len(
                state.get_row(C[1].get_index()).get_domain()) < 2:
            return len(state.get_row(C[1].get_index()).get_domain())
        elif len(state.get_col(C[0].get_index()).get_domain()) > 1:
            for n in xrange(1,
                            len(state.get_col(C[0].get_index()).get_domain())):
                check_val = state.get_col(
                    C[0].get_index()).get_domain()[0][index_d1]
                if check_val != state.get_col(
                        C[0].get_index()).get_domain()[n][index_d1]:
                    is_possible = False
        if not is_possible:
            return len(state.get_row(C[1].get_index()).get_domain())
        else:
            check_val = state.get_col(
                C[0].get_index()).get_domain()[0][index_d1]
            for domain in state.get_row(C[1].get_index()).get_domain():
                if domain[index_d0] != check_val:
                    state.get_row(C[1].get_index()).get_domain().remove(domain)
                    if show_gui_revise:
                        temp, board = state.make_board()
                        gui.rectMatrix = gui.generate_rectMatrix(
                            gui.generate_color_matrix(board))
                        gui.app.processEvents()
        return len(state.get_row(C[1].get_index()).get_domain())
Example #7
0
def Astar(start_state, constraints_rows, constraints_columns):
	if show_gui:
		import gui

	print "press any key to continue"
	raw_input()
	start_time2 = time.time()
	print "Astar running..."
	##
	all_states = create_dictionary(start_state.get_h())
	##
	if show_gui:
		temp, board = start_state.make_board()
		gui.rectMatrix = gui.generate_rectMatrix(gui.generate_color_matrix(board))
		gui.app.processEvents()
	print "for philter"
	time.sleep(0.5+algorithm_delay)

	Filter(start_state, make_all_constraints(start_state))

	if show_gui:
		temp, board = start_state.make_board()
		gui.rectMatrix = gui.generate_rectMatrix(gui.generate_color_matrix(board))
		gui.app.processEvents()
		time.sleep(0.2+algorithm_delay)

	temp, board = start_state.make_board()
	children = generate_child_states(start_state)
	#
	nodes_generated = 0
	nodes_expanded = 1
	while True:
		time.sleep(algorithm_delay)
	#for xxx in xrange(2):
		if children:
			nodes_generated += len(children)
			valid_children = []
			for child in children:
				if show_gui:
					gui.app.processEvents()
					temp, board = child.make_board()
					gui.rectMatrix = gui.generate_rectMatrix(gui.generate_color_matrix(board))
					gui.app.processEvents()
					time.sleep(algorithm_delay)
				#
				Filter(child,create_GAC_queue(child))

				if show_gui:
					temp, board = child.make_board()
					gui.rectMatrix = gui.generate_rectMatrix(gui.generate_color_matrix(board))
					gui.app.processEvents()
				#
				temp, board = child.make_board()
				#
				if temp and is_valid_state(board, constraints_rows, constraints_columns):
					#print "H for filter: ",child.get_h()
					child.set_h(child.calculate_h())
					#print "H etter filter: ",child.get_h(),"\n"
					valid_children.append(child)
					##-- check if if child is a solution --##
					if is_board_done(board) or is_done(child):
						print("--- Solved in %s seconds ---" % (time.time() - start_time2))
						print "ER I MAAL!!!\n\n"
						print "Number of nodes generated", nodes_generated
						print "Number of nodes expanded", nodes_expanded
						print "Antall steg til maal: ", child.get_g()
						print "\n\nBRETTTET"
						for b in board:
							print b
						gui.app.processEvents()
						temp, board = child.make_board()
						gui.rectMatrix = gui.generate_rectMatrix(gui.generate_color_matrix(board))
						gui.app.processEvents()

						print "Press ENTER to close gui, input 'n' to keep it open"
						stri = str(raw_input(""))
						if not (stri=="n" or stri=="N"):
							import subprocess
							subprocess.call("taskkill /F /IM python.exe", shell=True)
						time.sleep(10)
						return True
			#

			all_states = add_states_to_dict(valid_children, all_states)
			current_state = get_best_state(all_states)
			nodes_expanded += 1
			gui.app.processEvents()
			temp, board = current_state.make_board()
			gui.rectMatrix = gui.generate_rectMatrix(gui.generate_color_matrix(board))
			gui.app.processEvents()
			time.sleep(algorithm_delay)


			children = generate_child_states(current_state)
			all_states[current_state.get_h()].remove(current_state)
			#
		else:
			current_state = get_best_state(all_states)
			children = generate_child_states(current_state)
			all_states[current_state.get_h()+current_state.get_g()].remove(current_state)
Example #8
0
def draw_gui(state):
	import gui
	print "drawing gui"
	temp, board = state.make_board()
	gui.rectMatrix = gui.generate_rectMatrix(gui.generate_color_matrix(board))
	gui.app.processEvents()