托管服务客户端 API 参考#

服务客户端#

class cuopt_thin_client.CuOptServiceClient(
client_id: str = '',
client_secret: str | None = None,
sak: str = '',
function_name: str = '',
function_id: str = '',
function_version_id: str = '',
polling_interval: int = 1,
token_expiration_padding: int = 120,
request_excess_timeout: int | None = None,
api_path: str = '',
disable_compression=False,
disable_version_string=False,
only_validate=False,
config_path='',
exec_timeout=300,
polling_timeout=120,
timeout_exception=True,
)#

CuOptServiceClient 通过 NVIDIA 云函数 (NVCF) 处理对 cuOpt 服务的请求

参数:
  • client_id (str) – 注意:此参数已弃用,请使用 SAK。注册过程中获取的客户端 ID。两种授权方法(SAK 或 CLIENT ID-SECRET)中只能使用一种。

  • client_secret (str) – 注意:此参数已弃用,请使用 SAK。注册过程中获取的客户端密钥。两种授权方法(SAK 或 CLIENT ID-SECRET)中只能使用一种。

  • sak (str) – 通过 NGC 获取的 sak。两种授权方法(SAK 或 CLIENT ID-SECRET)中只能使用一种。

  • function_name (str) – 函数的名称,在注册期间提供,或通过 function_list API 调用发现。如果所有可用函数都具有相同的名称,或者如果已设置 function_id,则可以省略此值。客户端将选择此名称的函数的最新可用版本。如果设置了 function_id,则忽略此值。

  • function_id (str) – 函数的唯一标识符,在注册期间提供,或通过 function_list API 调用发现。如果同时设置了 function_name 和 function_id,则 function_id 优先。客户端将选择此 ID 的函数的最新可用版本。

  • function_version_id (str) – 选择按名称或 ID 指定的函数的特定版本。仅当有多个版本可用且 API 不兼容时,才应使用此项。如果省略此值,客户端将选择函数的最新版本。

  • polling_interval (int) – 连续轮询尝试之间的时间间隔,以秒为单位。默认为 1。

  • token_expiration_padding (int) – 令牌到期时间之前的缓冲时间,以秒为单位,在此期间将请求新令牌。默认为 120。

  • request_excess_timeout (int) – 轮询请求完成的时间,以秒为单位。如果轮询时间在请求完成之前到期,客户端可能会重新轮询请求。如果设置为 None,客户端将永不超时,并将无限期地轮询。默认为 120。

  • api_path (str) – 注意:已弃用。为 cuOpt 设置 auth/api 端点,仅对 NVIDIA 测试有用。

  • disable_compression (boolean) – 禁用大型文件的 zlib 压缩。

  • disable_version_string (boolean) – 不向服务器发送客户端版本。

  • only_validate (boolean) – 仅验证输入,不增加计费

  • config_path (boolean) – JSON 配置文件的路径,用于在 JSON 中设置客户端默认值。如果未将相应的参数传递给 __init__,则将使用这些值。格式为:{ “defaults”: { “function_name”: “”, “function_id”: “”, “function_version_id”: “” } }

  • exec_timeout (int) – 用于向托管服务发送和接收数据的 Http 请求超时。通常不需要更改此值。默认为 300 秒。

  • polling_timeout (int) – 客户端在退出并返回请求 ID 之前轮询结果的时间,以秒为单位。可以在对 repoll() 的调用中再次轮询请求 ID。如果设置为 None,客户端将永不超时,并将无限期地轮询。默认为 120。

  • timeout_exception (boolean) – 如果为 True,则如果 polling_timeout 秒在返回解决方案之前过去,客户端将返回 # noqa TimeoutError 异常。异常的值包含 JSON,其中提供请求 ID 和资产 ID(如果有),以便可以调用 repoll() 以再次轮询结果。如果为 False,客户端将返回包含 repoll 信息的字典,而不会出现异常。默认为 True。

