Skip to content

Faster parent population trials lookup in NSGA-II sampler - #238

Merged
Alnusjaponica merged 2 commits into
mainfrom
c-bata/nsgaii-fast-trial-number-lookup
Sep 2, 2026
Merged

Faster parent population trials lookup in NSGA-II sampler#238
Alnusjaponica merged 2 commits into
mainfrom
c-bata/nsgaii-fast-trial-number-lookup

Conversation

@c-bata

@c-bata c-bata commented Aug 28, 2026

Copy link
Copy Markdown
Member

Summary

Follow-up #179 and #222.

  • Resolve persisted NSGA-II parent trial IDs through get_cached_trial.
  • Avoid rebuilding the completed trial_id -> trial number map by scanning all trials on every sample.

Motivation

NSGA-II parent populations are persisted as trial IDs. On main, restoring a parent population rebuilds a trial_id -> trial number map by scanning all completed trials for every sample_joint call. This makes the cost grow with the total number of trials, even though the parent population contains only population_size trials.

Benchmark

I took the same benchmark with #237 (comment). Here are the benchmark results (n_trials=10000, n_params=40):

branch elapsed
main branch 128.034s
PR #179 7.878s
PR #237 123.207s
This PR 7.630s

@c-bata
c-bata force-pushed the c-bata/nsgaii-fast-trial-number-lookup branch from c75fbea to 708db2a Compare September 1, 2026 08:32
@c-bata c-bata changed the title Use cached trial lookup for parent restoration Faster parent population trials lookup in NSGA-II sampler Sep 1, 2026
Co-authored-by: Hemmi Shinichi <shemmi@preferred.jp>
@c-bata
c-bata force-pushed the c-bata/nsgaii-fast-trial-number-lookup branch from 708db2a to 14a7d15 Compare September 1, 2026 09:58
@c-bata

c-bata commented Sep 1, 2026

Copy link
Copy Markdown
Member Author

I also executed the following benchmark to confirm that this PR does not change the behavior.

import time
import rustuna

n_trials = 10000


def run_optimize():
    def objective(trial: rustuna.Trial) -> tuple[float, float]:
        x = trial.suggest_float("x", -15, 30)
        y = trial.suggest_float("y", -15, 30)

        v0 = 4 * x**2 + 4 * y**2
        v1 = (x - 5) ** 2 + (y - 5) ** 2
        trial.set_constraints({"c0": 1000 - v0})
        return v0, v1

    directions = ["minimize", "minimize"]
    sampler = rustuna.samplers.NSGAIISampler(seed=1)
    study = rustuna.create_study(sampler=sampler, directions=directions)
    study.optimize(objective, n_trials=n_trials)
    return study


def main():
    for _ in range(3):
        start = time.time()
        study = run_optimize()
        elapsed_rustuna = time.time() - start
        assert len(study.get_trials(states=[rustuna.trial.TrialState.COMPLETE])) == n_trials
        print(f"Rustuna\telapsed={elapsed_rustuna:.3f}\tbest_trials[0].values={study.best_trials[0].values}")

if __name__ == "__main__":
    main()

main branch:

% python ./bench_ngaii.py
Rustuna elapsed=1.286   best_trials[0].values=[9.837239422697502, 30.588169844860616]
Rustuna elapsed=1.249   best_trials[0].values=[9.837239422697502, 30.588169844860616]
Rustuna elapsed=1.226   best_trials[0].values=[9.837239422697502, 30.588169844860616]

This PR:

% python ./bench_ngaii.py
Rustuna elapsed=0.099   best_trials[0].values=[9.837239422697502, 30.588169844860616]
Rustuna elapsed=0.079   best_trials[0].values=[9.837239422697502, 30.588169844860616]
Rustuna elapsed=0.080   best_trials[0].values=[9.837239422697502, 30.588169844860616]

@c-bata
c-bata marked this pull request as ready for review September 1, 2026 10:04
@c-bata

c-bata commented Sep 1, 2026

Copy link
Copy Markdown
Member Author

@Alnusjaponica Could you review this PR?

@Alnusjaponica
Alnusjaponica requested review from Alnusjaponica and a lite review from Copilot and removed request for Copilot September 2, 2026 07:04

@Alnusjaponica Alnusjaponica left a comment

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.

Apologies for the delayed reply. I've added a few comments about the behavioral changes. Please take a look, and let me know if anything is unclear.

Comment thread rustuna_sampler/src/nsgaii.rs
Comment thread rustuna_sampler/src/nsgaii.rs Outdated
Comment thread rustuna_sampler/src/nsgaii.rs
@c-bata
c-bata force-pushed the c-bata/nsgaii-fast-trial-number-lookup branch from 21b5e96 to 64349c7 Compare September 2, 2026 09:16
@c-bata
c-bata force-pushed the c-bata/nsgaii-fast-trial-number-lookup branch from 64349c7 to 4ec59db Compare September 2, 2026 09:17
@c-bata

c-bata commented Sep 2, 2026

Copy link
Copy Markdown
Member Author

@Alnusjaponica I applied your suggestions. PTAL.

@Alnusjaponica Alnusjaponica left a comment

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.

LGTM. Thanks for the update.

Comment thread rustuna_sampler/src/nsgaii.rs
Comment on lines +450 to +452
&& matches!(
trial.state_values,
TrialStateValues::Complete(_)

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.

ref:

@Alnusjaponica
Alnusjaponica merged commit e6d619b into main Sep 2, 2026
6 checks passed
@Alnusjaponica
Alnusjaponica deleted the c-bata/nsgaii-fast-trial-number-lookup branch September 2, 2026 11:44
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.

2 participants