matlab中的fmincon函数的用法!急

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了matlab中的fmincon函数的用法!急相关的知识,希望对你有一定的参考价值。

x0=[1;1;1;1];
A=[]
b=[]
Aeq=[1 1 1 1];
beq=[100000];
VLB=[0;0;0;0];
VUB=[];
w=[100000];
[x,favl]=fmincon('100000/(x(1)*0.971/33.5+x(2)*0.445/5.39+x(3)*0.3659/9.2+x(4)*0.195/9.05)',x0,A,b,Aeq,beq,VLB,VUB);
x
sum=100000+100000/favl
我想在fmincon中加入一个变量,以便以后做循环,比如将100000/(x(1)*0.971/33.5+x(2)*0.445/5.39+x(3)*0.3659/9.2+x(4)*0.195/9.05)中的100000变成一个数组,每循环一次,它都将取数组中的下一个数,这个在matlab中怎么实现啊!!!!!急急急急急急急急急急急啊!!!!!!!@#¥%……&

一、fmincon函数基本介绍
求解问题的标准型为
min F(X)
s.t
AX <= b
AeqX = beq
G(x) <= 0
Ceq(X) = 0
VLB <= X <= VUB

其中X为n维变元向量,G(x)与Ceq(X)均为非线性函数组成的向量,其它变量的含义与线性规划,二次规划中相同,用Matlab求解上述问题,基本步骤分为三步:
1. 首先建立M文件fun.m定义目标函数F(X):
function f = fun(X);
f = F(X)

2. 若约束条件中有非线性约束:G(x) <= 0 或 Ceq(x) = 0,则建立M文件nonlcon.m定义函数G(X)和Ceq(X);
function [G, Ceq] = nonlcon(X)
G = ...
Ceq = ...

3. 建立主程序,非线性规划求解的函数时fmincon,命令的基本格式如下:
[转载]Matlab fmincon函数用法

