Professional Documents
Culture Documents
HW1 - G9 - Report - English
HW1 - G9 - Report - English
Group 9:
Nguyen Chinh Thuy 1513372
Nguyen Tan Phu 1512489
Le Van Hoang Phuong 1512579
Do Tieu Thien 1513172
Nguyen Tan Sy 1512872
Nguyen Van Qui 1512702
1 Problem
Given a single-variable function
subject to x ∈ [0, 5], use the Genetic Algorithm to find the global maximum of the function y(x).
2 Configuration
To solve the problem with the Genetic Algorithm, we use a configuration that is shown in the table 1.
Parameter Value
Number of binrary bits 13
Number of workers 100
Number of generations 200
Probability of mating 0.95
Probability of mutation 0.01
Range of x [0, 5]
Number of discrete points of x 200
1
3 Implementation
3.1 Python code
1 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
2 # Libraries
3 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
4 im po rt numpy a s np
5 im po rt m a t p l o t l i b . p y p l o t a s p l t
6 im po rt tqdm
7 from IPython imp ort d i s p l a y
8 im po rt time
9
10 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
11 # C l a s s o f G e n e t i c Algorithm
12 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
13 c l a s s GeneticAlgorithm ( o b j e c t ) :
14 ”””
15 [ Arguments ]
16 f u n c : F u n c t i o n t o f i n d t h e maximum v a l u e s
17
18 n b i t s : Number o f b i n r a r y b i t s
19
20 n w o r k e r s : Number o f w o r k e r s
21
22 n g e n : Number o f g e n e r a t i o n s
23
24 m a t i n g p r o b : P r o b a b i l i t y o f mating
25
26 m u t a t i o n p r o b : P r o b a b i l i t y o f mutation
27
28 x r a n g e : Range o f x
29
30 x n p o i n t s : Number o f d i s c r e t e p o i n t s o f x
31 ”””
32 def init ( s e l f , func , n b i t s , n w o r k e r s , n gen ,
33 mating prob , m u t a t i o n p r o b , x r a n g e , x n p o i n t s ) :
34 super ( GeneticAlgorithm , s e l f ) . init ()
35 # P a ra m e t e r s o f t h e G e n e t i c Algorithm
36 s e l f . func = func
37 self . n bits = n bits
38 s e l f . n workers = n workers
39 s e l f . n gen = n gen
40 s e l f . mating prob = mating prob
41 s e l f . mutation prob = mutation prob
42 s e l f . x range = x range
43 s e l f . x n points = x n points
44
45 # Inialize variables
46 s e l f . w o r k e r s = np . random . r a n d i n t ( 0 , 2 , s i z e =( s e l f . n w o r k e r s , self . n bits ) )
47 s e l f . b e s t w o r k e r = None
48
49 # Compute t h e o b j e c t i v e
50 d e f c o m p u t e o b j e c t i v e ( s e l f , pred , e s p=1e −8) :
51 r e t u r n pred + e s p − np . min ( pred )
52
53 # Convert b i n a r y t o d e c i m a l
54 d e f d ec od e ( s e l f , w o r k e r s ) :
55 delta = ( s e l f . x range [ 1 ] − s e l f . x range [ 0 ] )
56 norm range = w o r k e r s . dot ( 2 ∗∗ np . a r a n g e ( s e l f . n b i t s ) [ : : − 1 ] ) / ( 2 ∗ ∗ s e l f . n b i t s −1)
57 r e t u r n norm range ∗ d e l t a + s e l f . x r a n g e [ 0 ]
58
59 # Natural s e l e c t i o n
60 def s e l e c t ( s e l f , objective ) :
61 i d x = np . random . c h o i c e ( np . a r a n g e ( s e l f . n w o r k e r s ) , s i z e= s e l f . n w o r k e r s , p=o b j e c t i v e /
o b j e c t i v e . sum ( ) )
62 return s e l f . workers [ idx ]
63
64 # Mating
65 d e f mate ( s e l f , p a r e n t , w o r k e r s ) :
66 i f np . random . rand ( ) < s e l f . m a t i n g p r o b :
67 i d x = np . random . r a n d i n t ( 0 , s e l f . n w o r k e r s , s i z e =1)
68 c r o s s p o i n t s = np . random . r a n d i n t ( 0 , 2 , s i z e= s e l f . n b i t s ) . a s t y p e ( np . b o o l )
69 p a r e n t [ c r o s s p o i n t s ] = w o r k e r s [ idx , c r o s s p o i n t s ]
70 return parent
71
2
72 # Mutation
73 d e f mutate ( s e l f , c h i l d ) :
74 f o r i in range ( s e l f . n b i t s ) :
75 i f np . random . rand ( ) < s e l f . m u t a t i o n p r o b :
76 c h i l d [ i ] = 1 i f c h i l d [ i ]==0 e l s e 0
77 return child
78
79 # Perform a s t e p o f e v o l u t i o n
80 def step ( s e l f ) :
81 r e a l v a l s = s e l f . d ec od e ( s e l f . w o r k e r s )
82 o u t v a l s = func ( r e a l v a l s )
83 objective = s e l f . compute objective ( out vals )
84 s e l f . b e s t w o r k e r = s e l f . w o r k e r s [ np . argmax ( o b j e c t i v e ) , : ]
85 s e l f . b e s t o b j e c t i v e = s e l f . f u n c ( s e l f . de co de ( s e l f . b e s t w o r k e r ) )
86
87 s e l f . workers = s e l f . s e l e c t ( o b j e c t i v e )
88 w o r k e r s c o p y = s e l f . w o r k e r s . copy ( )
89 f o r parent in s e l f . workers :
90 c h i l d = s e l f . mate ( p a r e n t , w o r k e r s c o p y )
91 c h i l d = s e l f . mutate ( c h i l d )
92 parent [ : ] = c h i l d
93
94 return real vals , out vals
95
96 # Perform t h e whole e v o l u t i o n
97 def evolve ( s e l f ) :
98 f i g = p l t . f i g u r e ( f i g s i z e =(25 , 8 ) )
99 ax1 = f i g . a d d s u b p l o t ( 1 , 2 , 1 ) ; ax1 . s e t t i t l e ( ” Workers e v o l u t i o n ” )
100 x = np . l i n s p a c e ( ∗ s e l f . x r a n g e , s e l f . x n p o i n t s )
101 ax1 . p l o t ( x , f u n c ( x ) )
102 ax2 = f i g . a d d s u b p l o t ( 1 , 2 , 2 ) ; ax2 . s e t t i t l e ( ” O b j e c t i v e e v o l u t i o n ” )
103
104 best objectives = [ ]
105 f o r i i n tqdm . tqdm ( r a n g e ( s e l f . n g e n ) ) :
106 # Perform a s t e p o f e v o l u t i o n
107 real vals , out vals = s e l f . step ()
108
109 # Plot the workers e v o l u t i o n
110 i f ’ sca ’ in l o c a l s () :
111 s c a . remove ( )
112 s c a = ax1 . s c a t t e r ( r e a l v a l s , o u t v a l s , s= s e l f . x n p o i n t s , lw =0 , c= ’ r e d ’ , a l p h a
=0.5)
113
114 # Plot the o b j e c t i v e e v o l u t i o n
115 b e s t o b j e c t i v e s . append ( s e l f . b e s t o b j e c t i v e )
116 ax2 . p l o t ( b e s t o b j e c t i v e s , ”b” )
117
118 d i s p l a y . c l e a r o u t p u t ( w a i t=True )
119 display . display ( plt . gcf () )
120 time . s l e e p ( 0 . 0 1 )
121
122
123 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
124 # Main e x e c u t i o n
125 #−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
126 # D e f i n e a f u n c t i o n , which we want t o f i n d i t s maximum
127 f u n c = lambda x : 4∗ x ∗∗4 − 5∗ x ∗∗3 + np . exp (−2∗x ) − np . s i n ( x ) − 3∗ np . c o s ( x )
128
129 # I n s t a n t i a t e a G e n e t i c Algorithm
130 GA = G e n e t i c A l g o r i t h m ( f u n c=func , n b i t s =13 , n w o r k e r s =100 , n g e n =200 ,
131 m a t i n g p r o b = 0 . 9 5 , m u t a t i o n p r o b = 0 . 0 1 , x r a n g e = [ 0 , 5 ] , x n p o i n t s =200)
132
133 # Evolution
134 GA. e v o l v e ( )
135 b e s t w o r k e r = GA. b e s t w o r k e r
136 x o p t = GA. d ec od e ( b e s t w o r k e r )
137 y opt = func ( x opt )
138 p r i n t ( ” \ nOptimal r e s u l t : ” )
139 p r i n t ( ” ( x , y )max : (%.4 f , %.4 f ) ” % ( x o p t , y o p t ) )
3
3.2 Result