get_LP_solve(
cuopt_data_models,
solver_config=<solver_settings.solver_settings.SolverSettings object>,
response_type='obj',
)#

获取给定问题的线性规划解。

参数:
  • cuopt_data_models

    注意 - 批量模式仅适用于 LP,不适用于 MILP。

    mps 或 json/dict/DataModel 的文件路径,由 cuopt_mps_parser/list[mps 文件路径]/list[dict]/list[DataModel] 返回。

    对于单个问题,输入应为 mps/json 文件的路径、/DataModel(由 cuopt_mps_parser 返回)/json 文件路径/字典。

    对于批量问题,输入应为 mps 文件路径列表/DataModel 列表(由 cuopt_mps_parser 返回)/字典列表。

  • solver_config ((SolverSettings objectDict可选) 包含) – 求解器设置,包括容差值。有关求解器设置的详细信息,请参阅 LP 文档。

  • response_type (str) – 如果应将响应作为字典返回,则选择“dict”;如果应将响应作为 Solution 对象返回,则选择“obj”。默认为“obj”

  • Returns (dictSolution 对象。)

get_func_defaults_from_config(name, id, vid)#
get_functions()#

列出 NVCF 中用户的所有可用函数。

get_optimized_routes(cuopt_problem_json_data)#

获取给定问题的优化路由解决方案。

参数:

cuopt_problem_json_data (dictstr) – 这是问题数据(字典形式)或包含问题数据的文件路径。该文件可以是包含字典的 msgpack 文件,也可以是包含 JSON 格式字典的文本文件,也可以是包含 JSON 格式字典的 zlib 压缩文件。有关此字典的结构,请参阅服务器文档。

repoll(req_id, asset_id=None, response_type='obj')#

当 get_optimized_routes 导致 TimeoutError 异常时,轮询结果。req_id 和 asset_id 在异常中返回。

参数:
  • req_id (str) – 标识原始请求的 uuid,在 TimeoutError 异常中返回。

  • asset_id (str) – 标识原始请求使用的资产(如果有)的 uuid,在 TimeoutError 异常中返回。返回结果后,客户端将删除此资产。

  • response_type (str) – 对于 LP 问题,如果应将响应作为字典返回,则选择“dict”;如果应将响应作为 Solution 对象返回,则选择“obj”。默认为“obj”。对于 VRP 问题,将忽略 response_type,始终返回 dict。

set_function_by_id(id, version_id=None)#

通过 ID 设置要调用的函数,并可选择指定版本。可以使用 get_functions() 检索当前函数列表。

参数:
  • id (str) – 要调用的函数的 ID。

  • version_id (str) – 要调用的函数的可选版本 ID。如果由 ID 指定的函数有多个版本,则可以使用 version_id 选择特定版本。如果未设置 version_id,将选择指定函数的最新版本。

set_function_by_name(name, version_id=None)#

通过名称设置要调用的函数,并可选择指定版本。可以使用 get_functions() 检索当前函数列表。

参数:
  • name (str) – 要调用的函数的名称。如果 name 是空字符串,并且所有可用函数都具有相同的名称,则将使用该名称。

  • version_id (str) – 要调用的函数的可选版本 ID。如果命名函数有多个版本,则可以使用 version_id 选择特定版本。如果未设置 version_id,将选择命名函数的最新版本。

LP 支持类#

请参阅瘦客户端支持类

服务 CLI#

 1usage: cuopt_cli [-h] [-t TYPE] [-ss SOLVER_SETTINGS] [-s SAK]
 2                 [-c CREDENTIALS] [-cn CONFIG] [-f FUNC] [-i FVID] [-g]
 3                 [-l {critical,error,warning,info,debug}] [-p POLL_TIMEOUT]
 4                 [-pi POLL_INTERVAL] [-nc] [-nv] [-ov] [-e EXEC_TIMEOUT] [-v]
 5                 [file ...]
 6
 7Solve a cuOpt problem using a managed service client.
 8
 9positional arguments:
