# Differential evolution solutions for the Rosenbrock and Rastigrin functions

## Introduction

Mike Croucher’s blog post today, Which MATLAB Optimization functions can solve my problem?, demonstrated the solvers() function in MATLAB 2022b’s Optimization Toolbox. This function recommends one of ten MATLAB optimizers based on the objective function you give it. Pretty cool!

A few months ago I showed how SciPy’s differential evolution function can be called from MATLAB to solve global optimization problems. Mike’s post showed solution metrics for the Rosenbrock and Rastigrin functions for all ten solvers in the Optimization Toolbox. I couldn’t help but wonder how differential evolution stacks up.

## TL;DR

Differential evolution is roughly in the middle of the pack for both problems in terms of number of function evaluations. It found the correct solution in both cases but needed a relatively large number of of function evaluations, 4053 for Rosenbrock and 2193 for Rastigrin.

## MATLAB/Python Rosenbrock solution

Python:

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  # file: rosenbrock_bridge.py from scipy.optimize import differential_evolution def rosen(independent_vars): x, y = independent_vars return 100*(y - x**2)**2 + (1 - x)**2 def call_DE(bounds): DE_result = differential_evolution(rosen, bounds) soln = { 'success' : DE_result.success, 'message' : DE_result.message, 'x' : DE_result.x, 'nfev' : DE_result.nfev, } return soln 

MATLAB:

 1 2 3 4  % file: rosenbrock.m rosen = py.importlib.import_module('rosenbrock_bridge'); bounds = py.tuple({[-3, 3], [0, 9]}); result = rosen.call_DE(bounds) 

I tested the code in MATLAB 2020b and MATLAB 2022b. The results were the same:

MATLAB:

>> rosenbrock

result =

Python dict with no properties.

{'success': True, 'message': 'Optimization terminated successfully.',
'x': array([1., 1.]), 'nfev': 4053}

## MATLAB/Python Rastigrin solution

Python:

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16  # file: rastigrin_bridge.py from scipy.optimize import differential_evolution from math import cos, pi def rast(independent_vars): x, y = independent_vars return 20 + x**2 + y**2 - 10*(cos(2*pi*x) + cos(2*pi*y)) def call_DE(bounds): DE_result = differential_evolution(rast, bounds) soln = { 'success' : DE_result.success, 'message' : DE_result.message, 'x' : DE_result.x, 'nfev' : DE_result.nfev, } return soln 

MATLAB:

 1 2 3 4  % file: rastigrin.m rast = py.importlib.import_module('rastigrin_bridge'); bounds = py.tuple({[-70, 130], [-70, 130]}); result = rast.call_DE(bounds) 

The result:

MATLAB:

>> rastigrin

result =

Python dict with no properties.

{'success': True, 'message': 'Optimization terminated successfully.',
'x': array([2.54892996e-10, 2.49528753e-09]), 'nfev': 2193}