%|------------------------------------------|
%| Eisagwgi stin Texniti Noimosini (MPD306) |
%|      xeimerino eksamhno 2020-2021        |
%|------------------------------------------|
%|                 LAB 04                   |
%|  Genetikoi Algorithmoi - Paradeigma 1    |
%|                                          |
%|              find minimum                |
%|------------------------------------------|

clear all; close all; clc;

%% display function

[X,Y]=meshgrid(-3:0.1:3,-3:0.1:3);

f=[];
f=3*(1-X).^2.*exp(-(X.^2) - (Y+1).^2)...
    - 10*(X./5 - X.^3 - Y.^5).*exp(-X.^2-Y.^2)...
    - 1/3*exp(-(X+1).^2 - Y.^2);

figure;
fig = surf(X,Y,f);

%% run optimtools with Genetic Algorithms Solver & these parameters:
%
% Fitness Function    : 3*(1-x)^2*exp(-(x^2)-(y+1)^2)-10*(x/5-x^3-y^5)*exp(-x^2-y^2)-1/3*exp(-(x+1)^2-y^2)
% Integer variables   : no
% Bounds              : -3 <= x,y <= 3
% Population Size     : 100
% Creation of initial
%          population : random 
% Selection technique : Roulette selection technique
% Crossover fraction  : 85%
% Plot                : best/mean fitness in each generation
%
%% after running optimtools and exporting results in 'optimresults' variable

[X,Y]=meshgrid(-3:0.1:3,-3:0.1:3);

f=[];
f=3*(1-X).^2.*exp(-(X.^2) - (Y+1).^2)...
    - 10*(X./5 - X.^3 - Y.^5).*exp(-X.^2-Y.^2)...
    - 1/3*exp(-(X+1).^2 - Y.^2);

figure;
fig = surf(X,Y,f);
hold on;
h = scatter3(optimresults.x(1),optimresults.x(2),optimresults.fval,'filled');
h.MarkerFaceColor='r';
hold off;
