【数学建模】二次规划求解约束极值问题(Python+Gurobi实现)

目录

1 概述

2 算例及Python代码实现

2.1 算例

2.2 方法1

2.3 方法1求解结果

2.4 方法2


1 概述

        根据约束条件的不同,二次规划可分为等式约束二次规划问题和不等式约束二次规划问题。等式约束二次规划问题即只含有等式约束,常见的解法有直接消去法、广义消去法、拉格朗日(Lagrange)法;对于不等式约束二次规划问题,其基本思想是把不等式约束转化为等式约束再求解,常见解法有有效集(active set)方法,有效集方法在每步迭代中把有效约束作为等式约束,然后可以用拉格朗日法求解,重复直到求得最优解。

        很多学者专门研究各类二次规划的求解方法,如文献[4][5],对于非数学专业的的人来讲更重要的是怎么把二次规划当作一个工具去使用它。

带有约束条件的极值问题称为约束极值问题,也叫规划问题。
求解约束极值问题要比求解无约束极值问题困难得多。为了简化其优化工作,可采用下面两种方法:
1)将约束问题转化为无约束问题;
2)将非线性规划问题转化为线性规划问题。

2 算例及Python代码实现

2.1 算例

\begin{array}{c} \min f(x)=2 x_{1}^{2}-4 x_{1} x_{2}+4 x_{2}^{2}-6 x_{1}-3 x_{2} \\ \text { s.t. }\left\{\begin{array}{c} x_{1}+x_{2} \leq 3 \\ 4 x_{1}+x_{2} \leq 9 \\ x_{1}, x_{2} \geq 0 \end{array}\right. \end{array}

2.2 方法1



from gurobipy import *

# 创建模型
M_QCP=Model("QCP")

# 变量声明
'''
上下限如果定为无穷,容易出错,所以一般设为1e4
x1  =M_QCP.addVar(lb=0,ub=GRB.INFINITY, name="x1")
x2  =M_QCP.addVar(lb=0,ub=GRB.INFINITY, name="x2")
'''
x1  =M_QCP.addVar(lb=0,ub=1e4, name="x1")
x2  =M_QCP.addVar(lb=0,ub=1e4, name="x2")

# 设置目标函数
M_QCP.setObjective(2*x1**2-4*x1*x2+4*x2**2-6*x1-3*x2,GRB.MINIMIZE)

# 添加约束
M_QCP.addConstr(x1+x2<=3,"Con1")
M_QCP.addConstr(4*x1+x2<=9,"Con2")


M_QCP.Params.NonConvex=2

# Optimize model
M_QCP.optimize()

M_QCP.write("QCP.lp")


print('=======================')
print(' =========最优解======== ')
print('===================')
print('Obj is :',M_QCP.ObjVal) # 输出目标值
print('x1 is :',x1.x) # 输出 x1 的值
print('x2 is :',x2.x) # 输出 x2 的值

2.3 方法1求解结果

Set parameter NonConvex to value 2
Gurobi Optimizer version 9.5.2 build v9.5.2rc0 (win64)
Thread count: 8 physical cores, 16 logical processors, using up to 16 threads
Optimize a model with 2 rows, 2 columns and 4 nonzeros
Model fingerprint: 0xfaada561
Model has 3 quadratic objective terms
Coefficient statistics:
  Matrix range     [1e+00, 4e+00]
  Objective range  [3e+00, 6e+00]
  QObjective range [4e+00, 8e+00]
  Bounds range     [1e+04, 1e+04]
  RHS range        [3e+00, 9e+00]
Presolve time: 0.00s
Presolved: 2 rows, 2 columns, 4 nonzeros
Presolved model has 3 quadratic objective terms
Ordering time: 0.00s

Barrier statistics:
 Free vars  : 1
 AA' NZ     : 3.000e+00
 Factor NZ  : 6.000e+00
 Factor Ops : 1.400e+01 (less than 1 second per iteration)
 Threads    : 1

                  Objective                Residual
Iter       Primal          Dual         Primal    Dual     Compl     Time
   0   4.74748686e+05 -5.00249810e+05  1.12e+03 2.40e+01  4.98e+05     0s
   1   5.02754710e+04 -5.33880584e+04  1.10e+02 7.69e-09  3.72e+04     0s
   2   1.19043097e+04 -1.33290955e+04  1.05e+01 8.08e-10  6.95e+03     0s
   3  -1.68878089e-01 -3.18400831e+03  1.27e+00 9.39e-11  1.52e+03     0s
   4  -6.85245486e+00 -1.81873310e+03  5.57e-07 0.00e+00  3.02e+02     0s
   5  -7.05868976e+00 -5.47071675e+01  1.39e-08 0.00e+00  7.94e+00     0s
   6  -1.00688999e+01 -2.63884980e+01  1.37e-14 0.00e+00  2.72e+00     0s
   7  -1.09095108e+01 -1.14480512e+01  1.11e-16 0.00e+00  8.98e-02     0s
   8  -1.10239216e+01 -1.10381544e+01  2.44e-15 2.61e-15  2.37e-03     0s
   9  -1.10249905e+01 -1.10253761e+01  8.87e-14 4.44e-16  6.43e-05     0s
  10  -1.10250000e+01 -1.10250004e+01  9.22e-14 1.06e-15  7.01e-08     0s
  11  -1.10250000e+01 -1.10250000e+01  5.28e-14 0.00e+00  7.02e-11     0s

Barrier solved model in 11 iterations and 0.00 seconds (0.00 work units)
Optimal objective -1.10250000e+01

=======================
 =========最优解======== 
===================
Obj is : -11.024999999988966
x1 is : 1.9499999996029882
x2 is : 1.0500000003927306

Process finished with exit code 0

Set parameter NonConvex to value 2
Gurobi Optimizer version 9.5.2 build v9.5.2rc0 (win64)
Thread count: 8 physical cores, 16 logical processors, using up to 16 threads
Optimize a model with 2 rows, 2 columns and 4 nonzeros
Model fingerprint: 0xfaada561
Model has 3 quadratic objective terms
Coefficient statistics:
  Matrix range     [1e+00, 4e+00]
  Objective range  [3e+00, 6e+00]
  QObjective range [4e+00, 8e+00]
  Bounds range     [1e+04, 1e+04]
  RHS range        [3e+00, 9e+00]
Presolve time: 0.00s
Presolved: 2 rows, 2 columns, 4 nonzeros
Presolved model has 3 quadratic objective terms
Ordering time: 0.00s

Barrier statistics:
 Free vars  : 1
 AA' NZ     : 3.000e+00
 Factor NZ  : 6.000e+00
 Factor Ops : 1.400e+01 (less than 1 second per iteration)
 Threads    : 1

                  Objective                Residual
Iter       Primal          Dual         Primal    Dual     Compl     Time
   0   4.74748686e+05 -5.00249810e+05  1.12e+03 2.40e+01  4.98e+05     0s
   1   5.02754710e+04 -5.33880584e+04  1.10e+02 7.69e-09  3.72e+04     0s
   2   1.19043097e+04 -1.33290955e+04  1.05e+01 8.08e-10  6.95e+03     0s
   3  -1.68878089e-01 -3.18400831e+03  1.27e+00 9.39e-11  1.52e+03     0s
   4  -6.85245486e+00 -1.81873310e+03  5.57e-07 0.00e+00  3.02e+02     0s
   5  -7.05868976e+00 -5.47071675e+01  1.39e-08 0.00e+00  7.94e+00     0s
   6  -1.00688999e+01 -2.63884980e+01  1.37e-14 0.00e+00  2.72e+00     0s
   7  -1.09095108e+01 -1.14480512e+01  1.11e-16 0.00e+00  8.98e-02     0s
   8  -1.10239216e+01 -1.10381544e+01  2.44e-15 2.61e-15  2.37e-03     0s
   9  -1.10249905e+01 -1.10253761e+01  8.87e-14 4.44e-16  6.43e-05     0s
  10  -1.10250000e+01 -1.10250004e+01  9.22e-14 1.06e-15  7.01e-08     0s
  11  -1.10250000e+01 -1.10250000e+01  5.28e-14 0.00e+00  7.02e-11     0s

Barrier solved model in 11 iterations and 0.00 seconds (0.00 work units)
Optimal objective -1.10250000e+01

=======================
 =========最优解======== 
===================
Obj is : -11.024999999988966
x1 is : 1.9499999996029882
x2 is : 1.0500000003927306

Process finished with exit code 0

2.4 方法2

MPC的终结——二次规划求解约束极值问题

你可能感兴趣的:(数学建模,python,开发语言,matlab,回归,机器学习)