def main1(): vecs = load_inits(3) with open("./generations.json") as f: otp = json.load(f) otp = otp["results"] # DO GENETIC ALGO results = [] for i in range(3): for j in range(i+1, 3): # x1, x2 = BSC(vecs[i], vecs[j]) x1, x2 = K_point_crossover(vecs[i], vecs[j], 0.5, 8) x1 = mutate(x1) x2 = mutate(x2) # print(x1) # print(x2) # print("") res1 = get_errors(TEAM_KEY, x1.tolist()) res2 = get_errors(TEAM_KEY, x2.tolist()) results.append({"vector": x1.tolist(), "results": res1}) results.append({"vector": x2.tolist(), "results": res2}) print(res1[0]/1e11, res1[1]/1e11, x1) print(res2[0]/1e11, res2[1]/1e11, x2) print("") results = {"generation": 3, "vectors": results, "parents": []} for i in range(3): results["parents"].append(vecs[i].tolist()) otp.append(results) otp = {"results": otp} with open("./generations.json", "w") as f: json.dump(otp, f)
def explore(): INDEX_EXPLORING = 10 here = get_best_from_all_gens(1)[0] best_vec = here["vector"] orig_res = here["results"] print(orig_res[0] / 1e11, orig_res[1] / 1e11) values = [] for i in range(-5, 5): if i == 0: continue copied = [j for j in best_vec] toadd = i * 1e-15 copied[INDEX_EXPLORING] += toadd # res = [0, 0] res = get_errors(TEAM_KEY, copied) print(i, res[0] / 1e11, res[1] / 1e11, (fitness(res) - fitness(orig_res)) / 1e11) values.append({ "index": INDEX_EXPLORING, "difference": toadd, "results": res }) with open("exploring.json") as f: oldvals = json.load(f) for val in values: oldvals["explorations"].append(val) with open("exploring.json", "w") as f: json.dump(oldvals, f)
def generate_parents(): for _ in range(population): generated_vector = [] for i in range(len(gfather_vector)): scale = 1 if i in [7, 9]: scale = 1e-2 # for less change 1e-3 elif i in [8, 10]: scale = 1e-2 # for less change 1e-4 elif i in [5]: scale = 1e-1 # for less change 1e-2 random_add = (random.random() - 0.5) * scale new_value = gfather_vector[i] + gfather_vector[i] * random_add generated_vector.append(max(min(new_value, 10), -10)) generated_vectors.append(generated_vector) write_file = open("parent.txt", "a") for i in range(population): MSE = get_errors(generated_vectors[i]) print(MSE) json_data = { "MSE": MSE, "score": 1 / get_score(MSE), "generated_vector_used": generated_vectors[i], } json.dump(json_data, write_file) write_file.write("\n")
def get_both_err(population): ''' This function utilises the API call provided to us for getting the errors on the vectors within the population. Parameter --------- population: list of vector of 11-D Return ------ It returns two list train_err & validation_err which are errors for the given poplation's vectors. ''' # train_err = [ random.randint(1,300) for i in range(len(population))] # validation_err = [ random.randint(1,300) for i in range(len(population))] train_err = [] validation_err = [] for individual in population: [te, ve] = server.get_errors(TEAM_ID, individual) train_err.append(te) validation_err.append(ve) return train_err, validation_err
def get_fitness(chromosomes): global minVal global minguy global requests fitness = [] print() print("Errors:>>") print() print("XXXXX-----XXXXX") for chromosome in chromosomes: ta_answer = ta.get_errors(SECRET, list(chromosome)) requests += 1 # ta_answer = [np.random.uniform( # 10000, 1000000), np.random.uniform(10000, 100000)] if minVal == None: minVal = ta_answer minguy = chromosome else: if ((minVal[0] + (FACTOR * minVal[1])) * SUM_FACTOR) + abs(minVal[0] - minVal[1]) > ( ((FACTOR * ta_answer[1]) + ta_answer[0]) * SUM_FACTOR) + abs(ta_answer[0] - ta_answer[1]): minVal = ta_answer minguy = chromosome fitness.append(1e15 / (((ta_answer[0] + FACTOR * ta_answer[1]) * SUM_FACTOR) + abs(ta_answer[0] - ta_answer[1]))) print( f'kid: {chromosome} train error: {ta_answer[0]}, validation error: {ta_answer[1]}' ) print() print("XXXXX-----XXXXX") print() return np.array(fitness)
def cal_pop_fitness(vector): # Calculating the fitness value of each solution in the current population. for itr in range(population_number): temp = list(vector[itr][1:]) # take each of the genes err = client.get_errors(secret_key, temp) vector[itr][0] = err[1] + err[0] print("query number:- ", itr, err)
def gfather_test(): write_file = open("gfather.txt", "a") MSE = get_errors(gfather_vector) json_data = { "MSE": MSE, "score": 1 / get_score(MSE), "generated_vector_used": gfather_vector, } json.dump(json_data, write_file) write_file.write("\n")
def fitness_func(weight): ''' Given weights, calculates the fitness functions and calls the API ''' global debug weight = list(np.array(weight).ravel()) if not debug: train_error , validation_error = client.get_errors('GU0MCOlKFoi0lk7HmBpwjhFGlcTTZvO77FA3FVMq0m4lYCMIho' , weight) if debug: train_error , validation_error = equation.get_errors(weight) #train_error, validation_error = equation.get_errors(weight) if not debug: train_weight , val_weight = 1 , 1 if debug: train_weight , val_weight = 0.4 , 0.6 ratio = train_weight*train_error + val_weight*validation_error return (1/ratio), train_error, validation_error
def get_fitness(self, num_gen): # get the fitness of population vectors def error_to_fitness(train_err, valid_err): # return -(5*valid_err + 2*abs(train_err - valid_err)) # return -(train_err + valid_err) # return -(valid_err + 3*abs(valid_err - train_err)) # return 1/( abs(valid_err-train_err) + 5*valid_err ) # return 1/(train_err + 5*valid_err) # return 1 / (abs(train_err - valid_err)) return 1 / (2 * abs(train_err - valid_err) + 1.25 * train_err + 3 * valid_err) # return 1 / (2*abs(train_err - valid_err) + 5*valid_err + 2*train_err) fitness = [] train_errors = [] valid_errors = [] weight_fitness = [] counter = 1 for gene in self.population: train_err, valid_err = server.get_errors(TEAM_ID, list(gene)) fit = error_to_fitness(train_err, valid_err) fitness.append(fit) train_errors.append(train_err) valid_errors.append(valid_err) weight_fitness.append((gene, fit)) curr_dic = {} curr_dic["gene"] = gene.tolist() curr_dic["fitness"] = fit curr_dic["train_err"] = train_err curr_dic["valid_err"] = valid_err self.log_dict["generation_" + str(num_gen)][counter] = copy.deepcopy(curr_dic) counter += 1 fitness = np.array(fitness, dtype=np.double) self.gene_and_fitness = weight_fitness return fitness, train_errors, valid_errors
def main2(): # myvecs = [overfit_vector * 2] myvecs = init_values(POPULATION_SIZE, VECTOR_SIZE) with open("./init_vecs.json") as f: results = json.load(f) # print(myvecs) for vec in myvecs: # print(vec) res = get_errors(TEAM_KEY, vec) results["vectors"].append({ "vector": vec, "train_error": res[0], "val_error": res[1] }) print(res, vec) with open("./init_vecs.json", "w") as f: json.dump(results, f)
def variation_test_for_zero(): write_file = open("variation.txt", "a") overfit = [] for d in overfit_vector: overfit.append(d) mutation_pos = -10 overfit[0] += mutation_pos MSE = get_errors(overfit) json_data = { "mutation": "+" + "0", "value": mutation_pos, "MSE": MSE, "score": 1 / get_score(MSE), "generated_vector_used": overfit, } json.dump(json_data, write_file) write_file.write("\n")
def variation_test(index): # mutation_pos = random.random() * 0.05 # mutation_neg = - random.random() * 0.05 mutation_pos = 10**-4 write_file = open("variation.txt", "a") overfit = [] for d in overfit_vector: overfit.append(d) overfit[index] += overfit[index] * mutation_pos MSE = get_errors(overfit) json_data = { "mutation": "+" + str(index), "value": mutation_pos, "MSE": MSE, "score": 1 / get_score(MSE), "generated_vector_used": overfit, } json.dump(json_data, write_file) write_file.write("\n")
def update_fitness( self, real_data: bool = False, fn=lambda train, val: -(train + val + 10 * abs(val - train)) ) -> float: ''' Updating the fitness metric for every generation. ''' global BEST_ERROR global BEST_WEIGHTS train_error, validation_error = client.get_errors( SECRET_KEY, list(self.genes)) # Getting the best error if BEST_ERROR > train_error: BEST_ERROR = train_error BEST_WEIGHTS = self.genes print(self.genes, train_error, validation_error) self.fitness = fn(train=train_error, val=validation_error) return self.fitness
def calculate_fitness(population): rows, cols = population.shape fitness = np.empty((rows, 3)) for i in range(rows): error = C.get_errors(SECRET_KEY, list(population[i])) # calculate fitness from errors this_fit = 0.7 * error[0] + error[1] # 0: train, 1: val fitness[i] = [error[0], error[1], this_fit] # append each vector's fitness to its genes fit_pop = np.column_stack((population, fitness)) # pop sorted by fitness in increasing order fit_pop = fit_pop[np.argsort(fit_pop[:, -1])] # shape of fit_pop: (NUM_POPULATION, NUM_GENES + 3) # The extra 3 per element = err_valid, err_train, fitness return fit_pop
def check_fitness(self): fitness = [] train_errors = [] validation_errors = [] feature_fitness = [] for vector in self.population: file.write("\nsending vector for error: ") file.write(str(vector)) train_error, validation_error = client.get_errors( SECRET_KEY, list(vector)) file.write("\nreceived errors: ") outstr = str(train_error) + " " + str(validation_error) + "\n" file.write(outstr) fit = -(train_error * FITNESS_FACTOR + validation_error) feature_fitness.append((vector, fit)) fitness.append(fit) train_errors.append(train_error) validation_errors.append(validation_error) fitness = np.array(fitness, dtype=np.double) self.vector_fitness = feature_fitness return fitness, train_errors, validation_errors
def main(): parent_data = get_parents(population) # print(parent_data) with open("generation.txt", "a") as write_parent: for line in parent_data: json.dump(line, write_parent) write_parent.write("\n") write_parent.write("\n") probability_of_choosing(parent_data) children = get_children(parent_data) open("parent.txt", "w").close() write_file = open("parent.txt", "a") for i in range(population): MSE = get_errors(children[i]) json_data = { "MSE": MSE, "score": 1 / get_score(MSE), "generated_vector_used": children[i], } json.dump(json_data, write_file) if i != population - 1: write_file.write("\n")
def main(): #initial_vector = [0.0, -1.45799022e-12, -2.28980078e-13, 4.62010753e-11, -1.75214813e-10, -1.83669770e-15, 8.52944060e-16, 2.29423303e-05, -2.04721003e-06, -1.59792834e-08, 9.98214034e-10] #initial_vector = np.array(initial_vector) population = np.zeros((popsize, 11)) original_population = np.zeros((popsize, 11)) errors = np.zeros(popsize) errors1 = np.zeros(popsize) errors2 = np.zeros(popsize) child_errors = np.zeros(k) child_errors1 = np.zeros(k) child_errors2 = np.zeros(k) #generate the initial population #for i in range(popsize): # population[i] = mutate(initial_vector, 0.9, mut_range) population = np.copy([[ -2.1979127752353937e-12, -2.026467870191969e-12, -2.3404723082751514e-13, 4.591932205334458e-11, -5.2509248544757e-11, -1.1790963051945611e-15, 9.574997365652972e-16, 2.3969698057747152e-05, -1.6737692764075557e-06, -1.4590575921693647e-08, 7.766118028201611e-10 ], [ -2.1633841966501003e-12, -2.2545731851188725e-12, -2.423145404694651e-13, 4.806795894791935e-11, -5.3640045912088694e-11, -1.1790963051945611e-15, 1.0022849509638359e-15, 2.396969805774715e-05, -1.673769276407556e-06, -1.4590575921693647e-08, 7.76611802820161e-10 ], [ -2.197447526538208e-12, -2.183491606425423e-12, -2.340620550875386e-13, 4.815374426801597e-11, -5.368519351909754e-11, -1.1790963051945611e-15, 9.529137486438568e-16, 2.396969805774715e-05, -1.6737692764075557e-06, -1.4590575921693646e-08, 7.76611802820161e-10 ], [ -2.3912197686879736e-12, -2.2927152095020196e-12, -2.2879807724889627e-13, 4.79888088518559e-11, -5.550352995543773e-11, -1.1790963051945611e-15, 1.0514653087469206e-15, 2.3969698057747145e-05, -1.673769276407556e-06, -1.4590575921693646e-08, 7.766118028201611e-10 ], [ -2.1981531175953147e-12, -2.0265124359688085e-12, -2.3472698691188633e-13, 4.585548523696718e-11, -5.624523458939281e-11, -1.1790963051945611e-15, 1.0250179624645082e-15, 2.396969805774715e-05, -1.6737692764075557e-06, -1.4590575921693646e-08, 7.76611802820161e-10 ], [ -2.1980409334027405e-12, -2.246531342698275e-12, -2.2505705816718188e-13, 4.801834040536189e-11, -5.6289465463228505e-11, -1.1790963051945611e-15, 1.024663828780703e-15, 2.396969805774715e-05, -1.6737692764075557e-06, -1.4590575921693646e-08, 7.76611802820161e-10 ], [ -2.392340768948038e-12, -2.294260859031194e-12, -2.2876365230167475e-13, 4.800119553963215e-11, -5.5499223405949e-11, -1.1063069572876672e-15, 1.0959073175911582e-15, 2.396969805774715e-05, -1.6737692764075562e-06, -1.4765469346649082e-08, 7.766118028201612e-10 ], [ -2.2146607183639086e-12, -2.2445600804526126e-12, -2.3298832007325577e-13, 4.8080043336093405e-11, -5.3646405755563195e-11, -1.1790963051945611e-15, 9.953301389659054e-16, 2.4906746303516258e-05, -1.6737692764075557e-06, -1.4781464710893071e-08, 7.309512039608373e-10 ], [ -2.0903879868954033e-12, -2.2457776757976275e-12, -2.1562614767156933e-13, 4.801995921621891e-11, -5.6258330282775324e-11, -1.1251184108039062e-15, 9.360478325260698e-16, 2.3969698057747145e-05, -1.6737692764075557e-06, -1.3417703331811415e-08, 7.276540027476406e-10 ], [ -2.207906061715735e-12, -2.0399599203493415e-12, -2.3448116356491637e-13, 4.405033905006161e-11, -5.3394740862778736e-11, -1.16995475047025e-15, 1.0263539751262805e-15, 2.396969805774715e-05, -1.6737692764075557e-06, -1.4590575921693644e-08, 7.172249158312108e-10 ], [ -2.3073643351096137e-12, -2.062506339303927e-12, -2.2811920252484314e-13, 4.625878623629341e-11, -5.268790376814592e-11, -1.1790963051945611e-15, 9.645753742648893e-16, 2.3969698057747152e-05, -1.8260715279260443e-06, -1.4590575921693647e-08, 7.766118028201611e-10 ], [ -2.1926491006489646e-12, -2.1935047110916828e-12, -2.547024548646736e-13, 4.540964556241079e-11, -5.367883367562304e-11, -1.1790963051945611e-15, 9.584517029596301e-16, 2.396969805774715e-05, -1.6737692764075557e-06, -1.4590575921693646e-08, 7.056454933975837e-10 ], [ -2.2870701007174074e-12, -2.2185347160069147e-12, -2.373960366579039e-13, 4.416767921167938e-11, -5.502156681823769e-11, -1.1790963051945611e-15, 9.93828797999289e-16, 2.396969805774715e-05, -1.673769276407556e-06, -1.6022293801109776e-08, 7.219809685236768e-10 ], [ -2.381466824567553e-12, -2.174737518640329e-12, -2.290975817169694e-13, 4.78810419957519e-11, -6.03523777013701e-11, -1.2374723291153843e-15, 1.0501292960851483e-15, 2.226162549360819e-05, -1.8319154095185774e-06, -1.4590575921693644e-08, 7.766118028201611e-10 ], [ -2.197454620970051e-12, -2.18424527332607e-12, -2.5295023986132457e-13, 4.815212545715895e-11, -5.740879185623374e-11, -1.1790963051945611e-15, 9.537715514158083e-16, 2.2852690573656853e-05, -1.6737692764075557e-06, -1.4590575921693646e-08, 8.399968635931893e-10 ], [ -2.1970321173352507e-12, -2.024966786439635e-12, -2.206162461772447e-13, 4.5843098549190934e-11, -5.6249541138881557e-11, -1.2571005763362954e-15, 9.48464857025232e-16, 2.4481390682614638e-05, -1.5141194053745652e-06, -1.4590575921693647e-08, 7.766118028201611e-10 ]]) original_population = np.copy(population) #random.seed() factor = 2000 #print(factor) #print("\n") #generate errors for every individual in population for i in range(popsize): #passing the inidividual to get_errors function err = client.get_errors(key, population[i].tolist()) #for now, error=err[0]+err[1] but might change weightages later errors[i] = np.copy(err[0] + err[1] + factor * (abs(err[0] - err[1]))) errors1[i] = np.copy(err[0]) errors2[i] = np.copy(err[1]) indices = np.zeros(popsize) temp_population = np.zeros((popsize, 11)) temp_errors = np.zeros(popsize) temp_errors1 = np.zeros(popsize) temp_errors2 = np.zeros(popsize) #fitness = 1/error, so sorting in ascending error indices = np.copy(np.argsort(errors)) temp_population = np.copy(population) temp_errors = np.copy(errors) temp_errors1 = np.copy(errors1) temp_errors2 = np.copy(errors2) for i in range(popsize): population[i] = np.copy(temp_population[indices[i]]) errors[i] = np.copy(temp_errors[indices[i]]) errors1[i] = np.copy(temp_errors1[indices[i]]) errors2[i] = np.copy(temp_errors2[indices[i]]) for x in range(iterations): #print("Iteration: ",x) #now parents have been sorted acc to fitness gen_original_pop.append(population) parent1 = np.zeros(11) parent2 = np.zeros(11) child1 = np.zeros(11) child2 = np.zeros(11) x2 = 0 parents = np.zeros((k, 11)) unmutated_children = np.zeros((popsize - k, 11)) #taking top k individuals as parents parents = np.copy(population[:k]) gen_parents.append(parents) child_population = np.zeros((popsize - k, 11)) child_errors = np.zeros(popsize - k) child_errors1 = np.zeros(popsize - k) child_errors2 = np.zeros(popsize - k) gen_crossover_children.append([]) gen_mutated_children.append([]) #generate popsize-k children while (x2 < (popsize - k)): indarr = np.random.choice(k, 2, replace=False, p=[0.3, 0.3, 0.15, 0.1, 0.1, 0.05]) ind1 = indarr[0] ind2 = indarr[1] parent1 = np.copy(parents[ind1]) parent2 = np.copy(parents[ind2]) child1, child2 = crossover(parent1, parent2) gen_crossover_children[-1].append(child1) gen_crossover_children[-1].append(child2) child1 = mutate(child1, mut_prob, mut_range) child2 = mutate(child2, mut_prob, mut_range) gen_mutated_children[-1].append(child1) gen_mutated_children[-1].append(child2) #if any child is same as parent, discard the two children comparison1 = (child1 == parent1) comparison2 = (child2 == parent1) comparison3 = (child1 == parent2) comparison4 = (child2 == parent2) if (comparison1.all() == True or comparison2.all() == True or comparison3.all() == True or comparison4.all() == True): if comparison1 or comparison3: gen_crossover_children[-1].pop(-2) gen_mutated_children[-1].pop(-2) if comparison2 or comparison4: gen_crossover_children.pop() gen_mutated_children.pop() continue child_population[x2] = child1 x2 += 1 child_population[x2] = child2 x2 += 1 #find errors for children for i in range(popsize - k): child_err = client.get_errors(key, child_population[i].tolist()) #for now, error=err[0]+err[1] but might change weightages later child_errors[i] = np.copy(child_err[0] + child_err[1] + factor * (abs(child_err[0] - child_err[1]))) child_errors1[i] = np.copy(child_err[0]) child_errors2[i] = np.copy(child_err[1]) child_indices = np.zeros(popsize - k) temp_child_population = np.zeros((popsize - k, 11)) temp_child_errors = np.zeros(popsize - k) temp_child_errors1 = np.zeros(popsize - k) temp_child_errors2 = np.zeros(popsize - k) #sort children child_indices = np.copy(np.argsort(child_errors)) temp_child_population = np.copy(child_population) temp_child_errors = np.copy(child_errors) temp_child_errors1 = np.copy(child_errors1) temp_child_errors2 = np.copy(child_errors2) for i in range(popsize - k): child_population[i] = np.copy( temp_child_population[child_indices[i]]) child_errors[i] = np.copy(temp_child_errors[child_indices[i]]) child_errors1[i] = np.copy(temp_child_errors1[child_indices[i]]) child_errors2[i] = np.copy(temp_child_errors2[child_indices[i]]) new_population = np.zeros((popsize, 11)) new_errors = np.zeros(popsize) new_errors1 = np.zeros(popsize) new_errors2 = np.zeros(popsize) #take all popsize-k children, and the top k parents for i in range(popsize - k): new_population[i] = np.copy(child_population[i]) new_errors[i] = np.copy(child_errors[i]) new_errors1[i] = np.copy(child_errors1[i]) new_errors2[i] = np.copy(child_errors2[i]) for i in range(k): new_population[popsize - k + i] = np.copy(population[i]) new_errors[popsize - k + i] = np.copy(errors[i]) new_errors1[popsize - k + i] = np.copy(errors1[i]) new_errors2[popsize - k + i] = np.copy(errors2[i]) population = np.copy(new_population) errors = np.copy(new_errors) errors1 = np.copy(new_errors1) errors2 = np.copy(new_errors2) indices = np.copy(np.argsort(errors)) temp_population = np.copy(population) temp_errors = np.copy(errors) temp_errors1 = np.copy(errors1) temp_errors2 = np.copy(errors2) for i in range(popsize): population[i] = np.copy(temp_population[indices[i]]) errors[i] = np.copy(temp_errors[indices[i]]) errors1[i] = np.copy(temp_errors1[indices[i]]) errors2[i] = np.copy(temp_errors2[indices[i]]) print("Min error: ", errors1[0] + errors2[0]) print("Train error: ", errors1[0]) print("Val error: ", errors2[0]) print("Diff: ", abs(errors1[0] - errors2[0])) print("\n") dict = { "population": population.tolist(), "top_train_error": errors1[0].tolist(), "top_val_error": errors2[0].tolist() } with open("dict55.json", "a") as f: json.dump(dict, f) final_vector = np.copy(population[0]) return final_vector
def getError(genes): if conf.TEST: return [random.uniform(0, 10000), random.uniform(0, 10000)] return get_errors(secrets.KEY, genes.tolist())
def fitness_function(generation): fitness = [] for i in generation: fitness_per_individual = cl.get_errors(key,np.ndarray.tolist(i)) fitness.append(fitness_per_individual) return fitness
wt2 = [ 9.913269136508803e-19, -1.3651432220474988e-12, -2.298198393159188e-13, 5.0168495585679045e-11, -1.9142692264536784e-10, -5.708139268721448e-16, 9.222307658340838e-16, 3.2077307818350485e-05, -2.1023487765642816e-06, -1.3862270237706852e-08, 8.640892314427145e-10 ] wt2 = [ 9.915672591936594e-19, -1.3654972148183978e-12, -2.3013915250437e-13, 5.0202893139928207e-11, -1.9197681316495378e-10, -5.695747268129501e-16, 9.247415970198904e-16, 3.204237015306432e-05, -2.0963811355424884e-06, -1.3922304080398685e-08, 8.619693246341732e-10 ] # wt2 = [9.919603226020886e-19, -1.3643039330187416e-12, -2.300025187475665e-13, 5.01420607948874e-11, -1.9171213875705204e-10, -5.704916302387945e-16, 9.249323842424032e-16, 3.2018325502112234e-05, -2.092484885878187e-06, -1.3842788049128266e-08, 8.640308071699504e-10] # wt2 = [1.0152006490722198e-18, -1.2675221413533424e-12, -2.219456862667535e-13, 5.2746185700513954e-11, -2.048741688083159e-10, -5.906269772197416e-16, 9.464899300647767e-16, 3.105910784304104e-05, -2.055769267289396e-06, -1.6494805651419324e-08, 9.224439203791275e-10] status = server.submit(TEAM_ID, list(wt2)) print(status) # for i in range(10): # weights[7] -= 0.02e-05 train_err, valid_err = server.get_errors(TEAM_ID, list(wt2)) tot_err = train_err + valid_err # print(wt2) print('fitness:') print("{:e}".format(-tot_err), "{:e}".format(train_err), "{:e}".format(valid_err)) # INITIAL_WEIGHTS = [1.0016758736507022e-18, -1.3696155264603411e-12, -2.300768584393704e-13, 4.617028826499339e-11, -1.7627848513209744e-10, -1.7730847899381538e-15, 8.38639892842589e-16, 2.2778568625222342e-05, -1.9784050209132108e-06, -1.5846641527483793e-08, 9.522475355911996e-10]
def main(): POPULATION_SIZE = 16 CHILDREN_SIZE = 20 # CREATE MATING POOL ON ITS OWN mating_pool = hand_picked() for generation in range(1, 11): print("Generation", generation) gen_here = {"crossed_over": [], "mutated": [], "selected": []} # SELECT PARENTS parents = get_mating_pool(POPULATION_SIZE, CHILDREN_SIZE) # DO CROSSOVER OF PARENTS children = [] for i in range(len(parents)): child1, child2 = BSC( np.array(mating_pool[parents[i][0]]["vector"]), np.array(mating_pool[parents[i][1]]["vector"]), N=min(6, 3 + generation / 10)) children.append({ "child": child1, "parents": [mating_pool[parents[i][0]], mating_pool[parents[i][1]]] }) children.append({ "child": child2, "parents": [mating_pool[parents[i][0]], mating_pool[parents[i][1]]] }) gen_here["crossed_over"].append(child1.tolist()) gen_here["crossed_over"].append(child2.tolist()) # # DO MUTATIONS ON CHILDREN for i in range(len(children)): children[i]["child"] = mutate(children[i]["child"], generation) gen_here["mutated"].append(children[i]["child"].tolist()) # GET ERRORS errors = [] for child in children: # res = [0, 0] res = get_errors(TEAM_KEY, child["child"].tolist()) print(res[0] / 1e11, res[1] / 1e11, fitness(res)) child["child"] = { "vector": child["child"].tolist(), "results": res } errors.append({"vector": child, "results": res}) # ADD CHILDREN TO LIST with open("new_new_gen_9.json") as f: oldres = json.load(f) oldres = oldres["generations"] oldres.append({"generation": generation, "vectors": errors}) oldres = {"generations": oldres} with open("new_new_gen_9.json", "w") as f: json.dump(oldres, f) # SELECT BEST CHILDREN mating_pool = select_from_children(children, POPULATION_SIZE, CHILDREN_SIZE) for mat in mating_pool: gen_here["selected"].append(mat["vector"]) with open("generations/generation_" + str(generation) + ".txt", "w+") as f: json.dump(gen_here, f)