TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Maximize your Business Profitability with Python

Learn how to use linear programming and Python to help your local bakery maximize profitability by selling the right items.

Samir Saci
TDS Archive
Published in
7 min readApr 30, 2021

--

A graphic showing how to maximize bakery profitability by selecting the right items to sell. Three bakery items are listed: Cake (profit: 6€, preparation: 95 min), Croissant (profit: 0.9€, preparation: 55 min), and Sandwich (profit: 4.4€, preparation: 12 min). The graphic highlights the differences in profit margins and preparation times, helping business owners prioritize higher-margin, less time-intensive products to improve overall business profitability with Python.
Bakery Profitability Problem — (Image by Author)

Business profitability refers to a company's ability to generate income that exceeds its expenses over a certain period.

As a data scientist, can you help business owners maximize their profits?

Linear programming with Python can help.

This article will show you how to use Python and the PuLP library to optimize your bakery’s product selection, ensuring you focus on high-margin items.

Following our step-by-step guide, you can create a model that considers available resources, SKU profitability, and more.

Summary
I. Using Python to Optimize Your Business Profitability
1. Define the Problem: Maximizing Profit for a Local Bakery
2. Objective: Using Python to Determine the Optimal Production Plan
II. Building the Linear Programming Model with PuLP in Python
1. Declare your variables, parameters and model
2. Establish the objective function and add constraints
3. Solve your model and analyze the results
III. Become an Expert in Business Profitability
1. Inventory and Cash Flow Management
2. Generative AI for Business Optimization
3. Automatically Generate Business PowerPoint Slides with Python

Using Python to Optimize Your Business Profitability

Problem Statement: Maximizing Profit for a Local Bakery

You want to help your local bakery to maximize its profit. They sell several types of products, including cakes, pastries and sandwiches.

Available resources

  • 4 bakers working 6 hours per day (Total: 24 hours/day)
  • 1 assistant for sandwiches 4 hours per day
  • 2 ovens that can be used 24 hours per day (Total: 48 hours/day)
  • 100 slots available for stock and display

What do they sell?

These items have different resource needs and profit level

Resources needed for each of your SKUs
Resources needed for each of your SKUs — (Image by Author)

Objective: Define the Optimal Production Plan

The business owner would like to understand what this is the best mix of products to maximize its profits.

Chocolate cakes are highly profitable, but they require a long oven time.

This is where Linear Programming with Python can help find the optimal balance while respecting the constraints.

What do you need to produce to maximize your daily profit?

Let us run the model, defined in the next section, to determine the optimal mix.

  • Results
Lemon Cake = 0
Sandwich = 20
Chocolate Cake = 32
Croissant = 0
Chocolate Eclair = 0
Panini = 0
Profit reached: 328 euros/day

Based on these results, your local bakery owner should focus on Chocolate Cakes and Sandwiches to reach a maximum profit of 328 euros/day.

Can we do better? Let’s have a look at the constraints.

Implementing more constraints and parameters can improve this result, ensuring that your model reflects the actual reality of your business.

  • What is the utilization ratio of your resources?
Lemon Cake = 0
Sandwich = 20
Chocolate Cake = 32
Croissant = 0
Chocolate Eclair = 0
Panini = 0
1,440/1,440 minutes of bakers used
2,880/2,880 minutes of oven used
240/240 minutes of assistants working time used
126/200 display slots
Profit reached: 328.0 euros

Good news! You are fully utilizing your equipment and workforce.

However, we still have display slots available.

  • What if we have two more assistants to make sandwiches?
Lemon Cake = 0
Sandwich = 60
Chocolate Cake = 32
Croissant = 0
Chocolate Eclair = 0
Panini = 0
Profit reached: 504 euros
  • If you recruit two more assistants (4 hours/day), selling more sandwiches can increase your profit by 50%.

Please sense the type of discussions you could have with the business owner to help her improve the profitability.

This is a simple model that can be built with a few lines of code, let’s see how in the next section.

🏫 Discover 70+ case studies using data analytics to automate decision-making 🧑‍💼 and support business optimization 🏪 in this Cheat Sheet

Building the Linear Programming Model with Python

We will be using the Python PuLP library.

PuLP is a Python modelling framework for linear (LP) and Integer Programming (IP) problems maintained by the COIN-OR Foundation (Computational Infrastructure for Operations Research).

How do we start?

Declare your variables, parameters and model

  • LpMaximize: Your objective is to maximize your profit
  • lowBound =0: You cannot have a negative value of pastries
  • cat = ‘Integer’: Let’s try to sell entire croissants :)

These parameters can be loaded from an Excel file.

How do we set the objective of maximizing profits?

Establish the objective function and add constraints.

You can only add a limited number of constraints.

However, understand that each constraint is reducing the space of available solutions.

How do we find the optimal solution?

