clc
clear
x0=
[x,feval] = fgoalattain('erci',[5,400,5,1020],[20,-4,7,1.3,20,12],[1,1,1,1,1,1],[],[],[],[],[5,400,5,1020],[20,700,20,1200],'yueshu')
function y=erci(sol)
x1 = sol(1);
x2 = sol(2);
x3 = sol(3);
x4 = sol(4);
y(1) = -10.754*x1 + 1.445*x2 - 25.29*x3 + 0.719*x4 + 0.317*x1*x1 + 0.001*x1*x2 + 0.024*x1*x3 + 0.003*x1*x4 - 0.00005338*x2*x2 + 0.018*x2*x3 - 0.01*x2*x4 + 0.218*x3*x3 + 0.009*x3*x4 - 615.803;
y(2) = -1.067*x1 + +0.255*x2 - 1.496*x3 + 0.160*x4 + 0.086*x1*x1 + 0.001*x1*x2 - 0.052*x1*x3 - 0.001*x1*x4 + 0.00004775*x2*x2 + 0.002*x2*x3 - 0.0003*x2*x4 + 0.066*x3*x3 - 0.001*x3*x4 - 131.326;
y(3) = 0.0948*x1 - 0.039*x2 + 0.873*x3 + 0.007*x4 - 0.009*x1*x1 + 0.0004*x1*x2 - 0.002*x1*x3 - 0.001*x1*x4 + 0.00001251*x2*x2 - 0.001*x2*x3 + 0.00002463*x2*x4 + 0.00009444*x3*x3 - 0.0004*x3*x4 + 0.752;
y(4) = -0.00006840*x1 - 0.003*x2 + 0.007*x3 - 0.002*x4 - 0.001*x1*x1 - 0.00001482*x1*x2 + 0.001*x1*x3 + 0.00002242*x1*x4 - 0.0000008379*x2*x2 - 0.00001860*x2*x3 + 0.000004144*x2*x4 - 0.001*x3*x3 + 0.00001619*x3*x4 + 3.188;
y(5) = 0.0826*x1 - 0.0688*x2 + 2.195*x3 - 0.32*x4 - 0.137*x1*x1 - 0.002*x1*x2 - 0.097*x1*x3 + 0.002*x1*x4 + 0.0001*x2*x2 - 0.003*x2*x3 + 0.00005*x2*x4 - 0.113*x3*x3 + 0.003*x3*x4 + 365.347;
y(6) = 5.216*x1 - 0.870*x2 + 8.005*x3 - 0.517*x4 - 0.192*x1*x1 - 0.004*x1*x2 - 0.021*x1*x3 + 0.001*x1*x4 - 0.00001112*x2*x2 - 0.005*x2*x3 + 0.001*x2*x4 - 0.076*x3*x3 - 0.003*x3*x4 + 503.630;
function [c,ceq] = yueshu(x)
x1 = x(1);
x2 = x(2);
x3 = x(3);
x4 = x(4)
c(:,1) = -10.754*x1 + 1.445*x2 - 25.29*x3 + 0.719*x4 + 0.317*x1*x1 + 0.001*x1*x2 + 0.024*x1*x3 + 0.003*x1*x4 - 0.00005338*x2*x2 + 0.018*x2*x3 - 0.01*x2*x4 + 0.218*x3*x3 + 0.009*x3*x4 - 655.803;
c(:,2) = -1.067*x1 + 0.255*x2 - 1.496*x3 + 0.160*x4 + 0.086*x1*x1 + 0.001*x1*x2 - 0.052*x1*x3 - 0.001*x1*x4 + 0.00004775*x2*x2 + 0.002*x2*x3 - 0.0003*x2*x4 + 0.066*x3*x3 - 0.001*x3*x4 - 136.326;
c(:,3) = 1.067*x1 - 0.255*x2 + 1.496*x3 - 0.160*x4 - 0.086*x1*x1 - 0.001*x1*x2 + 0.052*x1*x3 + 0.001*x1*x4 - 0.00004775*x2*x2 - 0.002*x2*x3 + 0.0003*x2*x4 - 0.066*x3*x3 + 0.001*x3*x4 + 138.326;
c(:,4) = 0.0948*x1 - 0.039*x2 + 0.873*x3 + 0.007*x4 - 0.009*x1*x1 + 0.0004*x1*x2 - 0.002*x1*x3 - 0.001*x1*x4 + 0.00001251*x2*x2 - 0.001*x2*x3 + 0.00002463*x2*x4 + 0.00009444*x3*x3 - 0.0004*x3*x4 - 8.752;
c(:,5) = -0.00006840*x1 - 0.003*x2 + 0.007*x3 - 0.002*x4 - 0.001*x1*x1 - 0.00001482*x1*x2 + 0.001*x1*x3 + 0.00002242*x1*x4 - 0.0000008379*x2*x2 - 0.00001860*x2*x3 + 0.000004144*x2*x4 - 0.001*x3*x3 + 0.00001619*x3*x4 + 2.638;
c(:,6) = -0.0826*x1 + 0.0688*x2 - 2.195*x3 + 0.32*x4 + 0.137*x1*x1 + 0.002*x1*x2 + 0.097*x1*x3 - 0.002*x1*x4 - 0.0001*x2*x2 + 0.003*x2*x3 - 0.00005*x2*x4 + 0.113*x3*x3 - 0.003*x3*x4 - 375.347;
c(:,7) = 5.216*x1 - 0.870*x2 + 8.005*x3 - 0.517*x4 - 0.192*x1*x1 - 0.004*x1*x2 - 0.021*x1*x3 + 0.001*x1*x4 - 0.00001112*x2*x2 - 0.005*x2*x3 + 0.001*x2*x4 - 0.076*x3*x3 - 0.003*x3*x4 + 488.630;
麻烦大佬们看看是哪里出了问题,导致跑不了程序
代码不完整,无法判断
显示输出参数太多,应该如何修改,一共有6个约束式子
fgoalattain
求解涉及多目标的目标达到问题
语法
x = fgoalattain(fun,x0,goal,weight)
x = fgoalattain(fun,x0,goal,weight,A,b)
x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq)
x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq,lb,ub)
x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq,lb,ub,nonlcon)
x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq,lb,ub,nonlcon,options)
x = fgoalattain(problem)
[x,fval] = fgoalattain(___)
[x,fval,attainfactor,exitflag,output] = fgoalattain(___)
[x,fval,attainfactor,exitflag,output,lambda] = fgoalattain(___)
说明
fgoalattain
求解目标达到问题,这是多目标优化问题最小化的一种表示。
fgoalattain
求以下问题的最小值:
weight
、goal
、b 和 beq 是向量,A 和 Aeq 是矩阵,F(x)、c(x) 和 ceq(x) 是返回向量的函数。F(x)、c(x) 和 ceq(x) 可以是非线性函数。
x、lb 和 ub 可以作为向量或矩阵传递;
矩阵参数说明
Optimization Toolbox™ 求解器的许多参数都接受向量,例如初始点 x0
、下界 lb
和上界 ub
。它们也接受矩阵形式的参数,其中矩阵指任意大小的数组。如果求解器参数本身是数组而不是向量,您完全可以以数组形式提供这些参数。
以下说明求解器如何处理矩阵参数。
在处理前,求解器先在内部将矩阵参数转换为向量。例如,x0
变为 x0(:)
。有关此语法的解释,请参阅colon
中的 A(:)
条目,或数组索引的“用单个索引进行索引”部分。
对于输出,求解器将解 x
重构为与输入 x0
相同的大小。
当 x0
是矩阵时,求解器将 x
作为与 x0
大小相同的矩阵传递给目标函数和任何非线性约束函数。
不过,线性约束接受向量形式的 x
,即 x(:)
。也就是以下形式的线性约束
A*x ≤ b
或 Aeq*x = beq
接受向量(而不是矩阵)形式的 x
。请确保您的矩阵 A
或 Aeq
的列数与 x0
的元素数相同,否则求解器会出错。
<a href="https://ww2.mathworks.cn/help/optim/ug/fgoalattain.html#f814427_sep_shared-x"><code>x
= fgoalattain(fun
,x0
,goal
,weight
) 尝试从 x0
开始、用 weight
指定的权重更改 x
,使 fun
提供的目标函数达到 goal
指定的目标。
注意
传递额外参数说明如何将额外的参数传递给目标函数和非线性约束函数(如有必要)。
fgoalattain(fun
,x0
,goal
,weight
,A
,b
) 求解满足不等式 A*x ≤ b
的目标达到问题。
fgoalattain(fun
,x0
,goal
,weight
,A
,b
,Aeq
,beq
) 求解满足等式 Aeq*x = beq
的目标达到问题。如果不存在不等式,则设置 A = []
和 b = []
。
fgoalattain(fun
,x0
,goal
,weight
,A
,b
,Aeq
,beq
,lb
,ub
) 求解满足边界 lb
≤ x
≤ ub
的目标达到问题。如果不存在等式,请设置 Aeq = []
和 beq = []
。如果 x(i)
无下界,则设置 lb(i) = -Inf
;如果 x(i)
无上界,则设置 ub(i) = Inf
。
注意
如果为问题指定的输入边界不一致,则输出 x
为 x0
,输出 fval
为 []
。
fgoalattain(fun
,x0
,goal
,weight
,A
,b
,Aeq
,beq
,lb
,ub
,nonlcon
) 求解满足 nonlcon
所定义的非线性不等式 c(x)
或等式 ceq(x)
的目标达到问题。fgoalattain
进行优化,以满足 c(x) ≤ 0
和 ceq(x) = 0
。如果不存在边界,则设置 lb = []
和/或 ub = []
。
fgoalattain(fun
,x0
,goal
,weight
,A
,b
,Aeq
,beq
,lb
,ub
,nonlcon
,options
) 使用 options
所指定的优化选项求解目标达到问题。使用 optimoptions
可设置这些选项。
fgoalattain(problem
) 求解 problem
所指定的目标达到问题,其中 problem
是一个结构体,在 problem
中有述。通过从 Optimization 工具中导出问题来创建 problem
结构体,如导出您的工作中所述。
fgoalattain(___) 对上述任何语法,返回目标函数 fun
在解 x
处的值。
[x
,fval
,attainfactor
,exitflag
,output
]= fgoalattain(___) 还返回在解 x
处的达到因子、描述 fgoalattain
退出条件的值 exitflag
,以及包含优化过程信息的结构体 output
。
[x
,fval
,attainfactor
,exitflag
,output
,lambda
] = fgoalattain(___) 还返回结构体 lambda
,其字段包含在解 x
处的拉格朗日乘数
示例
目标函数是
此处,p_1
= [2,3] 且 p_2
= [4,1]。目标是 [3,6],权重是 [1,1],非线性约束是。
创建目标函数、目标和权重。
p_1 = [2,3]; p_2 = [4,1]; fun = @(x)[2 + norm(x-p_1)^2;5 + norm(x-p_2)^2/4]; goal = [3,6]; weight = [1,1];
非线性约束函数在 norm4.m
文件中。
type norm4
function [c,ceq] = norm4(x) ceq = []; c = norm(x)^2 - 4;
为线性约束和边界创建空输入参数。
A = []; Aeq = []; b = []; beq = []; lb = []; ub = [];
将初始点设置为 [1,1],并求解目标达到问题。
x0 = [1,1]; x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq,lb,ub,@norm4)
Local minimum possible. Constraints satisfied. fgoalattain stopped because the size of the current search direction is less than twice the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
x = 1×2 1.1094 1.6641
计算在解处的值。
fun(x)
ans = 2×1 4.5778 7.1991
fgoalattain
没有满足目标。尽管权重相等,但与其目标 3 相差约 1.58,与其目标 6 相差约 1.2。非线性约束会防止解 x
同等地达到各目标。
通过将选项设置为返回迭代输出来监控目标达到求解过程。
options = optimoptions('fgoalattain','Display','iter');
目标函数是
此处,p_1
= [2,3] 且 p_2
= [4,1]。目标是 [3,6],权重是 [1,1],线性约束是。
创建目标函数、目标和权重。
p_1 = [2,3]; p_2 = [4,1]; fun = @(x)[2 + norm(x-p_1)^2;5 + norm(x-p_2)^2/4]; goal = [3,6]; weight = [1,1];
创建表示 A*x <= b
的线性约束矩阵 A
和 b
。
A = [1,1]; b = 4;
为线性等式约束、边界和非线性约束创建空输入参数。
Aeq = []; beq = []; lb = []; ub = []; nonlcon = [];
设置初始点 [1,1] 并求解目标达到问题。
x0 = [1,1]; x = fgoalattain(fun,x0,goal,weight,A,b,Aeq,beq,lb,ub,nonlcon,options)
Attainment Max Line search Directional Iter F-count factor constraint steplength derivative Procedure 0 4 0 4 1 9 -1 2.5 1 -0.535 2 14 -1.115e-08 0.2813 1 0.883 3 19 0.1452 0.005926 1 0.883 4 24 0.1484 2.868e-06 1 0.883 5 29 0.1484 6.748e-13 1 0.883 Hessian modified Local minimum possible. Constraints satisfied. fgoalattain stopped because the size of the current search direction is less than twice the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
x = 1×2 2.0694 1.9306
报告的达到因子为正值表示 fgoalattain
找不到满足目标的解。
现在跑出来成这个样子是该如何调呢
fgoalattain已停止,因为它超出了功能评估限制,
下载新版本试试
options = optimset('Display','iter','Algorithm','interior-point','MaxIter',5000000,'MaxFunEvals',5000000);
调整 options
或者使用fimincon
语法
x = fmincon(fun,x0,A,b)
x = fmincon(fun,x0,A,b,Aeq,beq)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options)
x = fmincon(problem)
[x,fval] = fmincon(___)
[x,fval,exitflag,output] = fmincon(___)
[x,fval,exitflag,output,lambda,grad,hessian] = fmincon(___)