cuOpt Thin Client API 示例#
路径规划示例#
1import os
2from cuopt_thin_client import CuOptServiceClient
3import json
4
5data = {"cost_matrix_data": {"data": {"0": [[0,1],[1,0]]}},
6 "task_data": {"task_locations": [0,1]},
7 "fleet_data": {"vehicle_locations": [[0,0],[0,0]]}}
8
9# Load the credential "NVIDIA Identity Federation API Key" from the environment or some other way
10
11sak = os.environ["NVIDIA_IDENTITY_FEDERATION_API_KEY"]
12
13cuopt_service_client = CuOptServiceClient(
14 sak=sak,
15 function_id=<FUNCTION_ID_OBTAINED_FROM_NGC>
16)
17
18optimized_routes = cuopt_service_client.get_optimized_routes(data)
19print(json.dumps(optimized_routes, indent=4))
响应如下:
1{ 2"response": { 3 "solver_response": { 4 "status": 0, 5 "num_vehicles": 1, 6 "solution_cost": 2.0, 7 "objective_values": { 8 "cost": 2.0 9 }, 10 "vehicle_data": { 11 "0": { 12 "task_id": [ 13 "Depot", 14 "0", 15 "1", 16 "Depot" 17 ], 18 "arrival_stamp": [ 19 0.0, 20 0.0, 21 0.0, 22 0.0 23 ], 24 "type": [ 25 "Depot", 26 "Delivery", 27 "Delivery", 28 "Depot" 29 ], 30 "route": [ 31 0, 32 0, 33 1, 34 0 35 ] 36 } 37 }, 38 "dropped_tasks": { 39 "task_id": [], 40 "task_index": [] 41 } 42 } 43}, 44"reqId": "ebd378a3-c02a-47f3-b0a1-adec81be7cdd" 45}
function_id
可以在 NGC -> Cloud Functions -> Shared Functions 中找到。更多详情,请查看 快速入门指南。
get_optimized_routes
的 data
参数可以是字典格式,如 Get Routes Open-API 规范 中所示。有关响应的更多详细信息,请参阅 open-api 规范 中的响应架构,或者在 redoc 中也可以找到。它也可以是包含此类字典的文件的路径,格式为 JSON 或使用 Python msgpack 模块写入(pickle 已弃用)。JSON 文件可以选择使用 zlib 压缩。
LP 示例#
注意
线性规划 (LP) 和混合整数线性规划 (MILP) 是 Early Access 功能,目前仅对部分客户开放。
1import os
2from cuopt_thin_client import CuOptServiceClient
3import json
4
5data = {
6 "csr_constraint_matrix": {
7 "offsets": [0, 2, 4],
8 "indices": [0, 1, 0, 1],
9 "values": [3.0, 4.0, 2.7, 10.1]
10 },
11 "constraint_bounds": {
12 "upper_bounds": [5.4, 4.9],
13 "lower_bounds": ["ninf", "ninf"]
14 },
15 "objective_data": {
16 "coefficients": [0.2, 0.1],
17 "scalability_factor": 1.0,
18 "offset": 0.0
19 },
20 "variable_bounds": {
21 "upper_bounds": ["inf", "inf"],
22 "lower_bounds": [0.0, 0.0]
23 },
24 "maximize": False,
25 "solver_config": {
26 "tolerances": {
27 "optimality": 0.0001
28 }
29 }
30}
31
32# Load the credential "NVIDIA Identity Federation API Key" from the environment or some other way
33
34sak = os.environ["NVIDIA_IDENTITY_FEDERATION_API_KEY"]
35
36cuopt_service_client = CuOptServiceClient(
37 sak=sak,
38 function_id=<FUNCTION_ID_OBTAINED_FROM_NGC>
39)
40
41solution = cuopt_service_client.get_LP_solve(data, response_type="dict")
42
43print("---------- Normal mode --------------- \n", json.dumps(solution, indent=4))
44
45# For batch mode, send list of mps/dict/DataModel
46
47solution = cuopt_service_client.get_LP_solve([data, data], response_type="dict")
48
49print("---------- Batch mode ----------------- \n", json.dumps(solution, indent=4))
响应如下:
正常模式响应
1{
2"response": {
3 "solver_response": {
4 "status": 1,
5 "solution": {
6 "primal_solution": [
7 0.0,
8 0.0
9 ],
10 "dual_solution": [
11 0.0,
12 0.0
13 ],
14 "primal_objective": 0.0,
15 "dual_objective": 0.0,
16 "solver_time": 38.0,
17 "vars": {},
18 "lp_statistics": {
19 "primal_residual": 0.0,
20 "dual_residual": 0.0,
21 "gap": 0.0,
22 "reduced_cost": [
23 0.2,
24 0.1
25 ]
26 }
27 }
28 }
29},
30"reqId": "39c52105-736d-4383-a101-707390937141"
31}
批量模式响应
1{
2"response": {
3 "solver_response": [
4 {
5 "status": 1,
6 "solution": {
7 "primal_solution": [
8 0.0,
9 0.0
10 ],
11 "dual_solution": [
12 0.0,
13 0.0
14 ],
15 "primal_objective": 0.0,
16 "dual_objective": 0.0,
17 "solver_time": 5.0,
18 "vars": {},
19 "lp_statistics": {
20 "primal_residual": 0.0,
21 "dual_residual": 0.0,
22 "gap": 0.0,
23 "reduced_cost": [
24 0.2,
25 0.1
26 ]
27 }
28 }
29 },
30 {
31 "status": 1,
32 "solution": {
33 "primal_solution": [
34 0.0,
35 0.0
36 ],
37 "dual_solution": [
38 0.0,
39 0.0
40 ],
41 "primal_objective": 0.0,
42 "dual_objective": 0.0,
43 "solver_time": 3.0,
44 "vars": {},
45 "lp_statistics": {
46 "primal_residual": 0.0,
47 "dual_residual": 0.0,
48 "gap": 0.0,
49 "reduced_cost": [
50 0.2,
51 0.1
52 ]
53 }
54 }
55 }
56 ],
57 "total_solve_time": 9.0
58},
59"reqId": "f04a6936-830e-4235-b535-68ad51736ac0"
60}
get_LP_solve
的 data
参数可以是字典格式,如 LP Open-API 规范 中所示。有关响应的更多详细信息,请参阅 open-api 规范 中的响应架构,或者在 redoc 中也可以找到。
使用 DataModel 的示例可在 LP 示例 notebook 中找到
MILP 示例#
注意
线性规划 (LP) 和混合整数线性规划 (MILP) 是 Early Access 功能,目前仅对部分客户开放。
此示例与之前的 LP 示例之间的主要区别在于,某些变量是整数,因此需要共享变量类型。
1import os
2from cuopt_thin_client import CuOptServiceClient
3import json
4
5data = {
6 "csr_constraint_matrix": {
7 "offsets": [0, 2],
8 "indices": [0, 1],
9 "values": [1.0, 1.0]
10 },
11 "constraint_bounds": {
12 "upper_bounds": [5000.0],
13 "lower_bounds": [0.0]
14 },
15 "objective_data": {
16 "coefficients": [1.2, 1.7],
17 "scalability_factor": 1.0,
18 "offset": 0.0
19 },
20 "variable_bounds": {
21 "upper_bounds": [3000.0, 5000.0],
22 "lower_bounds": [0.0, 0.0]
23 },
24 "maximize": True,
25 "variable_names": ["x", "y"],
26 "variable_types": ["I", "I"],
27 "solver_config":{
28 "time_limit": 30,
29 "tolerances": {
30 "optimality": 0.0001
31 }
32 }
33}
34
35# Load the credential "NVIDIA Identity Federation API Key" from the environment or some other way
36
37sak = os.environ["NVIDIA_IDENTITY_FEDERATION_API_KEY"]
38
39cuopt_service_client = CuOptServiceClient(
40 sak=sak,
41 function_id=<FUNCTION_ID_OBTAINED_FROM_NGC>
42)
43
44solution = cuopt_service_client.get_LP_solve(data, response_type="dict")
45
46print(json.dumps(solution, indent=4))
响应如下:
1{
2"response": {
3 "solver_response": {
4 "status": 1,
5 "solution": {
6 "primal_solution": [
7 0.0,
8 0.0
9 ],
10 "dual_solution": [
11 0.0
12 ],
13 "primal_objective": 0.0,
14 "dual_objective": 0.0,
15 "solver_time": 0.0,
16 "vars": {
17 "x": 0.0,
18 "y": 0.0
19 },
20 "lp_statistics": {
21 "primal_residual": 0.0,
22 "dual_residual": 0.0,
23 "gap": 0.0,
24 "reduced_cost": [
25 0.0,
26 0.0
27 ]
28 }
29 }
30 }
31},
32"reqId": "97e61936-e503-423b-b932-709cfac56424"
33}
使用 DataModel 的示例可在 MILP 示例 notebook 中找到。有关响应的更多详细信息,请参阅 open-api 规范 中的响应架构,或者在 redoc 中也可以找到。
get_LP_solve
的 data
参数可以是字典格式,如 LP Open-API 规范 中所示。
cuOpt Thin Client CLI 示例#
将您的 NVIDIA Identity Federation API Key
放入 credentials.json 文件中,如下所示
{
"CUOPT_CLIENT_SAK" : "PASTE_YOUR_NVIDIA_IDENTITY_FEDERATION_API_KEY"
}
创建一个包含此示例数据的 data.json 文件
路径规划示例#
echo '{"cost_matrix_data": {"data": {"0": [[0, 1], [1, 0]]}},
"task_data": {"task_locations": [0, 1]},
"fleet_data": {"vehicle_locations": [[0, 0], [0, 0]]}}' > data.json
调用 CLI
cuopt_cli data.json -f <FUNCTION_ID> -s credentials.json
LP 示例#
注意
线性规划 (LP) 和混合整数线性规划 (MILP) 是 Early Access 功能,目前仅对部分客户开放。
echo '{
"csr_constraint_matrix": {
"offsets": [0, 2, 4],
"indices": [0, 1, 0, 1],
"values": [3.0, 4.0, 2.7, 10.1]
},
"constraint_bounds": {
"upper_bounds": [5.4, 4.9],
"lower_bounds": ["ninf", "ninf"]
},
"objective_data": {
"coefficients": [0.2, 0.1],
"scalability_factor": 1.0,
"offset": 0.0
},
"variable_bounds": {
"upper_bounds": ["inf", "inf"],
"lower_bounds": [0.0, 0.0]
},
"maximize": "False",
"solver_config": {
"tolerances": {
"optimality": 0.0001
}
}
}' > data.json
调用 CLI
cuopt_cli data.json -f <FUNCTION_ID> -t LP -s credentials.json
在批量模式下,您可以一次发送一堆 mps
文件,并获取结果。批量模式仅适用于 CLI 中的 mps
文件。
注意
批量模式不适用于 MILP 问题。
echo "* optimize
* cost = 0.2 * VAR1 + 0.1 * VAR2
* subject to
* 3 * VAR1 + 4 * VAR2 <= 5.4
* 2.7 * VAR1 + 10.1 * VAR2 <= 4.9
NAME good-1
ROWS
N COST
L ROW1
L ROW2
COLUMNS
VAR1 COST 0.2
VAR1 ROW1 3 ROW2 2.7
VAR2 COST 0.1
VAR2 ROW1 4 ROW2 10.1
RHS
RHS1 ROW1 5.4 ROW2 4.9
ENDATA" > sample.mps
cuopt_cli sample.mps sample.mps sample.mps -f <FUNCTION_ID> -t LP -s credentials.json
如上所述,function_id
可以在 NGC -> Cloud Functions -> Shared Functions 中找到。更多详情,请查看快速入门指南。
MILP 示例#
注意
线性规划 (LP) 和混合整数线性规划 (MILP) 是 Early Access 功能,目前仅对部分客户开放。
echo '{
"csr_constraint_matrix": {
"offsets": [0, 2, 4],
"indices": [0, 1, 0, 1],
"values": [3.0, 4.0, 2.7, 10.1]
},
"constraint_bounds": {
"upper_bounds": [5.4, 4.9],
"lower_bounds": ["ninf", "ninf"]
},
"objective_data": {
"coefficients": [0.2, 0.1],
"scalability_factor": 1.0,
"offset": 0.0
},
"variable_bounds": {
"upper_bounds": ["inf", "inf"],
"lower_bounds": [0.0, 0.0]
},
"variable_names": ["x", "y"],
"variable_types": ["I", "I"],
"maximize": "False",
"solver_config": {
"time_limit": 30,
"tolerances": {
"optimality": 0.0001
}
}
}' > data.json
调用 CLI
cuopt_cli data.json -f <FUNCTION_ID> -t LP -s credentials.json
注意
批量模式不支持 MILP。
或者,您可以将 NVIDIA Identity Federation API Key 设置为环境变量 CUOPT_CLIENT_SAK
,并省略 cuopt_cli
的 -s
参数。