Skip to content

Instantly share code, notes, and snippets.

View fyr91's full-sized avatar
🎯
Focusing

febbie fyr91

🎯
Focusing
  • Yoozoo
  • Singapore
View GitHub Profile
@fyr91
fyr91 / GPStracker.py
Created November 6, 2013 07:16
Locate photo with GPSInfo in. Just a test. Based on Erans' work get_lat_lon_exif_pil.py.
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)
@fyr91
fyr91 / ga_1b.py
Last active October 3, 2019 06:50
dummy ga
# 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
@fyr91
fyr91 / run_ga_1b.py
Last active October 3, 2019 06:54
dummpy ga
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)
@fyr91
fyr91 / result_ga_1b.txt
Last active October 3, 2019 09:18
dummy ga
{'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}
@fyr91
fyr91 / ga_1c.py
Last active October 3, 2019 07:03
dummpy ga
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:]
@fyr91
fyr91 / run_ga_1c.py
Last active October 3, 2019 07:10
dummy ga
# continue from run_ga_1b.py
elite, mutation_pool = select(lineups)
@fyr91
fyr91 / ga_1a.py
Last active October 3, 2019 10:34
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):
[{'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',
@fyr91
fyr91 / ga_1d.py
Last active October 3, 2019 10:33
# 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
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