Challenge

Week 5 - Genetic Algorithm

1.clear all

2.close all

3.clc

4.x = linspace(0,10,20);

5.y = linspace(0,20,20);

6.iteration = 50;

7.[xx yy] = meshgrid(x,y);

8.for i=1:length(x)

  1. for j=1:length(y)

10 input(1) = xx(i,j);

11 input(2) = yy(i,j);

12 f(i,j) = stalagmite(input);

13 end

14 end

15 tic

16 for i=1:num_spaces

17 [results,fopt(i)]=ga(@stagalmite,2)

18 xopt(i)=results(1)

19 yopt(i)=results(2)

20 end

21 study_time1=toc

22 subplot(2,1,1)

23 surf(x,y,-f)

24 shading interp

25 hold on

26 plot3(xopt,yopt,-fopt,'marker','o')

27 subplot(2,1,2)

28 plot(-fopt)

29 xlabel('iteration')

30 ylabel('function maximum')

31 tic

32 for i=1:num_spaces

33 [results,fopt(i)]=ga(@stagalmite,2,[],[],[],[],[0;0],[0.6;0.6])

34 xopt(i)=results(1)

35 yopt(i)=results(2)

36 end

37 study_time2=toc

38 subplot(2,1,1)

39 surf(x,y,-f)

40 shading interp

41 hold on

42 plot3(xopt,yopt,-fopt,'marker','o')

43 subplot(2,1,2)

44 plot(-fopt)

45 xlabel('iteration')

46 ylabel('function maximum')

47 tic

48 options = optimoptions('ga')

49 options = optimoptions(options,'populationsize',250)

50 for k=1:iteration

51 [results,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)

52 xfin(i) = results(1)

53 yfin(i) = results(2)

54 end

55 time = toc

56 subplot(2,1,1)

57 surf(x,y,-f)

58 shading interp

59 hold on

60 plot3(xfin,yfin,-fopt,'Marker','+')

61 subplot(2,1,2)

62 plot(-fopt)

63 xlabel('iterations')

64 ylabel('Function values')

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.