Last active
December 11, 2015 03:28
-
-
Save lukehorvat/4537918 to your computer and use it in GitHub Desktop.
[Blog post code] Genetic Programming with esec: An Introductory Example
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
instructions = [ | |
DecisionInstructionWithState(lambda fitness_case: 1 if fitness_case.outlook == 'SUNNY' else (2 if fitness_case.outlook == 'OVERCAST' else 3), param_count=3, name='OUT'), | |
DecisionInstructionWithState(lambda fitness_case: 1 if fitness_case.humidity == 'HIGH' else 2, param_count=2, name='HUM'), | |
DecisionInstructionWithState(lambda fitness_case: 1 if fitness_case.windy else 2, param_count=2, name='WIND'), | |
] |
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
config = { | |
'landscape': decision_tree_induction, | |
'system': { | |
'instructions': instructions, | |
'definition': esdl_system_definition, | |
'size': 500, | |
'deepest_result': 17, | |
}, | |
'monitor': { | |
'report': 'gen+births+best+local+best_length+time_delta+best_genome', | |
'summary': 'status+best+best_length+best_phenome', | |
'limits': { | |
'iterations': 50, | |
'fitness': TGPFitness([len(fitness_cases), 0]), | |
} | |
}, | |
} |
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
#iter. births b-date fitness | Local: minimum average maximum | length delta time genome | |
0 500 120 11.00 ( 31) | 2.00 ( 13) 5.88 ( 6) 11.00 ( 31) | 31n 4d 0:00'00.218 [[OUT(*,*,*), HUM(*,*), HUM(*,*), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), WIND(*,*), Terminal(1), HUM(*,*), Terminal(1), Terminal(1), OUT(*,*,*), Terminal(1), OUT(*,*,*), Terminal(0), Terminal(1), WIND(*,*), Terminal(0), Terminal(0), WIND(*,*), Terminal(0), OUT(*,*,*), Terminal(0), Terminal(1), Terminal(1), WIND(*,*), Terminal(0), HUM(*,*), Terminal(0), Terminal(1)]] | |
1 960 752 11.00 ( 28) | 3.00 ( 25) 6.81 ( 8) 11.00 ( 28) | 28n 4d 0:00'00.219 [[OUT(*,*,*), HUM(*,*), HUM(*,*), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), WIND(*,*), Terminal(1), HUM(*,*), Terminal(1), Terminal(1), OUT(*,*,*), Terminal(1), OUT(*,*,*), Terminal(0), Terminal(1), WIND(*,*), Terminal(0), Terminal(0), WIND(*,*), Terminal(0), Terminal(0), WIND(*,*), Terminal(0), HUM(*,*), Terminal(0), Terminal(1)]] | |
2 1420 752 11.00 ( 28) | 4.00 ( 33) 7.57 ( 9) 11.00 ( 28) | 28n 4d 0:00'00.218 [[OUT(*,*,*), HUM(*,*), HUM(*,*), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), WIND(*,*), Terminal(1), HUM(*,*), Terminal(1), Terminal(1), OUT(*,*,*), Terminal(1), OUT(*,*,*), Terminal(0), Terminal(1), WIND(*,*), Terminal(0), Terminal(0), WIND(*,*), Terminal(0), Terminal(0), WIND(*,*), Terminal(0), HUM(*,*), Terminal(0), Terminal(1)]] | |
3 1880 1879 11.00 ( 23) | 4.00 ( 25) 8.04 ( 16) 11.00 ( 23) | 23n 4d 0:00'00.265 [[OUT(*,*,*), HUM(*,*), HUM(*,*), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), WIND(*,*), Terminal(1), HUM(*,*), Terminal(1), Terminal(1), OUT(*,*,*), Terminal(1), Terminal(1), WIND(*,*), Terminal(0), Terminal(0), WIND(*,*), Terminal(0), HUM(*,*), Terminal(0), Terminal(1)]] | |
4 2340 1879 11.00 ( 23) | 4.00 ( 41) 8.21 ( 20) 11.00 ( 23) | 23n 4d 0:00'00.250 [[OUT(*,*,*), HUM(*,*), HUM(*,*), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), WIND(*,*), Terminal(1), HUM(*,*), Terminal(1), Terminal(1), OUT(*,*,*), Terminal(1), Terminal(1), WIND(*,*), Terminal(0), Terminal(0), WIND(*,*), Terminal(0), HUM(*,*), Terminal(0), Terminal(1)]] | |
5 2800 2636 12.00 ( 29) | 4.00 (---) 8.29 ( 18) 12.00 ( 29) | 29n 5d 0:00'00.265 [[OUT(*,*,*), WIND(*,*), HUM(*,*), Terminal(0), OUT(*,*,*), Terminal(1), Terminal(0), Terminal(0), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), WIND(*,*), OUT(*,*,*), HUM(*,*), Terminal(0), Terminal(1), WIND(*,*), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), OUT(*,*,*), WIND(*,*), Terminal(0), Terminal(1), Terminal(1), Terminal(0), Terminal(1)]] | |
6 3260 2636 12.00 ( 29) | 3.00 ( 19) 8.36 ( 20) 12.00 ( 29) | 29n 5d 0:00'00.281 [[OUT(*,*,*), WIND(*,*), HUM(*,*), Terminal(0), OUT(*,*,*), Terminal(1), Terminal(0), Terminal(0), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), WIND(*,*), OUT(*,*,*), HUM(*,*), Terminal(0), Terminal(1), WIND(*,*), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), OUT(*,*,*), WIND(*,*), Terminal(0), Terminal(1), Terminal(1), Terminal(0), Terminal(1)]] | |
7 3720 2636 12.00 ( 29) | 4.00 (---) 8.53 ( 27) 12.00 ( 31) | 29n 5d 0:00'00.312 [[OUT(*,*,*), WIND(*,*), HUM(*,*), Terminal(0), OUT(*,*,*), Terminal(1), Terminal(0), Terminal(0), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), WIND(*,*), OUT(*,*,*), HUM(*,*), Terminal(0), Terminal(1), WIND(*,*), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), OUT(*,*,*), WIND(*,*), Terminal(0), Terminal(1), Terminal(1), Terminal(0), Terminal(1)]] | |
8 4180 2636 12.00 ( 29) | 3.00 ( 19) 8.61 ( 31) 12.00 ( 29) | 29n 5d 0:00'00.327 [[OUT(*,*,*), WIND(*,*), HUM(*,*), Terminal(0), OUT(*,*,*), Terminal(1), Terminal(0), Terminal(0), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), WIND(*,*), OUT(*,*,*), HUM(*,*), Terminal(0), Terminal(1), WIND(*,*), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), OUT(*,*,*), WIND(*,*), Terminal(0), Terminal(1), Terminal(1), Terminal(0), Terminal(1)]] | |
9 4640 4440 12.00 ( 27) | 4.00 ( 17) 8.71 ( 37) 12.00 ( 27) | 27n 4d 0:00'00.375 [[OUT(*,*,*), HUM(*,*), HUM(*,*), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), WIND(*,*), Terminal(1), HUM(*,*), Terminal(1), Terminal(1), OUT(*,*,*), Terminal(1), OUT(*,*,*), Terminal(0), Terminal(1), Terminal(0), WIND(*,*), Terminal(0), Terminal(0), WIND(*,*), Terminal(0), OUT(*,*,*), Terminal(0), Terminal(1), Terminal(1)]] | |
10 5100 4440 12.00 ( 27) | 4.00 ( 7) 8.88 ( 34) 12.00 ( 28) | 27n 4d 0:00'00.359 [[OUT(*,*,*), HUM(*,*), HUM(*,*), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), WIND(*,*), Terminal(1), HUM(*,*), Terminal(1), Terminal(1), OUT(*,*,*), Terminal(1), OUT(*,*,*), Terminal(0), Terminal(1), Terminal(0), WIND(*,*), Terminal(0), Terminal(0), WIND(*,*), Terminal(0), OUT(*,*,*), Terminal(0), Terminal(1), Terminal(1)]] | |
11 5560 5338 12.00 ( 21) | 4.00 ( 3) 9.10 ( 38) 12.00 ( 21) | 21n 4d 0:00'00.390 [[OUT(*,*,*), HUM(*,*), HUM(*,*), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), WIND(*,*), Terminal(1), HUM(*,*), Terminal(1), Terminal(1), OUT(*,*,*), Terminal(1), Terminal(1), WIND(*,*), Terminal(0), Terminal(0), WIND(*,*), Terminal(0), Terminal(1)]] | |
12 6020 5605 12.00 ( 16) | 4.00 ( 14) 9.12 ( 43) 12.00 ( 16) | 16n 4d 0:00'00.436 [[OUT(*,*,*), HUM(*,*), HUM(*,*), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), WIND(*,*), Terminal(1), HUM(*,*), Terminal(1), Terminal(1), Terminal(1), WIND(*,*), Terminal(0), Terminal(1)]] | |
13 6480 5605 12.00 ( 16) | 4.00 ( 16) 9.27 ( 46) 12.00 ( 22) | 16n 4d 0:00'00.437 [[OUT(*,*,*), HUM(*,*), HUM(*,*), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), WIND(*,*), Terminal(1), HUM(*,*), Terminal(1), Terminal(1), Terminal(1), WIND(*,*), Terminal(0), Terminal(1)]] | |
14 6940 6504 12.00 ( 8) | 4.00 ( 6) 9.40 ( 51) 12.00 ( 8) | 8n 2d 0:00'00.484 [[OUT(*,*,*), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), WIND(*,*), Terminal(0), Terminal(1)]] | |
15 7400 6504 12.00 ( 8) | 4.00 ( 9) 9.66 ( 52) 12.00 ( 16) | 8n 2d 0:00'00.483 [[OUT(*,*,*), HUM(*,*), Terminal(0), Terminal(1), Terminal(1), WIND(*,*), Terminal(0), Terminal(1)]] |
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
fitness_cases = [SaturdayMorning(outlook, humidity, windy) for outlook in OUTLOOK for humidity in HUMIDITY for windy in WINDY] |
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
OUTLOOK = ('SUNNY', 'OVERCAST', 'RAIN') | |
HUMIDITY = ('HIGH', 'NORMAL') | |
WINDY = (True, False) | |
class SaturdayMorning(object): | |
def __init__(self, outlook, humidity, windy): | |
self.outlook = outlook | |
self.humidity = humidity | |
self.windy = windy | |
def classification(self): | |
if self.outlook == 'SUNNY': | |
return 0 if self.humidity == 'HIGH' else 1 | |
elif self.outlook == 'OVERCAST': | |
return 1 | |
elif self.outlook == 'RAIN': | |
return 0 if self.windy else 1 |
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
@esdl_eval | |
def decision_tree_induction(indiv): | |
score = 0 | |
for fitness_case in fitness_cases: | |
if indiv.evaluate(indiv, state=fitness_case, terminals=[0, 1]) == fitness_case.classification(): | |
score += 1 | |
return TGPFitness([score, len(indiv[0])]) |
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
instructions = [ | |
InstructionWithState(lambda fitness_case, a, b, c: a if fitness_case.outlook == 'SUNNY' else (b if fitness_case.outlook == 'OVERCAST' else c), param_count=3, name='OUT'), | |
InstructionWithState(lambda fitness_case, a, b: a if fitness_case.humidity == 'HIGH' else b, param_count=2, name='HUM'), | |
InstructionWithState(lambda fitness_case, a, b: a if fitness_case.windy else b, param_count=2, name='WIND'), | |
] |
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
esdl_system_definition = r''' | |
FROM random_tgp(instructions=instructions, terminals=2, deepest=4) SELECT (size) population | |
YIELD population | |
BEGIN generation | |
FROM population \ | |
SELECT (0.9*size) to_cross, (0.02*size) to_mutate, (0.08*size) to_reproduce \ | |
USING fitness_proportional | |
FROM to_cross SELECT offspring1 USING crossover_one(deepest_result, terminal_prob=0.1) | |
FROM to_mutate SELECT offspring2 USING mutate_random(deepest_result) | |
FROM offspring1, offspring2, to_reproduce SELECT (size) population | |
YIELD population | |
END generation | |
''' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment