-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhuman.py
72 lines (57 loc) · 2.28 KB
/
human.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
__author__ = 'gkour'
from creatures.abstractcreature import AbstractCreature
from creature_actions import Actions
from config import ConfigBiology, ConfigBrain
import utils
from evolution import DNA
from brains.brainsimple import RandomBrain
import random
import numpy as np
class Human(AbstractCreature):
Fitrah = [0, 0, 0, 0, 0, 0]
def __init__(self, universe, id, dna, age=0, energy=ConfigBiology.INITIAL_ENERGY, parents=None):
super(Human, self).__init__(universe, id, dna, age, energy, parents)
self.new_born()
@staticmethod
def race_basic_dna():
return DNA(ConfigBiology.BASE_MEMORY_SIZE,
ConfigBrain.BASE_LEARNING_RATE,
ConfigBrain.BASE_BRAIN_STRUCTURE_PARAM,
ConfigBiology.BASE_LEARN_FREQ,
ConfigBiology.BASE_LIFE_EXPECTANCY,
ConfigBrain.BASE_REWARD_DISCOUNT,
Human.race_fitrah())
@staticmethod
def get_actions():
return [Actions.LEFT, Actions.RIGHT, Actions.UP, Actions.DOWN, Actions.EAT, Actions.FIGHT]
@staticmethod
def get_race():
return Human
@staticmethod
def race_name():
return 'Human'
@staticmethod
def race_fitrah():
return utils.normalize_dist(Human.Fitrah)
@staticmethod
def self_race_enemy():
return False
def decide(self, state):
eps = max(ConfigBrain.BASE_EPSILON,
1 - (self._age / (self.learning_frequency() * ConfigBiology.MATURITY_AGE)))
brain_actions_prob = self.brain().think(state)
#There is a problem with the dna fitrah (7 instead of 6).
#action_prob = utils.normalize_dist(self.fitrah() + brain_actions_prob)
decision = utils.epsilon_greedy(eps, brain_actions_prob)
return decision
def new_born(self):
if self.get_parents() is None:
return
memories = [parent.get_memory() for parent in self.get_parents() if len(parent.get_memory()) > 0]
if len(memories) == 0:
return
oral_tradition = np.concatenate(memories)
self._memory.extend(
random.sample(oral_tradition.tolist(), min(int(self.memory_size() / 2), len(oral_tradition))))
for i in range(5):
self.brain().train(self.get_memory())