Maximize function with fminsearch

2019-07-06 20:05发布

Within my daily work, I have got to maximize a particular function making use of fminsearch; the code is:

clc
 clear all
 close all

 f = @(x,c,k) -(x(2)/c)^3*(((exp(-(x(1)/c)^k)-exp(-(x(2)/c)^k))/((x(2)/c)^k-(x(1)/c)^k))-exp(-(x(3)/c)^k))^2;
 c = 10.1;
 k = 2.3;
 X = fminsearch(@(x) f(x,c,k),[4,10,20]);

It works fine, as I expect, but not the issue is coming up: I need to bound x within certain limits, as:

 4 < x(1) < 5
 10 < x(2) < 15
20 < x(3) < 30

To achieve the proper results, I should use the optimization toolbox, that I unfortunately cannot hand.

Is there any way to get the same analysis by making use of only fminsearch?

3条回答
在下西门庆
2楼-- · 2019-07-06 20:08

Andrey has the right idea, and the smoother way of providing a penalty isn't hard: just add the distance to the equation.

To keep using the anonymous function:

f = @(x,c,k, Xmin, Xmax) -(x(2)/c)^3*(((exp(-(x(1)/c)^k)-exp(-(x(2)/c)^k))/((x(2)/c)^k-(x(1)/c)^k))-exp(-(x(3)/c)^k))^2 ...
+ (x< Xmin)*(Xmin' - x' + 10000) + (x>Xmax)*(x' - Xmax' + 10000) ;
查看更多
孤傲高冷的网名
3楼-- · 2019-07-06 20:09

Well, not using fminsearch directly, but if you are willing to download fminsearchbnd from the file exchange, then yes. fminsearchbnd does a bound constrained minimization of a general objective function, as an overlay on fminsearch. It calls fminsearch for you, applying bounds to the problem.

Essentially the idea is to transform your problem for you, in a way that your objective function sees as if it is solving a constrained problem. It is totally transparent. You call fminsearchbnd with a function, a starting point in the parameter space, and a set of lower and upper bounds.

For example, minimizing the rosenbrock function returns a minimum at [1,1] by fminsearch. But if we apply purely lower bounds on the problem of 2 for each variable, then fminsearchbnd finds the bound constrained solution at [2,4].

rosen = @(x) (1-x(1)).^2 + 105*(x(2)-x(1).^2).^2;

fminsearch(rosen,[3 3])     % unconstrained
ans =
   1.0000    1.0000

fminsearchbnd(rosen,[3 3],[2 2],[])     % constrained
ans =
   2.0000    4.0000

If you have no constraints on a variable, then supply -inf or inf as the corresponding bound.

fminsearchbnd(rosen,[3 3],[-inf 2],[])
ans =
       1.4137            2
查看更多
神经病院院长
4楼-- · 2019-07-06 20:22

The most naive way to bound x, would be giving a huge penalty for any x that is not in the range.

For example:

   function res = f(x,c,k)
        if x(1)>5 || x(1)<4
            penalty = 1000000000000;
        else
            penalty = 0;
        end
        res = penalty - (x(2)/c)^3*(((exp(-(x(1)/c)^k)-exp(-(x(2)/c)^k))/((x(2)/c)^k-(x(1)/c)^k))-exp(-(x(3)/c)^k))^2;
   end

You can improve this approach, by giving the penalty in a smoother way.

查看更多
登录 后发表回答