def generate(self): best_policy = None best_reward = -float('Inf') candidates = [] eps = 1 # equal to actions space resolution, eps is step size pop_size = 4 cross_prob = 1 exchange_prob = 1 mutation_pob = 1 generation = 4 tmp_reward = [] tmp_policy = [] random.seed(54) turb = 5 try: # Agents should make use of 20 episodes in each training run, if making sequential decisions # Define population indv_template = DecimalIndividual(ranges=[(0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1),(0, 1), (0, 1)], eps=eps) population = Population(indv_template=indv_template, size = pop_size) population.init() # Initialize population with individuals. # Create genetic operators # Use built-in operators here. selection = RouletteWheelSelection() crossover = UniformCrossover(pc=cross_prob, pe=exchange_prob) # PE = Gene exchange probability mutation = FlipBitMutation(pm=mutation_pob) # 0.1 todo The probability of mutation # Create genetic algorithm engine to run optimization engine = GAEngine(population=population, selection=selection, crossover=crossover, mutation=mutation,) # Define and register fitness function @engine.fitness_register def fitness(indv): p = [0 for _ in range(10)] p = indv.solution policy = {'1': [p[0], p[1]], '2': [p[2], p[3]], '3': [p[4], p[5]], '4': [p[6], p[7]], '5': [p[8], p[9]]}xw reward = self.environment.evaluatePolicy(policy) # Action in Year 1 only print('Sequential Result : ', reward) tmp_reward.append(reward) tmp_policy.append(policy) tmp_single = [] return reward + uniform(-turb, turb) # run engine.run(ng = generation) best_reward = max(tmp_reward) best_policy = tmp_policy[-pop_size] except (KeyboardInterrupt, SystemExit): print(exc_info()) return best_policy, best_reward
def test_mutate_decimal_indv(self): ''' Make sure individual with decimal encoding can be mutated correctly. ''' indv = DecimalIndividual(ranges=[(0, 1), (0, 2)]).init(solution=[0.5, 1.5]) mutation = FlipBitMutation(pm=1.0) chromsome_before = [0.5, 1.5] self.assertListEqual(indv.chromsome, chromsome_before) mutation.mutate(indv, engine=None) for a, b in zip(indv.chromsome, chromsome_before): self.assertNotEqual(a, b)
def tune_weights(self): old_fitness = self.individual.fitness weights_scaling = self.individual.get_subtree_scaling() weights_translation = self.individual.get_subtree_translation() # Create array with range for each scaling and translation parameter range = [ self.scale_range, ] * len(weights_scaling) + [ self.translation_range, ] * len(weights_translation) indv_template = DecimalIndividual(ranges=range, eps=0.1) population = Population(indv_template=indv_template, size=self.pop_size) population.init() engine = GAEngine( population=population, selection=TournamentSelection(), crossover=GaussianCrossover(pc=1.0), mutation=NoMutation(), fitness=self.fitness_function_GAFT, analysis=[ new_early_stopping_analysis(scale_range=self.scale_range) ]) engine.logger = NoLoggingLogger() # Run the GA with the specified number of iterations try: engine.run(ng=self.max_iterations) except ValueError: pass # Get the best individual. best_indv = engine.population.best_indv(engine.fitness) # Log the tuning process print( f"Tuner {np.round(old_fitness, 3)} {np.round(-engine.ori_fmax, 3)}" ) # Only use the new individual if it was really improved if old_fitness > -engine.ori_fmax: weights_scaling, weights_translation = self.split_list( best_indv.solution) self.individual.set_subtree_scaling(weights_scaling) self.individual.set_subtree_translation(weights_translation) self.individual.fitness = -engine.ori_fmax return deepcopy(self.individual)
def test_decimal_run(self): ''' Make sure GA engine can run correctly with decimal encoding individuals. ''' indv_template = DecimalIndividual(ranges=[(0, 10)], eps=0.001) population = Population(indv_template=indv_template, size=50).init() # Create genetic operators. selection = RouletteWheelSelection() crossover = UniformCrossover(pc=0.8, pe=0.5) mutation = FlipBitMutation(pm=0.1) # Create genetic algorithm engine. engine = GAEngine(population=population, selection=selection, crossover=crossover, mutation=mutation) @engine.fitness_register @engine.minimize def fitness(indv): x, = indv.solution return x + 10*sin(5*x) + 7*cos(4*x) engine.run(50)
matrix1 = mf.minMaxScale(matrix1) matrix2 = mf.minMaxScale(matrix2) matrix3 = mf.minMaxScale(matrix3) matrix1 = mf.calculateDistances(matrix1) matrix2 = mf.calculateDistances(matrix2) matrix3 = mf.calculateDistances(matrix3) domains = rs.loadDomainListFromFile(sample_for_domains) n_labels = scop.getUniqueClassifications(sample_for_domains) ground_truth = scop.getDomainLabels(domains) ##################################################### # GENETIC ALGORITHM CONFIG ##################################################### # Define population. indv_template = DecimalIndividual(ranges=[(0.1, 0.9),(0.1, 0.9),(0.1, 0.9)], eps=[0.001,0.001,0.001]) population = Population(indv_template=indv_template, size=30).init() # Create genetic operators. #selection = TournamentSelection() selection = RouletteWheelSelection() crossover = UniformCrossover(pc=0.8, pe=0.5) mutation = FlipBitMutation(pm=0.9) # Create genetic algorithm engine. engine = GAEngine(population=population, selection=selection, crossover=crossover, mutation=mutation, analysis=[FitnessStore]) writer = open(path_to_results+algorithm+'_'+sample+'_'+measure1+'_'+measure2, 'w') current_iteration = 0 current_generation = 0
from gaft.components import BinaryIndividual, DecimalIndividual from gaft.components import Population from gaft.operators import TournamentSelection from gaft.operators import UniformCrossover from gaft.operators import FlipBitBigMutation # Built-in best fitness analysis. from gaft.analysis.fitness_store import FitnessStore from gaft.analysis.console_output import ConsoleOutput # Import evaluation function. from cifar_train_eval_func import run # Define population. indv_template = DecimalIndividual(ranges=[(0, 2), (0, 2), (0, 2), (0, 2), (0, 2)], eps=1) population = Population(indv_template=indv_template, size=8).init() # Create genetic operators. #selection = RouletteWheelSelection() selection = TournamentSelection() crossover = UniformCrossover(pc=0.8, pe=0.5) mutation = FlipBitBigMutation(pm=0.1, pbm=0.55, alpha=0.6) # Create genetic algorithm engine. # Here we pass all built-in analysis to engine constructor. engine = GAEngine(population=population, selection=selection, crossover=crossover, mutation=mutation,
print('==> GENE works not well with SiamRPN') # Distribute task on multi-gpu comm = MPI.COMM_WORLD size = comm.Get_size() rank = comm.Get_rank() GPU_ID = rank % args.gpu_nums node_name = MPI.Get_processor_name() # get the name of the node os.environ['CUDA_VISIBLE_DEVICES'] = str(GPU_ID) time.sleep(rank * 5) print("node name: {}, GPU_ID: {}".format(node_name, GPU_ID)) # define population indv_template = DecimalIndividual(ranges=[(1.0, 1.2), (0.15, 0.7), (0.9, 1.0), (0.05, 0.65)], eps=0.0001) population = Population(indv_template=indv_template, size=100) # zzp: Population size population.init() # Initialize population with individuals. # Create genetic operators selection = RouletteWheelSelection() crossover = UniformCrossover(pc=0.8, pe=0.5) mutation = FlipBitMutation(pm=0.1) # Create genetic algorithm engine to run optimization engine = GAEngine(population=population, selection=selection, \ crossover=crossover, mutation=mutation, \ analysis=[FitnessStore, ConsoleOutput])
parameter_range=[] eps_number = [] for i in range(gas2.n_reactions): parameter_range.append((Arrhenius_parameters[i].pre_exponential_factor - abs(Arrhenius_parameters[i].pre_exponential_factor)*0.2 , Arrhenius_parameters[i].pre_exponential_factor + abs(Arrhenius_parameters[i].pre_exponential_factor)*0.2 + 0.1)) eps_number.append(1e-6) parameter_range.append((Arrhenius_parameters[i].temperature_exponent - abs(Arrhenius_parameters[i].temperature_exponent)*0.2 - 1, Arrhenius_parameters[i].temperature_exponent + abs(Arrhenius_parameters[i].temperature_exponent)*0.2 + 1)) eps_number.append(1e-5) parameter_range.append((Arrhenius_parameters[i].activation_energy-abs(Arrhenius_parameters[i].activation_energy)*0.2 - 300, Arrhenius_parameters[i].activation_energy + abs(Arrhenius_parameters[i].activation_energy)*0.2 + 300)) eps_number.append(1e-5) indv_template = DecimalIndividual(ranges=parameter_range, eps = eps_number) population = Population(indv_template=indv_template, size=50).init() # Create genetic operators. #selection = RouletteWheelSelection() selection = TournamentSelection() crossover = UniformCrossover(pc=0.8, pe=0.5) mutation = FlipBitBigMutation(pm=0.1, pbm=0.55, alpha=0.6) # Create genetic algorithm engine. # Here we pass all built-in analysis to engine constructor. engine = GAEngine(population=population, selection=selection, crossover=crossover, mutation=mutation, analysis=[ConsoleOutput, FitnessStore]) # Define fitness function.
eps = 1 # equal to actions space resolution, eps is step size pop_size = 4 cross_prob = 1 exchange_prob = 1 mutation_pob = 1 generation = 4 REWARDS = [] NEW = [] POLICY = [] tmp_reward = [] tmp_policy = [] random.seed(c) turb = 5 # Define population indv_template = DecimalIndividual(ranges=[(0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1),(0, 1), (0, 1)], eps=eps) population = Population(indv_template=indv_template, size = pop_size) population.init() # Initialize population with individuals. # Create genetic operators # Use built-in operators here. selection = RouletteWheelSelection() crossover = UniformCrossover(pc=cross_prob, pe=exchange_prob) # PE = Gene exchange probability mutation = FlipBitMutation(pm=mutation_pob) # 0.1 todo The probability of mutation # Create genetic algorithm engine to run optimization engine = GAEngine(population=population, selection=selection, crossover=crossover, mutation=mutation,) # Define and register fitness function @engine.fitness_register
# output files (perfs) definition if CASE.upper() == 'ALL': obsSOM, true_labels = utils.loadSOM( save_dir=config.OUTPUT_TRAINED_MODELS_PATH, file_name=config.ZALL_SOM_3D_MODEL_NAME) elif CASE.upper() == 'SEL': obsSOM, true_labels = utils.loadSOM( save_dir=config.OUTPUT_TRAINED_MODELS_PATH, file_name=config.ZSEL_SOM_3D_MODEL_NAME) setupEnv() # print(MODELS_VALUES.shape) # print(MODELS_DICT) indv_template = DecimalIndividual(ranges=[ (0, 1) for _ in range(config.NB_MODELS) ], eps=0.001) population = Population(indv_template=indv_template, size=POPULATION_SIZE) population.init() selection = TournamentSelection( tournament_size=10) # RouletteWheelSelection() crossover = UniformCrossover(pc=CROSSOVER_PROBABILITY, pe=GE_PROBABILITY) mutation = FlipBitMutation(pm=MUTATION_PROBABILITY) engine = GAEngine(population=population, selection=selection, crossover=crossover, mutation=mutation, analysis=[FitnessStore])