cuOpt Thin Client API Example#
Routing Example#
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))
response would be as follows,
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}
The function_id
can be found in NGC -> Cloud Functions -> Shared Functions. For more details, please check Quick Start Guide.
The data
argument to get_optimized_routes
may be a dictionary of the format shown in Get Routes Open-API spec.
More details of the response can be found under responses schema in open-api spec or same can be found in redoc as well.
It may also be the path of a file containing such a dictionary as JSON or written using the Python msgpack module (pickle is deprecated).
A JSON file may optionally be compressed with zlib.
LP Example#
Note
Linear Programming (LP) and Mixed Integer Linear Programming (MILP) are Early Access features and are currently open to only select customers.
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))
response would be as follows,
Normal mode response:
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}
Batch mode response:
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}
The data
argument to get_LP_solve
may be a dictionary of the format shown in LP Open-API spec.
More details on the response can be found under responses schema in open-api spec or same can be found in redoc as well.
Example with DataModel is available in the LP example notebook
MILP Example#
Note
Linear Programming (LP) and Mixed Integer Linear Programming (MILP) are Early Access features and are currently open to only select customers.
The major difference between this example and the prior LP example is that some of the variable are integers, so variable types need to be shared.
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))
response would be as follows,
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}
An example with DataModel is available in the MILP example notebook. More details on the response can be found under responses schema in open-api spec or same can be found in redoc as well.
The data
argument to get_LP_solve
may be a dictionary of the format shown in LP Open-API spec.
cuOpt Thin Client CLI Example#
Put your NVIDIA Identity Federation API Key
in a credentials.json file as below:
{
"CUOPT_CLIENT_SAK" : "PASTE_YOUR_NVIDIA_IDENTITY_FEDERATION_API_KEY"
}
Create a data.json file containing this sample data:
Routing Example#
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
Invoke the CLI
cuopt_cli data.json -f <FUNCTION_ID> -s credentials.json
LP Example#
Note
Linear Programming (LP) and Mixed Integer Linear Programming (MILP) are Early Access features and are currently open to only select customers.
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
Invoke the CLI:
cuopt_cli data.json -f <FUNCTION_ID> -t LP -s credentials.json
In the case of batch mode, you can send a bunch of mps
files at once, and acquire results. The batch mode works only for mps
in the case of CLI.
Note
Batch mode is not available for MILP problems.
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
As mentioned above function_id
can be found in NGC -> Cloud Functions -> Shared Functions. For more details, please check the Quickstart Guide.
MILP Example#
Note
Linear Programming (LP) and Mixed Integer Linear Programming (MILP) are Early Access features and are currently open to only select customers.
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
Invoke the CLI:
cuopt_cli data.json -f <FUNCTION_ID> -t LP -s credentials.json
Note
Batch mode is not supported for MILP.
Alternatively, you may set the NVIDIA Identity Federation API Key as CUOPT_CLIENT_SAK
in your environment and omit the -s
argument to cuopt_cli
.