一种基于启发式方法的python非线性编程API
flopt的Python项目详细描述
扑通
一个采用启发式方法的python非线性编程API。在
安装
git clone https://github.com/flab-coder/flopt.git
flopt中的公式化问题
- 在
非线性问题
^{pr2}$ 在 - 在
黑箱问题
在minimize simulator(a, b, c) s.t 0 <= a <= 1 1 <= b <= 2 1 <= c <= 3
- 在
寻找最佳排列问题(包括TSP)
在 - 在
可满足性问题(包括MAX-SAT)
在
启发式算法
- 随机搜索
- 2-选项
- 群体智能搜索
- 其他应用
简单的例子
您可以编写类似于纸浆应用程序的代码。在
fromfloptimportVariable,Problem,Solver# Variablesa=Variable('a',lowBound=0,upBound=1,cat='Integer')b=Variable('b',lowBound=1,upBound=2,cat='Continuous')c=Variable('c',upBound=3,cat='Continuous')# Problemprob=Problem()prob+=2*(3*a+b)*c**2+3# set the objective functionprob+=a+b*c<=3# set the constraint# Solversolver=Solver(algo='ScipySearch')# select the heuristic algorithmsolver.setParams(n_trial=1000)# setting of the hyper parametersprob.solve(solver,msg=True)# run solver to solve the problem# display the result, incumbent solutionprint('obj value',prob.getObjectiveValue())print('a',a.value())print('b',b.value())print('c',c.value())
此外,您可以通过CustomExpression
fromfloptimportCustomExpressionfrommathimportsin,cosdefuser_func(a,b,c):return(0.7*a+0.3*cos(b)**2+0.1*sin(c))*abs(c)custom_obj=CustomExpression(func=user_func,variables=[a,b,c])prob=Problem(name='CustomExpression')prob+=custom_obj
在求解TSP的情况下,Permutation变量很有用。在
# Variablesperm=Variable('perm',lowBound=0,upBound=N-1,cat='Permutation')# Objectdeftsp_dist(perm):distance=0forhead,tailinzip(perm,perm[1:]+[perm[0]]):distance+=D[head][tail]# D is the distance matrixreturndistancetsp_obj=CustomExpression(func=tsp_dist,variables=[perm])# Problemprob=Problem(name='TSP')prob+=tsp_obj
- 项目
标签: