Docsity

Comparative Analysis of Numerical Optimization Algorithms, Cheat Sheet of Computer science

Toggle sidebar
bg1
HW2
Yun-Chen, Tsai (R13222026)
November 2024
1 Problem 1
1.1 (a)
Take linear congruential generator(LCG) as an example: Let X be a sequence of pseudo-random numbers.
with X0as a seed. Then the generator is defined as a recurrence relation:
Xn+1 = (aยทXn+b) mod m
Set a= 1664525, b = 1013904223, m = 232 , seed = 20 and generate 10000 pseudo-random numbers and
plot the results using histogram:
Figure 1: 10000 pseudo-random numbers generated by linear congruential generator and numpy
As seen from above, it has great uniformity. The code is implemented in random number.py.
1.2 (b) Random sampling (hw2 1b.py)
Here I use np.random.uniform(low=0, high=1, size=3) to generate 3 uniform random numbers between
0 and 1. Initially the position is at (0.5, 0.5, 0.5) and the objective function value is -0.628022. These 2
values are set as currently best (xb, fb). At each iterations, 3 uniform random numbers are generated to
be the position within the unit hypercube. Then the correponding objective function value is calculated.
If the new objective function value is less than fb, then (xb, fb) will be updated. The random sampling
algorithm is implemented in hw2 1b.py.
Iteration Point (xb)Function Value (fb)
1๎˜‚0.500000,0.500000,0.500000๎˜ƒโˆ’0.628022
4๎˜‚0.088340,0.685360,0.953393๎˜ƒโˆ’2.538125
4๎˜‚0.003948,0.512192,0.812621๎˜ƒโˆ’3.655112
32 ๎˜‚0.519797,0.578136,0.853934๎˜ƒโˆ’3.749838
446 ๎˜‚0.164135,0.566596,0.860687๎˜ƒโˆ’3.850967
10000 ๎˜‚0.164135,0.566596,0.860687๎˜ƒโˆ’3.850967
1
pf3
pf4
pf5

Partial preview of the text

Download Comparative Analysis of Numerical Optimization Algorithms and more Cheat Sheet Computer science in PDF only on Docsity!

HW

Yun-Chen, Tsai (R13222026)

November 2024

1 Problem 1

1.1 (a)

Take linear congruential generator(LCG) as an example: Let X be a sequence of pseudo-random numbers. with X 0 as a seed. Then the generator is defined as a recurrence relation:

Xn+1 = (a ยท Xn + b) mod m

Set a = 1664525, b = 1013904223, m = 2^32 , seed = 20 and generate 10000 pseudo-random numbers and plot the results using histogram:

Figure 1: 10000 pseudo-random numbers generated by linear congruential generator and numpy

As seen from above, it has great uniformity. The code is implemented in random number.py.

1.2 (b) Random sampling (hw2 1b.py)

Here I use np.random.uniform(low=0, high=1, size=3) to generate 3 uniform random numbers between 0 and 1. Initially the position is at (0.5, 0.5, 0.5) and the objective function value is -0.628022. These 2 values are set as currently best (xb, fb). At each iterations, 3 uniform random numbers are generated to be the position within the unit hypercube. Then the correponding objective function value is calculated. If the new objective function value is less than fb, then (xb, fb) will be updated. The random sampling algorithm is implemented in hw2 1b.py.

Iteration Point (xb) Function Value (fb) 1

Scipy Optimization Result: [0.114589, 0.555649, 0.852547], with function value: -3.862780.

1.3 (c) Mutation function

Here I define same mutation function as taught in the lecture. First generate 3 random numbers between -1 and 1 to be the random vectoruโƒ— , then normalized it ห†u =u/โƒ— u|โƒ— |. The new point is calculated as follows: โˆ’xโˆ’โ†’ new =^ โˆ’xโˆ’โ†’ old +^ step^ ยท^ uห†.^ If one of the coordinate of the new point exceed either lower or upper bound, the correponding coordinate of the new point is set to the bound value. For example, if the new point is (1.2, 0.8, 0.7), x coordinate exceeds 1.0, so the new point will be (1.0, 0.8, 0.7). This critirion is the same for hill climbing and random walk. And the mutation function is called โ€updateโ€ in hw2 1d.py and hw2 1e.py.

1.4 (d) Hill climbing (hw2 1d.py)

