-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrey.py
More file actions
145 lines (116 loc) · 3.56 KB
/
Prey.py
File metadata and controls
145 lines (116 loc) · 3.56 KB
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
from pybrain.structure import FeedForwardNetwork
from pybrain.structure import LinearLayer, SigmoidLayer
from pybrain.structure import FullConnection
import math
class Prey:
# initial radius
init_radius = 20
def __init__(self, direction, x, y):
# radius
self.radius = self.init_radius
#Neural network
self.nn = FeedForwardNetwork()
#Add layers
inLayer = LinearLayer(8)
hiddenLayer = SigmoidLayer(9)
outLayer = LinearLayer(4)
self.nn.addInputModule(inLayer)
self.nn.addModule(hiddenLayer)
self.nn.addOutputModule(outLayer)
#Add connections
in_to_hidden = FullConnection(inLayer, hiddenLayer)
hidden_to_out = FullConnection(hiddenLayer, outLayer)
self.nn.addConnection(in_to_hidden)
self.nn.addConnection(hidden_to_out)
#initialize NN
self.nn.sortModules()
# Energy - dies when reaches 0
self.energy = 350
# Max Energy. the max amount of energy a prey can have
self.max_energy = 500
# Location
self.x = x
self.y = y
# direction / angle
self.direction = direction
# Senses predator
self.senses_predator = False
# predator's general direction
self.pred_direction = 0
# other prey's general direction
self.prey_direction = 0
# other prey's radius
self.prey_radius = 0
# where to move to next
self.next_x = x
self.next_y = y
# eat or not (eating regains energy)
self.want_to_eat = False
# move or not
self.want_to_move = False
# if energy is less than 100, gets hungry status
self.is_hungry = False
# Age
self.age = 0
# output thresholds for decisions
self.move_threshold = 0
self.eat_threshold = 0
# has it mated and reproduced yet?
self.not_mated = True
# number of attacking preds
self.num_atk_pred = 0
# energy per pred. how much energy each predator gains when eating this prey
self.energy_per_pred = 0
def update(self):
# metabolism depends on which state the prey is in (escaping from predator, idle)
if (self.senses_predator is True):
if (self.energy < 25):
self.energy = 0
else:
self.energy -= 25
else: # idle mode, consumes less energy
if (self.energy < 10):
self.energy = 0
else:
self.energy -= 10
if (self.energy < 100):
self.is_hungry = True
else:
self.is_hungry = False
# Aging
self.age += 1
# Input vector
# input values are determined by what the animat
# is seeing and / or touching
input_vector = (
(2000 * int(self.senses_predator)),
(2000 * self.energy),
(2000 * self.is_hungry),
(2000 * self.direction),
(2000 * self.pred_direction),
(2000 * self.prey_direction),
(2000 * self.prey_radius),
(2000 * self.age)
)
# Activate the nn
output_vector = self.nn.activate(input_vector)
# move
if (output_vector[0] > self.move_threshold):
self.want_to_move = True
else:
self.want_to_move = False
# eat
if (output_vector[1] > self.eat_threshold):
self.want_to_eat = True
else:
self.want_to_eat = False
# direction: turn right (clockwise)
self.direction -= output_vector[2]
#direction: turn left (counter clockwise)
self.direction += output_vector[3]
if (self.want_to_eat):
if (self.energy >= 400):
self.energy = 500
else:
self.energy += 100
self.is_hungry = False