Skip to content

Commit bddd1cf

Browse files
sofmonknorvig
authored andcommitted
Update weighted_sample_with_replacement() in utils.py (aimacode#366)
* Update utils.py in pseudo code the sequence of arguments is " WEIGHTED-SAMPLE-WITH-REPLACEMENT(N, S, W)"  same must follow in function "particle_filtering" in the file probability.py * Update learning.py weight_sample_with_replacement sequence of args * Update probability.py weighted_sample_with_replacement sequence of args * Update search.py
1 parent 14bf5e4 commit bddd1cf

File tree

4 files changed

+4
-4
lines changed

4 files changed

+4
-4
lines changed

learning.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ def weighted_replicate(seq, weights, n):
746746
wholes = [int(w * n) for w in weights]
747747
fractions = [(w * n) % 1 for w in weights]
748748
return (flatten([x] * nx for x, nx in zip(seq, wholes)) +
749-
weighted_sample_with_replacement(seq, fractions, n - sum(wholes)))
749+
weighted_sample_with_replacement(n - sum(wholes),seq, fractions, ))
750750

751751

752752
def flatten(seqs): return sum(seqs, [])

probability.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,5 +651,5 @@ def particle_filtering(e, N, HMM):
651651
w[i] = float("{0:.4f}".format(w[i]))
652652

653653
# STEP 2
654-
s = weighted_sample_with_replacement(s, w, N)
654+
s = weighted_sample_with_replacement(N,s,w)
655655
return s

search.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ def genetic_algorithm(population, fitness_fn, ngen=1000, pmut=0.1):
587587
new_population = []
588588
for i in range(len(population)):
589589
fitnesses = map(fitness_fn, population)
590-
p1, p2 = weighted_sample_with_replacement(population, fitnesses, 2)
590+
p1, p2 = weighted_sample_with_replacement(2,population, fitnesses)
591591
child = p1.mate(p2)
592592
if random.uniform(0, 1) < pmut:
593593
child.mutate()

utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def probability(p):
193193
return p > random.uniform(0.0, 1.0)
194194

195195

196-
def weighted_sample_with_replacement(seq, weights, n):
196+
def weighted_sample_with_replacement(n,seq, weights):
197197
"""Pick n samples from seq at random, with replacement, with the
198198
probability of each element in proportion to its corresponding
199199
weight."""

0 commit comments

Comments
 (0)