# Evaluate Functions in the Background Using `parfeval`

This example shows how to solve a simple optimization problem by using `parfeval`

to evaluate functions in the background. When you use `parfeval`

to evaluate functions in the background, you create `Future`

objects. You can collect the results as they become available and break out of the optimization loop early when the result is good enough.

To reproduce the same computations later, seed the random generator with the default value.

`rng("default")`

The objective function `objFun`

takes a row vector `x`

as input and returns a scalar value representing the output of the function. Define the objective function and the number of iterations for the optimization loop. Randomly generate candidates for the `x`

values to evaluate the objective function. Initialize the minimum value and index variables.

objFun = @(x) x(1)^2 + x(2)^2; numIterations = 500; xCandidates = rand(numIterations,2); minFval = inf; minIndex = inf;

Use `parfeval`

to evaluate the objective function for each set of `x`

candidates in the background.

When you use `parfeval`

to run computations in the background, the function creates and adds a `Future`

object for each computation to the pool queue. Futures remain in the queue until a worker becomes idle. When a worker becomes idle, it starts to compute a future if the queue is not empty. When a worker completes a future, the future is removed from the queue and the worker becomes idle.

For efficiency, preallocate an array of `Future`

objects.

f(1:numIterations) = parallel.FevalFuture; for i = 1:numIterations f(i) = parfeval(objFun,1,xCandidates(i,:)); end

Starting parallel pool (parpool) using the 'Processes' profile ... 08-Jul-2024 15:36:55: Job Queued. Waiting for parallel pool job with ID 26 to start ... Connected to parallel pool with 6 workers.

When a future completes, its `State`

property becomes `'finished'`

and its `Read`

property becomes `false.`

You can use the `fetchNext`

function to retrieve the results from the futures as they finish. After `fetchNext`

retrieves the output from the next unread future in array `f`

, MATLAB sets the `Read`

property of that future to `true`

.

Run a `for`

-loop with `numIterations`

iterations. In each iteration, use `fetchNext`

to retrieve the next completed future's index and value. Next, compare the new value to the existing minimum value, and update the minimum value and its index if a smaller value is found.

If a value less than or equal to `0.01`

is found, display the solution and exit the loop early.

for idx = 1:numIterations [completedIndex, fval] = fetchNext(f); if fval < minFval minFval = fval; minIndex = completedIndex; end if minFval <= 0.01 fprintf("Best solution found: x = [%f,%f], fval = %f\n", ... xCandidates(minIndex,1),xCandidates(minIndex,2),minFval); break; end end

Best solution found: x = [0.031833,0.093820], fval = 0.009816

Cancel any remaining futures.

```
cancel(f);
clear f;
```