线性问题示例¶
0/1 背包问题(MathOpt 后端)¶
使用 MathOpt 桥接后端求解经典的 0/1 背包问题。
from optagent import ExactBackendName, ModelBuilder, Orchestrator, OrchestratorConfig, OrchestratorSolver, PhaseConfig
builder = ModelBuilder(
metadata={"case": "knapsack_mathopt"},
solve_config={"preferred_backend": "mathopt_mp", "mathopt_solver_type": "GSCIP"},
)
weights = [2, 3, 4, 5, 9]
values = [3, 4, 8, 8, 10]
picks = [builder.int_var(default=0, lb=0, ub=1, name=f"pick_{idx}") for idx in range(len(weights))]
total_weight = builder.sum(*((pick * weight) for pick, weight in zip(picks, weights, strict=True)))
total_value = builder.sum(*((pick * value) for pick, value in zip(picks, values, strict=True)))
builder.constraint(total_weight <= 10, name="capacity")
builder.maximize(total_value, name="profit")
program = builder.freeze()
result = Orchestrator().run(
program,
config=OrchestratorConfig(
required_backend=ExactBackendName.MATHOPT_MP,
strict_backend=True,
phases=[PhaseConfig(name="mathopt_knapsack", solver=OrchestratorSolver.MILP, budget_iterations=20)],
),
)
运行方式:
指派问题(HiGHS 原生后端)¶
使用 HiGHS 原生后端求解工人-任务指派问题。
from optagent import ExactBackendName, ModelBuilder, Orchestrator, OrchestratorConfig, OrchestratorSolver, PhaseConfig
builder = ModelBuilder(
metadata={"case": "assignment_highs_native"},
solve_config={"preferred_backend": "highs_native"},
)
profit = {"a_x": 8, "a_y": 6, "b_x": 7, "b_y": 9}
a_x = builder.int_var(default=0, lb=0, ub=1, name="a_x")
a_y = builder.int_var(default=0, lb=0, ub=1, name="a_y")
b_x = builder.int_var(default=0, lb=0, ub=1, name="b_x")
b_y = builder.int_var(default=0, lb=0, ub=1, name="b_y")
builder.constraint(a_x + a_y == 1, name="assign_worker_a")
builder.constraint(b_x + b_y == 1, name="assign_worker_b")
builder.constraint(a_x + b_x == 1, name="fill_job_x")
builder.constraint(a_y + b_y == 1, name="fill_job_y")
builder.maximize(
(a_x * profit["a_x"]) + (a_y * profit["a_y"]) + (b_x * profit["b_x"]) + (b_y * profit["b_y"]),
name="profit",
)
program = builder.freeze()
result = Orchestrator().run(
program,
config=OrchestratorConfig(
required_backend=ExactBackendName.HIGHS_NATIVE,
strict_backend=True,
phases=[PhaseConfig(name="highs_assignment", solver=OrchestratorSolver.MILP, budget_iterations=20)],
),
)
运行方式:
选址问题(默认 MILP 后端)¶
不强制指定后端,由系统自动选择 MILP 求解器。
from optagent import ModelBuilder, Orchestrator, OrchestratorConfig, OrchestratorSolver, PhaseConfig
builder = ModelBuilder(metadata={"case": "facility_location_small"})
open_cost = {"north": 6, "south": 5}
service_cost = {
("north", "alpha"): 2, ("north", "beta"): 4, ("north", "gamma"): 5,
("south", "alpha"): 3, ("south", "beta"): 2, ("south", "gamma"): 3,
}
open_north = builder.int_var(default=0, lb=0, ub=1, name="open_north")
open_south = builder.int_var(default=0, lb=0, ub=1, name="open_south")
# ... 分配变量 ...
builder.constraint(assign_north_alpha + assign_south_alpha == 1, name="serve_alpha")
# ... 其他约束 ...
builder.minimize(
(open_north * open_cost["north"]) + (open_south * open_cost["south"])
+ (assign_north_alpha * service_cost[("north", "alpha")])
# + ...
, name="total_cost",
)
program = builder.freeze()
result = Orchestrator().run(
program,
config=OrchestratorConfig(
phases=[PhaseConfig(name="default_facility_location", solver=OrchestratorSolver.MILP, budget_iterations=20)],
),
)
运行方式:
后端选择
MathOpt:通过 Google MathOpt 桥接,支持 GSCIP、CP-SAT 等多种求解器HiGHS Native:直接调用 HiGHS 求解器,无需额外依赖- 默认 MILP:系统自动选择可用的 MILP 后端