Fantasy Cricket — Application of Linear Programming

A step-by-step guide using Python (PuLP package)

Lakshmi Ajay
Towards Data Science

--

The Indian Premier League (IPL) is the biggest domestic cricket league on the globe. In the year 2020, the IPL title sponsors were Dream11. Dream11 is a fantasy sports platform based out of India. It allows users to play fantasy cricket, hockey, football, kabaddi, and basketball.

In this article, we will be applying Prescriptive Analytics to sports where we will focus on selecting a fantasy cricket team with one of the matches as an example.

Image from Unsplash: source

Fantasy Cricket in a Nutshell

If you are new to fantasy cricket then this is how it works

  • User needs to create a fantasy cricket team
  • Users can then participate in a contest with this team.
  • Each player is assigned a credit value. There is an upper limit on the total credit points in a team.
  • Each player will fall into one of the following categories: batsman, wicket-keeper, bowler, all-rounder
  • During the match, points are assigned to the players based on their performance & category.
  • At the end of the match, based on the points, a team with the maximum points that adhere to all the rules will be the winning combination of the best 11 players.

Rules of the game

There are a set of rules that needs to be followed to build a fantasy team.

The rules could vary across different fantasy platforms and needs to be adjusted accordingly.

  • A team should have 11 players in total
  • There should be 1 to 3 all-rounders in the team
  • There should be 1 to 3 wicket-keepers in the team
  • There should be 3 to 6 batsmen in the team
  • There should be 3 to 6 bowlers in the team
  • There should be 4 to 7 players from each side
  • Total credit points of the team should not exceed 100

How to pick the players

Additionally, for every match, the following information is available for each of the players:

  1. Points made by the player in the previous matches of the same tournament
  2. Average points per match for a player
  3. No. of times the player has been in the best11 team in the past matches of the same tournament
  4. Percentage of times the player has been in the best11 team in the past

Any of the above metrics can be used to pick the best team.

Linear Programming

Linear Programming in Operations Research is one of the scientific techniques that is used to get an optimum solution to the given business problem by taking resource scarcity and constraints into account.

Linear programming allows us to model real-life problems into mathematical models represented by linear relationships. It involves an objective function to be defined based on what has to be achieved and the creation of constraints as linear inequalities. As the name clearly specifies, the relationship between the objective function and constraints should be linear in nature (one-degree equations).

Quick Recap of Linear Programming

Here is a quick recap of linear programming with a simple example.

Basic Terminologies in Linear Programming

Objective Function: An objective function is a linear function that we are trying to solve. The value of this function needs to be either minimized or maximized based on the problem we are trying to solve.

Decision Variables: The variables that are used as input to decide the final output are known as decision variables. It is also known as the target variables.

Constraints: Constraints are the limitations put on the decision variables.

The final objective function has to be achieved on the decision variables with all the constraints applied. Here is a simple example of a linear programming problem.

Simple Example

Say, we want to maximize z, given two variables x & y which need to follow the given constraints.

Objective Function: Maximize z, where z= 2x + 3y

Decision Variables: x & y

Constraints:
x >= 0
y >= 1
x + 2y <= 15
-2x + 4y >= -7
-2x + y <= 4

Plotting all the lines for the above constraints gives a graph as below. Here the shaded region is the common area where all the above constraints hold true.

The goal of the linear program is to identify the values of x & y within this shaded region that will provide the maximum possible value of z.

Simple Example — Linear Programming (Image by Author)

Solving this linear program gives a maximized value of z as 27 at x=9 & y=3.

NOTE: Refer to the git repo provided at the end to access the Python code to generate the above graph and also solve this linear programming problem using PuLP.

Fantasy Cricket & Linear Programming

Linear programming can be used to solve many real-life problems and fantasy cricket is one of them.

To create a fantasy team, one needs to select the best players based on an objective. The final team formed should follow all the rules or constraints put by the platform.

This makes it a perfect problem that can be solved with Linear Programming. We need to maximize the team’s selection metric projection while respecting the given budget (credits) and team composition constraints.

Understanding Linear Programming with a simple example and mapping it with Fantasy Team Generation (Image by Author)

The above mapping illustrates how each of the basic components of linear programming can be linked to a simple example and to this case study of generating a fantasy team.

What is PuLP?

PuLP is an open-source python package to solve optimization problems. It provides interfaces to convert a mathematical program into a form that can feed into the solvers.

PuLP works entirely within the syntax of the Python language by providing Python objects that represent optimization problems and decision variables and allowing constraints to be expressed in a way that is very similar to the original mathematical expression.

To keep the syntax as simple and intuitive as possible, PuLP focuses on supporting linear and mixed-integer models.

PuLP supports solvers like GLPK, CPLE, COIN, GUROBI. By default, PuLP uses the CBC solver (COIN-OR Branch-and-Cut solver).

Refer to the following link for more details on PuLP: https://pypi.org/project/PuLP/

Solving the Linear Problem in Python using PuLP

We will be using the python package PuLP to create the linear problem and solve it.

In addition to the provided rules, additional constraints can be added for the users to play around and create the best-suited team.

Once an optimal solution is identified, it will generate the fantasy team along with some summary details. Also, we will be using the ipywidgets package to build the user interface in the Jupyter notebook so that we can play around with the constraints.

The code snippets below are a part of the complete code to help the readers understand the logical flow.

Package installations

