%% Objective and Constraints Having a Common Function in Serial or Parallel, Problem-Based
% This example shows how to avoid calling a function twice when it computes values for both the objective and the constraints using the problem-based approach. For the solver-based approach, see Objective and Nonlinear Constraints in the Same Function.
% You typically use such a function in a simulation. Solvers usually evaluate the objective and nonlinear constraint functions separately. This evaluation is wasteful when you use the same calculation for both results.
% This example also shows the effect of parallel computation on solver speed. For time-consuming functions, computing in parallel can speed the solver, as can avoiding calling the time-consuming function repeatedly at the same point. Using both techniques together speeds the solver the most.
%% Create Time-Consuming Function That Computes Several Quantities
% The computeall function returns outputs that are part of the objective and nonlinear constraints.

type computeall

% The function includes a pause(1) statement to simulate a time-consuming function.
%
%% Create Optimization Variables
% This problem uses a four-element optimization variable.

x = optimvar('x',4);

%
%% Convert Function Using 'ReuseEvaluation'
% Convert the computeall function to an optimization expression. To save time during the optimization, use the 'ReuseEvaluation' name-value pair. To save time for the solver to determine the output expression sizes (this happens only once), set the 'OutputSize' name-value pair to [1 1], indicating that both f and c are scalar.

[f,c] = fcn2optimexpr(@computeall,x,'ReuseEvaluation',true,'OutputSize',[1 1]);

%
%% Create Objective, Constraint, and Problem
% Create the objective function from the f expression.

obj = f + 20*(x(3) - x(4)^2)^2 + 5*(1 - x(4))^2;

% Create the nonlinear inequality constraint from the c expression.

cons = c <= 0;

% Create an optimization problem and include the objective and constraint.

prob = optimproblem('Objective',obj);
prob.Constraints.cons = cons;
show(prob)

%
%% Solve Problem
% Monitor the time it takes to solve the problem starting from the initial point x0.x = [-1;1;1;2].

x0.x = [-1;1;1;2];
x0.x = x0.x/norm(x0.x); % Feasible initial point
tic
[sol,fval,exitflag,output] = solve(prob,x0)
time1 = toc

% The number of seconds for the solution is just over the number of function evaluations, which indicates that the solver computed each evaluation only once.

fprintf("The number of seconds to solve was %g, and the number of evaluation points was %g.\n",time1,output.funcCount)

%
% If, instead, you do not call fcn2optimexpr using 'ReuseEvaluation', then the solution time doubles.

[f2,c2] = fcn2optimexpr(@computeall,x,'ReuseEvaluation',false);
obj2 = f2 + 20*(x(3) - x(4)^2)^2 + 5*(1 - x(4))^2;
cons2 = c2 <= 0;
prob2 = optimproblem('Objective',obj2);
prob2.Constraints.cons2 = cons2;
tic
[sol2,fval2,exitflag2,output2] = solve(prob2,x0);
time2 = toc

%
%% Parallel Processing
% If you have a Parallel Computing Toolbox™ license, you can save even more time by computing in parallel. To do so, set options to use parallel processing, and call solve with options.

options = optimoptions(prob,'UseParallel',true);
tic
[sol3,fval3,exitflag3,output3] = solve(prob,x0,'Options',options);
time3 = toc

% Using parallel processing and 'ReuseEvaluation' together provides a faster solution than using 'ReuseEvaluation' alone. See how long it takes to solve the problem using parallel processing alone.
%

tic
[sol4,fval4,exitflag4,output4] = solve(prob2,x0,'Options',options);
time4 = toc

%
%% Summary of Timing Results
% Combine the timing results into one table.

timingtable = table([time1;time2;time3;time4],...
    'RowNames',["Reuse Serial";"No Reuse Serial";"Reuse Parallel";"No Reuse Parallel"])

% For this problem, on a computer with a 6-core processor, computing in parallel takes about half the time of computing in serial, and computing with 'ReuseEvaluation' takes about half the time of computing without 'ReuseEvaluation'. Computing in parallel with 'ReuseEvaluation' takes about a quarter of the time of computing in serial without 'ReuseEvaluation'.
%
% Copyright 2018-2019 The MathWorks, Inc.