跳到内容

命令行界面#

命令行界面允许用户运行非 Python 代码的目标函数。被传递并随后被调用的脚本(使用 Popen)需要返回标准输出,该输出将被解释以执行优化过程。

注意

在 SMAC v2.0 中,SMAC 不能直接从命令行调用。相反,用户应该使用 Python 接口调用 SMAC。命令行接口在 SMAC v1.4 中仍然可用。

目标函数的调用#

以下示例展示了如何调用脚本

filename --instance=test --instance_features=test --seed=0 --hyperparameter1=5323

然而,与 Python 目标函数一样,诸如实例或预算等参数取决于使用了哪些组件。超参数取决于配置空间。变量 filename 可以是 ./path/to/your/script.sh 之类的路径。

我们建议在 Bash 脚本中使用以下代码接收参数。请注意,用户不仅限于 Bash 脚本,也可以使用可执行文件、Python 脚本或任何其他类型的文件。

注意

由于脚本仅通过文件名调用,请确保标记文件类型(例如,#!/bin/bash#!/usr/bin/env python)。

警告

每次传递实例时,也会以逗号分隔(无空格)的浮点数列表形式传递实例特征。如果未提供实例特征,则传递空列表。

#!/bin/bash

# Set arguments first
for argument in "$@"
do
    key=$(echo $argument | cut -f1 -d=)
    value=$(echo $argument | cut -f2 -d=)   

    if [[ $key == *"--"* ]]; then
        v="${key/--/}"
        declare $v="${value}" 
    fi
done

echo $instance
echo $hyperparameter1

目标函数的返回#

脚本必须返回以下格式的 stdout (echo 或 print)(忽略空格)

cost=0.5; runtime=0.01; status=SUCCESS; additional_info=test (single-objective)
cost=0.5, 0.4; runtime=0.01; status=SUCCESS; additional_info=test (multi-objective)

除成本之外,所有参数都是可选的,并以分号分隔。状态字符串必须匹配 StatusType 中的某个值。

启动优化#

优化将通过普通的 Python 接口启动。唯一的区别是你需要传递一个字符串作为目标函数,而不是一个 Python 函数。

!! 警告

Your script needs to have rights to be executed (e.g., update the rights with ``chmod``).
...
smac = BlackBoxFacade(scenario, target_function="./path/to/your/script.sh")
incumbent = smac.optimize()
...