Skip to content

Commit ec7bf2d

Browse files
committed
feat: merge PR aimacode#1268
1 parent 8d43c29 commit ec7bf2d

File tree

2 files changed

+101
-2
lines changed

2 files changed

+101
-2
lines changed

problem.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
from mimetypes import init
2+
from search import *
3+
from timeit import default_timer as timer
4+
from datetime import timedelta
5+
import time
6+
def switch(type_of_problem):
7+
if type_of_problem=="queen":
8+
erfolgreiche_laufe=0
9+
succeded_gen = 0
10+
aes = 0
11+
start = time.time()
12+
for i in range(1):
13+
x,a = problem(100,8,fitness_fn_queens,[1,2,3,4,5,6,7,8],500,28,0.1)
14+
print(x,fitness_fn_queens(x))
15+
if(fitness_fn_queens(x)==28):
16+
succeded_gen += a
17+
erfolgreiche_laufe += 1
18+
if erfolgreiche_laufe!=0:
19+
aes = succeded_gen/erfolgreiche_laufe
20+
sr = erfolgreiche_laufe/100
21+
end = time.time()
22+
ausfuhrungszeit = end - start
23+
##init_pop = 5
24+
##pmut = 0.125
25+
##ngen = 1000
26+
27+
print(
28+
"(AES)Average Evaluations to a Solution(Geschwindigkeit) ist {}".format(aes)+
29+
"\n(SR)Success Rate(Lösungswahrscheinlichkeit) ist {}".format(sr)+
30+
"\nAusführungszeit ist {}".format(ausfuhrungszeit))
31+
else:
32+
erfolgreiche_laufe=0
33+
succeded_gen = 0
34+
aes = 0
35+
start = time.time()
36+
for i in range(100):
37+
x,a = problem(3,6,fitness_fn_landkart,["red","blue","grey"],200,6,0.01)
38+
print(x,fitness_fn_landkart(x))
39+
if(fitness_fn_landkart(x)==6):
40+
succeded_gen += a
41+
erfolgreiche_laufe += 1
42+
if erfolgreiche_laufe!=0:
43+
aes = succeded_gen/erfolgreiche_laufe
44+
sr = erfolgreiche_laufe/100
45+
end = time.time()
46+
ausfuhrungszeit = end - start
47+
init_pop = 5
48+
pmut = 0.125
49+
ngen = 1000
50+
51+
print("Einstellungen -> init_pop = {}, gen = {}, mut = {}\n".format(init_pop,ngen,pmut)+
52+
"(AES)Average Evaluations to a Solution(Geschwindigkeit) ist {}".format(aes)+
53+
"\n(SR)Success Rate(Lösungswahrscheinlichkeit) ist {}".format(sr)+
54+
"\nAusführungszeit ist {}".format(ausfuhrungszeit))
55+
def main():
56+
switch("queen")
57+
58+
59+
def problem(init_p,state_length,fitness_fn,gene_pool,ngen,f_thres,pmut):
60+
population = init_population(init_p,gene_pool,state_length)
61+
x,a = genetic_algorithm(population = population,fitness_fn=fitness_fn,gene_pool=gene_pool,ngen=ngen,f_thres= f_thres,pmut= pmut)
62+
return x,a
63+
64+
def fitness_fn_queens(individuum):
65+
fitness_ct = 28
66+
for idx, x in enumerate(individuum):
67+
for idx2,x2 in enumerate(individuum):
68+
if(idx>=idx2):
69+
continue
70+
else:
71+
if(abs(idx-idx2)==abs(x-x2)): # 2,4 5,1
72+
fitness_ct-=1
73+
if(x==x2):
74+
fitness_ct-=1
75+
76+
return fitness_ct
77+
78+
def fitness_fn_landkart(individuum):
79+
fitness_ct = 6
80+
if(individuum[1]==individuum[2]):
81+
fitness_ct -= 1
82+
if(individuum[0]==individuum[1] or individuum[0]==individuum[2]):
83+
if(individuum[0]==individuum[1]==individuum[2]):
84+
fitness_ct -= 2
85+
else:
86+
fitness_ct -= 1
87+
if(individuum[1]==individuum[3] or individuum[2]==individuum[3]):
88+
if(individuum[1]==individuum[2]==individuum[3]):
89+
fitness_ct -= 2
90+
else:
91+
fitness_ct -= 1
92+
if(individuum[3]==individuum[4]):
93+
fitness_ct-=1
94+
return fitness_ct
95+
96+
if __name__ == "__main__":
97+
main()
98+
99+

search.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -930,9 +930,9 @@ def genetic_algorithm(population, fitness_fn, gene_pool=[0, 1], f_thres=None, ng
930930

931931
fittest_individual = fitness_threshold(fitness_fn, f_thres, population)
932932
if fittest_individual:
933-
return fittest_individual
933+
return fittest_individual,i
934934

935-
return max(population, key=fitness_fn)
935+
return max(population, key=fitness_fn),-1
936936

937937

938938
def fitness_threshold(fitness_fn, f_thres, population):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy