Я пытаюсь реализовать Gradient projection Descent для следующей задачи:
# the objective function
def f(x_1, x_2):
return ((x_1-1)**2 + (x_2-1)**2)
# the constraint function
def constraint(x_1, x_2):
return x_1**2 - x_2 + 1
Я использую time l oop, чтобы применить евклидову проекцию к поверхности ограничения, но это не дает мне правильного ответ.
def feasible_point(constraint_set):
"""
constraint_set: A list of constraint boundaries
example: [np.array([0.,1.5]), np.array([1., 2.])]
"""
lower_bound = constraint_set[0]
upper_bound = constraint_set[1]
return np.random.uniform(lower_bound, upper_bound)
# project onto the constraint surface
def projection(point, constraint_set, constraint, stepsize=0.01):
# start with a feasible point
if constraint(point):
return point
else:
start = feasible_point(constraint_set)
update = copy.copy(start)
done= True
while done:
# the gradient of Euclidean distance
gradient = (start - point)
# update the point
update = update - (stepsize * gradient)
# if the constraint is not satified we do one more iteration
if not constraint(update) <= 0:
done=False
return start
else:
start = copy.copy(update)
return start
А вот как это выглядит после проецирования x = (0,4, 0,4) на поверхность, что, очевидно, не является правильным ответом. Кто-нибудь знает, как это исправить?
Проекция точки x = (0,4, 0,4) на поверхность ограничения