pathfinding icon indicating copy to clipboard operation
pathfinding copied to clipboard

Add bertsekas algo for assignment problem

Open smu160 opened this issue 1 year ago • 7 comments

Hi,

Apologies for the delay. I added a basic implementation with random cost matrices that compare to the hungarian algorithm. I'll add benchmarks, but I think I'll create a non-criterion based benchmark in order to account for possibly sub-optimal solutions. That is, we want to not only compare the speedup, but how close the total score is to what the hungarian algorithm provides.

I didn't have the time just yet to fix the lack of parallelism. However, the structure for concurrency is already there. That is why you'll see the use of mspc::channel. I also need to use the Matrix struct that is used by the current hungarian algorithm implementation. Luckily, that should only improve performance given the 1D representation. I'll keep working on this. Please feel free to let me know what you think.

Thank you!!

Fix #586

Summary by CodeRabbit

  • New Features
    • Added a new profiling configuration for detailed performance builds.
    • Expanded benchmarking capabilities to compare assignment algorithm performance over varying test sizes.
    • Introduced an example tool that demonstrates algorithm benchmarking with printed performance metrics.
    • Integrated an auction-based assignment algorithm into the public library.
    • Enhanced matrix functionality with improved row access for easier data handling.

smu160 avatar Sep 13 '24 20:09 smu160

CodSpeed Performance Report

Merging #600 will not alter performance

Comparing smu160:bertsekas (322f8a1) with main (e8e81df)

Summary

✅ 36 untouched benchmarks

🆕 14 new benchmarks

Benchmarks breakdown

Benchmark main smu160:bertsekas Change
🆕 Bertekas Auction[1000] N/A 18.7 ms N/A
🆕 Bertekas Auction[100] N/A 343.1 µs N/A
🆕 Bertekas Auction[10] N/A 24.1 µs N/A
🆕 Bertekas Auction[200] N/A 1.1 ms N/A
🆕 Bertekas Auction[20] N/A 30.9 µs N/A
🆕 Bertekas Auction[500] N/A 5.8 ms N/A
🆕 Bertekas Auction[50] N/A 124.8 µs N/A
🆕 Hungarian Algorithm[1000] N/A 5.8 s N/A
🆕 Hungarian Algorithm[100] N/A 1.3 ms N/A
🆕 Hungarian Algorithm[10] N/A 16.1 µs N/A
🆕 Hungarian Algorithm[200] N/A 7.2 ms N/A
🆕 Hungarian Algorithm[20] N/A 42.9 µs N/A
🆕 Hungarian Algorithm[500] N/A 434.7 ms N/A
🆕 Hungarian Algorithm[50] N/A 355 µs N/A

codspeed-hq[bot] avatar Sep 13 '24 22:09 codspeed-hq[bot]

lines

Preliminary benchmarks for #586.

smu160 avatar Sep 16 '24 00:09 smu160

@samueltardieu

I added a benchmark, using criterion, to compare the kuhn_munkres implementation with the bertsekas implementation.

Since the Bertsekas auction can give sub-optimal results (this is dependent on $\epsilon$ and the scaling factor), I included plots I rendered via matplotlib to compare the run-time as well as the overall score of the assignments. The underlying "benchmark" for these plots can be found in examples/assignment.rs.

cargo criterion --bench kuhn_munkres_vs_bertsekas --output-format bencher
    Finished `bench` profile [optimized] target(s) in 0.02s
test Assignment Problem/Bertekas Auction/10 ... bench:        3346 ns/iter (+/- 902)
test Assignment Problem/Hungarian Algorithm/10 ... bench:         777 ns/iter (+/- 126)
test Assignment Problem/Bertekas Auction/20 ... bench:        7051 ns/iter (+/- 1342)
test Assignment Problem/Hungarian Algorithm/20 ... bench:        2634 ns/iter (+/- 362)
test Assignment Problem/Bertekas Auction/50 ... bench:       20769 ns/iter (+/- 2251)
test Assignment Problem/Hungarian Algorithm/50 ... bench:       17671 ns/iter (+/- 2471)
test Assignment Problem/Bertekas Auction/100 ... bench:       54119 ns/iter (+/- 4672)
test Assignment Problem/Hungarian Algorithm/100 ... bench:      174590 ns/iter (+/- 38993)
test Assignment Problem/Bertekas Auction/200 ... bench:      169806 ns/iter (+/- 15348)

Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 5.4s, enable flat sampling, or reduce sample count to 60.
test Assignment Problem/Hungarian Algorithm/200 ... bench:      989247 ns/iter (+/- 128546)
test Assignment Problem/Bertekas Auction/500 ... bench:      888877 ns/iter (+/- 52426)
test Assignment Problem/Hungarian Algorithm/500 ... bench:    45738048 ns/iter (+/- 2187718)
test Assignment Problem/Bertekas Auction/1000 ... bench:     2360069 ns/iter (+/- 317069)

