min f ( x ) s . t . { A x ≤ b A e q ⋅ x = b e q c ( x ) ≤ 0 c e q ( x ) = 0 l b ≤ x ≤ u b 其中 , A , x , b , A e q , b e q , l b , u b 含义同线性规划 f ( x ) , c ( x ) , c e q ( x ) 代表非线性向量函数 \min f(x) \\ s.t. {Ax≤bAeq ⋅x=beqc(x)≤0ceq(x)=0lb≤x≤ub\\ 其中,A,x,b,Aeq,beq,lb,ub含义同线性规划\\ f(x),c(x),ceq(x)代表非线性向量函数 minf(x)s.t.⎩ ⎨ ⎧Ax≤bAeq ⋅x=beqc(x)≤0ceq(x)=0lb≤x≤ub其中,A,x,b,Aeq,beq,lb,ub含义同线性规划f(x),c(x),ceq(x)代表非线性向量函数
如:
min
f
(
x
)
=
x
1
2
+
x
2
2
+
x
3
2
+
8
s
.
t
.
{
x
1
2
−
x
2
+
x
3
2
≥
0
x
1
+
x
2
2
+
x
3
2
≤
20
−
x
1
−
x
2
2
+
2
=
0
x
2
+
2
x
3
2
=
3
x
1
,
x
2
,
x
3
≥
0
\min f(x)=x_1^2+x_2^2+x_3^2+8\\ s.t. {x21−x2+x23≥0x1+x22+x23≤20−x1−x22+2=0x2+2x23=3x1,x2,x3≥0\\
minf(x)=x12+x22+x32+8s.t.⎩
⎨
⎧x12−x2+x32≥0x1+x22+x32≤20−x1−x22+2=0x2+2x32=3x1,x2,x3≥0
化为标准型:
min
f
(
x
)
=
x
1
2
+
x
2
2
+
x
3
2
+
8
s
.
t
.
{
−
x
1
2
+
x
2
−
x
3
2
≤
0
x
1
+
x
2
2
+
x
3
2
−
20
≤
0
−
x
1
−
x
2
2
+
2
=
0
x
2
+
2
x
3
2
−
3
=
0
0
≤
x
1
,
x
2
,
x
3
\min f(x)=x_1^2+x_2^2+x_3^2+8\\ s.t. {−x21+x2−x23≤0x1+x22+x23−20≤0−x1−x22+2=0x2+2x23−3=00≤x1,x2,x3\\
minf(x)=x12+x22+x32+8s.t.⎩
⎨
⎧−x12+x2−x32≤0x1+x22+x32−20≤0−x1−x22+2=0x2+2x32−3=00≤x1,x2,x3
[x,fval]=fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
fun可以为自定义的非线性向量函数,代表
f
(
x
)
f(x)
f(x)x0是x的初始值A,b,Aeq,beq,lb,ub的含义同linprog函数nonlcon为非线性约束,可以为自定义的非线性向量函数,代表
c
(
x
)
c(x)
c(x),
c
e
q
(
x
)
ceq(x)
ceq(x)options为优化可选项% fun.m
function y = fun(x)
% 产生目标函数
y=x(1)^2+x(2)^2+x(3)^2+8;
end
% nonlcon.m
function [c,ceq]= nonclon(x)
% 产生非线性约束
% c为非线性不等式约束
% ceq为非线性等式约束
c=[-x(1)^2+x(2)-x(3)^2 ;x(1)+x(2)^2+x(3)^2-20];
ceq=[-x(1)-x(2)^2+2 ;x(2)+2*x(3)^2-3];
end
% NonlinprogTest.m
x0=[1 ;1 ;1];
lb=[0 ;0 ;0];
[x,fval]=fmincon('fun',x0,[],[],[],[],lb,[],'nonclon');
display(x);
display(fval);
x =
0.5522
1.2033
0.9478
fval =
10.6511
from scipy import optimize
import numpy as np
class Nolinprog:
"""非线性规划问题类"""
def __init__(self,
fun,
x0,
A=[],
b=[],
Aeq=[],
beq=[],
lb=None,
ub=None,
c=None,
ceq=None):
# 初始化参数
self.fun = fun
self.x0 = x0
self.A = A
self.b = b
self.Aeq = Aeq
self.beq = beq
self.lb = lb
self.ub = ub
self.c = c
self.ceq = ceq
self.cons = self.__make_cons()
self.bounds = None
self.x, self.fval, self.status = None, None, False
def __make_cons(self):
# 创建约束条件
cons = []
# 线性等式约束
if np.size(self.Aeq) and np.size(self.beq):
for i in range(self.Aeq.shape[0]):
cons.append({
'type':
'eq',
'fun': (lambda x: sum(self.Aeq[i] * x) - self.beq[i])
})
# 线性不等式约束(>=0)
if np.size(self.A) and np.size(self.b):
for i in range(self.A.shape[0]):
cons.append({
'type': 'ineq',
'fun': (lambda x: sum(self.A[i] * x) - self.b[i])
})
# 非线性不等式约束(>=0)
if self.c:
for fun in self.c():
cons.append({'type': 'ineq', 'fun': fun})
# 非线性等式约束
if self.ceq:
for fun in self.ceq():
cons.append({'type': 'eq', 'fun': fun})
# 上下界约束
if not self.lb:
self.lb = -np.ones(self.x0.shape) * np.inf
if not self.ub:
self.ub = np.ones(self.x0.shape) * np.inf
self.bounds = np.array(list(zip(self.lb, self.ub)), dtype=object)
cons = tuple(cons)
return cons
def solve(self):
# 求解规划问题
result = optimize.minimize(self.fun,
self.x0,
constraints=self.cons,
bounds=self.bounds)
self.x, self.fval, self.status = result.x, result.fun, result.success
return result
def show(self, for_min=True):
# 展示规划结果
if self.status == False:
print("无满足条件的解")
return
if for_min:
print(f"当x取 {self.x} 时,得最小值{self.fval}")
else:
print(f"当x取 {self.x} 时,得最大值{-self.fval}")
测试结果如下:
# 创建目标函数
def fun(x):
y = x[0]**2 + x[1]**2 + x[2]**2 + 8
return y
# 创建非线性不等式约束函数(>=0)
def c():
c1 = lambda x: x[0]**2 - x[1] + x[2]**2
c2 = lambda x: -x[0] - x[1]**2 - x[2]**2 + 20
return c1, c2
# # 创建非线性等式约束函数
def ceq():
ceq1 = lambda x: -x[0] - x[1]**2 + 2
ceq2 = lambda x: x[1] + 2 * x[2]**2 - 3
return ceq1, ceq2
x0 = np.array([1, 1, 1])
lb = [0, 0, 0]
# 求解非线性规划问题
pro = Nolinprog(fun, x0, c=c, ceq=ceq, lb=lb)
pro.solve()
pro.show()
当x取 [0.55216734 1.20325918 0.94782404] 时,得最小值10.651091840594646