目錄
1 概述
2 算例?
2.1 算例
2.2 參數(shù)設(shè)置
2.3 Python代碼實(shí)現(xiàn)
2.4 求解結(jié)果
1 概述
如果目標(biāo)函數(shù)或約束條件中包含非線性函數(shù),就稱這種規(guī)劃問(wèn)題為非線性規(guī)劃問(wèn)題。
參考:(非線性規(guī)劃Python)計(jì)及動(dòng)態(tài)約束及節(jié)能減排環(huán)保要求的經(jīng)濟(jì)調(diào)度
2 算例?
2.1 算例
2.2 參數(shù)設(shè)置
求解NLP/非凸問(wèn)題時(shí),Python+Gurobi 的參數(shù)設(shè)置
1)需要設(shè)置參數(shù)'NonConvex=2';
2)Gurobi接受的冪函數(shù)y =中,α的最大值為2.c.雖然有解,但不一定是最優(yōu)值。
3)求解時(shí)間看模型規(guī)模。
觀察模型可知,要想求此模型,需要在編程中把多個(gè)變量乘積變成兩兩相乘的形式。令、y23=x2x3即可實(shí)現(xiàn)此功能。建模問(wèn)題解決了,調(diào)用Gurobi求解器即可求解了。可是運(yùn)行模型的時(shí)候,出問(wèn)題了: GurobiError: Q matrix is notpositive semi-definite (PSD). Set NonConvex parameter to 2 to solvemodel.錯(cuò)誤很明顯,告訴你需要設(shè)定參數(shù)NonConvex=2,Python+Gurobi設(shè)定參數(shù)的方法,即:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-491163.html
M_NLP.Params.NonConvex=2文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-491163.html
2.3 Python代碼實(shí)現(xiàn)
from gurobipy import *
# 創(chuàng)建模型
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")
# 設(shè)置目標(biāo)函數(shù)
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")
# 表示乘積項(xiàng)
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) # 輸出目標(biāo)值
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 * # 創(chuàng)建模型 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") # 設(shè)置目標(biāo)函數(shù) 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") # 表示乘積項(xiàng) 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) # 輸出目標(biāo)值 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 求解結(jié)果
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
到了這里,關(guān)于【數(shù)學(xué)建?!縋ython+Gurobi求解非線性規(guī)劃模型的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!