saataa andagii
This commit is contained in:
parent
3e144acb88
commit
b858033964
2 changed files with 25 additions and 20 deletions
|
@ -36,50 +36,49 @@ class Population:
|
|||
self.tm = tm
|
||||
|
||||
def select(self) -> None:
|
||||
fitness_list = []
|
||||
fitness_list = []
|
||||
for individual in self.individuals:
|
||||
fitness_list.append(individual.fitness(self.pm))
|
||||
for i in range(int(1 - (self.ts * self.l))):
|
||||
least = min_i(self.individuals)
|
||||
fitness.pop(least)
|
||||
for i in range(int((1 - self.ts) * self.n)):
|
||||
least = min_i(fitness_list)
|
||||
fitness_list.pop(least)
|
||||
self.individuals.pop(least)
|
||||
|
||||
def reproduct(self) -> None:
|
||||
new = []
|
||||
while len(self.individuals) != self.n:
|
||||
while len(self.individuals) + len(new) != self.n:
|
||||
cut = random.randint(int(self.l / 3), int(2 * self.l / 3))
|
||||
indivi_1 = self.individuals[random.randint(0, len(self.individuals) - 1)]
|
||||
indivi_2 = self.individuals[random.randint(0, len(self.individuals) - 1)]
|
||||
while indivi_1 == indivi_2:
|
||||
indivi_2 = self.individuals[random.randint(0, len(self.individuals) - 1)]
|
||||
new_chromozome = indivi_1.getChromozome()[:cut] + indivi_2.getChromozome()[cut:]
|
||||
new.append(Individual.setChromozome(new_chromozome))
|
||||
child = Individual()
|
||||
child.setChromozome(new_chromozome)
|
||||
new.append(child)
|
||||
self.individuals += new
|
||||
|
||||
def mutate(self) -> None:
|
||||
mutated = []
|
||||
for i in range(int(self.tm * self.n)):
|
||||
to_mutate = self.individuals[random.randint(0, self.n - 1)]
|
||||
to_mutate = random.randint(0, self.n - 1)
|
||||
while to_mutate in mutated:
|
||||
to_mutate = self.individuals[random.randint(0, self.n - 1)]
|
||||
to_mutate.mutate()
|
||||
to_mutate = random.randint(0, self.n - 1)
|
||||
self.individuals[to_mutate].mutate()
|
||||
mutated.append(to_mutate)
|
||||
|
||||
def print_best(self) -> None:
|
||||
fitness_list = []
|
||||
for individual in self.individuals:
|
||||
fitness_list.append(individual.fitness(self.pm))
|
||||
print(self.individuals[max_i(fitness_list)].getChromozome)
|
||||
print(self.individuals[max_i(fitness_list)].getChromozome())
|
||||
|
||||
def run(self) -> None:
|
||||
for i in range(self.ng):
|
||||
print("select")
|
||||
self.select()
|
||||
print("reproduct")
|
||||
self.reproduct()
|
||||
print("mutate")
|
||||
self.mutate()
|
||||
self.print_best()
|
||||
self.print_best()
|
||||
|
||||
class Individual:
|
||||
"""
|
||||
|
@ -100,14 +99,13 @@ class Individual:
|
|||
new += chr(random.randint(0, 255))
|
||||
self.chromozome = new
|
||||
|
||||
def fitness(self, pm) -> float:
|
||||
def fitness(self, pm) -> int:
|
||||
sum = 0
|
||||
for i in range(len(self.chromozome)):
|
||||
sum += abs(ord(self.chromozome[i]) - ord(pm[i]))
|
||||
return -sum
|
||||
|
||||
def mutate(self) -> None:
|
||||
new = list(len(self.chromozome))
|
||||
new[random.randint(0, len(self.chromozome))] = chr(random.randint(0, 255))
|
||||
new = list(self.chromozome)
|
||||
new[random.randint(0, len(new) - 1)] = chr(random.randint(0, 255))
|
||||
self.chromozome = "".join(new)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue