def run_SPEA2(SIR_name, version, test_size): evaluator = evaluation.VEval(SIR_name, version, test_size) dim = test_size population = GA.initial_genes(dim, GLOB.POP) archive = [] population_history = [population] archive_history = [archive] if GLOB.DEBUG: print('0', len(population)) for i in range(GLOB.MAX_IT): #update union = population + archive GA.evaluate(union, evaluator) get_fitness(union) archive = select(union) population = GA.crossover(archive)[GLOB.POP + 1:] if GLOB.DEBUG: print(i, len(population)) #save history population_history.append(population) archive_history.append(archive) first_pareto = get_first_pareto(archive) #final result return first_pareto
def new_crossover(CA, DA, cr, mr, dim): ratio = GLOB.CA_DA_RATIO new_pop = [] if len(CA + DA) < 2: new_pop += GA.initial_genes(dim, GLOB.POP) return new_pop elif len(CA) < 2 or len(DA) < 2: new_pop += GA.crossover(CA + DA, mr=mr) return new_pop for i in range(GLOB.POP): child = None # consider crossover rate if random.random() < cr: p1, p2 = None, None # randomly choose parents between CA and DA rand_num = random.random() if rand_num < ratio * ratio: p1, p2 = random.sample(CA, 2) elif rand_num < 2 * ratio - ratio * ratio: if random.random() < 0.5: p1 = random.sample(CA, 1)[0] p2 = random.sample(DA, 1)[0] else: p1 = random.sample(DA, 1)[0] p2 = random.sample(CA, 1)[0] else: p1, p2 = random.sample(DA, 2) child = GA.PMX(p1, p2) else: seq = random.sample(CA + DA, 1)[0].get_seq().copy() child = GA.Gene_Info(seq) # mutation if random.random() < mr: seq = child.get_seq() idx1, idx2 = random.sample(list(seq), 2) seq[idx2], seq[idx1] = seq[idx1], seq[idx2] #check overlap overlapped = False for parent in CA + DA: if (child.get_seq() == parent.get_seq()).all(): overlapped = True break if overlapped: # if overlap, do not add child at new_pop continue new_pop.append(child) return new_pop #run_TAEA('sed',1,360)
def run_TAEA(input_fname): modify = modifier.modify_testcase(input_fname) dim = modify.get_datasize() population = GA.initial_genes(dim, GLOB.POP) population_history = [population] CA, DA = [], [] #covergence archive, diversity archive GA.evaluate(population,modify) for i in range(GLOB.MAX_IT): collect_non_dominated(population,CA,DA) new_pop = GA.crossover(CA+DA) GA.evaluate(new_pop,modify)
def run_NSGA3(input_fname): modify = modifier.modify_testcase(input_fname) dim = modify.get_datasize() reference = get_ref() population = GA.initial_genes(dim,GLOB.POP) population_history = [population] for i in range(GLOB.MAX_IT): new_pop = GA.crossover(population) GA.evaluate(new_pop,modify) pareto = GA.get_pareto(new_pop) new_pop = select(pareto) population = new_pop population_history.append(population) first_pareto = get_first_pareto(population) #final result
def run_NSGA2(SIR_name, version, test_size): evaluator = evaluation.VEval(SIR_name, version, test_size) dim = test_size population = GA.initial_genes(dim, GLOB.POP, version) if GLOB.DEBUG: print('0', len(population)) population_history = [population] for i in range(GLOB.MAX_IT): new_pop = GA.crossover(population, mr=1 / dim) GA.evaluate(new_pop, evaluator) pareto = GA.get_pareto(new_pop) new_pop = select(pareto) population = new_pop population_history.append(population) if GLOB.DEBUG: print(i, len(population)) first_pareto = get_first_pareto(population) #final result return first_pareto