-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.py
55 lines (47 loc) · 1.63 KB
/
sudoku.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# import timeit
# import_module = """
# Necessary imports
from charles.charles_file import Original, Population
from charles.selection import fps, tournament, ranking
from charles.crossover import cycle_co, pmx_co
from charles.mutation import swap_mutation, inversion_mutation
from puzzles import puzzle
import numpy as np
from matplotlib import pyplot as plt
# """
# testcode = """
# Converting the original puzzle (which was a list) to a numpy array
original_puzzle_as_array = np.asarray([puzzle[x:x+9] for x in range(0, len(puzzle), 9)])
# Creating the original puzzle with the class Original
original_puzzle = Original(original_puzzle_as_array)
# We will run until finding a solution for the puzzle
solution_found = 0
# Each time we start a new population from the 0, we save the fitness of the best individual of every generation
fitness_values = []
while solution_found == 0:
pop = Population(
100,
original_puzzle,
"max"
)
solution_found, fitness = pop.evolve(
gens=200,
select=ranking,
crossover=pmx_co,
mutate=inversion_mutation,
co_p=0.90,
mu_p=0.10,
elitism=0.1
)
fitness_values.append(fitness)
fitness_all = []
for sublist in fitness_values:
for fitness_value in sublist:
fitness_all.append(fitness_value)
generations = [generation for generation in range(len(fitness_all))]
plt.plot(generations, fitness_all)
plt.show()
print("Solution found in gen number: " + str(generations[-1]+1))
# """
# times = timeit.repeat(stmt=testcode, setup=import_module, number=1, repeat=1)
# print("Average time:" + str(round(sum(times)/len(times), 2)))