def Genetic_al(model, data, i, stud1): ga = GeneticAlgorithm(data, population_size=100, generations=100, crossover_probability=0.01, mutation_probability=0.01, elitism=False, maximise_fitness=False) def create_individual(data): return [ float(1.0 + 5.0 * random.uniform(0, 1)), # Drop time actual float(1.0 + 15.0 * random.uniform(0, 1)), # Drop time actual_EWMA float(1.0 + 15.0 * random.uniform(0, 1)), # Drop time difference float(1.0 + 15.0 * random.uniform(0, 1)), # Drop time difference_EWMA # float((random.randint(300, 4000) + random.uniform(0, 1))),# Energy # float((random.randint(300, 4000) + random.uniform(0, 1))), # Energy_EWMA float(1.0 + 2.0 * random.uniform(0, 1)), # Lift Height actual float(1.0 + 2.0 * random.uniform(0, 1)), # Lift Height actual_EWMA # float((random.randint(0, 2) + random.uniform(0, 1))), # lift height ref # float((random.randint(0, 2) + random.uniform(0, 1))), # lift height ref_EWMA float(15.0 + 40.0 * random.uniform(0, 1)), # Main Weldcurrent voltage actual float(15.0 + 4.0 * random.uniform(0, 1) ), # Main Weldcurrent voltage actual_EWMA # ((random.randint(25, 50) + random.uniform(0, 1))),# Main Weldcurrent voltage Max # ((random.randint(25, 50) + random.uniform(0, 1))), # Main Weldcurrent voltage Max_EWMA # ((random.randint(8, 28) + random.uniform(0, 1))), # Main Weldcurrent voltage Min # ((random.randint(8, 28) + random.uniform(0, 1))), # Main Weldcurrent voltage Min_EWMA # (-(random.randint(1, 3) + random.uniform(0, 1))), # Penetration Max # (-(random.randint(1, 3) + random.uniform(0, 1))), # Penetration Max_EWMA # (-(random.randint(0, 3) + random.uniform(0, 1))), # Penetration Min # (-(random.randint(0, 3) + random.uniform(0, 1))), # Penetration Min_EWMA # (-(random.randint(0, 3) + random.uniform(0, 1))),# Penetratiom Ref # (-(random.randint(0, 3) + random.uniform(0, 1))), # Penetratiom Ref_EWMA float(8.0 + 35.0 * random.uniform(0, 1)), # Pilot Weldcurrent Arc Voltage Act float(8.0 + 35.0 * random.uniform(0, 1) ), # Pilot Weldcurrent Arc Voltage Act_EWMA # ((random.randint(20, 50) + random.uniform(0, 1))),# Pilot Weldcurrent Arc Voltage Max # ((random.randint(20, 50) + random.uniform(0, 1))), # Pilot Weldcurrent Arc Voltage Max_EWMA # ((random.randint(5, 20) + random.uniform(0, 1))),# Pilot Weldcurrent Arc Voltage Min # ((random.randint(5, 20) + random.uniform(0, 1))), # Pilot Weldcurrent Arc Voltage Min_EWMA float(1.5 + 8.0 * random.uniform(0, 1)), # Stickout float(1.5 + 8.0 * random.uniform(0, 1)), # Stickout_EWMA float(500.0 + 1000.0 * random.uniform(0, 1)), # Weldcurrent actual Positive float(500.0 + 1000.0 * random.uniform(0, 1)), # Weldcurrent actual Positive_EWMA float(-1500.0 + 1500.0 * random.uniform(0, 1)), # Weldcurrent actual Negative float(-1500.0 + 1500.0 * random.uniform(0, 1)), # Weldcurrent actual Negative_EWMA float(10.0 + 100.0 * random.uniform(0, 1)), # Weld time actual float(10.0 + 100.0 * random.uniform(0, 1)) ] # Weld time actual_EWMA # ((random.randint(10, 100) + random.uniform(0, 1))), # Weldtime ref # ((random.randint(10, 100) + random.uniform(0, 1)))] # Weldtime ref_EWMA ga.create_individual = create_individual def eval_fitness(individual, data): array = np.array(individual)[np.newaxis] error_array = [] error = (model.predict(array, batch_size=1) + 2)**2 error_array.append(individual) print('Evaluating... error: ' + str(error)) return error ga.fitness_function = eval_fitness ga.run() Gen1 = pd.DataFrame(ga.last_generation()) filepath = "C:\\Users\PHLEGRA\Desktop\MASTER\Data_intersection\Prescribed_parameters\\new" filename = str(stud1) + "_" + str(i) + '_predictions.csv' Gen1.to_csv(filepath + '\\' + filename, index=False) print('Please see file. Process Complete')
parser.add_argument('--turbines', default=2, type=int, help='enter an integer number of turbines to start with') parser.add_argument('--grid_size', default=36, type=int, help='enter an perfect square integer representing grid size (e.g. 6x6 is 36)') args = parser.parse_args() given_crossover_probability = args.crossover_probability given_mutation_probability = args.starting_mutation_probability given_adaptive_mutation_factor = args.adaptive_mutation_factor given_clipping_threshold = args.clipping_threshold given_turbines = args.turbines grid_size= args.grid_size input_data = [0]*grid_size # length of this list is the # of grid cells in layout easyga = GeneticAlgorithm(input_data, crossover_probability =given_crossover_probability , mutation_probability=given_mutation_probability, adaptive_mutation_factor = given_adaptive_mutation_factor, clipping_threshold = given_clipping_threshold, num_turbines = given_turbines) easyga.fitness_function = fitness easyga.run() print(easyga.best_individual()) def fitness (individual, data): freestream_velocity = 15 individual_2D_list = [[individual[0], individual[1]],[individual[2], individual[3]]] individual_np = np.asarray(individual_2D_list) velocities = np.zeros(individual_np.shape) alpha = .9 # entrainment constant x = 150 # distance between perpendicular turbine centers r = 40 # radius of turbine for rowIndex in range(np.shape(individual_np)[0]):
mutate_index2], individual[mutate_index1] ga.mutate_function = mutate #Рулеточная селекция def selection(population): return random.choice(population) ga.selection_function = selection #Реализация функции приспособленности #Ищет такой х, при котором значение функции максимально def fitness(individual, data): fitness = -float("inf") x = -4 for item in individual: if item != 1: x += 0.01 else: fitness = 1 / x break return fitness ga.fitness_function = fitness ga.run() print(ga.best_individual()) #Выводит самую приспособленную особь
rnd = np.random.normal(0, 0.5) if rnd < -0.5: rnd = -0.5 elif rnd > 0.5: rnd = 0.5 individual[0] = individual[0] + rnd ga.mutate_function = mutate def selection(population): #Функция селекции (турнирный отбор) ind1 = random.choice(population) ind2 = random.choice(population) if ind1.fitness > ind2.fitness: return ind1 else: return ind2 ga.selection_function = selection def fitness (individual, data): #Целевая функция if individual[0] < -4.0: #Ограничения переменных ЦФ individual[0] = -4.0 if individual[0] >= 0: individual[0] = 1e-10 return 1 / individual[0] #Значение целевой функции ga.fitness_function = fitness ga.run() #Запуск ГА print("\nBest individual: ", ga.best_individual(), "\n") #Вывод лучшей особи популяции (Решение) for individual in ga.last_generation(): #Вывод всех особей популяции в последнем поколении print(individual)