In addition to the regular packages like NumPy, pandas, sklearn, this code also requires the installation of pulp. In case you are using pip, all the packages can be installed with the ‘pip install pulp’ command.

Input Data

The program will require two kinds of input. One will be the player details and the other will be the user configurations to build the linear problem.

The player details are created in a CSV file which is read into a pandas dataframe. The sample contents of the file will be like below.

This data is currently not available online and has been hand-curated for this case study.

Sample contents of the input data (Image by Author)

Create the Linear Programming problem

The first step in solving the linear programming problem is to create the problem. This can be done using the PuLP package.

Create the decision variables

Prepare the decision variables required to create the objective function and the constraints.

Similar variables should be created for each of the constraints. Shown above is a sample. The same approach can be used to create all the required variables.

Defining the objective function

Here the objective function could be defined based on one of the below:

  • Maximize based on each player’s points
  • Maximize based on no. of times the player has been in the best11 team in the past
  • Maximize based on average points per match
  • Maximize based on percentage times player is been in best11 team

This choice can be given to the user to configure based on which the objective function can be set.

Adding the constraints

Once the linear programming problem is created and the objective function defined, we are now ready to add the constraints to the problem.

Constraint based on the number of players

There should be exactly 11 players in the final team. This condition can be set with the below constraint.

Constraint based on players from Team 1

Each team can have 4 to 7 players. This can be configured further by the user if required.

Constraint based on players in the line-up

Since the playing 11 will be known only during the toss (normally 30 minutes before the start of the match), a constraint can be added on the players to be picked.

  • Select from all players (default)
  • Select only players who have played the last match
  • Select only players who have played at least one match
  • Select only players who are in the playing 11 of that match

Above are some samples of the different constraints. Similarly, all the required constraints can be set.

Ready to solve?

Now that the constraints are added and the objective function is defined, we are ready to solve the problem.

Since the goal is to maximize the objective, set the objective details to the problem accordingly and allow PuLP to solve it.

PuLP’s representation of the Mathematical Program

Once the details are provided to PuLP on what to solve, we can display the problem details by printing the problem object.

The sample output of printing the problem is as below. Here we have considered only 3 constraints for ease of illustration. Please refer to the git repo to view the results of all the constraints.

Sample output of PuLP’s problem representation (Image by Author)

There are mainly three types of information to solve the linear programming problem:

  1. The objective function that has to be maximized or minimized
  2. The constraints or rules that need to be followed while maximizing/minimizing
  3. The input variables on which the objective function and constraints have to be applied

Now let us try to understand PuLP’s internal representation for each of these.

PuLP’s representation of the objective function

Representation of the Objective Function in PuLP (Image by Author)
  • Remember, we created the target binary variable to decide if each player will be in the fantasy team or not
  • Each player has a unique player number that is used to create an identifier for that player
  • We have set the objective function to maximize based on the player points
  • PuLP has used this information to derive the objective function which is the weighted sum of points for each player.
  • The goal of this linear programming problem is to find a combination of players that will maximize this total sum.

PuLP’s representation of the constraints

Representation of the Constraints in PuLP (Image by Author)
  • Since we are dealing with binary target variables here, the sum of all the players matching the constraint is considered
  • For example, for the constraint on wicket-keepers, only the player numbers with type WK will be considered. Similarly for other types like batsmen and bowlers.
  • PuLP will internally hold all the constraints added to the LPProblem. The final optimal solution will only consider combinations that satisfy all the constraints.

PuLP’s representation of the variables

Decision/Target variable representation in PuLP (Image by Author)
  • Decision/target variables will be created for all the players in the dataset.
  • The objective function and the constraints are applied on these set of variables only

Interpreting the results

PuLP will try to solve the problem by identifying the best selection that will maximize the objective function and also fulfill all the constraints that have been set.

In case it is not able to find any feasible solution with the given constraints, then it will return the status as infeasible. In this case, PuLP drops the constraints that cannot be fulfilled and returns an output.

Sample output

Image by Author

Performance of the Solver

Since the data is small with 46 variables the performance of the solver is not an issue. It took less than a second in my local system (with 16 GB RAM).

The solver ran through 32 iterations to find an optimal solution with a maximum score of 2731 to decide on the final 11 players with the given constraints.

Logs from CBC Solver (Image by Author)
Final Optimal Solution Logs (Image by Author)

Playing around with the data

To make the application more interactive, ipywidgets have been used so that the configuration values can be selected by the user on a Jupyter notebook.

We will not go through the details of creating the widgets on the Jupyter notebook as a good amount of documentation is already available for it and also it is not our area of focus here.

Image by Author

These configurations are internally converted to either the objective function or constraints.

Configuring and creating a team — Sample Result

Give your choice of constraints and click on the “CREATE FANTASY TEAM” button.

Image by Author
Image by Author

BANG!!! You are ready with your fantasy team to start participating in the contest!! All the best :-)

Conclusion

This article is a simple example of how to apply the linear optimization technique to cricket to pick the best players. The principles of linear optimization can be applied to many problems that have a fundamentally simple goal.

Try it out and play with it. To explore more, we can get in some more historical data for each of the players and add that to the selection constraints. Though this may not make the elite team always, it will surely guide the user while picking the team.

Disclaimer: the fantasy team is only suggestive based on the data provided and the constraints given. It is not guaranteed that these players will be in the best11 team at the end of the match. Please use your own judgment while picking the team :-)

--

--