Skip to content

Commit

Permalink
solves #2. big improvement in effeciency and much nicer code
Browse files Browse the repository at this point in the history
  • Loading branch information
JMathiszig-Lee committed Nov 26, 2018
1 parent ca6b181 commit 85436d4
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions genetic_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,23 @@ def test_population(pop, best, second, one, two):
#print "best: " + str(best_fitness)

#switching between the tuple and value is confusing and i'm getting upset

pop_list = []
for i in pop:
try:
result = multi_core_test(cores, max, i)
fitness = result[1]

if fitness < best_fitness:
# move current best to second best
second = best
second_set = fittest_set
pop_list.append([fitness, i, result])

best = result
fittest_set = i
except:
result = (99, 99, 99)

elif fitness < second_fitness and fitness != best_fitness:
second = result
second_set = i


# these should be tuples
# print "%-40s %-40s" % ('best', 'second')
# print "%-40s %-40s" % (best, second)
except:
result = (99, 99, 99)
# print "except"
# print " "

output = (fittest_set, best, second_set, second)
pop_list.sort()
#output = (fittest_set, best, second_set, second)
output = (pop_list[0][1], pop_list[0][2], pop_list[1][1], pop_list[1][2])

return output

Expand Down Expand Up @@ -228,6 +217,7 @@ def multi_core_test(cores, max, params_vector):
second_fitness = fit_results[3]
sec_fit = second_fitness[1]

max_tries = 0
while sec_fit > 9.9:
new_pop = create_new_population(pop)
fit_results = test_population(new_pop, best_fitness, second_fitness, fittest_set, second_set)
Expand All @@ -238,6 +228,10 @@ def multi_core_test(cores, max, params_vector):
second_set = fit_results[2]
second_fitness = fit_results[3]
sec_fit = second_fitness[1]
#ctrl c with multiple cores is bad so give nicer way of exiting
max_tries += 1
if max_tries == 10:
quit()

print "trying again"

Expand All @@ -247,6 +241,7 @@ def multi_core_test(cores, max, params_vector):

for i in range(gens):

#TODO delete this function?
def pick_random_set():
not_fittest = 1
while not_fittest == 1:
Expand All @@ -256,7 +251,7 @@ def pick_random_set():
not_fittest = 0
return randset

new_pop = mutate_population(4, fittest_set, second_set, 4)
new_pop = mutate_population(10, fittest_set, second_set, 10)

gen +=1
fit_results = test_population(new_pop, best_fitness, second_fitness, fittest_set, second_set)
Expand Down

0 comments on commit 85436d4

Please sign in to comment.