Skip to content

Commit f1668b0

Browse files
MorvanZhouMorvan Zhou
authored and
Morvan Zhou
committed
update
1 parent 151f221 commit f1668b0

File tree

10 files changed

+51
-8
lines changed

10 files changed

+51
-8
lines changed

tutorial-contents/Evolution Strategy/(1+1)-ES.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""
2-
(1+1)-ES with 1/5th success rule
2+
(1+1)-ES with 1/5th success rule with visualization.
3+
4+
Visit my tutorial website for more: https://morvanzhou.github.io/tutorials/
35
"""
46
import numpy as np
57
import matplotlib.pyplot as plt

tutorial-contents/Evolution Strategy/Evolution Strategy Basic.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
The Evolution Strategy can be summarized as the following term:
33
{mu/rho +, lambda}-ES
44
5-
Here we use:
5+
Here we use following term to find a maximum point.
66
{n_pop/n_pop + n_kid}-ES
7+
8+
Visit my tutorial website for more: https://morvanzhou.github.io/tutorials/
79
"""
810
import numpy as np
911
import matplotlib.pyplot as plt

tutorial-contents/Evolution Strategy/Natural Evolution Strategy (NES).py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
"""
2+
The basic idea about Nature Evolution Strategy with visualation.
3+
4+
Visit my tutorial website for more: https://morvanzhou.github.io/tutorials/
5+
26
Dependencies:
37
Tensorflow >= r1.2
48
numpy
@@ -14,8 +18,9 @@
1418
N_GENERATION = 100 # training step
1519
LR = 0.02 # learning rate
1620

21+
1722
# fitness function
18-
get_fitness = lambda pred: -((pred[:, 0])**2 + pred[:, 1]**2)
23+
def get_fitness(pred): return -((pred[:, 0])**2 + pred[:, 1]**2)
1924

2025
# build multivariate distribution
2126
mean = tf.Variable(tf.random_normal([2, ], 13., 1.), dtype=tf.float32)

tutorial-contents/Genetic Algorithm/Find Path.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
Visualize Genetic Algorithm to find a path to the target.
3+
4+
Visit my tutorial website for more: https://morvanzhou.github.io/tutorials/
5+
"""
16
import matplotlib.pyplot as plt
27
import numpy as np
38

tutorial-contents/Genetic Algorithm/Genetic Algorithm Basic.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
Visualize Genetic Algorithm to find a maximum point in a function.
3+
4+
Visit my tutorial website for more: https://morvanzhou.github.io/tutorials/
5+
"""
16
import numpy as np
27
import matplotlib.pyplot as plt
38

@@ -27,7 +32,7 @@ def select(pop, fitness): # nature selection wrt pop's fitness
2732

2833

2934
def crossover(parent, pop_copy): # mating process (genes crossover)
30-
if np.random.uniform(0, 1) < CROSS_RATE:
35+
if np.random.rand() < CROSS_RATE:
3136
i_ = np.random.choice(np.arange(POP_SIZE), size=1, replace=False) # select another individual from pop
3237
cross_points = np.random.randint(0, 2, DNA_SIZE).astype(np.bool) # choose a crossover points
3338
parent[cross_points] = pop[i_, cross_points] # mating and produce one child
@@ -36,7 +41,7 @@ def crossover(parent, pop_copy): # mating process (genes crossover)
3641

3742
def mutate(child):
3843
for point in range(DNA_SIZE):
39-
if np.random.uniform(0, 1) < MUTATION_RATE:
44+
if np.random.rand() < MUTATION_RATE:
4045
child[point] = 1 if child[point] == 0 else 0
4146
return child
4247

tutorial-contents/Genetic Algorithm/Match Phrase.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
Visualize Genetic Algorithm to match the target phrase.
3+
4+
Visit my tutorial website for more: https://morvanzhou.github.io/tutorials/
5+
"""
16
import numpy as np
27

38
TARGET_PHRASE = 'You get it!' # target DNA

tutorial-contents/Genetic Algorithm/Microbial Genetic Algorithm.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
Visualize Microbial Genetic Algorithm to find the maximum point in a graph.
3+
4+
Visit my tutorial website for more: https://morvanzhou.github.io/tutorials/
5+
"""
16
import numpy as np
27
import matplotlib.pyplot as plt
38

tutorial-contents/Genetic Algorithm/Travel Sales Person.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
"""
2+
Visualize Genetic Algorithm to find the shortest path for travel sales problem.
3+
4+
Visit my tutorial website for more: https://morvanzhou.github.io/tutorials/
5+
"""
16
import matplotlib.pyplot as plt
27
import numpy as np
38

@@ -38,7 +43,7 @@ def select(self, fitness):
3843
return self.pop[idx]
3944

4045
def crossover(self, parent, pop):
41-
if np.random.uniform(0, 1) < self.cross_rate:
46+
if np.random.rand() < self.cross_rate:
4247
i_ = np.random.choice(np.arange(self.pop_size), size=1, replace=False) # select another individual from pop
4348
cross_points = np.random.randint(0, 2, self.DNA_size).astype(np.bool) # choose crossover points
4449
keep_city = parent[~cross_points] # find the city number
@@ -48,7 +53,7 @@ def crossover(self, parent, pop):
4853

4954
def mutate(self, child):
5055
for point in range(self.DNA_size):
51-
if np.random.uniform(0, 1) < self.mutate_rate:
56+
if np.random.rand() < self.mutate_rate:
5257
swap_point = np.random.randint(0, self.DNA_size)
5358
swapA, swapB = child[point], child[swap_point]
5459
child[point], child[swap_point] = swapB, swapA

tutorial-contents/Using Neural Nets/NEAT/run_xor.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
"""
2-
2-input XOR example -- this is most likely the simplest possible example.
2+
Using NEAT for supervised learning. This example comes from http://neat-python.readthedocs.io/en/latest/xor_example.html
3+
4+
The detail for NEAT can be find in : http://nn.cs.utexas.edu/downloads/papers/stanley.cec02.pdf
5+
Visit my tutorial website for more: https://morvanzhou.github.io/tutorials/
36
"""
47

58
import os

tutorial-contents/Using Neural Nets/NEAT_gym/run_cartpole.py

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
"""
2+
Using NEAT for reinforcement learning.
3+
4+
The detail for NEAT can be find in : http://nn.cs.utexas.edu/downloads/papers/stanley.cec02.pdf
5+
Visit my tutorial website for more: https://morvanzhou.github.io/tutorials/
6+
"""
17
import neat
28
import numpy as np
39
import gym

0 commit comments

Comments
 (0)