Solve your model and analyze the results

You now have a complete script to provide you with the optimal solution.

Note that you can visualize the parameters and play with the solutions using the variables.

If you need complete code, check this GitHub repository 👇

If you prefer, you can have a look at the video version

Become an Expert in Business Profitability

The approach presented here provides analytical support for business decision-making.

This is an opportunity to improve your business acumen while impacting small and large businesses.

What’s next? Business Planning with Python

Inventory and Cash Flow Management

This article inspired one of my friends, who is a small business owner.

Illustration showing the supply chain flow from supplier to customer, including stages such as freight transport, warehouse storage, and delivery. The icons represent each stage: supplier, freight via air and sea, warehouse, delivery by truck, and customer, visually emphasizing the key elements in inventory and distribution management for small businesses.
Business model of my friend — (Image by Author)

His company sells cups made from sustainable materials to distributors, retailers and coffee shops.

My friend: I am struggling with inventory and cash flow management.

Illustration of a small business supply chain model showing key elements like supplier costs per unit, freight costs per unit, warehouse storage costs per unit per week, and delivery costs per unit. The image also includes service level agreements (SLA), inventory management processes, sales channel strategy, and customer pricing per unit, visually representing the cost and revenue flows for inventory and cash flow management.
Overall Business Model — (Image by Author)

Their sales growth was limited by their inventory, which was itself limited by the cash flow.

As a data scientist, how could I help him?

In a series of articles, I detail the approach and methodology used to build a model that provides insights on optimising this business.

For more information,

Have you heard about Generative AI?

Generative AI for Business Optimization

I wanted to learn to use large language models (LLMs), so I experimented with their usage with this prototype.

This LangChain Agent is powered by GPT and connected to the transportation system database.

A diagram showing an automated supply chain control tower workflow with GPT and Langchain starting with ambiguous input (represented by question marks), proceeding through SQL queries, machine learning analysis, and generating insights that are communicated to users in an understandable form.
Supply Chain Control Tower Agent with LangChain SQL Agent [Article Link] — (Image by Author)

It is showing impressive performance.

It can answer operational questions autonomously by querying a database and extracting the results.

Can we create a super agent for Business Optimization?

The idea would be to create a Business GPT super agent with the GPT builder by including.

  • Advanced optimization models are written in Python in a core module
  • Documentation, articles and context to understand how to use them
This diagram outlines how the custom GPTs for Supply Chain Analytics work. It starts with the user asking a question or requesting an analysis, the agent retrieves data (from the provided dataset or sample), processes it with a core Python script, and returns output as charts or comments. The flow clearly illustrates three key steps: initial prompt, data processing using the script, and final analysis outputs that is used by “The Supply Chain Analyst”.
Example of Architecture — (Image by Author)

We have an agent who can interact with the users to build a business strategy.

If you want to follow me on this project, check 👇

How can you share the insights?

I need to prepare a PowerPoint …

Automate PowerPoint Creation with Python

Let us assume you spent several days fine-tuning the model to get the most efficient business plan for your bakery owner.

How do you present the results?

Unlike what I did here, you don’t want to present raw results with complex notations.

It would be best to convey your idea using a visual communication tool, such as graphs, figures, and comments.

Like everybody in the corporate world, you need to prepare PowerPoint slides.

But don’t worry, I got your back.

In another article, I shared a straightforward workflow to automatically generate PowerPoint slides with Python.

A flowchart illustrating the automated creation of PowerPoint slides for supply chain reports. It starts with 5 weeks of operational data from a warehouse management system database. SQL queries extract prepared order lines, which are processed by Python scripts to calculate KPIs. The output is a PowerPoint report with weekly warehouse workload and order profile visuals.
Process in 4 steps — (Image by Author)

You can easily adapt it to this model and avoid the hurdle of manually preparing PowerPoint slides.

For more information, have a look at the article linked below 👇

About Me

Let’s connect on Linkedin and Twitter. I am a Supply Chain Engineer who uses data analytics to improve logistics operations and reduce costs.

For consulting or advice on analytics and sustainable supply chain transformation, feel free to contact me via Logigreen Consulting.

If you are interested in Data Analytics and Supply Chain, look at my website.

💌 New articles straight in your inbox for free: Newsletter
📘 Boost your Productivity with Data Analytics: Productivity Cheat Sheet

References

  • Computational Infrastructure for Operations Research, Optimization with PuLP (Documentation)
TDS Archive
TDS Archive

Published in TDS Archive

An archive of data science, data analytics, data engineering, machine learning, and artificial intelligence writing from the former Towards Data Science Medium publication.

Samir Saci
Samir Saci

Written by Samir Saci

Top Supply Chain Analytics Writer — Case studies using Data Science for Supply Chain Sustainability 🌳 and Productivity: https://bit.ly/supply-chain-cheat

No responses yet