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

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()

Reference

Python Optimization Algorithms in Practice (Su Zhenyu)

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

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

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.