Skip to content

feat: mcts policy based on trace scheduler - #1203

Merged
jingyuanlm merged 24 commits into
mainfrom
fsk/MCTS-based-on-trace-scheduler
Oct 17, 2025
Merged

feat: mcts policy based on trace scheduler#1203
jingyuanlm merged 24 commits into
mainfrom
fsk/MCTS-based-on-trace-scheduler

Conversation

@xuangu-fang

@xuangu-fang xuangu-fang commented Aug 25, 2025

Copy link
Copy Markdown
Contributor

MCTS policy based on trace scheduler


📚 Documentation preview 📚: https://RDAgent--1203.org.readthedocs.build/en/1203/

@xuangu-fang xuangu-fang changed the title feat: MCTS policy based on trace scheduler feat: mcts policy based on trace scheduler Aug 25, 2025
Comment thread rdagent/scenarios/data_science/loop.py Outdated
self.trace.set_current_selection(exp.local_selection)
self.trace.sync_dag_parent_and_hist((exp, prev_out["feedback"]), cur_loop_id)
# Notify MCTS scheduler for value backpropagation
scheduler = getattr(self.exp_gen, "trace_scheduler", None)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put these logic in exp_gen, and make the framework less complicated.

self.trace_scheduler: TraceScheduler = import_class(DS_RD_SETTING.trace_scheduler)(
DS_RD_SETTING.max_trace_num,
DS_RD_SETTING.scheduler_temperature,
c_puct=DS_RD_SETTING.scheduler_c_puct,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should put it into the specific schedular to make it more general

Comment thread rdagent/scenarios/data_science/loop.py Outdated
scheduler = getattr(self.exp_gen, "trace_scheduler", None)
if isinstance(scheduler, MCTSScheduler):
try:
scheduler.reset()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exp_gen.reset()

Comment thread rdagent/scenarios/data_science/loop.py Outdated
logger.log_object(self.trace, tag="trace before restart")
self.trace = DSTrace(scen=self.trace.scen, knowledge_base=self.trace.knowledge_base)
# Reset the trace; MCTS stats will be cleared via registered callback
self.trace.reset()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.exp_gen.reset()


self.uncommitted_experiments: dict[int, DSExperiment] = {} # loop_id -> DSExperiment

def reset(self) -> None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trace does not need reset.

Comment thread rdagent/core/proposal.py Outdated
if callable(fn):
self._on_reset_callbacks.append(fn)

def reset(self) -> None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default do nothing

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Override def reset for ParallelMultiTraceExpGen

Comment thread rdagent/scenarios/data_science/loop.py Outdated

# Register MCTS reset callback so trace.reset() can cascade to MCTS stats reset
scheduler = getattr(self.exp_gen, "trace_scheduler", None)
if isinstance(scheduler, MCTSScheduler):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

reward = 0.0

# Attribute the reward to the immediate parent leaf that was expanded
parent_tuple = trace.dag_parent[new_idx] if 0 <= new_idx < len(trace.dag_parent) else ()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

back propogate recursive

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

find the recursive logic

if trace.sub_trace_count + self.uncommited_rec_status[trace.NEW_ROOT] < self.max_trace_num:
return trace.NEW_ROOT

# Step 2: consider only available leaves (not being expanded)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't only calculate UCT on leaves

# Avoid div-by-zero; encourage exploration when visits are small
return self.c_puct * prior * math.sqrt(max(1, self.global_visit_count)) / (1 + visits)

def select(self, trace: DSTrace) -> tuple[int, ...] | None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle parallel

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle unfinished running

else:
# New root expansion: no parent leaf to credit; optional: keep a pseudo key
pass
except Exception as e:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

for idx in range(start_idx, end_idx):
self.observe_feedback(trace, idx)
self.last_observed_commit_idx = end_idx
except Exception as e:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

@jingyuanlm
jingyuanlm merged commit 13890e0 into main Oct 17, 2025
9 checks passed
@jingyuanlm
jingyuanlm deleted the fsk/MCTS-based-on-trace-scheduler branch October 17, 2025 08:21
new_idx: Index of the newly appended experiment in trace.hist.
reward: Optional explicit reward. If None, derive from feedback.decision (1.0/0.0).
"""
if reward is None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove reward

reward: Optional explicit reward. If None, derive from feedback.decision (1.0/0.0).
"""
if reward is None:
if 0 <= new_idx < len(trace.hist):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unecessary

Hoder-zyf added a commit that referenced this pull request Oct 21, 2025
* init mcts class

* full ver of MCTS

* auto-lint

* make MCTS feedback in exp-gen()

* refactor: move reset logic from Trace to ExpGen and update usage accordingly

* fix: reinitialize trace on consecutive errors in DataScienceRDLoop

* feat: add reset method to BaseScheduler and call in MCTSScheduler reset

* style: reorder imports for consistency and PEP8 compliance

* lint

* fix observe_feedback

* fix bug

* remove uncommited_rec_status

* more simple

* refactor: move commit observation logic to process_uncommitted_nodes method

* docs: add TODO comment about rule-based virtual root node expansion

* add score reward

* fix bug

* fix small bug

* lint

* change reward

* lint

---------

Co-authored-by: Young <afe.young@gmail.com>
Co-authored-by: jingyuanlm <842442862@qq.com>
Co-authored-by: amstrongzyf <amstrongzyf@126.com>
licong01-cloud pushed a commit to licong01-cloud/RD-Agent that referenced this pull request Dec 13, 2025
* init mcts class

* full ver of MCTS

* auto-lint

* make MCTS feedback in exp-gen()

* refactor: move reset logic from Trace to ExpGen and update usage accordingly

* fix: reinitialize trace on consecutive errors in DataScienceRDLoop

* feat: add reset method to BaseScheduler and call in MCTSScheduler reset

* style: reorder imports for consistency and PEP8 compliance

* lint

* fix observe_feedback

* fix bug

* remove uncommited_rec_status

* more simple

* refactor: move commit observation logic to process_uncommitted_nodes method

* docs: add TODO comment about rule-based virtual root node expansion

* add score reward

* fix bug

* fix small bug

* lint

* change reward

* lint

---------

Co-authored-by: Young <afe.young@gmail.com>
Co-authored-by: jingyuanlm <842442862@qq.com>
Co-authored-by: amstrongzyf <amstrongzyf@126.com>
yongbin4 pushed a commit to yongbin4/RD-Agent that referenced this pull request Mar 8, 2026
* init mcts class

* full ver of MCTS

* auto-lint

* make MCTS feedback in exp-gen()

* refactor: move reset logic from Trace to ExpGen and update usage accordingly

* fix: reinitialize trace on consecutive errors in DataScienceRDLoop

* feat: add reset method to BaseScheduler and call in MCTSScheduler reset

* style: reorder imports for consistency and PEP8 compliance

* lint

* fix observe_feedback

* fix bug

* remove uncommited_rec_status

* more simple

* refactor: move commit observation logic to process_uncommitted_nodes method

* docs: add TODO comment about rule-based virtual root node expansion

* add score reward

* fix bug

* fix small bug

* lint

* change reward

* lint

---------

Co-authored-by: Young <afe.young@gmail.com>
Co-authored-by: jingyuanlm <842442862@qq.com>
Co-authored-by: amstrongzyf <amstrongzyf@126.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants