From c109fa8e99f9bb6ce3b903a73770c88db7146c32 Mon Sep 17 00:00:00 2001 From: Xu Yang Date: Tue, 25 Feb 2025 12:06:07 +0800 Subject: [PATCH] fix: qlib and other scenario bugs (#636) * fix qlib and other scenario bugs * bug fix --- rdagent/components/coder/factor_coder/factor.py | 2 +- rdagent/components/coder/model_coder/model.py | 2 +- rdagent/core/exception.py | 11 ++--------- rdagent/core/experiment.py | 7 +++++++ rdagent/scenarios/qlib/prompts.yaml | 8 +++++++- 5 files changed, 18 insertions(+), 12 deletions(-) diff --git a/rdagent/components/coder/factor_coder/factor.py b/rdagent/components/coder/factor_coder/factor.py index 502901c9..7902d429 100644 --- a/rdagent/components/coder/factor_coder/factor.py +++ b/rdagent/components/coder/factor_coder/factor.py @@ -117,7 +117,7 @@ class FactorFBWorkspace(FBWorkspace): - The cached information will include a tuple with the following: (execution_feedback, executed_factor_value_dataframe, Optional[Exception]) """ - super().execute() + self.before_execute() if self.file_dict is None or "factor.py" not in self.file_dict: if self.raise_exception: raise CodeFormatError(self.FB_CODE_NOT_SET) diff --git a/rdagent/components/coder/model_coder/model.py b/rdagent/components/coder/model_coder/model.py index 4db6a8a3..4e68d7de 100644 --- a/rdagent/components/coder/model_coder/model.py +++ b/rdagent/components/coder/model_coder/model.py @@ -97,7 +97,7 @@ class ModelFBWorkspace(FBWorkspace): input_value: float = 1.0, param_init_value: float = 1.0, ): - super().execute() + self.before_execute() try: qtde = QTDockerEnv() if self.target_task.version == 1 else KGDockerEnv() qtde.prepare() diff --git a/rdagent/core/exception.py b/rdagent/core/exception.py index 526b1544..12cbe504 100644 --- a/rdagent/core/exception.py +++ b/rdagent/core/exception.py @@ -48,16 +48,9 @@ class RunnerError(Exception): # NOTE: it corresponds to the error of whole **project** -class FactorEmptyError(Exception): - """ - Exceptions raised when no factor is generated correctly - """ +FactorEmptyError = CoderError # Exceptions raised when no factor is generated correctly - -class ModelEmptyError(Exception): - """ - Exceptions raised when no model is generated correctly - """ +ModelEmptyError = CoderError # Exceptions raised when no model is generated correctly class KaggleError(Exception): diff --git a/rdagent/core/experiment.py b/rdagent/core/experiment.py index 7d94cf8b..b2d63d4e 100644 --- a/rdagent/core/experiment.py +++ b/rdagent/core/experiment.py @@ -232,6 +232,13 @@ class FBWorkspace(Workspace): shutil.rmtree(self.workspace_path, ignore_errors=True) self.file_dict = {} + def before_execute(self) -> None: + """ + Before executing the code, we need to prepare the workspace and inject code into the workspace. + """ + self.prepare() + self.inject_files(**self.file_dict) + def execute(self, env: Env, entry: str) -> str: """ Before each execution, make sure to prepare and inject code. diff --git a/rdagent/scenarios/qlib/prompts.yaml b/rdagent/scenarios/qlib/prompts.yaml index b1ea9e52..c6b9baff 100644 --- a/rdagent/scenarios/qlib/prompts.yaml +++ b/rdagent/scenarios/qlib/prompts.yaml @@ -1,7 +1,13 @@ hypothesis_and_feedback: |- {% for experiment, feedback in trace.hist[-10:] %} Hypothesis {{ loop.index }}: {{ experiment.hypothesis }} - Corresponding Code (that leads to the difference in performance): {{experiment.sub_workspace_list[0].file_dict.get("model.py")}} + + Corresponding Code (that leads to the difference in performance): + {% for workspace in experiment.sub_workspace_list %} + {% if workspace is not none %} + Workspace {{loop.index}}: + {{workspace.all_codes}}{% endif %}{% endfor %} + Observation on the result with the hypothesis: {{ feedback.observations }} Feedback on the original hypothesis: {{ feedback.hypothesis_evaluation }} New Feedback for Context (For you to agree or improve upon): {{ feedback.new_hypothesis }}