使用symphy表达式制定优化问题,并使用与第三方优化软件(如glpk)的接口解决它们。
optlang的Python项目详细描述
sympy数学编程语言
optlang是一个用于求解数学优化的python包 问题,即在一个集合上最大化或最小化目标函数 受许多约束的变量。Optlang提供 一系列优化工具的公共接口,因此不同的求解器 后端可以以透明的方式更改。 optlang的面向对象api利用了符号数学库 sympy允许目标函数 以及易于从 变量(见示例)。
如果你觉得Optlang有用的话,就盯着这个报告给我们看看吧!
另外,请使用github issue tracker 让我们知道错误或功能请求,或者如果您有关于optlang的问题或疑问,我们的gitter channel。
依赖关系
需要以下依赖项。
以下是允许使用其他解算器的可选依赖项。
示例
制定和解决问题很简单(举个例子 来自GLPK documentation):
from__future__importprint_functionfromoptlangimportModel,Variable,Constraint,Objective# All the (symbolic) variables are declared, with a name and optionally a lower and/or upper bound.x1=Variable('x1',lb=0)x2=Variable('x2',lb=0)x3=Variable('x3',lb=0)# A constraint is constructed from an expression of variables and a lower and/or upper bound (lb and ub).c1=Constraint(x1+x2+x3,ub=100)c2=Constraint(10*x1+4*x2+5*x3,ub=600)c3=Constraint(2*x1+2*x2+6*x3,ub=300)# An objective can be formulatedobj=Objective(10*x1+6*x2+4*x3,direction='max')# Variables, constraints and objective are combined in a Model object, which can subsequently be optimized.model=Model(name='Simple model')model.objective=objmodel.add([c1,c2,c3])status=model.optimize()print("status:",model.status)print("objective value:",model.objective.value)print("----------")forvar_name,varinmodel.variables.iteritems():print(var_name,"=",var.primal)
示例将生成以下输出:
status: optimal objective value: 733.333333333 ---------- x2 = 66.6666666667 x3 = 0.0 x1 = 33.3333333333
使用特定解算器
如果安装了多个解算器,也可以通过直接从
各自的解算器接口,例如from optlang.glpk_interface import Model, Variable, Constraint, Objective
文档
Optlang的文档位于 readthedocs.org。
贡献
请阅读CONTRIBUTING.md。