@@ -7,8 +7,7 @@ running genetic algorithms to optimize teams.
77Thank you to github copilot for helping me write this document lmao
88
99``` python3
10-
11- team = generate_team(format = ' gen8randombattle' , seed = 1234 )
10+ team = generate_team(format = " gen8randombattle" , seed = 1234 )
1211
1312# a team object contains a list of 6 pokemon objects with their moves, items, etc.
1413# it's also tracking the (normalised) number of wins/losses/ties for this team.
@@ -30,12 +29,12 @@ team = generate_team(format='gen8randombattle', seed=1234)
3029# here's an idealised Bot class that uses these same env variables to run a showdown bot, but more cleanly:
3130bot_1 = Bot(
3231 team,
33- strategy = ' safest' ,
34- websocket_uri = ' sim.psim.us:8000' ,
35- user = ' whimsicaldreams' ,
36- password = ' password' ,
37- bot_mode = ' CHALLENGE_USER' ,
38- format = ' gen3ou' ,
32+ strategy = " safest" ,
33+ websocket_uri = " sim.psim.us:8000" ,
34+ user = " whimsicaldreams" ,
35+ password = " password" ,
36+ bot_mode = " CHALLENGE_USER" ,
37+ format = " gen3ou" ,
3938 num_battles = 1 ,
4039 save_replay = False ,
4140)
@@ -47,25 +46,27 @@ bot_1 = Bot(
4746# now we can run a battle between two bots:
4847bot_2 = Bot(
4948 team,
50- strategy = ' safest' ,
51- websocket_uri = ' sim.psim.us:8000' ,
52- user = ' melonchomper' ,
53- password = ' password' ,
54- bot_mode = ' CHALLENGE_USER' , # this would need to be ACCEPT_CHALLENGE
55- format = ' gen3ou' ,
49+ strategy = " safest" ,
50+ websocket_uri = " sim.psim.us:8000" ,
51+ user = " melonchomper" ,
52+ password = " password" ,
53+ bot_mode = " CHALLENGE_USER" , # this would need to be ACCEPT_CHALLENGE
54+ format = " gen3ou" ,
5655 num_battles = 1 ,
5756 save_replay = False ,
5857)
5958
6059# from a little google searching, it looks like we can run these bots in parallel using asyncio:
6160
61+
6262async def run_battle (bot_1 , bot_2 ):
6363 # individually run the showdown bots at the same time, and wait for the results
6464 await asyncio.gather(bot_1.challenge(bot_2), bot_2.accept(bot_1))
6565 # parse the results
6666 # update the win/loss/tie counts for each team
6767 ...
6868
69+
6970run_battle(bot_1, bot_2)
7071
7172# within this function, we'll need to:
@@ -74,7 +75,20 @@ run_battle(bot_1, bot_2)
7475# - update the win/loss/tie counts for each team
7576
7677# here's a more generic example, where we specify a number of battles for each team, and then run them all:
77- bots = {f ' bot { i} ' :Bot(team, strategy = ' safest' , websocket_uri = ' sim.psim.us:8000' , user = f ' bot { i} ' , password = ' password' , bot_mode = ' CHALLENGE_USER' if i % 2 == 0 else ' ACCEPT_CHALLENGE' , format = ' gen3ou' , num_battles = 10 , save_replay = False ) for i in range (10 )}
78+ bots = {
79+ f " bot { i} " : Bot(
80+ team,
81+ strategy = " safest" ,
82+ websocket_uri = " sim.psim.us:8000" ,
83+ user = f " bot { i} " ,
84+ password = " password" ,
85+ bot_mode = " CHALLENGE_USER" if i % 2 == 0 else " ACCEPT_CHALLENGE" ,
86+ format = " gen3ou" ,
87+ num_battles = 10 ,
88+ save_replay = False ,
89+ )
90+ for i in range (10 )
91+ }
7892
7993# num_battles = 10 would mean that bot would challenge the same bot 10 times, so we can make every bot fight every other bot 10 times
8094
@@ -85,14 +99,18 @@ for bot_1, bot_2 in itertools.combinations(bots.values(), 2):
8599 if bot_1 != bot_2:
86100 try :
87101 # make sure bot_1 is the challenger and bot_2 is the acceptor
88- bot_1.bot_mode = ' CHALLENGE_USER'
89- bot_2.bot_mode = ' ACCEPT_CHALLENGE'
102+ bot_1.bot_mode = " CHALLENGE_USER"
103+ bot_2.bot_mode = " ACCEPT_CHALLENGE"
90104
91105 # check if either bot is currently in a battle
92106 if bot_1.in_battle or bot_2.in_battle:
93- print (f ' Bot { bot_1} or { bot_2} is currently in a battle, waiting for them to finish... ' )
107+ print (
108+ f " Bot { bot_1} or { bot_2} is currently in a battle, waiting for them to finish... "
109+ )
94110 while bot_1.in_battle or bot_2.in_battle:
95- await asyncio.sleep(1 ) # hopefully this will wait for the battle to finish
111+ await asyncio.sleep(
112+ 1
113+ ) # hopefully this will wait for the battle to finish
96114
97115 # run the battle
98116 bot_1.in_battle = True
@@ -105,7 +123,7 @@ for bot_1, bot_2 in itertools.combinations(bots.values(), 2):
105123
106124 except Exception as e:
107125 print (e)
108- print (f ' Error running battle between { bot_1} and { bot_2} ' )
126+ print (f " Error running battle between { bot_1} and { bot_2} " )
109127
110128# ensure we're only here once every bot has fought every other bot
111129while any (bot.in_battle for bot in bots.values()):
0 commit comments