gr-libs 0.1.8__py3-none-any.whl → 0.2.5__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. gr_libs/__init__.py +3 -1
  2. gr_libs/_version.py +2 -2
  3. gr_libs/all_experiments.py +260 -0
  4. gr_libs/environment/__init__.py +14 -1
  5. gr_libs/environment/_utils/__init__.py +0 -0
  6. gr_libs/environment/{utils → _utils}/utils.py +1 -1
  7. gr_libs/environment/environment.py +278 -23
  8. gr_libs/evaluation/__init__.py +1 -0
  9. gr_libs/evaluation/generate_experiments_results.py +100 -0
  10. gr_libs/metrics/__init__.py +2 -0
  11. gr_libs/metrics/metrics.py +166 -31
  12. gr_libs/ml/__init__.py +1 -6
  13. gr_libs/ml/base/__init__.py +3 -1
  14. gr_libs/ml/base/rl_agent.py +68 -3
  15. gr_libs/ml/neural/__init__.py +1 -3
  16. gr_libs/ml/neural/deep_rl_learner.py +241 -84
  17. gr_libs/ml/neural/utils/__init__.py +1 -2
  18. gr_libs/ml/planner/mcts/{utils → _utils}/tree.py +1 -1
  19. gr_libs/ml/planner/mcts/mcts_model.py +71 -34
  20. gr_libs/ml/sequential/__init__.py +0 -1
  21. gr_libs/ml/sequential/{lstm_model.py → _lstm_model.py} +11 -14
  22. gr_libs/ml/tabular/__init__.py +1 -3
  23. gr_libs/ml/tabular/tabular_q_learner.py +27 -9
  24. gr_libs/ml/tabular/tabular_rl_agent.py +22 -9
  25. gr_libs/ml/utils/__init__.py +2 -9
  26. gr_libs/ml/utils/format.py +13 -90
  27. gr_libs/ml/utils/math.py +3 -2
  28. gr_libs/ml/utils/other.py +2 -2
  29. gr_libs/ml/utils/storage.py +41 -94
  30. gr_libs/odgr_executor.py +263 -0
  31. gr_libs/problems/consts.py +570 -292
  32. gr_libs/recognizer/{utils → _utils}/format.py +2 -2
  33. gr_libs/recognizer/gr_as_rl/gr_as_rl_recognizer.py +127 -36
  34. gr_libs/recognizer/graml/{gr_dataset.py → _gr_dataset.py} +11 -11
  35. gr_libs/recognizer/graml/graml_recognizer.py +186 -35
  36. gr_libs/recognizer/recognizer.py +59 -10
  37. gr_libs/tutorials/draco_panda_tutorial.py +58 -0
  38. gr_libs/tutorials/draco_parking_tutorial.py +56 -0
  39. {tutorials → gr_libs/tutorials}/gcdraco_panda_tutorial.py +11 -11
  40. {tutorials → gr_libs/tutorials}/gcdraco_parking_tutorial.py +6 -8
  41. {tutorials → gr_libs/tutorials}/graml_minigrid_tutorial.py +18 -14
  42. {tutorials → gr_libs/tutorials}/graml_panda_tutorial.py +11 -12
  43. {tutorials → gr_libs/tutorials}/graml_parking_tutorial.py +8 -10
  44. {tutorials → gr_libs/tutorials}/graml_point_maze_tutorial.py +17 -3
  45. {tutorials → gr_libs/tutorials}/graql_minigrid_tutorial.py +2 -2
  46. {gr_libs-0.1.8.dist-info → gr_libs-0.2.5.dist-info}/METADATA +95 -29
  47. gr_libs-0.2.5.dist-info/RECORD +72 -0
  48. {gr_libs-0.1.8.dist-info → gr_libs-0.2.5.dist-info}/WHEEL +1 -1
  49. gr_libs-0.2.5.dist-info/top_level.txt +2 -0
  50. tests/test_draco.py +14 -0
  51. tests/test_gcdraco.py +2 -2
  52. tests/test_graml.py +4 -4
  53. tests/test_graql.py +1 -1
  54. tests/test_odgr_executor_expertbasedgraml.py +14 -0
  55. tests/test_odgr_executor_gcdraco.py +14 -0
  56. tests/test_odgr_executor_gcgraml.py +14 -0
  57. tests/test_odgr_executor_graql.py +14 -0
  58. evaluation/analyze_results_cross_alg_cross_domain.py +0 -267
  59. evaluation/create_minigrid_map_image.py +0 -38
  60. evaluation/file_system.py +0 -53
  61. evaluation/generate_experiments_results.py +0 -141
  62. evaluation/generate_experiments_results_new_ver1.py +0 -238
  63. evaluation/generate_experiments_results_new_ver2.py +0 -331
  64. evaluation/generate_task_specific_statistics_plots.py +0 -500
  65. evaluation/get_plans_images.py +0 -62
  66. evaluation/increasing_and_decreasing_.py +0 -104
  67. gr_libs/ml/neural/utils/penv.py +0 -60
  68. gr_libs-0.1.8.dist-info/RECORD +0 -70
  69. gr_libs-0.1.8.dist-info/top_level.txt +0 -4
  70. /gr_libs/{environment/utils/__init__.py → _evaluation/_generate_experiments_results.py} +0 -0
  71. /gr_libs/ml/planner/mcts/{utils → _utils}/__init__.py +0 -0
  72. /gr_libs/ml/planner/mcts/{utils → _utils}/node.py +0 -0
  73. /gr_libs/recognizer/{utils → _utils}/__init__.py +0 -0
