def main(): # The cma module uses the numpy random number generator # numpy.random.seed(128) MU, LAMBDA = 10, 10 NGEN = 500 verbose = True create_plot = False # The MO-CMA-ES algorithm takes a full population as argument population = [creator.Individual(x) for x in (numpy.random.uniform(0, 1, (MU, N)))] for ind in population: ind.fitness.values = toolbox.evaluate(ind) strategy = cma.StrategyMultiObjective(population, sigma=1.0, mu=MU, lambda_=LAMBDA) toolbox.register("generate", strategy.generate, creator.Individual) toolbox.register("update", strategy.update) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("min", numpy.min, axis=0) stats.register("max", numpy.max, axis=0) logbook = tools.Logbook() logbook.header = ["gen", "nevals"] + (stats.fields if stats else []) fitness_history = [] for gen in range(NGEN): # Generate a new population population = toolbox.generate() # Evaluate the individuals fitnesses = toolbox.map(toolbox.evaluate, population) for ind, fit in zip(population, fitnesses): ind.fitness.values = fit fitness_history.append(fit) # Update the strategy with the evaluated individuals toolbox.update(population) record = stats.compile(population) if stats is not None else {} logbook.record(gen=gen, nevals=len(population), **record) if verbose: print(logbook.stream) if verbose: print("Final population hypervolume is %f" % hypervolume(strategy.parents, [11.0, 11.0])) # Note that we use a penalty to guide the search to feasible solutions, # but there is no guarantee that individuals are valid. # We expect the best individuals will be within bounds or very close. num_valid = 0 for ind in strategy.parents: dist = distance(closest_feasible(ind), ind) if numpy.isclose(dist, 0.0, rtol=1.e-5, atol=1.e-5): num_valid += 1 print("Number of valid individuals is %d/%d" % (num_valid, len(strategy.parents))) print("Final population:") print(numpy.asarray(strategy.parents)) if create_plot: interactive = 0 if not interactive: import matplotlib as mpl_tmp mpl_tmp.use('Agg') # Force matplotlib to not use any Xwindows backend. import matplotlib.pyplot as plt fig = plt.figure() plt.title("Multi-objective minimization via MO-CMA-ES") plt.xlabel("First objective (function) to minimize") plt.ylabel("Second objective (function) to minimize") # Limit the scale because our history values include the penalty. plt.xlim((-0.1, 1.20)) plt.ylim((-0.1, 1.20)) # Plot all history. Note the values include the penalty. fitness_history = numpy.asarray(fitness_history) plt.scatter(fitness_history[:,0], fitness_history[:,1], facecolors='none', edgecolors="lightblue") valid_front = numpy.array([ind.fitness.values for ind in strategy.parents if close_valid(ind)]) invalid_front = numpy.array([ind.fitness.values for ind in strategy.parents if not close_valid(ind)]) if len(valid_front) > 0: plt.scatter(valid_front[:,0], valid_front[:,1], c="g") if len(invalid_front) > 0: plt.scatter(invalid_front[:,0], invalid_front[:,1], c="r") if interactive: plt.show() else: print("Writing cma_mo.png") plt.savefig("cma_mo.png") return strategy.parents
def main(): # The cma module uses the numpy random number generator # numpy.random.seed(128) MU, LAMBDA = 10, 10 NGEN = 500 verbose = True # The MO-CMA-ES algorithm takes a full population as argument population = [ creator.Individual(x) for x in (numpy.random.uniform(0, 1, (MU, N))) ] for ind in population: ind.fitness.values = toolbox.evaluate(ind) strategy = cma.StrategyMultiObjective(population, sigma=1.0, mu=MU, lambda_=LAMBDA) toolbox.register("generate", strategy.generate, creator.Individual) toolbox.register("update", strategy.update) stats = tools.Statistics(lambda ind: ind.fitness.values) stats.register("min", numpy.min, axis=0) stats.register("max", numpy.max, axis=0) logbook = tools.Logbook() logbook.header = ["gen", "nevals"] + (stats.fields if stats else []) for gen in range(NGEN): # Generate a new population population = toolbox.generate() # Evaluate the individuals fitnesses = toolbox.map(toolbox.evaluate, population) for ind, fit in zip(population, fitnesses): ind.fitness.values = fit # Update the strategy with the evaluated individuals toolbox.update(population) record = stats.compile(population) if stats is not None else {} logbook.record(gen=gen, nevals=len(population), **record) if verbose: print(logbook.stream) if verbose: print("Final population hypervolume is %f" % hypervolume(strategy.parents, [11.0, 11.0])) # import matplotlib.pyplot as plt # valid_front = numpy.array([ind.fitness.values for ind in strategy.parents if valid(ind)]) # invalid_front = numpy.array([ind.fitness.values for ind in strategy.parents if not valid(ind)]) # fig = plt.figure() # if len(valid_front) > 0: # plt.scatter(valid_front[:,0], valid_front[:,1], c="g") # if len(invalid_front) > 0: # plt.scatter(invalid_front[:,0], invalid_front[:,1], c="r") # plt.show() return strategy.parents
def clone_model(model): new_model = creator.Individual(model.lr) new_model.model.set_weights(model.model.get_weights()) return new_model
def evaluateIndividuals(individuals, gen): results_inds = [] pending_inds = [] delayed_inds = [] pending_cache = set() cache_hit = 0 cache_miss = 0 pending_cache_hit = 0 for ind in individuals: if not hasattr(ind, 'archIndex'): ind.archIndex = archIndex_gen() lastArchIndex = ind.archIndex with lock: if representation.normalize(tuple(ind)) in cache: # cache hit ind.fitness.values = cache[representation.normalize(tuple(ind))] results_inds.append(ind) cache_hit += 1 elif representation.normalize(tuple(ind)) in pending_cache: # pending cache hit # this ind has already been queued for evaluation # don't duplicate the work, we wait for the result to come back and use it delayed_inds.append(ind) pending_cache_hit += 1 else: # cache miss cache_miss += 1 pending_inds.append(ind) pending_cache.add(representation.normalize(tuple(ind))) request = {'individual':ind, 'archIndex':ind.archIndex, 'gen': gen, 'worker_args':worker_args} corr_id = str(uuid.uuid4()) channel.basic_publish(exchange='', routing_key=request_queue, properties=pika.BasicProperties( reply_to = callback_queue, correlation_id = corr_id, content_type = 'application/json', ), body=json.dumps(request)) pbar = tqdm(total = len(pending_inds)) while len(results_inds) < len(individuals): method_frame, header_frame, body = channel.basic_get(queue = callback_queue) if not method_frame: time.sleep(1) continue else: #print(method_frame, header_frame, body) response = parse(body) # handle response here #print('response =', response) ind = creator.Individual(response['individual']) ind.archIndex = response['archIndex'] # update cache with lock: if representation.normalize(tuple(ind)) not in cache: # update cache cache[representation.normalize(tuple(ind))] = response['fitness'] ind.fitness.values = cache[representation.normalize(tuple(ind))] results_inds.append(ind) # now that response is handle we can safely acknowledge channel.basic_ack(delivery_tag=method_frame.delivery_tag) pbar.update(1) # check if we can process some delayed_inds with lock: for delayed_ind in delayed_inds: if representation.normalize(tuple(delayed_ind)) in cache: # we finally got an evaluation for this delayed_ind, add it to the final result delayed_ind.fitness.values = cache[representation.normalize(tuple(delayed_ind))] results_inds.append(delayed_ind) # update list of delayed_inds, to keep only the one that did not get a result yet delayed_inds = [delayed_ind for delayed_ind in delayed_inds if not representation.normalize(tuple(delayed_ind)) in cache] if len(results_inds) != len(individuals): raise ValueError('missing responses') assert len(delayed_inds) == 0, 'some delayed_inds did not get a results. Something went wrong...' assert len(individuals) == cache_hit+cache_miss+pending_cache_hit, 'Wrong classification of cache hit/miss. Something is wrong...' print('') for ind in results_inds: print(individual_to_str(ind)) metrics = Metrics(cache_hit, pending_cache_hit, cache_miss) printMetrics(metrics) return results_inds, lastArchIndex, metrics
def create_gene(): gene = np.random.randint(0, 255, (K, 3)) return creator.Individual(gene)
def shuffleGen(priority): priority_new = priority[:] random.shuffle(priority_new) return creator.Individual(priority_new)
def get_individual(ind): return creator.Individual(ind)
def newIndividual(num_input, max_num_hidden): agent = Agente(num_input, max_num_hidden).get_random_agent() #print(agent) agent = creator.Individual(agent) return agent
def flatlist(li): flat_list = list(chain.from_iterable(li)) flat_list = creator.Individual(flat_list) return flat_list
def evolve(sample_num, config): #random.seed(64) toolbox = getToolBox(config) start = time.time() problem=config["PROBLEM"] direccion=config["DIRECCION"] n_corr=config["n_corr"] n_prob=config["n_problem"] #server = evospace.Population("pop") server = jsonrpclib.Server(config["SERVER"]) #evospace_sample = server.get_sample(config["SAMPLE_SIZE"]) evospace_sample = server.getSample(config["SAMPLE_SIZE"]) print 'hey' #evospace_specie= server.getSample_specie(config["set_specie"]) pop = [creator.Individual(neat_gp.PrimitiveTree.from_string(cs['chromosome'], pset)) for cs in evospace_sample['sample']] cxpb = config["CXPB"]#0.7 # 0.9 mutpb = config["MUTPB"]#0.3 # 0.1 ngen = config["WORKER_GENERATIONS"]#50000 params = config["PARAMS"] neat_cx = config["neat_cx"] neat_alg = config["neat_alg"] neat_pelit = config["neat_pelit"] neat_h = config["neat_h"] funcEval.LS_flag = config["LS_FLAG"] LS_select = config["LS_SELECT"] funcEval.cont_evalp = 0 num_salto = config["num_salto"] cont_evalf = config["cont_evalf"] SaveMatrix = config["save_matrix"] GenMatrix = config["gen_matrix"] data_(n_corr, n_prob, toolbox) begin =time.time() print "inicio del proceso" if neat_alg: num_Specie, specie_list = neatGPLS_evospace.evo_species(pop, neat_h) for specie in specie_list: pop_gpo=getInd_perSpecie(specie, pop) pop, log = neatGPLS.neat_GP_LS(pop_gpo, toolbox, cxpb, mutpb, ngen, neat_alg, neat_cx, neat_h, neat_pelit, funcEval.LS_flag, LS_select, cont_evalf, num_salto, SaveMatrix, GenMatrix, pset, n_corr, n_prob, params, direccion, problem, stats=None, halloffame=None, verbose=True) else: pop, log = neatGPLS.neat_GP_LS(pop, toolbox, cxpb, mutpb, ngen, neat_alg, neat_cx, neat_h, neat_pelit, funcEval.LS_flag, LS_select, cont_evalf, num_salto, SaveMatrix, GenMatrix, pset, n_corr, n_prob, params, direccion, problem, stats=None, halloffame=None, verbose=True) putback = time.time() # sample = [{"chromosome":str(ind),"id":None, "fitness":{"DefaultContext":[ind.fitness.values[0].item() if isinstance(ind.fitness.values[0], np.float64) else ind.fitness.values[0]]}, "params":[x for x in ind.get_params()]if funcEval.LS_flag else [0.0] } for ind in pop] #print sample evospace_sample = {'sample_id': 'None', 'sample': sample} #evospace_sample['sample'] = sample #server.put_sample(evospace_sample) server.putZample(evospace_sample) best_ind = tools.selBest(pop, 1)[0] # best = [len(best_ind), sample_num, round(time.time() - start, 2), round(begin - start, 2), round(putback - begin, 2), round(time.time() - putback, 2), best_ind] return best
def gene_updater(self, gene, rewards): self.gene = [creator.Individual(i) for i in gene] # CXPB is the probability with which two individuals # are crossed # # MUTPB is the probability for mutating an individual # # NEWINDPB is the probability for an new individual # NGEN is the number of generations for which the # evolution runs CXPB, MUTPB, NEWINDPB, NGEN = 0.7, 0.5, 0.05, 1 # Evaluate the entire population fitnesses = list(map(self.toolbox.evaluate, rewards)) for ind, fit in zip(self.gene, fitnesses): ind.fitness.values = fit # Begin the evolution for g in range(NGEN): # Select the next generation individuals offspring = self.toolbox.select(self.gene, len(self.gene)) # Clone the selected individuals offspring = list(map(self.toolbox.clone, offspring)) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring[::2], offspring[1::2]): # cross two individuals with probability CXPB if random.random() < CXPB: self.toolbox.mate(child1, child2) # fitness values of the children # must be recalculated later del child1.fitness.values del child2.fitness.values for mutant in offspring: # mutate an individual with probability MUTPB if random.random() < MUTPB: self.toolbox.mutate(mutant) del mutant.fitness.values for ind in range(len(offspring)): # new individual with probability NEWINDPB if random.random() < NEWINDPB: offspring[ind] = creator.Individual(self.gene_generator(1))[0] del offspring[ind].fitness.values #print offspring[ind] # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = map(self.toolbox.evaluate, rewards) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # The population is entirely replaced by the offspring self.gene[:] = offspring return self.gene
repeat=repeat_mutation) toolbox.register("select", tools.selTournament, tournsize=tournament_size) toolbox.register("select_worst", tools.selWorst) toolbox.register("decode", decode, indices=indices, init_state=starting_points, r=all_charging_ops) # %% the algorithm tInitGA = time.time() # Population TODO create function pop = [] for i in range(0, n_individuals): pop.append(creator.Individual(toolbox.individual())) # Evaluate the entire population # fitnesses = list(map(toolbox.evaluate, pop)) for ind in pop: fit, feasible = toolbox.evaluate(ind) ind.fitness.values = (fit, ) ind.feasible = feasible print(" Evaluated %i individuals" % len(pop)) # Extracting all the fitnesses of fits = [ind.fitness.values for ind in pop] # Variable keeping track of the number of generations
creator.create("FitnessMin", base.Fitness, weights=[-1.0] * 3) # TODO set "4"? creator.create("Individual", str, fitness=creator.FitnessMin) for model in models: union_candidates = [] for f in folders: for i in fetch_all_files(f, model): with open(i, 'r') as records: content = records.readlines() content = map(lambda l: l.strip('\n'), content) for l in content: if l.startswith('T') or l.startswith('~~~') or l.startswith( 'G'): continue e = l.split(' ') e = [float(i) for i in e] ind = creator.Individual(str(e)) ind.fitness = creator.FitnessMin(e) union_candidates.append(ind) # pdb.set_trace() frontier = _get_frontier(union_candidates) # write out the frontier with open( os.path.dirname(os.path.abspath(__file__)) + '/' + model + '.txt', 'w') as f: for front in frontier: f.write(' '.join(map(str, front.fitness.values))) f.write('\n')
def init_individual(): return creator.Individual( Utils.get_instance().init_individual_new_method())
feasible_ind = numpy.array(individual) feasible_ind = numpy.maximum(MIN_BOUND, feasible_ind) feasible_ind = numpy.minimum(MAX_BOUND, feasible_ind) return feasible_ind def valid(individual): """Determines if the individual is valid or not.""" if any(individual < MIN_BOUND) or any(individual > MAX_BOUND): return False return True toolbox = base.Toolbox() toolbox.register("evaluate", benchmarks.zdt2) toolbox.decorate( "evaluate", ClosestValidPenality(valid, closest_feasible, 1.0e-6, distance)) ind1 = creator.Individual( (-5.6468535666e-01, 2.2483050478e+00, -1.1087909644e+00, -1.2710112861e-01, 1.1682438733e+00, -1.3642007438e+00, -2.1916417835e-01, -5.9137308999e-01, -1.0870160336e+00, 6.0515070232e-01, 2.1532075914e+00, -2.6164718271e-01, 1.5244071578e+00, -1.0324305612e+00, 1.2858152343e+00, -1.2584683962e+00, 1.2054392372e+00, -1.7429571973e+00, -1.3517256013e-01, -2.6493429355e+00, -1.3051320798e-01, 2.2641961090e+00, -2.5027232340e+00, -1.2844874148e+00, 1.9955852925e+00, -1.2942218834e+00, 3.1340109155e+00, 1.6440111097e+00, -1.7750105857e+00, 7.7610242710e-01)) print(toolbox.evaluate(ind1)) print("Individuals is valid: %s" % ("True" if valid(ind1) else "False"))
def param_vectors_to_deap_population(param_vectors): population = [] for param_vector in param_vectors: ind = creator.Individual(param_vector) population.append(ind) return population
def main(): toolbox, creator = init() evl = Evaluate() stat_log = StatLog('results/temp/') pop = toolbox.population(n=POP_SIZE) default_config_list = [ 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1 ] pop[0] = creator.Individual(tuple(default_config_list)) count = 0 for ind in pop: print str(count) + ' : ' + ','.join(map(str, ind)) sys.stdout.flush() print len(pop) sys.stdout.flush() invalid_ind = [ind for ind in pop if not ind.fitness.valid] fitnesses = evl.eval_pop(invalid_ind) #map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit sys.stdout.flush() fits = [ind.fitness.values[0] for ind in pop] # This is just to assign the crowding distance to the individuals # no actual selection is done pop = toolbox.select(pop, POP_SIZE) print 'selected pop size' + str(len(pop)) sys.stdout.flush() pop_bef = [toolbox.clone(ind) for ind in pop] # Variable keeping track of the number of generations stat_log.record_gen(pop) stop_rule = StopRule() sys.stdout.flush() g = 0 # Begin the evolution while g < 20: #max(fits) < 100 and g < 10000: # A new generation g = g + 1 print("-- Generation %i --" % g) # Select the next generation individuals print 'TournamentDCD' + str(len(pop)) sys.stdout.flush() offspring = tools.selTournamentDCD(pop, len(pop)) # Clone the selected individuals offspring = [toolbox.clone(ind) for ind in offspring] # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring[::2], offspring[1::2]): # if random.random() < CXPB: toolbox.mate(child1, child2) if random.random() < MUTPB: toolbox.mutate(child1) if random.random() < MUTPB: toolbox.mutate(child2) del child1.fitness.values del child2.fitness.values for ind in offspring: print 'offspring' + ' : ' + ','.join(map(str, ind)) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = evl.eval_pop(invalid_ind) print '## ' + str(len(fitnesses)) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit pop = toolbox.select(pop + offspring, POP_SIZE) stat_log.record_gen(pop) if not stop_rule.stop_criteria_all(pop_bef, pop): pop_bef = [toolbox.clone(ind) for ind in pop] else: break sys.stdout.flush()
def evolve(sample_num, config, toolbox, pset, evospace_sample): start = time.time() problem = config["problem"] direccion = "./data_corridas/%s/train_%d_%d.txt" n_corr = sample_num # config["n_corr"] n_prob = config["n_problem"] name_database = config["db_name"] print config["set_specie"] server = jsonrpclib.Server(config["server"]) # evospace_sample = server.getSample(config["population_size"]) pop = [] for cs in evospace_sample['sample']: i = creator.Individual( neat_gp.PrimitiveTree.from_string(cs['chromosome'], pset)) if isinstance(cs['params'], list): i.params_set(np.asarray(cs['params'])) elif isinstance(cs['params'], unicode): i.params_set( np.asarray([ float(elem) for elem in cs['params'].strip('f[]').split(',') ])) i.specie(int(cs['specie'])) pop.append(i) if len(pop) < 2: a = server.getSpecie() aux_specie = random.choice(a) print("check specie - 2 - %d" % aux_specie) evospace_sample2 = check_specie_aux(server, aux_specie) if evospace_sample2 == []: while evospace_sample2 == []: aux_specie = random.choice(a) evospace_sample2 = check_specie_aux(server, aux_specie) for cs in evospace_sample2['sample']: i = creator.Individual( neat_gp.PrimitiveTree.from_string(cs['chromosome'], pset)) if isinstance(cs['params'], list): i.params_set(np.asarray(cs['params'])) elif isinstance(cs['params'], unicode): i.params_set( np.asarray([ float(elem) for elem in cs['params'].strip('[]').split(',') ])) i.specie(int(cs['specie'])) pop.append(i) data_specie = {'id': aux_specie, 'b_key': 'True'} server.setSpecieFree(data_specie) #Tomar una especie aleatoria print 'Especie: %d --- Numero de individuos: %d' % (len(pop), config["set_specie"]) cxpb = config["cxpb"] mutpb = config["mutpb"] ngen = config["worker_generations"] params = config["params"] neat_cx = config["neat_cx"] neat_alg = config["neat_alg"] neat_pelit = config["neat_pelit"] neat_h = config["neat_h"] funcEval.LS_flag = config["ls_flag"] LS_select = config["ls_select"] funcEval.cont_evalp = 0 num_salto = config["num_salto"] cont_evalf = config["cont_evalf"] SaveMatrix = config["save_matrix"] GenMatrix = config["gen_matrix"] version = 3 testing = True data_(n_corr, n_prob, problem, name_database, toolbox, config) begin = time.time() print "inicio del proceso" pop, log, funcEval_ = neatGPLS.neat_GP_LS(pop, toolbox, cxpb, mutpb, ngen, neat_alg, neat_cx, neat_h, neat_pelit, funcEval.LS_flag, LS_select, cont_evalf, num_salto, SaveMatrix, GenMatrix, pset, n_corr, n_prob, params, direccion, problem, testing, version=version, benchmark_flag=False, beta=0.5, random_speciation=True, set_specie=config["set_specie"], stats=None, halloffame=None, verbose=True) putback = time.time() d_intraspecie = intracluster(pop) d_intracluster = server.getIntraSpecie(config["set_specie"]) resp_flag = 0 id_ = "specie:%s" % config["set_specie"] print 'calculation intra-cluster' if d_intraspecie > (1.5 * float(d_intracluster)): specielist = { 'id': id_, 'specie': config["set_specie"], 'intra_distance': str(d_intracluster), 'flag_speciation': 'True', 'sp_event': 'False' } server.putSpecie(specielist) resp_flag = 1 sample = [{ "specie": str(config["set_specie"]), "chromosome": str(ind), "id": None, "fitness": { "DefaultContext": [ ind.fitness.values[0].item() if isinstance( ind.fitness.values[0], np.float64) else ind.fitness.values[0] ] }, "params": str([x for x in ind.get_params()]) if funcEval.LS_flag else None } for ind in pop] evospace_sample = { 'sample_id': 'None', 'sample_specie': str(config["set_specie"]), 'sample': sample } server.putZample(evospace_sample) d = './Data/%s/dintra_%d_%s.txt' % (problem, n_prob, config["set_specie"]) neatGPLS.ensure_dir(d) dintr = open(d, 'a') dintr.write('\n%s;%s;%s;%s' % (n_corr, d_intraspecie, d_intracluster, resp_flag)) best_ind = tools.selBest(pop, 1)[0] best = [ len(best_ind), sample_num, round(time.time() - start, 2), round(begin - start, 2), round(putback - begin, 2), round(time.time() - putback, 2), best_ind ], len(pop), resp_flag, funcEval, d_intracluster return best
def GAPoolingHeuristic(case_id, failure_rates, service_rates, holding_costs, penalty_cost, skill_cost, machine_cost, numSKUs, minCluster, maxCluster): # 1 is for maximization -1 for minimization # Minimize total cost creator.create("FitnessMax", base.Fitness, weights=(-1.0, )) creator.create("Individual", list, fitness=creator.FitnessMax) def generateIndividual(numSKUs, minCluster, maxCluster): # Generating initial indvidual that are in the range of given max-min cluster numbers individual = [0] * numSKUs randomSKUsindex = np.random.choice(range(numSKUs), minCluster, replace=False) cluster_randomSKUs = np.random.choice(range(1, maxCluster + 1), minCluster, replace=False) for i in range(minCluster): individual[randomSKUsindex[i]] = cluster_randomSKUs[i] for i in range(numSKUs): if individual[i] == 0: individual[i] = random.randint(1, maxCluster) # print type (creator.Individual(individual)) return creator.Individual(individual) toolbox = base.Toolbox() # Attribute generator # define 'attr_bool' to be an attribute ('gene') # which corresponds to integers sampled uniformly # from the range [1,number of SKUs] (i.e. 0 or 1 with equal # probability) # Structure initializers # define 'individual' to be an individual # consisting of #number of maximum cluster =#of SKUs 'attr_bool' elements ('genes') toolbox.register("individual", generateIndividual, numSKUs, minCluster, maxCluster) # define the population to be a list of individuals toolbox.register("population", tools.initRepeat, list, toolbox.individual) # the goal ('fitness') function to be maximized # for objective function call pooling optimizer !!! # what values need for optimizer !!! # def evalOneMax(individual): # return sum(individual), # ---------- # Operator registration # ---------- # register the goal / fitness function toolbox.register("evaluate", evalOneMax, failure_rates, service_rates, holding_costs, penalty_cost, skill_cost, machine_cost) # register the crossover operator toolbox.register("mate", tools.cxTwoPoint) # register a mutation operator with a probability to # flip each attribute/gene of 0.05 # toolbox.register("mutate", swicthtoOtherMutation, indpb=0.4) # toolbox.register("mutate", swicthtoOtherMutation) # operator for selecting individuals for breeding the next # generation: each individual of the current generation # is replaced by the 'fittest' (best) of three individuals # drawn randomly from the current generation. toolbox.register("select", tools.selTournament, tournsize=10) random.seed(64) # create an initial population of 100 individuals (where # each individual is a list of integers) # print("Start of evolution") pop = toolbox.population(n=1) # helper for findT0 def x_bar(T, tcs): tot_next = 0.0 tot_prev = 0.0 nsamples = len(tcs) / 2 for i in range(nsamples): tot_next = tot_next + math.exp(-tcs[i + nsamples] / T) tot_prev = tot_prev + math.exp(-tcs[i] / T) return tot_next / tot_prev def findT0(Tn, tc, X0, eps, p): x_bar_val = x_bar(Tn, tc) while math.fabs(x_bar_val - X0) > eps: Tn = Tn * ((math.log(x_bar_val) / math.log(X0))**(1.0 / p)) x_bar_val = x_bar(Tn, tc) # print("diff:{0}, eps:{1}".format(math.fabs(x_bar_val - X0), eps)) return Tn def findT1(tc, nsamples, X0): tot = 0.0 for i in range(nsamples): tot = tot + tc[i + nsamples] - tc[i] return -tot / (nsamples * math.log(X0)) """ Finds best candidates. :param T0: Initial Tempreture :param Tf: Final Temprature. :param I_sa_inner: Max num of non-improved solution in inside loop. :param I_sa_main: Max num of non-improved solution in main loop. :returns: A list of selected individuals. """ _, case_no = case_id.split(':') case_no = int(case_no) def MSSA(T0, Tf=10, I_sa_inner=12, I_sa_main=12.0, Psize=5): S_list = n_best_individuals[case_no - 1] print("Best {0} individuals of KMedian are: {1}".format(Psize, S_list)) # get their fitness(obj) values fitness_vals = list(map(toolbox.evaluate, S_list)) # find best total cost TC_S_best = min(fitness_vals) # find best individual index best_idx = fitness_vals.index(TC_S_best) T = T0 S = S_list[best_idx] S_best = S NT_main = 1 # corresponds to N in paper iter_idx = 0 # logging purposes # TRICK # set all other solutions to the best solution S_list = [S_best] * Psize # print("Initial Best Cost:{0} Best Individual{1}".format(TC_S_best,S_best)) (a, f) = (3, 0.25) #nonlinear cooling parameters # start_time = time.time() # remember when we started while NT_main <= I_sa_main and T >= Tf: # traverse each individual # traverse each individual # start the search operations and mark each future with individuals array index with cf.ProcessPoolExecutor(max_workers=5) as executor: future_to_k = { executor.submit(check_neighbor_solutions, S_list[k], T, TC_S_best, I_sa_inner, toolbox.evaluate, k, minCluster, maxCluster): k for k in range(Psize) } # executor.shutdown() # future result is (S,TC_Cost) pair results = [] for future in cf.as_completed(future_to_k): results.append(future.result()) # print("Case:{0} Results returned:{1}".format(case_no, len(results))) # sort the results by Total cost sorted_results = sorted(results, key=lambda x: x[1]) best_result = sorted_results[0] if best_result[1] < TC_S_best: S_best = best_result[0] TC_S_best = best_result[1] print("New Best! T:{0} TC_Best:{1} Ind:{2}".format( T, TC_S_best, S_best)) NT_main = 1 # set all other solutions to the best solution S_list = [S_best] * Psize else: NT_main += 1 # use a nonlinear cooling method P = math.log(math.log(T0 / Tf) / math.log(a)) Q = math.log(1.0 / f) b = P / Q T = T0 * pow(a, -pow(NT_main / (f * I_sa_main), b)) iter_idx += 1 print("Iter:{0} Cooling... T={1} TC_Best:{2}".format( iter_idx, T, TC_S_best)) return (S_best, TC_S_best) # num_samples_arr = [20, 100, 500] num_samples_arr = [40] # X0_arr = [0.9, 0.8, 0.5] X0_arr = [0.9] eps = 0.01 p = 1.0 for num_samples in num_samples_arr: pop0 = toolbox.population(n=num_samples * 2) print("Finding Neighbors to find T0 for Case:{0}".format(case_no)) with cf.ProcessPoolExecutor(max_workers=5) as exctr: future_to_i = { exctr.submit(getHigherEnergyValuedNeighbors, pop0, toolbox.evaluate, i, minCluster, maxCluster): i for i in range(num_samples) } # future result is (i,neighbori) pair for ftr in cf.as_completed(future_to_i): i, negh_i = ftr.result() pop0[i + num_samples] = creator.Individual(negh_i) # Evaluate the entire population TCs = list(map(toolbox.evaluate, pop0)) for ind, tc in zip(pop0, TCs): ind.fitness.values = tc # Extracting all the fitnesses of tc = [ind.fitness.values[0] for ind in pop0] for x in range(num_samples): if (tc[x] > tc[x + num_samples]): print("Neighbor Solution Fails for T0") print("Finding T0") for X0 in X0_arr: T1 = findT1(tc, num_samples, X0) T0 = findT0(T1, tc, X0, eps, p) print("Running MSSA") S_best, TC_S_best = MSSA(T0=T0, Psize=5) print("Best Solution:{0} Best Total Cost:{1}".format( S_best, TC_S_best)) pop[0] = creator.Individual(S_best) TCs = list(map(toolbox.evaluate, pop)) for ind, tc in zip(pop, TCs): ind.fitness.values = tc best_ind = tools.selBest(pop, 1)[0] # print("Best individual is %s, %s" % (individual2cluster(best_ind), best_ind.fitness.values)) return best_ind.fitness.values, best_ind
def generate_individual(): ind = random.sample(range(0, settings.SEQUENCE_LENGTH_MAX), settings.SEQUENCE_LENGTH_MAX) return creator.Individual(ind)
def process(self, backdoor: Backdoor) -> List[Individual]: self.output.st_timer(self.name, 'algorithm') timestamp = now() front, points = tools.ParetoFront(), [] creator.create("Fitness", base.Fitness, weights=self.weights) creator.create("Individual", Individual, fitness=creator.Fitness) self.log_info().log_delim() self.limit.set('stagnation', 0) self.output.st_timer('Evolution_init', 'init') root = creator.Individual(backdoor) count = self.sampling.get_size(backdoor) self.log_it_header(0, 'base').log_delim() estimation = self.predict(backdoor, count) best = root.set(estimation.value) root.fitness.values = (estimation.value, estimation.value_sd()) self.log_delim() self.output.ed_timer('Evolution_init') population = [root] pop = self.strategy.breed(population) offspring = [creator.Individual(ind.backdoor) for ind in pop] self.limit.set('iteration', 1) self.limit.set('time', now() - timestamp) while not self.limit.exhausted(): it = self.limit.get('iteration') self.log_it_header(it).log_delim() self.output.st_timer('Evolution_iteration_%d' % it, 'iteration') self.output.st_timer('Evolution_evaluate', 'evaluate') for individual in offspring: backdoor = individual.backdoor count = self.sampling.get_size(backdoor) estimation = self.predict(backdoor, count) if not estimation.from_cache: self.limit.increase('predictions') individual.set(estimation.value) individual.fitness.values = (estimation.value, estimation.value_sd()) self.log_delim() self.output.ed_timer('Evolution_evaluate') # update pareto front population = tools.selNSGA2(population + offspring, len(offspring)) front.update(population) for individual in front: if best > individual: best = individual self.limit.set('stagnation', -1) # restart self.output.st_timer('Evolution_next', 'next') self.limit.increase('iteration') self.limit.increase('stagnation') if self.limit.get('stagnation') >= self.stagnation_limit: self.log_delim().output.log('Front:') for point in front: self.output.log(str(point)) front = tools.ParetoFront() points.append(best) best = root self.limit.set('stagnation', 0) info = self.strategy.configure(self.limit.limits) self.output.debug(3, 1, 'configure: ' + str(info)) population = [root] pop = self.strategy.breed(population) offspring = [creator.Individual(ind.backdoor) for ind in pop] # create new log file if not self.limit.exhausted(): self.touch_log().log_info().log_delim() else: info = self.strategy.configure(self.limit.limits) self.output.debug(3, 1, 'configure: ' + str(info)) pop = self.strategy.breed(population) offspring = [creator.Individual(ind.backdoor) for ind in pop] self.output.ed_timer('Evolution_next') self.limit.set('time', now() - timestamp) self.output.ed_timer('Evolution_iteration_%d' % it) if root > best: self.log_delim().output.log('Front:') for point in front: self.output.log(str(point)) points.append(best) self.output.ed_timer(self.name) return points
def evolve(sample_num, config): #random.seed(64) toolbox = getToolBox(config) start = time.time() evospace_sample = get_sample(config) tGetSample = time.time() - start startEvol = time.time() pop = [ creator.Individual(cs['chromosome']) for cs in evospace_sample['sample'] ] # Evaluate the entire population fitnesses = map(toolbox.evaluate, pop) for ind, fit in zip(pop, fitnesses): ind.fitness.values = fit total_evals = len(pop) best_first = None # Begin the evolution for g in range(config["WORKER_GENERATIONS"]): # Select the next generation individuals offspring = toolbox.select(pop, len(pop)) # Clone the selected individuals offspring = map(toolbox.clone, offspring) # Apply crossover and mutation on the offspring for child1, child2 in zip(offspring[::2], offspring[1::2]): if random.random() < config["CXPB"]: toolbox.mate(child1, child2) del child1.fitness.values del child2.fitness.values for mutant in offspring: if random.random() < config["MUTPB"]: toolbox.mutate(mutant) del mutant.fitness.values # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit total_evals += len(invalid_ind) #print " Evaluated %i individuals" % len(invalid_ind), # The population is entirely replaced by the offspring pop[:] = offspring # Gather all the fitnesses in one list and print the stats fits = [ind.fitness.values[0] for ind in pop] #length = len(pop) #mean = sum(fits) / length #sum2 = sum(x*x for x in fits) #std = abs(sum2 / length - mean**2)**0.5 best = max(fits) if not best_first: best_first = best if best >= 1.0: print tools.selBest(pop, 1)[0] break #print " Min %s" % min(fits) + " Max %s" % max(fits)+ " Avg %s" % mean + " Std %s" % std #print "-- End of (successful) evolution --" sample = [{ "chromosome": ind[:], "id": None, "fitness": { "DefaultContext": ind.fitness.values[0] } } for ind in pop] evospace_sample['sample'] = sample tEvol = time.time() - startEvol startPutback = time.time() if random.random() < config["RETURN_RATE"]: put_sample(config, evospace_sample) was_returned = "RETURNED" else: was_returned = "LOST" tPutBack = time.time() - startPutback return best >= 1.0, \ [config["CHROMOSOME_LENGTH"],best, sample_num, round(time.time() - start, 2), round(tGetSample,2) , round( tEvol,2), round(tPutBack, 2), total_evals, best_first,was_returned]
def CIQ_byGA(img, window_size, gen, K=16, pop=100, filename='', sug_flag=False, a=50): if not os.path.isdir(filename): os.mkdir(filename) print('make dir {}'.format(filename)) save_dir = os.path.join(filename, 'CIQ_byGA_class_{}_window_{}_weight_{}'.format(K, window_size, a)) if not os.path.isdir(save_dir): os.mkdir(save_dir) print('make dir {}'.format(save_dir)) # 問題の設定 creator.create("FitnessMin", base.Fitness, weights=(-1.0,)) creator.create("Individual", list, fitness=creator.FitnessMin) def create_gene(): gene = np.random.randint(0, 255, (K, 3)) return creator.Individual(gene) # 個体集団の設定 toolbox = base.Toolbox() toolbox.register("population", tools.initRepeat, list, create_gene) def fit(individual): """ 適合度関数 MSEまたは提案距離を用いる :param individual: :return: """ quantized = CIQ_from_ColorPalettes(img, individual) fitness = 0 if sug_flag: fitness, _, __ = get_sug1_error(img, quantized, window_size, 1, a) else: fitness = get_mse(img, quantized) return fitness, def mutate(individual): point = random.randint(0, 4) sumple = create_gene() individual[point] = sumple[point] return individual, toolbox.register("evaluate", fit) toolbox.register("mate", tools.cxTwoPoint) # toolbox.register("mutate", mutate) toolbox.register("mutate", mutate) toolbox.register("select", tools.selTournament, tournsize=3) random.seed() # 初期の個体群を生成 pop = toolbox.population(n=(pop - 1)) # 1個体のみKMeansによる量子化で得たカラーパレットを用いる _, centers = KMeans_Clastering(img, img, K) pop.append(creator.Individual(centers)) CXPB, MUTPB, NGEN = 0.8, 0.1, gen # 交差確率、突然変異確率、進化計算のループ回数 print("Start of evolution") # 初期の個体群の評価 fitnesses = list(map(toolbox.evaluate, pop)) for ind, fit in zip(pop, fitnesses): ind.fitness.values = fit print(" Evaluated %i individuals" % len(pop)) hist = [] pop_hist = [] # 進化計算開始 fits_for_graph = [] for g in range(NGEN): # print("-- Generation %i --" % g) # 次世代の個体群を選択 offspring = toolbox.select(pop, len(pop)) # 個体群のクローンを生成 offspring = list(map(toolbox.clone, offspring)) # 選択した個体群に交差と突然変異を適応する # 偶数番目と奇数番目の個体を取り出して交差 # offspring.sort() for child1, child2 in zip(offspring[::2], offspring[1::2]): if random.random() < CXPB: toolbox.mate(child1, child2) del child1.fitness.values del child2.fitness.values for mutant in offspring: if random.random() < MUTPB: toolbox.mutate(mutant) del mutant.fitness.values # 適合度が計算されていない個体を集めて適合度を計算 invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # print(" Evaluated %i individuals" % len(invalid_ind)) # 次世代群をoffspringにする pop[:] = offspring # すべての個体の適合度を配列にする fits = [ind.fitness.values[0] for ind in pop] best_ind = tools.selBest(pop, 1)[0] if g % 10 == 0 or g == (NGEN - 1): print("%s gen: Best individual score is %s" % (g, best_ind.fitness.values)) filename = os.path.join(save_dir, 'class_{}_MSE_gen{}.bmp'.format(K, g)) quantized = CIQ_from_ColorPalettes(img, best_ind) cv2.imwrite(filename, quantized) mse = get_mse(img, quantized) hist.append(best_ind.fitness.values[0]) fits_for_graph.append(best_ind.fitness.values) pop_hist.append([best_ind]) # print("-- End of (successful) evolution --") gens_index = np.arange(0, NGEN) fig = plt.figure() gen_ax = fig.add_subplot(1, 1, 1) gen_ax.plot(gens_index, fits) gen_ax.set_xlabel('Generations') gen_ax.set_ylabel('Fitness') plt.tight_layout() res_filename = os.path.join(save_dir + 'fitness.bmp') plt.savefig(res_filename) arr = np.array(fits) df = pd.DataFrame(arr) df.column = ['fitness'] plt.close(fig) csv_name = os.path.join(save_dir, 'fitness.csv') df.to_csv(csv_name) print('save {} csv'.format(csv_name)) best_ind = tools.selBest(pop, 1)[0] print("Best individual score is %s" % (best_ind.fitness.values)) return best_ind
def create_individual(lst): arr = np.array(lst).clip(bounds[:, 0], bounds[:, 1]) #@UndefinedVariable return creator.Individual(arr) # @UndefinedVariable
def modEuPlusLambda(self, population, toolbox, mu, lambda_, cxpb, mutpb, ngen, stats=None, halloffame=None, cutoff=600, start=0, trace_path=None, verbose=__debug__): logbook = tools.Logbook() logbook.header = ['gen', 'nevals'] + (stats.fields if stats else []) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in population if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit if halloffame is not None: halloffame.update(population) record = stats.compile(population) if stats is not None else {} logbook.record(gen=0, nevals=len(invalid_ind), **record) if verbose: print(logbook.stream) gen = 1 overall_best = None # Begin the generational process while gen < ngen + 1 and time.time() - start < cutoff: gen += 1 # Vary the population population.extend(tools.selBest( population, 5)) num = random.randint(900, 1000) / 1000.0 # num = len(population[0]) / 2.0 population.extend( [creator.Individual(self.create_individual(bar=num)) for i in range(10)]) offspring = algorithms.varOr( population, toolbox, lambda_, cxpb, mutpb) # Evaluate the individuals with an invalid fitness invalid_ind = [ind for ind in offspring if not ind.fitness.valid] fitnesses = toolbox.map(toolbox.evaluate, invalid_ind) for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit # Update the hall of fame with the generated individuals if halloffame is not None: halloffame.update(offspring) # Select the next generation population population[:] = toolbox.select(population + offspring, mu) # this_best = max(population, key=lambda x: x.fitness) this_best = tools.selBest(population, 1)[0] if overall_best is None: overall_best = this_best overall_best = tools.selBest([this_best, overall_best], 1)[0] if trace_path is not None: with open(trace_path, 'a') as f: f.write( ','.join([str(time.time() - start), str(sum(overall_best))]) + "\n") # Update the statistics with the new population try: record = stats.compile(population) if stats is not None else {} except: None logbook.record(gen=gen, nevals=len(invalid_ind), **record) if verbose and gen % 10 == 0: print(logbook.stream) print(f" Score: {sum(overall_best)}") return population, logbook
def gen_individual(device, apk_dir, package_name): if settings.DEBUG: print "Generate Individual on device, ", device suite = get_suite(device, apk_dir, package_name) return (creator.Individual(suite), device)
def counter(toolbox, pset): print 'Counter' re_sp = 0 config = yaml.load(open("conf/conf.yaml")) server = jsonrpclib.Server(config["server"]) free_pop = eval(server.getFreePopulation()) free_file = eval(server.getFreeFile()) print free_file, free_pop if free_pop and free_file: server.setFreeFile('False') print 'Free and Free' r = server.get_CounterSpecie() print r if scheck_(server, r, config["porcentage_flag"], config["porcentage"]): print "speciation-required" free_species = [] # List of free species flag_check = True rs_species = [] # List of species for i in range(1, int(r)): rs_species.append(int(server.getSpecieInfo(i)['specie'])) # Opening files to save data. d = './ReSpeciacion/%s/rspecie_%d.txt' % (config["problem"], config["n_problem"]) ensure_dir(d) best = open(d, 'a') d = './ReSpeciacion/%s/nspecie_%d.csv' % (config["problem"], config["n_problem"]) ensure_dir(d) n_specie = open(d, 'a') d = './General/%s/datapop_%d_%d.txt' % ( config["problem"], config["n_problem"], config["set_specie"]) neatGPLS.ensure_dir(d) datapop_ = open(d, 'a') while flag_check: flag_check = check_(server, r, free_species) print("waiting - this worker will make speciation") if not flag_check: print 'ReSpeciacion' sp_init = datetime.datetime.now() pop = [] for sp in rs_species: evospace_sample = server.getSample_specie(sp) for cs in evospace_sample['sample']: i = creator.Individual( neat_gp.PrimitiveTree.from_string( cs['chromosome'], pset)) if isinstance(cs['params'], list): i.params_set(np.asarray(cs['params'])) elif isinstance(cs['params'], unicode): i.params_set( np.asarray([ float(elem) for elem in cs['params'].strip( '[]').split(',') ])) i.specie(int(cs['specie'])) pop.append(i) print 'Flush population' server.flushPopulation() for ind in pop: datapop_.write( '\n%s;%s;%s;%s' % (str(sp_init), ind, len(pop), ind.get_specie())) server.initialize() print 'Initialize population' neat_alg = config["neat_alg"] if neat_alg: a, b, init_pop = speciation_init(config, server, pop, config["neat_h"]) list_spe = calc_intracluster(pop) for elem in list_spe: specielist = { 'id': None, 'specie': str(elem[0]), 'intra_distance': str(elem[1]), 'flag_speciation': 'False', 'sp_event': 'True' } server.putSpecie(specielist) n_specie.write('\n%s,%s' % (str(elem[0]), str(elem[1]))) server.putZample(init_pop) server.setFreePopulation('True') num_specie = server.get_CounterSpecie() print "numero de especies creadas: ", num_specie print 'ReSpeciacion- Done' re_sp = 1 best.write( '\n%s;%s;%s;%s' % (str(datetime.datetime.now()), str(sp_init), len(pop), num_specie)) server.setFreePopulation('True') server.setFreeFile('True') contSpecie.cont_specie = 0 return re_sp else: print 'No Free' if free_pop and free_file is False: while free_file is False: print "waiting" time.sleep(5) try: free_file = eval(server.getFreeFile()) except TypeError: free_file = False re_sp = 0 free_pop = eval(server.getFreePopulation()) while free_pop is False: re_sp = 1 print "still waiting", free_pop try: time.sleep(1) free_pop = eval(server.getFreePopulation()) except TypeError: time.sleep(5) free_pop = eval(server.getFreePopulation()) return re_sp
def main(mode,filename): random.seed(64) #学習を途中から開始するモード if mode == "LP": try: pop = load_indivisuals(filename) #既存の世代をロード pop = [creator.Individual(l) for l in pop] #Individual型に変更 except FileNotFoundError: pop = toolbox.population(n=NIND) #初期世代を作成 #新しく学習を開始するモード elif mode == "NP": pop = toolbox.population(n=NIND) #初期世代を作成 write_indivisuals(pop=pop,filename=filename,mode="w") else: exit(1) print(POST(name = "set_indivisuals",data=str(pop)[1:-1])) print("Start of evolution") fitnesses = list(map(toolbox.evaluate, pop)) #初期世代をここで評価 for ind, fit in zip(pop, fitnesses): # 初期世代の各個体の適応度を記録 ind.fitness.values = fit print(" Evaluated %i indivisuals" % len(pop)) for g in range(NGEN): print("-- Generation %i --" % g) offspring = toolbox.select(pop, len(pop)) #適応度を元に次世代の親となる子孫選択 offspring = list(map(toolbox.clone, offspring)) for child1, child2 in zip(offspring[:int(len(offspring)/2)], offspring[int(len(offspring)/2):]): if random.random() < CXPB: toolbox.mate(child1, child2) del child1.fitness.values del child2.fitness.values for mutant in offspring: if random.random() < MUTPB: toolbox.mutate(mutant) del mutant.fitness.values invalid_ind = [ind for ind in offspring if not ind.fitness.valid] print(POST(name="set_indivisuals",data=str(invalid_ind)[1:-1])) fitnesses = map(toolbox.evaluate, invalid_ind) #再評価 for ind, fit in zip(invalid_ind, fitnesses): ind.fitness.values = fit print(" Evaluated %i indivisuals" % len(invalid_ind)) pop[:] = offspring # 最新の世代を保存 write_indivisuals(pop=pop,filename=filename) fits = [ind.fitness.values[0] for ind in pop] length = len(pop) mean = sum(fits) / length sum2 = sum(x*x for x in fits) std = abs(sum2 / length - mean**2)**0.5 print(" Min %s" % min(fits)) print(" Max %s" % max(fits)) print(" Avg %s" % mean) print(" Std %s" % std) print("-- End of (successful) evolution --") best_ind = tools.selBest(pop, 1)[0] print("Best individual is %s, %s" % (best_ind, best_ind.fitness.values)) POST(name="complete")
def make_ind(toolbox, creator, num_trees): return creator.Individual([toolbox.tree() for _ in range(num_trees)])
def select_parameter(hyper_para): hyper_list = [] for i in hyper_para: hyper_list.append(random.choice(i)) return creator.Individual(hyper_list)