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