Generate a python code optimizing HARTMANN 6-DIMENSIONAL FUNCTION with plot graph
- User
Generate a python code optimizing HARTMANN 6-DIMENSIONAL FUNCTION with plot graph
- Assistant
To optimize the Hartmann 6-dimensional function using Python, we will use the Scipy library's optimization functions, and the popular plotting library, Matplotlib, to visualize the optimization process. Please make sure you have these libraries installed using:
pip install scipy matplotlibHere's the code for optimizing the Hartmann 6-dimensional function:
import numpy as np from scipy.optimize import minimize import matplotlib.pyplot as plt def hartmann6(x, alpha=[1.0, 1.2, 3.0, 3.2], A=[[10, 3, 17, 3.50, 1.7, 8], [0.05, 10, 17, 0.1, 8, 14], [3, 3.5, 1.7, 10, 17, 8], [17, 8, 0.05, 10, 0.1, 14]], P=[[1312, 1696, 5569, 124, 8283, 5886], [2329, 4135, 8307, 3736, 1004, 9991], [2348, 1451, 3522, 2883, 3047, 6650], [4047, 8828, 8732, 5743, 1091, 381]] ): result = 0 for i, a in enumerate(alpha): inner_sum = 0 for j in range(6): inner_sum += A[i][j] * (x[j] - P[i][j]) ** 2 result -= a * np.exp(-inner_sum) return result def plot_convergence(x_list, y_list, title): plt.plot(x_list, y_list, 'k-') plt.scatter(x_list, y_list, marker='o', s=40) plt.title(title) plt.xlabel('Iteration') plt.ylabel('Function Value') plt.show() if __name__ == '__main__': x0 = np.random.uniform(low=0, high=1, size=(6,)) bounds = [(0, 1)] * 6 fun_history = [] def record_fun_history(x): fun_history.append(hartmann6(x)) result = minimize(hartmann6, x0, callback=record_fun_history, bounds=bounds, options={'disp': True}) print(f"Optimal parameters: {result.x}") plot_convergence(range(1, len(fun_history)+1), fun_history, 'Hartmann 6-dimensional function optimization')This code optimizes the Hartmann 6-dimensional function using the
minimize()function from Scipy's optimize module, and plots the convergence of the optimization process using Matplotlib. The initial guess for the optimization is generated using random uniform values, and the bounds for each parameter in the optimization are set to (0, 1).
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.