def entrainer2v2_snap(self, fname1, fname2, nb_snap): if self.nb_players != 2: raise Exception( "Not the good number of players for this function.") #Creation d'une partie kb_strat1 = KeyboardStrategy() kb_strat1.add("a", m.strategy.ShootStrat()) kb_strat1.add("z", m.strategy.DefStrat()) kb_strat1.add("e", m.strategy.GardienStrat()) kb_strat1.add("q", m.strategy.DribleStrat()) kb_strat1.add("s", m.strategy.PassStrat()) kb_strat1.add("d", m.strategy.AtkStrat()) kb_strat2 = KeyboardStrategy() kb_strat2.add("a", m.strategy.ShootStrat()) kb_strat2.add("z", m.strategy.DefStrat()) kb_strat2.add("e", m.strategy.GardienStrat()) kb_strat2.add("q", m.strategy.DribleStrat()) kb_strat2.add("s", m.strategy.PassStrat()) kb_strat2.add("d", m.strategy.AtkStrat()) kb_strat2.idp = 1 sortie = False i = 0 while (not sortie) and i < nb_snap: state = self.create_rd_state() kb_strat1.state = state kb_strat2.state = state self.affiche_rd_state(state, 900, 600) joueur_0 = input("Choose strategy for player red 0 : ") joueur_1 = input("Choose strategy for player red 1 : ") kb_strat1.send_strategy(joueur_0) kb_strat2.send_strategy(joueur_1) if input("wanna continue?") == "n": sortie = True i += 1 try: temp_joueur_1 = load_jsonz(fname1) temp_joueur_1 += kb_strat1.states dump_jsonz(temp_joueur_1, fname1) except FileNotFoundError: dump_jsonz(kb_strat1.states, fname1) try: temp_joueur_2 = load_jsonz(fname2) temp_joueur_2 += kb_strat2.states dump_jsonz(temp_joueur_2, fname2) except FileNotFoundError: dump_jsonz(kb_strat2.states, fname2)
def entrainer2v2_main(self, fname1, fname2, ia): if self.nb_players != 2: raise Exception( "Not the good number of players for this function.") #Creation d'une partie kb_strat1 = KeyboardStrategy() kb_strat1.add("a", m.strategy.ShootStrat()) kb_strat1.add("z", m.strategy.DefStrat()) kb_strat1.add("e", m.strategy.GardienStrat()) kb_strat1.add("q", m.strategy.DribleStrat()) kb_strat1.add("s", m.strategy.PassStrat()) kb_strat1.add("d", m.strategy.AtkStrat()) kb_strat2 = KeyboardStrategy() kb_strat2.add("u", m.strategy.ShootStrat()) kb_strat2.add("i", m.strategy.DefStrat()) kb_strat2.add("o", m.strategy.GardienStrat()) kb_strat2.add("j", m.strategy.DribleStrat()) kb_strat2.add("k", m.strategy.PassStrat()) kb_strat2.add("l", m.strategy.AtkStrat()) team1 = SoccerTeam(name="Control Team") #team2 = SoccerTeam(name="Sparing") team1.add("ControlPlayer", kb_strat1) team1.add("ControlPlayer", kb_strat2) #team2.add("Player",m.strategy.ShootStrat()) team2 = ia.get_team(2) simu = Simulation(team1, team2) #Jouer, afficher et controler la partie show_simu(simu) print("Nombre d'exemples : " + str(len(kb_strat1.states) + len(kb_strat2.states))) # Sauvegarde des etats dans un fichier if simu.get_score_team(1) >= simu.get_score_team(2): try: temp_joueur_1 = load_jsonz(fname1) temp_joueur_1 += kb_strat1.states dump_jsonz(temp_joueur_1, fname1) except FileNotFoundError: dump_jsonz(kb_strat1.states, fname1) try: temp_joueur_2 = load_jsonz(fname2) temp_joueur_2 += kb_strat2.states dump_jsonz(temp_joueur_2, fname2) except FileNotFoundError: dump_jsonz(kb_strat2.states, fname2)
def apprentissage(fn): ### chargement d'un fichier sauvegarder states_tuple = load_jsonz(fn) ## Apprentissage de l'arbre data_train, data_labels = build_apprentissage(states_tuple,my_get_features) dt = apprend_arbre(data_train,data_labels,depth=10) # Visualisation de l'arbre affiche_arbre(dt) genere_dot(dt,"test_arbre.dot") return dt
with open(fname, "wb") as f: pickle.dump(dt, f) return dt if __name__ == "__main__": entrainer("test_kb_strat.jz") dic_strategy = { FonceurStrategy().name: FonceurStrategy(), Milieu().name: Milieu(), Gardien().name: Gardien() } states_tuple = load_jsonz("test_kb_strat.jz") apprendre(states_tuple, my_get_features, "tree_test.pkl") with open("tree_test.pkl", "rb") as f: dt = pickle.load(f) # Visualisation de l'arbre genere_dot(dt, "test_arbre.dot") #Utilisation de l'arbre : arbre de decision, dico strategy, fonction de transformation etat->variables treeStrat1 = DTreeStrategy(dt, dic_strategy, my_get_features) treeteam = SoccerTeam("Arbre Team") team2 = SoccerTeam(name="Sparing") treeteam.add("Joueur 1", treeStrat1) treeteam.add("Joueur 2", treeStrat1) team2.add("Joueur 1", FonceurStrategy()) team2.add("Joueur 2", Gardien()) simu = Simulation(treeteam, team2) show_simu(simu)
team1.add("Jexp 2",strat_j2) team2 = SoccerTeam("team2") team2.add("rien 1", StaticStrategy()) team2.add("rien 2", StaticStrategy()) simu = Simulation(team1,team2) show_simu(simu) # recuperation de tous les etats sur les 2 joueurs training_states = strat_j1.states+strat_j2.states # sauvegarde dans un fichier dump_jsonz(training_states,"test_states.jz") ### chargement d'un fichier sauvegarder states_tuple = load_jsonz("test_states.jz") ### Transformation d'un etat en features def my_get_features(state,idt,idp): """ extraction du vecteur de features d'un etat, ici distance a la balle, distance au but, distance balle but """ p_pos= state.player_state(idt,idp).position f1 = p_pos.distance(state.ball.position) f2= p_pos.distance( Vector2D((2-idt)*settings.GAME_WIDTH,settings.GAME_HEIGHT/2.)) f3 = state.ball.position.distance(Vector2D((2-idt)*settings.GAME_WIDTH,settings.GAME_HEIGHT/2.)) return [f1,f2,f3] #Nom des features (optionel) my_get_features.names = ["ball_dist","goal_dist","ballgoal_dist"] ##### ## Apprentissage de l'arbre
team1.add("Manuel1", KBStrat) #team1.add("Manuel2", MS.DefenseBase()) team2.add("Auto1",MS.Attack2()) #team2.add("Auto2", MS.Intercept()) simu = Simulation(team1,team2,1000) """ show_simu(simu) training_states = KBStrat.states dump_jsonz(training_states,"game12.jz") """ states_tuple = load_jsonz("game11.jz")+load_jsonz("yannick.jz")+load_jsonz("game2.jz")+load_jsonz("game3.jz")+load_jsonz("game10.jz")+load_jsonz("game9.jz")+load_jsonz("game4.jz")+load_jsonz("game5.jz")+load_jsonz("game8.jz")+load_jsonz("game6.jz")+load_jsonz("game7.jz") data_train, data_labels = build_apprentissage(states_tuple,mes_params) dt = apprend_arbre(data_train,data_labels,depth=10) affiche_arbre(dt) genere_dot(dt,"test_arbre.dot") pickle.dump(dt,open("tree.pkl","wb")) dtree = pickle.load(open(os.path.join(os.path.dirname(__file__),"tree.pkl"),"rb")) dic = {"Dribbler":MS.Dribbler(),"Tirer":MS.Tirer(),"Degager":MS.Degager(),"Intercepter":MS.Intercept(),"Saligner":MS.Saligner()} treeStrat1 = DTreeStrategy(dtree,dic,MS.mes_params) treeStrat2 = DTreeStrategy(dtree,dic,mes_params) team3 = SoccerTeam("Arbre Team") #team3.add("Joueur 1",treeStrat1) team3.add("Arbre",treeStrat1) simu = Simulation(team3,team2) show_simu(simu)
from soccersimulator import show_simu, load_jsonz import sys try: input = raw_input except NameError: pass if __name__ == "__main__": if len(sys.argv) != 2: print("Usage : %s file.jz" % sys.argv[0]) sys.exit(0) tournoi = load_jsonz(sys.argv[1]) key = '' while key != 'q': tournoi.print_scores(True) key = input("Match ? : ") try: i, j = [int(x) for x in key.split(" ")] show_simu(tournoi.get_match(i, j)) except: key = 'q'
m.strategy.PassStrat().name: m.strategy.PassStrat(), m.strategy.AtkStrat().name: m.strategy.AtkStrat() } nb_players = input('Nombre de joueurs : ') nb_players = int(nb_players) ent = entrainer(nb_players) fname0 = "entrainer1v1" #fname1 = "entrainer2v2_snap_teste_1e" ent.entrainer_contre_tous(fname0 + ".jz") # ent.entrainer2v2_snap(fname0 + ".jz", fname1 + ".jz", 5) states_tuple0 = load_jsonz(fname0 + ".jz") #states_tuple1 = load_jsonz(fname1 + ".jz") ent.apprendre_1v1(states_tuple0, ent.my_get_features_1v1, fname0 + ".pkl") #ent.apprendre(states_tuple1,ent.my_get_features, fname1 + ".pkl") with open(fname0 + ".pkl", "rb") as f: dt0 = pickle.load(f) # Visualisation de l'arbre genere_dot(dt0, fname0 + "testeee.dot") #with open(fname1 + ".pkl","rb") as f: # dt1 = pickle.load(f) ##Visualisation de l'arbre #genere_dot(dt1,fname1 + "rtestee1.dot")