def genetic(): ITERATIONS = 30 POP_SIZE = 40 CROSSOVER_RATE = 0.5 MUTATION_RATE = 0.01 TOURNAMENT_SIZE = 6 task = read('generatedTaskFile.csv') print("Here it goes...") population = init_population(task.numberOfObjects, POP_SIZE, task) i = 0 while i < ITERATIONS: print("New iteration") j = 0 new_population = Population(task) new_population.listOfIndividuals = [] while j < POP_SIZE: parent1 = tournament(population, TOURNAMENT_SIZE, task) parent2 = tournament(population, TOURNAMENT_SIZE, task) child = Individual() child.itemsTaken = crossover(parent1, parent2, CROSSOVER_RATE) mutate(child, MUTATION_RATE) new_population.addIndividualToPopulation(child) j += 1 population = new_population i += 1 return population.best()
def get_good_solution(task, crossover_rate=CROSSOVER_RATE, mutation_rate=MUTATION_RATE, tournament_size=TOURNAMENT_SIZE, population_size=POPULATION_SIZE): best_individuals_values = np.empty(MAX_ITERATIONS) population = init_population(task, population_size) outer_iterator = 0 best_individual = 0 best_individual_value = 0 global_best_individual = 0 global_best_individual_value = 0 print('Initial value of the knapsack =', population.individuals[0].evaluate()) while outer_iterator < MAX_ITERATIONS: inner_iterator = 0 new_population = Population() while inner_iterator < population_size: parent1 = tournament(population, tournament_size) parent2 = tournament(population, tournament_size) child = crossover(parent1, parent2, crossover_rate) mutate(child, mutation_rate) new_population.add_individual(child) inner_iterator += 1 best_individual = population.best_individual() best_individual_value = best_individual.evaluate() if best_individual_value >= global_best_individual_value: global_best_individual = best_individual global_best_individual_value = best_individual_value new_population.set_first_individual(global_best_individual) population = new_population best_individuals_values[outer_iterator] = global_best_individual_value outer_iterator += 1 return global_best_individual, best_individuals_values
def benchmarkcomp(self): population = [] population.append(UltimateTTT(chromosome=self.prevbest)) if len(self.genbest) == 0: population.append(UltimateTTT(chromosome=[0])) else: index = len(self.genbest) - 1 population.append(UltimateTTT(chromosome=self.genbest[index])) for x in xrange(500): tournament.tournament(population, self.serieslen) self.report(pop=population)
def benchmarkcomp(self): population = [] population.append(UltimateTTT(chromosome = self.prevbest)) if len(self.genbest) == 0: population.append(UltimateTTT(chromosome = [0])) else: index = len(self.genbest) - 1 population.append(UltimateTTT(chromosome = self.genbest[index])) for x in xrange(500): tournament.tournament(population, self.serieslen) self.report(pop = population)
def get_best_from_population(population): result = np.empty(ITERATIONS) for i in range(ITERATIONS): new_population = Population() while new_population.population_size < POPULATION_SIZE: parent1 = tournament(population, TOURNAMENT_SIZE) parent2 = tournament(population, TOURNAMENT_SIZE) child = crossover(parent1, parent2, CROSSOVER_RATE) mutate(child, MUTATION_RATE) new_population.add_individual(child) print('.', end='') population = new_population result[i] = population.get_best_individual().evaluate() print('-', end='') return result
def main(): """ Run a tournament between two agents and print the resulting winrate for the first agent. """ interface1 = gtpinterface(TreeThreadingAgent(threads=5)) interface2 = gtpinterface(rave_mctsagent()) arguments = [10, 500, 1000, 7000] # lst = subsets_of_2(arguments) address = 'results/result.txt' with open(address, 'a') as f: f.write('TESTING THREADING_MCTSAGENT\n') f.close() i = 0 while i != len(arguments): interface2.RAVE_CONSTANT = arguments[i] result = tournament(interface1, interface2, game_number, num_rollouts, boardsize, opening_moves) with open(address, 'a') as file: file.write('Result of tournament %a \n' % i) file.write('For agent with RAVE_CONSTANT %a\n' % interface2.RAVE_CONSTANT) file.write('player 1 wins = %a games \n' % result[0]) file.write('player 2 wins = %a games \n' % result[1]) file.write("Total time : %a \n\n\n" % result[2]) file.close() i += 1
def run(self): start = time.clock() if self.decs is None: population = Global.initialize() else: population = Global.individual(self.decs) front_no, max_front = nd_sort(population[1], np.inf) crowd_dis = crowding_distance(population[1], front_no) evaluation = self.eva while self.eva >= 0: fit = np.vstack((front_no, crowd_dis)).T mating_pool = tournament(2, Global.N, fit) pop_dec, pop_obj = population[0], population[1] parent = [pop_dec[mating_pool, :], pop_obj[mating_pool, :]] offspring = Global.variation(parent[0], boundary=(Global.lower, Global.upper)) population = [ np.vstack((population[0], Global.individual(offspring)[0])), np.vstack((population[1], Global.individual(offspring)[1])) ] population, front_no, crowd_dis, _ = environment_selection( population, Global.N) self.eva = self.eva - Global.N if self.eva % (10 * evaluation / self.ite) == 0: end = time.clock() print('Running time %10.2f, percentage %s' % (end - start, 100 * (evaluation - self.eva) / evaluation)) return population
async def tournament(ctx, *args): global currentTournaments if len(args) == 0: if len(currentTournaments) == 0: currentTournaments = tournament() else: ctx.send("There is always a game night") else: if args[0] == "clear": if int(args[1]).isDigit(): try: currentTournaments.remove(int(args[1]) - 1) except IndexError: ctx.send("Index out of bounds") elif args[1] == "all": currentTournaments = None else: ctx.send(f"Cannot clear {args[1]}") elif len(args) > 4: del args[4:] #truncate args newTournament = tournament.tournament(args) currentTournaments.append(newTournament) ctx.send(newTournament.getAnnouncement()) else: newGamesNight = gamesnight.gamesnight(args) currentTournaments.append(newGamesNight) ctx.send(newGamesNight.getAnnouncement())
def mating_selection(population, Range, n): """ Mating selection in RSEA :param population: current population :param n: number of selected individuals :param Range: the range of the objective vectors :return: next generation population """ pop_obj = population[1] N = np.shape(pop_obj)[0] pop_obj = (pop_obj - np.tile(Range[0], (N, 1))) / \ np.tile(Range[1] - Range[0], (N, 1)) con = np.sqrt(np.sum(pop_obj**2, axis=1)) site, _ = radar_grid(pop_obj, np.ceil(np.sqrt(N))) crowd_g = itemfreq(site)[:, 1] mating_pool = np.zeros(np.ceil(N / 2).astype(int) * 2) grids = tournament(2, len(mating_pool), crowd_g.reshape((crowd_g.size, 1))) for i in range(len(mating_pool)): current = np.nonzero(site == grids[i])[0] if current is None: mating_pool[i] = np.random.randint(0, N, 1) else: parents = current[np.random.randint(0, len(current), 4)] best = np.argmin(con[parents]) mating_pool[i] = parents[best] return mating_pool.astype(int)
def main(): """ Run a tournament between two agents and print the resulting winrate for the first agent. """ interface1 = gtpinterface(mctsagent()) interface2 = gtpinterface(mctsagent()) print(str(tournament(interface1, interface2, 4, 2, 3, ['a1'])))
def main(): """ Run a tournament between two agents and print the resulting winrate for the first agent. """ interface1 = gtpinterface(mctsagent()) interface2 = gtpinterface(mctsagent()) print(str(tournament(interface1, interface2, game_number, move_time, boardsize, opening_moves)))
def genetic_algorithm(task, crossover_rate=CROSSOVER_RATE, mutation_rate=MUTATION_RATE, tournament_size=TOURNAMENT_SIZE, population_size=POPULATION_SIZE): best_individuals_values = np.empty(MAX_ITERATIONS) start_time = time.time() population = init_population(task, population_size) outer_iterator = 0 best_individual = 0 best_individual_value = 0 global_best_individual = 0 global_best_individual_value = 0 while outer_iterator < MAX_ITERATIONS: inner_iterator = 0 new_population = Population() while inner_iterator < population_size: parent1 = tournament(population, tournament_size) parent2 = tournament(population, tournament_size) child = crossover(parent1, parent2, crossover_rate) mutate(child, mutation_rate) new_population.add_individual(child) inner_iterator += 1 best_individual = population.best_individual() best_individual_value = best_individual.evaluate() if best_individual_value >= global_best_individual_value: global_best_individual = best_individual global_best_individual_value = best_individual_value new_population.set_first_individual(global_best_individual) population = new_population best_individuals_values[outer_iterator] = global_best_individual_value outer_iterator += 1 print("--- Genetic algorithm\'s execution time = %s seconds ---" % (time.time() - start_time)) print('Genetic algorithm\'s final result =', global_best_individual_value)
def run(self): pro = self.gp.pro if self.decs is None: population = pro.fit('init', self.gp.n) else: population = pro.fit(in_value=self.decs) evaluated = np.shape(population[0])[0] score = [[evaluated, pro.IGD(population[1])]] net = GAN(self.gp.d, self.batch_size, self.lr, self.epoch, self.n_noise) while evaluated <= self.gp.eva: _, index = environment_selection(population, self.k) ref_dec = population[0][index, :] pool = ref_dec / np.tile(pro.upper, (self.k, 1)) label = np.zeros((self.gp.n, 1)) label[index, :] = 1 pop_dec = population[0] input_dec = (pop_dec - np.tile(pro.lower, (np.shape(pop_dec)[0], 1))) / \ np.tile(pro.upper - pro.lower, (np.shape(pop_dec)[0], 1)) net.train(input_dec, label, pool) for i in range(self.w_max): print('IGD value: %.3f, Per: %.2f' % (pro.IGD(population[1]), 100 * evaluated / self.gp.eva)) if 1 - (i / self.w_max)**2 > np.random.random(1): off = net.generate(ref_dec / np.tile(pro.upper, (np.shape(ref_dec)[0], 1)), self.gp.n) * \ np.tile(pro.upper, (self.gp.n, 1)) off = pm_mutation(off, [self.gp.lower, self.gp.upper]) else: fitness = cal_fit(population[1]) mating = tournament(k_size=2, n_size=self.gp.n, fit=fitness.reshape((len(fitness), 1))) off = self.gp.operator(population[0][mating, :], boundary=[pro.lower, pro.upper]) offspring = pro.fit(in_value=off) evaluated += np.shape(offspring[0])[0] population = [ np.r_[population[0], offspring[0]], np.r_[population[1], offspring[1]] ] population, _ = environment_selection(population, self.gp.n) score.append([evaluated, pro.IGD(population[1])]) return population, score
def CreateTournament( self, start_date, type="drr", offset=True, country=None ): if offset: start_date += self.api.Call("get_current_julian") name = self.CreateTournamentName(country=country) new_tournament = None if type == "drr": name += " drr" new_tournament = tournament.tournament( name, start_date, ) if new_tournament is not None: self.tournament_list.append(new_tournament) return new_tournament
def __init__( self, name, start_julian_date, num_player_range, schedule ): self.invite_sender = invitesender.invitesender( num_player_range ) self.tournament = tournament.tournament( name, start_julian_date, num_player_range, schedule )
def CreateTournament(self, start_date, type="drr", offset=True, country=None): if offset: start_date += self.api.Call("get_current_julian") name = self.CreateTournamentName(country=country) new_tournament = None if type == "drr": name += " drr" new_tournament = tournament.tournament( name, start_date, ) if new_tournament is not None: self.tournament_list.append(new_tournament) return new_tournament
def simulation(init_players, rounds, steps, nr_kick): players = init_players results = [] player_types = {name.split("#")[0] for name in players} evolution = {t: [] for t in player_types} results = [] for s in range(steps): total_coins, _ = tournament(players, rounds) res = sorted_results(total_coins) players = next_players(res, nr_kick=nr_kick) ptypes = count_types(res, player_types) for t, cnt in ptypes.items(): evolution[t].append(cnt) results.append(ptypes) print("Step: {:>3}".format(s), ptypes) return results, evolution
def main(): """ Run a tournament between two agents and print the resulting winrate for the first agent. """ interface1 = gtpinterface(mctsagent()) interface2 = gtpinterface(rave_mctsagent()) address = r'E:\HEX\MCTS-agent-python-master\image\result.txt' f = open(address, 'a') f.write('Tournament between QB UCTRAVE , UCT \n') print('Tournament between QB RAVE , UCT \n') f.close() for i in range(3): result = tournament(interface1, interface2, game_number, move_time, boardsize, opening_moves) with open(address, 'a') as file: file.write('Result of tournament %a \n' % i) file.write('player 1 wins = %a games \n' % result[0]) file.write('player 2 wins = %a games \n' % result[1]) file.write("Simulations : \nAvg [ %a ] max = [ %a ] min = [ %a ] \n" % result[2]) file.write("Total time : %a \n\n\n" % result[3]) file.close()
def play(self): tournament.tournament(self.population, self.serieslen)
# -*- coding: utf-8 -*- import utils import tournament # get instances my_utils = utils.utils() my_selection = tournament.tournament(10) # do selection and plot results for i in range(5): my_utils._g2pop = my_selection.selection(my_utils._g1pop) my_utils.plotter(i, my_selection._CI)
genbest = [] for n in xrange(5): env = Environment(UltimateTTT, size=40, maxgenerations=80, threshold=4, genbest=genbest) genbest = env.run() elitepop = [] elites = list(genbest for genbest, _ in itertools.groupby(genbest)) for best in elites: print "Result", str(elites.index(best)), ":", best elitepop.append(UltimateTTT(chromosome=best)) number = 500 // (len(elites) - 1) for x in xrange(number): tournament.tournament(elitepop, 7) eliteenv = Environment(UltimateTTT, population=elitepop) eliteenv.report() #other feats # winning position of the board # block the opponent # leading to a board which the opponent can win # leading to a board which I can win # new benchmark?
parser.add_argument("--level_path", type=str, default=None, help="Path to level file") args = parser.parse_args() print(args, flush=True) train.validate_args(args) print("Worker", args.worker_idx, "is starting a portion of a tournament", flush=True) results = tournament(args.model_dir, args.local_pop_dir, args.game_path, args.base_port, args.num_envs, args.num_trials, args.worker_idx, args.total_workers, reuse_ports=args.dont_reuse_ports, level_path=args.level_path) print("Worker", args.worker_idx, "has completed a portion of a tournament", flush=True) for p, p_results in results: record_results(args.model_dir, p, p_results) if args.summary: eval.print_summary(p, p_results)
#!/usr/bin/env python # # Test cases for tournament.py from tournament import tournament from unittest import * ''' if the database does not exist, this will throw an error and can serve as a test case. ''' tourn = tournament() def testDeleteMatches(): tourn.deleteMatches() print "1. Old matches can be deleted." def testDelete(): tourn.deleteMatches() tourn.deletePlayers() print "2. Player records can be deleted." def testCount(): tourn.deleteMatches() tourn.deletePlayers() c = tourn.countPlayers() if c == '0': raise TypeError(
def __init__(self, name, start_julian_date, num_player_range, schedule): self.invite_sender = invitesender.invitesender(num_player_range) self.tournament = tournament.tournament(name, start_julian_date, num_player_range, schedule)
def gaManager(experimentPath, seed, numGen, popSize, indSize, numElit, numRegen, tourSize, mutationProb, crossProb, maxMutations, repetitions, segsize,waitgen, popmode): stime = time.time() #lastrep=0 #Find last evaluated generation #for lastrep in range(0,repetitions): #if(not os.path.exists(experimentPath + "/rep" + str(lastrep+1))): # break for rep in range(0, repetitions): random.seed(seed+rep) path = experimentPath + "/rep" + str(rep+1) + "/" + experimentPath + "_gen" sys.stdout.write("\r................................................") sys.stdout.write("\rEvaluating " + experimentPath + " rep " + str(rep)) #Generate Initial Population if popmode: gp.premadegenPopulation(path+"0",popSize, indSize) else: gp.genPopulation(path+"0",popSize, indSize) #Repeat for a number of generations fitbygen = [] for i in range(0, numGen): sys.stdout.write("\rEvaluating " + experimentPath + " repetition " + str(rep) + " generation " + str(i+1)) #If it exists, this generatios was already evaluated and the next one created, The program most lilely crashed if(os.path.exists(path+str(i+2))): continue #Evaluate Population cf.calcFit(path+str(i), 'Game') fitbygen.append(open(os.path.join(path+str(i),'0.fit'),'r').read()) #Carryover to next generation the best individuals best = elit.nextGen(path, i, numElit) k = numElit #size of next generation population if (segsize>0 and shouldSmartMutate(fitbygen,waitgen)): go.smartmutation(best,path,'Game',i,segsize,k) k = k+2 #Insert random individuals back into population gp.regnextGen(path, i+1, k,numRegen, indSize) #cf.calcFit(path+str(i+1), 'Game') k = k + numRegen #size of next generation population while k<popSize: #Select good invidual in current population father1 = tour.tournament(path+str(i),tourSize, popSize) father2 = tour.tournament(path+str(i),tourSize, popSize) #Generate new individuals through crossover and mutation #and evaluate them go.reproduce(father1, father2, path, i, k, indSize, mutationProb, crossProb, maxMutations) k=k+2 #lastgen=0 #sets last evaluated generation to be 0 again sys.stdout.write("\r................................................") sys.stdout.write("\rExperiment " + experimentPath + " done!") sys.stdout.write("\n Elapsed time: " + str(time.time() - stime)) o = open(os.path.join(experimentPath,'param.txt'),'w') o.write(experimentPath + ' ' + str(seed) + ' ' + str(numGen) + ' ' + str(popSize) + ' ' + str(indSize) + ' ' + str(numElit) + ' ' + str(numRegen) + ' ' + str(tourSize) + ' ' + str(mutationProb) + ' ' + str(crossProb) + ' ' + str(maxMutations) + ' ' + str(repetitions) + "\n Elapsed time: " + str(time.time() - stime)) o.close()
def run(self): """ run the wof_nsgaii to obtain the final population :return: the final population """ start = time.clock() if self.decs is None: population = Global.initialize() else: population = Global.individual(self.decs) g_size = math.ceil(Global.d / self.group_no) group = [] for i in range(self.group_no): group.append([]) lower = np.tile(Global.lower, (Global.N, 1)) upper = np.tile(Global.upper, (Global.N, 1)) w_lower = np.zeros((1, self.group_no)) w_upper = np.ones((1, self.group_no)) * 2 evaluated = 0 evaluated = evaluated + Global.N front_no, max_front = nd_sort(population[1], np.inf) crowd_dis = crowding_distance(population[1], front_no) while evaluated < int(self.eva * self.delta): en1 = evaluated while evaluated < (self.t1 + en1): fit = np.vstack((front_no, -crowd_dis)).T mating_pool = tournament(2, Global.N, fit) pop_dec, pop_obj = population[0], population[1] parent = [pop_dec[mating_pool, :], pop_obj[mating_pool, :]] off_dec = Global.variation(parent[0],boundary=(Global.lower,Global.upper)) offspring = Global.individual(off_dec) evaluated = evaluated + Global.N print('Number of evaluation: %d, time %.2f' % (evaluated, -start + time.clock())) population = [np.vstack((population[0], offspring[0])), np.vstack((population[1], offspring[1]))] population, front_no, crowd_dis, _ = environment_selection(population, Global.N) crowd_index = np.argsort(-crowd_dis) pop_dec, pop_obj = population[0], population[1] x_q = [pop_dec[crowd_index[:self.choose_no], :], pop_obj[crowd_index[:self.choose_no], :]] k = 1 sq = [np.tile(pop_dec, (self.choose_no + 1, 1)), np.tile(pop_obj, (self.choose_no + 1, 1))] while k <= self.choose_no: x_dec = np.zeros((self.weight_size, Global.d)) temp = x_q[0][k - 1, :] g_sort = np.argsort(temp) w = 2 * np.random.random((self.weight_size, self.group_no)) for i in range(self.group_no): group[i] = g_sort[int(g_size * i):int(g_size * (i + 1))] x_dec[:, group[i]] = np.tile(temp[group[i]], (self.weight_size, 1)) + self.p * ( np.tile(w[:, i].reshape(self.weight_size, 1), (1, g_size)) - 1) * np.tile( Global.upper[:, group[i]] - Global.lower[:, group[i]], (self.weight_size, 1)) x_dec = np.maximum(np.minimum(x_dec, Global.upper), Global.lower) en2 = evaluated X = Global.individual(x_dec) evaluated = evaluated + x_dec.shape[0] w_front, _ = nd_sort(X[1], np.inf) w_crowd = crowding_distance(X[1], w_front) while evaluated < (self.t2 + en2): fit = np.vstack((w_front, -w_crowd)).T w_mating_pool = tournament(2, self.weight_size, fit) w_offspring = Global.variation(w[w_mating_pool, :], boundary=(w_lower, w_upper)) for i in range(self.group_no): x_dec[:, group[i]] = np.tile(temp[group[i]], (self.weight_size, 1)) \ * np.tile(w_offspring[:, i].reshape((self.weight_size, 1)), (1, g_size)) x_offspring = Global.individual(x_dec) evaluated += self.weight_size # X can be update or not update? Reference given not update X, front_no, crowd_dis, next_label = environment_selection( [np.vstack((X[0], x_offspring[0])), np.vstack((X[1], x_offspring[1]))], self.weight_size) combine = np.vstack((w, w_offspring)) w = combine[next_label, :] s_dec = population[0].copy() for i in range(self.group_no): s_dec[:, group[i]] = s_dec[:, group[i]] + self.p * ( np.ones((Global.N, g_size)) * w[math.ceil(self.weight_size / 2), i] - 1) * ( upper[:, group[i]] - lower[:, group[i]]) s_pop = Global.individual(s_dec) evaluated = evaluated + s_dec.shape[0] print('Number of evaluation: %d, time %.2f' % (evaluated, -start + time.clock())) sq[0][Global.N * (k - 1):Global.N * k, :] = s_pop[0][:, :] sq[1][Global.N * (k - 1):Global.N * k, :] = s_pop[1][:, :] k += 1 sq[0][Global.N * self.choose_no:, :] = population[0] sq[1][Global.N * self.choose_no:, :] = population[1] population, front_no, crowd_dis, _ = environment_selection(sq, Global.N) while evaluated < self.eva: fit = np.vstack((front_no, -crowd_dis)).T mating_pool = tournament(2, Global.N, fit) pop_dec, pop_obj = population[0], population[1] parent = [pop_dec[mating_pool, :], pop_obj[mating_pool, :]] offspring = Global.individual(Global.variation(parent[0], boundary=(Global.lower,Global.upper))) evaluated = evaluated + Global.N print('Number of evaluation: %d, time %.2f' % (evaluated, -start + time.clock())) population = [np.vstack((population[0], offspring[0])), np.vstack((population[1], offspring[1]))] population, front_no, crowd_dis,_ = environment_selection(population, Global.N) return population
print "Simulation", str(x) print "time: ", currtime genbest = [] for n in xrange(5): env = Environment(UltimateTTT, size=40, maxgenerations=80, threshold=4, genbest = genbest) genbest = env.run() elitepop = [] elites = list(genbest for genbest,_ in itertools.groupby(genbest)) for best in elites: print "Result", str(elites.index(best)), ":", best elitepop.append(UltimateTTT(chromosome=best)) number = 500 // (len(elites) - 1) for x in xrange(number): tournament.tournament(elitepop, 7) eliteenv = Environment(UltimateTTT, population=elitepop) eliteenv.report() #other feats # winning position of the board # block the opponent # leading to a board which the opponent can win # leading to a board which I can win # new benchmark?