注意:
(1)fmincon函数提供了大型优化算法和中型优化算法。默认时,若在fun函数中提供了梯度(options 参数的GradObj设置为\'on\'),并且只有上下界存在或只有等式约束,fmincon函数将选择大型算法,当既有等式约束又有梯度约束时,使用中型算法。
(2)fmincon函数的中型算法使用的是序列二次规划法。在每一步迭代中 求解二次规划子问题,并用BFGS法更新拉格朗日Hessian矩阵。
(3)fmincon函数可能会给出局部最优解,这与初值X0的选取有关。

二、实例
1. 第一种方法,直接设置边界
主要是指直接设置A,b等参数。
例1:min f = -x1 - 2*x2 + 1/2*x1^2 + 1/2 * x2^2
2*x1 + 3*x2 <= 6
x1 + 4*x2 <= 5
x1, x2 >= 0

function ex131101

x0 = [1; 1];
A = [2, 3; 1, 4];
b = [6, 5];
Aeq = [];
beq = [];
VLB = [0; 0];
VUB = [];
[x, fval] = fmincon(@fun3, x0, A, b, Aeq, beq, VLB, VUB)

function f = fun3(x)
f = -x(1) - 2*x(2) + (1/2)*x(1)^2 + (1/2)*x(2)^2;

2. 第二种方法,通过函数设置边界
例2: min f(x) = exp(x1) * (4*x1^2 + 2*x2^2 + 4*x1*x2 + 2*x2 + 1)
x1 + x2 = 0
1.5 + x1 * x2 - x1 - x2 <= 0
-x1*x2 - 10 <= 0
function youh3
clc;
x0 = [-1, 1];
A = [];b = [];
Aeq = []; beq = [];
vlb = []; vub = [];
[x, fval] = fmincon(@fun4, x0, A, b, Aeq, beq, vlb, vub, @mycon)

function f = fun4(x);
f = exp(x(1)) * (4*x(1)^2 + 2*x(2)^2 + 4*x(1)*x(2) + 2*x(2) + 1);

function [g, ceq] = mycon(x)
g = [1.5 + x(1)*x(2) - x(1) - x(2); -x(1)*x(2) - 10];
ceq = [x(1) + x(2)];

3. 进阶用法,增加梯度以及传递参数
这里用无约束优化函数fminunc做示例,对于fmincon方法相同,只需将边界项设为空即可。
(1)定义目标函数
function [J, grad] = costFunction(theta, X, y)
%COSTFUNCTION Compute cost and gradient for logistic regression
% J = COSTFUNCTION(theta, X, y) computes the cost of using theta as the
% parameter for logistic regression and the gradient of the cost
% w.r.t. to the parameters.

% Initialize some useful values
m = length(y); % number of training examples

% You need to return the following variables correctly
J = 0;
grad = zeros(size(theta));

% ====================== YOUR CODE HERE ======================
% Instructions: Compute the cost of a particular choice of theta.
% You should set J to the cost.
% Compute the partial derivatives and set grad to the partial
% derivatives of the cost w.r.t. each parameter in theta
%
% Note: grad should have the same dimensions as theta
%

z = X * theta;
hx = 1 ./ (1 + exp(-z));
J = 1/m * sum([-y\' * log(hx) - (1 - y)\' * log(1 - hx)]);

for j = 1: length(theta)
grad(j) = 1/m * sum((hx - y)\' * X(:,j));
end

% =============================================================

end

(2)优化求极小值
% Set options for fminunc
options = optimset(\'GradObj\', \'on\', \'MaxIter\', 400);

% Run fminunc to obtain the optimal theta
% This function will return theta and the cost
[theta, cost] = ...
fminunc(@(t)(costFunction(t, X, y)), initial_theta, options);

% [theta, cost] = ...
% fminunc(@(t)(costFunction(t, X, y)), initial_theta);
% Print theta to screen
fprintf(\'Cost at theta found by fminunc: %fn\', cost);
fprintf(\'theta: n\');
fprintf(\' %f n\', theta);
参考技术A 可以把'10000/(x(1).......'这个写成如下形式:
w=[100000,w(2),...,w(n)];
for i=1:length(w)
y=@(x)w(i)/(x(1)*0.971/33.5.........;
[x,favl]=fmincon(y,x0,A,b,Aeq,beq,VLB,VUB);
end
或者:可以写一个函数myfun.m
function f=myfun(x)
global p
f=p/(x(1)*0.971/33.5..............;
------------------------------------
主函数调用部分写成
global p
...
...
w=[w(1),w(2),....w(n)];
for i=1:n
p=w(i);
[x,favl]=fmincon(@myfun,x0,A,b,Aeq,beq,VLB,VUB);
end

使用定义的 Matlab 函数进行 fmincon 优化

【中文标题】使用定义的 Matlab 函数进行 fmincon 优化【英文标题】:fmincon optimization with defined Matlab function 【发布时间】:2019-02-05 10:43:41 【问题描述】:

是否可以将优化函数fmincon 与 Matlab 定义的函数一起使用?

我写了一个函数,我给出了几个常量参数(实数或复数),现在,每次我改变这些参数,结果都会改变(你不说)。

[output1, output2] = my_function(input1,input2,input3,input4)

我看到fmincon 函数允许在给定约束条件下找到最佳结果。假设我想找到仅作用于input1 的最佳输出,并保持所有其他输入不变。是否可以定义类似的东西

fmincon(@(input1)my_function,[1,2],[],mean)

对于input1,从 1 到 2 表示最佳值 mean,其中平均值是其他一些结果的平均值。

我知道这是一个非常模糊的问题,但我无法给出一个最简单的例子,因为function 做了很多事情

第一次尝试有多个输出给了我错误Only functions can return multiple values.

然后我只尝试了一个输出,如果我使用

output1 = @(input1)function(input2,input3);
fmincon(@output1,[1,2],[],mean)

我得到了错误

错误:“output1”以前用作变量,与此处用作函数或命令的名称相冲突。 有关详细信息,请参阅 MATLAB 文档中的“MATLAB 如何识别命令语法”。

fmincon(@my_function,[1,2],[],mean) 我得到Not enough input arguments.

【问题讨论】:

你试过了吗?如果是这样,请告诉我们。否则,向我们展示您失败的尝试。 MATLAB 定义的函数与用户定义的函数的不同之处仅在于它们是专有的且通常是封闭源代码,在编程方面与您编写的内容没有什么不同。 我添加了一些尝试 您的问题似乎与 fmincon 无关,只是对 MATLAB 语法的使用不当。您不能使用 function 这个词,因为它是保留的。使用my_fun 之类的其他内容。同样,匿名函数(例如@(input1) sum(input1))仅输出 1 个值,因此您可能需要在单独的文件中创建一个函数来执行您想要的操作。没有一个真实的例子,我无法帮助更多,一个真实的minimal reproducible example 【参考方案1】:

输入应该用在你的函数定义中——阅读anonymous functions应该如何写。您不必使用匿名函数来定义实际的目标函数(下面的myFunction),您可以在自己的文件中使用函数。关键是目标函数应该返回一个要最小化的标量。

这里有一个非常简单的例子,使用fmincon根据初始猜测[1.5,1.5]找到myFunction中的最小值。

% myFunction is min when x=1,y=2
myFunction = @(x,y) (x-1).^2 + (y-2).^2;
% Define the optimisation function.
% This should take one input (can be an array) 
% and output a scalar to be minimised
optimFunc = @(P) myFunction( P(1), P(2) );

% Use fmincon to find the optimum solution, based on some initial guess
optimSoln = fmincon( optimFunc, [1.5, 1.5] );

% >> optimSoln
% optimSoln =
%     0.999999990065893   1.999999988824129

% Optimal x = optimSoln(1), optimal y = optimSoln(2);

您可以看到计算出的最优值并不完全是[1,2],但它在默认最优容差范围内。您可以更改 fmincon 求解器的选项 - 阅读 documentation。


如果你想保持y=1 为常量,你只需要更新函数定义:

% We only want solutions with y=1
optimFunc_y1 = @(P) myFunction( P(1), 1 ); % y=1 always
% Find new optimal solution
optimSoln_y1 = fmincon( optimFunc_y1, 1.5 );

% >> optimSoln_y1
% optimSoln_y1 = 
%    0.999999990065893
% Optimal x when y=1 = optimSoln(1)

您可以使用fminconABAeqBeq 输入来添加不等式约束,但这太宽泛,无法在此处介绍,请参阅文档。


请注意,您使用关键字function 的方式是无效的语法。我在演示中为函数使用了有效的变量名。

【讨论】:

以上是关于matlab中的fmincon函数的用法!急的主要内容,如果未能解决你的问题,请参考以下文章

Matlab fmincon函数

matlab中主函数调用fmincon函数,结果出问题

使用定义的 Matlab 函数进行 fmincon 优化

急!matlab solve用法

急!matlab solve用法

Matlab非线性优化函数:fmincon();