Example #1
0
def find_best_match():
    """Find the best matching word from the dictionary."""
    i = input().split()
    time_limit = int(i[0])
    letter_multiset_size = int(i[1])
    initial_size = int(i[2])
    multiset = {}
    initial_words = []
    start_time = time.time()

    for _ in range(letter_multiset_size):
        e = input().split()
        if e[0] in multiset.keys():
            multiset[e[0]]['count'] += 1
        else:
            multiset[e[0]] = {'count': 1, 'value': int(e[1])}

    for _ in range(initial_size):
        initial_words.append(input().replace('\r', ''))

    trie = read_dict(multiset)
    matcher = WordMatcher(trie)
    genetic = GeneticAlgorithm(matcher)

    preprocessing_time = time.time() - start_time
    result = genetic.search(time_limit - preprocessing_time, initial_words)

    print(result, file=sys.stderr)
    print(matcher.fitness(result))
Example #2
0
def escape():
    """Perform genetic algorithm on a maze from the input data."""
    i = list(map(int, input().split()))
    time_limit = i[0]
    n = i[1]
    m = i[2]
    initial_size = i[3]
    population_size = i[4]

    grid = []
    start_field = -1
    exit_field = -1
    for i in range(n):
        row = list(map(lambda u: Field(int(u)), input().replace('\r', '')))
        if Field.AGENT in row:
            start_field = (i, row.index(Field.AGENT))
        if Field.EXIT in row:
            exit_field = (i, row.index(Field.EXIT))
        grid.append(row)

    maze = Maze(grid, start_field, exit_field)

    initial_paths = []
    for i in range(initial_size):
        initial_paths.append(path_from_string(input().replace('\r', '')))

    genetic = GeneticAlgorithm(maze, population_size)
    unchanged_iterations = 10 * (n * m)
    result = genetic.search(time_limit, initial_paths, unchanged_iterations)

    print(result[1])
    print_path(result[0])