Skip to content

Commit

Permalink
Mutation's working, fitness scores are more or less consistantly goin…
Browse files Browse the repository at this point in the history
…g down. Forgot that interior angles are treated as the same as outside ones, so uh yeah. Try plotting (x=2.378, y=2.09), vertex(x=4.195, y=6.963), vertex(x=8.247, y=0.386), vertex(x=0.162, y=0.533), vertex(x=5.363, y=9.495), vertex(x=9.453, y=3.101)]
  • Loading branch information
solomondg committed Nov 10, 2015
1 parent 0eb0b1b commit 3fa0050
Showing 1 changed file with 53 additions and 23 deletions.
76 changes: 53 additions & 23 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import random
import math
from collections import namedtuple
csvgen = True
# import statistics
Vertex = namedtuple('vertex', 'x y')
# This'll make the vertex tuple that we'll use for the chromosones.
Expand All @@ -31,9 +32,9 @@
# pass


mutation_rate = 3 # (out of 100)
mutation_rate = 2 # (out of 100)

population_size = 256
population_size = 64


def chromosonegen():
Expand Down Expand Up @@ -184,7 +185,7 @@ def roulette_generate(fitness_dict, genmethod):

def checker(a, b):
returns = False
while n in a:
for n in a:
for m in b:
if n == m:
returns = True
Expand Down Expand Up @@ -218,10 +219,13 @@ def mutation_chance(mutation_rate):


def random_mutation(individual):
# x = random.randint(0, 128)
x = random.randint(0, 128)
# print (individual.chromosone)
# chromosone_regen(individual)
individual = chromosone_scramble(individual)
if x < 64:
individual.chromosone = chromosone_scramble(individual)
else:
individual.chromosone = chromosone_regen(individual)
# print (individual.chromosone)
# if x < 8:
# individual = bound_mutation(individual)
Expand All @@ -231,7 +235,7 @@ def random_mutation(individual):
# individual = chromosone_scramble(individual)
# else:
# individual = arithmatic_mutation(individual)
return individual
return individual.chromosone


# def bound_mutation(individual):
Expand Down Expand Up @@ -267,17 +271,32 @@ def random_mutation(individual):
# temp[y].y = individual.chromosone[y].z / a
# return temp

def return_highest_fitness_value(fitness_dict):
y = []
for x in fitness_dict:
y.append(fitness_dict[x])

return sorted(y)[0]


def return_highest_fitness_chromosone(fitness_dict):
max_fitness_object = 360
max_fitness = 360
for x in fitness_dict:
if fitness_dict[x] < max_fitness:
max_fitness_object = x
max_fitness = fitness_dict[max_fitness_object]
return max_fitness_object.chromosone


def chromosone_regen(individual):
individual.chromosone = chromosonegen
return individual
individual.chromosone = chromosonegen()
return individual.chromosone


def chromosone_scramble(individual):
# for x in range(random.randint(0, 100)):
individual.chromosone = \
point_swap(individual.chromosone, individual.chromosone)
return individual
return point_swap(individual.chromosone, individual.chromosone)

popset = initiate_population()

Expand All @@ -290,18 +309,29 @@ def chromosone_scramble(individual):

# print (y.chromosone)
y = 0
for n in range(1024):
if csvgen:
csv = open('csv.txt', 'w')
csvtemp = ""

exit = False

while exit is False:
y += 1
for n in range(0, len(popset)):
if mutation_chance(mutation_rate):
# print ("before: " + str(popset[n].chromosone))
popset[n].chromosone = random_mutation(popset[n])
# print ("after: " + str(popset[n].chromosone))
x = make_fitness_dict(popset)
print ("Generation " + str(y) + ". Top score is " +
str(return_highest_fitness_value(x)) + " with a chromosone of " +
str(return_highest_fitness_chromosone(x)))
if csvgen:
csvtemp += str(y) + "," + str(return_highest_fitness_value(x)) + "\n"
if return_highest_fitness_value(x) < 32:
exit = True
print (return_highest_fitness_chromosone(x))
popset = generate_generation(popset)
y += 1
print ("Generation " + str(y) + ". Example roulette selection is " +
str(evaluator(fitness_select(x))))
# for n in range(0, len(popset)):
# if mutation_chance(mutation_rate):
# popset[n] = random_mutation(popset[n])
fitlist = []
for z in x:
fitlist.append(x[z])
a = findmax(x, fitlist[0])
print (a.chromosone)

if csvgen:
csv.write(csvtemp)

0 comments on commit 3fa0050

Please sign in to comment.