Created
August 2, 2016 10:57
-
-
Save rukshn/f5cc571aeb2ca149d472b7701bf75734 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
low = 0 | |
high = 9 | |
chromes = 200 | |
epoch = 2000 | |
achivement = 0.58 | |
cr = 0.4 | |
mr = 0.3 | |
corrector_rate = 0.5 | |
master_fitness = "" | |
sample = np.random.randint(high, size=(10,2)) | |
att = [430.0697618, 382.2041339, 315.9746825, 383.6665219, 456.2981481, 255.5777768, 269.8147513, 176.7710384, 127.984374, 84.70537173] | |
defe =[760, 712, 632, 560, 504, 528, 552, 406, 364, 270] | |
dna = [] | |
def select(fs): | |
p = np.random.uniform(0, 1) | |
for i, f in enumerate(fs): | |
if p <= 0: | |
break | |
p -= f | |
return i | |
for ch in range(0,chromes): | |
dna.append(np.random.randint(9, size=(10,2))) | |
for xxy in range(epoch): | |
F_obj = [] | |
fitness = [] | |
fitness_total = 0 | |
p = [] | |
c = [] | |
new_gen = [] | |
cross = [] | |
for d in range(len(dna)): | |
total = -5.6 | |
for dn in range(len(dna[d])): | |
attacker = dna[d][dn][0] | |
defence = dna[d][dn][1] | |
v_attacker = att[attacker] | |
v_defence = defe[defence] | |
fight = v_attacker/v_defence | |
# attack = achivement - fight | |
# attack = np.power(attack,2) | |
# attack = np.sqrt(attack) | |
total += fight | |
# print(attack) | |
total = np.power(total,2) | |
total = np.sqrt(total) | |
F_obj.append(total) | |
for d in range(len(F_obj)): | |
fit = 1/(1+F_obj[d]) | |
fitness_total += fit | |
fitness.append(fit) | |
# print(fitness_total) | |
# print(F_obj) | |
for d in range(len(fitness)): | |
prob = fitness[d]/fitness_total | |
p.append(prob) | |
# print('-----------') | |
# print(p) | |
for x in range(len(fitness)): | |
sel = select(p) | |
new_gen.append(dna[x]) | |
# print('-----------') | |
dna = new_gen | |
for x in range(len(dna)): | |
r = np.random.uniform(0,1) | |
mutation_cutoff = np.random.uniform(0,1) | |
corrector = np.random.uniform(0,1) | |
mpos = np.random.randint(9) | |
mutation = np.random.randint(9) | |
mp = np.random.randint(1) | |
# if corrector < corrector_rate: | |
# for co in dna[x]: | |
# pos0 = dna[x][co][0][0] | |
# pos1 = dna[x][co][0][1] | |
# corrector_mutation = np.random.randint(-2, high=2) | |
# | |
# if pos0-pos1 > 3: | |
# pos1 = pos0 + corrector_mutation | |
# dna[x][co][0][1] = pos1 | |
# elif pos1 - pos0 > 3: | |
# pos0 = pos1 + corrector_mutation | |
# dna[x][co][0][0] = pos0 | |
if mutation_cutoff < mr: | |
dna[x][mpos][mp] = mutation | |
if r < cr: | |
cross.append(x) | |
for x in range(len(cross)): | |
r = np.random.randint(len(cross)) | |
cpoint = np.random.randint(1,high=len(dna)-1) | |
splice1 = dna[x][cpoint:] | |
splice2 = dna[r][:cpoint] | |
joint = np.concatenate((splice1,splice2), axis=0) | |
dna[x] = joint | |
maxfit = np.argmin(F_obj) | |
master_fitness += str(F_obj[maxfit]) + "\n" | |
if F_obj[maxfit] < 0.00001: | |
break | |
minval = np.argmin(F_obj) | |
print(F_obj[minval]) | |
print(minval) | |
print(dna[minval]) | |
print(fitness[minval]) | |
f = open('coc.csv','w') | |
f.write(master_fitness) # python will convert \n to os.linesep | |
f.close() # you can omit in most cases as the destructor will call it |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment