Eric Spencer

projects


← back to index

mc-carspot: Online Parking Simulation in Rust

A tiny Rust simulator for the online parking-spot problem with switching costs.

repo: https://github.com/EricSpencer00/mc-carspot


GitHub Repo

mc-carspot is a small Rust program, written in one sitting, on the online parking-spot problem. A car is parked somewhere on a street, closer spots open up as time goes on, and every move costs a switching penalty. The question is whether the walk saved is worth the move, and which policy minimizes total cost over a sequence of events. It is a classic toy problem from online algorithms and metrical task systems, which is where the "mc" in the name comes from (metrical cost).

The program is about 70 lines in src/main.rs. An Env struct holds the number of spots, the per-step walk cost, the move cost, and a vector of distances. On each tick some spots randomly become available (rng.gen_bool(0.3)), and the policy decides whether to stay or move to the closest open one. The default policy is a threshold rule: move only if (dist_cur - dist_new) * walk_cost * expected_uses > move_cost, that is, when the expected walking savings clears the move cost. The trivial baselines, always stay and always move, are included for comparison.

The only dependency is rand, and Cargo.toml is six lines. The program prints a table to stdout showing the spot, the action, the per-step cost, and the running total. There is no plotting and no CSV export. Configuration means editing src/main.rs and recompiling.

The repository is one commit and one file, with no tests. Possible additions: more policies (dynamic programming for the offline optimum, a small RL agent for comparison) and dumping runs to CSV to plot regret curves in Python. A new policy would go in the should_move function.