Example #1
0
def test_recalculate_fitness_rotations():
    rotations = [F, R, U, B, L, D, Fi, Ri, Ui, Bi, Li, Di]
    for rotation in rotations:
        c = Cube()
        rotation(c)
        c.recalculate_fitness()
        assert c.fitness == 52
Example #2
0
def test_recalculate_fitness_rotations():
    rotations = [F, R, U, B, L, D,
                 Fi, Ri, Ui, Bi, Li, Di]
    for rotation in rotations:
        c = Cube()
        rotation(c)
        c.recalculate_fitness()
        assert c.fitness == 52
def test_4_turn():
    rotations = [F, R, U, B, L, D, Fi, Ri, Ui, Bi, Li, Di]
    for rotation in rotations:
        c = Cube()
        for i in range(4):
            rotation(c)
        fresh_one = Cube()
        assert c == fresh_one
Example #4
0
def run(problem=None,
        parents=PARENTS,
        offspring=OFFSPRING,
        use_tournament_selection=False):
    random.seed()
    if problem is None:
        problem = Cube()
        problem.scramble()

    population = [(deepcopy(problem), []) for i in range(parents)]

    selection_fn = tournament_selection if use_tournament_selection else truncation_selection

    # print("Original problem fitness: " + str(problem.fitness))

    generations = 0
    generations_without_improvement = 0
    best_guy = population[0]
    while not solved(best_guy) and generations < MAX_GENERATIONS:

        old_population = deepcopy(population)
        old_best_buy = deepcopy(best_guy)

        # reproduction
        while len(population) < offspring:
            population.extend(deepcopy(old_population))

        # mutation
        if generations_without_improvement >= MAX_ALLOWED_GENERATIONS_WITHOUT_IMPROVEMENT:
            print("Chaining mutations !")
            mutate(population, chain_mutations=True)
            generations_without_improvement = 0
        else:
            mutate(population)

        population.extend(old_population)

        # selection
        population, best_guy = selection_fn(population, parents)

        print(
            "Generation: %d\tPopulation: %s\tFitness: %d\tBest_guy's_rotations_count: %s"
            % (generations, len(population), fitness(best_guy), len(
                best_guy[1])))

        # measure improvement
        if fitness(best_guy) >= fitness(old_best_buy):
            generations_without_improvement += 1
        else:
            generations_without_improvement = 0

        generations += 1

    if generations == MAX_GENERATIONS:
        return None
    else:
        # print("Solved in %d generations" % generations)
        return generations, len(best_guy[1]), calc_compression(best_guy[1])
Example #5
0
def run(problem=None, parents=PARENTS, offspring=OFFSPRING, use_tournament_selection=False):
    random.seed()
    if problem is None:
        problem = Cube()
        problem.scramble()

    population = [(deepcopy(problem), []) for i in range(parents)]

    selection_fn = tournament_selection if use_tournament_selection else truncation_selection

    # print("Original problem fitness: " + str(problem.fitness))

    generations = 0
    generations_without_improvement = 0
    best_guy = population[0]
    while not solved(best_guy) and generations < MAX_GENERATIONS:

        old_population = deepcopy(population)
        old_best_buy = deepcopy(best_guy)

        # reproduction
        while len(population) < offspring:
            population.extend(deepcopy(old_population))

        # mutation
        if generations_without_improvement >= MAX_ALLOWED_GENERATIONS_WITHOUT_IMPROVEMENT:
            print("Chaining mutations !")
            mutate(population, chain_mutations=True)
            generations_without_improvement = 0
        else:
            mutate(population)

        population.extend(old_population)

        # selection
        population, best_guy = selection_fn(population, parents)

        print("Generation: %d\tPopulation: %s\tFitness: %d\tBest_guy's_rotations_count: %s"
              % (generations, len(population), fitness(best_guy), len(best_guy[1])))

        # measure improvement
        if fitness(best_guy) >= fitness(old_best_buy):
            generations_without_improvement += 1
        else:
            generations_without_improvement = 0

        generations += 1

    if generations == MAX_GENERATIONS:
        return None
    else:
        # print("Solved in %d generations" % generations)
        return generations, len(best_guy[1]), calc_compression(best_guy[1])
def _assert_rotate(fresh_visually, expected_visually, rotation_fun):
    def faces_visually_to_faces(faces_visually):
        row0_indices = (0, 9, 12, 15, 18, 45)
        next_row_indices_shift = (3, 12, 12, 12, 12, 3)

        def facelet_value(face_idx, row_idx, col_idx):
            index = row0_indices[face_idx] + next_row_indices_shift[
                face_idx] * row_idx + col_idx
            return faces_visually[index]

        faces = [[[
            facelet_value(face_idx, row_idx, col_idx) for col_idx in range(3)
        ] for row_idx in range(3)] for face_idx in range(6)]

        return faces

    fresh = Cube()
    expected = Cube()
    fresh.faces = faces_visually_to_faces(fresh_visually)
    expected.faces = faces_visually_to_faces(expected_visually)
    rotation_fun(fresh)

    print("actual:\n" + fresh.readable_string())
    print("expected:\n" + expected.readable_string())

    # more useful for debugging than: assert fresh == expected :
    assert fresh.faces == expected.faces
