does something
This commit is contained in:
parent
b76e252f3c
commit
521e317a39
1 changed files with 28 additions and 39 deletions
35
genetic.py
35
genetic.py
|
@ -1,12 +1,12 @@
|
|||
import os
|
||||
from typing import Callable
|
||||
|
||||
import frozendict
|
||||
from deap import creator, base, tools, algorithms
|
||||
|
||||
import fuzzer
|
||||
import instrument
|
||||
from fuzzer import get_test_cases, get_test_class
|
||||
import frozendict
|
||||
|
||||
INDMUPROB = 0.05
|
||||
MUPROB = 0.1
|
||||
|
@ -36,37 +36,30 @@ def generate(f_name: str):
|
|||
creator.create("Fitness", base.Fitness, weights=(-1.0,))
|
||||
creator.create("Individual", list, fitness=creator.Fitness)
|
||||
|
||||
n_initial = 10
|
||||
|
||||
args = instrument.functions[f_name]
|
||||
|
||||
toolbox = base.Toolbox()
|
||||
toolbox.register("attr_test_case", get_test_case_generator(to_test, instrument.functions[to_test]))
|
||||
toolbox.register("individual",
|
||||
tools.initRepeat,
|
||||
tools.initIterate,
|
||||
creator.Individual,
|
||||
toolbox.attr_test_case,
|
||||
n=n_initial)
|
||||
lambda: toolbox.attr_test_case())
|
||||
toolbox.register("population",
|
||||
tools.initRepeat,
|
||||
list,
|
||||
toolbox.individual)
|
||||
toolbox.register("evaluate", fitness)
|
||||
toolbox.register("evaluate", compute_fitness)
|
||||
|
||||
def mate(tc1, tc2):
|
||||
t1, t2 = frozendict.frozendict(tc1), frozendict.frozendict(tc2)
|
||||
|
||||
print("ticino", tc1, tc2)
|
||||
o1, o2 = fuzzer.crossover(t1, t2, args)
|
||||
i1, i2 = creator.Individual(o1.items()), creator.Individual(o2.items())
|
||||
print("mate", i1, i2)
|
||||
return i1, i2
|
||||
|
||||
def mutate(tc):
|
||||
t = frozendict.frozendict(tc)
|
||||
o = fuzzer.mutate(t, args)
|
||||
i1 = creator.Individual(o.items())
|
||||
print("mutate", i1)
|
||||
return i1,
|
||||
|
||||
toolbox.register("mate", mate)
|
||||
|
@ -78,8 +71,7 @@ def generate(f_name: str):
|
|||
instrument.archive_true_branches = {}
|
||||
instrument.archive_false_branches = {}
|
||||
population = toolbox.population(n=NPOP)
|
||||
print("population", population)
|
||||
algorithms.eaSimple(population, toolbox, CXPROB, MUPROB, NGEN, verbose=False)
|
||||
algorithms.eaSimple(population, toolbox, CXPROB, MUPROB, NGEN) # , verbose=False)
|
||||
cov = len(instrument.archive_true_branches) + len(instrument.archive_false_branches)
|
||||
print(cov, instrument.archive_true_branches, instrument.archive_false_branches)
|
||||
coverage.append(cov)
|
||||
|
@ -87,24 +79,23 @@ def generate(f_name: str):
|
|||
print(coverage)
|
||||
|
||||
|
||||
def fitness(individuals: list[list]) -> tuple[float]:
|
||||
def compute_fitness(individual: list) -> tuple[float]:
|
||||
x = frozendict.frozendict(individual)
|
||||
range_start, range_end = instrument.n_of_branches[to_test]
|
||||
|
||||
# Reset any distance values from previous executions
|
||||
instrument.distances_true = {}
|
||||
instrument.distances_false = {}
|
||||
|
||||
fitness = 0.0
|
||||
for individual in individuals:
|
||||
x = frozendict.frozendict(individual)
|
||||
|
||||
# Run the function under test
|
||||
try:
|
||||
out = instrument.invoke(to_test, x)
|
||||
except AssertionError:
|
||||
print(to_test, x, "=", "[FAILS]")
|
||||
print(to_test, x, "=", "[FAILS] fitness = 10000")
|
||||
return 10000,
|
||||
|
||||
fitness = 0.0
|
||||
|
||||
# Sum up branch distances
|
||||
for branch in range(range_start, range_end):
|
||||
if branch in instrument.distances_true:
|
||||
|
@ -112,16 +103,14 @@ def fitness(individuals: list[list]) -> tuple[float]:
|
|||
instrument.archive_true_branches[branch] = x
|
||||
if branch not in instrument.archive_true_branches:
|
||||
fitness += normalize(instrument.distances_true[branch])
|
||||
|
||||
for branch in range(range_start, range_end):
|
||||
if branch in instrument.distances_false:
|
||||
if instrument.distances_false[branch] == 0 and branch not in instrument.archive_false_branches:
|
||||
instrument.archive_false_branches[branch] = x
|
||||
if branch not in instrument.archive_false_branches:
|
||||
fitness += normalize(instrument.distances_false[branch])
|
||||
print(to_test, x, "=", out)
|
||||
|
||||
print("fitness", fitness)
|
||||
print()
|
||||
print(to_test, x, "=", out, "fitness =", fitness)
|
||||
return fitness,
|
||||
|
||||
|
||||
|
|
Reference in a new issue