-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathevaluation_controller.py
More file actions
1147 lines (1005 loc) · 50.8 KB
/
evaluation_controller.py
File metadata and controls
1147 lines (1005 loc) · 50.8 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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
from typing import Dict, List, Tuple, Optional, Any, Union
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler())
import grpc
from concurrent import futures
import time
from generated.mara import mara_environment_pb2 as env_pb2
from generated.mara import mara_environment_service_pb2 as env_service_pb2
from generated.mara import mara_environment_service_pb2_grpc as env_grpc
from generated.mara import mara_agent_pb2 as agent_pb2
from generated.mara import mara_agent_pb2_grpc as agent_grpc
from generated.mara import mara_evaluation_controller_pb2 as controller_pb2
from generated.mara import mara_evaluation_controller_pb2_grpc as controller_grpc
from .environment_interfaces import MARACompositeAutumnChangeDetectionServicer, MARACompositeAutumnPlanningServicer
from .environment_interfaces_mfp import MARACompositeAutumnMFPServicer
from .agent import MARARandomAgentServicer
from .llm_agent import ReactLLMAgentServicer, ReactLLMAgent2, SummaryReactLLMAgent, ReactVLMAgent, UnifiedReactAgent
from .simple_wm_agent import SimpleWMAgentServicer
from generated.mara import mara_environment_service_pb2 as env_service_pb2
from generated.mara import mara_environment_service_pb2_grpc as env_grpc
from concurrent import futures
class EvaluationControllerNoServer:
def __init__(self) -> None:
self.environments: Dict[str, Optional[Any]] = {}
self.transitions: Dict[str, Dict[str, str]] = {}
self.transition_messages: Dict[Any, Any] = {
} # Used both as nested dict and tuple keys
self.configs: Dict[str, Dict[str, str]] = {}
self.current_environment: Optional[str] = None
self.environment_sequence: List[str] = []
self.environment_rewards: Dict[str, Dict[str, Any]] = {}
self.aggregate_reward: float = 0.0
self.evaluation_complete: bool = False
self.environment_ids: List[str] = []
def initialize(self, environment_ids: List[str], transitions: List[Any],
agent_id: str, config: Dict[str, Any]) -> Tuple[bool, str]:
print("Initializing controller")
print(f"Environment IDs: {environment_ids}")
self.environment_ids = environment_ids
self.environments = {
**{
f"{eid}_interactive_{config['task_name']}": None
for eid in environment_ids
},
}
self.transitions = {
**{
f"{environment_ids[eid]}_interactive_{config['task_name']}": {
"default":
f"{environment_ids[eid + 1]}_interactive_{config['task_name']}",
"quit":
f"{environment_ids[eid + 1]}_interactive_{config['task_name']}",
"finish":
f"{environment_ids[eid + 1]}_interactive_{config['task_name']}",
"fail":
f"{environment_ids[eid + 1]}_interactive_{config['task_name']}",
}
for eid in range(len(environment_ids) - 1)
},
}
self.transition_messages = {
**{
f"{environment_ids[eid]}_interactive_{config['task_name']}": {
"default":
f"{environment_ids[eid + 1]}_interactive_{config['task_name']}",
"quit":
f"{environment_ids[eid + 1]}_interactive_{config['task_name']}",
"finish":
f"{environment_ids[eid + 1]}_interactive_{config['task_name']}",
"fail":
f"{environment_ids[eid + 1]}_interactive_{config['task_name']}",
}
for eid in range(len(environment_ids) - 1)
},
}
self.agents = {
"autumn_random_interactive_agent_v1": MARARandomAgentServicer,
"autumn_llm_interactive_agent_v1": ReactLLMAgentServicer,
"autumn_llm_interactive_agent_v2": ReactLLMAgent2,
"autumn_llm_summary_interactive_agent_v1": SummaryReactLLMAgent,
"autumn_llm_image_interactive_agent_v1": ReactVLMAgent,
"autumn_llm_unified_interactive_agent_v1": UnifiedReactAgent,
"autumn_simple_wm_agent": SimpleWMAgentServicer,
}
# Initialize rewards for environments that exist
for env_id in self.environments:
self.environment_rewards[env_id] = {
"reward": 0.0,
"interaction_steps": 0,
"test_steps": 0,
"interaction_resets": 0
}
# Store available transitions (only those involving available environments)
for transition in transitions:
from_env = transition.from_environment_id
to_env = transition.to_environment_id
# Skip transitions for non-existent environments
if from_env not in self.environments or to_env not in self.environments:
logger.warning(
f"Skipping transition {from_env} -> {to_env} (environment not available)"
)
continue
condition = transition.transition_condition
message = transition.transition_message
if from_env not in self.transitions:
self.transitions[from_env] = {}
self.transitions[from_env][condition] = to_env
self.transition_messages[(from_env, to_env)] = message
# Reset state - use the first available environment
available_envs = list(self.environments.keys())
self.current_environment = available_envs[0] if available_envs else None
self.environment_sequence = []
self.aggregate_reward = 0.0
self.evaluation_complete = False
self.config = config
return True, f"Initialized controller with available environments: {list(self.environments.keys())}, transitions: {self.transitions.keys()}, agent: {agent_id}"
def run_evaluation(self,
reset: bool = False,
max_transitions: int = 10) -> Dict[str, Any]:
"""Run full evaluation across multiple environments"""
logger.info(
f"Starting evaluation with reset={reset}, max_transitions={max_transitions}"
)
if reset:
self.environment_sequence = []
self.environment_rewards = {
env_id: {
"reward": 0.0,
"interaction_steps": 0,
"test_steps": 0,
"interaction_resets": 0,
}
for env_id in self.environments
}
self.aggregate_reward = 0.0
self.evaluation_complete = False
first_env_id = next(iter(self.environments.keys()))
self.current_environment = first_env_id
logger.info(
f"Reset evaluation state, starting with environment: {self.current_environment}"
)
transitions_made = 0
logger.info(
f"Current environment before loop: {self.current_environment}")
logger.info(
f"Loop conditions: transitions_made={transitions_made}, max_transitions={max_transitions}, evaluation_complete={self.evaluation_complete}"
)
if transitions_made < max_transitions and not self.evaluation_complete:
logger.info("Entering main evaluation loop")
else:
logger.warning(
"Loop conditions not met, skipping main evaluation loop")
while transitions_made < max_transitions and not self.evaluation_complete:
# Run current environment
if not self.current_environment:
logger.warning(
"No current environment, marking evaluation as complete")
self.evaluation_complete = True
break
print(f"Current environment: {self.current_environment}")
env_id = self.current_environment
logger.info(f"Adding environment to sequence: {env_id}")
self.environment_sequence.append(env_id)
logger.info(
f"Running environment: {env_id}, agent: {self.config.get('agent', 'autumn_llm_interactive_agent_v1')}"
)
reward, terminal_condition, final_state, misc_info = self.run_environment(
env_id, self.agents[self.config.get(
"agent", "autumn_llm_interactive_agent_v1")])
logger.info(
f"Environment run complete. Reward: {reward}, Terminal condition: {terminal_condition}"
)
# Store reward
self.environment_rewards[env_id]["reward"] += reward
self.environment_rewards[env_id]["interaction_steps"] = misc_info["interaction_steps"]
self.environment_rewards[env_id]["test_steps"] = misc_info["test_steps"]
self.environment_rewards[env_id]["interaction_resets"] = misc_info["interaction_resets"]
# Check for transition
next_env = None
if env_id in self.transitions and terminal_condition in self.transitions[
env_id]:
next_env = self.transitions[env_id][terminal_condition]
transition_message = self.transition_messages.get(
(env_id, next_env), "")
logger.info(
f"Transitioning: {env_id} -> {next_env} | {transition_message}"
)
if next_env:
self.current_environment = next_env
transitions_made += 1
logger.info(
f"Transitioned to new environment: {next_env}, transitions made: {transitions_made}"
)
else:
# No valid transition found, evaluation is complete
if terminal_condition == "error":
logger.error(f"Environment reported error: {final_state}")
else:
logger.info(
f"No transition found for condition: {terminal_condition}"
)
self.current_environment = None
self.evaluation_complete = True
logger.info(
"No more transitions available, marking evaluation as complete"
)
logger.info(
f"Evaluation loop finished. Transitions made: {transitions_made}, evaluation complete: {self.evaluation_complete}"
)
logger.info(f"Environment sequence: {self.environment_sequence}")
# Calculate aggregate reward (R_C)j
self.aggregate_reward = self.calculate_aggregate_reward(
self.environment_rewards)
logger.info(f"Aggregate reward: {self.aggregate_reward}")
return {
"success": True,
"message": "Evaluation complete"
if self.evaluation_complete else "Evaluation in progress",
"aggregate_reward": self.aggregate_reward,
"environment_rewards": self.environment_rewards,
"environments_visited": self.environment_sequence,
"evaluation_complete": self.evaluation_complete
}
def run_environment(self,
env_id: str,
agent_class,
max_steps: int = 501) -> Tuple[float, str, str, Dict[str, Any]]:
"""Run a single environment episode"""
logger.info(f"Attempting to run environment: {env_id}")
print(f"Environment map: {self.environments}"
) # Print the environments map
print(f"Looking up endpoint for {env_id}")
observation_text = ""
misc_info = {
"interaction_steps": 0,
"test_steps": 0,
"interaction_resets": 0,
"resets": 0
}
try:
# Create environment stub
env_stub = None
if "_mfp" in env_id:
env_stub = MARACompositeAutumnMFPServicer()
elif "_cd" in env_id:
env_stub = MARACompositeAutumnChangeDetectionServicer()
elif "_planning" in env_id:
env_stub = MARACompositeAutumnPlanningServicer()
else:
logger.error(f"No environment stub found for {env_id}")
return 0.0, "error", f"No environment stub found for {env_id}", misc_info
env_init = env_stub.Initialize(
env_service_pb2.InitializeRequest(
env_type=env_pb2.REACTIVE,
config=self.configs.get(
env_id, {
"env_name":
"_".join(env_id.split("_")[:-2]),
"max_interaction_steps":
str(self.config.get("max_interaction_steps", 25)),
"stack_frames": str(self.config.get("stack_frames", False)),
"skip_frames":
str(self.config.get("skip_frames", False)),
"render_mode":
self.config.get("render_mode", "text"),
"logging_path":
self.config.get("logging_path", "./logs"),
"seed":
str(self.config.get("seed", 0)),
"data_dir":
self.config.get("data_dir", "./data")
})), None)
logger.info(f"Environment initialized: {env_init.message}")
logger.info("Resetting environment")
env_reset = env_stub.Reset(env_service_pb2.ResetRequest(), None)
logger.info("Environment reset successfully")
agent_stub = agent_class()
# Initialize agent
logger.info(f"Initializing agent with config: {self.config}")
agent_init = agent_stub.Initialize(
agent_pb2.AgentInitializeRequest(
config={
"llm_provider":
self.config.get("llm_provider", "openai"),
"llm_model":
self.config.get("llm_model", "openai/gpt-4o"),
"env_name":
"_".join(env_id.split("_")[:-2]),
"logging_path":
self.config.get("logging_path", "./logs"),
"max_history_length":
str(self.config.get("max_history_length", -1)),
"use_scratchpad":
str(self.config.get("use_scratchpad", False)),
"instruction_type":
self.config.get("instruction_type", "react"),
"hint":
str(self.config.get("hint", False)),
"task_name":
self.config.get("task_name", "mfp"),
"stack_frames":
str(self.config.get("stack_frames", False)),
"skip_frames":
str(self.config.get("skip_frames", False)),
"render_mode":
self.config.get("render_mode", "text"),
"seed":
str(self.config.get("seed", 0)),
"data_dir":
self.config.get("data_dir", "./data"),
"use_oracle_interpreter_seed":
str(self.config.get("use_oracle_interpreter_seed")),
}), None)
logger.info(
f"\n\n[Controller] Agent initialized: {agent_init.message}, id: {agent_init.agent_id}"
)
# Reset agent
agent_reset = agent_stub.Reset(
agent_pb2.AgentResetRequest(
initial_observation=env_reset.initial_observation), None)
logger.info("Agent reset successfully")
# Run episode loop
steps = 0
total_reward = 0.0
is_terminal = False
current_observation = env_reset.initial_observation
logger.info(f"Starting episode loop (max_steps={max_steps})")
logger.info(
f"Initial observation: {current_observation.text_data}")
while not is_terminal and steps < max_steps:
# Log step information
logger.info(f"Step {steps + 1}/{max_steps}")
# Query action space
try:
space_query = env_service_pb2.SpaceQueryRequest()
space_query.reactive_query.SetInParent()
space_response = env_stub.QuerySpaces(space_query, None)
# logger.info(f"Action space: {space_response.reactive_response.action_space}")
except grpc.RpcError as e:
logger.error(f"Failed to query action space: {e}")
break
# Get agent action
try:
act_response = agent_stub.Act(
agent_pb2.ActRequest(
observation=current_observation,
reactive_action_space=space_response.
reactive_response.action_space), None)
action_text = act_response.action.text_data if hasattr(
act_response.action, 'text_data') else "unknown"
logger.info(f"Agent action: {action_text}")
except grpc.RpcError as e:
logger.error(f"Failed to get agent action: {e}")
break
# Take environment step
try:
step_response = env_stub.Step(
env_service_pb2.StepRequest(
action=act_response.action), None)
reward = step_response.reward
is_terminal = step_response.is_terminal
total_reward += reward
logger.info(
f"Step result: reward={reward}, terminal={is_terminal}"
)
except grpc.RpcError as e:
logger.error(f"Failed to take environment step: {e}")
break
# Store previous observation for feedback
prev_observation = current_observation
current_observation = step_response.observation
logger.info(f"Observation: {current_observation.text_data}")
# Provide feedback to agent
try:
agent_stub.Feedback(
agent_pb2.FeedbackRequest(
previous_observation=prev_observation,
action=act_response.action,
current_observation=current_observation,
reward=reward,
is_terminal=is_terminal,
info=step_response.info), None)
except grpc.RpcError as e:
logger.error(f"Failed to provide feedback to agent: {e}")
if "resets" in step_response.info:
misc_info["interaction_resets"] += step_response.info["resets"]
if "interaction_steps" in step_response.info:
misc_info["interaction_steps"] = step_response.info["interaction_steps"]
elif hasattr(env_stub, 'transiting_state') and env_stub.transiting_state == "Interactive":
misc_info["interaction_steps"] = env_stub.steps
elif hasattr(env_stub, 'transiting') and env_stub.transiting == "Interactive":
misc_info["interaction_steps"] = env_stub.steps
elif env_id.endswith("_cd"):
misc_info["test_steps"] = env_stub.steps
else:
misc_info["test_steps"] = env_stub.steps
steps += 1
if "terminal_condition" in step_response.info:
terminal_condition = step_response.info["terminal_condition"]
# Determine terminal condition
if is_terminal:
terminal_condition = "default"
if "terminal_condition" in step_response.info:
terminal_condition = step_response.info[
"terminal_condition"]
else:
observation_text = current_observation.text_data.lower(
) if hasattr(current_observation, 'text_data') else ""
if "quit" in observation_text:
terminal_condition = "quit"
elif "finish" in observation_text or "congratulations" in observation_text:
terminal_condition = "finish"
elif "fail" in observation_text:
terminal_condition = "fail"
else:
# No terminal condition detected - this is normal for non-terminal states
terminal_condition = "default"
logger.debug(f"No terminal condition detected in observation: {observation_text[:100]}...")
# Episode complete
logger.info(
f"Episode complete: steps={steps}, total_reward={total_reward}"
)
# Notify agent of episode completion
try:
agent_stub.EndEpisode(
agent_pb2.EndEpisodeRequest(total_reward=total_reward,
num_steps=steps,
success=total_reward > 0),
None)
logger.info("Agent notified of episode completion")
except grpc.RpcError as e:
logger.error(f"Failed to notify agent of episode end: {e}")
# Close environment
try:
env_stub.Close(env_service_pb2.CloseRequest(), None)
logger.info("Environment closed successfully")
except grpc.RpcError as e:
logger.error(f"Failed to close environment: {e}")
return total_reward, "default", observation_text, misc_info
except Exception as e:
logger.exception(
f"Unexpected error running environment {env_id}: {e}")
return 0.0, "error", f"Unexpected error: {str(e)}", misc_info
def get_state(self) -> Dict[str, Any]:
"""Get current evaluation state"""
return {
"current_environment_id": self.current_environment,
"rewards_so_far": self.environment_rewards,
"environment_sequence": self.environment_sequence,
"aggregate_reward": self.aggregate_reward,
"evaluation_complete": self.evaluation_complete
}
def transition(self, from_env: str, to_env: str,
transition_data: Any) -> Tuple[bool, str, str]:
"""Manual environment transition"""
if from_env != self.current_environment:
return False, "Specified from_environment does not match current environment", ""
if to_env not in self.environments:
return False, f"Specified to_environment {to_env} not found", ""
# Perform transition (T function in the paper)
self.current_environment = to_env
message = self.transition_messages.get((from_env, to_env), "")
return True, "Transition successful", message
def calculate_aggregate_reward(
self, environment_rewards: Dict[str, float]) -> float:
"""Calculate aggregate reward (R_C function in the paper)"""
# Simple implementation: sum of rewards
# A more complex implementation could use weights or other transformations
return sum([env_reward["reward"] for env_reward in environment_rewards.values()])
def get_transition_message(self, from_env: str, to_env: str) -> str:
"""Get transition message (O_C function in the paper)"""
return self.transition_messages.get((from_env, to_env), "")
class EvaluationController:
def __init__(self) -> None:
self.environments: Dict[str,
str] = {} # Map of environment_id to endpoint
self.transitions: Dict[str, Dict[str, str]] = {
} # Map of from_env -> {condition: to_env}
self.transition_messages: Dict[Any, Any] = {
} # Messages to show during transitions
self.configs: Dict[str, Dict[str, str]] = {
} # Configuration for each environment
self.current_environment: Optional[str] = None
self.environment_sequence: List[str] = []
self.environment_rewards: Dict[str, float] = {}
self.aggregate_reward: float = 0.0
self.evaluation_complete: bool = False
self.environment_ids: List[str] = []
def initialize(self, environment_ids: List[str], transitions: List[Any],
agent_id: str, config: Dict[str, Any]) -> Tuple[bool, str]:
print("Initializing controller")
print(f"Environment IDs: {environment_ids}")
self.environment_ids = environment_ids
# Only use the text adventure environment on its actual port
self.environments = {
**{
f"{eid}_interactive_mfp": f"localhost:{port}"
for eid, port in zip(
environment_ids,
range(50050, 50050 + len(environment_ids) * 3, 3))
},
# **{f"{eid}_interactive_cd": f"localhost:{port}" for eid, port in zip(environment_ids, range(50051, 50051 + len(environment_ids)*3, 3))},
}
print(f"Environments: {environment_ids}")
self.transitions = {
**{
f"{environment_ids[eid]}_interactive_mfp": {
"default": f"{environment_ids[eid + 1]}_interactive_mfp",
"quit": f"{environment_ids[eid + 1]}_interactive_mfp",
"finish": f"{environment_ids[eid + 1]}_interactive_mfp",
"fail": f"{environment_ids[eid + 1]}_interactive_mfp",
}
for eid in range(len(environment_ids) - 1)
},
}
self.transition_messages = {
**{
f"{environment_ids[eid]}_interactive_mfp": {
"default": f"{environment_ids[eid + 1]}_interactive_mfp",
"quit": f"{environment_ids[eid + 1]}_interactive_mfp",
"finish": f"{environment_ids[eid + 1]}_interactive_mfp",
"fail": f"{environment_ids[eid + 1]}_interactive_mfp",
}
for eid in range(len(environment_ids) - 1)
},
}
# self.transitions = {
# **{f"{eid}_interactive_mfp": {
# "default": f"{eid}_interactive_cd",
# "quit": f"{eid}_interactive_cd",
# "finish": f"{eid}_interactive_cd",
# "fail": f"{eid}_interactive_cd",
# } for eid in environment_ids},
# **{f"{environment_ids[eid]}_interactive_cd": {
# "default": f"{environment_ids[eid + 1]}_interactive_mfp",
# "quit": f"{environment_ids[eid + 1]}_interactive_mfp",
# "finish": f"{environment_ids[eid + 1]}_interactive_mfp",
# "fail": f"{environment_ids[eid + 1]}_interactive_mfp",
# } for eid in range(len(environment_ids) - 1)},
# }
# self.transition_messages = {
# **{f"{eid}_interactive_mfp": {
# "default": f"{eid}_interactive_cd",
# "quit": f"{eid}_interactive_cd",
# "finish": f"{eid}_interactive_cd",
# "fail": f"{eid}_interactive_cd",
# } for eid in environment_ids},
# **{f"{environment_ids[eid]}_interactive_cd": {
# "default": f"{environment_ids[eid]}_interactive_mfp",
# "quit": f"{environment_ids[eid]}_interactive_mfp",
# "finish": f"{environment_ids[eid]}_interactive_mfp",
# "fail": f"{environment_ids[eid]}_interactive_mfp",
# } for eid in range(len(environment_ids) - 1)},
# }
self.agents = {
"autumn_random_interactive_agent_v1": "localhost:50253",
"autumn_llm_interactive_agent_v1": "localhost:50254"
}
# Initialize rewards for environments that exist
for env_id in self.environments:
self.environment_rewards[env_id] = 0.0
# Store available transitions (only those involving available environments)
for transition in transitions:
from_env = transition.from_environment_id
to_env = transition.to_environment_id
# Skip transitions for non-existent environments
if from_env not in self.environments or to_env not in self.environments:
logger.warning(
f"Skipping transition {from_env} -> {to_env} (environment not available)"
)
continue
condition = transition.transition_condition
message = transition.transition_message
if from_env not in self.transitions:
self.transitions[from_env] = {}
self.transitions[from_env][condition] = to_env
self.transition_messages[(from_env, to_env)] = message
# Reset state - use the first available environment
available_envs = list(self.environments.keys())
self.current_environment = available_envs[0] if available_envs else None
self.environment_sequence = []
self.aggregate_reward = 0.0
self.evaluation_complete = False
self.config = config
return True, f"Initialized controller with available environments: {list(self.environments.keys())}, transitions: {self.transitions.keys()}, agent: {agent_id}"
def run_evaluation(self,
reset: bool = False,
max_transitions: int = 10) -> Dict[str, Any]:
"""Run full evaluation across multiple environments"""
logger.info(
f"Starting evaluation with reset={reset}, max_transitions={max_transitions}"
)
if reset:
self.environment_sequence = []
self.environment_rewards = {
env_id: 0.0
for env_id in self.environments
}
self.aggregate_reward = 0.0
self.evaluation_complete = False
first_env_id = next(iter(self.environments.keys()))
self.current_environment = first_env_id
logger.info(
f"Reset evaluation state, starting with environment: {self.current_environment}"
)
transitions_made = 0
logger.info(
f"Current environment before loop: {self.current_environment}")
logger.info(
f"Loop conditions: transitions_made={transitions_made}, max_transitions={max_transitions}, evaluation_complete={self.evaluation_complete}"
)
if transitions_made < max_transitions and not self.evaluation_complete:
logger.info("Entering main evaluation loop")
else:
logger.warning(
"Loop conditions not met, skipping main evaluation loop")
while transitions_made < max_transitions and not self.evaluation_complete:
# Run current environment
if not self.current_environment:
logger.warning(
"No current environment, marking evaluation as complete")
self.evaluation_complete = True
break
print(f"Current environment: {self.current_environment}")
env_id = self.current_environment
logger.info(f"Adding environment to sequence: {env_id}")
self.environment_sequence.append(env_id)
logger.info(
f"Running environment: {env_id}, agent: {self.config.get('agent', 'autumn_llm_interactive_agent_v1')}"
)
reward, terminal_condition, final_state = self.run_environment(
env_id, self.agents[self.config.get(
"agent", "autumn_llm_interactive_agent_v1")], max_steps=self.config.get("max_steps", 301))
logger.info(
f"Environment run complete. Reward: {reward}, Terminal condition: {terminal_condition}"
)
# Store reward
self.environment_rewards[env_id] += reward
# Check for transition
next_env = None
if env_id in self.transitions and terminal_condition in self.transitions[
env_id]:
next_env = self.transitions[env_id][terminal_condition]
transition_message = self.transition_messages.get(
(env_id, next_env), "")
logger.info(
f"Transitioning: {env_id} -> {next_env} | {transition_message}"
)
if next_env:
self.current_environment = next_env
transitions_made += 1
logger.info(
f"Transitioned to new environment: {next_env}, transitions made: {transitions_made}"
)
else:
# No valid transition found, evaluation is complete
if terminal_condition == "error":
logger.error(f"Environment reported error: {final_state}")
else:
logger.info(
f"No transition found for condition: {terminal_condition}"
)
self.current_environment = None
self.evaluation_complete = True
logger.info(
"No more transitions available, marking evaluation as complete"
)
logger.info(
f"Evaluation loop finished. Transitions made: {transitions_made}, evaluation complete: {self.evaluation_complete}"
)
logger.info(f"Environment sequence: {self.environment_sequence}")
# Calculate aggregate reward (R_C)j
self.aggregate_reward = self.calculate_aggregate_reward(
self.environment_rewards)
logger.info(f"Aggregate reward: {self.aggregate_reward}")
return {
"success": True,
"message": "Evaluation complete"
if self.evaluation_complete else "Evaluation in progress",
"aggregate_reward": self.aggregate_reward,
"environment_rewards": self.environment_rewards,
"environments_visited": self.environment_sequence,
"evaluation_complete": self.evaluation_complete
}
def run_environment(self,
env_id: str,
agent_endpoint: str,
max_steps: int = 501) -> Tuple[float, str, str]:
"""Run a single environment episode"""
logger.info(f"Attempting to run environment: {env_id}")
print(f"Environment map: {self.environments}"
) # Print the environments map
print(f"Looking up endpoint for {env_id}")
observation_text = ""
try:
# Connect to environment
env_endpoint = self.environments.get(env_id)
print(f"Found endpoint: {env_endpoint}") # Print found endpoint
# Create environment stub
env_stub = None
if "_mfp" in env_id:
env_stub = MARACompositeAutumnMFPServicer()
elif "_cd" in env_id:
env_stub = MARACompositeAutumnChangeDetectionServicer()
elif "_planning" in env_id:
env_stub = MARACompositeAutumnPlanningServicer()
else:
logger.error(f"No environment stub found for {env_id}")
return 0.0, "error", f"No environment stub found for {env_id}"
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
env_grpc.add_MARAEnvironmentServicer_to_server(env_stub, server)
port = self.environments[env_id].split(":")[1]
server.add_insecure_port(f'[::]:{port}')
server.start()
print(f"Environment server started on port {port}")
server_is_running = False
while not server_is_running:
try:
logger.info(f"Connecting to environment at {env_endpoint}")
time.sleep(1)
env_channel = grpc.insecure_channel(env_endpoint)
env_stub = env_grpc.MARAEnvironmentStub(env_channel)
server_is_running = True
except Exception as e:
print("Waiting for server to start, retrying...")
time.sleep(1)
if not env_endpoint:
logger.error(
f"No endpoint configured for environment {env_id}")
return 0.0, "error", f"No endpoint for {env_id}"
try:
logger.info("Initializing environment")
env_init = env_stub.Initialize(
env_service_pb2.InitializeRequest(
env_type=env_pb2.REACTIVE,
config=self.configs.get(
env_id, {
"env_name": "_".join(env_id.split("_")[:-2]),
"max_interaction_steps": "100"
})),
timeout=10)
logger.info(f"Environment initialized: {env_init.message}")
except grpc.RpcError as e:
logger.error(f"Failed to initialize environment: {e}")
return 0.0, "error", f"Initialization failed: {str(e)}"
# Reset environment
try:
logger.info("Resetting environment")
env_reset = env_stub.Reset(env_service_pb2.ResetRequest())
logger.info("Environment reset successfully")
except grpc.RpcError as e:
logger.error(f"Failed to reset environment: {e}")
return 0.0, "error", f"Reset failed: {str(e)}"
# Connect to agent
try:
logger.info(f"Connecting to agent at {agent_endpoint}")
agent_channel = grpc.insecure_channel(agent_endpoint)
agent_stub = agent_grpc.MARAAgentStub(agent_channel)
# Initialize agent
logger.info(
f"Initializing agent with config herhehre: {self.config}")
agent_init = agent_stub.Initialize(
agent_pb2.AgentInitializeRequest(
config={
"llm_provider":
self.config.get("llm_provider", "openai"),
"llm_model":
self.config.get("llm_model", "openai/gpt-4o")
}))
logger.info(
f"\n\n[Controller] Agent initialized: {agent_init.message}, id: {agent_init.agent_id}"
)
# Reset agent
agent_reset = agent_stub.Reset(
agent_pb2.AgentResetRequest(
initial_observation=env_reset.initial_observation))
logger.info("Agent reset successfully")
except grpc.RpcError as e:
logger.error(f"Failed to initialize or reset agent: {e}")
return 0.0, "error", f"Agent initialization failed: {str(e)}"
# Run episode loop
steps = 0
total_reward = 0.0
is_terminal = False
current_observation = env_reset.initial_observation
logger.info(f"Starting episode loop (max_steps={max_steps})")
logger.info(
f"Initial observation: {current_observation.text_data}")
while not is_terminal and steps < max_steps:
# Log step information
logger.info(f"Step {steps + 1}/{max_steps}")
# Query action space
try:
space_query = env_service_pb2.SpaceQueryRequest()
space_query.reactive_query.SetInParent()
space_response = env_stub.QuerySpaces(space_query)
except grpc.RpcError as e:
logger.error(f"Failed to query action space: {e}")
break
# Get agent action
try:
act_response = agent_stub.Act(
agent_pb2.ActRequest(
observation=current_observation,
reactive_action_space=space_response.
reactive_response.action_space))
action_text = act_response.action.text_data if hasattr(
act_response.action, 'text_data') else "unknown"
logger.info(f"Agent action: {action_text}")
except grpc.RpcError as e:
logger.error(f"Failed to get agent action: {e}")
break
# Take environment step
try:
step_response = env_stub.Step(
env_service_pb2.StepRequest(
action=act_response.action))
reward = step_response.reward
is_terminal = step_response.is_terminal
total_reward += reward
logger.info(
f"Step result: reward={reward}, terminal={is_terminal}"
)
except grpc.RpcError as e:
logger.error(f"Failed to take environment step: {e}")
break
# Store previous observation for feedback
prev_observation = current_observation
current_observation = step_response.observation
logger.info(f"Observation: {current_observation.text_data}")
# Provide feedback to agent
try:
agent_stub.Feedback(
agent_pb2.FeedbackRequest(
previous_observation=prev_observation,
action=act_response.action,
current_observation=current_observation,
reward=reward,
is_terminal=is_terminal,
info=step_response.info))
except grpc.RpcError as e:
logger.error(f"Failed to provide feedback to agent: {e}")
steps += 1
if "terminal_condition" in step_response.info:
terminal_condition = step_response.info["terminal_condition"]
# Determine terminal condition
if is_terminal:
terminal_condition = "default"
if "terminal_condition" in step_response.info:
terminal_condition = step_response.info[
"terminal_condition"]
else:
observation_text = current_observation.text_data.lower(
) if hasattr(current_observation, 'text_data') else ""
if "quit" in observation_text:
terminal_condition = "quit"
elif "finish" in observation_text or "congratulations" in observation_text:
terminal_condition = "finish"
elif "fail" in observation_text:
terminal_condition = "fail"
else:
logger.warning(
f"Unknown terminal condition: {observation_text}"
)
# raise ValueError(f"Unknown terminal condition: {observation_text}")
# Episode complete
logger.info(
f"Episode complete: steps={steps}, total_reward={total_reward}"
)
# Notify agent of episode completion
try:
agent_stub.EndEpisode(
agent_pb2.EndEpisodeRequest(total_reward=total_reward,
num_steps=steps,
success=total_reward > 0))
logger.info("Agent notified of episode completion")
except grpc.RpcError as e:
logger.error(f"Failed to notify agent of episode end: {e}")
# Close environment
try:
env_stub.Close(env_service_pb2.CloseRequest())
logger.info("Environment closed successfully")
except grpc.RpcError as e:
logger.error(f"Failed to close environment: {e}")
return total_reward, "default", observation_text
except Exception as e:
logger.exception(
f"Unexpected error running environment {env_id}: {e}")