@@ -1,500 +0,0 @@
1
- import argparse
2
- import sys
3
- import matplotlib.pyplot as plt
4
- import numpy as np
5
- import os
6
- import ast
7
- import inspect
8
- import torch
9
- import dill
10
-
11
- from gr_libs.ml.utils import get_embeddings_result_path
12
- from gr_libs.ml.utils.storage import (
13
- get_experiment_results_path,
14
- set_global_storage_configs,
15
- get_graql_experiment_confidence_path,
16
- )
17
- from gr_libs.metrics.metrics import measure_average_sequence_distance
18
-
19
-
20
- def get_tasks_embeddings_dir_path(env_name):
21
- return os.path.join("../gr_libs", get_embeddings_result_path(env_name))
22
-
23
-
24
- def get_figures_dir_path(domain_name, env_name):
25
- return os.path.join("../gr_libs", "figures", domain_name, env_name)
26
-
27
-
28
- def similarities_vector_to_std_deviation_units_vector(
29
- ref_dict: dict, relative_to_largest
30
- ):
31
- """
32
- Calculate the number of standard deviation units every other element is
33
- from the largest/smallest element in the vector.
34
-
35
- Parameters:
36
- - vector: list or numpy array of numbers.
37
- - relative_to_largest: boolean, if True, measure in relation to the largest element,
38
- if False, measure in relation to the smallest element.
39
-
40
- Returns:
41
- - List of number of standard deviation units for each element in the vector.
42
- """
43
- vector = np.array(list(ref_dict.values()))
44
- mean = np.mean(vector) # for the future maybe another method for measurement
45
- std_dev = np.std(vector)
46
-
47
- # Determine the reference element (largest or smallest)
48
- if relative_to_largest:
49
- reference_value = np.max(vector)
50
- else:
51
- reference_value = np.min(vector)
52
- for goal, value in ref_dict.items():
53
- ref_dict[goal] = abs(value - reference_value) / std_dev
54
- return ref_dict
55
-
56
-
57
- def analyze_and_produce_plots(
58
- recognizer_type: str,
59
- domain_name: str,
60
- env_name: str,
61
- fragmented_status: str,
62
- inf_same_length_status: str,
63
- learn_same_length_status: str,
64
- ):
65
- if recognizer_type == "graml":
66
- assert os.path.exists(
67
- get_embeddings_result_path(domain_name)
68
- ), "Embeddings weren't made for this environment, run graml_main.py with this environment first."
69
- tasks_embedding_dicts = {}
70
- tasks_plans_dict = {}
71
- goals_similarity_dict = {}
72
- plans_similarity_dict = {}
73
-
74
- embeddings_dir_path = get_tasks_embeddings_dir_path(domain_name)
75
- for embeddings_file_name in [
76
- filename
77
- for filename in os.listdir(embeddings_dir_path)
78
- if "embeddings" in filename
79
- ]:
80
- with open(
81
- os.path.join(embeddings_dir_path, embeddings_file_name), "rb"
82
- ) as emb_file:
83
- splitted_name = embeddings_file_name.split("_")
84
- goal, percentage = splitted_name[0], splitted_name[1]
85
- with open(
86
- os.path.join(
87
- embeddings_dir_path, f"{goal}_{percentage}_plans_dict.pkl"
88
- ),
89
- "rb",
90
- ) as plan_file:
91
- tasks_plans_dict[f"{goal}_{percentage}"] = dill.load(plan_file)
92
- tasks_embedding_dicts[f"{goal}_{percentage}"] = dill.load(emb_file)
93
-
94
- for goal_percentage, embedding_dict in tasks_embedding_dicts.items():
95
- goal, percentage = goal_percentage.split("_")
96
- similarities = {
97
- dynamic_goal: []
98
- for dynamic_goal in embedding_dict.keys()
99
- if "true" not in dynamic_goal
100
- }
101
- real_goal_embedding = embedding_dict[f"{goal}_true"]
102
- for dynamic_goal, goal_embedding in embedding_dict.items():
103
- if "true" in dynamic_goal:
104
- continue
105
- curr_similarity = torch.exp(
106
- -torch.sum(torch.abs(goal_embedding - real_goal_embedding))
107
- )
108
- similarities[dynamic_goal] = curr_similarity.item()
109
- if goal not in goals_similarity_dict.keys():
110
- goals_similarity_dict[goal] = {}
111
- goals_similarity_dict[goal][percentage] = (
112
- similarities_vector_to_std_deviation_units_vector(
113
- ref_dict=similarities, relative_to_largest=True
114
- )
115
- )
116
-
117
- for goal_percentage, plans_dict in tasks_plans_dict.items():
118
- goal, percentage = goal_percentage.split("_")
119
- real_plan = plans_dict[f"{goal}_true"]
120
- sequence_similarities = {
121
- d_goal: measure_average_sequence_distance(real_plan, plan)
122
- for d_goal, plan in plans_dict.items()
123
- if "true" not in d_goal
124
- } # aps = agent plan sequence?
125
- if goal not in plans_similarity_dict.keys():
126
- plans_similarity_dict[goal] = {}
127
- plans_similarity_dict[goal][percentage] = (
128
- similarities_vector_to_std_deviation_units_vector(
129
- ref_dict=sequence_similarities, relative_to_largest=False
130
- )
131
- )
132
-
133
- goals = list(goals_similarity_dict.keys())
134
- percentages = sorted(
135
- set(
136
- percentage
137
- for similarities in goals_similarity_dict.values()
138
- for percentage in similarities.keys()
139
- )
140
- )
141
- num_percentages = len(percentages)
142
- fig_string = f"{recognizer_type}_{domain_name}_{env_name}_{fragmented_status}_{inf_same_length_status}_{learn_same_length_status}"
143
-
144
- else: # algorithm = "graql"
145
- assert os.path.exists(
146
- get_graql_experiment_confidence_path(domain_name)
147
- ), "Embeddings weren't made for this environment, run graml_main.py with this environment first."
148
- tasks_scores_dict = {}
149
- goals_similarity_dict = {}
150
- experiments_dir_path = get_graql_experiment_confidence_path(domain_name)
151
- for experiments_file_name in os.listdir(experiments_dir_path):
152
- with open(
153
- os.path.join(experiments_dir_path, experiments_file_name), "rb"
154
- ) as exp_file:
155
- splitted_name = experiments_file_name.split("_")
156
- goal, percentage = splitted_name[1], splitted_name[2]
157
- tasks_scores_dict[f"{goal}_{percentage}"] = dill.load(exp_file)
158
-
159
- for goal_percentage, scores_list in tasks_scores_dict.items():
160
- goal, percentage = goal_percentage.split("_")
161
- similarities = {
162
- dynamic_goal: score for (dynamic_goal, score) in scores_list
163
- }
164
- if goal not in goals_similarity_dict.keys():
165
- goals_similarity_dict[goal] = {}
166
- goals_similarity_dict[goal][percentage] = (
167
- similarities_vector_to_std_deviation_units_vector(
168
- ref_dict=similarities, relative_to_largest=False
169
- )
170
- )
171
-
172
- goals = list(goals_similarity_dict.keys())
173
- percentages = sorted(
174
- set(
175
- percentage
176
- for similarities in goals_similarity_dict.values()
177
- for percentage in similarities.keys()
178
- )
179
- )
180
- num_percentages = len(percentages)
181
- fig_string = f"{recognizer_type}_{domain_name}_{env_name}_{fragmented_status}"
182
-
183
- # -------------------- Start of Confusion Matrix Code --------------------
184
- # Initialize matrices of size len(goals) x len(goals)
185
- confusion_matrix_goals, confusion_matrix_plans = np.zeros(
186
- (len(goals), len(goals))
187
- ), np.zeros((len(goals), len(goals)))
188
-
189
- # if domain_name == 'point_maze' and args.task == 'L555':
190
- # if env_name == 'obstacles':
191
- # goals = ['(4, 7)', '(3, 6)', '(5, 5)', '(8, 8)', '(6, 3)', '(7, 4)']
192
- # else: # if env_name is 'four_rooms'
193
- # goals = ['(2, 8)', '(3, 7)', '(3, 4)', '(4, 4)', '(4, 3)', '(7, 3)', '(8, 2)']
194
-
195
- # Populate confusion matrix with similarity values for goals
196
- for i, true_goal in enumerate(goals):
197
- for j, dynamic_goal in enumerate(goals):
198
- percentage = percentages[-3]
199
- confusion_matrix_goals[i, j] = goals_similarity_dict[true_goal][
200
- percentage
201
- ].get(dynamic_goal, 0)
202
-
203
- if plans_similarity_dict:
204
- # Populate confusion matrix with similarity values for plans
205
- for i, true_goal in enumerate(goals):
206
- for j, dynamic_goal in enumerate(goals):
207
- percentage = percentages[-1]
208
- confusion_matrix_plans[i, j] = plans_similarity_dict[true_goal][
209
- percentage
210
- ].get(dynamic_goal, 0)
211
-
212
- # Create the figure and subplots for the unified display
213
- fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 6), sharex=True)
214
-
215
- # Plot for goal similarities
216
- im1 = ax1.imshow(confusion_matrix_goals, cmap="Blues", interpolation="nearest")
217
- cbar1 = fig.colorbar(im1, ax=ax1, fraction=0.046, pad=0.04)
218
- cbar1.set_label("St. dev from most probable goal", fontsize=18)
219
- ax1.set_title("Embeddings", fontsize=22, pad=20)
220
- ax1.set_xticks(np.arange(len(goals)))
221
- ax1.set_xticklabels(goals, rotation=45, ha="right", fontsize=16)
222
- ax1.set_yticks(np.arange(len(goals)))
223
- ax1.set_yticklabels(goals, fontsize=16) # y-tick labels for ax1
224
-
225
- # Plot for plan similarities
226
- im2 = ax2.imshow(confusion_matrix_plans, cmap="Greens", interpolation="nearest")
227
- cbar2 = fig.colorbar(im2, ax=ax2, fraction=0.046, pad=0.04)
228
- cbar2.set_label("Distance between plans", fontsize=18)
229
- ax2.set_title("Sequences", fontsize=22, pad=20)
230
- ax2.set_xticks(np.arange(len(goals)))
231
- ax2.set_xticklabels(goals, rotation=45, ha="right", fontsize=16)
232
- ax2.set_yticks(np.arange(len(goals))) # y-ticks for ax2 explicitly
233
- ax2.set_yticklabels(goals, fontsize=16) # y-tick labels for ax2
234
-
235
- # Adjust the figure layout to reduce overlap
236
- plt.subplots_adjust(left=0.15, right=0.9, bottom=0.25, top=0.85, wspace=0.1)
237
-
238
- # Unified axis labels, placed closer to the left
239
- fig.text(0.57, 0.07, "Goals Adaptation Phase", ha="center", fontsize=22)
240
- fig.text(
241
- 0.12, 0.5, "Inference Phase", va="center", rotation="vertical", fontsize=22
242
- )
243
-
244
- # Save the combined plot
245
- fig_dir = get_figures_dir_path(domain_name=domain_name, env_name=env_name)
246
- if not os.path.exists(fig_dir):
247
- os.makedirs(fig_dir)
248
- confusion_matrix_combined_path = os.path.join(
249
- fig_dir, f"{fig_string}_combined_conf_mat.png"
250
- )
251
- plt.savefig(confusion_matrix_combined_path, dpi=300)
252
- print(
253
- f"Combined confusion matrix figure saved at: {confusion_matrix_combined_path}"
254
- )
255
-
256
- # -------------------- End of Confusion Matrix Code --------------------
257
- fig, axes = plt.subplots(
258
- nrows=num_percentages, ncols=1, figsize=(10, 6 * num_percentages)
259
- )
260
-
261
- if num_percentages == 1:
262
- axes = [axes]
263
-
264
- for i, percentage in enumerate(percentages):
265
- correct_tasks, tasks_num = 0, 0
266
- ax = axes[i]
267
- dynamic_goals = list(
268
- next(iter(goals_similarity_dict.values()))[percentage].keys()
269
- )
270
- num_goals = len(goals)
271
- num_dynamic_goals = len(dynamic_goals)
272
- bar_width = 0.8 / num_dynamic_goals
273
- bar_positions = np.arange(num_goals)
274
-
275
- if recognizer_type == "graml":
276
- for j, dynamic_goal in enumerate(dynamic_goals):
277
- goal_similarities = [
278
- goals_similarity_dict[goal][percentage][dynamic_goal] + 0.04
279
- for goal in goals
280
- ]
281
- plan_similarities = [
282
- plans_similarity_dict[goal][percentage][dynamic_goal] + 0.04
283
- for goal in goals
284
- ]
285
- ax.bar(
286
- bar_positions + j * bar_width,
287
- goal_similarities,
288
- bar_width / 2,
289
- label=f"embedding of {dynamic_goal}",
290
- )
291
- ax.bar(
292
- bar_positions + j * bar_width + bar_width / 2,
293
- plan_similarities,
294
- bar_width / 2,
295
- label=f"plan to {dynamic_goal}",
296
- )
297
- else:
298
- for j, dynamic_goal in enumerate(dynamic_goals):
299
- goal_similarities = [
300
- goals_similarity_dict[goal][percentage][dynamic_goal] + 0.04
301
- for goal in goals
302
- ]
303
- ax.bar(
304
- bar_positions + j * bar_width,
305
- goal_similarities,
306
- bar_width,
307
- label=f"policy to {dynamic_goal}",
308
- )
309
-
310
- x_labels = []
311
- for true_goal in goals:
312
- guessed_goal = min(
313
- goals_similarity_dict[true_goal][percentage],
314
- key=goals_similarity_dict[true_goal][percentage].get,
315
- )
316
- tasks_num += 1
317
- if true_goal == guessed_goal:
318
- correct_tasks += 1
319
- second_lowest_value = sorted(
320
- goals_similarity_dict[true_goal][percentage].values()
321
- )[1]
322
- confidence_level = abs(
323
- goals_similarity_dict[true_goal][percentage][guessed_goal]
324
- - second_lowest_value
325
- )
326
- label = f"True: {true_goal}\nGuessed: {guessed_goal}\nConfidence: {confidence_level:.2f}"
327
- x_labels.append(label)
328
-
329
- ax.set_ylabel("Distance (units in st. deviations)", fontsize=10)
330
- ax.set_title(
331
- f"Confidence level for {domain_name}, {env_name}, {fragmented_status}. Accuracy: {correct_tasks / tasks_num}",
332
- fontsize=12,
333
- )
334
- ax.set_xticks(bar_positions + bar_width * (num_dynamic_goals - 1) / 2)
335
- ax.set_xticklabels(x_labels, fontsize=8)
336
- ax.legend()
337
-
338
- fig_path = os.path.join(fig_dir, f"{fig_string}_stats.png")
339
- fig.savefig(fig_path)
340
- print(f"general figure saved at: {fig_path}")
341
-
342
-
343
- def parse_args():
344
- parser = argparse.ArgumentParser(
345
- description="Parse command-line arguments for the RL experiment.",
346
- formatter_class=argparse.RawTextHelpFormatter,
347
- )
348
-
349
- # Required arguments
350
- required_group = parser.add_argument_group("Required arguments")
351
- required_group.add_argument(
352
- "--domain",
353
- choices=["point_maze", "minigrid", "parking", "franka_kitchen", "panda"],
354
- required=True,
355
- help="Domain type (point_maze, minigrid, parking, or franka_kitchen)",
356
- )
357
- required_group.add_argument(
358
- "--recognizer",
359
- choices=["graml", "graql", "draco"],
360
- required=True,
361
- help="Recognizer type (graml, graql, draco). graql only for discrete domains.",
362
- )
363
- required_group.add_argument(
364
- "--task",
365
- choices=[
366
- "L1",
367
- "L2",
368
- "L3",
369
- "L4",
370
- "L5",
371
- "L11",
372
- "L22",
373
- "L33",
374
- "L44",
375
- "L55",
376
- "L111",
377
- "L222",
378
- "L333",
379
- "L444",
380
- "L555",
381
- ],
382
- required=True,
383
- help="Task identifier (e.g., L1, L2,...,L5)",
384
- )
385
- required_group.add_argument(
386
- "--partial_obs_type",
387
- required=True,
388
- choices=["fragmented", "continuing"],
389
- help="Give fragmented or continuing partial observations for inference phase inputs.",
390
- )
391
-
392
- # Optional arguments
393
- optional_group = parser.add_argument_group("Optional arguments")
394
- optional_group.add_argument(
395
- "--minigrid_env",
396
- choices=["four_rooms", "obstacles"],
397
- help="Minigrid environment (four_rooms or obstacles)",
398
- )
399
- optional_group.add_argument(
400
- "--parking_env",
401
- choices=["gd_agent", "gc_agent"],
402
- help="Parking environment (agent or gc_agent)",
403
- )
404
- optional_group.add_argument(
405
- "--point_maze_env",
406
- choices=["obstacles", "four_rooms"],
407
- help="Parking environment (agent or gc_agent)",
408
- )
409
- optional_group.add_argument(
410
- "--franka_env",
411
- choices=["comb1", "comb2"],
412
- help="Franka Kitchen environment (comb1 or comb2)",
413
- )
414
- optional_group.add_argument(
415
- "--panda_env",
416
- choices=["gc_agent", "gd_agent"],
417
- help="Panda Robotics environment (gc_agent or gd_agent)",
418
- )
419
- optional_group.add_argument(
420
- "--learn_same_seq_len",
421
- action="store_true",
422
- help="Learn with the same sequence length",
423
- )
424
- optional_group.add_argument(
425
- "--inference_same_seq_len",
426
- action="store_true",
427
- help="Infer with the same sequence length",
428
- )
429
-
430
- args = parser.parse_args()
431
-
432
- ### VALIDATE INPUTS ###
433
- # Assert that all required arguments are provided
434
- assert (
435
- args.domain is not None
436
- and args.recognizer is not None
437
- and args.task is not None
438
- ), "Missing required arguments: domain, recognizer, or task"
439
-
440
- # Validate the combination of domain and environment
441
- if args.domain == "minigrid" and args.minigrid_env is None:
442
- parser.error(
443
- "Missing required argument: --minigrid_env must be provided when --domain is minigrid"
444
- )
445
- elif args.domain == "parking" and args.parking_env is None:
446
- parser.error(
447
- "Missing required argument: --parking_env must be provided when --domain is parking"
448
- )
449
- elif args.domain == "point_maze" and args.point_maze_env is None:
450
- parser.error(
451
- "Missing required argument: --point_maze_env must be provided when --domain is point_maze"
452
- )
453
- elif args.domain == "franka_kitchen" and args.franka_env is None:
454
- parser.error(
455
- "Missing required argument: --franka_env must be provided when --domain is franka_kitchen"
456
- )
457
-
458
- if args.recognizer != "graml":
459
- if args.learn_same_seq_len == True:
460
- parser.error("learn_same_seq_len is only relevant for graml.")
461
- if args.inference_same_seq_len == True:
462
- parser.error("inference_same_seq_len is only relevant for graml.")
463
-
464
- return args
465
-
466
-
467
- if __name__ == "__main__":
468
- args = parse_args()
469
- set_global_storage_configs(
470
- recognizer_str=args.recognizer,
471
- is_fragmented=args.partial_obs_type,
472
- is_inference_same_length_sequences=args.inference_same_seq_len,
473
- is_learn_same_length_sequences=args.learn_same_seq_len,
474
- )
475
- (env_name,) = [
476
- x
477
- for x in [
478
- args.minigrid_env,
479
- args.parking_env,
480
- args.point_maze_env,
481
- args.franka_env,
482
- ]
483
- if isinstance(x, str)
484
- ]
485
- if args.inference_same_seq_len:
486
- inference_same_seq_len = "inference_same_seq_len"
487
- else:
488
- inference_same_seq_len = "inference_diff_seq_len"
489
- if args.learn_same_seq_len:
490
- learn_same_seq_len = "learn_same_seq_len"
491
- else:
492
- learn_same_seq_len = "learn_diff_seq_len"
493
- analyze_and_produce_plots(
494
- args.recognizer,
495
- domain_name=args.domain,
496
- env_name=env_name,
497
- fragmented_status=args.partial_obs_type,
498
- inf_same_length_status=inference_same_seq_len,
499
- learn_same_length_status=learn_same_seq_len,
500
- )
@@ -1,62 +0,0 @@
1
- import sys
2
- import os
3
- import pickle
4
- import inspect
5
-
6
-
7
- currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
8
- GRAML_itself = os.path.dirname(currentdir)
9
- GRAML_includer = os.path.dirname(os.path.dirname(currentdir))
10
- sys.path.insert(0, GRAML_includer)
11
- sys.path.insert(0, GRAML_itself)
12
-
13
-
14
- def get_plans_result_path(env_name):
15
- return os.path.join("dataset", (env_name), "plans")
16
-
17
-
18
- def get_policy_sequences_result_path(env_name):
19
- return os.path.join("dataset", (env_name), "policy_sequences")
20
-
21
-
22
- # TODO: instead of loading the model and having it produce the sequence again, just save the sequence from the framework run, and have this script accept the whole path (including is_fragmented etc.)
23
- def analyze_and_produce_images(env_name):
24
- models_dir = get_models_dir(env_name=env_name)
25
- for dirname in os.listdir(models_dir):
26
- if dirname.startswith("MiniGrid"):
27
- model_dir = get_model_dir(
28
- env_name=env_name, model_name=dirname, class_name="MCTS"
29
- )
30
- model_file_path = os.path.join(model_dir, "mcts_model.pth")
31
- try:
32
- with open(model_file_path, "rb") as file: # Load the pre-existing model
33
- monteCarloTreeSearch = pickle.load(file)
34
- full_plan = monteCarloTreeSearch.generate_full_policy_sequence()
35
- plan = [pos for ((state, pos), action) in full_plan]
36
- plans_result_path = get_plans_result_path(env_name)
37
- if not os.path.exists(plans_result_path):
38
- os.makedirs(plans_result_path)
39
- img_path = os.path.join(get_plans_result_path(env_name), dirname)
40
- print(
41
- f"plan to {dirname} is:\n\t{plan}\ngenerating image at {img_path}."
42
- )
43
- create_sequence_image(plan, img_path, dirname)
44
-
45
- except FileNotFoundError as e:
46
- print(
47
- f"Warning: {e.filename} doesn't exist. It's probably a base goal, not generating policy sequence for it."
48
- )
49
-
50
-
51
- if __name__ == "__main__":
52
- # preventing circular imports. only needed for running this as main anyway.
53
- from gr_libs.ml.utils.storage import get_models_dir, get_model_dir
54
-
55
- # checks:
56
- assert (
57
- len(sys.argv) == 2
58
- ), f"Assertion failed: len(sys.argv) is {len(sys.argv)} while it needs to be 2.\n Example: \n\t /usr/bin/python scripts/get_plans_images.py MiniGrid-Walls-13x13-v0"
59
- assert os.path.exists(
60
- get_models_dir(sys.argv[1])
61
- ), "plans weren't made for this environment, run graml_main.py with this environment first."
62
- analyze_and_produce_images(sys.argv[1])
@@ -1,104 +0,0 @@
1
- import os
2
- import dill
3
- import numpy as np
4
- import matplotlib.pyplot as plt
5
- from gr_libs.ml.utils.storage import (
6
- get_experiment_results_path,
7
- set_global_storage_configs,
8
- )
9
-
10
- if __name__ == "__main__":
11
-
12
- # Define the tasks and percentages
13
- increasing_base_goals = ["L1", "L2", "L3", "L4", "L5"]
14
- increasing_dynamic_goals = ["L111", "L222", "L555", "L333", "L444"]
15
- percentages = ["0.3", "0.5", "0.7", "0.9", "1"]
16
-
17
- # Prepare a dictionary to hold accuracy data
18
- accuracies = {
19
- task: {perc: [] for perc in percentages}
20
- for task in increasing_base_goals + increasing_dynamic_goals
21
- }
22
-
23
- # Collect data for both sets of goals
24
- for task in increasing_base_goals + increasing_dynamic_goals:
25
- set_global_storage_configs(
26
- recognizer_str="graml",
27
- is_fragmented="fragmented",
28
- is_inference_same_length_sequences=True,
29
- is_learn_same_length_sequences=False,
30
- )
31
- res_file_path = (
32
- f'{get_experiment_results_path("parking", "gd_agent", task)}.pkl'
33
- )
34
-
35
- if os.path.exists(res_file_path):
36
- with open(res_file_path, "rb") as results_file:
37
- results = dill.load(results_file)
38
- for percentage in percentages:
39
- accuracies[task][percentage].append(results[percentage]["accuracy"])
40
- else:
41
- print(f"Warning: no file for {res_file_path}")
42
-
43
- # Create the figure with two subplots
44
- fig, axes = plt.subplots(1, 2, figsize=(12, 6))
45
-
46
- # Bar plot function
47
- def plot_accuracies(ax, task_set, title, type):
48
- """Plot accuracies for a given set of tasks on the provided axis."""
49
- x_vals = np.arange(len(task_set)) # X-axis positions for the number of goals
50
- bar_width = 0.15 # Width of each bar
51
- for i, perc in enumerate(["0.3", "0.5", "1"]):
52
- if perc == "1":
53
- y_vals = [
54
- max(
55
- [
56
- accuracies[task]["0.5"][0],
57
- accuracies[task]["0.7"][0],
58
- accuracies[task]["0.9"][0],
59
- accuracies[task]["1"][0],
60
- ]
61
- )
62
- for task in task_set
63
- ] # Get mean accuracies
64
- else:
65
- y_vals = [
66
- accuracies[task][perc][0] for task in task_set
67
- ] # Get mean accuracies
68
- if type != "base":
69
- ax.bar(
70
- x_vals + i * bar_width,
71
- y_vals,
72
- width=bar_width,
73
- label=f"Percentage {perc}",
74
- )
75
- else:
76
- ax.bar(x_vals + i * bar_width, y_vals, width=bar_width)
77
- ax.set_xticks(x_vals + bar_width) # Center x-ticks
78
- ax.set_xticklabels(
79
- [i + 3 for i in range(len(task_set))], fontsize=16
80
- ) # Set custom x-tick labels
81
- ax.set_yticks(np.linspace(0, 1, 6))
82
- ax.set_ylim([0, 1])
83
- ax.set_title(title, fontsize=20)
84
- ax.set_xlabel(f"Number of {type} Goals", fontsize=20)
85
- if type == "base":
86
- ax.set_ylabel("Accuracy", fontsize=22)
87
- ax.legend()
88
-
89
- # Plot for increasing base goals
90
- plot_accuracies(axes[0], increasing_base_goals, "Increasing Base Goals", "base")
91
-
92
- # Plot for increasing dynamic goals
93
- plot_accuracies(
94
- axes[1], increasing_dynamic_goals, "Increasing Active Goals", "active"
95
- )
96
- plt.subplots_adjust(
97
- left=0.1, right=0.9, top=0.9, bottom=0.1, wspace=0.3, hspace=0.3
98
- )
99
- # Adjust layout and save the figure
100
- plt.tight_layout()
101
- plt.savefig(
102
- "increasing_goals_plot_bars.png", dpi=300
103
- ) # Save the figure as a PNG file
104
- print("Figure saved at: increasing_goals_plot_bars.png")