🐉

2020/12/29に公開

# 問題設定

• パラメータとして「drop penalty」を「#vehicles」を想定します
• これを外から管理したいです

# hydra example

## 簡単な例

• ディレクトリ構造です
``````.
├── conf
│   └── default.yaml
├── experiment.py
└── solver.py
``````
conf/default.yaml
``````solver:
penalty: 1000
num_vehicle: 4
``````
• 具体的にPythonで呼び出す例です．
experiment.py
``````import os
import hydra
from solver import main
from omegaconf import OmegaConf

@hydra.main(config_name="conf/default.yaml")
def run_experiment(cfg):
print(OmegaConf.to_yaml(cfg))
ds, td, tl = main(cfg.solver.penalty, cfg.solver.num_vehicle)
print("Dropped nodes  :", ds)
print("Total distance :", td)

if __name__ == '__main__':
run_experiment()
``````

## 実行

デフォルトパラメータで呼び出してみます．

``````\$ python experiment.py
solver:
penalty: 1000
num_vehicle: 4

Dropped nodes  : {16, 15}
Total distance : 5548
``````

パラメータを上書きして呼び出してみます．

``````\$ python experiment.py solver.penalty=500
solver:
penalty: 500
num_vehicle: 4

Dropped nodes  : {8, 16, 14, 15}
Total distance : 4452
``````

## multirunの使用例

hydraにまかせて見ます．コマンド呼び出しは以下の形です．

``````\$ python experiment.py -m solver.penalty=100,250,500,1000,2000
``````

``````[2020-12-29 13:43:24,342][HYDRA] Launching 5 jobs locally
[2020-12-29 13:43:24,342][HYDRA]        #0 : solver.penalty=100
Dropped nodes  : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
Total distance : 0
[2020-12-29 13:43:25,593][HYDRA]        #1 : solver.penalty=250
Dropped nodes  : {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
Total distance : 0
[2020-12-29 13:43:26,846][HYDRA]        #2 : solver.penalty=500
Dropped nodes  : {8, 16, 14, 15}
Total distance : 4452
[2020-12-29 13:43:28,087][HYDRA]        #3 : solver.penalty=1000
Dropped nodes  : {16, 15}
Total distance : 5548