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')
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)
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): #Целевая функция return individual[0] * individual[0] + 4 #Значение целевой функции ga.fitness_function = fitness ga.run() # Вывод лучшей особи популяции (Наше решение) print("\nBest individual: ", ga.best_individual(), "\n") # Вывод всех особей популяции в последнем поколении for individual in ga.last_generation(): print(individual)
materiasSelec = [] if individual.count( 1 ) <= 5: #con esto controlo cuantas materias selecciono de data. (ahora me quedo solo con individuos que tienen 6 materias MAX) for (selected, (materia)) in zip(individual, data): if selected: #con esto veo unicamente las materias seleccionadas (una por una) materiasSelec.append(materia) fitness += restricciones(materiasSelec) ac.append(fitness) return fitness #ga.selection_function = ga.random_selection ga.fitness_function = fitness ga.run() print(ga.best_individual()) print(ga.last_generation()) for index, materia in enumerate(ga.best_individual()[1]): if materia == 1: print(data[index]) #print(ac)