solve_ortools_problem
Solve constraint programming problems with Google OR-Tools. Define variables, constraints, and objectives in a structured format to find optimal solutions for complex scenarios.
Instructions
Solve a constraint programming problem using Google OR-Tools.
This tool takes a constraint programming problem defined with variables,
constraints, and an optional objective, and returns a solution if one exists.
Important Note:
Each constraint expression must be a single evaluable Python statement.
You cannot use Python control flow (loops, if statements) in the expressions.
Instead, you need to generate separate constraints for each case.
Example:
Nurse Scheduling Problem:
```python
# Schedule 4 nurses across 3 shifts over 3 days
shifts_var = Variable(
name="shifts_var",
type=VariableType.BOOLEAN,
shape=[4, 3, 3], # [nurses, days, shifts]
description="Binary variable indicating if a nurse works a shift",
)
constraints = []
# INCORRECT - This will fail:
# Constraint(
# expression=(
# "for d in range(3): for s in range(3): "
# "model.add(sum([shifts_var[n][d][s] for n in range(4)]) == 1)"
# )
# )
# CORRECT - Add each constraint separately:
# Each shift must have exactly one nurse
for d in range(3):
for s in range(3):
constraints.append(
Constraint(
expression=f"model.add(sum([shifts_var[n][{d}][{s}] for n in range(4)]) == 1)",
description=f"One nurse for day {d}, shift {s}",
)
)
# Each nurse works at most one shift per day
for n in range(4):
for d in range(3):
constraints.append(
Constraint(
expression=f"model.add(sum([shifts_var[{n}][{d}][s] for s in range(3)]) <= 1)",
description=f"Max one shift for nurse {n} on day {d}",
)
)
# Each nurse works 2-3 shifts total
for n in range(4):
constraints.append(
Constraint(
expression=f"model.add(sum([shifts_var[{n}][d][s] for d in range(3) for s in range(3)]) >= 2)",
description=f"Min shifts for nurse {n}",
)
)
problem = Problem(
variables=[shifts_var],
constraints=constraints,
description="Hospital nurse scheduling problem",
)
```
Args:
problem: The problem definition with variables, constraints, and optional objective
Returns:
A list of TextContent containing the solution or an error message
Input Schema
Name | Required | Description | Default |
---|---|---|---|
problem | Yes |
Input Schema (JSON Schema)
{
"$defs": {
"Constraint": {
"description": "Model representing a constraint in an OR-Tools problem.",
"properties": {
"description": {
"default": "",
"title": "Description",
"type": "string"
},
"expression": {
"title": "Expression",
"type": "string"
}
},
"required": [
"expression"
],
"title": "Constraint",
"type": "object"
},
"Objective": {
"description": "Model representing an optimization objective.",
"properties": {
"expression": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"default": null,
"title": "Expression"
},
"type": {
"$ref": "#/$defs/ObjectiveType",
"default": "feasibility"
}
},
"title": "Objective",
"type": "object"
},
"ObjectiveType": {
"description": "Enum for optimization objective types.",
"enum": [
"minimize",
"maximize",
"feasibility"
],
"title": "ObjectiveType",
"type": "string"
},
"Problem": {
"description": "Model representing a complete OR-Tools constraint programming problem.",
"properties": {
"constraints": {
"items": {
"$ref": "#/$defs/Constraint"
},
"title": "Constraints",
"type": "array"
},
"description": {
"default": "",
"title": "Description",
"type": "string"
},
"objective": {
"anyOf": [
{
"$ref": "#/$defs/Objective"
},
{
"type": "null"
}
],
"default": null
},
"parameters": {
"title": "Parameters",
"type": "object"
},
"variables": {
"items": {
"$ref": "#/$defs/Variable"
},
"title": "Variables",
"type": "array"
}
},
"required": [
"variables",
"constraints"
],
"title": "Problem",
"type": "object"
},
"Variable": {
"description": "Model representing a variable in an OR-Tools problem.",
"properties": {
"description": {
"default": "",
"title": "Description",
"type": "string"
},
"domain": {
"anyOf": [
{
"maxItems": 2,
"minItems": 2,
"prefixItems": [
{
"type": "integer"
},
{
"type": "integer"
}
],
"type": "array"
},
{
"type": "null"
}
],
"default": null,
"title": "Domain"
},
"name": {
"title": "Name",
"type": "string"
},
"shape": {
"anyOf": [
{
"items": {
"type": "integer"
},
"type": "array"
},
{
"type": "null"
}
],
"default": null,
"title": "Shape"
},
"type": {
"$ref": "#/$defs/VariableType"
}
},
"required": [
"name",
"type"
],
"title": "Variable",
"type": "object"
},
"VariableType": {
"description": "Enum for supported variable types in OR-Tools.",
"enum": [
"boolean",
"integer",
"interval"
],
"title": "VariableType",
"type": "string"
}
},
"properties": {
"problem": {
"$ref": "#/$defs/Problem"
}
},
"required": [
"problem"
],
"title": "solve_ortools_problem_toolArguments",
"type": "object"
}