-3

I have below function in 7D space (means x=(x1,x2,x3,x4,x5,x6,x7)) and I want find the minimum point of this function with hill climbing in matlab.

I found this link useful but I don't know how can I implement my function in Matlab.

enter image description here

Update:

I implement below code but I don't really know if it is correct.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 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
halfer
  • 19,824
  • 17
  • 99
  • 186
Yuseferi
  • 7,931
  • 11
  • 67
  • 103
  • 1
    You probably realize this function is riddled with local minima; by inspection the minimum is [0,0,0,0,0,0,0]... – Floris Dec 22 '13 at 19:46
  • @Floris,yes I know, but I want implement it with hill-climbing,the method is important not really arrive to local optimum.,can you help me? – Yuseferi Dec 22 '13 at 20:03
  • @zhilevan: Use the description at wikipedia, think about a useful definition of a neighbour and implement it. – Daniel Dec 23 '13 at 01:49
  • Are you looking for the minimum or maximum? You need steepest descent, not hill climbing. – Floris Dec 23 '13 at 19:29
  • Could it simply be that you have `best = -100000000;` before looking for a better value? You will not find a smaller minimum... – Floris Dec 27 '13 at 14:38

1 Answers1

0

I implement it here. I hope be useful for another reader that have problem.

clear all ,close all;
clc;
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);
for (j=1:20)
    s=floor(rand(7,1)*64-32) ;
    i=0;
    convergence=0;
    while(convergence~=1 && i <10000)
        % 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);
        %step=rand();
        step=0.005; % this is step of climbing
        for tt=1:7
            arr=[0;0;0;0;0;0;0]; 
            arr(tt)=step;
            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)=-step;
            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)=step;
            s=s+arr;
        elseif(vi>7 && vi ~= 15)
            arr(index)=-step;
            s=s+arr;
            else
             convergence=1; %this means no neighbor has better value than current a
                            %maybe this point be local optimom
        end     
        i=i+1;
    end
    disp('*****************************');
    disp(sprintf('Step of convergence %i:',i));
    disp('coordination of optimum point :');
    disp(s');
    disp('*****************************');
end
halfer
  • 19,824
  • 17
  • 99
  • 186
Yuseferi
  • 7,931
  • 11
  • 67
  • 103