-
Notifications
You must be signed in to change notification settings - Fork 0
/
experiment.py
205 lines (168 loc) · 7.16 KB
/
experiment.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import csv
from datetime import datetime
import os
import random
from typing import List
import numpy
from engine.agents import Agent, ContextRegistry, MoveToLocation, WeightVector
from engine.agents.p_basic import Idle, Sleep
from engine.entities.object import Object
from engine.logger import Logger
from engine.world import Location, World
from utils.dependency_manager import DependencyManager
NUM_TICKS = 24000
NUM_TICKS_TO_LOG_COMMIT = 10000
def create_bed(name: str, world: World, location: Location) -> Object:
bed = Object(name)
bed.add_attribute("bed", True)
bed.add_attribute("occupied", False)
world.register_entity(bed)
world.place_entity(bed, location)
return bed
def create_random_weight_vector(context_registry: ContextRegistry) -> WeightVector:
weight_vector = context_registry.createEmptyWeightVector()
weight_vector.registerScalarFeatureWeights(
"Time", numpy.random.uniform(-1, 1), numpy.random.uniform(-1, 1)
)
for location in context_registry.getFeatureValues("CurrentLocation"):
weight_vector.registerCategorialFeatureWeights(
"CurrentLocation",
location,
numpy.random.uniform(-1, 1),
numpy.random.uniform(-1, 1),
)
for location in context_registry.getFeatureValues("TargetLocation"):
weight_vector.registerCategorialFeatureWeights(
"TargetLocation",
location,
numpy.random.uniform(-1, 1),
numpy.random.uniform(-1, 1),
)
for entity in context_registry.getFeatureValues("TargetEntity"):
weight_vector.registerCategorialFeatureWeights(
"TargetEntity",
entity,
numpy.random.uniform(-1, 1),
numpy.random.uniform(-1, 1),
)
weight_vector.registerScalarFeatureWeights(
"NumberNearbyAgent", numpy.random.uniform(
-1, 1), numpy.random.uniform(-1, 1)
)
return weight_vector
def create_base_agent(
name: str,
world: World,
starting: Location,
) -> Agent:
agent = Agent(name, world)
world.register_entity(agent)
world.place_entity(agent, starting)
return agent
def add_random_weights_to_practices(agent: Agent, context: ContextRegistry) -> None:
agent.add_weight_vector(
MoveToLocation, create_random_weight_vector(context))
agent.add_weight_vector(Sleep, create_random_weight_vector(context))
agent.add_weight_vector(Idle, create_random_weight_vector(context))
def run_world():
logger = DependencyManager.instance().get_logger()
w1 = World()
# Add Locations
house1 = Location("House1", min_time_inside=10, is_path=False)
house2 = Location("House2", min_time_inside=10, is_path=False)
house3 = Location("House3", min_time_inside=10, is_path=False)
house4 = Location("House4", min_time_inside=8, is_path=False)
square = Location("Square", min_time_inside=50, is_path=False)
path1 = Location("Path1", min_time_inside=1, is_path=True)
path2 = Location("Path2", min_time_inside=2, is_path=True)
path3 = Location("Path3", min_time_inside=2, is_path=True)
path4 = Location("Path4", min_time_inside=5, is_path=True)
path5 = Location("Path5", min_time_inside=10, is_path=True)
workplace1 = Location("Workplace 1", min_time_inside=10, is_path=False)
workplace2 = Location("Workplace 2", min_time_inside=10, is_path=False)
w1.register_location(house1)
w1.register_location(house2)
w1.register_location(house3)
w1.register_location(house4)
w1.register_location(workplace1)
w1.register_location(workplace2)
w1.register_location(square)
w1.register_location(path1)
w1.register_location(path2)
w1.register_location(path3)
w1.register_location(path4)
w1.register_location(path5)
w1.register_location_connection(house1, path1)
w1.register_location_connection(house2, path2)
w1.register_location_connection(house3, path2)
w1.register_location_connection(path1, square)
w1.register_location_connection(path2, square)
w1.register_location_connection(path3, square)
w1.register_location_connection(path3, path4)
w1.register_location_connection(path4, house4)
w1.register_location_connection(path3, workplace1)
w1.register_location_connection(path5, square)
w1.register_location_connection(path5, workplace2)
# Create Beds
create_bed("Bed 1", w1, house1)
create_bed("Bed 2", w1, house2)
create_bed("Bed 3", w1, house3)
create_bed("Bed 4", w1, house4)
create_bed("Bed 5", w1, house3)
create_bed("Bed 6", w1, house4)
create_bed("Bed 7", w1, house2)
create_bed("Bed 8", w1, house2)
create_bed("Bed 9", w1, house1)
# Create Agent 1
agent_1 = create_base_agent(name="Agent1", world=w1, starting=house1)
agent_2 = create_base_agent(name="Agent2", world=w1, starting=house2)
agent_3 = create_base_agent(name="Agent3", world=w1, starting=house3)
agent_4 = create_base_agent(name="Agent4", world=w1, starting=house4)
agent_5 = create_base_agent(name="Agent5", world=w1, starting=house1)
agent_6 = create_base_agent(name="Agent6", world=w1, starting=house1)
agent_7 = create_base_agent(name="Agent7", world=w1, starting=house2)
agent_8 = create_base_agent(name="Agent8", world=w1, starting=house3)
agent_9 = create_base_agent(name="Agent9", world=w1, starting=house3)
agents = [agent_1, agent_2, agent_3, agent_4,
agent_5, agent_6, agent_7, agent_8, agent_9]
# Define Features
context_registry = ContextRegistry()
context_registry.registerScalarFeature("Time")
context_registry.registerScalarFeature("NumberNearbyAgent")
context_registry.registerCategoricalFeature(
"CurrentLocation", w1.locations
)
context_registry.registerCategoricalFeature(
"TargetLocation", w1.locations
)
context_registry.registerCategoricalFeature(
"TargetEntity", w1.entities
)
add_random_weights_to_practices(agent_1, context_registry)
add_random_weights_to_practices(agent_2, context_registry)
add_random_weights_to_practices(agent_3, context_registry)
add_random_weights_to_practices(agent_4, context_registry)
add_random_weights_to_practices(agent_5, context_registry)
add_random_weights_to_practices(agent_6, context_registry)
add_random_weights_to_practices(agent_7, context_registry)
add_random_weights_to_practices(agent_8, context_registry)
add_random_weights_to_practices(agent_9, context_registry)
# w1.plot_map()
# Simulate
print("Starting Simulation...")
start = datetime.now()
for i in range(NUM_TICKS):
w1.tick()
if i % NUM_TICKS_TO_LOG_COMMIT == 0:
logger.commit()
logger.commit()
print("Simulation ended")
end = datetime.now()
delta = end - start
total_miliseconds = delta.total_seconds() * 1000 + delta.microseconds / 1000
print(f"Total simulation took {total_miliseconds/1000} seconds")
print(f"Average tick took {total_miliseconds/NUM_TICKS} miliseconds")
if __name__ == "__main__":
while True:
DependencyManager.instance().add_logger(Logger(f"logs/{ datetime.now().strftime('%Y_%m_%d_%H_%M_%S_%f')}_{random.randint(0,9999)}.db"))
run_world()