FelooPy,一个Python的算法建模集成库介绍

科技   2024-10-14 20:02   德国  

今天要给大家介绍一个解决运筹优化问题的开源库FelooPy 。这个开源库封装了精确算法、启发式算法、多目标优化、不确定优化等相关算法,可以很方便的建模并解决优化问题。

FelooPy库基础介绍

FelooPy是一个集成优化环境 (IOE),旨在作为决策优化中心。它涉及使用自动化运筹学 (AutoOR) 方法和技术来确定可行的解决方案,从而根据给定或可学习的政策做出具有最佳(最佳)结果的逻辑决策。

FelooPy 是feasible、logicaldecisions、optimal、Python取一到两个字母的合成词。

安装

FelooPy要求python版本在3.10及以上。与其他库类似,可通过pip安装。但是每个版本有对应的安装方式。我这里用full这个功能比较齐全的版本。

pip install -U feloopy[full]

试用案例

精确算法

from feloopy import *

# Define a model
m = model('exact''target_model_name''pymprog')

# Define variables
x = m.bvar(name='x', dim=0)
y = m.pvar(name='y', dim=0, bound = [0, 10])

# Define constraints
m.con(x + y <= 1, label='c1')
m.con(x - y >= 1, label='c2')

# Define an objective
m.obj(x + y)

# Solve the model
m.sol(['max'], 'glpk')

# Report the results
m.report()
╭─ FelooPy v0.2.8 ───────────────────────────────────────────────────────────────╮
│                                                                                │
│ Date: 2024-02-13                                                Time: 20:57:48 │
│ Interface: pymprog                                                Solver: glpk │
│                                                                                │
╰────────────────────────────────────────────────────────────────────────────────╯
╭─ Model ────────────────────────────────────────────────────────────────────────╮
│                                                                                │
│ Name: target_model_name                                                        │
│ Feature:                                Class:                        Total:   │
│ Positive variable                       1                             1        │
│ Binary variable                         1                             1        │
│ Total variables                         2                             2        │
│ Objective                               -                             1        │
│ Constraint                              2                             2        │
│                                                                                │
╰────────────────────────────────────────────────────────────────────────────────╯
╭─ Solve ────────────────────────────────────────────────────────────────────────╮
│                                                                                │
│ Method: exact                                                  Objective value │
│ Status:                                                                    max │
│ optimal                                                                   1.00 │
│                                                                                │
╰────────────────────────────────────────────────────────────────────────────────╯
╭─ Metric ───────────────────────────────────────────────────────────────────────╮
│                                                                                │
│ CPT (microseconds)                                                     1183.10 │
│ CPT (hour:min:sec)                                                    00:00:00 │
│                                                                                │
╰────────────────────────────────────────────────────────────────────────────────╯
╭─ Decision ─────────────────────────────────────────────────────────────────────╮
│                                                                                │
│ x = 1.0                                                                        │
│                                                                                │
╰──────────────────────────────────────────────────────────────────
──────────────╯

启发式算法

from feloopy import *

def instance(X):

   # Define model instance
   m = model('heuristic''representor_model_name''feloopy', X)

   # Define variables for the model instance
   x = m.bvar(name='x', dim=0)
   y = m.pvar(name='y', dim=0, bound = [0, 10])

   # Define constraints for the model instance
   m.con(x + y |l| 1, label='c1')
   m.con(x - y |g| 1, label='c2')

   # Define an objective for the model instance
   m.obj((x-1)**2+(y-1)**2)

   # Solve the model instance
   m.sol(['max'], 'ga', {'epoch': 1000, 'pop_size': 100})

   return m[X]

# Make the main model
m = make_model(instance)

# Solve the model
m.sol(penalty_coefficient=10)

# Report the results
m.report()
╭─ FelooPy v0.2.8 ───────────────────────────────────────────────────────────────╮
│                                                                                │
│ Date: 2024-02-13                                                Time: 21:01:52 │
│ Interface: feloopy                                                  Solver: ga │
│                                                                                │
╰────────────────────────────────────────────────────────────────────────────────╯
╭─ Model ────────────────────────────────────────────────────────────────────────╮
│                                                                                │
│ Name: representor_model_name                                                   │
│ Feature:                                Class:                        Total:   │
│ Positive variable                       1                             1        │
│ Binary variable                         1                             1        │
│ Total variables                         2                             2        │
│ Objective                               -                             1        │
│ Constraint                              2                             2        │
│                                                                                │
╰────────────────────────────────────────────────────────────────────────────────╯
╭─ Solve ────────────────────────────────────────────────────────────────────────╮
│                                                                                │
│ Method: heuristic                                              Objective value │
│ Status:                                                                    max │
│ feasible (constrained)                                                    1.00 │
│                                                                                │
╰────────────────────────────────────────────────────────────────────────────────╯
╭─ Metric ───────────────────────────────────────────────────────────────────────╮
│                                                                                │
│ CPT (microseconds)                                                    2.05e+06 │
│ CPT (hour:min:sec)                                                    00:00:02 │
│                                                                                │
╰────────────────────────────────────────────────────────────────────────────────╯
╭─ Decision ─────────────────────────────────────────────────────────────────────╮
│                                                                                │
│ x = [1.]                                                                       │
│                                                                                │
╰───────────────────────────────────────────────────────────────────
─────────────╯

