Index to Scalar variable error when trying to plot pareto front

24 Views Asked by At

I am trying to plot the pareto front for an equation but i keep getting "Index to scalar variable error". this is what the code looks like

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import differential_evolution

# Define the problem
def f(x):
    return [(x[0] - 2)**2 + (x[1] - 1)**2, (x[0] - 1)**2 + (x[1] - 2)**2]

# Define the bounds
bounds = [(0, 5), (0, 5)]

# Define the differential evolution function
result = differential_evolution(f, bounds, strategy='best1bin', popsize=50, tol=1e-7, mutation=(0.5, 1), recombination=0.7, seed=42)

# Extract the Pareto front
pareto_front = np.array([[f(individual[:2])[0], f(individual[2:])[0]] for individual in result.x])

# Print the Pareto front
print("Pareto Front:")
for individual in pareto_front:
    print("  ", individual)

# Plot the Pareto front
plt.scatter(pareto_front[:, 0], pareto_front[:, 1])
plt.xlabel("f1")
plt.ylabel("f2")
plt.title("Pareto Front")
plt.show()

This is the error i got " pareto_front = np.array([[f(individual[:2])[0], f(individual[:2])[1]] for individual in result.x]) File "C:\Users\Admin\Desktop\bj\new\new.py", line 19, in pareto_front = np.array([[f(individual[:2])[0], f(individual[:2])[1]] for individual in result.x]) IndexError: invalid index to scalar variable."

i expected it to plot the pareto graph but i get the following error=

pareto_front = np.array([[f(individual[:2])[0], f(individual[:2])[1]] for individual in result.x]) File "C:\Users\Admin\Desktop\bj\new\new.py", line 19, in pareto_front = np.array([[f(individual[:2])[0], f(individual[:2])[1]] for individual in result.x]) IndexError: invalid index to scalar variable.

0

There are 0 best solutions below