diff --git a/hamazaki1990/individual.py b/hamazaki1990/individual.py index 12abfe5..626e51a 100644 --- a/hamazaki1990/individual.py +++ b/hamazaki1990/individual.py @@ -1,15 +1,22 @@ class Individual: - def __init__(self, n): + def __init__(self, n, f=1.0): self._id = n + self._fitness = f def get_id(self): return self._id + def get_fitness(self): + return self._fitness + def __repr__(self): return str(self._id) ind = Individual(42) print(ind.get_id()) -print(ind) -print(str(ind)) +print(ind.get_fitness()) + +ind2 = Individual(30, 0.8) +print(ind2.get_id()) +print(ind2.get_fitness()) diff --git a/hamazaki1990/population.py b/hamazaki1990/population.py index ac19d53..cf2e18e 100644 --- a/hamazaki1990/population.py +++ b/hamazaki1990/population.py @@ -2,30 +2,77 @@ from individual import Individual +def roulettechoice(individuals, cumsum_fitness): + r = random.uniform(0.0, max(cumsum_fitness)) + for i in range(len(cumsum_fitness)): + if r < cumsum_fitness[i]: + return individuals[i] + + class Population: - def __init__(self, size): - self._inds = [Individual(x) for x in range(size)] + def __init__(self, n, mutantrate=0, s=0): + num_mutant = int(n * mutantrate) + mutant_inds = [Individual(x, 1 + s) for x in range(num_mutant)] + wild_inds = [Individual(x) for x in range(num_mutant, n)] + self._inds = mutant_inds + wild_inds def print_ids(self): print(self._inds) + def print_fitness(self): + fitness = [x.get_fitness() for x in self._inds] + print(fitness) + def next_genwf(self): - n = len(self._inds) - self._inds = [self._inds[random.randrange(n)] for x in range(n)] + fitness = [x.get_fitness() for x in self._inds] + size = len(self._inds) + cumsum_fitness = [sum(fitness[:i]) for i in range(1, size + 1)] + next_generation = [] + for x in range(size): + next_generation.append(roulettechoice(self._inds, cumsum_fitness)) + self._inds = next_generation def next_genmo(self): - n = len(self._inds) - self._inds[random.randrange(n)] = self._inds[random.randrange(n)] + fitness = [x.get_fitness() for x in self._inds] + size = len(self._inds) + cumsum_fitness = [sum(fitness[:i]) for i in range(1, size + 1)] + i_dying = random.randrange(size) + self._inds[i_dying] = roulettechoice(self._inds, cumsum_fitness) + + +p1_1 = Population(10) +p1_1.print_ids() +p1_1.print_fitness() + +for x in range(20): + p1_1.next_genwf() + p1_1.print_ids() +p1_1.print_fitness() + +p1_2 = Population(10, 0.3, 0.2) +p1_2.print_ids() +p1_2.print_fitness() + +for x in range(20): + p1_2.next_genwf() + p1_2.print_ids() +p1_2.print_fitness() -p1 = Population(10) +p2_1 = Population(10) +p2_1.print_ids() +p2_1.print_fitness() -for t in range(20): - p1.print_ids() - p1.next_genwf() +for x in range(20): + p2_1.next_genmo() + p2_1.print_ids() +p2_1.print_fitness() -p2 = Population(10) +p2_2 = Population(10, 0.3, 0.2) +p2_2.print_ids() +p2_2.print_fitness() -for t in range(20): - p2.print_ids() - p2.next_genmo() +for x in range(20): + p2_2.next_genmo() + p2_2.print_ids() +p2_2.print_fitness()