Skip to content

Instantly share code, notes, and snippets.

@greeness
Created December 2, 2012 00:02
Show Gist options
  • Save greeness/4186012 to your computer and use it in GitHub Desktop.
Save greeness/4186012 to your computer and use it in GitHub Desktop.
hill_climb_params.py
import numpy as np
import copy
n_params = 5
upper_bound = 5
lower_bound = 0
def get_neighbors(solution):
neighbors = []
for i in range(n_params):
x = copy.deepcopy(solution)
if x[i] < upper_bound:
x[i] += 1
neighbors.append(x)
x = copy.deepcopy(solution)
if x[i] > lower_bound:
x[i] -= 1
neighbors.append(x)
return neighbors
def get_cost(solution):
cost = 0
for i,param in enumerate(solution):
cost += (-1.)**i * param**(i+1)
return cost
def hill_climb():
initial_solution = np.random.randint(lower_bound, upper_bound, n_params)
current_solution = initial_solution
print 'initial solution', initial_solution
current_cost = get_cost(initial_solution)
step = 1
while True:
#try to replace each single component w/ its neighbors
lowest_cost = current_cost
lowest_solution = current_solution
print 'hill-climbing cost at step %6d: %d' % (step, lowest_cost)
neighbors = get_neighbors(current_solution)
for new_solution in neighbors:
neighbor_cost = get_cost(new_solution)
if neighbor_cost < lowest_cost:
lowest_cost = neighbor_cost
lowest_solution = new_solution
if lowest_cost >= current_cost:
break
else:
current_solution= lowest_solution
current_cost = lowest_cost
step += 1
return current_solution
print hill_climb()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment