Cricket Fantasy Team Optimization System


Script Preview

Overview

This project provides a system for optimizing fantasy cricket team selection using player performance data and roster information. It calculates player form scores based on recent match data and uses these scores to select an optimal team under specific constraints, leveraging a modular design and linear programming techniques.

Key Features

GitHub

The project is open source and available on GitHub. You can clone and contribute to the project.

Repository

github.com/adimail/fantasy-sports-machine-learning

Clone the Repository

git clone https://github.com/adimail/fantasy-sports-machine-learning.git
cd fantasy-sports-machine-learning

Branch Structure

Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Issues

Feel free to file a new issue if you find a bug or want to suggest an enhancement. Before creating a new issue, please check if a similar issue already exists.

Type Template
Bug Report Include steps to reproduce, expected behavior, and actual behavior
Feature Request Describe the feature and its potential benefits
Question Provide context and what you've tried so far

Usage

Build Team from Latest Data

To build a fantasy team using the latest player form data, run:

python3 src/buildteam.py

Or, if integrated with a main script:

python3 main.py --build

Note: Ensure the roster data is in the SquadPlayerNames.csv file, located as specified in config.yaml (typically under a data directory or Downloads folder).

Update Player Form Data

To update player form scores without building a team, run:

python3 -m src.playerform

This calculates form scores based on recent match data specified in config.yaml.

Project Structure

project_root/
├── main.py                     # Main entry point for the application
├── Dockerfile                  # Container configuration for Docker deployment
├── config.yaml                 # Configuration settings and parameters
├── requirements.txt            # Python package dependencies
└── src/                        # Source code directory
    ├── __init__.py             # Makes src a Python package
    ├── geneticalgorithm.py     # Genetic algorithm implementation for team optimization
    ├── lifetimeperformance.py  # Calculates historical player performance metrics
    ├── scrapper.py             # Data scraping utilities for player statistics
    ├── playerform.py           # Player form score calculation
    └── buildteam.py            # Team optimization and selection logic

Getting Started

Set Up Virtual Environment

  1. Create and activate a virtual environment:
    python3 -m venv .venv
    source .venv/bin/activate   # On Windows: .venv\Scripts\activate
  2. Install dependencies:
    pip3 install --no-cache-dir -r requirements.txt

Run the Team Builder

python3 main.py --build

Alternatively, you can use Docker:

docker build -t fantasy-team-builder .
docker run -it --rm fantasy-team-builder

Algorithm

The system consists of two main modules:

1. Player Form Calculation (playerform.py)

This module processes historical match data to compute performance scores for each player.

Process Overview:

Example Output:

Player Batting Form Bowling Form Fielding Form Credits Player Type Team
Kane Williamson 85.2 10.5 45.3 9.0 BAT NZ
Shaheen Afridi 20.1 90.7 30.2 9.0 BOWL PAK
2. Fantasy Team Optimizer (buildteam.py)

This module selects an optimal 11-player team based on form scores and roster data.

Process Overview:

Example Output:

Score Role Player Position
93.66 Batsmen Daryl Mitchell Captain
80.18 Batsmen Fakhar Zaman Vice Captain
85.20 Batsmen Kane Williamson Player
76.07 Bowler Haris Rauf Player
74.66 Batsmen Babar Azam Player
90.70 Bowler Shaheen Afridi Player
70.75 All Rounder Glenn Phillips Player
68.82 Wicket Keeper Mohammad Rizwan Player
65.30 All Rounder Michael Bracewell Player
70.61 Bowler Abrar Ahmed Player
60.40 All Rounder Agha Salman Player

Docker Commands

Prerequisites

Ensure you have the following files in your ~/Downloads folder:

Commands

  1. Load the Model:
    docker load -i sinister6.tar
  2. Run the Container:
    touch ~/Downloads/sinister6.csv
    docker run --rm \
      -v ~/Downloads/SquadPlayerNames.csv:/app/data/SquadPlayerNames.csv \
      -v ~/Downloads/sinister6.csv:/app/sinister6.csv \
      sinister6

Input Data Format

Roster CSV Example (SquadPlayerNames.csv):

Credits Player Type Player Name Team IsPlaying lineupOrder
7.5 ALL Michael Bracewell NZ PLAYING 7
8.5 BAT Babar Azam PAK PLAYING 2
9 BOWL Shaheen Afridi PAK PLAYING 8

Output File Format

Team Selection Output Example:

Player Role Team Position
Daryl Mitchell Batsmen NZ Captain
Fakhar Zaman Batsmen PAK Vice Captain
Babar Azam Batsmen PAK Player

Conclusion

Explanation of How the Model Works

The Fantasy Team Optimization System operates in two primary stages:

1. Player Form Calculation (playerform.py)

2. Fantasy Team Optimizer (buildteam.py)

Integration

This two-stage process ensures a robust and strategic approach to fantasy team selection, blending historical performance with current form data.

Happy team building!