예제 #1
0
def main():
    puzzle = Puzzle(hard_table)
    stopwatch = Stopwatch()
    stopwatch.tic()
    print("Searching A*")
    result = AStar(puzzle, h1).search()
    stopwatch.toc()
    for path in result.path:
        sleep(0.25)
        print_puzzle_table(path)

    print("Depth = " + str(result.depth))
    print("Cost = " + str(result.cost))
    print("Time = " + str(stopwatch))
    return 0
예제 #2
0
def main():
    containers = 6
    capacity = 6
    wastes = ['t{}'.format(i) for i in range(4)]
    explosive = ['e{}'.format(i) for i in range(4)]
    frozen = ['fz{}'.format(i) for i in range(5)]
    fresh = ['fs{}'.format(i) for i in range(6)]
    edibles = ['f{}'.format(i) for i in range(6)]

    stopwatch = Stopwatch()
    stopwatch.tic()
    container = ContainerAssignment(containers, capacity, wastes, edibles,
                                    explosive, frozen, fresh)
    result = container.backtrack_search()
    stopwatch.toc()
    print("N = {};\ttime={}".format(4, stopwatch))
    print(container)
    return 0
예제 #3
0
def main():
    table = hardest_table

    sudoku = Sudoku(table)
    stopwatch = Stopwatch()
    stopwatch.tic()
    res = sudoku.search()
    stopwatch.toc()
    print("Solved={}\ttime={}".format(res.success, stopwatch))

    if res.success:
        for coordinates, number in res.assignment.items():
            if table[coordinates[0]][coordinates[1]] == 0:
                table[coordinates[0]][coordinates[1]] = number
            else:
                if table[coordinates[0]][coordinates[1]] != number:
                    raise Exception("Algoritmo di ricerca sminchiato")

        sudoku_pretty_print(table)
    return 0
예제 #4
0
파일: queen.py 프로젝트: DavideLoconte/AIML
    def is_assignment_consistent(self, var, value):
        return self.assignment.count(value) == 0 and self.__check_diagonals(
            (var, value))

    def add_assignment(self, var, value):
        self.assignment[var] = value

    def remove_assignment(self, var):
        self.assignment[var] = -1

    def __check_diagonals(self, house):
        for i in range(self.size // 2 + 1):
            for old_assignment in enumerate(self.assignment):
                if old_assignment[1] == -1:
                    continue
                if house[0] in [old_assignment[0] + i, old_assignment[0] - i] and \
                        house[1] in [old_assignment[1] + i, old_assignment[1] - i]:
                    return False
        return True


stopwatch = Stopwatch()
stopwatch.tic()
queen = QueenCSP(60)
result = queen.backtrack_search()
stopwatch.toc()
print("N = {};\ttime={}".format(8, stopwatch))
if result.success:
    print("result: {}".format(result.assignment))
    print(make_board(result.assignment))