diff --git a/learning.py b/learning.py index df5d6fce3..3e7f4690c 100644 --- a/learning.py +++ b/learning.py @@ -746,7 +746,7 @@ def weighted_replicate(seq, weights, n): wholes = [int(w * n) for w in weights] fractions = [(w * n) % 1 for w in weights] return (flatten([x] * nx for x, nx in zip(seq, wholes)) + - weighted_sample_with_replacement(seq, fractions, n - sum(wholes))) + weighted_sample_with_replacement(n - sum(wholes),seq, fractions, )) def flatten(seqs): return sum(seqs, []) diff --git a/probability.py b/probability.py index abbc07791..d28a8a38b 100644 --- a/probability.py +++ b/probability.py @@ -651,5 +651,5 @@ def particle_filtering(e, N, HMM): w[i] = float("{0:.4f}".format(w[i])) # STEP 2 - s = weighted_sample_with_replacement(s, w, N) + s = weighted_sample_with_replacement(N,s,w) return s diff --git a/search.py b/search.py index 04d5b6c51..e4e20da3f 100644 --- a/search.py +++ b/search.py @@ -587,7 +587,7 @@ def genetic_algorithm(population, fitness_fn, ngen=1000, pmut=0.1): new_population = [] for i in range(len(population)): fitnesses = map(fitness_fn, population) - p1, p2 = weighted_sample_with_replacement(population, fitnesses, 2) + p1, p2 = weighted_sample_with_replacement(2,population, fitnesses) child = p1.mate(p2) if random.uniform(0, 1) < pmut: child.mutate() diff --git a/utils.py b/utils.py index 3c070293e..714512ae0 100644 --- a/utils.py +++ b/utils.py @@ -193,7 +193,7 @@ def probability(p): return p > random.uniform(0.0, 1.0) -def weighted_sample_with_replacement(seq, weights, n): +def weighted_sample_with_replacement(n,seq, weights): """Pick n samples from seq at random, with replacement, with the probability of each element in proportion to its corresponding weight."""