Skip to content

Instantly share code, notes, and snippets.

# 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
)
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
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
@andrewjkuo
andrewjkuo / robby-robot-4.py
Created August 20, 2020 06:46
Robby the robot implementation (part 4)
# 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):
@andrewjkuo
andrewjkuo / robby-robot-3.py
Created August 20, 2020 00:32
Robby the robot implementation (part 3)
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
@andrewjkuo
andrewjkuo / robby-robot-2.py
Created August 19, 2020 23:49
Robby the robot implementation (part 2)
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
@andrewjkuo
andrewjkuo / robby-robot-1.py
Last active August 19, 2020 23:49
Robby the robot implementation
"""
Import Packages
"""
import numpy as np
from tqdm.notebook import tqdm
"""
Set Parameters
"""
# Simulation Settings