GEMRec-Gallery / Archive /optimization2.py
Ricercar's picture
important bug fix for image selection
97b4d0f
raw
history blame
1.49 kB
import numpy as np
from scipy.optimize import minimize
if __name__ == '__main__':
# Given subset of m values for x_1, x_2, and x_3
x1_subset = [2, 3, 4]
x2_subset = [0, 1]
x3_subset = [5, 6, 7]
# Full set of possible values for x_1, x_2, and x_3
x1_full = [1, 2, 3, 4, 5]
x2_full = [0, 1, 2, 3, 4, 5]
x3_full = [3, 5, 7]
# Define the objective function for quantile-based ranking
def objective_function(w):
y_subset = [x1 * w[0] + x2 * w[1] + x3 * w[2] for x1, x2, x3 in zip(x1_subset, x2_subset, x3_subset)]
y_full_set = [x1 * w[0] + x2 * w[1] + x3 * w[2] for x1 in x1_full for x2 in x2_full for x3 in x3_full]
# Calculate the 90th percentile of y values for the full set
y_full_set_90th_percentile = np.percentile(y_full_set, 90)
# Maximize the difference between the 90th percentile of the subset and the 90th percentile of the full set
return - min(y_subset) + y_full_set_90th_percentile
# Bounds for w_1, w_2, and w_3 (-1 to 1)
bounds = [(-1, 1), (-1, 1), (-1, 1)]
# Perform bounded optimization to find the values of w_1, w_2, and w_3 that maximize the objective function
result = minimize(objective_function, np.zeros(3), method='TNC', bounds=bounds)
# Get the optimal values of w_1, w_2, and w_3
w_1_opt, w_2_opt, w_3_opt = result.x
# Print the results
print("Optimal w_1:", w_1_opt)
print("Optimal w_2:", w_2_opt)
print("Optimal w_3:", w_3_opt)