Example #7
0
def main():
    problem = Cube()
    problem.scramble()

    results = init_results_dict()
    for is_tournament in USING_TOURNAMENT_SELECTION:
        for parents in PARENTS:
            for offspring in OFFSPRING:
                times = []
                generations = []
                rotations_count = []
                compression = []
                for i in range(REPETITIONS):
                    start = time.process_time()
                    result = None
                    successful = False
                    while not successful:
                        result = run(problem,
                                     parents,
                                     offspring,
                                     use_tournament_selection=is_tournament)
                        if result is not None:
                            successful = True
                    end = time.process_time()
                    times.append(end - start)
                    generations.append(result[0])
                    rotations_count.append(result[1])
                    compression.append(result[2])
                results[is_tournament][parents][offspring] = (
                    avg(times), avg(generations), avg(rotations_count),
                    avg(compression))
                print(
                    str(parents) + "," + str(offspring) + ": " +
                    str(avg(times)))

    print('#' * 80 + '\n' * 2)
    print(
        "is_tournament parents offspring time[s] generations rotations_count compression\n"
    )
    for is_tournament, v1 in results.items():
        for parents, v2 in v1.items():
            for offspring, v3 in v2.items():
                print("%s\t%d\t%d\t%f\t%f\t%f\t%f" %
                      (str(is_tournament), parents, offspring, v3[0], v3[1],
                       v3[2], v3[3]))
def main():
    problem = Cube()
    problem.scramble()

    results = init_results_dict()
    for is_tournament in USING_TOURNAMENT_SELECTION:
        for parents in PARENTS:
            for offspring in OFFSPRING:
                times = []
                generations = []
                rotations_count = []
                compression = []
                for i in range(REPETITIONS):
                    start = time.process_time()
                    result = None
                    successful = False
                    while not successful:
                        result = run(problem, parents, offspring, use_tournament_selection=is_tournament)
                        if result is not None:
                            successful = True
                    end = time.process_time()
                    times.append(end - start)
                    generations.append(result[0])
                    rotations_count.append(result[1])
                    compression.append(result[2])
                results[is_tournament][parents][offspring] = (
                    avg(times),
                    avg(generations),
                    avg(rotations_count),
                    avg(compression),
                )
                print(str(parents) + "," + str(offspring) + ": " + str(avg(times)))

    print("#" * 80 + "\n" * 2)
    print("is_tournament parents offspring time[s] generations rotations_count compression\n")
    for is_tournament, v1 in results.items():
        for parents, v2 in v1.items():
            for offspring, v3 in v2.items():
                print(
                    "%s\t%d\t%d\t%f\t%f\t%f\t%f" % (str(is_tournament), parents, offspring, v3[0], v3[1], v3[2], v3[3])
                )
def _assert_rotate(fresh_visually, expected_visually, rotation_fun):
    def faces_visually_to_faces(faces_visually):
        row0_indices = (0, 9, 12, 15, 18, 45)
        next_row_indices_shift = (3, 12, 12, 12, 12, 3)

        def facelet_value(face_idx, row_idx, col_idx):
            index = row0_indices[face_idx] + next_row_indices_shift[face_idx] * row_idx + col_idx
            return faces_visually[index]

        faces = [[[facelet_value(face_idx, row_idx, col_idx)
                   for col_idx in range(3)]
                  for row_idx in range(3)]
                 for face_idx in range(6)]

        return faces

    fresh = Cube()
    expected = Cube()
    fresh.faces = faces_visually_to_faces(fresh_visually)
    expected.faces = faces_visually_to_faces(expected_visually)
    rotation_fun(fresh)

    print("actual:\n" + fresh.readable_string())
    print("expected:\n" + expected.readable_string())

    # more useful for debugging than: assert fresh == expected :
    assert fresh.faces == expected.faces
Example #10
0
def test_recalculate_fitness_fresh_cube():
    c = Cube()
    c.recalculate_fitness()

    assert c.fitness == 0
Example #11
0
def test_recalculate_fitness_fresh_cube():
    c = Cube()
    c.recalculate_fitness()

    assert c.fitness == 0
 def cube_factory():
     cube = Cube()
     cube.scramble()
     return cube
Example #13
0
 def cube_factory():
     cube = Cube()
     cube.scramble()
     return cube
Example #14
0
def test_each_mutation_changes_fitness_only_slightly_solved_cube():
    cube_factory = lambda: Cube()
    _each_mutation_changes_fitness_only_slightly(cube_factory)