30 lines
1.0 KiB
Python
30 lines
1.0 KiB
Python
import numpy
|
|
|
|
|
|
def find_sols(cost_array, jacobian, step_size=0.001, max_iterations=5, initial=(0, 0), desired_cost=1e-6, step_size_tries=30):
|
|
desired_cost_squared = desired_cost**2
|
|
current = numpy.array(initial)
|
|
iterations = 0
|
|
curr_cost = numpy.array(cost_array(current))
|
|
|
|
def total_cost(x):
|
|
cost = numpy.array(cost_array(x))
|
|
return cost.dot(cost)
|
|
|
|
while iterations < max_iterations:
|
|
curr_cost = numpy.array(cost_array(current))
|
|
if curr_cost.dot(curr_cost) < desired_cost_squared:
|
|
return ("Finished early", iterations, current)
|
|
gradient = .5 * numpy.matmul(numpy.transpose(jacobian(current)), curr_cost)
|
|
|
|
tries = step_size_tries
|
|
current_step = step_size
|
|
while total_cost(current - current_step * gradient) > (total_cost(current) - 0.5 * current_step * gradient.dot(gradient)):
|
|
current_step = current_step * .8
|
|
tries -= 1
|
|
if tries == 0:
|
|
return ("hit minimum step size", iterations, current)
|
|
current = current - current_step * gradient
|
|
iterations += 1
|
|
return ("Ran out of iterations", iterations, current)
|