非线性规划

最小化目标函数:

\[f(x) = 5x_{1}-2x_{2}+x_{3} \]

约束条件如下:

\[ \begin{matrix} x_{1}+x_{2}+x_{3} & = & 1 \\ x_{1}^{2}+x_{2}^{2}+x_{3}^2 & \le & 1 \\ -x_{1}-x_{2}-x_{3} & \le & -1 \\ x_{1}-x_{3}^{2} & \ge & 0 \end{matrix} \]

其解\(x = [-9.4591001698063e-12,0.99999999999627,0]^{T}\),最小值是\(-2.0000000000398\)。

local print = require('package.print').print
local mol = libminoptlab

function test(x)
	return 5*x[1]-2*x[2]+x[3]
end

function eqcon(x)
	return {x[1]+x[2]+x[3]-1}
end

function ineqcon(x)
	return {x[1]^2+x[2]^2+x[3]^2-1,x[3]^2-x[1],1-x[1]-x[2]-x[3]}
end

local eps = 1e-5
local n = 2000
local algo = mol.listoptalgo()[1]

local lb = {}
local ub = {}

for i = 1,3 do
	table.insert(lb,-100)
	table.insert(ub,100)
end

local best,fbest = mol.fmincon(test,lb,ub,eqcon,ineqcon,algo,eps,n)

print({
	best = best,
	fbest = fbest,
	eq = eqcon(best),
	ineq = ineqcon(best)
})