Look at this simple optimization problem which had just a bit of complexity (penalty function):

I checked scipy and pulp library documentation in Python. I know that I can use "def" for my objective but it doesnt work even in a simple problem - I'm confused and don't know which solver or method should be used to solve it.
Even I try the easiest form of penalty function as shown below, a problem which just should try 4 given numbers instead of the y variable to minimize penalty function as objective (the answer is clearly y=-2 and the obj function will be -4):
from scipy.optimize import minimize
import numpy as np
def f(y):
if y>=0:
return y
else:
return 2*y
y = np.array([1,-1,2,-2])
res = minimize(f(y),args = [y])
But I am faced with this error:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Please help me to know how I should code this penalty function, and which other library or method I should use.