IBM is known for its innovations regarding problem-solving technologies. From AI-powered robots to quantum computing, IBM has laid hands on every technological advance (even blockchain!). With increasing technological needs, these innovations really help the individual programmers as well as businesses to ease out their tasks. CPLEX is one of their innovations that handles high-level mathematical optimizations and calculates the best optimal solution as per need. IBM has also made Cplex available on Python in the form of a library.
CPLEX in Python is a library providing an API wrapper over IBM CPLEX Optimizer. Over the API, you can create mathematical optimizations for your business decisions and development. Mostly, used in production-based businesses where the optimal decision needs to be taken regarding production priorities.
What is CPLEX exactly?
If you’re familiar with C programming, you might know the Simplex() algorithm which is used for linear programming. As a result, the newborn baby of C and Simplex, CPLEX was created. Although it originated from C programming, CPLEX has many different interfaces than C. One of the popular interfaces is in Python.
CPLEX is capable of solving extremely large linear problems with hundreds of constraints with no issues. Not only linear programming, but it also has support for complex level optimizations for quadratic, interior points, and continuous variable problems.
In simpler words, I might say, CPLEX is software that tells us the most optimal solutions to any problems with provided constraints.
Why do we need CPLEX in Python?
Python is one of the top growing programming languages in recent times. Its simplicity to code and open-source availability makes it even better. Apart from it, the constant updates and availability of thousands of modules grant us an easy way to actually use the language easily.
Moreover, as Python is prominent in the data science field, it’s very optimal for developers to use CPLEX on it. Knowing that demand, IBM released their API wrapper for CPLEX which is easy to install as well as use.
How to Install CPLEX in Python?
There are two ways to install and use CPLEX in Python. We’ll go through both methods to do it.
1. Using pip install
Use the python package manager to install the CPLEX library on Python as follows –
pip install cplex docplex
This method will add CPLEX and Docplex to your python libraries. Then you can start using it right away by importing it. But this way has a drawback, you can easily reach limits exceeded error while using complicated calculations.
As a result, you need to install the proper CPLEX runtime and library as mentioned below if you’re going for harder problems.
2. Using IBM Installer
Head over to the IBM website and log in to your account. To download CPLEX, you need to first add it to your products. You can either get a free trial or get it for free if you are a student (using the institute’s email address). Then move to the downloads section and download the CPLEX installer.
After downloading the installer, install it and locate the installation folder on your computer. By default, the python package will be in yourCPLEXhome\python\VERSION\PLATFORM folder. After going to this directory, open the terminal in this directory and use the following command –
python setup.py install
This will run the setup and install CPLEX in your python environment.
Please make sure you execute this command with your working python environment.
Note: CPLEX currently only supports the API integration in Python 3.7 and 3.8 versions.
In Anaconda –
To install the CPLEX module in the Anaconda environment, run the setup.py location in the CPLEX python directory using python in the anaconda environment. The default directory is yourCPLEXhome\python\VERSION\PLATFORM)
Examples of CPLEX Optimizations in Python
There are several optimization problems on the internet. We’ve made sure to include all of the basic as well as advanced level problems –
1. Basic One Variable Optimization
This is a very simple one variable optimization depending on constraints. The problem stands as follows –
Minimize variable 3*x, where the condition is x should be greater than 3.
from docplex.mp.model import Model m = Model(name='single variable') x = m.continuous_var(name="x", lb=0) c1 = m.add_constraint(x >= 2, ctname="const1") m.set_objective("min", 3*x) m.print_information() m.solve() m.print_solution()
Model: single variable - number of variables: 1 - binary=0, integer=0, continuous=1 - number of constraints: 1 - linear=1 - parameters: defaults - objective: minimize - problem type is: LP Solution --> objective: 6.000 x=2.000
We first start by importing the Docplex module. Then we create a new Model with the name as we require. Then, we initialize a continuous variable x which has a lower bound set to 0. You can change this bound by using the lb parameter.
Also, note that there are three types of variables that you can use, Binary (takes value 0 and 1), Continous (All values from lower bound to infinity), and Integer (All integers from lower bound to infinity). In this case, we’ve used continuous variables.
Then, we add a new constraint x>=2 by using the add_constraint method. Also, make sure that you use >= instead of >.
Then, set an objective function along with its objective ie. minimum or maximum. Also, make sure that your objective function is convex. Otherwise, it will not be solved.
2. Basic Linear Optimizations in Two Variables
This problem will feature two variables with multiple constraints. You can use this model for basic linear optimizations.
Minimize the function 5x + 4y, where x>0 and y>0.
Constraints: 2*x + y >= 10
x + y >= 8
x + 4*y >= 11
from docplex.mp.model import Model m = Model(name='two variable') x = m.continuous_var(name="x", lb=0) y = m.continuous_var(name="y", lb=0) c1 = m.add_constraint(2*x + y >= 10, ctname="const1") c2 = m.add_constraint(x + y >= 8, ctname="const2") c3 = m.add_constraint(x + 4*y >= 11, ctname="const3") m.set_objective("min", 5*x + 4*y) m.print_information() m.solve() m.print_solution()
Model: two variable - number of variables: 2 - binary=0, integer=0, continuous=2 - number of constraints: 3 - linear=3 - parameters: defaults - objective: minimize - problem type is: LP objective: 34.000 x=2.000 y=6.000
After importing the module, we created two continuous variables from the model. These variables can now be used to set up constraints and objective functions. In this case, we have 3 different constraints as mentioned in the problem statement.
Then, by using set_objective, we set up the objective function and items objective to minimize.
print_information() is a great way of knowing your model. In this case, we have 2 variables, 3 constraints, minimize objective and Linear Programming problem types.
Lastly, we call the solve() function to use the CPLEX Python runtime to solve the model.
3. Quadratic Optimizations Using CPLEX Python
Quadratic functions are polynomials with degree 2. Unfortunately, CPLEX doesn’t calculate the optimizations over degree 2. In this example, we’ll have a look at the convex quadratic problems and how to solve them.
Objective: Minimize 5x2 + 4*y
Constraints: x + 2*y >= 11
x + y >= 4
from docplex.mp.model import Model m = Model(name='two variable') x = m.continuous_var(name="x", lb=0) y = m.continuous_var(name="y", lb=0) c1 = m.add_constraint(x + 2*y >= 11, ctname="const1") c2 = m.add_constraint(x + y >= 4, ctname="const2") m.set_objective("min", 5*x*x + 4*y) m.print_information() m.solve() m.print_solution()
Model: two variable - number of variables: 2 - binary=0, integer=0, continuous=2 - number of constraints: 2 - linear=2 - parameters: defaults - objective: minimize quadratic - problem type is: QP objective: 21.800 x=0.200 y=5.400
The above example is used when our variables are dependent on other variables for their task.
We first start by creating a Model and then creating two variables x and y. Then we add the constraints by using the add_constraints method. Then, we create a quadratic objective function with the objective of finding a minimum value.
Also, while printing the Model information, you can check that the objective is labeled as quadratic rather than LP.
4. Knapsack Problem
Knapsack problems are typical optimal filling problems where you have a container that needs to be filled with items according to their weights and values. This way you can carry maximum items that contribute large values.
We’re going to go through a similar example where you need to fill a bag that can handle a limited amount of weight. Your task is to maximize the number of items you carry.
Capacity = 15
Values = [4, 2, 5, 4, 5, 1, 3, 5]
Weights = [10, 5, 18, 12, 15, 1, 2, 8]
from docplex.mp.model import Model w = [4, 2, 5, 4, 5, 1, 3, 5] v = [10, 5, 18, 12, 15, 1, 2, 8] C = 15 N = len(w) knapsack_model = Model('knapsack') x = knapsack_model.binary_var_list(N, name="x") knapsack_model.add_constraint(sum(w[i]*x[i] for i in range(N)) <= C) obj_fn = sum(v[i]*x[i] for i in range(N)) knapsack_model.set_objective("max", obj_fn) knapsack_model.print_information() sol = knapsack_model.solve() knapsack_model.print_solution() if sol is None: print("Infeasible")
Model: knapsack - number of variables: 8 - binary=8, integer=0, continuous=0 - number of constraints: 1 - linear=1 - parameters: defaults - objective: maximize - problem type is: MILP objective: 46 x_2=1 x_3=1 x_4=1 x_5=1
We first start by creating a CPLEX Python Model for handling knapsacks. Then, we create a list of binary variables with length 8, This list will contact the binary variable whether the item will be included in the container or not.
Then we add a constant that the total weight should be less than the capacity of the container. For this, we use a combination of the sum() function along with comprehension.
When we create an objective function which calculates the total value carrying in the container. Then we set the objective to maximize the objective function.
Then we solve the problem and the solution is printed. In this case, the variables with a binary value of 1 are printed as the solution.
5. Sudoku Problem
Sudoku is a famous puzzle/game where you have to fill all the grids of the squares depending on the conditions. For the human mind, some hard sudoku puzzles are real head-scratchers.
But for software like CPLEX, it’s piece of cake. In this example, we’ll use a hard sudoku problem and check the benchmark of the CPLEX Python.
from docplex.cp.model import * problem = ( (0, 0, 0, 0, 0, 8, 0, 0, 0), (0, 5, 9, 0, 0, 0, 0, 0, 8), (2, 0, 0, 0, 0, 6, 0, 0, 0), (0, 4, 5, 0, 0, 0, 0, 0, 0), (0, 0, 3, 0, 0, 0, 0, 0, 0), (0, 0, 6, 0, 0, 3, 0, 5, 4), (0, 0, 0, 3, 2, 5, 0, 0, 6), (0, 0, 0, 0, 0, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0, 0, 0, 0) ) sodoku_model = CpoModel(name="Sudoku") grid = [[integer_var(min=1, max=9, name="C" + str(l) + str(c)) for l in range(9)] for c in range(9)] for l in range(9): sodoku_model.add(all_diff([grid[l][c] for c in range(9)])) for c in range(9): sodoku_model.add(all_diff([grid[l][c] for l in range(9)])) for sl in range(0, 9, 3): for sc in range(0, 9, 3): sodoku_model.add(all_diff([grid[l][c] for l in range(sl, sl + 3) for c in range(sc, sc + 3)])) for l in range(9): for c in range(9): v = problem[l][c] if v > 0: grid[l][c].set_domain((v, v)) print("\nSolving model....") msol = sodoku_model.solve(TimeLimit=10) if msol: sol = [[msol[grid[l][c]] for c in range(9)] for l in range(9)] print("Solve time: " + str(msol.get_solve_time())) print("Solution", sol) else: print("No solution found")
The above code is inspired by a solution from IBM Decision Optimization Github code.
Solving model.... ! --------------------------------------------------- CP Optimizer 188.8.131.52 -- ! Satisfiability problem - 81 variables, 27 constraints ! TimeLimit = 10 ! Initial process time : 0.01s (0.01s extraction + 0.00s propagation) ! . Log search space : 154.9 (before), 154.9 (after) ! . Memory usage : 335.3 kB (before), 335.3 kB (after) ! Using parallel search with 4 workers. ! ---------------------------------------------------------------------------- ! Branches Non-fixed W Branch decision * 25 0.02s 1 1 = C82 ! ---------------------------------------------------------------------------- ! Search completed, 1 solution found. ! ---------------------------------------------------------------------------- ! Number of branches : 122 ! Number of fails : 11 ! Total memory usage : 3.0 MB (2.9 MB CP Optimizer + 0.0 MB Concert) ! Time spent in solve : 0.02s (0.02s engine + 0.01s extraction) ! Search speed (br. / s) : 12200.0 ! ---------------------------------------------------------------------------- Solve time: 0.022002220153808594 Solution [[4, 3, 1, 9, 5, 8, 7, 6, 2], [6, 5, 9, 1, 7, 2, 4, 3, 8], [2, 7, 8, 4, 3, 6, 5, 9, 1], [7, 4, 5, 2, 6, 1, 3, 8, 9], [9, 2, 3, 5, 8, 4, 6, 1, 7], [8, 1, 6, 7, 9, 3, 2, 5, 4], [1, 9, 7, 3, 2, 5, 8, 4, 6], [5, 8, 2, 6, 4, 9, 1, 7, 3], [3, 6, 4, 8, 1, 7, 9, 2, 5]]
We first create a puzzle grid that is meant to be solved in this problem. Then we start by creating an empty CPLEX model. Then we create a list of integer variables with a minimum value of 1 and a maximum value of 9. This list will serve as the grid values for the solution.
Then we start to add constraints to the model one by one. At first, we create a constraint for the horizontal rows. For this purpose, we use the all_diff method which matches the values to the range.
Then for the second constraint, we do the same for columns.
Thirdly, we add a constraint for the square box. For this, we’ll loop through all the arrays with a jump of 3.
Then, we initialize the known grids in the sodoku model and then solve it.
Lastly, we print all the values and calculate the time required to solve the puzzle.
CPLEX vs Gurobi: Which is better?
Gurobi is another library to solve mathematical optimizations problem. It has the same functionality as CPLEX as far as a beginner is concerned. But as we move towards the higher-dimensional problem, CPLEX is known to outperform Gurobi.
However, this doesn’t imply that CPLEX is better than Gurobi in all sense. There are thousands of different problems on optimization topics where you can find clear differences between major commercial solvers.
The only way to know the best for you is to benchmark both of them against your problem.
FAQs Related to Cplex Python
Here are some of the FAQs on the CPLEX python module –
Yes, CPLEX is free to install the library on Python. However, you need to have CPLEX runtime installed in order to run it. There is the free version of runtime with a limited size of 1000 variables and 1000 constraints. But if you’re looking for models with more size, you might have to purchase CPLEX.
Both the libraries are managed by IBM and wrap around CPLEX C API. Docplex library is a well-managed OOP API that uses the C API to provide a good interface. Whereas, CPLEX library is a simple wrapper for C API. This wrapper doesn’t have an interface (even variables and constraints are stored as indices in the matrix).
Model.add_constraint() is used to add constraints over the variables in CPLEX Models.
CPLEX has helped many organizations to create optimal solutions. The API integration in Python has opened its way to many of the new developers and provided an opportunity to learn it. Many institutes and colleges have their computer science lectures based on objective optimizations using CPLEX.
If you have any doubts regarding CPLEX, let us know in the comments.