Artificial Intelligence 2 min read

Visualizing and Implementing the Rastrigin Function in Python

The Rastrigin function, a challenging multimodal benchmark used to test optimization algorithms, is explained with its mathematical form, visual plot, and a complete Python implementation for generating and visualizing its landscape.

Model Perspective
Model Perspective
Model Perspective
Visualizing and Implementing the Rastrigin Function in Python

Rastrigin Function

The Rastrigin function is a classic nonlinear multimodal function with many local maxima and minima, making it difficult to locate the global minimum; it is commonly used to evaluate the performance of optimization algorithms.

Function Plot

Python Code

<code>import numpy as np
import matplotlib.pyplot as plt
from matplotlib import cm
from mpl_toolkits.mplot3d import Axes3D

# generate X and Y data
X = np.arange(-5, 5, 0.1)
Y = np.arange(-5, 5, 0.1)
X, Y = np.meshgrid(X, Y)

# objective function
A = 10
Z = 2 * A + X ** 2 - A * np.cos(2 * np.pi * X) + Y ** 2 - A * np.cos(2 * np.pi * Y)

# plot
fig = plt.figure()
ax = Axes3D(fig)
surf = ax.plot_surface(X, Y, Z, cmap=cm.coolwarm)
plt.show()
</code>

Reference

Python Optimization Algorithms in Practice (Su Zhenyu)

Pythonalgorithm testingmultimodal functionoptimization benchmarkRastrigin function
Model Perspective
Written by

Model Perspective

Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.