If we are trying to solve optimization problems, sometimes we will end up with a mathematical programming which requires modelling the problem and solve it with some solvers. For example, when we are computing VM placement in data centers, we can use this kind of optimization to minimize usage of computer resources and network utilization. IBM ILOG CPLEX Optimizer is a powerful solver that can help us easily solve this kind of problems.
CPLEX includes CPLEX studio, an interactive command line tool and APIs for different languages like C++, Java and Python. In this blog, I’ll go through CPLEX’s example for Python API to solve a linear programming problem, which is a type of mathematical problems.
Problem Statement
The problem we want to solve is as follows.
|
|
Prameters
For the previous examples, some global variables can be written as follows:
|
|
Populate by rows
The most common way to model this problems is to set constraint as rows.
|
|
We first set our target to maximize our objective. Then we add our objectives, upper bound of variables (lower bound set to 0 as default) and name of columns (variables). Then we set the constrainst by rows, first adding indexes or name of variables, and set coefficient secondly. At last we add our constraints into the problem solver.
Populate by columns
In some situations, it may be better to populate by columns.
|
|
It’s like we tranverse the matrix in previous method and we set “c” as basic element in constraints. We also add constraints before adding variables in this method. Whether to populate by rows or columns should be based on number of constraints and number of variables.
Populate by non-zero
At last, there’s a third method to populate the problem with non-zero elements in the matrix.
|
|
This method is good at handle sparse matrix. In this example, the matrix is like this:
|
|
The value in rows and cols composite the location of a value in corresponding order in vals.
Final
There are still some final codes which is easy to understand.
|
|