def test_run(self): ''' Make sure GA engine can run correctly. ''' indv_template = GAIndividual(ranges=[(0, 10)], encoding='binary', eps=0.001) population = GAPopulation(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 def fitness(indv): x, = indv.variants return x + 10 * sin(5 * x) + 7 * cos(4 * x) engine.run(50)
def ga(df, start, end, _positionList, ranges=[(20,100),(0.01, 1),(0.01, 1),(0.01, 1),(1, 5)], eps=0.01): indv_template = BinaryIndividual(ranges=ranges, eps=eps) population = Population(indv_template=indv_template, size=100) population.init() # Initialize population with individuals. # Use built-in operators here. selection = RouletteWheelSelection() crossover = UniformCrossover(pc=0.8, pe=0.5) mutation = FlipBitMutation(pm=0.3) engine = GAEngine(population=population, selection=selection, crossover=crossover, mutation=mutation, analysis=[FitnessStore]) @engine.fitness_register def fitness(indv): n, upper, lower, adds, cutoff = indv.solution df['KAMA'] = talib.KAMA(df.close, int(n)) df['VAR'] = talib.VAR(df.close-df.KAMA.shift(1) - df.close.shift(1)+df.KAMA.shift(2),10) profitsList, buypriceList, sellpriceList, fits,positionList = profitsCal(df, start, end, _positionList, upper=upper, lower=lower, adds = adds, cutoff=cutoff) return float(fits) @engine.analysis_register class ConsoleOutput(OnTheFlyAnalysis): master_only = True interval = 1 def register_step(self, g, population, engine): best_indv = population.best_indv(engine.fitness) msg = 'Generation: {}, best fitness: {:.3f}'.format(g, engine.fmax) print(best_indv.solution) engine.logger.info(msg) engine.run(ng=30) return population.best_indv(engine.fitness).solution, _positionList
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 doga(units, ploads, hload, size=50, ng=5, pc=0.8, pe=0.5, pm=0.1): # 计算不同典型日下,最小运行成本均值 def calcost(indv): # 输入改造方案 for chpunit, rtype in zip(chpunits, indv): chpunit.rtype = rtype meancost = 0 # ploads为字典,key:典型日出现的概率,value:[典型日负荷曲线,风电出力极限曲线1,...,风电出力极限曲线n] for p in ploads.keys(): x = ProSimu() x.pload = ploads[p][0] x.hload = hload wn = 0 for wpunit in wpunits: wn += 1 wpunit.maxwp = ploads[p][wn] x.units = units meancost += x.getoptvalue()*p return meancost ranges = list() wpunits = list() chpunits = list() for unit in units: if unit.ptype == 0: # Wind Power Unit wpunits += [unit] elif unit.ptype == 2: # CHP Unit-1 ranges += [(0, 4)] chpunits += [unit] elif unit.ptype == 3: # CHP Unit-2 ranges += [(0, 3)] chpunits += [unit] template = BinaryIndividual(ranges, eps=1) population = Population(indv_template=template, size=size).init() selection = TournamentSelection() crossover = UniformCrossover(pc=pc, pe=pe) mutation = FlipBitMutation(pm=pm) engine = GAEngine(population=population, selection=selection, crossover=crossover, mutation=mutation, analysis=[ConsoleOutput, FitnessStore]) @engine.fitness_register @engine.minimize def fitness(indv): # print(type(float(calcost(indv.solution)))) return float(calcost(indv.solution)) engine.run(ng=ng) bestindv = population.best_indv(engine.fitness).solution for unitt, rtype in zip(chpunits, bestindv): unitt.rtype = rtype result = calcost(bestindv) print('Best individual:', bestindv) print('Optimal result:', result)
def __init__(self ,k ,total_implied_variance ,slice_before ,slice_after ,tau): self.k =k self.total_implied_variance =total_implied_variance self.slice_before =slice_before self.slice_after = slice_after self.tau = tau # Define population. indv_template = BinaryIndividual(ranges=[(1e-5, 20),(1e-5, 20),(1e-5, 20)], eps=0.001) self.population = Population(indv_template=indv_template, size=30).init() # Create genetic operators. selection = TournamentSelection() crossover = UniformCrossover(pc=0.8, pe=0.5) mutation = FlipBitMutation(pm=0.1) # Create genetic algorithm engine. self.engine = GAEngine(population=self.population, selection=selection, crossover=crossover, mutation=mutation, analysis=[FitnessStore]) # Define fitness function. @self.engine.fitness_register @self.engine.minimize def fitness(indv): a, b, m, rho, sigma = indv.solution model_total_implied_variance=svi_raw(self.k,np.array([a, b, m, rho, sigma]),self.tau) value = norm(self.total_implied_variance - model_total_implied_variance,ord=2) # if bool(len(self.slice_before)) and np.array(model_total_implied_variance < self.slice_before).any(): # value +=(np.count_nonzero(~np.array(model_total_implied_variance < self.slice_before))*100) # # value = 1e6 # # if bool(len(self.slice_after)) and np.array(model_total_implied_variance > self.slice_after).any(): # value += float(np.count_nonzero(~np.array(model_total_implied_variance > self.slice_after)) * 100) # # value = 1e6 # if np.isnan(value): # value = 1e6 value = float(value) return value
''' population = Population(indv_template=indv_template, size=30).init() # Create genetic operators. selection = TournamentSelection() crossover = UniformCrossover(pc=0.8, pe=0.5) ''' Crossover operator with uniform crossover algorithm, see https://en.wikipedia.org/wiki/Crossover_(genetic_algorithm) :param pc: The probability of crossover (usaully between 0.25 ~ 1.0) :type pc: float in (0.0, 1.0] :param pe: Gene exchange probability. ''' mutation = FlipBitMutation(pm=0.1) # pm is the possibility of the mutation # Create genetic algorithm engine. engine = GAEngine(population=population, selection=selection, crossover=crossover, mutation=mutation, analysis=[FitnessStore]) # Define fitness function. @engine.fitness_register def fitness(indv): x, = indv.solution return x + 10*sin(5*x) + 7*cos(4*x) # Define on-the-fly analysis. @engine.analysis_register class ConsoleOutputAnalysis(OnTheFlyAnalysis):
#str_P = 20 #str_PC = 0.8 #str_PM = 0.1 #str_ng = 2 # Define population. indv_template = GAIndividual(ranges=[(str_THF_LB, str_THF_HB), (str_TOL_LB, str_TOL_HB)], encoding='binary', eps=0.001) population = GAPopulation(indv_template=indv_template, size=str_P).init() # Create genetic operators. selection = RouletteWheelSelection() crossover = UniformCrossover(pc=str_PC, pe=0.5) mutation = FlipBitMutation(pm=str_PM) # 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=[ConsoleOutputAnalysis, FitnessStoreAnalysis]) # Define fitness function. @engine.fitness_register def fitness(indv): # Active HYSYS Model hyApp_Fitness = win32.Dispatch("HYSYS.Application")
def generate(self): import random eps = 0.2 # equal to actions space resolution # range/eps pop_size = 2 cross_prob = 0.6 exchange_prob = 0.7 mutation_pob = 0.8 generation = 6 REWARDS = [] tmp_reward = [] tmp_policy = [] bad_p = [] good_p = [] turb = 0 try: # Agents should make use of 20 episodes in each training run, if making sequential decisions first_action = [] for a1 in [i / 10 for i in range(0, 11, 2)]: for a2 in [i / 10 for i in range(0, 11, 2)]: first_action.append([a1, a2]) # [0,0] is absolutely bad action first_action = first_action[1:] action_reward = [] for i in range(len(first_action)): ar = self.environment.evaluateAction(first_action[i]) self.environment.reset() action_reward.append(ar[1]) # get the best policy for first year best_action = first_action[action_reward.index(max(action_reward))] test_action = ([[i / 10 for i in range(0, 11, 2)], [i / 10 for i in range(0, 11, 2)]]) # 2. Define population indv_template = OrderIndividual( ranges=[(0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1)], eps=eps, actions=test_action, best_1=best_action) # low_bit and high_bit population = Population(indv_template=indv_template, size=pop_size) population.init() # Initialize population with individuals. # 3. Create genetic operators # Use built-in operators here. selection = LinearRankingSelection() crossover = UniformCrossover( pc=cross_prob, pe=exchange_prob) # PE = Gene exchange probability mutation = FlipBitMutation( pm=mutation_pob) # 0.1 todo The probability of mutation # 4. Create genetic algorithm engine to run optimization engine = GAEngine(population=population, selection=selection, crossover=crossover, mutation=mutation) # 5. Define and register fitness function @engine.fitness_register def fitness(indv): p = [0 for _ in range(10)] p = indv.solution # encode 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]] } reward = self.environment.evaluatePolicy(policy) tmp_reward.append(reward) tmp_policy.append(policy) return reward + uniform(-turb, turb) @engine.analysis_register class ConsoleOutput(OnTheFlyAnalysis): master_only = True interval = 1 def register_step(self, g, population, engine): best_indv = population.best_indv(engine.fitness) msg = 'Generation: {}, best fitness: {:.3f}'.format( g + 1, engine.fmax) REWARDS.append( max(tmp_reward[pop_size * (g - 0):pop_size * (g + 1)])) engine.logger.info(msg) 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
from gaft.analysis.console_output import ConsoleOutput # 输出 # 定义编码 individual_template = BinaryIndividual(ranges=[(0, 10)], eps=0.001) # 定义种群 _population = Population(indv_template=individual_template, size=20) # 种群初始化 _population.init() # 遗传操作 selection = RouletteWheelSelection() # 个体选择:轮盘赌 crossover = UniformCrossover(pc=0.8, pe=0.5) # 交叉算子:均匀交叉 mutation = FlipBitMutation(pm=0.1) # 变异算子:翻转突变 # 遗传算法引擎 _engine = GAEngine(population=_population, selection=selection, crossover=crossover, mutation=mutation, analysis=[ConsoleOutput]) # 适应度:目标 @_engine.fitness_register # @_engine.minimize def fitness(individual): x, = individual.solution return 0.01 * x + 10 * math.sin(5 * x) + 7 * math.cos(4 * x) if __name__ == '__main__':
file_name=config.ZSEL_SOM_3D_MODEL_NAME) setupEnv() # print(MODELS_VALUES.shape) # print(MODELS_DICT) indv_template = ProbabilisticIndividual(ranges=[ (0, 1) for _ in range(config.NB_MODELS) ], eps=0.001) population = Population(indv_template=indv_template, size=POPULATION_SIZE) population.init() selection = 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]) @engine.fitness_register def fitness(indv): global MODELS_VALUES global MODELS_DICT global CASE global true_labels solution_weights = indv.solution
for i in range(popsize): indv_list.append(indv_template.clone()) population = GAPopulation(indv_template=indv_template, size=popsize).init(indv_list) # Create genetic operators. # selection = TournamentSelection()#RouletteWheelSelection() if tselect: print('Tournament selection') selection = TournamentSelection() else: print('RouletteWheelSelection') selection = RouletteWheelSelection() crossover = UniformCrossover(pc=pc, pe=pe) mutation = FlipBitMutation(pm=pm) # Create genetic algorithm engine. engine = GAEngine(population=population, selection=selection, crossover=crossover, mutation=mutation, analysis=[FitnessStore]) @engine.fitness_register def fitness(indv): # print('x variants',indv.variants,type(indv.variants),len(indv.variants)) x_decode = indv.chromsome assert (len(x_decode)==n) state = decode_sequence(x_decode) power = power_by_mtt_fastgraph(state, graph)
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 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]]} reward = envSeqDec.evaluatePolicy(policy) # Action in Year 1 only print('Sequential Result : ', reward) tmp_reward.append(reward) tmp_policy.append(policy)
ax = fig.add_subplot(111) ax.plot(geracoes, n_nines) plt.show() if __name__ == "__main__": individuo = BinaryIndividual(ranges=[(-100, 100), (-100, 100)], eps=0.000001) populacao = Population(indv_template=individuo, size=100) populacao.init() selecao = TournamentSelection() crossover = UniformCrossover(pc=0.65, pe=0.65) mutacao = FlipBitMutation(pm=0.008) engine = GAEngine(population=populacao, selection=selecao, crossover=crossover, mutation=mutacao, analysis=[FitnessStore, ConsoleOutput]) @engine.fitness_register def aptidao(ind): x, y = ind.solution return 0.5 - ((sin(sqrt(x**2 + y**2))**2 - 0.5) / (1 + 0.001 * (x**2 + y**2))**2) engine.run(ng=40)
def generate(): import random good_seed = int(sys.argv[1]) print('currrrrrrrrrrrrrrrrrrrrrrrrrrrrr', good_seed) envSeqDec = ChallengeProveEnvironment() # Initialise a New Challenge Environment to post entire policy #env = ChallengeEnvironment(experimentCount = 20000) eps = 0.1 # equal to actions space resolution # range/eps pop_size = 4 cross_prob = 0.6 exchange_prob = 0.7 mutation_pob = 0.8 generation = 4 REWARDS = [] NEW = [] POLICY = [] tmp_reward = [] tmp_policy = [] policy_450 = [] reward_generation = [] time = [] random.seed(good_seed) # best_action = ([0], [0.8, 1]) turb = 0 test_action = ([[i/10 for i in range(0, 11)],[i/10 for i in range(0,11)]]) # test_action = ([0, 0.1, 0.2, 0.3, 0.4, 0.5], [0.6, 0.7, 0.8, 0.9, 1]) # 2. Define population indv_template = OrderIndividual(ranges=[(0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1), (0, 1)], eps=eps, actions = test_action) # low_bit and high_bit population = Population(indv_template=indv_template, size = pop_size) population.init() # Initialize population with individuals. # 3. Create genetic operators # Use built-in operators here. #selection = RouletteWheelSelection() selection = TournamentSelection() crossover = UniformCrossover(pc=cross_prob, pe=exchange_prob) # PE = Gene exchange probability mutation = FlipBitMutation(pm=mutation_pob) # 0.1 todo The probability of mutation # 4. Create genetic algorithm engine to run optimization engine = GAEngine(population=population, selection=selection, crossover=crossover, mutation=mutation,) # analysis=[FitnessStore]) # 5. Define and register fitness function @engine.fitness_register #@engine.dynamic_linear_scaling(target='max', ksi0=2, r=0.9) def fitness(indv): p = [0 for _ in range(10)] p = indv.solution # encode 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]]} reward = envSeqDec.evaluatePolicy(policy) # Action in Year 1 only #print('Sequential Result : ', reward) tmp_reward.append(reward) reward_generation.append(reward) tmp_policy.append(policy) #print('Policy : ', policy) #print(policy_450,'**************************good solution***************') #print(policy_bad,'**************************bad solution***************') return reward + uniform(-turb, turb) @engine.analysis_register class ConsoleOutput(OnTheFlyAnalysis): master_only = True interval = 1 def register_step(self, g, population, engine): best_indv = population.best_indv(engine.fitness) msg = 'Generation: {}, best fitness: {:.3f}'.format(g + 1, engine.fmax) #best_reward = max(tmp_reward[g + pop_size * (generation - 1): g + pop_size * generation]) #print(pop_size * (g - 0), pop_size * (g + 1),'&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') REWARDS.append(max(reward_generation[pop_size * (g - 0): pop_size * (g + 1)])) #best_policy = POLICY[tmp_reward.index(best_reward)] #POLICY.append(best_policy) engine.logger.info(msg) engine.run(ng = generation) # print(policy_450) x = list(range(len(REWARDS))) plt.plot(x, REWARDS) plt.title(f'Sequential Rewards {good_seed}') plt.savefig(f'./GA_trick/GA_seed_{good_seed}.jpg') # #plt.savefig(f'./res_geneticAlgorithm/Sequential_Rewards_eps:{eps}_popsize:{pop_size}_generation:{generation}_mutation_pob:{mutation_pob}_exchange_prob:{exchange_prob}_cross_prob:{cross_prob}.jpg') plt.show()