爬山
您可以尝试一些随机优化算法,例如Hill Climbing,在其中您从随机解决方案开始(如@Don Reba 指出的),并查看一组与成本函数相关的更好的相邻解决方案。我将使用一些示例 python 代码来解释这个想法。
获取邻居解决方案
对于您的邻居,您可以使用一个简单的函数,例如:
n_params = 5 # number of parameters
upper_bound = 5 # upper limit of your parameters
lower_bound = 0 # lower limit of your parameters
def get_neighbors(solution):
neighbors = []
for i in range(n_params):
x = copy.deepcopy(solution)
if x[i] < upper_bound:
x[i] += 1 # increment one of the components
neighbors.append(x)
x = copy.deepcopy(solution)
if x[i] > lower_bound:
x[i] -= 1 # decrement one of the components
neighbors.append(x)
return neighbors
如果您有 [1,3,4,2,2] 的当前解决方案,通过增加或减少任何组件,您将获得 10 个不同的邻居,如下所示:
[2, 3, 4, 2, 2],
[0, 3, 4, 2, 2],
[1, 4, 4, 2, 2],
[1, 2, 4, 2, 2],
[1, 3, 5, 2, 2],
[1, 3, 3, 2, 2],
[1, 3, 4, 3, 2],
[1, 3, 4, 1, 2],
[1, 3, 4, 2, 3],
[1, 3, 4, 2, 1]
这里我们假设每个参数都是一个整数。您可以通过调整步长(例如,0.05)来获得更多的粒度。
爬山的迭代
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
成本函数
为了完整起见,我将使用我自己的成本函数(仅用于演示目的),即
f(x) = x1^1 - x2^2 + x3^3 - x4^4 + x5^5
那是 :
def get_cost(solution):
cost = 0
for i,param in enumerate(solution):
cost += (-1.)**i * param**(i+1)
return cost
优化结果:
这是结果。我们使用 [4, 0, 1, 3, 1] 的随机初始猜测。经过 14 步(评估 14*10 = 140 个邻居)后,我们找到了 [0, 5, 0, 5, 0] 的最佳答案,这使成本最小化。对于蛮力,您必须评估 6^6 = 46656 个解决方案。当您拥有高维解决方案时,可以节省更多的运行时间。
请注意,由于这是一种随机方法,因此会找到局部最小值作为最终结果(尽管有时它与全局最小值相同,但不能保证)。然而实际上它已经足够好了。
initial solution: [4 0 1 3 1]
hill-climbing cost at step 1: -75
hill-climbing cost at step 2: -250
hill-climbing cost at step 3: -619
hill-climbing cost at step 4: -620
hill-climbing cost at step 5: -621
hill-climbing cost at step 6: -622
hill-climbing cost at step 7: -623
hill-climbing cost at step 8: -624
hill-climbing cost at step 9: -627
hill-climbing cost at step 10: -632
hill-climbing cost at step 11: -639
hill-climbing cost at step 12: -648
hill-climbing cost at step 13: -649
hill-climbing cost at step 14: -650
Final solution: [0 5 0 5 0]
相关帖子
这里有一个相关但更复杂的问题:Algorithm for selection n vector out of a set while minimize cost
上面的所有代码都可以在这里找到。