-3
我在7D空間(意思是x =(x1,x2,x3,x4,x5,x6,x7))有以下函數,我想用matlab中的爬山算法找到這個函數的最小點。在7D空間爬山
我發現this link有用,但我不知道如何在Matlab中實現我的功能。
更新:
我實現下面的代碼,但我真的不知道這是否是正確的。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Create a grid of states %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clear all ,close all;
n=7;
range=[-32.768:0.1:32.768];
x=[0,0,0,0,0,1,1];
F=-20*exp(-0.2*sqrt(1/n*sum(x.^2)))-exp(1/n*sum(cos(2*pi*x)))+20 +exp(1);
F1=zeros(7,2);
best = -100000000; % Best value found so far.
for (j=1:20)
% Pick a starting location at random, and try and find the maximum state by hill climbing.
% Repeat this a (to be precise, repeat it until j = 20).
s=floor(100*rand(7,1)) ;
% Generate successors, and compute the one with the maximum value.
% Only consider states to the N, S, E, W, and NoMove.
for (i=1:100)
% Find successors
S0=s;
F0=-20*exp(-0.2*sqrt(1/n*sum(S0.^2)))-exp(1/n*sum(cos(2*pi*S0)))+20 +exp(1);
for tt=1:7
arr=[0;0;0;0;0;0;0];
arr(tt)=1;
S1=s+arr;
F1(tt,1)=-20*exp(-0.2*sqrt(1/n*sum(S1.^2)))-exp(1/n*sum(cos(2*pi*S1)))+20 +exp(1);
arr(tt)=-1;
S1=s+arr;
F1(tt,2)=-20*exp(-0.2*sqrt(1/n*sum(S1.^2)))-exp(1/n*sum(cos(2*pi*S1)))+20 +exp(1);
end
[v,vi] = max([F1(:,1)',F1(:,1)',F0]);
arr=[0;0;0;0;0;0;0];
index=mod(vi,7);
if(index==0)
index=7;
end
if(vi<=7 && vi ~= 15)
arr(index)=1;
s=s+arr;
elseif(vi>7 && vi ~= 15)
arr(index)=-1;
s=s+arr;
else
s=s ; %% for better understanding
end
end
end
你可能會意識到這個函數充斥着局部最小值;通過檢查的最小值是[0,0,0,0,0,0,0] ... – Floris
@弗洛里斯,是的,我知道,但我想用爬山來實現它,方法很重要,不是真的到達本地最佳的,你能幫助我嗎? – zhilevan
@zhilevan:使用維基百科的描述,思考一個有用的鄰居定義並實現它。 – Daniel