Warning: Unable to complete 100 samples in 5.0s. You may wish to increase target time to 47.1s, or reduce sample count to 10.
test Assignment Problem/Hungarian Algorithm/1000 ... bench:   492148479 ns/iter (+/- 10239431)

execution_time_comparison score_comparison

This PR is definitely still a work in progress, but I thought I'd share these preliminary results with you so you can have a clearer picture.

Note that the code is setup for parallelism already via the use of channels. Thus, these benchmark results contain a lot of unnecessary overhead for the bertseka's algorithm implementation, without the advantage of using multiple threads.

In addition this is a very basic implementation of this algorithm. We can actually converge on the solution much quicker if we have a forward/reverse auction. Luckily, this current implementation can be easily extended to facilitate this.

Lastly, please note this current implementation does not support asymmetric assignment problems. Thus, we always have to have $N == M$, where $N$ is the # of agents, and $M$ is the # of tasks. The addition of the reverse auction (see the above paper) would facilitate assignment problems where $N < M$.

Thank you!!

smu160 avatar Sep 20 '24 00:09 smu160

The preliminary results are encouraging, this is interesting to read.

samueltardieu avatar Sep 22 '24 11:09 samueltardieu

@coderabbitai review

samueltardieu avatar Apr 05 '25 09:04 samueltardieu

:white_check_mark: Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

coderabbitai[bot] avatar Apr 05 '25 09:04 coderabbitai[bot]

Walkthrough

This pull request updates project configurations and introduces new benchmarking and algorithm functionalities. The Cargo.toml file is reformatted, a new profiling configuration is added, and a benchmark entry is introduced. Additionally, new Rust files are added to benchmark assignment algorithms and provide an example usage. The auction algorithm—implementing Dmitri Bertsekas’s approach—is introduced with supporting data structures and methods, and its public API is exposed via a new module. A utility method for matrix row access is also added.

Changes

File(s) Summary
Cargo.toml Reformatted the pre-release-replacements, added a [profile.profiling] section inheriting from release, and inserted a new benchmark entry.
benches/kuhn_munkres_vs_bertsekas.rs, examples/assignment.rs Introduced new benchmark files: one using Criterion to compare the Hungarian algorithm against the Bertsekas Auction, and an example demo for assignment problems.
src/bertsekas.rs, src/lib.rs Added a new implementation of the Bertsekas Auction Algorithm including new structs, methods (e.g., initialization, bidding, scoring), and tests; exposed via the public module.
src/matrix.rs Added a public method get_row to retrieve a specific row as a slice from a matrix.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant BM as Benchmark (Criterion)
    participant CM as Matrix Creator
    participant HA as Hungarian Algorithm
    participant AA as Auction Algorithm
    participant R as Results Logger

    U->>BM: Trigger benchmark run
    BM->>CM: Generate matrices for given sizes
    CM->>HA: Provide integer matrix for Hungarian algorithm
    CM->>AA: Provide floating-point matrix for Auction algorithm
    HA->>BM: Return performance metrics
    AA->>BM: Return performance metrics
    BM->>R: Record and plot throughput data
sequenceDiagram
    participant A as Agent
    participant AU as Auction Context
    participant T as Task Manager

    A->>AU: Submit bid for a task
    AU->>T: Evaluate bid and update task assignment
    T-->>AU: Confirm assignment update
    AU->>A: Notify bid outcome

Assessment against linked issues

Objective Addressed Explanation
Implement a faster maximum weight matching solver using the auction algorithm (#586)
Benchmark and compare the auction algorithm against the Hungarian algorithm (#586)
Validate algorithm correctness with integrated tests (#586)

Poem

I hopped through lines of code so light,
Auction bids and benchmarks in plain sight.
Matrices sang with rows unfurled,
Algorithms danced in a joyful world.
With every tweak, my code took flight—
A rabbit’s cheer in code so bright! 🐰✨

✨ Finishing Touches
  • [ ] 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

coderabbitai[bot] avatar Apr 05 '25 09:04 coderabbitai[bot]