This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # initialise teams | |
| t1 = Team( | |
| name='Sydney Sixers', bat_order=t1_pids, bat_lookup=bat_lookup, bwl_lookup=bwl_lookup, | |
| mtc_ply_df=mtc_ply_df | |
| ) | |
| t2 = Team( | |
| name='Brisbane Heat', bat_order=t2_pids, bat_lookup=bat_lookup, bwl_lookup=bwl_lookup, | |
| mtc_ply_df=mtc_ply_df | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MatchSimulator: | |
| def __init__(self, t1, t2, ground, model, ave_score=160, verbose=False, pid2pname=None): | |
| self.t_bat = t1 # batting team | |
| self.t_bwl = t2 # bowling team | |
| self.model = model # ball prediction model | |
| self.verbose = verbose # flag to run simulation in verbose mode | |
| self.pid2pname = pid2pname # dictionary mapping player ids to player names (for running in verbose mode) | |
| self.res_opts = ['res_0', 'res_1', 'res_2', 'res_3', 'res_4', 'res_6', 'res_W', 'res_WD'] # list of ball result options |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Team: | |
| def __init__(self, name, bat_order, bat_lookup, bwl_lookup, mtc_ply_df): | |
| self.name = name # team name | |
| self.bat_lookup = bat_lookup # batter stats dictionary | |
| self.bwl_lookup = bwl_lookup # bowler stats dictionary | |
| self.mtc_ply_df = mtc_ply_df # historical player statistic (pandas dataframe) | |
| self.order = {i+1:x for i, x in enumerate(bat_order)} # batting order | |
| self.p_header = ['W','0','1','2','3','4','6','WD'] | |
| # batting innings state |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # initial population | |
| pop = [Robot() for x in range(pop_size)] | |
| results = [] | |
| # run evolution | |
| for i in tqdm(range(num_gen)): | |
| scores = np.zeros(pop_size) | |
| # iterate through all robots | |
| for idx, rob in enumerate(pop): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Robot: | |
| """ | |
| Class for representing a rubbish-collecting robot | |
| """ | |
| def __init__(self, p1_dna=None, p2_dna=None, m_rate=mutation_rate, w_pen=wall_penalty, nr_pen=no_rub_penalty, r_score=rubbish_score): | |
| self.m_rate = m_rate # mutation rate | |
| self.wall_penalty = w_pen # penalty for crashing into a wall | |
| self.no_rub_penalty = nr_pen # penalty for picking up rubbish in empty cell | |
| self.rubbish_score = r_score # reward for picking up rubbish | |
| self.p1_dna = p1_dna # parent 1 DNA |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Environment: | |
| """ | |
| Class for representing a grid environment full of rubbish. Each cell can be: | |
| 'o': empty | |
| 'x': rubbish | |
| 'w': wall | |
| """ | |
| def __init__(self, p=rubbish_prob, g_size=grid_size): | |
| self.p = p # probability of a cell being rubbish | |
| self.g_size = g_size # excluding walls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Import Packages | |
| """ | |
| import numpy as np | |
| from tqdm.notebook import tqdm | |
| """ | |
| Set Parameters | |
| """ | |
| # Simulation Settings |