- Details
- Parent Category: Programming Assignments' Solutions
We Helped With This MATLAB Programming Homework: Have A Similar One?

Category | Programming |
---|---|
Subject | MATLAB |
Difficulty | Undergraduate |
Status | Solved |
More Info | Pay Someone To Do My Matlab Homework |
Assignment Description
Assignment 4
This assignment 4 is divided into 3 parts:
- Part 1: GA and DE (Maximum of 3 points) o Pre-requisites: None.
- Part 2: PSO (Maximum of 2 point) o Pre-requisites: Minimum of 2 points in Part 1.
Part 1: Basic algorithms A
The objective of this assignment is to study the performance of evolutionary algorithms with real codification. The algorithms will be applied to 10 functions with dimension 30. These functions were proposed in the competition organized by the especial session “Real parameter optimization” in the 2005 IEEE Congress on Evolutionary Computation (CEC2005).
Your assignment is to implement two different algorithms:
1) GA algorithm (Lecture 3) (0.75 points)
2) DE algorithm (Lecture 3) (0.75 points)
Your report for part 1 has to cover the key parts as follow:
1a. Explain the implemented GA algorithm and give the parameter settings. 1b. Give the results of the GA algorithm. (0.75, Mandatory to have part a and b) 2a. Explain the implemented DE algorithm and give the parameter settings.
2b. Give the results of the DE algorithm. (0.75, Mandatory to have part a and b)
How to present the results:
1. - Run the algorithm to each function 20 times and calculate the mean value of the 20 runs. One algorithms has to have the same setting for all the functions and runs. For example: If you use a mutation factor F = 0.5, you have to use this value to all the functions.
2. - Present the following table:
| F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | F10 |
Algorithm name | X.XXE+XX | X.XXE-XX | 1.23E+02 |
|
|
|
|
|
|
|
Each value inside the table is the mean value of the 20 runs for each algorithm on each function.
Every run is independently of the others.
The mean values has to be presented in scientific formulation.
Examples: 567.8 = 5.68E+02, 0.023 = 2.30E-02, 5.345 = 5.35E+00
In order to do the assignment, you have to use the matlab code given by the teacher and explained in the lectures. If you have doubts of how to use it, you can ask the teacher.
Part 2: Basic Algorithms B
Your assignment is to implement two different algorithms:
1) PSO algorithm (Lecture 4) (1 points)
Your report for part 2 has to cover the key parts as follow:
1a. Explain the implemented PSO algorithm and give the parameter settings.
1b. Give the results of the PSO algorithm. (1, Mandatory to have part a and b)
How to present the results:
1. - Run the algorithm to each function 20 times and calculate the mean value of the 20 runs. One algorithms has to have the same setting for all the functions and runs. For example: If you use a mutation factor F = 0.5, you have to use this value to all the functions.
2. - Present the following table:
| F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | F10 |
Algorithm name | X.XXE+XX | X.XXE-XX | 1.23E+02 |
|
|
|
|
|
|
|
Each value inside the table is the mean value of the 20 runs for each algorithm on each function.
Every run is independently of the others.
The mean values has to be presented in scientific formulation.
Examples: 567.8 = 5.68E+02, 0.023 = 2.30E-02, 5.345 = 5.35E+00
In order to do the assignment, you have to use the matlab code given by the teacher and explained in the lectures. If you have doubts of how to use it, you can ask the teacher. A second option is to implement your own version of the code in a different language and ask for help/permission to the teacher.
Conditions to approve the assignment 4
In order to receive the points for the code, you need to submit the report and have more than half of the points. Example: If a report have 1 point, then you need to have a minimum of 0.5.
- Score to pass part 1: 2 points.
- Score to pass part 2: 1 point.
- Minimum score to pass assignment 4: 3 points.
SCORES
- Your score < 3: U
- 3 <= your score <= 3.75: 3 - 4 <= your score < 4.75: 4
- 4.75 <= your score <= 5: 5
SUBMISSION DETAILS
Maximum Score: 5 points.
Time to submit: 21 January – 24 March & 1 April – 7 April & 21 April – 28 April
I will not consider a submission without the following format:
File names for the submission:
StudentName_StudentLastName_Assignment4_Part1_Code.zip
StudentName_StudentLastName_Assignment4_Part1_Report.pdf StudentName_StudentLastName_Assignment4_Part2_Code.zip
StudentName_StudentLastName_Assignment4_Part2_Report.pfd
Email subject for the submission:
[DVA340] StudentName StudentLastName Assignment4 Part1 Code
[DVA340] StudentName StudentLastName Assignment4 Part1 Report
ADDITIONAL INFORMATION!
You are allowed to send each part of the assignment a maximum of three times. When you approve the assignment, you are not allowed to submit the assignment again.
Before defending the code to Miguel Leon, you have to send the code by email to ....
Before submitting the report, you should present this assignment first to Miguel Leon. After that, and only if everything is correct, you are able to send the report to ....
You cannot get the points of the report without presenting the code first.
Extra information
The functions description is given in the file: “assignment 5 function description.pdf”.
Use the code given in: “assignment 5 matlab code.zip”. Only use “test.m”
To compare the results, you can use the summation of the relative error. Use the file: “assignment 5 comparison.xlsx”
Assignment Code
%
%
%
%
%
function populationFitness = calculateFitnessPopulation_2005(func, population, o, A, M, a, alpha, b)
populationSize = size(population,1);
dimension = size(population,2);
populationFitness = zeros(1,populationSize);
i = 1;
while i <= populationSize
if func == 1
populationFitness(i) = f1_2005(population(i,:), o, A, M, a, alpha, b);
end
if func == 2
populationFitness(i) = f2_2005(population(i,:), o, A, M, a, alpha, b);
end
if func == 3
populationFitness(i) = f3_2005(population(i,:), o, A, M, a, alpha, b);
end
if func == 4
populationFitness(i) = f4_2005(population(i,:), o, A, M, a, alpha, b);
end
if func == 5
populationFitness(i) = f5_2005(population(i,:), o, A, M, a, alpha, b);
end
if func == 6
populationFitness(i) = f6_2005(population(i,:), o, A, M, a, alpha, b);
end
if func == 7
populationFitness(i) = f7_2005(population(i,:), o, A, M, a, alpha, b);
end
if func == 8
populationFitness(i) = f8_2005(population(i,:), o, A, M, a, alpha, b);
end
if func == 9
populationFitness(i) = f9_2005(population(i,:), o, A, M, a, alpha, b);
end
if func == 10
populationFitness(i) = f10_2005(population(i,:), o, A, M, a, alpha, b);
end
if func == 11
populationFitness(i) = f11_2005(population(i,:), o, A, M, a, alpha, b);
end
if func == 12
populationFitness(i) = f12_2005(population(i,:), o, A, M, a, alpha, b);
end
if func == 13
populationFitness(i) = f13_2005(population(i,:), o, A, M, a, alpha, b);
end
i = i + 1;
end
end
Assignment Code
%
% DATABASE INFORMATION
%
% 1999 -> 1-13 functions
%
function res = calculateFitness_2005(func, x, o, A, M, a, alpha, b)
dimension = size(x,2);
if func == 1
res = f1_2005(x, o, A, M, a, alpha, b);
end
if func == 2
res = f2_2005(x, o, A, M, a, alpha, b);
end
if func == 3
res = f3_2005(x, o, A, M, a, alpha, b);
end
if func == 4
res = f4_2005(x, o, A, M, a, alpha, b);
end
if func == 5
res = f5_2005(x, o, A, M, a, alpha, b);
end
if func == 6
res = f6_2005(x, o, A, M, a, alpha, b);
end
if func == 7
res = f7_2005(x, o, A, M, a, alpha, b);
end
if func == 8
res = f8_2005(x, o, A, M, a, alpha, b);
end
if func == 9
res = f9_2005(x, o, A, M, a, alpha, b);
end
if func == 10
res = f10_2005(x, o, A, M, a, alpha, b);
end
if func == 11
res = f11_2005(x, o, A, M, a, alpha, b);
end
if func == 12
res = f12_2005(x, o, A, M, a, alpha, b);
end
if func == 13
res = f13_2005(x, o, A, M, a, alpha, b);
end
end
Assignment Code
%
% DATABASE INFORMATION
%
% 1999 -> 1-13 functions
%
% OUTPUTS
%
% correct = 1 -> The function is correct.
% correst = 0 -> The function is incorrect.
%
function [correct, upper, lower, optimum, o, A, M, a, alpha, b] = getInformation_2005(func, dimension)
correct = 1;
D = dimension;
if func == 1
lower = -100;
upper = 100;
optimum = 0;
load sphere_func_data
A = []; M = []; a = []; alpha = []; b = [];
end
if func == 2
lower = -100;
upper = 100;
optimum = 0;
load schwefel_102_data
A = []; M = []; a = []; alpha = []; b = [];
end
if func == 3
lower = -100;
upper = 100;
optimum = 0;
load high_cond_elliptic_rot_data
A = []; a = []; alpha = []; b = [];
if D == 2
load elliptic_M_D2;
elseif D == 10
load elliptic_M_D10;
elseif D == 30
load elliptic_M_D30;
elseif D == 50
load elliptic_M_D50;
end
end
if func == 4
lower = -100;
upper = 100;
optimum = 0;
load schwefel_102_data
A = []; M = []; a = []; alpha = []; b = [];
end
if func == 5
lower = -100;
upper = 100;
optimum = 0;
load schwefel_206_data
M = []; a = []; alpha = []; b = [];
end
if func == 6
lower = -100;
upper = 100;
optimum = 0;
load rosenbrock_func_data
A = []; M = []; a = []; alpha = []; b = [];
end
if func == 7
lower = -600;
upper = 600;
optimum = 0;
load griewank_func_data
A = []; a = []; alpha = []; b = [];
if D == 2, load griewank_M_D2,
elseif D == 10, load griewank_M_D10,
elseif D == 30, load griewank_M_D30,
elseif D == 50, load griewank_M_D50,
end
end
if func == 8
lower = -32;
upper = 32;
optimum = -12569.5*dimension/30;
load ackley_func_data
A = []; a = []; alpha = []; b = [];
if D == 2, load ackley_M_D2,
elseif D == 10, load ackley_M_D10,
elseif D == 30, load ackley_M_D30,
elseif D == 50, load ackley_M_D50,
end
end
if func == 9
lower = -5;
upper = 5;
optimum = 0;
load rastrigin_func_data
A = []; M = []; a = []; alpha = []; b = [];
end
if func == 10
lower = -5;
upper = 5;
optimum = 0;
load rastrigin_func_data
A = []; a = []; alpha = []; b = [];
if D == 2, load rastrigin_M_D2,
elseif D == 10, load rastrigin_M_D10,
elseif D == 30, load rastrigin_M_D30,
elseif D == 50, load rastrigin_M_D50,
end
end
if func == 11
lower = -0.5;
upper = 0.5;
optimum = 0;
load weierstrass_data
A = []; a = []; alpha = []; b = [];
if D == 2, load weierstrass_M_D2, ,
elseif D == 10, load weierstrass_M_D10,
elseif D == 30, load weierstrass_M_D30,
elseif D == 50, load weierstrass_M_D50,
end
end
if func == 12
lower = -pi;
upper = pi;
optimum = 0;
load schwefel_213_data
A = []; M = [];
o = [];
end
if func == 13
lower = -3;
upper = 1;
optimum = 0;
load EF8F2_func_data
A = []; M = []; a = []; alpha = []; b = [];
end
if func == 14
lower = -100;
upper = 100;
optimum = 0;
load E_ScafferF6_func_data
if D == 2, load E_ScafferF6_M_D2,
elseif D == 10, load E_ScafferF6_M_D10,
elseif D == 30, load E_ScafferF6_M_D30,
elseif D == 50, load E_ScafferF6_M_D50,
end
A = []; a = []; alpha = []; b = [];
end
if func > 13 || func < 1
correct = 0;
end
end
Assignment Code
%path = 'C:Usersmlz02DocumentsMATLABDE LIBRARY';
%addpath(genpath(path))
functions = [1 2 3 4 5 6 7 8 9 10]; %functions being solved
%example: functions = 1;
%example: functions = [2 4 9];
numF = size(functions,2);
nTimes = 20; % Number of times in which a function is going to be solved
dimension = 30; % Dimension of the problem
populationSize = 10; % Adjust this to your algorithm
for i = 1:numF
fitfun = functions(i); %fitfun is the function that we are solving
fprintf('
----- Function %d started -----
', fitfun);
for t = 1:nTimes
maxEval = 10000*dimension; % maximum number of evaluation
[value, upper,lower,objetiveValue, o, A, M, a, alpha, b] = getInformation_2005(fitfun, dimension);
currentEval = 0;
% Start generating the initial population
population = zeros(populationSize, dimension);
for j =1:populationSize
population(j,:) = lower + (upper-lower).*rand(1,dimension);
end
populationFitness = calculateFitnessPopulation_2005(fitfun, population, o, A, M, a, alpha, b); %Fitness values of all individuals (smaller value is better)
bestSolutionFitness = min(populationFitness);
currentEval = currentEval + populationSize;
% Algorithm loop
while(objetiveValue < bestSolutionFitness && currentEval < maxEval)
% Your algorithm goes here
populationFitness = calculateFitnessPopulation_2005(fitfun, population, o, A, M, a, alpha, b); %Fitness values of all individuals (smaller value is better)
bestSolutionFitness = min(populationFitness);
currentEval = currentEval + populationSize;
% Your algorithm goes here
end
% best individual
bestSolutionFitness = min(populationFitness);
fprintf('%dth run, The best individual fitness is %d
', t, bestSolutionFitness);
end
end
Assignment Code
%
%
%
%
%
function f = f10_2005(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -5 + 10 * rand(1, D);
end
x = x - repmat(o, ps, 1);
x = x * M;
f = sum(x .^ 2 - 10 .* cos(2 .* pi .* x) + 10, 2);
end
Assignment Code
%
%
%
%
%
function f = f11_2005(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -0.5 + 0.5 * rand(1, D);
end
x = x - repmat(o, ps, 1);
x = x * M;
x = x + 0.5;
a = 0.5;%0<a<1
b = 3;
kmax = 20;
[ps, D] = size(x);
c1(1 : kmax + 1) = a .^ (0 : kmax);
c2(1 : kmax + 1) = 2 * pi * b .^ (0 : kmax);
c = -w(0.5, c1, c2);
f = 0;
for i = 1 : D
f = f + w(x(:, i)', c1, c2);
end
f = f + repmat(c * D, ps, 1);
end
function y = w(x, c1, c2)
y = zeros(length(x), 1);
for k = 1 : length(x)
y(k) = sum(c1 .* cos(c2 .* x(:, k)));
end
end
Assignment Code
%
%
% No confiar en esta funcion
%
%
function f = f12_2005(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(alpha) >= D
alpha = alpha(1 : D);
b = b(1 : D, 1 : D);
a = a(1 : D, 1 : D);
else
alpha = -3 + 6 * rand(1, D);
a = round(-100 + 200 .* rand(D, D));
b = round(-100 + 200 .* rand(D, D));
end
alpha = repmat(alpha, D, 1);
A = sum(a .* sin(alpha) + b .* cos(alpha), 2);
for i = 1 : ps
xx = repmat(x(i, :), D, 1);
B = sum(a .* sin(xx) + b .* cos(xx), 2);
f(i, 1) = sum((A - B) .^ 2, 1);
end
end
Assignment Code
%
%
%
%
%
function fit = f13_2005(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -1 + 1 * rand(1, D);
end
x = x - repmat(o, ps, 1) + 1;
fit = 0;
for i = 1 : (D - 1)
fit = fit + F8F2(x(:, [i, i + 1]));
end
fit = fit + F8F2(x(:, [D, 1]));
end
function f = F8F2(x)
f2 = 100 .* (x(:, 1).^2 - x(:, 2)).^2 + (1 - x(:, 1)).^2;
f = 1 + f2 .^ 2 ./ 4000 - cos(f2);
end
Assignment Code
%
%
%
%
%
function fit = f1_2005(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -100 + 200 * rand(1, D);
end
x = x - repmat(o, ps, 1);
fit = sum(x .^ 2, 2);
end
Assignment Code
%
%
%
%
%
function f = f2_2005(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -100 + 200 * rand(1, D);
end
x = x - repmat(o, ps, 1);
f = 0;
for i = 1 : D
f = f + sum(x(:, 1 : i), 2).^2;
end
end
Assignment Code
%
%
%
%
%
function fit = f3_2005(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -100 + 200 * rand(1, D);
end
x = x - repmat(o, ps, 1);
x = x * M;
a = 1e+6;
fit = 0;
for i = 1 : D
fit = fit + a .^ ((i - 1) / (D - 1)) .* x(:, i).^2;
end
end
Assignment Code
%
%
%
%
%
function f = f4_2005(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -100 + 200 * rand(1, D);
end
x = x - repmat(o, ps, 1);
f = 0;
for i = 1 : D
f = f + sum(x(:, 1 : i), 2).^2;
end
f = f .* (1 + 0.4 .* abs(normrnd(0, 1, ps, 1)));
end
Assignment Code
%
%
%
% No confiar en esta funcion
%
function f = f5_2005(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
A = A(1 : D, 1 : D); o = o(1 : D);
else
o = -100 + 200 * rand(1, D);
A = round(-100 + 2 * 100 .* rand(D, D));
while det(A) == 0
A = round(-100 + 2 * 100 .* rand(D, D));
end
end
o(1 : ceil(D / 4)) = -100; o(max(floor(0.75 * D), 1) : D) = 100;
B = A * o';
for i = 1 : ps
f(i, 1) = max(abs(A * (x(i, :)') - B));
end
end
Assignment Code
%
%
%
%
%
function f = f6_2005(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -90 + 180 * rand(1, D);
end
x = x - repmat(o, ps, 1) + 1;
f = sum(100 .* (x(:, 1 : D - 1) .^ 2 - x(:, 2 : D)) .^ 2 + (x(:, 1 : D - 1) - 1) .^ 2, 2);
end
Assignment Code
%
%
%
%
%
function f = f7_2005(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -600 + 0 * rand(1, D);
end
o = o(1 : D);
x = x - repmat(o, ps, 1);
x = x * M;
f = 1;
for i = 1 : D
f = f .* cos(x(:, i) ./ sqrt(i));
end
f = sum(x .^ 2, 2) ./ 4000 - f + 1;
end
Assignment Code
%
%
%
%
%
function f = f8_2005(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -30 + 60 * rand(1, D);
end
o(2 .* [1 : floor(D / 2)] - 1) = -32;
x = x - repmat(o, ps, 1);
x = x * M;
f = sum(x .^ 2, 2);
f = 20 - 20.* exp(-0.2 .* sqrt(f ./ D)) - exp(sum(cos(2 .* pi .* x), 2) ./ D) + exp(1);
end
Assignment Code
%
%
%
%
%
function f = f9_2005(x, o, A, M, a, alpha, b)
[ps, D] = size(x);
if length(o) >= D
o = o(1 : D);
else
o = -5 + 10 * rand(1, D);
end
x = x - repmat(o, ps, 1);
f = sum(x .^ 2 - 10 .* cos(2 .* pi .* x) + 10, 2);
end