Optimization with Python-doCplex- Beginners Guide
Are you passionate about Python ?
If someone, asked me that question. I would say a Big yes.
Why would I say so ?
Python is a good programming language we can learn quickly, and it can be used in many areas. If someone is working with data or analytics, developing Python skills is a good place to start. In my case I was able to learn considerable amount within one year. I have worked with couple of other languages sometime back but Python is the one that I have come to love the most.
You are also keen on Optimization ?
Optimization is very interesting area we can solve extremely complex problems in Management Science. You, probably might have seen below graph before.
Optimization sits in the Prescriptive analytics section of the graph. It creates high value and is more difficult compared to the rest.
What exactly is Optimization ?
Optimization is used to find solutions for unknown variables, given a set of constraints and an objective to maximize or minimize. Complex business, engineering, or economic problems need to be modelled in into a structured mathematical form. A mathematician specialising in optimization could do this rather complex activity, but now there are software that can automatically produce the mathematical model give the constraints, variables, objectives. This has made optimization accessible for those without high mathematical skills.
Still not sure what this is ?
Let’s try to understand this using a simple Optimization problem and later in this article we are going to solve the problem.
A manufacturing company produces 2 types of Cars. ‘CarA’ & ‘CarB’. In manufacturing cars there are some constraints (limitations) that company needs to work within. The profit generated from each car is also going to be different. The company needs to know how much of ‘CarA’ and ‘CarB’ should be produced in order to maximize the profits (the objective).
Assembly line capacity constraints
Say it takes 0.5 Days to assemble ‘CarA’ and 0.25 Days to assemble ‘CarB’. Each month there is only 20 days allocated to assembling.
Painting and finishing constraints
It takes 0.15 days to paint one ‘CarA’ and 0.10 days for one ‘CarB’ .Each month there is only 10 Days available for the painting and finishing processes.
Other constraints
There is a contractual requirement to manufacture a minimum of 10 units of both cars.
Profit maximizing — Objective
Profits by selling one car ‘CarA’ is $12000 and $15000 by selling one ‘CarB’.
Question:
In order to maximize profit how many units need to be produced from each type of car ?
How do we solve this kind of problem ?
There are many ways you can solve optimization problems. Using Graphs, Excel Solver, Online tools, manually calculation (simplex) are some of them.
However when solving large and complex problems we need to use Optimization engines that can handle multiple variables and solve these problems faster.
Why is IBM Cplex considered as a good Optimization solver ?
IBM’s Cplex Optimization studio is a good platform to solve Optimization problems. Cplex also provides a community edition with limited capabilities of 5000 constraints and 5000 variables. Anyone interested in optimization can use this to get hands on experience.
You can download the Cplex community edition here: https://www.ibm.com/products/ilog-cplex-optimization-studio
After downloading Cplex you can model Optimization problems using OPL (Optimization Programming Language). If you are beginner and need to understand OPL syntax and about various problems domain knowledge you don’t need to worry. Cplex Optimization studio has its own set of examples from mathematical programming to constraint programming (scheduling problems) that you can practice.
Below is a screen shot of it and you can try it later.
I am python guy and I don't like OPL can I code from Python ?
Yes, you can do it.
I have provided the steps below from installing Python to solving a simple Optimization problem using Python. If you are fresher to Python and need to download Python start with the first step. Otherwise you can skip it and proceed to the next step.
Download the Python 3.7.7 from below link
https://www.python.org/downloads/release/python-377/
Download the Python .exe file as required to your system requirements.
Install the downloaded Python 3.7.7 file.
Installing the required packages to solve Optimization problems
After successfully installing Python, the next step is to install the library files (packages). Mainly there are two packages need to be installed: doCplex and Cplex
Open your command prompt and run the below lines from it. Make sure you are connected to internet since the packages are going to be downloaded.
doCplex package
py -3.7 -m pip install doCplex
Cplex package
py -3.7 -m pip install Cplex
Below is a Screen shot of Cplex being installed from command prompt.
If you need any other packages (e.g. for your data preparation and handling) you can easily use pip command and install that required packages.
Imagine you need Pandas package, then you just need to enter
py -3.7 -m pip install pandas
(For the demo in this article Pandas would not be required.)
Our car problem is tabled below.
We can model and get the optimized solution by following these 6 Python scripts.
1. Import the model class
# from docplex package import the model class
from docplex.mp.model import Model
2. Create the model
# create a model instance, with a name. You can assign any name to the model
my_model = Model(name='Car_Production')
3. Define the decision variables
CarA = my_model.integer_var(name='CarA')
CarB = my_model.integer_var(name='CarB')
4. Add the constraints
# constraint
#1: CarA production is greater than 10
my_model.add_constraint(CarA >= 10)# constraint
#2: CarB production is greater than 10
my_model.add_constraint(CarB >= 10)# constraint
#3: Assembly Line has a maximum capacity limitation
ct_assembly = my_model.add_constraint( 0.5 * CarA + 0.25 * CarB <= 20)# constraint
#4: Painting and finishing Line has a maximum capacity limitation.
ct_painting = my_model.add_constraint( 0.15 * CarA + 0.1 * CarB <= 10)
5. Define the objective function
# Define the objective statement.
my_model.maximize(12000 * CarA + 15000 * CarB)
6. Solve statement
# Solve the problem using solve() function
solution = my_model.solve()
7. Getting the solution
# Get the Output
my_model.print_solution()
Save above line of codes in a single file. If not you can download all the script in a single .py file from my GitHub Repository.
Always you can use an Integrated Development Environment(IDE) to run .py file. For this instance, I am going to solve this problem using the command prompt. But If you require an IDE to test the above, I recommend downloading Anaconda that has neat IDE. From the link below you can download Anaconda and launch the Spyder IDE.
Now the Script.py file is in below location.
Open the Command Prompt and run the command below, and now you have the answer.
Thank you for reading, and happy optimizing!
Suresh Abeyweera