0% found this document useful (0 votes)
14 views5 pages

Funciones

The document describes functions to calculate objective functions and neighbors for optimization problems. It defines functions to calculate objective values for different test functions and get neighboring solutions. It then uses these functions in a hill climbing algorithm to optimize an objective function over iterations, tracking the trajectory and finding the best solution.

Uploaded by

Marbecita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Funciones

The document describes functions to calculate objective functions and neighbors for optimization problems. It defines functions to calculate objective values for different test functions and get neighboring solutions. It then uses these functions in a hill climbing algorithm to optimize an objective function over iterations, tracking the trajectory and finding the best solution.

Uploaded by

Marbecita
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

function [neighbours] = getNeighbours(A, stepSize, ub,

...
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

neighbours(idx).position = A.position + stepVector;

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

neighbours(idx).cost = objectiveFunction( ...


neighbours(idx).position, testFunctionNo);
end
end
Función objetivo, prueba las variables.

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

testFunctionNo = 2; % Escoger la función a testear


initial = [0.8, -0.5];
cost_initial = objectiveFunction(initial ,
testFunctionNo);

ub = [1 1];
lb = [-1 -1];

stepSize = [0.05 , 0.05];

nVar = length(initial);

optimumFound = logical(false);

A.position = initial;
A.cost = cost_initial;

iter = 0;

while optimumFound == false

neighbours = getNeighbours(A, stepSize, ub, lb,


testFunctionNo);

iter = iter + 1;
trajectory(iter).position = A.position;
trajectory(iter).cost = A.cost;

improvement = logical(false);
for k = 1 : length(neighbours)
B = neighbours(k);

if B.cost > A.cost % uphil movement


A.position = B.position;
A.cost = B.cost;
improvement = true;
%A = B;
end

end

if improvement == false
optimumFound = true;
end

end

disp('best solution obtained by HC: ')


A

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);

[x_new, y_new] = meshgrid(x,y);

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

surfc(x_new, y_new, z_new)


colormap jet
shading flat
zlabel('Objective value');
hold on

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

plot(traj_fianl_x, traj_fianl_y, '-.k' , 'lineWidth', 1)


plot(traj_fianl_x(1), traj_fianl_y(1), '*r');
plot(traj_fianl_x(end), traj_fianl_y(end), '*g');

subplot(2,2 , [3, 4])


plot([trajectory.cost])
xlabel('Iteration');
ylabel('Cost')

You might also like