【数学建模】Python+Gurobi求解非线性规划模型

目录

1 概述

2 算例 

2.1 算例

2.2 参数设置

2.3 Python代码实现

2.4 求解结果


1 概述

如果目标函数或约束条件中包含非线性函数,就称这种规划问题为非线性规划问题。

参考:(非线性规划Python)计及动态约束及节能减排环保要求的经济调度

2 算例 

2.1 算例

\begin{array}{r} \max _{x_{i}} \mathrm{OF}=\quad x_{1} x_{4}\left(x_{1}+x_{2}+x_{3}\right)+x_{2} \\ x_{1} x_{2} x_{3} x_{4} \geq 20 \\ x_{1}^{2}+x_{2}^{2}+x_{3}^{2}+x_{4}^{2}=30 \\ 1 \leq x_{1}, x_{2}, x_{3}, x_{4} \leq 3 \end{array}

2.2 参数设置

求解NLP/非凸问题时,Python+Gurobi 的参数设置

1)需要设置参数'NonConvex=2';
2)Gurobi接受的幂函数y =中,α的最大值为2.c.虽然有解,但不一定是最优值。
3)求解时间看模型规模。
观察模型可知,要想求此模型,需要在编程中把多个变量乘积变成两两相乘的形式。令y_{14}=x_{1}\cdot x_{4}、y23=x2x3即可实现此功能。建模问题解决了,调用Gurobi求解器即可求解了。可是运行模型的时候,出问题了: GurobiError: Q matrix is notpositive semi-definite (PSD). Set NonConvex parameter to 2 to solvemodel.错误很明显,告诉你需要设定参数NonConvex=2,Python+Gurobi设定参数的方法,即:

M_NLP.Params.NonConvex=2

2.3 Python代码实现


from gurobipy import *

# 创建模型
NLP=Model("NLP")

# 变量声明
x1  =NLP.addVar(lb=1,ub=3, name="x1")
x2  =NLP.addVar(lb=1,ub=3, name="x2")
x3  =NLP.addVar(lb=1,ub=3, name="x3")
x4  =NLP.addVar(lb=1,ub=3, name="x4")

y14 =NLP.addVar(lb=0,ub=300, name="y14")
y23 =NLP.addVar(lb=0,ub=300, name="y23")

# 设置目标函数
NLP.setObjective(y14*(x1+x2+x3)+x2,GRB.MAXIMIZE)

# 添加约束
NLP.addConstr(y14*y23>=20,"Con1")
NLP.addConstr(x1*x1+x2*x2+x3*x3+x4*x4==30,"Con2")

# 表示乘积项
NLP.addConstr(y14==x1*x4,"Con_y14")
NLP.addConstr(y23==x2*x3,"Con_y23")

NLP.Params.NonConvex=2

# Optimize model
NLP.optimize()

NLP.write("NLP.lp")

print('**************')
print(' The optimal solution ')
print('**************')
print('Obj is :',NLP.ObjVal) # 输出目标值
print('x1 is :',x1.x) # 输出 x1 的值
print('x2 is :',x2.x) # 输出 x2 的值
print('x3 is :',x3.x) # 输出 x3 的值
print('x4 is :',x4.x) # 输出 x4 的值
from gurobipy import *

# 创建模型
NLP=Model("NLP")

# 变量声明
x1  =NLP.addVar(lb=1,ub=3, name="x1")
x2  =NLP.addVar(lb=1,ub=3, name="x2")
x3  =NLP.addVar(lb=1,ub=3, name="x3")
x4  =NLP.addVar(lb=1,ub=3, name="x4")

y14 =NLP.addVar(lb=0,ub=300, name="y14")
y23 =NLP.addVar(lb=0,ub=300, name="y23")

# 设置目标函数
NLP.setObjective(y14*(x1+x2+x3)+x2,GRB.MAXIMIZE)

# 添加约束
NLP.addConstr(y14*y23>=20,"Con1")
NLP.addConstr(x1*x1+x2*x2+x3*x3+x4*x4==30,"Con2")

# 表示乘积项
NLP.addConstr(y14==x1*x4,"Con_y14")
NLP.addConstr(y23==x2*x3,"Con_y23")

NLP.Params.NonConvex=2

# Optimize model
NLP.optimize()

NLP.write("NLP.lp")

print('**************')
print(' The optimal solution ')
print('**************')
print('Obj is :',NLP.ObjVal) # 输出目标值
print('x1 is :',x1.x) # 输出 x1 的值
print('x2 is :',x2.x) # 输出 x2 的值
print('x3 is :',x3.x) # 输出 x3 的值
print('x4 is :',x4.x) # 输出 x4 的值

2.4 求解结果

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 0 rows, 6 columns and 0 nonzeros
Model fingerprint: 0x11cd71a3
Model has 3 quadratic objective terms
Model has 4 quadratic constraints
Coefficient statistics:
  Matrix range     [0e+00, 0e+00]
  QMatrix range    [1e+00, 1e+00]
  QLMatrix range   [1e+00, 1e+00]
  Objective range  [1e+00, 1e+00]
  QObjective range [2e+00, 2e+00]
  Bounds range     [1e+00, 3e+02]
  RHS range        [0e+00, 0e+00]
  QRHS range       [2e+01, 3e+01]

Continuous model is non-convex -- solving as a MIP

Presolve time: 0.00s
Presolved: 28 rows, 14 columns, 74 nonzeros
Presolved model has 1 quadratic constraint(s)
Presolved model has 9 bilinear constraint(s)
Variable types: 14 continuous, 0 integer (0 binary)

Root relaxation: objective 7.463397e+01, 16 iterations, 0.00 seconds (0.00 work units)

    Nodes    |    Current Node    |     Objective Bounds      |     Work
 Expl Unexpl |  Obj  Depth IntInf | Incumbent    BestBd   Gap | It/Node Time

     0     0   74.63397    0    3          -   74.63397      -     -    0s
H    0     0                      73.6047208   74.63397  1.40%     -    0s
     0     0   73.62217    0    3   73.60472   73.62217  0.02%     -    0s
     0     0   73.61845    0    3   73.60472   73.61845  0.02%     -    0s
     0     0   73.61845    0    3   73.60472   73.61845  0.02%     -    0s
     0     2   73.61845    0    3   73.60472   73.61845  0.02%     -    0s

Cutting planes:
  PSD: 1

Explored 5 nodes (37 simplex iterations) in 0.02 seconds (0.00 work units)
Thread count was 16 (of 16 available processors)

Solution count 1: 73.6047 

Optimal solution found (tolerance 1.00e-04)
Best objective 7.360472078597e+01, best bound 7.361117907001e+01, gap 0.0088%
**************
 The optimal solution 
**************
Obj is : 73.60472078597236
x1 is : 2.9999999998974762
x2 is : 2.5748464524640973
x3 is : 2.317361807798582
x4 is : 2.9999999997674647

Process finished with exit code 0

你可能感兴趣的:(数学建模,python,人工智能,机器学习)