tsp问题

这里其实是通过feloopy使用了ortools。

from feloopy import *

#Environment
m = target_model('exact''traveling salesperson problem''ortools')

dt = data_toolkit(key=0)

#Sets
N = range(10)
U = range(9)

#Dataset
c = dt.uniformint('c', dim= [N,N], bound=[1, 11])
for i, j in sets(N, N):
    c[i][i] = 0
    c[i][j] = c[j][i]

#Variables
x = m.bvar('x', [N, N])
u = m.ivar('u', [N])

#Objective
m.obj(sum(c[i, j]*x[i, j] for i, j in sets(N, N)))

#Constraints
for j in N:
    m.con(sum(x[i, j] for i in N if i != j) == 1)

for i in N:
    m.con(sum(x[i, j] for j in N if j != i) == 1)

for i, j in sets(U, N):
    if i != j:
        m.con(u[i] - u[j] + x[i, j] * len(N) <= len(N)-1)

#Solve
m.sol(['min'], 'glop', show_log=False)

m.report(show_tensors=True)
╭─ FelooPy v0.2.8 ───────────────────────────────────────────────────────────────╮
│                                                                                │
│ Date: 2024-02-13                                                Time: 21:19:23 │
│ Interface: ortools                                                Solver: glop │
│                                                                                │
╰────────────────────────────────────────────────────────────────────────────────╯
╭─ Model ────────────────────────────────────────────────────────────────────────╮
│                                                                                │
│ Name: traveling salesperson problem                                            │
│ Feature:                                Class:                        Total:   │
│ Binary variable                         1                             100      │
│ Integer variable                        1                             10       │
│ Total variables                         2                             110      │
│ Objective                               -                             1        │
│ Constraint                              1                             101      │
│                                                                                │
╰────────────────────────────────────────────────────────────────────────────────╯
╭─ Solve ────────────────────────────────────────────────────────────────────────╮
│                                                                                │
│ Method: exact                                                  Objective value │
│ Status:                                                                    min │
│ optimal                                                                  27.00 │
│                                                                                │
╰────────────────────────────────────────────────────────────────────────────────╯
╭─ Metric ───────────────────────────────────────────────────────────────────────╮
│                                                                                │
│ CPT (microseconds)                                                    4.71e+04 │
│ CPT (hour:min:sec)                                                    00:00:00 │
│                                                                                │
╰────────────────────────────────────────────────────────────────────────────────╯
╭─ Decision ─────────────────────────────────────────────────────────────────────╮
│                                                                                │
│ x = [[0., 0., 0., 0., 0., 0., 0., 0., 0., 1.],                                 │
│      [0., 0., 0., 0., 1., 0., 0., 0., 0., 0.],                                 │
│      [1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],                                 │
│      [0., 0., 0., 0., 0., 0., 0., 0., 1., 0.],                                 │
│      [0., 0., 0., 1., 0., 0., 0., 0., 0., 0.],                                 │
│      [0., 1., 0., 0., 0., 0., 0., 0., 0., 0.],                                 │
│      [0., 0., 0., 0., 0., 1., 0., 0., 0., 0.],                                 │
│      [0., 0., 0., 0., 0., 0., 1., 0., 0., 0.],                                 │
│      [0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],                                 │
│      [0., 0., 0., 0., 0., 0., 0., 1., 0., 0.]]                                 │
│ u = [8.e+00, 3.e+00, 7.e+00, 5.e+00, 4.e+00, 2.e+00, 1.e+00, 0.e+00, 6.e+00,   │
│      1.e+20]                                                                   │
│                                                                                │
╰─────────────────────────────────────────────────────────────────
───────────────╯

封装算法介绍

feloopy除了依赖于一些常用的求解器,其启发式算法模块,封装了许多算法,这些算法都有相应的文档介绍。

看看2023年这个算法。BBOA算法是从mealpy包里直接调用的。

该算法来源文献及相应代码也均有提供。

总结

FelooPy是一个融合多种运筹优化算法包的建模库,可以通过它很方便的进行运筹优化建模。

但是,由于FelooPy比较新,官网的文档维护和制作比较粗糙,有些功能介绍还不够详细,所以该优化包现在知名度不太高,使用的人比较少。




微信公众号后台回复

加群:加入全球华人OR|AI|DS社区硕博微信学术群

资料:免费获得大量运筹学相关学习资料

人才库:加入运筹精英人才库,获得独家职位推荐

电子书:免费获取平台小编独家创作的优化理论、运筹实践和数据科学电子书,持续更新中ing...

加入我们:加入「运筹OR帷幄」,参与内容创作平台运营

知识星球:加入「运筹OR帷幄」数据算法社区,免费参与每周「领读计划」、「行业inTalk」、「OR会客厅」等直播活动,与数百位签约大V进行在线交流



                    


        




文章须知

微信编辑:疑疑





关注我们 

       FOLLOW US


































运筹OR帷幄
致力于成为全球最大的运筹学中文线上社区
 最新文章