Python SciPy differential_evolution yield progress

36 views Asked by At

I am using SciPy differential_evolution to solve an optimization problem. I have a web application that I want to show the progress in. What would be a good technique for giving intermediate feedback of the optimization process? Does SciPy support the yield keyword?

2

There are 2 answers

1
Nima_Ebr On

No, the differential_evolution function in SciPy does not directly support the yield keyword for providing intermediate feedback during the optimization process.

However, you can implement a custom callback function that prints out or logs the optimization progress at each iteration. You can use this callback function to provide feedback to your web application. Here's an example of how you can do it:

import numpy as np
from scipy.optimize import rosen, differential_evolution

# Define the objective function
def objective_function(x):
    return rosen(x)

# Define a custom callback function to print out the progress
def callback(xk, convergence):
    print(f"Iteration {callback.iteration}: Best solution: {xk}, Best value: {rosen(xk)}, Convergence: {convergence}")
    callback.iteration += 1
callback.iteration = 0  # Initialize the iteration counter

# Define the bounds for the variables
bounds = [(-5, 5)] * 2

# Perform differential evolution optimization with the custom callback
result = differential_evolution(objective_function, bounds, callback=callback, disp=True)

# Print the final result
print("\nFinal result:")
print(f"Best solution: {result.x}, Best value: {result.fun}, Convergence: {result.success}")

In this example, the callback function is called at each iteration of the optimization process. It prints out the iteration number, the current best solution, the value of the objective function at the best solution, and the convergence status. You can modify this function to log the progress or send it to your web application as needed.

0
Andrew Nelson On

You can gain feedback on solver process by providing a callback:

A callable called after each iteration. Has the signature:

callback(intermediate_result: OptimizeResult)

where intermediate_result is a keyword parameter containing an OptimizeResult with attributes x and fun, the best solution found so far and the objective function. Note that the name of the parameter must be intermediate_result for the callback to be passed an OptimizeResult.

The callback also supports a signature like:

callback(x, convergence: float=val)

val represents the fractional value of the population convergence. When val is greater than 1.0, the function halts.

Introspection is used to determine which of the signatures is invoked.

Global minimization will halt if the callback raises StopIteration or returns True; any polishing is still carried out.

That intermediate_result will also contain the current population.

differential_evolution does not use yield. However, the internal solver backend is an iterator. This backend is considered private.