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
from PIL import Image, ExifTags | |
import webbrowser | |
def converter(value): | |
d0 = value[0][0] | |
d1 = value[0][1] | |
d = float(d0) / float(d1) | |
m0 = value[1][0] | |
m1 = value[1][1] | |
m = float(m0) / float(m1) |
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
# dummy battle class | |
class Battle: | |
def __init__(self, lineup_a, lineup_b, result=None): | |
self.lineup_a = lineup_a | |
self.lineup_b = lineup_b | |
self.result = result | |
# fight - compare number of 1s in the gene | |
def fight(pair): | |
# save battle info for evaluation |
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
if __name__ == "__main__": | |
lineups = [] | |
generation_size = 20 | |
# initialize 20 lineups with initial score 50 | |
for i in range(generation_size): | |
lineups.append(Lineup(50)) | |
# form battle pairs | |
pairs = np.random.choice(lineups, size=(int(generation_size/2),2), replace=False) |
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
{'name': 'wimpy-indigo-falcon', 'gene': '00011010101110011111', 'combat_power': 12, 'score': 51} | |
{'name': 'surly-chestnut-dachsbracke', 'gene': '10111011100101100101', 'combat_power': 12, 'score': 51} | |
{'name': 'silly-blue-gar', 'gene': '11010010110011111011', 'combat_power': 13, 'score': 51} | |
{'name': 'gummy-magenta-sloth', 'gene': '10101000110110010110', 'combat_power': 10, 'score': 51} | |
{'name': 'bluesy-gamboge-iguana', 'gene': '01010101111011101110', 'combat_power': 13, 'score': 51} | |
{'name': 'pokey-azure-zebra', 'gene': '10000110011101011110', 'combat_power': 11, 'score': 51} | |
{'name': 'stinky-asparagus-affenpinscher', 'gene': '00011101110011110001', 'combat_power': 11, 'score': 51} | |
{'name': 'scummy-sepia-shrew', 'gene': '01101100001010101011', 'combat_power': 10, 'score': 51} | |
{'name': 'freaky-taupe-shark', 'gene': '11010010111110110111', 'combat_power': 14, 'score': 51} | |
{'name': 'queasy-plum-ladybird', 'gene': '01000111100001100110', 'combat_power': 9, 'score': 51} |
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
def select(lineups, elite_rate=0.1): | |
num_elite = int(len(lineups)*0.1) | |
# sort lineups according to score | |
scores = [lineup.score for lineup in lineups] | |
order = np.argsort(scores) | |
sorted_lineups = [lineups[i] for i in order] | |
# get 10% elite and 80% mutation pool | |
elite = sorted_lineups[len(lineups)-num_elite:] |
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
# continue from run_ga_1b.py | |
elite, mutation_pool = select(lineups) |
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 | |
import namegenerator | |
from copy import deepcopy | |
# dummy lineup class | |
class Lineup: | |
def __init__(self, score, gene=''): | |
# name only used to differentiate the lineup | |
self.name = namegenerator.gen() | |
if len(gene): |
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
[{'name': 'leaky-rose-tarantula', | |
'gene': '11111110010100111001', | |
'combat_power': 13, | |
'score': 50}, | |
{'name': 'cloudy-aquamarine-malamute', | |
'gene': '01111110111111100010', | |
'combat_power': 14, | |
'score': 50}, | |
{'name': 'sloppy-thistle-lobster', | |
'gene': '10000101110100011101', |
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
# check breedable | |
def breedable(parents): | |
# add two gene sequences | |
# if two gene at the same index is different | |
# the sum will be 1 | |
# if more than 1 position is different | |
# then they are breedable | |
x = parents[0].gene + parents[1].gene | |
if np.count_nonzero(x == 1) >= 2: | |
return True |
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
population = [] | |
offsprings = [] | |
# when offspring not enough for 10% of the population | |
# breed | |
while len(offsprings) < len(elite): | |
# select parent pairs for breeding | |
parents_pairs = np.random.choice(elite, size=(int(len(elite)/2),2), replace=False) | |
for parents in parents_pairs: | |
# pre-pregancy checkup |
OlderNewer