def start(self, show=False, verbose=True): self.results = np.empty((self.trials, 2), dtype=int) for i in range(self.trials): if verbose: print(" Match", i) brasil = SoccerTeam("Brasil") brasil.add( "self.strategy", self.strategy(accShoot=self.list_param[0], accDrible=self.list_param[1], vit=self.list_param[2], n=self.list_param[3], maxAngle=self.list_param[4], tooFar=self.list_param[5], rSurfBut=self.list_param[6], AngleHyst=self.list_param[7], alpha=self.list_param[8])) desafiante = self.ia.get_team(1) simu = Simulation(brasil, desafiante, max_steps=self.max_steps) if show: show_simu(simu) else: simu.start() self.results[i, 0] = simu.get_score_team(1) self.results[i, 1] = simu.get_score_team(2) if verbose: print(" Score : {} x {}".format(self.results[i, 0], self.results[i, 1]))
class Observer(object): MAX_STEP=40 def __init__(self,visu = False): # Visu permet la visualisation de la simu. Si on en veut pas, on met false. Cela est permis par la fonction start en bas. team1 = SoccerTeam("expe1") team2 = SoccerTeam("expe2") self.strat = shoot() # On donne la strat qu'on va utiliser. team1.add("jexp1", self.strat ) team2.add("jplot",Strategy()) # Si besoin d'un joueur de team adverse self.simu = Simulation(team1,team2,max_steps=10000000) # On def la simu avec un enorme max_steps car on veut test x round et on veut pas devoir recommencer un match self.visu = visu self.simu.listeners+=self #ajout de l observer list_a = np.linspace(0.1,20,30) # Creation de la matrice pour la parametre a. De param1 a param2 avec param2 valeurs list_b = np.linspace(0.1,20,30) self.list_params = [(a,b) for a in list_a for b in list_b] # Creation de tout les couples possible self.cpt_params = 0 # Va permettre de tester toute la liste de couple de params self.nb_expe = 20 # Nb de round que l on fera par postion self.res = dict() # Ini de notre dico self.pos = dict() def begin_match(self,team1,team2,state): #initialisation des parametres ... self.last, self.but, self.expe = 0, 0, 0 def begin_round(self,team1,team2,state): self.x,self.y = 4*GAME_WIDTH/7+random.uniform(0,1)*GAME_WIDTH/7,GAME_HEIGHT/4+random.uniform(0,1)*GAME_HEIGHT/4 # Ini des postions qu on voudra en depart, dans un secteur donne self.simu.state.states[(1,0)].position = Vector2D(self.x,self.y) self.simu.state.ball.position = Vector2D(self.x,self.y) #ou self.simu.set_state(state) self.strat.a,self.strat.b = self.list_params[self.cpt_params] # On met les vals de a et b dans shoot que l on veut pour les couples dans list_params self.last = self.simu.step # Pas a la fin du round precedant def update_round(self,team1,team2,state): if state.step>self.last+self.MAX_STEP: self.simu.end_round() def end_round(self,team1,team2,state): if state.goal>0: # Si but marque, on incremente but self.but+=1 self.expe+=1 # On increment pour chaque round if self.expe > self.nb_expe: if self.cpt_params <len(self.list_params)-1: # Si on a pas traite tout les couples if self.but*1./self.expe > 0.5 : self.res[self.list_params[self.cpt_params]]= self.but*1./self.expe # On met dans res la proba self.pos[self.list_params[self.cpt_params]]= self.x,self.y self.cpt_params+=1 # On change les params qu on va tester self.last, self.but, self.expe = 0, 0, 0 # On reinitialise else: self.simu.end_match() # Sinon on end def start(self): if self.visu : show_simu(self.simu) else: self.simu.start() # Trouver parametre
class ParamSearch(object): def __init__(self, strategy, params, simu=None, trials=20, max_steps=1000000, max_round_step=40): self.strategy = strategy self.params = params.copy() self.simu = simu self.trials = trials self.max_steps = max_steps self.max_round_step = max_round_step def start(self, show=True): if not self.simu: team1 = SoccerTeam("Team 1") team2 = SoccerTeam("Team 2") team1.add(self.strategy.name, self.strategy) team2.add(Strategy().name, Strategy()) self.simu = Simulation(team1, team2, max_steps=self.max_steps) self.simu.listeners += self if show: show_simu(self.simu) else: self.simu.start()
class ShootSearch(object): """ nombre d'iterations maximales jusqu'a l'arret d'un round discr_step : pas de discretisation du parametre nb_essais : nombre d'essais par parametre """ MAX_STEP = 40 def __init__(self): self.strat = ShootExpe() team1 = SoccerTeam("test") team1.add("Expe", self.strat) team2 = SoccerTeam("test2") team2.add("Nothing", Strategy()) self.simu = Simulation(team1, team2, max_steps=40000) self.simu.listeners += self self.discr_step = 20 self.nb_essais = 20 def start(self, visu=True): """ demarre la visualisation avec ou sans affichage""" if visu: show_simu(self.simu) else: self.simu.start() def begin_match(self, team1, team2, state): """ initialise le debut d'une simulation res : dictionnaire des Resultats last : step du dernier round pour calculer le round de fin avec MAX_STEP but : nombre de but pour ce parametre cpt : nombre d'essais pour ce parametre params : liste des parametres a tester idx : identifiant du parametre courant """ self.res = dict() self.last = 0 self.but = 0 self.cpt = 0 self.params = [ x for x in np.linspace(1, settings.maxPlayerShoot, self.discr_step) ] self.idx = 0 def begin_round(self, team1, team2, state): """ engagement : position random du joueur et de la balle """ position = Vector2D( np.random.random() * settings.GAME_WIDTH / 2. + settings.GAME_WIDTH / 2., np.random.random() * settings.GAME_HEIGHT) self.simu.state.states[(1, 0)].position = position.copy() self.simu.state.states[(1, 0)].vitesse = Vector2D() self.simu.state.ball.position = position.copy() self.strat.norm = self.params[self.idx] self.last = self.simu.step def update_round(self, team1, team2, state): """ si pas maximal atteint, fin du tour""" if state.step > self.last + self.MAX_STEP: self.simu.end_round() def end_round(self, team1, team2, state): if state.goal > 0: self.but += 1 self.cpt += 1 if self.cpt >= self.nb_essais: self.res[self.params[self.idx]] = self.but * 1. / self.cpt logger.debug("parametre %s : %f" % ( (str(self.params[self.idx]), self.res[self.params[self.idx]]))) self.idx += 1 self.but = 0 self.cpt = 0 """ si plus de parametre, fin du match""" if self.idx >= len(self.params): self.simu.end_match()
nVect = 5 gk_st_team = GKStrikerTeam(size=size) gk_st_team.start() gk_cf_team = GKStrikerTeam(size=size) #GKCForwardTeam(size) gk_cf_team.start() for n in range(nbIter): gk_st_team.restart() gk_cf_team.restart() for i in range(size): gk_st = gk_st_team.getTeam(i) gk_st_p = gk_st_team.getVector(i) for j in range(size): gk_cf = gk_cf_team.getTeam(j) gk_cf_p = gk_cf_team.getVector(j) team = {1: (gk_st, gk_st_p), 2: (gk_cf, gk_cf_p)} if random.random() < 0.5: team[1], team[2] = team[2], team[1] simu = Simulation(team[1][0], team[2][0]) simu.start() #print(j) setCounters(simu, team[1][1], team[2][1]) print(n) gk_st_team.update() gk_cf_team.update() gk_st_team.printVectors(nVect) gk_cf_team.printVectors(nVect) gk_st_team.save() simu = Simulation(gk_st_team.getBestTeam(), gk_cf_team.getBestTeam()) show_simu(simu)
class ParamSearch(object): def __init__(self, strategy, params, simu=None, trials=20, max_steps=1000000, max_round_step=40): self.strategy = strategy self.params = params.copy() self.simu = simu self.trials = trials self.max_steps = max_steps self.max_round_step = max_round_step def start(self, show=True): if not self.simu: team1 = SoccerTeam("Team 1") team2 = SoccerTeam("Team 2") team1.add(self.strategy.name, self.strategy) team2.add(Strategy().name, Strategy()) self.simu = Simulation(team1, team2, max_steps=self.max_steps) self.simu.listeners += self if show: show_simu(self.simu) else: self.simu.start() def begin_match(self, team1, team2, state): self.last = 0 # Step of the last round self.crit = 0 # Criterion to maximize (here, number of goals) self.cpt = 0 # Counter for trials if len(self.params) > 2: raise ValueError('Max two parameters') self.param_keys = list(self.params.keys()) # Name of all parameters self.param_id = [0] * len(self.param_keys) # Index of the parameter values self.res = dict() # Dictionary of results def begin_round(self, team1, team2, state): ball = Vector2D.create_random(low=0, high=1) ball.x *= GAME_WIDTH / 2 ball.x += GAME_WIDTH / 2 ball.y *= GAME_HEIGHT # Player and ball postion (random) self.simu.state.states[(1, 0)].position = ball.copy() # Player position self.simu.state.states[(1, 0)].vitesse = Vector2D() # Player acceleration self.simu.state.ball.position = ball.copy() # Ball position # Last step of the game self.last = self.simu.step # Set the current value for the current parameter for i, (key, values) in zip(self.param_id, self.params.items()): setattr(self.strategy, key, values[i]) def update_round(self, team1, team2, state): # Stop the round if it is too long if state.step > self.last + self.max_round_step: self.simu.end_round() def end_round(self, team1, team2, state): # A round ends when there is a goal if state.goal > 0: self.crit += 1 # Increment criterion self.cpt += 1 # Increment number of trials for i, (key, values) in zip(self.param_id, self.params.items()): print("{}: {}".format(key, values[i]), end=" ") print("Crit: {} Cpt: {}".format(self.crit, self.cpt)) if self.cpt >= self.trials: # Save the result res_key = tuple() for i, values in zip(self.param_id, self.params.values()): res_key += values[i], self.res[res_key] = self.crit * 1. / self.trials # Reset parameters self.crit = 0 self.cpt = 0 # Go to the next parameter value to try key0 = self.param_keys[0] if self.param_id[0] < len(self.params[key0]) - 1: self.param_id[0] += 1 elif len(self.params) > 1: key1 = self.param_keys[1] if self.param_id[1] < len(self.params[key1]) - 1: self.param_id[0] = 0 self.param_id[1] += 1 else: self.simu.end_match() else: self.simu.end_match() def get_res(self): return self.res
from salsa import Attaquant, Defense from soccersimulator import Player, SoccerTeam, Simulation, show_simu joueura1 = Player("Attaquant A", Attaquant()) joueura2 = Player("Defenseur A", Defense()) team1 = SoccerTeam("Equipe A", [joueura1, joueura2]) # nombre de joueurs de l equipe joueurb1 = Player("Attaquant B", Attaquant()) joueurb2 = Player("Defenseur B", Defense()) team2 = SoccerTeam("Equipe B", [joueurb1, joueurb2]) # Creer un match entre 2 equipes et de duree 10 pas match = Simulation(team1, team2, 1000) # Jouer le match ( sans le visualiser ) match.start() # Jouer le match en le visualisant show_simu(match) # Attention !! une fois le match joue , la fonction start () permet de faire jouer le replay # mais pas de relancer le match !!! # Pour regarder le replay d un match
class GoalSearch(object): def __init__(self, strategy, params, simu=None, trials=20, max_steps=1000000, max_round_step=40): self.strategy = strategy self.params = params.copy() self.simu = simu self.trials = trials self.max_steps = max_steps self.max_round_step = max_round_step def start(self, show=True): if not self.simu: team1 = SoccerTeam(" Team ␣ 1 ") team2 = SoccerTeam(" Team ␣ 2 ") team1.add(self.strategy.name, self.strategy) team2.add(Strategy().name, Strategy()) self.simu = Simulation(team1, team2, max_steps=self.max_steps) self.simu.listeners += self if show: show_simu(self.simu) else: self.simu.start() def begin_match(self, team1, team2, state): self.last_step = 0 # Step of the last round self.criterion = 0 # Criterion to maximize ( here , number of goals ) self.cpt_trials = 0 # Counter for trials self.param_grid = iter(ParameterGrid( self.params)) # Iterator for the g self.cur_param = next(self.param_grid, None) # Current parameter if self.cur_param is None: raise ValueError('no parameter given.') self.res = dict() # Dictionary of results def begin_round(self, team1, team2, state): ball = Vector2D.create_random(low=0, high=1) ball.x += 30 ball.y += GAME_HEIGHT / 2 # Player and ball postion ( random ) self.simu.state.states[(1, 0)].position = ball.copy() # Player position self.simu.state.states[(1, 0)].vitesse = Vector2D() # Player accelerati self.simu.state.ball.position = ball.copy() # Ball position self.last_step = self.simu.step # Last step of the game # Set the current value for the current parameters for key, value in self.cur_param.items(): setattr(self.strategy, key, value) def end_round(self, team1, team2, state): # A round ends when there is a goal of if max step is achieved if state.goal > 0: self.criterion += 1 # Increment criterion self.cpt_trials += 1 # Increment number of trials print(self.cur_param) print(" Crit : ␣{}␣␣␣ Cpt :␣{} ".format(self.criterion, self.cpt_trials)) if self.cpt_trials >= self.trials: # Save the result self.res[tuple( self.cur_param.items())] = self.criterion * 1. / self.trials # Reset parameters self.criterion = 0 self.cpt_trials = 0 # Next parameter value self.cur_param = next(self.param_grid, None) if self.cur_param is None: self.simu.end_match() def update_round(self, team1, team2, state): # Stop the round if it is too long if state.step > self.last_step + self.max_round_step: self.simu.end_round() def get_res(self): return self.res def get_best(self): return max(self.res, key=self.res.get)
class QLearning(object ): def __init__(self , strategy , simu=None , max_steps =10000 , max_round_step =100, monte_carlo=False): self.strategy = strategy self.simu = simu self.max_steps = max_steps self.max_round_step = max_round_step self.monte_carlo = monte_carlo def start(self , show=True , fps=None): if not self.simu: team1 = SoccerTeam("Team␣1") team2 = SoccerTeam("Team␣2") team1.add(self.strategy.name , self.strategy) team2.add(StrategyAttaquant().name , StrategyAttaquant()) self.simu = Simulation(team1 , team2 , max_steps=self.max_steps) self.simu.listeners += self if show: show_simu(self.simu , fps=fps) else: self.simu.start () def get_res(self): return self.qtable def begin_match(self , team1 , team2 , state): self.last_step = 0 # Step of the last round self.qtable = dict() # Q table def begin_round(self , team1 , team2 , state): # ball = Vector2D.create_random(low=0, high =1) # ball.x *= GAME_WIDTH # ball.y *= GAME_HEIGHT ball = Vector2D(GAME_WIDTH/2, GAME_HEIGHT/2) # Player and ball postion (random) # self.simu.state.states [(1, 0)]. position = Vector2D() # Player position self.simu.state.states [(1, 0)]. vitesse = Vector2D () # Player acceleration self.simu.state.ball.position = ball.copy() # Ball position # Last step of the game self.last_step = self.simu.step self.last_state = None self.last_score = self.simu.score [1] # Score of Team 1 self.cur_state = self.strategy.get_state(state , id_team=1, id_player =0) self.rewards = [] def update_round(self , team1 , team2 , state): # Q-learning update self.qupdate(state) if state.step > self.last_step + self.max_round_step: # Change action when state doesn’t change if self.cur_state == self.last_state: self.strategy.strategy = self.next_action(self.cur_state) self.last_state = self.cur_state def qupdate(self , state): qstate_next = self.strategy.get_state(state , id_team=1, id_player =0) if self.cur_state != qstate_next: qaction = self.strategy.strategy # Strategy name key = (self.cur_state , qaction) # Future Q-value qvalues_next = [q for k, q in self.qtable.items() \ if k[0] == qstate_next] qnext = max(qvalues_next , default =0) # Reinforcement score = state.score [1] if score > self.last_score: self.last_score = score r = 0 else: r = -1 if state.goal == 0 else -10 qvalue = r + 0.9 * qnext # Update Q-table self.rewards.append ((key , r)) if not self.monte_carlo: if key in self.qtable: self.qtable[key] = 0.5 * self.qtable[key] + 0.5 * qvalue else: self.qtable[key] = qvalue # Change action self.strategy.strategy = self.next_action(qstate_next) # Update current state self.cur_state = qstate_next def next_action(self , qstate_next ): minq = min([q for key , q in self.qtable.items() \ if key[0] == qstate_next], default =0) minq = min(minq , 0) prob = [0.1 if (qstate_next , name) not in self.qtable \ else self.qtable [( qstate_next , name)] - minq + 0.1 \ for name in self.strategy.strategy_names] prob = np.asarray(prob) if prob.sum() < 1e-15: prob = None else: prob /= prob.sum() return choice(list(self.strategy.strategy_names), p=prob)
class SimpleParamSearch(object): def __init__(self, trials=5, max_round_step=100, discret=5): self.trials = trials self.max_round_step = max_round_step #self.list_forces = [0,0.5,1.,1.5,2,2.5,3,3.5] self.list_forces = [4.5, 5, 5.5, 6] self.list_grille = [] #Fabrication de la grille de discretisation du terrain self.discret = discret self.stepx = GAME_WIDTH / (2. * discret) self.stepy = GAME_HEIGHT / (1. * discret) for i in range(discret): for j in range(discret): self.list_grille.append( Vector2D(GAME_WIDTH / 2. + self.stepx * i, self.stepy * j)) self.strategy = FonceurTestStrategy() def start(self, show=True): team1 = SoccerTeam("Team 1") team1.add("Test tire", self.strategy) self.simu = Simulation(team1, max_steps=100000) self.simu.listeners += self if show: show_simu(self.simu) else: self.simu.start() def begin_match(self, team1, team2, state): self.last = 0 # Step of the last round self.crit = 0 # Criterion to maximize (here, number of goals) self.cpt = 0 # Counter for trials self.res = dict() # Dictionary of results self.idx_force = 0 # counter for parameter index self.idx_grille = 0 def begin_round(self, team1, team2, state): # On fixe la position de la balle et du joueur ball_pos = self.list_grille[self.idx_grille] self.simu.state.states[(1, 0)].position = ball_pos.copy() self.simu.state.states[(1, 0)].vitesse = Vector2D() self.simu.state.ball.position = ball_pos.copy() # Set the current value for the current parameter self.strategy.strength = self.list_forces[self.idx_force] # Last step of the game self.last = self.simu.step def update_round(self, team1, team2, state): # Stop the round if it is too long if state.step > self.last + self.max_round_step: self.simu.end_round() def end_round(self, team1, team2, state): # A round ends when there is a goal if state.goal > 0: self.crit += 1 # Increment criterion self.cpt += 1 # Increment number of trials if self.cpt >= self.trials: key = (self.list_grille[self.idx_grille].x, self.list_grille[self.idx_grille].y) if key not in self.res: self.res[key] = [] self.res[key].append((self.list_forces[self.idx_force], self.crit * 1. / self.trials)) print("Res pour position %s force %f : %f" % (str(key), self.res[key][-1][0], self.res[key][-1][1])) # Reset parameters self.crit = 0 self.cpt = 0 # Go to the next parameter value to try self.idx_force += 1 if self.idx_force >= len(self.list_forces): self.idx_grille += 1 self.idx_force = 0 if self.idx_grille >= len(self.list_grille): self.simu.end_match() def end_match(self, team1, team2, state): #Trouve les meilleurs parametres et les sauvegarde best_force = dict() for k, v in self.res.items(): best_force[k] = sorted(v, key=lambda x: x[1])[-1][0] with open("best_force.pkl", "wb") as fn: pickle.dump(best_force, fn)
class GoalSearchc(object): def __init__(self,strategy,params,simu=None, trials=2,max_steps=1000000,max_round_step=40,nogen=2,cpt_gen=0,l=[]): self.strategy=strategy self.params=params.copy() self.simu=simu self.trials=trials self.max_steps=max_steps self.max_round_step=max_round_step self.nogen=nogen self.cpt_gen=cpt_gen self.l=l def start(self,show=True): if not self.simu: team1 = SoccerTeam("Team 1") team2 = SoccerTeam("Team 2") team1.add(self.strategy.name,self.strategy) team2.add(Strategy().name , Strategy()) self.simu = Simulation(team1, team2 , max_steps = self.max_steps ) self.simu.listeners += self if show : show_simu(self.simu) else : self.simu.start() def begin_match(self, team1, team2, state): self.last_step = 0 # Step of the last round self.criterion = 0 # Criterion to maximize ( here , number of goals ) self.cpt_trials = 0 # Counter for trials for i in range(8): a=random.random() b=random.random() if(b<0.5): b=-1 else: b=1 if(a<0.5): a=-1 else: a=1 dic={ "forcet":1+random.random()*0.1*a, "dpos":5/8*GAME_WIDTH+random.random()*0.1*b } self.l.append(dic) self.param_grid = iter(self.l) self.cur_param = next(self.param_grid, None ) # Current parameter if self.cur_param is None : raise ValueError("no parameter given") self.res = dict() # Dictionary of results def begin_round ( self , team1 , team2 , state ): ball = Vector2D.create_random(low=-30,high=30) ball.x+=GAME_WIDTH*8/10 ball.y+=GAME_HEIGHT/2 # Player and ball postion ( random ) self.simu.state.states[(1 , 0)].position = ball.copy() # Player position self.simu.state.states[(1 , 0)].vitesse = Vector2D() # Player accelerati self.simu.state.ball.position = ball.copy() # Ball position self.last_step = self.simu.step # Last step of the game # Set the current value for the current parameters for key , value in self.cur_param.items(): setattr( self.strategy , key , value ) def end_round(self, team1, team2, state): # A round ends when there is a goal of if max step is achieved if state.goal > 0: self.criterion += 1 # Increment criterion self.cpt_trials += 1 # Increment number of trials print(self.cur_param,end = " " ) print ( " Crit : {} Cpt : {} " . format(self.criterion,self.cpt_trials)) if self.cpt_trials >= self.trials : # Save the result self.res[tuple(self.cur_param.items())] = self.criterion*1./self.trials # Reset parameters self.criterion = 0 self.cpt_trials = 0 # Next parameter value self.cur_param = next(self.param_grid , None ) if self.cur_param is None : if(self.cpt_gen==self.nogen-1): self.simu.end_match () else: self.cpt_gen+=1 mr=0 nr=0 for t in self.res: mr+=self.res[t] nr+=1 mr=mr/nr print(self.res) for t in self.l: key = tuple(t.items()) if key in self.res: if (self.res[tuple(t.items())]<mr): self.l.remove(t) for i in range(8-len(self.l)): a=random.random() b=random.random() if(b<0.5): b=-1 else: b=1 if(a<0.5): a=-1 else: a=1 dic={ "forcet":2+random.random()*0.1*a, "dpos":5/8+random.random()*0.1*b } self.l.append(dic) self.param_grid = iter(self.l) self.cur_param = next ( self.param_grid , None ) def update_round ( self , team1 , team2 , state ): # Stop the round if it is too long if state.step>self.last_step+self.max_round_step: self.simu.end_round() def get_res (self): return self.res def get_best(self): return max(self.res, key=self.res.get)
class SimpleParamSearch(object): def __init__(self, trials=10, max_round_step=100, discret=3): """ :param trials: nombre d'essais par endroit et par puissance :param max_round_step: durée maximale d'un essai :param discret: pas (step) de découpage du terrain. Attention, le nombre d'endroits testés est égal à discret^2 """ self.trials = trials self.max_round_step = max_round_step self.list_forces = [0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5] #7 self.list_grille = [] #Fabrication de la grille de discretisation du terrain if discret == 1: self.stepx = 0 self.stepy = 0 else: self.stepx = GAME_WIDTH / (2. * discret) self.stepy = GAME_HEIGHT / (2. * discret - 2) #D'où le if ==1 for i in range(discret): for j in range(discret): self.list_grille.append( Vector2D(GAME_WIDTH / 2. + self.stepx * i, self.stepy * j)) self.strategy = Fonceur() def start(self, show=True): team1 = SoccerTeam("Team 1") team1.add("Test", self.strategy) self.simu = Simulation(team1, max_steps=100000) self.simu.listeners += self if show: show_simu(self.simu) else: self.simu.start() def begin_match(self, team1, team2, state): self.last = 0 # Step of the last round self.crit = 0 # Criterion to maximize (here, number of goals) self.cpt = 0 # Counter for trials self.res = dict() # Dictionary of results self.idx_force = 0 # counter for parameter index self.idx_grille = 0 def begin_round(self, team1, team2, state): # On fixe la position de la balle et du joueur ball_pos = self.list_grille[self.idx_grille] self.simu.state.states[(1, 0)].position = ball_pos.copy() self.simu.state.states[(1, 0)].vitesse = Vector2D() self.simu.state.ball.position = ball_pos.copy() # Set the current value for the current parameter self.strategy.strength = self.list_forces[self.idx_force] # Last step of the game self.last = self.simu.step def update_round(self, team1, team2, state): # Stop the round if it is too long if state.step > self.last + self.max_round_step: self.simu.end_round() def end_round(self, team1, team2, state): # A round ends when there is a goal if state.goal > 0: self.crit += 1 # Increment criterion self.cpt += 1 # Increment number of trials if self.cpt >= self.trials: key = (self.list_grille[self.idx_grille].x, self.list_grille[self.idx_grille].y) if key not in self.res: self.res[key] = [] self.res[key].append((self.list_forces[self.idx_force], self.crit * 1. / self.trials)) print("Res pour position", key, "force", self.res[key][-1][0], ":", self.res[key][-1][1]) # Reset parameters self.crit = 0 self.cpt = 0 # Go to the next parameter value to try self.idx_force += 1 if self.idx_force >= len(self.list_forces): self.idx_grille += 1 self.idx_force = 0 if self.idx_grille >= len(self.list_grille): self.simu.end_match() def end_match(self, team1, team2, state): #Trouve les meilleurs paramètres et les sauvegarde meilleure_force = dict() for k, v in self.res.items(): meilleure_force[k] = sorted(v, key=lambda x: x[1])[-1][0] with open("meilleure_force.pkl", "wb") as f: pickle.dump(meilleure_force, f)
class ParamSearchReception(object): def __init__(self, strategy, params, simu=None, trials=7, max_steps=1000000, max_round_step=40): self.strategy = strategy self.params = params.copy() self.simu = simu self.trials = trials self.max_steps = max_steps self.max_round_step = max_round_step def start(self, show=True): if not self.simu: team1 = SoccerTeam("Team Passeurs") team2 = SoccerTeam("Team Vide") team1.add(self.strategy.name, self.strategy) team2.add(Strategy().name, Strategy()) self.simu = Simulation(team1, team2, max_steps=self.max_steps) self.simu.listeners += self if show: show_simu(self.simu) else: self.simu.start() def begin_match(self, team1, team2, state): self.last = 0 # Step of the last round self.crit = 0 # Criterion to maximize (here, number of goals) self.cpt = 0 # Counter for trials self.param_keys = list(self.params.keys()) # Name of all parameters self.param_id = [0] * len(self.params) # Index of the parameter values self.param_id_id = len(self.params) - 1 # Index of the current parameter self.res = dict() # Dictionary of results def begin_round(self, team1, team2, state): ball = Vector2D(GAME_WIDTH/2., 60.) # Player and ball postion (random) self.simu.state.states[(1, 0)].position = Vector2D(50, 45) # Recepteur position self.simu.state.states[(1, 0)].vitesse = Vector2D() # Recepteur acceleration self.simu.state.ball.position = ball.copy() # Ball position self.simu.state.ball.vitesse = Vector2D(-4,-2) # Ball acceleration # Last step of the game self.last = self.simu.step self.strategy.cont = True # Set the current value for the current parameter for i, (key, values) in zip(self.param_id, self.params.items()): setattr(self.strategy, key, values[i]) def update_round(self, team1, team2, state): me = StateFoot(state, 1, 0) # Stop the round if it is too long if state.step > self.last + self.max_round_step: #print(me.my_pos - me.center_point) self.simu.end_round() def end_round(self, team1, team2, state): # A round ends when there is a goal self.cpt += 1 # Increment number of trials if self.cpt >= self.trials: # Save the result res_key = tuple() for i, values in zip(self.param_id, self.params.values()): res_key += values[i], self.res[res_key] = self.crit * 1. / self.trials #print(res_key, self.crit) # Reset parameters self.crit = 0 self.cpt = 0 # Go to the next parameter value to try key = self.param_keys[self.param_id_id] if self.param_id[self.param_id_id] < len(self.params[key]) - 1: self.param_id[self.param_id_id] += 1 else: self.simu.end_match() for i, (key, values) in zip(self.param_id, self.params.items()): #print("{}: {}".format(key, values[i]), end=" ") #print("Crit: {} Cpt: {}".format(self.crit, self.cpt)) def get_res(self): return self.res
class ParamSearchDribble(object): def __init__(self, strategy, params, simu=None, trials=20, max_steps=1000000, max_round_step=40): self.strategy = strategy self.params = params.copy() self.simu = simu self.trials = trials self.max_steps = max_steps self.max_round_step = max_round_step def start(self, show=True): if not self.simu: team1 = SoccerTeam("Team Fonceur") team2 = SoccerTeam("Team Dribbler") #team1.add(FonceurChallenge1Strategy().name, FonceurChallenge1Strategy()) team1.add(FonceurStrategy().name, FonceurStrategy()) team2.add(self.strategy.name, self.strategy) self.simu = Simulation(team1, team2, max_steps=self.max_steps) self.simu.listeners += self if show: show_simu(self.simu) else: self.simu.start() def begin_match(self, team1, team2, state): self.last = 0 # Step of the last round self.crit = 0 # Criterion to maximize (here, number of goals) self.cpt = 0 # Counter for trials self.param_keys = list(self.params.keys()) # Name of all parameters self.param_id = [0] * len(self.params) # Index of the parameter values self.param_id_id = len(self.params) - 1 # Index of the current parameter self.res = dict() # Dictionary of results def begin_round(self, team1, team2, state): ball = Vector2D.create_random(low=-1, high=1) if ball.x < 0. : ball.x = -ball.x aleat = Vector2D.create_random(low=25, high=45) ball.normalize().scale(aleat.x) ball.y += GAME_HEIGHT/2. angle = Vector2D.create_random(low=0., high=pi*2.) aleat = Vector2D.create_random(low=5, high=15) fonc = ball + Vector2D(aleat.x*cos(angle.x), aleat.x*sin(angle.x)) # Player and ball postion (random) self.simu.state.states[(2, 0)].position = ball.copy() # Shooter position self.simu.state.states[(2, 0)].vitesse = Vector2D() # Shooter acceleration self.simu.state.states[(1, 0)].position = fonc.copy() # Shooter position self.simu.state.states[(1, 0)].vitesse = Vector2D() # Shooter acceleration self.simu.state.ball.position = ball.copy() # Ball position # Last step of the game self.last = self.simu.step # Set the current value for the current parameter for i, (key, values) in zip(self.param_id, self.params.items()): setattr(self.strategy, key, values[i]) def update_round(self, team1, team2, state): # Stop the round if it is too long if state.step > self.last + self.max_round_step: self.simu.end_round() def end_round(self, team1, team2, state): # A round ends when there is a goal if state.goal > 0: self.crit += 1 # Increment criterion self.cpt += 1 # Increment number of trials if self.cpt >= self.trials: # Save the result res_key = tuple() for i, values in zip(self.param_id, self.params.values()): res_key += values[i], self.res[res_key] = self.crit * 1. / self.trials #print(res_key, self.crit) # Reset parameters self.crit = 0 self.cpt = 0 # Go to the next parameter value to try key2 = self.param_keys[self.param_id_id] key1 = self.param_keys[self.param_id_id-1] if self.param_id[self.param_id_id] < len(self.params[key2]) - 1: self.param_id[self.param_id_id] += 1 elif self.param_id[self.param_id_id-1] < len(self.params[key1]) - 1: self.param_id[self.param_id_id] = 0 self.param_id[self.param_id_id-1] += 1 else: self.simu.end_match() for i, (key, values) in zip(self.param_id, self.params.items()): #print("{}: {}".format(key, values[i]), end=" ") #print("Crit: {} Cpt: {}".format(self.crit, self.cpt)) def get_res(self): return self.res