10  file                  Filename or JSON string, required if -g is not
11                        specified. Data may be a cuopt problem or contain
12                        reqId and assetId (if any) as displayed in the output
13                        from a previous request which timed out.For VRP and
14                        MILP a single problem file is expected. For LP
15                        multiple problem input files can be passed for batch
16                        mode.
17
18options:
19  -h, --help            show this help message and exit
20  -t TYPE, --type TYPE  The type of problem to solve. Supported options are
21                        VRP and LP. If the type is not supplied, problem
22                        defaults to VRP
23  -ss SOLVER_SETTINGS, --solver-settings SOLVER_SETTINGS
24                        Filename or JSON string containing solver settings for
25                        LP problem type
26  -s SAK, --sak SAK     File containing sak value in JSON. If this file is not
27                        supplied, the environment variable CUOPT_CLIENT_SAK
28                        will be used. Only one of the two authetication
29                        methods (SAK or CLIENT ID & SECRET) can be used.
30  -c CREDENTIALS, --credentials CREDENTIALS
31                        Note: This is deprecated, use -s option for SAK.File
32                        containing client_id and secret values in JSON. If
33                        this file is not supplied, the environment variables
34                        CUOPT_CLIENT_SAK or pair ofCUOPT_CLIENT_ID and
35                        CUOPT_CLIENT_SECRET will be used.Only one of the two
36                        authetication methods(SAK or CLIENT ID & SECRET) can
37                        be used.In case both are provided, SAK takes
38                        precedence.
39  -cn CONFIG, --config CONFIG
40                        Config file for setting client defaults in JSON. The
41                        format is {"defaults": {"function_name": "",
42                        "function_id": "", "function_version_id": ""}}. None
43                        are required, and function_id takes precedence over
44                        function_name. If set, function_version_id must be a
45                        valid version of the function identified by name or id
46  -f FUNC, --func FUNC  name or id of the cuOpt cloud function to call. If no
47                        name or id is specified and all available functions
48                        have the same name then that name will be used. The
49                        latest version of the specified function will be
50                        called if the -i option is not set.
51  -i FVID, --fvid FVID  The id of a specific version of the specified function
52                        to call. This should only be used when multiple
53                        versions of the same function are available and a
54                        version other than the latest is desired. By default,
55                        the latest version will be called.
56  -g, --get-functions   Print a list of available cuOpt functions and exit.
57                        Refreshes the version cache.
58  -l {critical,error,warning,info,debug}, --log-level {critical,error,warning,info,debug}
59                        Log level
60  -p POLL_TIMEOUT, --poll-timeout POLL_TIMEOUT
61                        Number of seconds to poll for a result before timing
62                        out and returning a request id to re-query (defaults
63                        to 120)
64  -pi POLL_INTERVAL, --poll-interval POLL_INTERVAL
65                        How often to poll in seconds. Mininum is 1. Default is
66                        5.
67  -nc, --no-compress    Disable zlib compression for uncompressed data files
68                        larger than 250kb. For most cases zlib compression
69                        will improve cuOpt download speed and should be left
70                        enabled.This flag may be used to preserve backward
71                        compatibility with older cuOpt versions that do not
72                        support zlib compression.
73  -nv, --no-version-string
74                        Do not send the client version to the server. This
75                        flag may be used to preserve backward compatibility
76                        with older cuOpt versions that do not support the
77                        version string.
78  -ov, --only-validation
79                        If set, only validates input and doesn't add to
80                        billing.
81  -e EXEC_TIMEOUT, --exec-timeout EXEC_TIMEOUT
82                        http timeout when invoking a function. The server may
83                        wait up to 5 minutes before returning a pending-
84                        evalution result on long jobs. Most likely this does
85                        not need to be modified (defaults to 300)
86  -v, --version         Print client version and exit.