CPLEX is a convex optimization toolkit for solving a wide variety of optimization problems.
Convex optimization is a more general approach to solving mathematical optimization problems that encompasses classic linear programming, systems of equations, and least-squares techniques, along with quadratic linear/non-linear techniques. A very good reference on the subject can be found in Convex Optimization – Boyd and Vandenberghe. For those who are interested in the subject, it contains a good balance of theory and practical applications along with useful algorithmic descriptions.
The goal of the example program (links below) was to demonstrate how the API for CPLEX is used to setup an optimization problem in C# and solve it. CPLEX refers to this API as “Concert.”
While CPLEX has its own language (OPL) for expressing problems and allowing them be run in the CPLEX environment, along with a Javascript-like procedural language (ILOG) to manage pre/post data processing and control flow. For cases where a model must be integrated into other applications, the CPLEX API is available for C/C++, Python, and .Net shops to take advantage of.
The CPLEX package comes with a lot of example code for OPL and extensive application examples, 80% of which are written in native OPL/ILOG. The one financial example provided for portfolio optimization is expressed in OPL.
The particular problem I was interested in was a fairly straightforward portfolio optimization problem where the objective function has quadratic terms. The problem is easily expressed in OPL:
dexpr float Objective =
(sum(i in Investments) Return[i]*Allocation[i])
- (Rho/2)*(sum(i,j in Investments) Covariance[i][j]*Allocation[i]*Allocation[j]);
maximize Objective;
subject to {
// sum of allocations equals amount to be invested
allocate: (sum (i in Investments) (Allocation[i])) == Wealth;
}
However, when using the Concert API, the developer needs to write additional code to present data in the correct form to the model, as well as determine how to build up the objective function and constraint matrices. It’s helpful to be able to see the model expressed as you develop the code.
One technique that’s helpful is the ability to export the model as an LP file – a somewhat standardized way to express linear programming problems. Then you can review how your objective function and constraints look. The example program shows how to do that. Here’s an example of how the output looks after a run of the C# program.
Maximize
obj: 0.555555555555555 x1 + 1.06666666666667 x2 - 0.4 x3 + [ - 0.00001 x1 ^2
+ 0.0000133333333333333 x1 * x2 + 0.0000333333333333333 x1 * x3
- 0.00002 x2 ^2 + 0.0000166666666666667 x2 * x3
- 0.0000579166666666667 x3 ^2 ] / 2
Subject To
c1: x1 + x2 + x3 = 1
Bounds
0 <= x1 <= 1
0 <= x2 <= 1
0 <= x3 <= 1
End
The resulting C# program brings everything together, including how to link the appropriate libraries for the API. Hopefully this example will save some time for interested readers.
The Github Repository for the C# Example is here:
https://github.com/oshea00/CPLEX
You can download the free edition of Optimization Studio here:
https://www.ibm.com/products/ilog-cplex-optimization-studio