On 1/27/2012 1:01 PM, Michal Kvasnicka wrote:
> Alan Weiss <aweiss@mathworks.com> wrote in message
> <jfug8r$1bh$1@newscl01ah.mathworks.com>...
>> On 1/27/2012 5:15 AM, Michal Kvasnicka wrote:
>> > "Michal Kvasnicka" wrote in message
>> > <jftsl6$sor$1@newscl01ah.mathworks.com>...
>> >> "Michal Kvasnicka" wrote in message
>> >> <jftncd$eg1$1@newscl01ah.mathworks.com>...
>> >> > Hi, I just start to play with Global Optimization Toolbox.
>> >> > > How can I create two separate files for Objective function and
>> >> Constraints in the case of one complex blackbox function BB(x)
>> >> optimization (where x is the input parameters vector). The blackbox
>> >> function produce the output vector (obj(x), c1(x), c2(x), c3(x)),
>> >> containing object function value together with corresponding
>> >> constraints. So in this case I am not able to separate objective
>> >> function and constraints evaluation to the separate independent files
>> >> (functions).
>> >> > > Is in Global optimization Toolbox any possibility to evaluate
>> >> objective function and constraints in one file (function)?
>> >> > > Michal
>> >>
>> >> After thorough manual reading I see, that there is probably no simple
>> >> way how to separate my blackbox function BB(x) on objective function
>> >> and constraints function without need to evaluate BB(x) in each
>> >> function separately.
>> >>
>> >> I am looking for way how to evaluate BB(x) and then extract objective
>> >> function value and constraints.
>> >
>> > This thread is probably relevant to my problem:
>> >
>> http://www.mathworks.com/matlabcentral/newsreader/view_thread/269936#707304
>>
>>
>> Here is a worked example. Just replace "computeall" with "BB"
>> everywhere it appears:
>>
>> 
>> function [x,f,eflag,outpt] = runobjconstr(x0,opts)
>> if nargin == 1 % no options supplied
>> opts = [];
>> end
>>
>> xLast = []; % Last place computeall was called
>> myf = []; % Value of objective at xLast
>> myc = []; % Value of nonlinear inequality constraint
>> myceq = []; % Value of nonlinear equality constraint
>> fun = @objfun; % the objective function, nested below
>> cfun = @constr; % the constraint function, nested below
>>
>> % Call fmincon
>> [x,f,eflag,outpt] = fmincon(fun,x0,[],[],[],[],[],[],cfun,opts);
>>
>> function y = objfun(x)
>> if ~isequal(x,xLast) % Check if computation is necessary
>> [y,myc,myceq] = computeall(x);
>> myf = y;
>> xLast = x;
>> else % If no computation necessary, use existing value
>> y = myf;
>> end
>> end
>>
>> function [c,ceq] = constr(x)
>> if ~isequal(x,xLast) % Check if computation is necessary
>> [myf,c,ceq] = computeall(x);
>> myc = c;
>> myceq = ceq;
>> xLast = x;
>> else % If no computation necessary, use existing values
>> c = myc;
>> ceq = myceq;
>> end
>> end
>>
>> function [f1,c1,ceq1] = computeall(x)
>> ceq1 = [];
>> c1 = norm(x)^2  1;
>> f1 = 100*(x(2)  x(1)^2)^2 + (1x(1))^2;
>> pause(1) % simulate expensive computation
>> end
>>
>> end
>> 
>>
>> To run the file:
>> opts = optimset('Algorithm','interiorpoint','Display','off');
>> x0 = [1,1];
>> tic
>> [x,fval,exitflag,output] = runobjconstr(x0,opts);
>> toc
>>
>> Alan Weiss
>> MATLAB mathematical toolbox documentation
> Alan,
> thanks a lot for your example!
> Is this method, based on nested functions, applicable with the following
> option:
> options = gaoptimset('UseParallel', 'always', 'Vectorized', 'off');
>
> The parallelized optimization is crucial in my case, because my
> blackbox function is very CPU time consuming.
>
> Michal
I believe that the technique should work in parallel, but I have only
tested it with fmincon in parallel, not with ga or patternsearch. I will
be interested to hear if it works for your problem.
Speaking of patternsearch, let me make my usual plug that you should try
patternsearch instead of ga. You will very likely get faster, more
reliable answers, and the solver is easier to tune. The only point that
is slightly more difficult in patternsearch is you have to choose
initial points yourself. It is not difficult to set up, for example,
random initial points within bounds. If lb and ub are your vectors of
bounds, with all components finite,
x0 = lb + rand(size(lb)).*(ub  lb);
It can also be helpful to tune patternsearch by changing the initial
mesh size and the final mesh tolerance.
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation
