-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaiq.py
158 lines (134 loc) · 6.65 KB
/
aiq.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
__author__ = 'gkour'
import numpy as np
from config import ConfigBiology
import utils
from creature_actions import Actions
import random
class AIQ:
@staticmethod
def get_population_aiq(universe):
creatures = universe.get_all_creatures()
sample_creatures = random.sample(creatures, utils.safe_log2(len(creatures)))
all_aiq = [AIQ.get_creature_aiq(creature) for creature in sample_creatures]
return np.round(utils.emptynanmean(all_aiq), 2)
@staticmethod
def get_population_aiq_dist(universe):
creatures = universe.get_all_creatures()
bounds = [ConfigBiology.BASE_LIFE_EXPECTANCY / 3, 2 * ConfigBiology.BASE_LIFE_EXPECTANCY / 3]
young = [AIQ.get_creature_aiq(creature) for creature in creatures if creature.age() <= bounds[0]]
adult = [AIQ.get_creature_aiq(creature) for creature in creatures if bounds[0] < creature.age() <= bounds[1]]
old = [AIQ.get_creature_aiq(creature) for creature in creatures if bounds[1] < creature.age()]
return np.round([utils.emptynanmean(young), utils.emptynanmean(adult), utils.emptynanmean(old)], 2)
@staticmethod
def get_creature_aiq(creature):
score = 0
scenarios = [AIQ.haven_left, AIQ.haven_right, AIQ.haven_inplace, AIQ.haven_up, AIQ.haven_down,
AIQ.border_awareness_up, AIQ.border_awareness_down, AIQ.border_awareness_left, AIQ.border_awareness_right]
w = 0
for i in range(len(scenarios)):
test_state, positive_test_type, expected_actions, weight = scenarios[i](creature.vision_range())
if creature._universe.num_races() == 1:
# delete the other race creatures entry from the state
test_state = np.delete(test_state, obj=2, axis=0)
w += weight
decision = creature.index_to_enum(np.argmax(creature.brain().think(test_state)))
if positive_test_type:
score += weight if decision in expected_actions else 0
else:
score += weight if decision not in expected_actions else 0
return score / w
@staticmethod
def _haven(vision_range, where):
''' Haven cell in current location.'''
energy = 3
age = 3
food = np.zeros(shape=(2 * vision_range + 1, 2 * vision_range + 1))
same_race_creatures = np.ones(shape=(2 * vision_range + 1, 2 * vision_range + 1)) * 20
different_race_creatures = np.ones(shape=(2 * vision_range + 1, 2 * vision_range + 1)) * 20
sound = np.zeros(shape=(2 * vision_range + 1, 2 * vision_range + 1))
energy = np.ones(shape=(2 * vision_range + 1, 2 * vision_range + 1)) * energy
age = np.ones(shape=(2 * vision_range + 1, 2 * vision_range + 1)) * age
if where == 'INPLACE':
food[vision_range][vision_range] = 20
same_race_creatures[vision_range][vision_range] = 0
different_race_creatures[vision_range][vision_range] = 0
optimal_action = Actions.EAT
if where == 'UP':
food[vision_range - 1][vision_range] = 20
same_race_creatures[vision_range - 1][vision_range] = 0
different_race_creatures[vision_range - 1][vision_range] = 0
optimal_action = Actions.UP
if where == 'DOWN':
food[vision_range + 1][vision_range] = 20
same_race_creatures[vision_range + 1][vision_range] = 0
different_race_creatures[vision_range + 1][vision_range] = 0
optimal_action = Actions.DOWN
if where == 'LEFT':
food[vision_range][vision_range - 1] = 20
same_race_creatures[vision_range][vision_range - 1] = 0
different_race_creatures[vision_range][vision_range - 1] = 0
optimal_action = Actions.LEFT
if where == 'RIGHT':
food[vision_range][vision_range + 1] = 20
same_race_creatures[vision_range][vision_range + 1] = 0
different_race_creatures[vision_range][vision_range + 1] = 0
optimal_action = Actions.RIGHT
return np.stack((food, sound, same_race_creatures, different_race_creatures, energy, age)), True, [
optimal_action], 1
@staticmethod
def haven_inplace(vision_range):
return AIQ._haven(vision_range, 'INPLACE')
@staticmethod
def haven_right(vision_range):
return AIQ._haven(vision_range, 'RIGHT')
@staticmethod
def haven_left(vision_range):
return AIQ._haven(vision_range, 'LEFT')
@staticmethod
def haven_up(vision_range):
return AIQ._haven(vision_range, 'UP')
@staticmethod
def haven_down(vision_range):
return AIQ._haven(vision_range, 'DOWN')
@staticmethod
def _border_awareness(vision_range, direction):
energy = 3
age = 3
food = np.zeros(shape=(2 * vision_range + 1, 2 * vision_range + 1))
creatures = np.zeros(shape=(2 * vision_range + 1, 2 * vision_range + 1))
energy = np.ones(shape=(2 * vision_range + 1, 2 * vision_range + 1)) * energy
age = np.ones(shape=(2 * vision_range + 1, 2 * vision_range + 1)) * age
sound = np.zeros(shape=(2 * vision_range + 1, 2 * vision_range + 1))
if direction == 'DOWN':
food[vision_range + 1:][:] = -1
creatures[vision_range + 1:][:] = -1
sound[vision_range + 1:][:] = -1
bad_action = Actions.DOWN
if direction == 'UP':
food[:vision_range][:] = -1
creatures[:vision_range][:] = -1
sound[:vision_range][:] = -1
bad_action = Actions.UP
if direction == 'LEFT':
food[:][:vision_range] = -1
creatures[:][:vision_range] = -1
sound[:][:vision_range] = -1
bad_action = Actions.LEFT
if direction == 'RIGHT':
food[:][vision_range + 1:] = -1
creatures[:][vision_range + 1:] = -1
sound[:][vision_range + 1:] = -1
bad_action = Actions.RIGHT
return np.stack((food, sound, creatures, creatures, energy, age)), False, [bad_action], 0.5
@staticmethod
def border_awareness_up(vision_range):
return AIQ._border_awareness(vision_range, 'UP')
@staticmethod
def border_awareness_down(vision_range):
return AIQ._border_awareness(vision_range, 'DOWN')
@staticmethod
def border_awareness_left(vision_range):
return AIQ._border_awareness(vision_range, 'LEFT')
@staticmethod
def border_awareness_right(vision_range):
return AIQ._border_awareness(vision_range, 'RIGHT')