a = 0.95;
weight = [2;5;18;3;2;5;10;4;11;7;14;6];
value = [5;10;13;4;3;11;13;10;8;16;7;4];
value = -value;
restriction = 46;
num = 12;
newSol = ones(1,num);
curValue = inf; bestValue = inf;
curSol = newSol; bestSol =newSol;
T0 = 97; Tf = 3; t = T0;
p = 1;
while t >= Tf
for r = 1:100
%產生隨機擾動
tmp = ceil(rand*num);
newSol(1, tmp) =~newSol(1, tmp);
%檢查是否滿足約束條件
while 1
q = (newSol*weight <= restriction);
if q
break;
else
p = ~p;
tmp1 = find(newSol == 1);
if p
newSol(1, tmp1) = 0;
else
newSol(1, tmp1(end)) = 0;
end
end
end
newValue = newSol*value;
if newValue < curValue
curValue = newValue;
curSol = newSol;
if newValue < bestValue
bestValue = newValue;
bestSol = newSol;
end
else
if rand < exp(-(newValue-curValue)/t)
curValue = newValue;
curSol = newSol;
else
newSol = curSol;
end
end
end
t = t*a;
end
disp('最優解爲:');
disp(bestSol);
disp('揹包中物品總價值爲:');
bestValue = -bestValue;
disp(bestValue);
disp('揹包中物品總重量爲:');
disp(bestSol*weight);
《模擬退火(SA)求解0-1揹包問題》
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.