Funciones
Funciones
...
lb, testFunctionNo)
nVar = length(A.position);
for idx = 1: 4
switch idx
case 1
stepVector = [stepSize(1) , 0 ];
case 2
stepVector = [0 , stepSize(2) ];
case 3
stepVector = [-stepSize(1) , 0 ];
case 4
stepVector = [0 , -stepSize(2) ];
end
for i = 1 : nVar
if neighbours(idx).position(i) > ub(i)
neighbours(idx).position(i) = ub(i)
end
if neighbours(idx).position(i) < lb(i)
neighbours(idx).position(i) = lb(i)
end
end
function [ o ] = objectiveFunction(initial ,
testFunctionNo)
x = initial;
switch testFunctionNo
case 1
o = 10 * -cos( 2* (x(1)^2+x(2)^2)^0.5 ) * ...
exp( -0.5 * ((x(1)+1)^2+(x(2)-1)^2)^0.5 ) + 5.1
;
case 2
o = -peaks(x(1),x(2)) + 8.5 ;
case 3
o =-1*( 0.2 + x(1).^2 + x(2).^2 -
0.1*cos(6*pi*x(1)) ...
- 0.1*cos(6*pi*x(2)) );
case 4
o = -1 * sum(x.^2);
end
end
%file1 : objective function
%file2 : getNeighbouring solution
%file 3: HC algoritmo
clear all
close all
clc
ub = [1 1];
lb = [-1 -1];
nVar = length(initial);
optimumFound = logical(false);
A.position = initial;
A.cost = cost_initial;
iter = 0;
iter = iter + 1;
trajectory(iter).position = A.position;
trajectory(iter).cost = A.cost;
improvement = logical(false);
for k = 1 : length(neighbours)
B = neighbours(k);
end
if improvement == false
optimumFound = true;
end
end
figure
set(gcf, 'position', [50, 50, 700, 700])
subplot(2,2,1)
x = lb(1) : stepSize(1):ub(1);
y = lb(2) : stepSize(2):ub(2);
for i = 1: length(x)
for j = 1 : length(y)
X = [x_new(i,j) , y_new(i,j)];
z_new(i,j) = objectiveFunction(X,
testFunctionNo);
end
end
for k = 1 : length(trajectory)
traj_fianl_x(k) = trajectory(k).position(1);
traj_fianl_y(k) = trajectory(k).position(2);
traj_fianl_z(k) = trajectory(k).cost;
end
plot3(traj_fianl_x, traj_fianl_y,traj_fianl_z, ...
'-.k' , 'lineWidth', 1)
plot3(traj_fianl_x(1),
traj_fianl_y(1),traj_fianl_z(1), '*r');
plot3(traj_fianl_x(end),
traj_fianl_y(end),traj_fianl_z(end), '*g');
subplot(2,2,2)
hold on
pcolor(x_new, y_new, z_new)
colormap jet
shading flat