Step Iteration Point (xb) Function Value (fb)

  1. 00000 e โˆ’ 01 1
  1. 00000 e โˆ’ 01 4
  1. 00000 e โˆ’ 01 4
  1. 00000 e โˆ’ 01 5
  1. 00000 e โˆ’ 01 5
  1. 00000 e โˆ’ 01 5
  1. 00000 e โˆ’ 01 5
  1. 00000 e โˆ’ 01 5
  1. 00000 e โˆ’ 01 5
  1. 00000 e โˆ’ 01 22
  1. 00000 e โˆ’ 01 31
  1. 00000 e โˆ’ 01 34
  1. 00000 e โˆ’ 01 1000
  1. 00000 e โˆ’ 02 1000
  1. 50000 e โˆ’ 02 67
  1. 50000 e โˆ’ 02 104
  1. 50000 e โˆ’ 02 180
  1. 50000 e โˆ’ 02 437
  1. 50000 e โˆ’ 02 1000
  1. 25000 e โˆ’ 02 32
  1. 25000 e โˆ’ 02 34
  1. 25000 e โˆ’ 02 1000
  1. 25000 e โˆ’ 03 367
  1. 25000 e โˆ’ 03 411
  1. 25000 e โˆ’ 03 655
  1. 25000 e โˆ’ 03 1000
  1. 12500 e โˆ’ 03 45
  1. 12500 e โˆ’ 03 223
  1. 12500 e โˆ’ 03 283
  1. 12500 e โˆ’ 03 1000
  1. 56250 e โˆ’ 03 379
  1. 56250 e โˆ’ 03 1000
  1. 81250 e โˆ’ 04 1000
  1. 90625 e โˆ’ 04 1000
  1. 95313 e โˆ’ 04 1000
  1. 76563 e โˆ’ 05 1000
  1. 88281 e โˆ’ 05 1000
  1. 44141 e โˆ’ 05 1000
  1. 22070 e โˆ’ 05 1000
  1. 10352 e โˆ’ 06 1000
  1. 05176 e โˆ’ 06 1000
  1. 52588 e โˆ’ 06 1000

2.2 (b) Cauchy method (hw2 2b.py)

New direction in Cauchy method is simply: S 1 = โˆ’โˆ‡(f (X))|X 0 = [โˆ’ 1. 046557 , 2. 742792 , 3 .643449]. Next use direction exploitation to find optimal step along this direction. Given a maximal step ฮป 1 = 0.1, slice it into 100 equally spaced points and perform a complete search. The result is ฮปโˆ— 1 = 0.080808, so the new point is X 1 = X 0 + ฮปโˆ— 1 S 1 = [0. 4154300. 7216400 .794420], with correponding function value: f (X 1 ) = โˆ’ 2 .750658.

2.3 (c) Fletcher-Reeves (hw2 2d.py)

Start with X 1 calculated from 2(b), first calculate โˆ’โˆ‡f (X)|X 1 = [โˆ’ 0. 730195 , โˆ’ 7. 537728 , 5 .682158]. So

the ratio: r = |โˆ‡f^ (X)|X^1 |

2 |โˆ‡f (X)|X 0 |^2 = 4.094360. New direction: S 2 = โˆ’โˆ‡f (X)|X 1 +rยท(โˆ’โˆ‡f (X)|X 0 ) = [โˆ’ 5. 015177 , 3. 692249 , 20 .599747]. Set the maximal step to be ฮป 1 = 0.1 and use the same direction exploitation technique to find ฮปโˆ— 2 = 0.002020. So the new point is: X 2 = X 1 + ฮปโˆ— 2 S 2 = [0. 405298 , 0. 729099 , 0 .836036], with function value: f (X 2 ) : โˆ’ 2 .866772.

2.4 (d) Fletcher-Reeves continued (hw2 2d.py)

Step (ฮปโˆ—) Iteration Point (xb) Function Value (fb)

  1. 02020 e โˆ’ 03 2
  1. 01010 e โˆ’ 02 3
  1. 08081 e โˆ’ 03 4
  1. 05051 e โˆ’ 03 5
  1. 71717 e โˆ’ 02 6
  1. 87879 e โˆ’ 02 7
  1. 74747 e โˆ’ 02 8
  1. 05051 e โˆ’ 03 9
  1. 41414 e โˆ’ 02 10
  1. 11111 e โˆ’ 02 11
  1. 28283 e โˆ’ 02 12
  1. 31313 e โˆ’ 02 13
  1. 09091 e โˆ’ 03 14
  1. 21212 e โˆ’ 02 15
  1. 53535 e โˆ’ 02 16
  1. 83838 e โˆ’ 02 17
  1. 07071 e โˆ’ 03 18
  1. 11111 e โˆ’ 02 19
  1. 00000 e โˆ’ 01 20
  1. 21212 e โˆ’ 02 21
  1. 07071 e โˆ’ 03 22
  1. 91919 e โˆ’ 02 23
  1. 33333 e โˆ’ 02 24
  1. 22222 e โˆ’ 02 25
  1. 07071 e โˆ’ 03 26

3 Problem 3 (hw2 3.py)

3.1 (a) Initial temperature

10 random points are the columns of the following matrix: ๏ฃฎ

๏ฃฐ

The initial temperature is the mean function values of these 10 points: -1.319623.

3.2 (b) First iteration of SA

