Skip to content

Commit dd9fe83

Browse files
MorvanZhouMorvan Zhou
authored andcommitted
update
1 parent cc1ad65 commit dd9fe83

File tree

5 files changed

+8
-8
lines changed

5 files changed

+8
-8
lines changed

tutorial-contents/Genetic Algorithm/Find Path.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def select(self, fitness):
5151

5252
def crossover(self, parent, pop):
5353
if np.random.rand() < self.cross_rate:
54-
i_ = np.random.choice(np.arange(self.pop_size), size=1, replace=False) # select another individual from pop
54+
i_ = np.random.randint(0, self.pop_size, size=1) # select another individual from pop
5555
cross_points = np.random.randint(0, 2, self.DNA_size).astype(np.bool) # choose crossover points
5656
parent[cross_points] = pop[i_, cross_points] # mating and produce one child
5757
return parent

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ def select(pop, fitness): # nature selection wrt pop's fitness
3333

3434
def crossover(parent, pop): # mating process (genes crossover)
3535
if np.random.rand() < CROSS_RATE:
36-
i_ = np.random.randint(0, POP_SIZE, 1) # select another individual from pop
37-
cross_points = np.random.randint(0, 2, DNA_SIZE).astype(np.bool) # choose crossover points
38-
parent[cross_points] = pop[i_, cross_points] # mating and produce one child
36+
i_ = np.random.randint(0, POP_SIZE, size=1) # select another individual from pop
37+
cross_points = np.random.randint(0, 2, size=DNA_SIZE).astype(np.bool) # choose crossover points
38+
parent[cross_points] = pop[i_, cross_points] # mating and produce one child
3939
return parent
4040

4141

tutorial-contents/Genetic Algorithm/Match Phrase.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def select(self):
4141

4242
def crossover(self, parent, pop):
4343
if np.random.rand() < self.cross_rate:
44-
i_ = np.random.choice(np.arange(self.pop_size), size=1) # select another individual from pop
44+
i_ = np.random.randint(0, self.pop_size, size=1) # select another individual from pop
4545
cross_points = np.random.randint(0, 2, self.DNA_size).astype(np.bool) # choose crossover points
4646
parent[cross_points] = pop[i_, cross_points] # mating and produce one child
4747
return parent

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ def get_fitness(self, product):
3939
def crossover(self, loser_winner): # crossover for loser
4040
cross_idx = np.empty((self.DNA_size,)).astype(np.bool)
4141
for i in range(self.DNA_size):
42-
cross_idx[i] = True if np.random.uniform(0, 1) < self.cross_rate else False # crossover index
42+
cross_idx[i] = True if np.random.rand() < self.cross_rate else False # crossover index
4343
loser_winner[0, cross_idx] = loser_winner[1, cross_idx] # assign winners genes to loser
4444
return loser_winner
4545

4646
def mutate(self, loser_winner): # mutation for loser
4747
mutation_idx = np.empty((self.DNA_size,)).astype(np.bool)
4848
for i in range(self.DNA_size):
49-
mutation_idx[i] = True if np.random.uniform(0, 1) < self.mutate_rate else False # mutation index
49+
mutation_idx[i] = True if np.random.rand() < self.mutate_rate else False # mutation index
5050
# flip values in mutation points
5151
loser_winner[0, mutation_idx] = ~loser_winner[0, mutation_idx].astype(np.bool)
5252
return loser_winner

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def evolve(self, fitness):
7171

7272
class TravelSalesPerson(object):
7373
def __init__(self, n_cities):
74-
self.city_position = np.random.uniform(0, 1, size=(n_cities, 2))
74+
self.city_position = np.random.rand(n_cities, 2)
7575
plt.ion()
7676

7777
def plotting(self, lx, ly, total_d):

0 commit comments

Comments
 (0)