这是一个通用的模型上下文协议(MCP)服务器,用于解决带有逻辑和数值约束的组合优化问题。该服务器为多个优化求解器提供了统一接口,使AI助手能够解决跨多个领域的复杂优化问题。
# 运行单个示例
python examples/nqueens.py
python examples/knapsack.py
python examples/portfolio_optimization.py
python examples/job_shop_scheduling.py
python examples/nurse_scheduling.py
python examples/economic_production_planning.py
# 运行交互式笔记本
jupyter notebook examples/constrained_optimization_demo.ipynb
constrained-opt-mcp
将服务器添加到您的MCP配置中:
{
"mcpServers": {
"constrained-opt-mcp": {
"command": "constrained-opt-mcp",
"args": []
}
}
}
服务器提供以下工具:
solve_constraint_satisfaction - 解决逻辑约束问题solve_convex_optimization - 解决凸优化问题solve_linear_programming - 解决线性规划问题solve_constraint_programming - 解决约束规划问题solve_portfolio_optimization - 解决投资组合优化问题# 安装软件包
pip install constrained-opt-mcp
# 或者从源代码安装
git clone https://github.com/your-org/constrained-opt-mcp
cd constrained-opt-mcp
pip install -e .
# Solve a simple arithmetic constraint problem
variables = [
{"name": "x", "type": "integer"},
{"name": "y", "type": "integer"},
]
constraints = [
"x + y == 10",
"x - y == 2",
]
# Result: x=6, y=4
# Optimize portfolio allocation
assets = ["Stocks", "Bonds", "Real Estate", "Commodities"]
expected_returns = [0.10, 0.03, 0.07, 0.06]
risk_factors = [0.15, 0.03, 0.12, 0.20]
correlation_matrix = [
[1.0, 0.2, 0.6, 0.3],
[0.2, 1.0, 0.1, 0.05],
[0.6, 0.1, 1.0, 0.25],
[0.3, 0.05, 0.25, 1.0],
]
# Result: Optimal portfolio weights and performance metrics
# Production planning problem
sense = "maximize"
objective_coeffs = [3.0, 2.0] # Profit per unit
variables = [
{"name": "product_a", "lb": 0, "ub": None, "type": "cont"},
{"name": "product_b", "lb": 0, "ub": None, "type": "cont"},
]
constraint_matrix = [
[2, 1], # Labor: 2*A + 1*B <= 100
[1, 2], # Material: 1*A + 2*B <= 80
]
constraint_senses = ["<=", "<="]
rhs_values = [100.0, 80.0]
# Result: Optimal production quantities
股票投资组合优化:
多资产投资组合优化:
高级风险指标:
约束优化MCP服务器实现了各种优化问题的解决方案:
$$\min_{x} c^T x \quad \text{subject to} \quad Ax \leq b, \quad x \geq 0$$
$$\min_{x} \frac{1}{2}x^T Q x + c^T x \quad \text{subject to} \quad Ax \leq b, \quad x \geq 0$$
$$\min_{x} f(x) \quad \text{subject to} \quad g_i(x) \leq 0, \quad h_j(x) = 0$$
其中 $f$ 和 $g_i$ 是凸函数。
找到 $x \in \mathcal{D}$ 使得 $C_1(x) \land C_2(x) \land \ldots \land C_k(x)$
$$\max_{w} \mu^T w - \frac{\lambda}{2} w^T \Sigma w \quad \text{subject to} \quad \sum_{i=1}^{n} w_i = 1, \quad w_i \geq 0$$
其中:
| 问题类型 | 求解器 | 复杂度 | 数学形式 |
|---|---|---|---|
| 约束满足 | Z3 | NP完全 | 逻辑约束 |
| 凸优化 | CVXPY | 多项式 | 凸函数 |
| 线性规划 | HiGHS | 多项式 | 线性约束 |
| 约束规划 | OR-Tools | NP完全 | 离散域 |
constrained_opt_mcp/core/) - 基类和问题类型constrained_opt_mcp/models/) - 特定问题的模型定义constrained_opt_mcp/solvers/) - 求解器实现constrained_opt_mcp/server/) - MCP服务器实现constrained_opt_mcp/examples/) - 使用示例和演示| 问题类型 | 求解器 | 使用场景 |
|---|---|---|
| 约束满足 | Z3 | 逻辑谜题、验证、规划 |
| 凸优化 | CVXPY | 投资组合优化、机器学习 |
| 线性规划 | HiGHS | 生产计划、资源分配 |
| 约束规划 | OR-Tools | 调度、分配、路由 |
| 投资组合优化 | 多个 | 风险管理、投资组合构建 |
运行全面的测试套件:
# 运行所有测试
pytest
# 运行特定测试类别
pytest tests/test_z3_solver.py
pytest tests/test_cvxpy_solver.py
pytest tests/test_highs_solver.py
pytest tests/test_ortools_solver.py
pytest tests/test_mcp_server.py
# 运行测试并统计覆盖率
pytest --cov=constrained_opt_mcp
本项目采用Apache License 2.0许可协议。有关详细信息,请参阅 LICENSE 文件。
如果您有问题、发现问题或想要贡献代码,请: