-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_visual_agent.py
100 lines (82 loc) · 3.08 KB
/
simple_visual_agent.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
from visual_object import Line, Circle
from visual_agent import VisualAgent
import os
import sys
import random
import time
import csv
RANDOM_SEED = 1
# The main program
def run_process(outfile_csv, step_size, X1, Y1, X2, Y2, is_circle=False,
show_details=False, path=None):
agent = VisualAgent()
obj = Line()
obj_id = 1
if is_circle is True:
obj = Circle()
obj_id = 2
if path is None:
path = "categorize.ns"
if os.path.exists(path) is False:
print('The network file is Not exit')
sys.exit(1)
agent.nervous_system.load(path)
# Run the agent
random.seed()
agent.reset(0, Y1)
agent.set_positionX(X1)
obj.set_positionX(X2)
obj.set_positionY(Y2)
timer = 0
status = 0
start_time = time.time()
t = 0
if show_details is True:
agent.nervous_system.print_model_abstract()
while obj.positionY() > VisualAgent.BODY_SIZE/2:
t += step_size
timer += 1
if show_details is True:
print("------------------")
print(agent.positionX(), agent.positionY())
print(obj.positionX(), obj.positionY())
outfile_csv.writerow([obj_id, timer, step_size, X1, Y1, X2, Y2,
agent.positionX(), agent.positionY(),
obj.positionX(), obj.positionY(), status])
status = 1
agent.step(step_size, obj, show_details=show_details)
obj.step(step_size)
if show_details is True:
agent.nervous_system.print_model_abstract()
status += 1
end_time = time.time()
if show_details is True:
print('finished computation at', end_time, ', elapsed time: ',
end_time - start_time)
outfile_csv.writerow([obj_id, timer, step_size, X1, Y1, X2, Y2,
agent.positionX(), agent.positionY(),
obj.positionX(), obj.positionY(), status])
if __name__ == "__main__":
outfile = open('output.csv', 'w')
outfile_csv = csv.writer(outfile, delimiter=',',
quotechar="'", quoting=csv.QUOTE_MINIMAL)
outfile_csv.writerow(['obj_type', 'timer', 'step_size', 'X1', 'Y1',
'X2', 'Y2', 'agent_X', 'agent_Y', 'obj_X',
'obj_Y', 'status'])
dataset = []
with open('dataset.csv', 'r') as fi:
csv_file = csv.reader(fi, delimiter=',',
quotechar="'", quoting=csv.QUOTE_MINIMAL)
for row in csv_file:
dataset.append([float(i) for i in row])
for d in dataset:
print(d)
is_circle = True if int(d[0]) == 2 else False
run_process(outfile_csv, 0.1, d[1], d[2], d[3], d[4],
show_details=False, is_circle=is_circle,
path="models/sample.ns")
# run_process(outfile_csv, 0.1, -20, 0, 0, 200, show_details=True)
# print("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&")
# run_process(outfile_csv, 0.1, -20, 0, 0, 200, is_circle=True,
# show_details=True)
outfile.close()