5.7.5. SDP Modeling and Optimization in Python¶
In this chapter, we will use MindOpt Python API to model and solve the problem in Examples of semidefinite programming.
5.7.5.1. SDP Example I¶
Include Python package:
26from mindoptpy import *
Step I: Create an optimization model
Create an empty MindOpt model:
31 # Step 1. Create a model.
32 model = Model()
Step II: SDP model input
By using Model.addPsdVar()
, we add a semidefinite matrix variable \(\mathbf{X}\) with dimensions of \(3\times3\).
36 # Add a PSD matrix variable.
37 X = model.addPsdVar(dim=3, name="X")
We input the coefficient matrix \(\mathbf{C}\) of the objective function. And we set the objective function of the model with the first argument of Model.setObjective()
and set the optimization direction to maximize with the second argument.
40 C = np.array([[-3, 0, 1], [0, -2, 0], [1, 0, -3]])
41 objective = C * X
42 model.setObjective(objective, MDO.MAXIMIZE)
We input the constraint coefficient matrix \(\mathbf{A}\) and add constraints to the model by
using Model.addConstr()
.
44 # Input the constraint.
45 A = np.array([[3, 0, 1], [0, 4, 0], [1, 0, 5]])
46 model.addConstr(A * X == 1, "c0")
Step III: Solve SDP model
Solve the optimization problem via Model.optimize()
.
49 model.optimize()
Retrieve the optimal objective function value,
52 # Display objective.
53 print("Objective: " + str(objective.getValue()))
Obtain the value of positive semi-definite matrix \(\mathbf{X}\).
55 # Display the solution.
56 print("X = ")
57 print(X.PsdX)
Step IV: Release model
68 # Step 4. Free the model.
69 model.dispose()
The complete example code is provided in mdo_sdo_ex1.py:
1"""
2/**
3 * Description
4 * -----------
5 *
6 * Semidefinite optimization (row-wise input).
7 *
8 * Formulation
9 * -----------
10 *
11 * Maximize
12 * obj:
13 * tr(C X)
14 * Subject To
15 * c0 : tr(A X) = 1
16 * Bounds
17 * X is p.s.d.
18 *
19 * Matrix
20 * C = [ -3 0 1 ] A = [ 3 0 1 ]
21 * [ 0 -2 0 ] [ 0 4 0 ]
22 * [ 1 0 -3 ] [ 1 0 5 ]
23 * End
24 */
25 """
26from mindoptpy import *
27import numpy as np
28
29if __name__ == "__main__":
30
31 # Step 1. Create a model.
32 model = Model()
33
34 try:
35 # Step 2. Input model.
36 # Add a PSD matrix variable.
37 X = model.addPsdVar(dim=3, name="X")
38
39 # Set objective.
40 C = np.array([[-3, 0, 1], [0, -2, 0], [1, 0, -3]])
41 objective = C * X
42 model.setObjective(objective, MDO.MAXIMIZE)
43
44 # Input the constraint.
45 A = np.array([[3, 0, 1], [0, 4, 0], [1, 0, 5]])
46 model.addConstr(A * X == 1, "c0")
47
48 # Step 3. Solve the problem and display the result.
49 model.optimize()
50
51 if model.status == MDO.OPTIMAL:
52 # Display objective.
53 print("Objective: " + str(objective.getValue()))
54
55 # Display the solution.
56 print("X = ")
57 print(X.PsdX)
58 else:
59 print("No feasible solution.")
60 except Exception as e:
61 print("Received Mindopt exception.")
62 print(" - Code : {}".format(e.errno))
63 print(" - Reason : {}".format(e.message))
64 except Exception as e:
65 print("Received exception.")
66 print(" - Reason : {}".format(e))
67 finally:
68 # Step 4. Free the model.
69 model.dispose()
5.7.5.2. SDP Example II¶
Include Python package:
32from mindoptpy import *
Step I: Create an optimization model
Create an empty MindOpt model:
38 # Step 1. Create a model.
39 model = Model()
Step II: SDP model input
Add two non-negative variables \(x_0\) and \(x_1\) via Model.addVar()
.
43 # Add nonnegative scalar variables.
44 x0 = model.addVar(lb=0.0, name="x0")
45 x1 = model.addVar(lb=0.0, name="x1")
By using Model.addPsdVar()
, we add two semidefinite matrix variables \(\mathbf{X}_0\) and \(\mathbf{X}_1\) with their dimensions of \(2\times 2\) and \(3\times 3\).
47 # Add PSD matrix variables.
48 X0 = model.addPsdVar(dim = 2, name = "X0")
49 X1 = model.addPsdVar(dim = 3, name = "X1")
We input the coefficient matrices \(\mathbf{C}_0\) and \(\mathbf{C}_1\) of the objective function.
Set the objective function of the model with the first argument of Model.setObjective()
and set the optimization direction to maximize with the second argument.
51 # Set objective
52 C0 = np.array([[2, 1], [1, 2]])
53 C1 = np.array([[3, 0, 1], [0, 2, 0], [1, 0, 3]])
54 objective = C0 * X0 + C1 * X1
55 model.setObjective(objective, MDO.MAXIMIZE)
We input the constraint coefficient matrix \(\mathbf{A}_{00}\) and add the first constraint to the model by using Model.addConstr()
.
57 # Input the first constraint.
58 A00 = np.array([[3, 1], [1, 3]])
59 model.addConstr(A00 * X0 + x0 == 1, "c0")
Input the second constraint coefficient matrix \(\mathbf{A}_{11}\), and add the second constraint via Model.addConstr()
.
61 # Input the second constraint.
62 A11 = np.array([[3, 0, 1], [0, 4, 0], [1, 0, 5]])
63 model.addConstr(A11 * X1 + x1 == 2, "c1")
Step III: Solve SDP model
Solve the optimization problem via Model.optimize()
.
66 model.optimize()
Retrieve the optimal objective function value.
69 # Display objective.
70 print("Objective: " + str(objective.getValue()))
Obtain the value of positive semi-definite matrices.
72 # Display the solution.
73 print("x0 = " + " {0:7.6f}".format(x0.X))
74 print("x1 = " + " {0:7.6f}".format(x1.X))
75 print("X0 = ")
76 print(X0.PsdX)
77 print("X1 = ")
78 print(X1.PsdX)
Step IV: Release model
89 # Step 4. Free the model.
90 model.dispose()
The complete example code is provided in mdo_sdo_ex2.py :
1"""
2/**
3 * Description
4 * -----------
5 *
6 * Semidefinite optimization (row-wise input).
7 *
8 * Formulation
9 * -----------
10 *
11 * Maximize
12 * obj:
13 * tr(C0 X0) + tr(C1 X1) + 0 x0 + 0 x1
14 * Subject To
15 * c0 : tr(A00 X0) + 1 x0 = 1
16 * c1 : tr(A11 X1) + 1 x1 = 2
17 * Bounds
18 * 0 <= x0
19 * 0 <= x1
20 * X0,X1 are p.s.d.
21 *
22 * Matrix
23 * C0 = [ 2 1 ] A00 = [ 3 1 ]
24 * [ 1 2 ] [ 1 3 ]
25 *
26 * C1 = [ 3 0 1 ] A11 = [ 3 0 1 ]
27 * [ 0 2 0 ] [ 0 4 0 ]
28 * [ 1 0 3 ] [ 1 0 5 ]
29 * End
30 */
31 """
32from mindoptpy import *
33import numpy as np
34
35
36if __name__ == "__main__":
37
38 # Step 1. Create a model.
39 model = Model()
40
41 try:
42 # Step 2. Input model.
43 # Add nonnegative scalar variables.
44 x0 = model.addVar(lb=0.0, name="x0")
45 x1 = model.addVar(lb=0.0, name="x1")
46
47 # Add PSD matrix variables.
48 X0 = model.addPsdVar(dim = 2, name = "X0")
49 X1 = model.addPsdVar(dim = 3, name = "X1")
50
51 # Set objective
52 C0 = np.array([[2, 1], [1, 2]])
53 C1 = np.array([[3, 0, 1], [0, 2, 0], [1, 0, 3]])
54 objective = C0 * X0 + C1 * X1
55 model.setObjective(objective, MDO.MAXIMIZE)
56
57 # Input the first constraint.
58 A00 = np.array([[3, 1], [1, 3]])
59 model.addConstr(A00 * X0 + x0 == 1, "c0")
60
61 # Input the second constraint.
62 A11 = np.array([[3, 0, 1], [0, 4, 0], [1, 0, 5]])
63 model.addConstr(A11 * X1 + x1 == 2, "c1")
64
65 # Step 3. Solve the problem and display the result.
66 model.optimize()
67
68 if model.status == MDO.OPTIMAL:
69 # Display objective.
70 print("Objective: " + str(objective.getValue()))
71
72 # Display the solution.
73 print("x0 = " + " {0:7.6f}".format(x0.X))
74 print("x1 = " + " {0:7.6f}".format(x1.X))
75 print("X0 = ")
76 print(X0.PsdX)
77 print("X1 = ")
78 print(X1.PsdX)
79 else:
80 print("No feasible solution.")
81 except Exception as e:
82 print("Received Mindopt exception.")
83 print(" - Code : {}".format(e.errno))
84 print(" - Reason : {}".format(e.message))
85 except Exception as e:
86 print("Received exception.")
87 print(" - Reason : {}".format(e))
88 finally:
89 # Step 4. Free the model.
90 model.dispose()