New point: pnew.x = [0. 536213 , 0. 479619 , 0 .457870] with new function value: pnew.y = โˆ’ 0 .429740. dE = pcur.y โˆ’ pnew.y = โˆ’ 0. 628022 โˆ’ (โˆ’ 0 .429740) = โˆ’ 0. 198282 < 0 (Note that I changed the definition of dE since the initial temperature is negative) Although dE < 0, thereโ€™s a chance to accept it using the following procedure: Generate a uniform random number between 0 and 1: 0.805223. It is less then expโˆ’dE/T^ = 0.860487, so this point is accepted as pcur.

3.3 (c) Second iteration of SA

Next point: pnew.x = [0. 540542 , 0. 561349 , 0 .421717] with new function value: pnew.y = โˆ’ 0 .471208. dE = pcur.y โˆ’ pnew.y = โˆ’ 0. 429740 โˆ’ (โˆ’ 0 .471208) = 0. 041468 > 0. Since pnew is better then pcur, it is accepted as pcur.

3.4 (d) Remaining iterations of SA

Iteration Point (xb) Function Value (fb) 3

It takes 196843 iterations for the function value to converge. For the sake of curiosity, I tried another value of search range = ยฑ 0 .01. It shows better convergence behavior:

Using 0-based indexing, 0th, 1st, 3rd, 4th, 5th, 6th, 7th and 8th particles have lower function values. So their local best positions should be updated acoordingly.

โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โ†’ Plocalbest =

  1. 415046 0. 056009 0. 633648 0. 404275 0. 284544 0. 153614 0. 140826 0. 409884 0. 126977 0. 088340
  2. 700600 0. 828815 0. 003948 0. 617765 0. 761476 0. 665760 0. 718010 0. 512376 0. 811776 0. 714576
  3. 573058 0. 381538 0. 373341 0. 636004 0. 524882 0. 521142 0. 609041 0. 558991 0. 624650 0. 601039

Also, 8th particle has lowest function value now, so global best position is updated as:

โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โ†’ Pglobalbest =

  1. 126977
  2. 811776
  3. 624650

With global best function value: -2.843445.

4.3 (c) Next iteration of PSO (hw2 4abc.py)

Using similar procedure, new random numbers, velocities and positions are:

r 1 , r 2 = 0. 908649 , 0. 319236

V 2 =

hโˆ’ 0. 400000 0. 057913 โˆ’ 0. 096924 โˆ’ 0. 400000 โˆ’ 0. 264264 โˆ’ 0. 079686 โˆ’ 0. 061658 โˆ’ 0. 400000 โˆ’ 0. 042134 0. 012334

  1. 050732 โˆ’ 0. 130018 0. 173412 0. 167508 โˆ’ 0. 035087 0. 099848 0. 026188 0. 316079 โˆ’ 0. 105998 0. 031030
  2. 046983 0. 316978 0. 053159 โˆ’ 0. 041754 0. 114899 0. 120171 โˆ’ 0. 003743 0. 066814 โˆ’ 0. 025748 0. 007537

i

X 2 =

h 0. 015046 0. 113921 0. 252266 0. 004275 0. 020281 0. 073928 0. 079168 0. 009884 0. 084843 0. 100674

  1. 751332 0. 698797 0. 548057 0. 785273 0. 726389 0. 765608 0. 744198 0. 828456 0. 705778 0. 745606
  2. 620041 0. 698515 0. 545278 0. 594250 0. 639781 0. 641314 0. 605298 0. 625805 0. 598902 0. 608576

i

Their function values are:

[โˆ’ 2. 676097 โˆ’ 2. 579238 โˆ’ 1. 306032 โˆ’ 2. 831252 โˆ’ 2. 566089 โˆ’ 2. 688510 โˆ’ 2. 738507 โˆ’ 2. 773287 โˆ’ 2. 563926 โˆ’ 2. 744254 ]

โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โ†’ Plocalbest =

  1. 015046 0. 113921 0. 252266 0. 004275 0. 020281 0. 073928 0. 079168 0. 009884 0. 126977 0. 100674
  2. 751332 0. 698797 0. 548057 0. 785273 0. 726389 0. 765608 0. 744198 0. 828456 0. 811776 0. 745606
  3. 620041 0. 698515 0. 545278 0. 594250 0. 639781 0. 641314 0. 605298 0. 625805 0. 624650 0. 608576

โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โ†’ Pglobalbest =

  1. 126977
  2. 811776
  3. 624650

Global best function value is: โˆ’ 2 .843445.

4.4 (d) Remaining iterations of PSO (hw2 4d.py)

Next set the maximum iterations = 200000. Convergence critirion: If the variance of particle positions is less than 1e โˆ’ 6 for 3 coordinates, then break the while loop. Result: iterations break at 3609.

โˆ’โˆ’โˆ’โˆ’โˆ’โˆ’โ†’ Pglobalbest =

  1. 103705
  2. 553259
  3. 851089

With function value: โˆ’ 3 .862313.