-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
68 lines (52 loc) · 2.07 KB
/
main.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
56
57
58
59
60
61
62
63
64
65
66
67
68
import random
import numpy
from deap import base
from deap import creator
from deap import tools
from deap import algorithms
import generated as formula
import plotly.graph_objects as go
chromosome_length = formula.variable_count
population_size = 200
mate_rate = 0.5
mutation_rate = 0.01
n_of_generations = 500
creator.create("FitnessSAT", base.Fitness, weights=(1.0,))
creator.create("Individual", list, fitness=creator.FitnessSAT)
toolbox = base.Toolbox()
toolbox.register("attr_bool", random.randint, 0, 1)
toolbox.register("individual", tools.initRepeat, creator.Individual,
toolbox.attr_bool, chromosome_length)
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
def fitness(chromosome):
clauses_satisfied = 0
for clause in formula.clauses:
if clause(chromosome):
clauses_satisfied += 1
return [clauses_satisfied]
toolbox.register("evaluate", fitness)
toolbox.register("mate", tools.cxTwoPoint)
toolbox.register("mutate", tools.mutFlipBit, indpb=mutation_rate)
toolbox.register("select", tools.selBest)
pop = toolbox.population(n=population_size)
best = tools.HallOfFame(1)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", numpy.mean)
stats.register("std", numpy.std)
stats.register("min", numpy.min)
stats.register("max", numpy.max)
pop, log = algorithms.eaSimple(pop, toolbox, cxpb=mate_rate, mutpb=mutation_rate, ngen=n_of_generations,
stats=stats, halloffame=best, verbose=True)
print("Clause count: " + str(formula.clause_count))
generations = [i['gen'] for i in log]
bests = [i['max'] for i in log]
averages = [i['avg'] for i in log]
fig = go.Figure()
fig.add_trace(go.Scatter(x=tuple(generations),y=tuple(bests),mode='lines+markers',name='Max'))
fig.add_trace(go.Scatter(x=tuple(generations),y=tuple(averages),mode='lines+markers',name='Avg'))
fig.update_layout(
title = "SAT",
xaxis=go.layout.XAxis(title=go.layout.xaxis.Title(text="Generations")),
yaxis=go.layout.YAxis(title=go.layout.yaxis.Title(text="Clauses satisfied")),
)
fig.show()