saataa andagii
This commit is contained in:
parent
3e144acb88
commit
b858033964
2 changed files with 25 additions and 20 deletions
|
@ -39,45 +39,44 @@ class Population:
|
||||||
fitness_list = []
|
fitness_list = []
|
||||||
for individual in self.individuals:
|
for individual in self.individuals:
|
||||||
fitness_list.append(individual.fitness(self.pm))
|
fitness_list.append(individual.fitness(self.pm))
|
||||||
for i in range(int(1 - (self.ts * self.l))):
|
for i in range(int((1 - self.ts) * self.n)):
|
||||||
least = min_i(self.individuals)
|
least = min_i(fitness_list)
|
||||||
fitness.pop(least)
|
fitness_list.pop(least)
|
||||||
self.individuals.pop(least)
|
self.individuals.pop(least)
|
||||||
|
|
||||||
def reproduct(self) -> None:
|
def reproduct(self) -> None:
|
||||||
new = []
|
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))
|
cut = random.randint(int(self.l / 3), int(2 * self.l / 3))
|
||||||
indivi_1 = self.individuals[random.randint(0, len(self.individuals) - 1)]
|
indivi_1 = self.individuals[random.randint(0, len(self.individuals) - 1)]
|
||||||
indivi_2 = 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:
|
while indivi_1 == indivi_2:
|
||||||
indivi_2 = self.individuals[random.randint(0, len(self.individuals) - 1)]
|
indivi_2 = self.individuals[random.randint(0, len(self.individuals) - 1)]
|
||||||
new_chromozome = indivi_1.getChromozome()[:cut] + indivi_2.getChromozome()[cut:]
|
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
|
self.individuals += new
|
||||||
|
|
||||||
def mutate(self) -> None:
|
def mutate(self) -> None:
|
||||||
mutated = []
|
mutated = []
|
||||||
for i in range(int(self.tm * self.n)):
|
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:
|
while to_mutate in mutated:
|
||||||
to_mutate = self.individuals[random.randint(0, self.n - 1)]
|
to_mutate = random.randint(0, self.n - 1)
|
||||||
to_mutate.mutate()
|
self.individuals[to_mutate].mutate()
|
||||||
mutated.append(to_mutate)
|
mutated.append(to_mutate)
|
||||||
|
|
||||||
def print_best(self) -> None:
|
def print_best(self) -> None:
|
||||||
fitness_list = []
|
fitness_list = []
|
||||||
for individual in self.individuals:
|
for individual in self.individuals:
|
||||||
fitness_list.append(individual.fitness(self.pm))
|
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:
|
def run(self) -> None:
|
||||||
for i in range(self.ng):
|
for i in range(self.ng):
|
||||||
print("select")
|
|
||||||
self.select()
|
self.select()
|
||||||
print("reproduct")
|
|
||||||
self.reproduct()
|
self.reproduct()
|
||||||
print("mutate")
|
|
||||||
self.mutate()
|
self.mutate()
|
||||||
self.print_best()
|
self.print_best()
|
||||||
|
|
||||||
|
@ -100,14 +99,13 @@ class Individual:
|
||||||
new += chr(random.randint(0, 255))
|
new += chr(random.randint(0, 255))
|
||||||
self.chromozome = new
|
self.chromozome = new
|
||||||
|
|
||||||
def fitness(self, pm) -> float:
|
def fitness(self, pm) -> int:
|
||||||
sum = 0
|
sum = 0
|
||||||
for i in range(len(self.chromozome)):
|
for i in range(len(self.chromozome)):
|
||||||
sum += abs(ord(self.chromozome[i]) - ord(pm[i]))
|
sum += abs(ord(self.chromozome[i]) - ord(pm[i]))
|
||||||
return -sum
|
return -sum
|
||||||
|
|
||||||
def mutate(self) -> None:
|
def mutate(self) -> None:
|
||||||
new = list(len(self.chromozome))
|
new = list(self.chromozome)
|
||||||
new[random.randint(0, len(self.chromozome))] = chr(random.randint(0, 255))
|
new[random.randint(0, len(new) - 1)] = chr(random.randint(0, 255))
|
||||||
self.chromozome = "".join(new)
|
self.chromozome = "".join(new)
|
||||||
|
|
||||||
|
|
11
main.py
11
main.py
|
@ -4,10 +4,17 @@
|
||||||
import lib.ultra_mastermind_obj as libobj
|
import lib.ultra_mastermind_obj as libobj
|
||||||
import lib.ultra_mastermind_imp as libomp
|
import lib.ultra_mastermind_imp as libomp
|
||||||
|
|
||||||
|
# constants
|
||||||
|
PM = "Saataa andagii !"
|
||||||
|
NG = 2000
|
||||||
|
N = 400
|
||||||
|
TS = 0.5
|
||||||
|
TM = 0.01
|
||||||
|
|
||||||
|
# main function
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
pop = libobj.Population('saucisse', 1000, 100, 0.3, 0.3)
|
pop = libobj.Population(pm = PM, ng = NG, n = N, ts = TS, tm = TM)
|
||||||
pop.run()
|
pop.run()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue