import numpy as np from scipy.optimize import minimize, differential_evolution # Define the function y = x_1*w_1 + x_2*w_2 + x_3*w_3 def objective_function(w_indices): x_1 = x_1_values[int(w_indices[0])] x_2 = x_2_values[int(w_indices[1])] x_3 = x_3_values[int(w_indices[2])] return - (x_1 * w_indices[3] + x_2 * w_indices[4] + x_3 * w_indices[5]) # Use w_indices to get w_1, w_2, w_3 if __name__ == '__main__': # Given sets of discrete values for x_1, x_2, and x_3 x_1_values = [1, 2, 3, 5, 6] x_2_values = [0, 5, 7, 2, 1] x_3_values = [3, 7, 4, 5, 2] # Perform differential evolution optimization with integer variables # bounds = [(0, len(x_1_values) - 2), (0, len(x_2_values) - 1), (0, len(x_3_values) - 1), (-1, 1), (-1, 1), (-1, 1)] bounds = [(3, 4), (3, 4), (3, 4), (-1, 1), (-1, 1), (-1, 1)] result = differential_evolution(objective_function, bounds) # Get the optimal indices of x_1, x_2, and x_3 x_1_index, x_2_index, x_3_index, w_1_opt, w_2_opt, w_3_opt = result.x # Calculate the peak point (x_1, x_2, x_3) corresponding to the optimal indices x_1_peak = x_1_values[int(x_1_index)] x_2_peak = x_2_values[int(x_2_index)] x_3_peak = x_3_values[int(x_3_index)] # Print the results print("Optimal w_1:", w_1_opt) print("Optimal w_2:", w_2_opt) print("Optimal w_3:", w_3_opt) print("Peak Point (x_1, x_2, x_3):", (x_1_peak, x_2_peak, x_3_peak)) print("Maximum Value of y:", -result.fun) # Use negative sign as we previously used to maximize