优化问题(数学)

时间:2018-08-04 18:00:57

标签: python optimization pulp

我有一个优化问题,需要将已知数量的屏幕投影(缩放比例保持不变)装配到屏幕上,并占用尽可能大的表面积。

为此,我们需要找到最佳的网格(例如以下示例中的2x2和1x2)以及投影宽度,以产生具有最大可能表面积的投影。基本上,我们想消耗尽可能多的房地产。

考虑这两个示例-第一个有4个,另一个有2个投影:

+----------------------------------+   +----------------------------------+
|                                  |   |                                  |
|  +----------+     +----------+   |   |                                  |
|  |    1     |     |     2    |   |   |  +-----------+   +-----------+   |
|  |          |     |          |   |   |  |           |   |           |   |
|  +----------+     +----------+   |   |  |     1     |   |     2     |   |
|  +----------+     +----------+   |   |  |           |   |           |   |
|  |    3     |     |     4    |   |   |  |           |   |           |   |
|  |          |     |          |   |   |  +-----------+   +-----------+   |
|  +----------+     +----------+   |   |                                  |
|                                  |   |                                  |
+----------------------------------+   +----------------------------------+

(让我们假装投影是按比例绘制的)

如何以编程方式找出最佳的网格布局和投影宽度? 请注意,投影的数量也可能是奇数,其中网格可能是3x4,但其中的一行仅包含2至3个元素,而不是全部4个

如果我们已经知道网格并且只想查找宽度,则可以手动计算它或使用pulp

import pulp

def get_projection_dimensions():
    screen_w = 1600
    screen_h = 900
    grid = [4, 3]  # 2x4 grid: 2 rows, first contains 4, other 3 projections
    max_row_len = max(grid)  # == 4
    ratio = screen_h / screen_w

    # find projection width:
    problem = pulp.LpProblem('maxProjectionWidth', pulp.LpMaximize)
    proj_w = pulp.LpVariable('proj_w', lowBound = 0)
    problem += proj_w
    problem += ratio * len(grid) * proj_w <= screen_h
    problem += max_row_len * proj_w <= screen_w

    result = problem.solve()
    assert result == pulp.LpStatusOptimal
    proj_w = proj_w.value()
    proj_h = proj_w * ratio

    return proj_w, proj_h

但是当引入其他变量以查找网格布局(grid_y x grid_x)时,我遇到了纸浆限制:

import pulp

def get_projection_dimensions():
    screen_w = 1600
    screen_h = 900
    projections = 10
    ratio = screen_h / screen_w

    # find tile width:
    problem = pulp.LpProblem('maxProjectionWidth', pulp.LpMaximize)
    proj_w = pulp.LpVariable('proj_w', lowBound = 0)
    grid_x = pulp.LpVariable('grid_x', lowBound = 1)  # max number of projections in a row
    grid_y = pulp.LpVariable('grid_y', lowBound = 1)  # number of rows
    problem += proj_w  # note only 'proj_w' should be maximized, we don't care for other vars
    problem += grid_x
    problem += grid_y

    problem += grid_y * proj_w * ratio <= screen_h
    problem += grid_x * proj_w <= screen_w
    problem += grid_x * grid_y >= projections
    problem += grid_y == math.ceil(projections / grid_x)

    result = problem.solve()
    assert result == pulp.LpStatusOptimal
    proj_w = proj_w.value()
    proj_h = proj_w * ratio

    return proj_w, proj_h

引发错误是

File "/usr/local/lib/python3.6/dist-packages/pulp/pulp.py", line 775, in __mul__
  raise TypeError("Non-constant expressions cannot be multiplied")

其他库,例如scipy是否适合于此?

0 个答案:

没有答案