Python客户端和命令行
项目描述
cuOpt Service Python Thin Client
cuOpt Service Python Thin Client是一个Python接口,用于访问NVIDIA管理的cuOpt服务。
通过pip安装cuopt-thin-client
pip install cuopt-thin-client --extra-index-url=https://pypi.nvidia.com
运行thin客户端 - CLI
安装后,可以通过以下方式访问命令行实用程序
cuopt_cli -c credentials.json cuopt_problem_data.json
检查超时的请求状态
如果请求超时是因为问题解决需要很长时间,您将看到类似的结果
Request timed out.
Re-check the status with the following command:
cuopt_cli '{"reqId": "cde52321-693e-444c-aa77-f55a3f6b0105", "assetId": "3958447c-389d-40ea-bd69-bbf242a9ec80"}'
显示的JSON对象可以传递回cuopt_cli以检查状态。这可能需要多次执行,直到返回解决方案。JSON对象可能包含或不包含assetId
cuopt_cli '{"reqId": "cde52321-693e-444c-aa77-f55a3f6b0105", "assetId": "3958447c-389d-40ea-bd69-bbf242a9ec80"}'
2023-08-17 15:13:16.120 cuopt_thin_client.cuopt_service_client INFO Using Cached Token
...
参数列表(简写,更多详细信息请参阅程序帮助)
file: Filename or JSON string, required if -g is not specified.
Data may be a cuopt problem or contain reqId and assetId (if any)
as displayed in the output from a previous request which timed out.
-s File containing sak value in JSON. If this file is not supplied, the environment variable CUOPT_CLIENT_SAK will be used.
Only one of the two authetication methods (SAK or CLIENT ID & SECRET) can be used.
-c File containing client_id and secret values in JSON. If this file is not supplied, the environment variables CUOPT_CLIENT_SAK
or pair of CUOPT_CLIENT_ID and CUOPT_CLIENT_SECRET will be used. SAK takes precedence.
-cn Config file for setting client defaults in JSON. The format is {"defaults": {"function_name": "", "function_id": "", "function_version_id": ""}}
None are required, and function_id takes precedence over function_name. If set, function_version_id must be a valid version of the function identified by name or id.
-f Name or id of the cuOpt cloud function to call. If no name or id is specified and all available functions have the same name
then that name will be used. The latest version of the specified function will be called if the -i option is not set.
A default function name or id may be set in a config file with the -cn option.
-i The id of a specific version of the specified function to call. By default, the latest version will be called.
A default function version id may be set in a config file with the -cn option.
-g Print a list of available cuOpt functions and exit. Refreshes the version cache.
-l Log level
-p Number of seconds to poll for a result before timing out and returning a request id to re-query (defaults to 120)
-ov If set, only validates input and doesn't add to billing.
运行thin客户端 - python脚本
使用提供的客户端SAK初始化CuOptServiceClient
from cuopt_thin_client import CuOptServiceClient
cuopt_service_client = CuOptServiceClient(
sak=sak
)
或使用提供的客户端ID和密钥初始化CuOptServiceClient(已弃用)
from cuopt_thin_client import CuOptServiceClient
cuopt_service_client = CuOptServiceClient(
client_id=cuopt_client_id,
client_secret=cuopt_client_secret,
)
加载问题数据
with open(cuopt_problem_data_file_path, "r") as f:
cuopt_problem_data = json.load(f)
问题数据文件应包含以下详细信息的json
cost_waypoint_graph_data: Waypoint graph of Cost matrix
travel_time_waypoint_graph_data: Waypoint graph of Travel time matrix
cost_matrix_data: Cost matrix
travel_time_matrix_data: Travel time matrix
fleet_data: Fleet information
task_data: Task Waypoint graph of
solver_config: Solver settings
For more details see https://docs.nvda.net.cn/cuopt/user-guide/serv_api.html
获取优化路线
optimized_routes = cuopt_service_client.get_optimized_routes(
cuopt_problem_data
)
检查超时的先前请求状态
if "reqId" in cuopt_problem_data:
optimized_routes = cuopt_service_client.repoll(
cuopt_problem_data["reqId"],
cuopt_problem_data.get("assetId", None),
)
凭证文件格式
对于客户端ID和密钥
{
"CUOPT_CLIENT_ID" : "PASTE_YOUR_CLIENT_ID_HERE",
"CUOPT_CLIENT_SECRET" : "PASTE_YOUR_CLIENT_SECRET_HERE"
}
对于SAK
{
"CUOPT_CLIENT_SAK" : "PASTE_YOUR_CLIENT_SAK_HERE",
}
配置文件格式
此文件可用于在多个函数可用且客户端需要澄清调用哪个函数时设置函数名称、ID和/或版本的默认值。这是将这些值显式传递给客户端的替代方法。
当函数ID和函数名称都设置时,函数ID优先。如果两者都没有设置,并且所有函数名称都相同,则使用该名称。如果根据名称选择函数,客户端将选择该名称的最新函数。如果设置了函数版本,则该版本必须在ID或名称指定的函数中存在。如果没有设置,将选择该函数的最新版本。
{
"defaults": {
"function_name": "PASTE_FUNCTION_NAME_HERE",
"function_id": "PASTE_FUNCTION_ID_HERE",
"function_version_id": "PASTE_VERSION_ID_HERE"
}
}
项目详情
关闭
cuopt_thin_client-24.7.0.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 2e0a8b3e168ec0a9d6b5f44c7da0a71ed55a5c132e6f986cf7d5625a78e6d81a |
|
MD5 | 561619f56958a70600095f46bc1a6f19 |
|
BLAKE2b-256 | 853e941c0acc66c1b96c2a3eb1c72a42975bbf39b813ff20f0fa40a2225045d9 |