Skip to content

Commit e35951b

Browse files
committed
Final experiments
1 parent 0b46564 commit e35951b

File tree

129 files changed

+5531
-4757
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+5531
-4757
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
/out
1+
/out*
22
/result
33
/others
44
__pycache__
55
/.idea
66
/evaluation_1
77
/fig
8+
/env
89

910
*.jar
1011
*.txt

analyse.ipynb

Lines changed: 4997 additions & 977 deletions
Large diffs are not rendered by default.

config.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
JAR_VERSION="04ffa0"
2-
TAG="designed1_deviated_state"
1+
JAR_VERSION="ef8d28"
2+
TRACE_FILE="sample_designed2_12o-8c-1111.txt"
3+
TAG="designed2_unknown"
34
IMAGE="nan42/eval"
45
TRACE_VOL="trace"
56
RESULT_VOL="result"

run.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function run() {
1616

1717
echo ${compare_method}
1818

19-
for threshold in 5 10 15 20
19+
for threshold in 20
2020
do
2121
for seed in 100
2222
do
@@ -26,7 +26,7 @@ function run() {
2626
--mount source=${result_vol},target=/experiments/runner/result \
2727
--mount source=${trace_vol},target=/experiments/simulator/trace/prediction \
2828
--name ${compare_method}_t${threshold}_s${seed} \
29-
${docker_image}:${tag} -t $threshold -s $seed -c $compare_method -w $compare_window &
29+
${docker_image}:${tag} -t $threshold -s $seed -c $compare_method -w $compare_window -u -q &
3030
done
3131
done
3232
}

src/analysis/Analyse.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from src.runner.Loader import TraceLoader
2+
from src.runner.Comparator import Comparator
3+
4+
# load real trace
5+
path_real_trace = "/Users/Nann/workspace/icws2021/src/simulator/traces_real/sample_20o-10c-3333-999.txt"
6+
Loader = TraceLoader(trace_path=)
7+
# load
8+
9+
comparator = Comparator(compare_method="utility", k=2)
10+
comparator.compare( , )
11+
12+
# Analysis here
13+
plt.plot(np.array(time_list_archive), np.array(dists_list_archive))
14+
plt.title("position")
15+
plt.savefig(statistics_dir + "distance_plots{}_{}.pdf".format(seed, human_seed))
16+
plt.close("all")
17+
18+
plt.plot(np.array(time_list_archive), np.array(dists_knowledge_list_archive))
19+
plt.title("global knowledge")
20+
plt.savefig(statistics_dir + "distance_knowledge_plots{}_{}.pdf".format(seed, human_seed))
21+
plt.close("all")
22+
23+
plt.plot(np.array(time_list_archive), np.array(dists_interaction_list_archive))
24+
plt.title("interaction")
25+
plt.savefig(statistics_dir + "distance_interaction_plots{}_{}.pdf".format(seed, human_seed))
26+
plt.close("all")

src/runner/Client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self, rs_dir=None, port=None):
1818
self.__rs_dir = rs_dir
1919
else:
2020
self.__rs_dir = "/Users/Nann/eclipse-workspace/mobileCameras/mobileCameras.rs"
21-
for i in range(5):
21+
for i in range(10):
2222
try:
2323
if self.__port is not None:
2424
self.__gateway = JavaGateway(gateway_parameters=GatewayParameters(port=self.__port))
@@ -36,7 +36,7 @@ def __init__(self, rs_dir=None, port=None):
3636
def __load(self):
3737
try:
3838
self.__runner.load(self.__rs_dir)
39-
print("loaded: " + self.__rs_dir)
39+
# print("loaded: " + self.__rs_dir)
4040
except:
4141
print("ERROR: load config file failed, please check your file path")
4242
exit(-2)
@@ -53,6 +53,7 @@ def __load(self):
5353

5454
def __run_init(self):
5555
self.__runner.runInitialize()
56+
self.__runner.setOutputStream()
5657

5758
def load_and_init(self):
5859
try:
@@ -84,6 +85,7 @@ def terminate(self):
8485
self.__runner.stop()
8586
self.__runner.cleanUpRun()
8687
self.__runner.cleanUpBatch()
88+
self.__gateway.shutdown()
8789

8890
def get_tick_count(self):
8991
return self.__gateway.jvm.repast.simphony.engine.environment.RunEnvironment \

src/runner/Comparator.py

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44

55

66
class Comparator(object):
7-
def __init__(self, compare_method="position"):
7+
def __init__(self, compare_method="position", k=2):
88
self.__method = compare_method
9-
self.__k = 2
9+
self.__k = k
1010

1111
def set_compare_method(self, compare_method):
1212
self.__method = compare_method
1313

