Skip to content

Ironman9527/UnityPathFinding

Repository files navigation

Windows 下使用 conda 创建环境并运行

本文档说明如何在 Windows(PowerShell)上为本仓库创建 Python 虚拟环境,并运行训练与推理脚本。

1. 安装 Conda

  • 安装 Miniconda 或 Anaconda。
  • 打开 PowerShell,确保 conda --version 可用。

2. 创建并激活环境(CPU 默认, 推荐使用GPU)

cd "YourPath\GameEnvUnity\UnityPathFinding"
conda env create -f environment.yml
conda activate unity-pathfinding

验证 PyTorch 与 NumPy 是否可用:

python -c "import torch, numpy as np; print('torch', torch.__version__, 'cuda?', torch.cuda.is_available()); print('numpy', np.__version__)"

3. 可选:安装 GPU 版本 PyTorch(CUDA)

环境默认装的是 CPU 版(通过 cpuonly)。如需 GPU:

conda remove -n unity-pathfinding cpuonly --yes
conda install -n unity-pathfinding pytorch pytorch-cuda=12.4 -c pytorch -c nvidia --yes
python -c "import torch; print(torch.version.cuda, torch.cuda.is_available())"

如需使用 pip 安装(不推荐,按需选择):

pip install -r requirements.txt
# CPU 版 PyTorch:
pip install torch --index-url https://download.pytorch.org/whl/cpu

4. Unity 可执行文件路径

  • Unity 环境默认可执行路径:BasicPathfindingEnvironment/BasicPathfindingEnvironment.exe
  • 如路径不同,请在脚本参数或配置中指定。例如:
    • 训练:ppo_batched.pyCONFIG["unity"]["executablePath"]
    • 推理:infer.py 运行时传 --exe 参数

5. 运行推理(单/少量智能体)

# 示例:修改端口/智能体数量/是否确定性
python infer.py --ip 127.0.0.1 --port 5555 --numAgents 1 --timeout 9999 --exe BasicPathfindingEnvironment/BasicPathfindingEnvironment.exe --deterministic True --steps 100000
  • 模型权重默认路径:GymlikeEnvWrapper/ppo/checkpoints/best_cf_0.888_u115.pt(按需替换)。
  • 终止:Ctrl + C。

6. 运行训练(批量智能体 + PPO)

ppo_batched.py 中编辑 CONFIG,尤其是:

  • CONFIG["unity"]["port"]:确保端口空闲;
  • CONFIG["unity"]["executablePath"]:指向正确的 Unity 可执行文件。

运行:

python ppo_batched.py

检查点默认保存目录:GymlikeEnvWrapper/ppo/checkpoints

7. 常见问题排查

  • 端口占用:修改 --portCONFIG["unity"]["port"]
  • Unity 路径错误:确认 BasicPathfindingEnvironment.exe 路径存在。
  • ZMQ 超时:检查防火墙与 ip/port/timeout;首次运行可能弹出防火墙询问,允许即可。
  • CUDA 不可用:确认已用 pytorch + pytorch-cuda 安装,并更新显卡驱动与 CUDA 运行时。

About

基于Unity实现的简易寻路环境

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published