Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
black-box function

Subject: black-box function

From: Michal Kvasnicka

Date: 27 Jan, 2012 08:31:09

Message: 1 of 6

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 black-box function BB(x) optimization (where x is the input parameters vector). The black-box 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

Subject: black-box function

From: Michal Kvasnicka

Date: 27 Jan, 2012 10:01:10

Message: 2 of 6

"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 black-box function BB(x) optimization (where x is the input parameters vector). The black-box 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 black-box 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.

Subject: black-box function

From: Michal Kvasnicka

Date: 27 Jan, 2012 10:15:09

Message: 3 of 6

"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 black-box function BB(x) optimization (where x is the input parameters vector). The black-box 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 black-box 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

Subject: black-box function

From: Alan Weiss

Date: 27 Jan, 2012 15:35:55

Message: 4 of 6

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 black-box function BB(x)
>> optimization (where x is the input parameters vector). The black-box
>> 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 black-box 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 + (1-x(1))^2;
  pause(1) % simulate expensive computation
end

end
--------------------------------

To run the file:
opts = optimset('Algorithm','interior-point','Display','off');
x0 = [-1,1];
tic
[x,fval,exitflag,output] = runobjconstr(x0,opts);
toc

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: black-box function

From: Michal Kvasnicka

Date: 27 Jan, 2012 18:01:10

Message: 5 of 6

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 black-box function BB(x)
> >> optimization (where x is the input parameters vector). The black-box
> >> 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 black-box 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 + (1-x(1))^2;
> pause(1) % simulate expensive computation
> end
>
> end
> --------------------------------
>
> To run the file:
> opts = optimset('Algorithm','interior-point','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 black-box function is very CPU time consuming.

Michal

Subject: black-box function

From: Alan Weiss

Date: 30 Jan, 2012 13:46:20

Message: 6 of 6

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 black-box function BB(x)
>> >> optimization (where x is the input parameters vector). The black-box
>> >> 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 black-box 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 + (1-x(1))^2;
>> pause(1) % simulate expensive computation
>> end
>>
>> end
>> --------------------------------
>>
>> To run the file:
>> opts = optimset('Algorithm','interior-point','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
> black-box 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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us