Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 为什么我的模拟退火算法会产生越来越差的解并提前收敛?_Algorithm_Search_Machine Learning_Artificial Intelligence_Simulated Annealing - Fatal编程技术网

Algorithm 为什么我的模拟退火算法会产生越来越差的解并提前收敛?

Algorithm 为什么我的模拟退火算法会产生越来越差的解并提前收敛?,algorithm,search,machine-learning,artificial-intelligence,simulated-annealing,Algorithm,Search,Machine Learning,Artificial Intelligence,Simulated Annealing,为什么我的程序会产生越来越糟糕的解决方案,而且会这么早收敛? 我最近一直在阅读优化和各种元启发式技术,最近我决定尝试实现模拟退火,如本文所述 我相信我对这个理论理解得足够好 有一个接受概率由以下函数引导 math.exp(-cost_delta/temp) 其中,成本增量是当前解决方案的“成本”与新随机生成的建议解决方案的成本之差。成本增量越大,新解决方案被接受的概率就越低。随着迭代次数的增加,温度“冷却”并接近0,新解决方案被接受的可能性就越小 你的成本是由你想要最小化或最大化的目

为什么我的程序会产生越来越糟糕的解决方案,而且会这么早收敛?

我最近一直在阅读优化和各种元启发式技术,最近我决定尝试实现模拟退火,如本文所述

我相信我对这个理论理解得足够好

有一个接受概率由以下函数引导

 math.exp(-cost_delta/temp)    
其中,成本增量是当前解决方案的“成本”与新随机生成的建议解决方案的成本之差。成本增量越大,新解决方案被接受的概率就越低。随着迭代次数的增加,温度“冷却”并接近0,新解决方案被接受的可能性就越小

你的成本是由你想要最小化或最大化的目标函数决定的,你的目标函数会根据你的问题而改变

我实现了一个模拟退火函数,如博文和维基百科中所述:

def simulated_annealing(initial_state):
current_state = initial_state
current_cost = cost_of_state(current_state)
temp = 3.0
num_iteration = 0

while current_cost > 0:
    neighbour = get_random_neighbour(current_state)
    neighbour_cost = cost_of_state(neighbour)

    cost_delta = neighbour_cost - current_cost

    if cost_delta <= 0 or random.random() < math.exp(-cost_delta/temp):
        current_state = neighbour
        current_cost = neighbour_cost

    print('current cost: '+str(current_cost))
    print('Num of iterations: '+str(num_iteration))

    num_iteration += 1
    if num_iteration % 500 == 0 and temp > 0.10:
        temp -= 0.10

return current_state, num_iteration
并根据某些目标函数计算当前状态的成本:

def cost_of_state(state):
cost = 15

for i , h in enumerate(state):
    cost -= sum([
        h[nat] == 'brit' and h[col] == 'red',
        h[nat] == 'swede' and h[ani] == 'dog',
        h[nat] == 'dane' and h[bev] == 'tea',
        i< 4 and h[col] == 'green' and state[i+1][col] == 'white',
        h[col] == 'green' and h[bev] == 'coffee',
        h[cig] == 'pall mall' and h[ani] == 'bird',
        h[col] == 'yellow' and h[cig] == 'dunhill',
        i == 2 and h[bev] == 'milk',
        i == 0 and h[nat] == 'norwegian',
        h[cig] == 'blends' and ((i > 0 and state[i-1][ani] == 'cat') or (i < 4 and state[i+1][ani] == 'cat')),
        h[ani] == 'horse' and ((i > 0 and state[i-1][cig] == 'dunhill') or (i < 4 and state[i-1][cig] == 'dunhill')),
        h[cig] == 'blue master' and h[bev] == 'root beer',
        h[nat] == 'german' and h[cig] == 'prince',
        h[nat] == 'norwegian' and ((i > 0 and state[i-1][col] == 'blue') or (i < 4 and state[i+1][col] == 'blue')),
        h[cig] == 'blends' and ((i > 0 and state[i-1][bev] == 'water') or (i < 4 and state[i+1][bev] == 'water')),
    ])

return cost    
现在,我的问题是,当我运行所有程序时,当我实际运行我的程序时,我只看到一些状态变化。下面您可以看到我从前10次迭代中得到的输出

current cost: 11
Num of iterations: 0
current cost: 11
Num of iterations: 1
current cost: 11
Num of iterations: 2
current cost: 10
Num of iterations: 3
current cost: 10
Num of iterations: 4
current cost: 10
Num of iterations: 5
current cost: 10
Num of iterations: 6
current cost: 11
Num of iterations: 7
current cost: 11
Num of iterations: 8
current cost: 11
Num of iterations: 9
current cost: 11
Num of iterations: 10
current cost: 11
当我到达迭代65时,我的解决方案实际上变得越来越糟糕,事情似乎停滞不前:

urrent cost: 12
Num of iterations: 63
current cost: 12
Num of iterations: 64
current cost: 13
Num of iterations: 65
current cost: 13

您的交换在邻居函数中实现错误。(顺便说一句,如果您查看了算法生成的解决方案,您可能会注意到。)@user2357112感谢您的回答。这确实回答了我的一个问题,但现在我有了相反的问题。它不是在最差的解决方案(成本=13)附近暂停,而是在最佳解决方案附近暂停(成本=2或1,取决于我如何设置温度)。我回去把我的代码和文章中的代码进行了比较,我发现成本函数中有一部分规则写得不正确,但我仍然有同样的问题,只是方向相反。嗯。。。是 啊这很正常。@user2357112虽然我知道元文化技术不能保证找到最佳解决方案,但为什么作者能够找到最佳解决方案,而我只能接近?您的交换在您的邻居函数中实现错误。(顺便说一句,如果你看看你的算法生成了什么样的解决方案,你可能会注意到。)@user2357112谢谢你的回答。这确实回答了我的一个问题,但现在我遇到了相反的问题。它不是在最差的解决方案(成本=13)附近停滞,而是在最好的解决方案附近停滞(成本=2或1,取决于我如何设置温度)。我回去将我的代码与文章中的代码进行了比较,我发现成本函数中有一条规则写得不正确,但我仍然有相反的问题。嗯……是的。这很正常。@user2357112虽然我知道元文字技术不能保证找到最佳解决方案,但为什么当我只能接近的时候,作者能找到最优的解决方案吗?
current cost: 11
Num of iterations: 0
current cost: 11
Num of iterations: 1
current cost: 11
Num of iterations: 2
current cost: 10
Num of iterations: 3
current cost: 10
Num of iterations: 4
current cost: 10
Num of iterations: 5
current cost: 10
Num of iterations: 6
current cost: 11
Num of iterations: 7
current cost: 11
Num of iterations: 8
current cost: 11
Num of iterations: 9
current cost: 11
Num of iterations: 10
current cost: 11
urrent cost: 12
Num of iterations: 63
current cost: 12
Num of iterations: 64
current cost: 13
Num of iterations: 65
current cost: 13