I need to minimize a function objective() that takes a list of tuples x as argument.
But there's a catch : the tuples can only be choosen in a predefined set of tuples.
A minimal example would be:
import numpy as np
from scipy.optimize import minimize
initial_guess = [(2.,0.6), (0.9,8.1)]
possible_values = [(0.,0.), (1.7,2.2), (10.,1.2), (0.,0.15), (4.1,6.2)]
def objective(x:list):
return np.sum([item[0]**2 + item[1]**2 for item in x])
# Need to add a constraint to select elements of x in elements of possible_values
# Minimize objective function
result = minimize(fun = objective,
x0 = initial_guess)
print(result.x)
This code procudes the following error:
ValueError: 'x0' must only have one dimension.
Thanks
You can keep just the tuples in
xthat are present inpossible_values: