首页 > 数据库 > 数据库开发技术 > 阿里达摩院MindOpt求解器使用介绍
MindOpt是阿里巴巴达摩院决策智能实验室研发的优化求解器,它主要用于解决各行各业的决策优化问题。

以下是从多个维度对MindOpt进行介绍。
MindOpt在技术上具有显著的优势。它能够实现单纯形法和内点法并发处理,这意味着在求解线性规划问题时,可以同时激活两种算法,从而将求解速度提升1.2到3.5倍。
自2020年8月起,MindOpt经历了多次重要的发布记录。最初发布了C语言版本API的单纯形线性规划(LP)方法,随后在2020年11月新增了内点法、并发法,并增加了C++、Python版本的API。到了2021年1月,MindOpt在阿里云天池平台发布,成为中国第一个免费开放的商用求解器。
MindOpt可应用于众多领域,如云计算领域的资源分配、资源调度问题;零售领域的仓库选址、补货优化、人员排班、收益管理问题;金融领域的资产配置、量化投资问题;制造领域的产销协同、厂房规划、排产排程、工艺优化问题;交通领域的路线规划、运力分配问题;能源领域的电网调度、发电规划问题等。
pip install mindoptpy
#%%
"""
/**
* Description
* -----------
*
* Linear optimization.
*
* Formulation
* -----------
*
* Minimize
* obj: 1 x0 + 2 x1 + 1 x2 + 1 x3
* Subject To
* c1 : 1 x0 + 1 x1 + 2 x2 + 3 x3 >= 1
* c2 : 1 x0 - 1 x2 + 6 x3 = 1
* Bounds
* 0 <= x0 <= 10
* 0 <= x1
* 0 <= x2
* 0 <= x3
* End
*/
"""
from mindoptpy import *
if __name__ == "__main__":
# Step 1. Create model.
model = Model("LP_01")
try:
# Step 2. Input model.
# Change to minimization problem.
model.ModelSense = MDO.MINIMIZE
# Add variables.
x = []
x.append(model.addVar(0.0, 10.0, 1.0, 'C', "x0"))
x.append(model.addVar(0.0, float('inf'), 2.0, 'C', "x1"))
x.append(model.addVar(0.0, float('inf'), 1.0, 'C', "x2"))
x.append(model.addVar(0.0, float('inf'), 1.0, 'C', "x3"))
# Add constraints.
model.addConstr(1.0 * x[0] + 1.0 * x[1] + 2.0 * x[2] + 3.0 * x[3] >= 1, "c0")
model.addConstr(1.0 * x[0] - 1.0 * x[2] + 6.0 * x[3] == 1, "c1")
# Step 3. Solve the problem and populate optimization result.
model.optimize()
if model.status == MDO.OPTIMAL:
print(f"Optimal objective value is: {model.objval}")
print("Decision variables: ")
for v in x:
print(f"x[{v.VarName}] = {v.X}")
else:
print("No feasible solution.")
except MindoptError as e:
print("Received Mindopt exception.")
print(" - Code : {}".format(e.code))
print(" - Reason : {}".format(e.message))
except Exception as e:
print("Received other exception.")
print(" - Reason : {}".format(e))
finally:
# Step 4. Free the model.
model.dispose()
结果输出:
"""
/**
* Description
* -----------
*
* Mixed Integer Linear optimization (row-wise input).
*
* Formulation
* -----------
*
* Minimize
* obj: 1 x0 + 2 x1 + 1 x2 + 1 x3
* Subject To
* c1 : 1 x0 + 1 x1 + 2 x2 + 3 x3 >= 1
* c2 : 1 x0 - 1 x2 + 6 x3 = 1
* Bounds
* 0 <= x0 <= 10
* 0 <= x1
* 0 <= x2
* 0 <= x3
* Integers
* x0 x1 x2
* End
*/
"""
from mindoptpy import *
if __name__ == "__main__":
# Step 1. Create a model and change the parameters.
model = Model("MILP_01")
try:
# Step 2. Input model.xs
# Change to minimization problem.
model.modelsense = MDO.MINIMIZE
# Add variables.
x = []
x.append(model.addVar(0.0, 10.0, 1.0, 'I', "x0"))
x.append(model.addVar(0.0, float('inf'), 2.0, 'I', "x1"))
x.append(model.addVar(0.0, float('inf'), 1.0, 'I', "x2"))
x.append(model.addVar(0.0, float('inf'), 1.0, 'C', "x3"))
# Add constraints.
model.addConstr(1.0 * x[0] + 1.0 * x[1] + 2.0 * x[2] + 3.0 * x[3] >= 1, "c0")
model.addConstr(1.0 * x[0] - 1.0 * x[2] + 6.0 * x[3] == 1, "c1")
# Step 3. Solve the problem and populate optimization result.
model.optimize()
if model.status == MDO.OPTIMAL:
print("Optimal objective value is: {model.objval}")
print("Decision variables: ")
for v in x:
print(f"x[{v.VarName}] = {v.X}")
else:
print("No feasible solution.")
except MindoptError as e:
print("Received Mindopt exception.")
print(" - Code : {}".format(e.code))
print(" - Reason : {}".format(e.message))
except Exception as e:
print("Received other exception.")
print(" - Reason : {}".format(e))
finally:
# Step 4. Free the model.
model.dispose()
输出结果:
"""
/**
* Description
* -----------
*
* Quadratuc optimization (row-wise input).
*
* Formulation
* -----------
*
* Minimize
* obj: 1 x0 + 1 x1 + 1 x2 + 1 x3
* + 1/2 [ x0^2 + x1^2 + x2^2 + x3^2 + x0 x1]
* Subject To
* c1 : 1 x0 + 1 x1 + 2 x2 + 3 x3 >= 1
* c2 : 1 x0 - 1 x2 + 6 x3 = 1
* Bounds
* 0 <= x0 <= 10
* 0 <= x1
* 0 <= x2
* 0 <= x3
* End
*/
"""
from mindoptpy import *
if __name__ == "__main__":
# Step 1. Create model.
model = Model("QP_01")
try:
# Step 2. Input model.
# Add variables.
x = []
x.append(model.addVar(0.0, 10.0, 0.0, 'C', "x0"))
x.append(model.addVar(0.0, float('inf'), 0.0, 'C', "x1"))
x.append(model.addVar(0.0, float('inf'), 0.0, 'C', "x2"))
x.append(model.addVar(0.0, float('inf'), 0.0, 'C', "x3"))
# Add constraints.
# Note that the nonzero elements are inputted in a row-wise order here.
model.addConstr(1.0 * x[0] + 1.0 * x[1] + 2.0 * x[2] + 3.0 * x[3] >= 1, "c0")
model.addConstr(1.0 * x[0] - 1.0 * x[2] + 6.0 * x[3] == 1, "c1")
# Add objective: 1 x0 + 1 x1 + 1 x2 + 1 x3 + 1/2 [ x0^2 + x1^2 + x2^2 + x3^2 + x0 x1]
obj = QuadExpr()
#option-I
obj.addTerms([1.0, 1.0, 1.0, 1.0], [x[0], x[1], x[2], x[3]])
obj.addTerms([0.5, 0.5, 0.5, 0.5, 0.5], [x[0], x[1], x[2], x[3], x[0]], [x[0], x[1], x[2], x[3], x[1]])
#option II
# obj = 1*x[0] + 1*x[1] + 1*x[2] + 1*x[3] + 0.5 * x[0]*x[0] + 0.5 * x[1]*x[1] + 0.5 * x[2]*x[2] + 0.5 * x[3]*x[3] + 0.5*x[0]*x[1]
# Set objective and change to minimization problem.
model.setObjective(obj, MDO.MINIMIZE)
# Step 3. Solve the problem and populate optimization result.
model.optimize()
if model.status == MDO.OPTIMAL:
print(f"Optimal objective value is: {model.objval}")
print("Decision variables:")
for v in x:
print(f"x[{v.VarName}] = {v.X}")
else:
print("No feasible solution.")
except MindoptError as e:
print("Received Mindopt exception.")
print(" - Code : {}".format(e.code))
print(" - Reason : {}".format(e.message))
except Exception as e:
print("Received other exception.")
print(" - Reason : {}".format(e))
finally:
# Step 4. Free the model.
model.dispose()
MindOpt作为我国自主研发的高性能数学规划求解器,不仅打破了国外技术的垄断,还在多个领域内展现出了广泛的应用潜力和卓越的性能表现。
想进一步研究的朋友,可查阅其官方文档链接。https://solver.damo.alibaba.com/doc/html/fast-intro.html
原文作者:Python学习杂记;如有侵权,请联系管理员删除。