赞
踩
本篇文章是系列文章的第三篇,MindOpt对于python的支持还是挺不错的,我已经编写了建模优化线性规划和混合整数线性规划问题的例子,下文我会对Python调用MindOpt建模优化二次规划问题进行总结。
求解器安装包的发布渠道。请大家:
在前文线性规划问题示例中,讲述到线性规划在我个人认为是在线性的目标和约束中,找出一个最优解。而本文的二次规划,是非线性规划中的一类。具体地说,是一个线性约束的、二次规划问题,就是优化(最小化或最大化)二次函数目标的问题。
关于优化的类别,有很多,比如MindOpt的案例广场的标签里面提到的问题标签,就列出了常见的数学规划的类型。其中关于变量、约束、目标这建模三要素,进行罗列:
本文主要讲 凸二次规划,Convex Quadratic Programming。
""" /** * Description * ----------- * * Linear 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__": MDO_INFINITY = MdoModel.get_infinity() # Step 1. Create a model and change the parameters. model = MdoModel() try: # Step 2. Input model. # Change to minimization problem. model.set_int_attr(MDO_INT_ATTR.MIN_SENSE, 1) # Add variables. x = [] x.append(model.add_var(0.0, 10.0, 1.0, None, "x0", False)) x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x1", False)) x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x2", False)) x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x3", False)) # Add constraints. # Note that the nonzero elements are inputted in a row-wise order here. model.add_cons(1.0, MDO_INFINITY, 1.0 * x[0] + 1.0 * x[1] + 2.0 * x[2] + 3.0 * x[3], "c0") model.add_cons(1.0, 1.0, 1.0 * x[0] - 1.0 * x[2] + 6.0 * x[3], "c1") # Add quadratic objective matrix Q. # # Note. # 1. The objective function is defined as c^Tx + 1/2 x^TQx, where Q is stored with coordinate format. # 2. Q will be scaled by 1/2 internally. # 3. To ensure the symmetricity of Q, user needs to input only the lower triangular part. # # Q = [ 1.0 0.5 0 0 ] # [ 0.5 1.0 0 0 ] # [ 0.0 0.0 1.0 0 ] # [ 0 0 0 1.0 ] model.set_quadratic_elements([ x[0], x[1], x[1], x[2], x[3] ], [ x[0], x[0], x[1], x[2], x[3] ], [ 1.0, 0.5, 1.0, 1.0, 1.0 ]) # Step 3. Solve the problem and populate the result. model.solve_prob() model.display_results() except MdoError as e: print("Received Mindopt exception.") print(" - Code : {}".format(e.code)) print(" - Reason : {}".format(e.message)) except Exception as e: print("Received exception.") print(" - Reason : {}".format(e)) finally: # Step 4. Free the model. model.free_mdl()
model = MdoModel()
model.set_int_attr(MDO_INT_ATTR.MIN_SENSE, 1)
x = []
x.append(model.add_var(0.0, 10.0, 1.0, None, "x0", False))
x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x1", False))
x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x2", False))
x.append(model.add_var(0.0, MDO_INFINITY, 1.0, None, "x3", False))
model.add_cons(1.0, MDO_INFINITY, 1.0 * x[0] + 1.0 * x[1] + 2.0 * x[2] + 3.0 * x[3], "c0")
model.add_cons(1.0, 1.0, 1.0 * x[0] - 1.0 * x[2] + 6.0 * x[3], "c1")
# Add quadratic objective matrix Q.
#
# Note.
# 1. The objective function is defined as c^Tx + 1/2 x^TQx, where Q is stored with coordinate format.
# 2. Q will be scaled by 1/2 internally.
# 3. To ensure the symmetricity of Q, user needs to input only the lower triangular part.
#
# Q = [ 1.0 0.5 0 0 ]
# [ 0.5 1.0 0 0 ]
# [ 0.0 0.0 1.0 0 ]
# [ 0 0 0 1.0 ]
model.set_quadratic_elements([ x[0], x[1], x[1], x[2], x[3] ], [ x[0], x[0], x[1], x[2], x[3] ], [ 1.0, 0.5, 1.0, 1.0, 1.0 ])
可以通过mindooptpy.MdoModel.set_real_param()设置,例如设置求解时间上限等:
model.set_real_param("MaxTime", 7200.0)
# MindOpt 当前使用墙上时间作为默认计时。
# 如果将该值设置为0,则不会施加求解时间限制。
设置求解方法:
model.set_int_param("Method", 2) # 内点法
model.set_int_param("Method", 1) # 对偶单纯形法
model.set_int_param("Method", 0) # 原始单纯形法
model.set_int_param("Method", -1) # 让求解器决定(并发优化)
model.set_int_param("Method", -2) # 使用多线程单纯形法
model.solve_prob()
model.display_results()
更多Python 接口函数使用方式
MindOpt最新版本1.1.1支持更多API、语法更简洁
下载地址、API查询地址
钉钉群号:32451444
邮箱地址:solver.damo@list.alibaba-inc.com
更多更新通知:https://solver.damo.alibaba.com
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。