This article explains how to upload, configure, and solve optimization models programmatically using the Rose API (with a Python example). This can also be translated into other programming languages.
The full API docs can be found here.
The full process consists of three main steps:
- Authentication - Secure access using your API key and secret
- Uploading the model - Send your existing model file to the Rose platform
- Solver configuration and execution - Choose solver settings and initiate the solve
Prerequisites
Before you begin:
- Authenticate and load your access token
- The
load_token()andauthenticate()functions are given in the Authenticating and getting a token article
- The
- You must know the path to your .mps or .lp optimization file you want to solve.
Uploading the model
Before uploading your model file to the Rose API, you must request a pre-signed URL. This is a temporary, secure link that allows you to upload your model directly. You need to define a PROBLEM_ID, the name that you want your model to appear as in the system. This should include the appropriate extension (.mps for MPS file or .lp for LP files). You also need to pass in a valid access token to get a pre-signed URL.
To verify that your file was uploaded correctly and without corruption, in the request you must include the file’s SHA-256 checksum, encoded in Base64. If the uploaded file’s checksum does not match the provided value, the file is rejected.
Once you receive the pre-signed URL in the response, you can upload your model file to that location using a standard HTTP PUT request. The pre-signed URL is valid for 1 hour. The file content must match the format implied by the file extension in the PROBLEM_ID.
Solver configuration and execution
In this step, you’ll submit a solve request that references:
- One or more solver configurations (e.g., time limits, solver strategies), and
- One or more previously uploaded problems (by
PROBLEM_ID)
Below is an example with one solver configuration and one problem. But multiple solver configurations can be specified and multiple problems can be started at once.
Complete code example
import base64
import hashlib
import json
MODEL_FILE_PATH = "./path/to/bubbles_brewery.mps"
PROBLEM_ID = "bubbles_brewery.mps"
def checksum_sha256_b64_from_file(path: str) -> str:
h = hashlib.sha256()
with open(path, "rb") as f:
for chunk in iter(lambda: f.read(256 * 1024), b""):
h.update(chunk)
return base64.b64encode(h.digest()).decode("ascii")
def request_upload_url(token: str, problem_id: str, checksum_sha256: str):
url = f"{BASE_URL}/problem/{problem_id}"
headers = {"Authorization": f"Bearer {token}"}
response = requests.post(url, headers=headers, json={"checksum_sha256": checksum_sha256})
response.raise_for_status()
payload = response.json()
return payload['upload_url']
def upload_file_to_presigned_url(upload_url: str, file_path: str, checksum_sha256: str):
headers = {"x-amz-checksum-sha256": checksum_sha256}
with open(file_path, "rb") as f:
response = requests.put(upload_url, data=f, headers=headers)
response.raise_for_status()
print("📤 File uploaded successfully.")
# Start solving step
def submit_solve(token):
url = f"{BASE_URL}/solve"
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json"
}
payload = {
"solver_config": [
{
"basic": {
"max_time_seconds": 3600,
},
},
],
"problems": [
{ "name": PROBLEM_ID }
],
"solves": [
{
"problem": 0,
"solver_config": 0
}
]
}
response = requests.put(url, headers=headers, json=payload)
if response.status_code != 200:
print("❌ Solve submission failed.")
print("Status code:", response.status_code)
print("Response:", response.text)
response.raise_for_status()
solve_ids = response.json()["solve_ids"]
print(f"🚀 Solve submitted. Solve IDs:")
return solve_ids
# Main workflow
if __name__ == "__main__":
token = load_token() or authenticate()
print("✅ Authenticated")
print("🔢 Calculating checksum")
checksum = checksum_sha256_b64_from_file(MODEL_FILE_PATH)
print("🔗 Requesting upload URL...")
upload_url = request_upload_url(token, PROBLEM_ID, checksum)
print("⬆️ Uploading MPS file...")
# upload_file_to_presigned_url(upload_url, MODEL_FILE_PATH, checksum)
print("🧮 Submitting solve...")
solve_ids = submit_solve(token)
print(json.dumps(solve_ids, indent=4))
Comments
0 comments
Please sign in to leave a comment.