Skip to content

fix: use absolute path for aco_tsp data file#383

Open
himanksingh2024-svg wants to merge 5 commits intomesa:mainfrom
himanksingh2024-svg:fix-aco-tsp-path
Open

fix: use absolute path for aco_tsp data file#383
himanksingh2024-svg wants to merge 5 commits intomesa:mainfrom
himanksingh2024-svg:fix-aco-tsp-path

Conversation

@himanksingh2024-svg
Copy link

@himanksingh2024-svg himanksingh2024-svg commented Mar 14, 2026

Thanks for opening a PR! Please click the Preview tab and select a PR template:

Problem: The aco_tsp example fails with FileNotFoundError when run from the repo root directory because the data file path is relative.
Fix: Used os.path.dirname(file) to make the path absolute, so it works regardless of where the script is run from.
Fixes the error: FileNotFoundError: No such file or directory: 'aco_tsp/data/kroA100.tsp'


GSoC contributor checklist

Context & motivation

I found this bug while testing mesa-examples locally. When running the aco_tsp example from the repo root, it throws a FileNotFoundError because the data file path is relative to the working directory, not the script location. This is a real usability issue for anyone trying to run the examples.

What I learned

I learned that file paths in Python scripts should use os.path.dirname(file) to be robust regardless of where the script is run from. I also learned the Mesa contribution workflow including forking, branching, and PR process.

Learning repo

🔗 My learning repo: https://github.com/himanksingh2024-svg/GSoC-learning-space
🔗 Relevant model(s): N/A (bug fix PR)

Readiness checks

  • This PR addresses an agreed-upon problem (linked issue or discussion with maintainer approval), or is a small/trivial fix
  • I have read the contributing guide and deprecation policy
  • I have performed a self-review
  • Another GSoC contributor has reviewed this PR: @
  • Tests pass locally
  • Code is formatted

@ShreyasN707
Copy link
Contributor

also can you improve the PR description following the PR requirements mentioned in : mesa/mesa#3536 (comment)


tsp_graph = TSPGraph.from_tsp_file("aco_tsp/data/kroA100.tsp")
tsp_graph = TSPGraph.from_tsp_file(
os.path.join(os.path.dirname(__file__), "aco_tsp/data/kroA100.tsp")
Copy link
Contributor

Choose a reason for hiding this comment

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

since app.py is already inside the aco_tsp directory, the joined path may not need the extra aco_tsp/ prefix. Using:

os.path.join(os.path.dirname(__file__), "data/kroA100.tsp")

would resolve directly to the data folder next to the script.

@himanksingh2024-svg
Copy link
Author

Updated the fix based on @ShreyasN707's review. The path is correct as aco_tsp/data/kroA100.tsp relative to app.py since the data folder is inside the aco_tsp subdirectory.

@EwoutH
Copy link
Member

EwoutH commented Mar 15, 2026

Cool seeing a peer-review going! I wrote down a checklist, maybe it's useful!

Copy link
Author

@himanksingh2024-svg himanksingh2024-svg left a comment

Choose a reason for hiding this comment

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

Self-review:

  • ✅ Fix is minimal — only changes the file path, nothing else
  • ✅ Tested locally — python examples/aco_tsp/app.py runs with no errors
  • ✅ No dead code or unused imports added (os was already needed)
  • ✅ No deprecated APIs used
  • ✅ Simplest solution to the FileNotFoundError problem

Copy link
Contributor

@ShreyasN707 ShreyasN707 left a comment

Choose a reason for hiding this comment

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

@EwoutH I have checked this PR locally and works. This is the notes:

Is it correct and current?

  • Uses current Mesa APIs (no deprecated features)
  • Fixes the FileNotFoundError caused by the relative data path
  • Example runs correctly from different working directories

Is the fix appropriate?

  • Uses a robust path resolution (os.path.dirname(__file__))
  • Change is minimal and does not affect model logic

Is it clean?

  • No unnecessary code changes
  • Clear and readable modification
  • PR description explains the issue and fix
  • Commits are clean and relevant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants