前言:
现在各位老铁们对“带约束的最短路径算法”大概比较注意,姐妹们都需要学习一些“带约束的最短路径算法”的相关知识。那么小编在网络上网罗了一些对于“带约束的最短路径算法””的相关内容,希望大家能喜欢,兄弟们一起来学习一下吧!fseminf 是非线性规划求解器,用于求由下式指定的问题的最小值(带有额外参数的约束条件):
1.fseminf函数
x = fseminf(fun,x0,ntheta,seminfcon)x = fseminf(fun,x0,ntheta,seminfcon,A,b)x = fseminf(fun,x0,ntheta,seminfcon,A,b,Aeq,beq)x = fseminf(fun,x0,ntheta,seminfcon,A,b,Aeq,beq,lb,ub)x = fseminf(fun,x0,ntheta,seminfcon,A,b,Aeq,beq,lb,ub,options)x = fseminf(problem)[x,fval] = fseminf(___)[x,fval,exitflag,output] = fseminf(___)[x,fval,exitflag,output,lambda] = fseminf(___)
b 和 beq 是向量。A 和 Aeq 是矩阵。c(x)、ceq(x) 和 Ki(x,wi) 是返回向量的函数。f(x) 是返回标量的函数。f(x)、c(x) 和 ceq(x) 可以是非线性函数。向量(或矩阵)Ki(x,wi) ≤ 0 是 x 和一组附加变量 w1,w2,...,wn 的连续函数。变量 w1,w2,...,wn 是长度最大为 2 的向量。x、lb 和 ub 可以作为向量或矩阵传递;请参阅矩阵参数。x = fseminf(fun,x0,ntheta,seminfcon) 从 x0 开始,求解满足 seminfcon 中定义的 ntheta 半无限约束条件的函数 fun 的最小值。x = fseminf(fun,x0,ntheta,seminfcon,A,b) 还尝试满足线性不等式 A*x ≤ b。x = fseminf(fun,x0,ntheta,seminfcon,A,b,Aeq,beq) 还会在满足线性等式 Aeq*x = beq 的情况下进行最小化。如果不存在不等式,请设置 A = [] 和 b = []。x = fseminf(fun,x0,ntheta,seminfcon,A,b,Aeq,beq,lb,ub) 对 x 中的设计变量定义一组下界和上界,使解始终在 lb ≤ x ≤ ub 范围内。[x,fval,exitflag,output] = fseminf(___) 还返回描述退出条件的值 exitflag,以及提供优化过程信息的结构体 output。[x,fval,exitflag,output,lambda] = fseminf(___) 返回结构体 lambda,该结构体的字段包含在解 x 处的拉格朗日乘数。x = fseminf(fun,x0,ntheta,seminfcon,A,b,Aeq,beq,lb,ub,options) 使用 options 所指定的优化选项执行最小化。使用 optimoptions 可设置这些选项。x = fseminf(problem) 求 problem 的最小值,它是 problem 中所述的一个结构体。[x,fval] = fseminf(___),对任何上述输入语法,返回目标函数 fun 在解 x 处的值。
2.实例1
程序
clc;clear all;objfun = @(x)(x-1)^2;%匿名函数x0 = 0.2;%设置初始点 x0 = 0.2。ntheta = 1;%指定一个半无限约束。A = [];b = [];Aeq = [];beq = [];lb = 0;ub = 2;options = optimoptions('fseminf','PlotFcn', 'optimplotfval','Display','iter','MaxIterations',1000);[x,fval,exitflag,output,lambda] = fseminf(objfun,x0,ntheta,@seminfcon,A,b,Aeq,beq,lb,ub,options )%通过调用 fseminf 求解问题并查看结果。x1 = fseminf(objfun,x0,ntheta,@seminfcon)
seminfcon.m函数
function [c, ceq, K1, s] = seminfcon(x,s)c = [];%非线性不等式约束条件ceq = [];%非线性等式约束条件%t 的初始采样间隔(步长为 0.01,采样间隔为 0 到 1)if isnan(s) s = [0.01 0];endt = 0:s(1):1;% 以及半无限约束函数 g(x,t)K1 = (x - 0.5) - (t - 0.5).^2;end
运行结果
Max Line search Directional First-order Iter F-count f(x) constraint steplength derivative optimality Procedure 0 2 0.64 -0.2 1 4 0.25 4.684e-12 1 -1.6 0.3 Local minimum found that satisfies the constraints.Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance,and constraints are satisfied to within the value of the constraint tolerance.<stopping criteria details>x = 0.5000fval = 0.2500exitflag = 1output = 包含以下字段的 struct: iterations: 2 funcCount: 4 lssteplength: 1 stepsize: 4.6841e-12 algorithm: 'active-set' firstorderopt: 9.3682e-12 constrviolation: 4.6841e-12 message: '↵Local minimum found that satisfies the constraints.↵↵Optimization completed because the objective function is non-decreasing in ↵feasible directions, to within the value of the optimality tolerance,↵and constraints are satisfied to within the value of the constraint tolerance.↵↵<stopping criteria details>↵↵Optimization completed: The first-order optimality measure, 9.368173e-12, is less↵than options.OptimalityTolerance = 1.000000e-06, and the maximum constraint violation,↵4.684060e-12,is less than options.ConstraintTolerance = 1.000000e-06.↵↵'lambda = 包含以下字段的 struct: lower: 0 upper: 0 eqlin: [0×1 double] eqnonlin: [0×1 double] ineqlin: [0×1 double] ineqnonlin: [1×0 double]Local minimum found that satisfies the constraints.Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance,and constraints are satisfied to within the value of the constraint tolerance.<stopping criteria details>x1 = 0.5000>>
3.实例2
方式一 目标函数以匿名函数形式计算
程序
clc;clear all;objfun = @(x) -0.225*(x(1))-0.225*(x(2))-0.1*(x(3));x0 = [0.5 0.5 1];ntheta = 1;A = [1,2 1;3,4 0;5,6 0];b = [10;20;30];Aeq = [1,2,3;2,4,1];beq = [10;20];lb = [0 0 -inf];ub = [];options = optimoptions('fseminf','PlotFcn', 'optimplotfval','Display','iter','MaxIterations',1000);x = fseminf(objfun,x0,ntheta,@seminfcon1,A,b,Aeq,beq,lb,ub,options)
seminfcon1.m函数
function [c, ceq, K1, s] = seminfcon1(x,s)c = [];%非线性不等式约束条件ceq = [];%非线性等式约束条件%t 的初始采样间隔(步长为 0.01,采样间隔为 0 到 1)if isnan(s) s = [0.01 0];endt(1,:) = 0:s(1):1;t(2,:) = 1 - t(1);num = length(t(1,:));t(3,:) = linspace(-5,5,num );t(4,:) = linspace(-5,5,num );t(5,:) = linspace(-5,5,num );save t% 以及半无限约束函数 g(x,t)K1 = (0.48-0.225).*(t(1,:)-x(1))+(0.51-0.225).*(t(2,:)-x(2))-0.1*(t(3,:)-x(3))-t(4,:)-t(5,:)*0.5;end
运行结果
Max Line search Directional First-order Iter F-count f(x) constraint steplength derivative optimality Procedure 0 4 -0.325 16 Infeasible start point 1 8 -4.33029 56.98 1 -0.135 5.89 infeasibleConverged to an infeasible point.fseminf stopped because the size of the current search direction is less thantwice the value of the step size tolerance but constraints are not satisfied to within the value of the constraint tolerance.<stopping criteria details>x = 28.4915 -9.2458 0.0000>>
方式二 目标函数以函数句柄计算
程序
clc;clear all;x0 = [0.5 0.5 1];ntheta = 1;A = [1,2 1;3,4 0;5,6 0];b = [10;20;30];Aeq = [1,2,3;2,4,1];beq = [10;20];lb = [0 0 -inf];ub = [];options = optimoptions('fseminf','PlotFcn', 'optimplotfval','Display','iter','MaxIterations',1000);x = fseminf(@myfun,x0,ntheta,@seminfcon1,A,b,Aeq,beq,lb,ub,options)
seminfcon1.m函数
function [c, ceq, K1, s] = seminfcon1(x,s)c = [];%非线性不等式约束条件ceq = [];%非线性等式约束条件%t 的初始采样间隔(步长为 0.01,采样间隔为 0 到 1)if isnan(s) s = [0.01 0];endt(1,:) = 0:s(1):1;t(2,:) = 1 - t(1);num = length(t(1,:));t(3,:) = linspace(-5,5,num );t(4,:) = linspace(-5,5,num );t(5,:) = linspace(-5,5,num );save t% 以及半无限约束函数 g(x,t)K1 = (0.48-0.225).*(t(1,:)-x(1))+(0.51-0.225).*(t(2,:)-x(2))-0.1*(t(3,:)-x(3))-t(4,:)-t(5,:)*0.5;end
myfun.m
function y = myfun(x) y= -0.225*(x(1))-0.225*(x(2))-0.1*(x(3));end
运行结果
Max Line search Directional First-order Iter F-count f(x) constraint steplength derivative optimality Procedure 0 4 -0.325 16 Infeasible start point 1 8 -4.33029 56.98 1 -0.135 5.89 infeasibleConverged to an infeasible point.fseminf stopped because the size of the current search direction is less thantwice the value of the step size tolerance but constraints are not satisfied to within the value of the constraint tolerance.<stopping criteria details>x = 28.4915 -9.2458 0.0000
fseminf 停止的原因,以整数形式返回。
标志
说明
1
函数收敛于解 x。
4
搜索方向的模小于指定的容差,约束违反度小于 options.ConstraintTolerance。
5
方向导数的模小于指定容差,约束违反度小于 options.ConstraintTolerance。
0
迭代次数超过 options.MaxIterations 或函数计算次数超过 options.MaxFunctionEvaluations。
-1
由输出函数或绘图函数停止。
-2
找不到可行点。
4.参考内容
[1]
作 者 | 郭志龙
编 辑 | 郭志龙
校 对 | 郭志龙
标签: #带约束的最短路径算法