def cal_pop_fitness(pop): fitness = [get_errors(team_secret_key, list(i)) for i in pop] # fitness = [[i,2*i] for i in range(len(pop))] fitness = [abs(i[0] - i[1]) for i in fitness] # fitness = [i[0]*0.3+i[1] for i in fitness] # print(fitness) return fitness
def naturalSelection(population, populationSize, private_key): prevGenFitnessTrain = [] prevGenFitnessValidation = [] for i in range(0, populationSize): temp = client_moodle.get_errors(private_key, population[i]) print( str(i) + "th element's error for train and validation are " + str(temp[0]) + " " + str(temp[1])) prevGenFitnessTrain.append(temp[0]) prevGenFitnessValidation.append(temp[1]) # trainGuidelineUpper = 3625792 # trainGuidelineLower = 79569 # validationGuidelineUpper = 3625792 population = [ x for _, x in sorted(zip(prevGenFitnessValidation, population)) ] sortedFitnessValArray = sorted(prevGenFitnessValidation) fittestIndividualsForDirect = [ i for i in range(0, int(populationSize / 10)) ] fittestIndividualsForCrossing = [ i for i in range(0, int(populationSize / 5)) ] return fittestIndividualsForDirect, fittestIndividualsForCrossing, sortedFitnessValArray
def foo(ar, kee=kee): f = open("responses.txt", "a") cap = get_errors(kee, ar) f.write('Got this!' + str(cap) + ' ' + str(cap[0]+cap[1])) f.write(str(ar)) f.write('\n') print(str(cap) + ' ' + str(cap[0]+cap[1])) f.close() return cap
def cal_error(self): # err = sample_err(self.params) err = get_errors(team_id, list(self.params)) data_collected.append((list(self.params), list(err))) # print(data_collected) # data.append((list(self.params), err)) # outfile.seek(0) # json.dump(data, outfile, indent=2) return -np.mean(err)
def cal_pop_fitness( pop): # Calculating the fitness value of each solution in the current population. # The fitness function caulcuates the sum of products between each input and its corresponding weight. # fitness = numpy.sum(pop*equation_inputs, axis=1) # print("IN HERE") fitness =[] trainVals=[] validVals=[] for i in range(0,pop.shape[0]): a=list(client_moodle.get_errors('bTcDQRvCITniLCqT38zzd4CaYs8gPsrnjxyZyIVTiTv6DyX0kX',list(pop[i]) )) trainVals.append(a[0]) validVals.append(a[1]) #TANMAYS print("Individual :", str(i), "has errors", str(a[0]), str(a[1]),"has fitness\t\t", pow(a[0], 2)*pow(a[1], 5))
def get_best(pop1, best1, arr1, top1): res = [] w = [] for i in range(len(pop1)): res = cli.get_errors( 'jcikTU98ZdeaUH5uBHsOPXzXAzAhBVdwtDDj7SqoF98mqbjZLw', pop1[i]) if (res[0] + 0 * res[1]) < (best[0] + 0 * best1[1]) and res[ 0] <= 1.5 * res[1] and res[1] <= 1.5 * res[0]: arr1 = pop1 top1 = pop1[i] best1 = res print(f"Train Error : {best1[0]} , Validation Error : {best1[1]}") print(pop[i], file=check) w.append(res[0] + res[1] * 0) zipped_pairs = zip(w, pop1) z = [a for _, a in sorted(zipped_pairs)] pop1 = z return pop1, best1, arr1, top1
import numpy as np import random import statistics import geneticFunctions populationSize = 10 f = open("overfit.txt", "r+") num = f.read() num = num.replace("[", " ") num = num.replace("]", " ") num = num.strip() num = num.split(", ") for i in range(0, len(num)): num[i] = float(num[i]) private_key = "JVlzF9h4oeN3fyaOoSYgA1HiW82SlS1iptEqtB4lDQAeCK2k8C" ret_val = client_moodle.get_errors(private_key, num) ret_val2 = client_moodle.submit(private_key, num) print(ret_val, ret_val2) population = [] for i in range(0, populationSize): temp = [] for i in range(0, 11): temp.append(random.uniform(-10, 10)) population.append(temp) nextGenPopulation = [] print("Generation: 0", end=" ") for i in range(1, 10): # for i in range(0, populationSize): # print(type(population[i])) fittestIndividualsForDirect, fittestIndividualsForCrossing, sortedFitnessValArray = geneticFunctions.naturalSelection(
print("Generation: {}".format(generation)) child_errors = [] chromosome_probability = gen_mating_pool_probab(fitness) for i in range(child_number): print("\nChild: {}".format(i)) child = crossover(select_mates(chromosome_probability, chromosomes)) print("Child: {}".format(child)) if random.randint(0, 1): mutate_child = child else: mutate_child = mutate(child) err = client_moodle.get_errors( '32EZBpTMqjBF5XByc7riOKvbIw2EykjhBEUqjDSAA9geHjqTaW', list(mutate_child)) submit_status = client_moodle.submit( '32EZBpTMqjBF5XByc7riOKvbIw2EykjhBEUqjDSAA9geHjqTaW', list(mutate_child)) mean_error = find_mean_error(err[0], err[1]) min_training_error = min(min_training_error, err[0]) min_validation_error = min(min_validation_error, err[1]) min_error = min(min_error, mean_error) child_errors.append([mean_error, err[0], err[1], child]) random_values = numpy.array(random.sample(range(population_size), 5)) for index in random_values:
# new_population[parents.shape[0]:, :] = offspring_mutation new_population = ga.create_new_pop(parents, offspring_mutation, num_parents_to_take, num_children_to_take) # The best result in the current iteration. # print("Best result : ", numpy.max(numpy.sum(new_population*equation_inputs, axis=1))) # Getting the best solution after iterating finishing all generations. #At first, the fitness is calculated for each solution in the final generation. print("final:", new_population.tolist()) print("final fitness:", fitness) # print(fitness) # Then return the index of that solution corresponding to the best fitness. best_match_idx = numpy.where(fitness == numpy.min(fitness)) print("best match!", best_match_idx) # print("Best_match_index:", best_match_idx) ans = list(new_population[best_match_idx, :][0][0]) # ans = list(new_population[best_match_idx]) print("Best solution : ", ans) # print("Best solution fitness : ", list(fitness[best_match_idx])) p = list( client_moodle.get_errors( 'bTcDQRvCITniLCqT38zzd4CaYs8gPsrnjxyZyIVTiTv6DyX0kX', list(new_population[best_match_idx, :][0][0]))) print("Best solution fitness : ", p) client_moodle.submit('bTcDQRvCITniLCqT38zzd4CaYs8gPsrnjxyZyIVTiTv6DyX0kX', ans)
def main(): print("PC: ", pc, " POP_SIZE: ", pop_size, " ITER : ", iter) vector_og = [ 0.0, 0.1240317450077846, -6.211941063144333, 0.04933903144709126, 0.03810848157715883, 8.132366097133624e-05, -6.018769160916912e-05, -1.251585565299179e-07, 3.484096383229681e-08, 4.1614924993407104e-11, -6.732420176902565e-12 ] # vector_og=[-9.78736351e+00 ,-6.30079234e+00 ,-5.86904268e+00 , 4.93390314e-02,3.81084816e-02 , 8.13236610e-05, -6.01876916e-05, -1.25158557e-07,3.48409638e-08, 4.16149250e-11, -6.73242018e-12] to_send = [-20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20] min_error = -1 min_error1 = -1 min_error2 = -1 parenterrors = np.zeros(pop_size) parenterrors1 = np.zeros(pop_size) parenterrors2 = np.zeros(pop_size) parentprobalities = np.zeros(pop_size) population = np.zeros((pop_size, MAX_DEG)) # generate the population for i in range(pop_size): temp = np.copy(vector_og) population[i] = mutateall(temp) #generate errors for each individual in the population for j in range(pop_size): temp = population[j].tolist( ) #passing a list to the get_errors function err = server.get_errors(key, temp) #adding the two errors and storing in parenterror parenterrors[j] = (err[0] + err[1]) parenterrors1[j] = (err[0]) parenterrors2[j] = (err[1]) bank = np.copy(population) bankerrors = np.copy(parenterrors) bankerrors1 = np.copy(parenterrors1) bankerrors2 = np.copy(parenterrors2) # have to change this to a while loop with appropriate condition later for iter_num in range(iter): new_iter = 0 print("\n\n\n\n********" + str(iter_num) + "*********") # Sort the errors in ascending order # Least error => max fittness # Correspondingly sort the population also parenterrorsinds = parenterrors.argsort() parenterrors = parenterrors[parenterrorsinds[::1]] parenterrors1 = parenterrors1[parenterrorsinds[::1]] parenterrors2 = parenterrors2[parenterrorsinds[::1]] population = population[parenterrorsinds[::1]] #debug statements for j in range(pop_size): print("person " + str(j) + " error " + str(parenterrors[j])) print("\tvalues" + str(population[j]) + "\n\n") # Assign probabilities to the population parentprobalities = gen_parent_probabilities(pop_size) # Checking sum(prob) = 1 # print(np.sum(parentprobalities)) child_population = np.zeros((pop_size, MAX_DEG)) #perform crossover cross_n times for j in range(cross_n): arr = crossover_select(parentprobalities) # Two parents chosen based on probabilities => arr[0], arr[1] # Sending parents for crossover temp = crossover(population[arr[0]], population[arr[1]]) child_population[new_iter] = temp[0] new_iter += 1 child_population[new_iter] = temp[1] new_iter += 1 # print("BEFORE MUTATION: CHILD POPULATION") # for j in range(len(child_population)): # print("Child", j) # print(child_population[i]) # Send the new population for mutation for j in range(pop_size): temp = np.copy(child_population[j]) child_population[j] = mutation(temp) # print("AFTER MUTATION: CHILD POPULATION") # for j in range(len(child_population)): # print("Child", j) # print(child_population[i]) # get the errors for the new population childerrors = np.zeros(pop_size) childerrors1 = np.zeros(pop_size) childerrors2 = np.zeros(pop_size) # generate errors for each child for j in range(pop_size): temp = child_population[j].tolist( ) #passing a list to the get_errors function err = server.get_errors(key, temp) #adding the two errors and storing in parnterror childerrors[j] = (err[0] + err[1]) childerrors1[j] = (err[0]) childerrors2[j] = (err[1]) #combining parents and children into one array candidates = np.concatenate([population, child_population]) candidate_errors = np.concatenate([parenterrors, childerrors]) candidate_errors1 = np.concatenate([parenterrors1, childerrors1]) candidate_errors2 = np.concatenate([parenterrors2, childerrors2]) # sorting all the candidates by error candidateerrorsinds = candidate_errors.argsort() candidate_errors = candidate_errors[candidateerrorsinds[::1]] candidate_errors1 = candidate_errors1[candidateerrorsinds[::1]] candidate_errors2 = candidate_errors2[candidateerrorsinds[::1]] candidates = candidates[candidateerrorsinds[::1]] # setting the probability and choosing the indices # candidate_prob=gen_parent_probabilities(pop_size*2) # chosenindices=np.random.choice(np.arange(0,2*pop_size),pop_size, replace=False,p=candidate_prob) # set population for the next iteration ind = 0 for i in range(2 * pop_size): # ind=chosenindices[i] if ind >= pop_size: break checkflag = 0 for j in range(ind): # print(i,j,ind) # print(population[i],population[j]) if check_match(candidates[i], population[j]) == 1: checkflag = 1 break if (checkflag == 0): population[ind] = candidates[i] parenterrors[ind] = candidate_errors[i] parenterrors1[ind] = candidate_errors1[i] parenterrors2[ind] = candidate_errors2[i] ind += 1 if (ind < pop_size): print("choosing from the bank from index " + str(ind)) i = 0 while (ind < pop_size): population[ind] = bank[i] parenterrors[ind] = bankerrors[i] parenterrors1[ind] = bankerrors1[i] parenterrors2[ind] = bankerrors2[i] ind += 1 i += 1 # set the send array by choosing the minimum from all the candidates NOTE: it may not be selected in the new population if (min_error == -1 or min_error > candidate_errors[0]): to_send = candidates[0] min_error = candidate_errors[0] min_error1 = candidate_errors1[0] min_error2 = candidate_errors2[0] print( "-------------------------------------------------------------------------------\n" ) print("Min error = ", min_error, "\n\n") print("Min error1 = ", min_error1, "\n\n") print("Min error2 = ", min_error2, "\n\n") if (min_error < 40000000): print( "sending\n\n" + str(to_send) + "\n\nwas it successfully submitted?", server.submit(key, to_send.tolist())) return to_send
return np.array(to_ret) num_weights = 11 population = 40 num_parents_mating = 15 fil = open('lulli.txt', 'r') model = fil.readline() model = model.strip('[]').split(',') model = [float(i) for i in model] error(get_errors(team_secret_key, model)) prev_error = cal_pop_fitness([model]) print(prev_error) #model = [ 0.00000000e+00, 1.28200354e-01, -6.05800043e+00, 5.29444159e-02, # 3.63051580e-02, 7.99636168e-05, -5.97183727e-05, -1.33975300e-07, # 3.54504234e-08, 4.36850525e-11, -6.90589558e-12] new_population = np.array([distort(model) for i in range(population)]) # generations to train for num_generations = 20 for generation in range(num_generations): print("Generation : ", generation) fitness = cal_pop_fitness(new_population) parents = select_mating_pool(new_population, fitness, num_parents_mating)
def main(): w1=0.2 w2=0.8 print("PC: " ,pc, " POP_SIZE: ",pop_size," ITER : ", iter, "w1: ",w1,"w2: ",w2, "Stop : ", "50") vector_og=[0.0, 0.1240317450077846, -6.211941063144333, 0.04933903144709126, 0.03810848157715883, 8.132366097133624e-05, -6.018769160916912e-05, -1.251585565299179e-07, 3.484096383229681e-08, 4.1614924993407104e-11, -6.732420176902565e-12] # vector_og=[-9.78736351e+00 ,-6.30079234e+00 ,-5.86904268e+00 , 4.93390314e-02,3.81084816e-02 , 8.13236610e-05, -6.01876916e-05, -1.25158557e-07,3.48409638e-08, 4.16149250e-11, -6.73242018e-12] to_send=[-20,-20,-20,-20,-20,-20,-20,-20,-20,-20,-20] min_error=-1 min_error1=-1 min_error2=-1 parenterrors=np.zeros(pop_size) parenterrors1=np.zeros(pop_size) parenterrors2=np.zeros(pop_size) parentprobalities=np.zeros(pop_size) population=np.zeros((pop_size,MAX_DEG)) # generate the population for i in range(pop_size): temp=np.copy(vector_og) population[i]=mutateall(temp) #generate errors for each individual in the population for j in range(pop_size): temp=population[j].tolist() #passing a list to the get_errors function err=server.get_errors(key,temp) #adding the two errors and storing in parenterror parenterrors[j]=(w1*err[0]+w2*err[1]) parenterrors1[j]=(err[0]) parenterrors2[j]=(err[1]) # have to change this to a while loop with appropriate condition later for iter_num in range(iter): new_iter=0 if(iter_num > 50 ): w1=1 w2=1 print("\n\n\n\n********"+str(iter_num)+"*********") # Sort the errors in ascending order # Least error => max fittness # Correspondingly sort the population also # arrinds=sortfunc(parenterrors,parenterrors1,parenterrors2) parenerrorsinds=parenterrors.argsort() parenterrors=parenterrors[parenerrorsinds[::1]] parenterrors1=parenterrors1[parenerrorsinds[::1]] parenterrors2=parenterrors2[parenerrorsinds[::1]] population=population[parenerrorsinds[::1]] #debug statements for j in range(pop_size): print("person " + str(j)+" error "+ str(parenterrors[j])) print("\tvalues"+str(population[j])+"\n\n") # Assign probabilities to the population parentprobalities=gen_parent_probabilities(pop_size) # Checking sum(prob) = 1 # print(np.sum(parentprobalities)) child_population=np.zeros((pop_size,MAX_DEG)) #perform crossover cross_n times for j in range (cross_n): arr=crossover_select(parentprobalities) # Two parents chosen based on probabilities => arr[0], arr[1] # Sending parents for crossover temp=crossover(population[arr[0]],population[arr[1]]) child_population[new_iter]=temp[0] new_iter+=1 child_population[new_iter]=temp[1] new_iter+=1 # Send the new population for mutation for j in range(pop_size): temp=np.copy(child_population[j]) child_population[j]=mutation(temp) # get the errors for the new population childerrors=np.zeros(pop_size) childerrors1=np.zeros(pop_size) childerrors2=np.zeros(pop_size) # generate errors for each child for j in range(pop_size): temp=child_population[j].tolist() #passing a list to the get_errors function err=server.get_errors(key,temp) #adding the two errors and storing in parenterror childerrors[j]=(w1*err[0]+w2*err[1]) childerrors1[j]=(err[0]) childerrors2[j]=(err[1]) #combining parents and children into one array candidates=np.concatenate([population,child_population]) candidate_errors=np.concatenate([parenterrors,childerrors]) candidate_errors1=np.concatenate([parenterrors1,childerrors1]) candidate_errors2=np.concatenate([parenterrors2,childerrors2]) # sorting all the candidates by error # arrinds=sortfunc(candidate_errors,candidate_errors1, candidate_errors2) candidate_errors_inds=candidate_errors.argsort() candidate_errors=candidate_errors[candidate_errors_inds[::1]] candidate_errors1=candidate_errors1[candidate_errors_inds[::1]] candidate_errors2=candidate_errors2[candidate_errors_inds[::1]] candidates=candidates[candidate_errors_inds[::1]] # set population for the next iteration by selecting the best k in the candidaes for i in range(pop_size): population[i]=candidates[i] parenterrors[i]=candidate_errors[i] parenterrors1[i]=candidate_errors1[i] parenterrors2[i]=candidate_errors2[i] # set the send array by choosing the minimum from all the candidates NOTE: it may not be selected in the new population if(min_error==-1 or min_error>candidate_errors[0]): to_send=candidates[0] min_error=candidate_errors[0] min_error1=candidate_errors1[0] min_error2=candidate_errors2[0] print("-------------------------------------------------------------------------------\n") print("Min error = ", min_error,"\n\n") print("Min error1 = ", min_error1,"\n\n") print("Min error2 = ", min_error2,"\n\n") if(min_error<40000000): print("sending\n\n"+str(to_send)+"\n\nwas it successfully submitted?", server.submit(key,to_send.tolist())) return to_send
def main(): mutate_range = 0.1 prob_mut_cross = 0.9 print("pop_size:", pop_size, "iter:", iter, "cross_select_from", cross_select_from) print("select_sure", select_sure, "prob_mut_cross", prob_mut_cross, "mutate_range", mutate_range) vector_og = [ -1e-19, 0.1240317450077846, -6.211941063144333, 0.04933903144709126, 0.03810848157715883, 8.132366097133624e-05, -6.018769160916912e-05, -1.251585565299179e-07, 3.484096383229681e-08, 4.1614924993407104e-11, -6.732420176902565e-12 ] to_send = [-20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20] min_error = -1 min_error1 = -1 min_error2 = -1 parenterrors = np.zeros(pop_size) parenterrors1 = np.zeros(pop_size) parenterrors2 = np.zeros(pop_size) population = np.zeros((pop_size, MAX_DEG)) # generate the population for i in range(pop_size): temp = np.copy(vector_og) population[i] = np.copy(mutateall(temp, 0.85, mutate_range)) # generate errors for each individual in the population for j in range(pop_size): # passing a list to the get_errors function temp = population[j].tolist() err = server.get_errors(key, temp) # adding the two errors and storing in parenterror - fitness function parenterrors[j] = np.copy((err[0] + 1.5 * err[1])) # parenterrors[j] = np.copy((err[0]+err[1])) parenterrors1[j] = np.copy((err[0])) parenterrors2[j] = np.copy((err[1])) # have to change this to a while loop with appropriate condition later for iter_num in range(iter): if ((iter_num) % 6 == 0 and iter_num != 0): if (mutate_range < 0.01): mutate_range = 0.01 else: mutate_range -= 0.01 if (prob_mut_cross >= 1): prob_mut_cross = 1 else: prob_mut_cross += 0.01 print("::::::::::::::::changing ", mutate_range, prob_mut_cross) #has popsize/2 pairs, each is a set of parents used to generate two children arrchoices = np.zeros((int(pop_size / 2), 2)) arrposswap = np.zeros((int(pop_size / 2), 5)) #has the array of all the children arrchildren = np.zeros((pop_size, MAX_DEG)) arrchildrenmutated = np.zeros((pop_size, MAX_DEG)) arrchilderrors = np.zeros((pop_size)) print("\n\n\n\n********" + str(iter_num) + "*********") parenerrorsinds = parenterrors.argsort() parenterrors = np.copy(parenterrors[parenerrorsinds[::1]]) parenterrors1 = np.copy(parenterrors1[parenerrorsinds[::1]]) parenterrors2 = np.copy(parenterrors2[parenerrorsinds[::1]]) population = np.copy(population[parenerrorsinds[::1]]) #parents with their errors arrparents = np.copy(population) arrparrerrs = np.copy(parenterrors) # debug statements for j in range(pop_size): print("person " + str(j) + " errorfunc" + str(parenterrors[j])) print("person " + str(j) + " error1" + str(parenterrors1[j])) print("person " + str(j) + " error2" + str(parenterrors2[j])) print("\tvalues" + str(population[j]) + "\n\n") child_population = np.zeros((pop_size, MAX_DEG)) new_iter = 0 while (new_iter < pop_size): # arr = crossover_select(parentprobalities) # TODO: Select randomly among top k parents (For now k =10) arr = crossover_select2(parenterrors, cross_select_from) # Sending parents for crossover temp = crossover(population[arr[0]], population[arr[1]], mutate_range, prob_mut_cross) if temp[0].tolist() == population[arr[0]].tolist( ) or temp[1].tolist() == population[arr[0]].tolist( ) or temp[0].tolist() == population[arr[1]].tolist( ) or temp[1].tolist() == population[arr[1]].tolist(): continue arrchoices[int(new_iter / 2)][0] = np.copy(arr[0]) arrchoices[int(new_iter / 2)][1] = np.copy(arr[1]) arrposswap[int(new_iter / 2)] = np.copy(np.sort(temp[4])) arrchildren[new_iter] = np.copy(temp[2]) arrchildrenmutated[new_iter] = np.copy(temp[0]) child_population[new_iter] = np.copy(temp[0]) new_iter += 1 arrchildren[new_iter] = np.copy(temp[3]) arrchildrenmutated[new_iter] = np.copy(temp[1]) child_population[new_iter] = np.copy(temp[1]) new_iter += 1 childerrors = np.zeros(pop_size) childerrors1 = np.zeros(pop_size) childerrors2 = np.zeros(pop_size) # generate errors for each child for j in range(pop_size): temp = child_population[j].tolist() err = server.get_errors(key, temp) # adding the two errors and storing in parenterror if (iter_num >= 25): childerrors[j] = np.copy((err[0] + err[1])) else: childerrors[j] = np.copy((err[0] + 1.5 * err[1])) childerrors1[j] = np.copy((err[0])) childerrors2[j] = np.copy((err[1])) arrchilderrors[j] = np.copy(childerrors[j]) # Sort children childinds = np.copy(childerrors.argsort()) childerrors = np.copy(childerrors[childinds[::1]]) childerrors1 = np.copy(childerrors1[childinds[::1]]) childerrors2 = np.copy(childerrors2[childinds[::1]]) child_population = np.copy(child_population[childinds[::1]]) # TODO: Select the best select_sure number of parents and chilren [select these many parents and children for sure] # now the children are sorted and stored in child and parents are sorted in population # we will now create a tempbank array to store top k parents, top k childs and rest being sorted taking from the top tempbankerr = np.zeros(pop_size) tempbankerr1 = np.zeros(pop_size) tempbankerr2 = np.zeros(pop_size) tempbank = np.zeros((pop_size, MAX_DEG)) for j in range(select_sure): #choosing the top jth parent and putting in the array tempbank[j] = np.copy(population[j]) tempbankerr[j] = np.copy(parenterrors[j]) tempbankerr1[j] = np.copy(parenterrors1[j]) tempbankerr2[j] = np.copy(parenterrors2[j]) #choosing the top jth child and putting it into the array tempbank[j + select_sure] = np.copy(child_population[j]) tempbankerr[j + select_sure] = np.copy(childerrors[j]) tempbankerr1[j + select_sure] = np.copy(childerrors1[j]) tempbankerr2[j + select_sure] = np.copy(childerrors2[j]) # combining parents and children into one array # TODO: Concatenating remaining parents and children and selecting from them candidates = np.copy( np.concatenate( [population[select_sure:], child_population[select_sure:]])) candidate_errors = np.copy( np.concatenate( [parenterrors[select_sure:], childerrors[select_sure:]])) candidate_errors1 = np.copy( np.concatenate( [parenterrors1[select_sure:], childerrors1[select_sure:]])) candidate_errors2 = np.copy( np.concatenate( [parenterrors2[select_sure:], childerrors2[select_sure:]])) # sorting all the candidates by error candidate_errors_inds = candidate_errors.argsort() candidate_errors = np.copy( candidate_errors[candidate_errors_inds[::1]]) candidate_errors1 = np.copy( candidate_errors1[candidate_errors_inds[::1]]) candidate_errors2 = np.copy( candidate_errors2[candidate_errors_inds[::1]]) candidates = np.copy(candidates[candidate_errors_inds[::1]]) # TODO: Select the best popsize - 2*(select_sure) cand_iter = 0 while (cand_iter + 2 * select_sure < pop_size): tempbank[cand_iter + 2 * select_sure] = np.copy( candidates[cand_iter]) tempbankerr[cand_iter + 2 * select_sure] = np.copy( candidate_errors[cand_iter]) tempbankerr1[cand_iter + 2 * select_sure] = np.copy( candidate_errors1[cand_iter]) tempbankerr2[cand_iter + 2 * select_sure] = np.copy( candidate_errors2[cand_iter]) cand_iter += 1 #now setting the next population population = np.copy(tempbank) parenterrors = np.copy(tempbankerr) parenterrors1 = np.copy(tempbankerr1) parenterrors2 = np.copy(tempbankerr2) #we will now sort before updating min_error parenerrorsinds = parenterrors.argsort() parenterrors = np.copy(parenterrors[parenerrorsinds[::1]]) parenterrors1 = np.copy(parenterrors1[parenerrorsinds[::1]]) parenterrors2 = np.copy(parenterrors2[parenerrorsinds[::1]]) population = np.copy(population[parenerrorsinds[::1]]) if (min_error == -1 or min_error > parenterrors[0]): to_send = np.copy(population[0]) min_error = np.copy(parenterrors[0]) min_error1 = np.copy(parenterrors1[0]) min_error2 = np.copy(parenterrors2[0]) nochange = 0 else: print("no improvement!!!") nochange += 1 print( "-------------------------------------------------------------------------------\n" ) print("Min error = ", min_error, "\n\n") print("Min error1 = ", min_error1, "\n\n") print("Min error2 = ", min_error2, "\n\n") showtable(arrparents, arrparrerrs, arrchoices, arrchildren, arrchildrenmutated, arrchilderrors, arrposswap) return to_send
def main(): vector_og = [ 0.0, 0.1240317450077846, -6.211941063144333, 0.04933903144709126, 0.03810848157715883, 8.132366097133624e-05, -6.018769160916912e-05, -1.251585565299179e-07, 3.484096383229681e-08, 4.1614924993407104e-11, -6.732420176902565e-12 ] to_send = [-20, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20] min_error = -1 min_error1 = -1 min_error2 = -1 parenterrors = np.zeros(pop_size) parenterrors1 = np.zeros(pop_size) parenterrors2 = np.zeros(pop_size) parentprobalities = np.zeros(pop_size) population = np.zeros((pop_size, MAX_DEG)) # generate the population for i in range(pop_size): temp = np.copy(vector_og) population[i] = mutateall(temp) # have to change this to a while loop with appropriate condition later for i in range(iter): new_iter = 0 new_population = np.zeros((pop_size, MAX_DEG)) #generate errors for each individual in the population print("\n\n\n\n********" + str(i) + "*********") for j in range(pop_size): temp = population[j].tolist( ) #passing a list to the get_errors function err = server.get_errors(key, temp) #adding the two errors and storing in parenterror parenterrors[j] = (err[0] + err[1]) parenterrors1[j] = (err[0]) parenterrors2[j] = (err[1]) # Sort the errors in ascending order # Least error => max fittness # Correspondingly sort the population also parenterrorsinds = parenterrors.argsort() parenterrors = parenterrors[parenterrorsinds[::1]] parenterrors1 = parenterrors1[parenterrorsinds[::1]] parenterrors2 = parenterrors2[parenterrorsinds[::1]] population = population[parenterrorsinds[::1]] #set the send array if (min_error == -1 or min_error > parenterrors[0]): to_send = population[0] min_error = parenterrors[0] min_error1 = parenterrors1[0] min_error2 = parenterrors2[0] #debug statements for j in range(pop_size): print("person " + str(j) + " error " + str(parenterrors[j])) print("\tvalues" + str(population[j]) + "\n\n") # Assign probabilities to the population parentprobalities = gen_parent_probabilities() # Checking sum(prob) = 1 # print(np.sum(parentprobalities)) #perform crossover cross_n times for j in range(cross_n): arr = crossover_select(parentprobalities) # Two parents chosen based on probabilities => arr[0], arr[1] # Sending parents for crossover temp = crossover(population[arr[0]], population[arr[1]]) #select from the two parents and the two children the ones with min val and train error childerror0 = server.get_errors(key, temp[0]) childerror1 = server.get_errors(key, temp[1]) # new_iter is the iterator for the new_population numpy min1 = min(childerror0[0], childerror1[0], parenterrors1[arr[0]], parenterrors1[arr[1]]) min2 = min(childerror0[1], childerror1[1], parenterrors2[arr[0]], parenterrors2[arr[1]]) if min1 == childerror0[0]: new_population[new_iter] = temp[0] elif min1 == childerror1[0]: new_population[new_iter] = temp[1] elif min1 == parenterrors1[arr[0]]: new_population[new_iter] = population[arr[0]] elif min1 == parenterrors1[arr[1]]: new_population[new_iter] = population[arr[1]] new_iter += 1 if min2 == childerror0[1]: new_population[new_iter] = temp[0] elif min2 == childerror1[1]: new_population[new_iter] = temp[1] elif min2 == parenterrors2[arr[0]]: new_population[new_iter] = population[arr[0]] elif min2 == parenterrors2[arr[1]]: new_population[new_iter] = population[arr[1]] new_iter += 1 # Send the new population for mutation for j in range(pop_size): temp = np.copy(new_population[j]) new_population[j] = mutation(temp) # print(new_population) population = np.copy(new_population) print( "-------------------------------------------------------------------------------\n" ) print("Min error = ", min_error, "\n\n") print("Min error1 = ", min_error1, "\n\n") print("Min error2 = ", min_error2, "\n\n") return to_send
import client_moodle as cli, pickle fname = 'topvec.pkl' f = open(fname, 'rb') vector = pickle.load(f) res1 = cli.get_errors('jcikTU98ZdeaUH5uBHsOPXzXAzAhBVdwtDDj7SqoF98mqbjZLw', vector) print(res1) res2 = cli.submit('jcikTU98ZdeaUH5uBHsOPXzXAzAhBVdwtDDj7SqoF98mqbjZLw', vector) print(res2)