5.8.2. Modeling and Optimization Based on MAPL¶
In this section, we will use MindOpt APL to model and call MindOpt to solve the problem in Example of Non-linear Programming.
You can refer to the section Modeling and Optimization with MindOpt APL for more details. For detailed syntax on using MindOpt APL, please refer to User Manual.
First, create a .mapl file and declare the variables to be optimized.
var x;
var y;
var z;
Declare the objective function
minimize ln(1+exp(x+2*y+z)) + ln(1+exp(x-y-z)) + ln(1+exp(-3*x+y-z)) + ln(1+exp(3*x+z));
Declare the constraints
subto x^2 + y^2 <= 1;
Then, run the solving command, which will automatically build the optimization model and call MindOpt for solving.
solve;
The log of the MindOpt solving process is as follows.
MindOpt Version 2.1.0 (Build date: xxxxxxxx)
Start license validation (current time : xxxxxxxx xx:xx:xx).
License validation terminated. Time : 0.002s
wantsol=1
Model summary.
- Num. variables : 3
- Num. constraints : 1
- Num. nonzeros : 8
- Bound range : [1.00e+00,1.00e+00]
Interior point method started.
Iter PrimObj DualObj PrimFea DualFea GapFea Mu Time
0 +2.77258874e+00 +3.77258875e+00 0.0e+00 1.0e+00 1.0e+00 1.0e+00 0.00s
1 +2.10107174e+00 +2.23028984e+00 0.0e+00 7.8e-01 1.0e-01 1.0e-01 0.00s
2 +1.42075610e+00 +1.52543264e+00 6.7e-01 2.7e-01 1.8e-03 1.8e-03 0.00s
3 +1.65885758e+00 +1.72802441e+00 0.0e+00 1.2e-01 3.6e-02 3.6e-02 0.00s
4 +1.65511048e+00 +1.69576276e+00 0.0e+00 6.6e-02 2.1e-02 2.1e-02 0.00s
5 +1.65736811e+00 +1.66000850e+00 0.0e+00 2.6e-04 2.6e-03 2.6e-03 0.00s
6 +1.65490062e+00 +1.65506485e+00 0.0e+00 9.0e-06 1.6e-04 1.6e-04 0.00s
7 +1.65474536e+00 +1.65474726e+00 0.0e+00 3.6e-08 1.9e-06 1.9e-06 0.00s
8 +1.65474358e+00 +1.65474367e+00 0.0e+00 4.9e-12 9.1e-08 9.1e-08 0.00s
Terminated.
- Method : Interior point method.
- Primal objective : 1.6547435766246e+00
- Dual objective : 1.6547436675411e+00
- Num. threads : 1
- Num. iterations : 8
- Solver details : Solver terminated with a primal/dual optimal status.
Interior point method terminated. Time : 0.003883s
Optimizer summary.
- Optimizer used : Interior point method
- Optimizer status : OPTIMAL
Solution summary. Primal solution
- Objective : +1.6547435766e+00
OPTIMAL; objective 1.654744
Completed.
Upon successful solving, MindOpt APL will obtain the optimal solution to the problem. We can print it out to view:
print "x={}" % x;
print "y={}" % y;
print "z={}" % z;
The output of the above command is
x = -0.5728551903261085
y = -0.8196564705817261
z = 1.2728750741453017
We can also calculate the corresponding optimal objective function value based on the obtained optimal solution (keeping three decimal places here).
param obj = ln(1+exp(x+2*y+z)) + ln(1+exp(x-y-z)) + ln(1+exp(-3*x+y-z)) + ln(1+exp(3*x+z));
print "obj={:.3f}" % obj;
The output of the above code is as follows:
obj=1.655
The example MAPLLR.mapl provides the complete MindOpt APL source code.
# Example: 2D Regularized Logistic Regression Classifcation
#
# data points:
# - label=1: {(1,2), (3,0)}
# - label=-1: {(-1,1),(3,-1)}
var x;
var y;
var z;
minimize ln(1+exp(x+2*y+z)) + ln(1+exp(x-y-z)) + ln(1+exp(-3*x+y-z)) + ln(1+exp(3*x+z));
subto x^2 + y^2 <= 1;
solve;
print "x={}" % x;
print "y={}" % y;
print "z={}" % z;
param obj = ln(1+exp(x+2*y+z)) + ln(1+exp(x-y-z)) + ln(1+exp(-3*x+y-z)) + ln(1+exp(3*x+z));
print "obj={:.3f}" % obj;