14+
def set_k(self, k):
15+
self.__k = k
16+
1417
def compare(self, traces_pre, traces_real):
1518
"""
1619
Compare two input traces. A trace can be a list or a state.
@@ -34,8 +37,12 @@ def compare(self, traces_pre, traces_real):
3437
distances = self.__compare_action(traces_pre, traces_real)
3538
elif self.__method == "position":
3639
distances = self.__compare_position(traces_pre, traces_real)
40+
elif self.__method == "position_all":
41+
distances = self.__compare_position(traces_pre, traces_real, compare_all=True)
3742
elif self.__method == "interaction":
3843
distances = self.__compare_interaction(traces_pre, traces_real)
44+
elif self.__method == "utility":
45+
distances = self.__compare_utility(traces_pre, traces_real)
3946
else:
4047
exit(-1)
4148

@@ -51,14 +58,18 @@ def __compare_action(self, traces_1, traces_2):
5158
for ID in state_2["cameras"].keys():
5259
action_r = state_2["cameras"][ID]["actions"]
5360
action_p = state_1["cameras"][ID]["actions"]
54-
d += self.__action_deviation(action_p, action_r)
61+
d += self.__action_deviation_v2(action_p, action_r)
5562
distances.append(float(d)/len(state_2["cameras"]))
5663
return distances
5764

58-
def __compare_position(self, traces_pre, traces_real):
65+
def __compare_position(self, traces_pre, traces_real, compare_all=False):
5966
distances = []
60-
for state_pre, state_real in zip(traces_pre, traces_real):
61-
distances.append(self.__position_deviation(state_pre, state_real))
67+
if compare_all is False:
68+
for state_pre, state_real in zip(traces_pre, traces_real):
69+
distances.append(self.__position_deviation(state_pre, state_real))
70+
else:
71+
for state_pre, state_real in zip(traces_pre, traces_real):
72+
distances.append(self.__position_deviation_all(state_pre, state_real))
6273
return distances
6374

6475
def __compare_interaction(self, traces_1, traces_2):
@@ -67,6 +78,12 @@ def __compare_interaction(self, traces_1, traces_2):
6778
distances.append(self.__interaction_deviation(state_1["graph"], state_2["graph"]))
6879
return distances
6980

81+
def __compare_utility(self, traces_1, traces_2):
82+
distances = []
83+
for state_1, state_2 in zip(traces_1, traces_2):
84+
distances.append(self.__utility_deviation(state_1, state_2))
85+
return distances
86+
7087
def __action_deviation(self, action_1, action_2):
7188
if action_1["random"] == 1 and action_2["random"] == 1:
7289
return 0
@@ -94,6 +111,51 @@ def __action_deviation(self, action_1, action_2):
94111
else:
95112
return 1
96113

114+
115+
def __action_deviation_v2(self, action_1, action_2):
116+
# both random movement
117+
if action_1["random"] == 1 and action_2["random"] == 1:
118+
return 0
119+
# both responding to someone else (and follow)
120+
elif action_1["respond"] != -1 and action_2["respond"] != -1:
121+
return 0
122+
# both notifying someone else
123+
elif len(action_1["notify"]) != 0 and len(action_2["notify"]) != 0:
124+
return 0
125+
# both only following
126+
elif action_1["respond"] == -1 and action_2["respond"] == -1 and \
127+
len(action_1["notify"]) == 0 and len(action_2["notify"]) == 0:
128+
return 0
129+
# otherwise
130+
else:
131+
return 1
132+
133+
def __position_deviation_all(self, state_p, state_r): # p for predicted and r for real
134+
# store only the value [x,y] in the array, discarding id
135+
136+
# read all cameras
137+
cams_r_array = []
138+
cams_p_array = []
139+
for cam_id, cam in state_r["cameras"].items():
140+
cams_r_array.append([cam["x"], cam["y"]])
141+
cam_p = state_p["cameras"].get(cam_id)
142+
cams_p_array.append([cam_p["x"], cam_p["y"]])
143+
cams_p_array = np.array(cams_p_array)
144+
cams_r_array = np.array(cams_r_array)
145+
146+
# get all objects
147+
objs_r = Loader.get_all_objects(state_r)
148+
objs_p = Loader.get_all_objects(state_p)
149+
150+
sorted_objs_r = sorted(objs_r.items())
151+
objs_r_array = np.array([item[1] for item in sorted_objs_r])
152+
sorted_objs_p = sorted(objs_p.items())
153+
objs_p_array = np.array([item[1] for item in sorted_objs_p])
154+
155+
p_array = np.concatenate([objs_p_array, cams_p_array])
156+
r_array = np.concatenate([objs_r_array, cams_r_array])
157+
return np.linalg.norm(p_array - r_array)
158+
97159
def __position_deviation(self, state_p, state_r): # p for predicted and r for real
98160
# store only the value [x,y] in the array, discarding id
99161

@@ -141,6 +203,11 @@ def __interaction_deviation(self, graph_1: list, graph_2: list):
141203
strengths_2 = [edge['strength'] for edge in graph_2]
142204
return np.linalg.norm(np.array(strengths_1) - np.array(strengths_2))
143205

206+
def __utility_deviation(self, state_1, state_2):
207+
util_1 = self.calc_k_coverage_value(self.__k, state_1)
208+
util_2 = self.calc_k_coverage_value(self.__k, state_2)
209+
return np.linalg.norm(np.array(util_1) - np.array(util_2))
210+
144211
def calc_k_coverage_value(self, k, state):
145212
num_objs = Loader.get_num_objs(state)
146213
return len(self.filter_k_coverage(k, state)) / float(num_objs)
@@ -163,7 +230,3 @@ def calc_cov_for_objs(self, state):
163230
cov_objs += obj_list
164231
return dict(Counter(cov_objs))
165232

166-
def knowledge_distance(self, k, state_1, state_2):
167-
knowledge_1 = self.calc_k_coverage_value(k, state_1)
168-
knowledge_2 = self.calc_k_coverage_value(k, state_2)
169-
return np.linalg.norm(np.array(knowledge_1) - np.array(knowledge_2))

src/runner/Config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
class Config(object):
77
def __init__(self, path):
88
self.data = {}
9+
self.curr_time = ""
910
with open(path, 'r') as f:
1011
self.data = yaml.safe_load(f)
1112
if self.data["version"] != "1.0":
1213
self.data = {}
1314
else:
15+
self.curr_time = datetime.now().strftime('%Y-%m-%d--%H-%M-%S-%f')[:-3]
1416
self.__load()
1517

1618
def __load(self):
@@ -28,6 +30,5 @@ def __parse(self, name):
2830
new_string = self.data[name]
2931
for ma in matches:
3032
if ma.strip() == "time":
31-
curr_time = datetime.now().strftime('%Y-%m-%d--%H-%M-%S')
32-
new_string = re.sub("\${{.*?}}", curr_time, self.data[name])
33+
new_string = re.sub("\${{.*?}}", self.curr_time, self.data[name])
3334
return new_string

src/runner/Loader.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ def get_all_covered_objects(state):
1212
return cov_objs
1313

1414

15+
def get_all_objects(state):
16+
covered_objs = get_all_covered_objects(state)
17+
uncovered_objs = {}
18+
for obj_id, obj_content in state["objects"].items():
19+
uncovered_objs[str(obj_id)] = [obj_content["x"], obj_content["y"]]
20+
return covered_objs | uncovered_objs # merge two dictionaries, python 3.9 only.
21+
22+
1523
def get_num_cams(state):
1624
return len(state["cameras"])
1725

@@ -33,6 +41,20 @@ def __init__(self, trace_path):
3341
def set_trace_path(self, trace_path):
3442
self.__trace_path = trace_path
3543

44+
def read_trace(self, trace_raw=None):
45+
"""
46+
47+
:param trace_raw: raw trace as a string
48+
:return:
49+
"""
50+
if trace_raw is None:
51+
with open(self.__trace_path, 'r') as f:
52+
tmp_traces = self._preprocess_trace(f)
53+
return [self._parse_state_for_each_timestep(_state) for _state in tmp_traces]
54+
else:
55+
tmp_traces = self._preprocess_trace(io.StringIO(trace_raw))
56+
return [self._parse_state_for_each_timestep(_state) for _state in tmp_traces]
57+
3658
def _parse_state_for_each_timestep(self, trace):
3759
"""
3860
:param trace:
@@ -124,15 +146,6 @@ def _parse_state_for_each_timestep(self, trace):
124146
duration=int(uncovered_obj_xml[5]))
125147
return state
126148

127-
def read_trace(self, my_string=None):
128-
if my_string is None:
129-
with open(self.__trace_path, 'r') as f:
130-
tmp_traces = self._preprocess_trace(f)
131-
return [self._parse_state_for_each_timestep(_state) for _state in tmp_traces]
132-
else:
133-
tmp_traces = self._preprocess_trace(io.StringIO(my_string))
134-
return [self._parse_state_for_each_timestep(_state) for _state in tmp_traces]
135-
136149
def _preprocess_trace(self, f):
137150
lines = f.readlines()
138151

0 commit comments

Comments
 (0)