当前位置: 首页>>代码示例>>Python>>正文


Python LpProblem.add方法代码示例

本文整理汇总了Python中pulp.LpProblem.add方法的典型用法代码示例。如果您正苦于以下问题:Python LpProblem.add方法的具体用法?Python LpProblem.add怎么用?Python LpProblem.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pulp.LpProblem的用法示例。


在下文中一共展示了LpProblem.add方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: find_optimal_strategy

# 需要导入模块: from pulp import LpProblem [as 别名]
# 或者: from pulp.LpProblem import add [as 别名]
def find_optimal_strategy(states, controls, costs, kernels, solver=None):
    """
    :param states: Number of system states (X)
    :param controls: Number of system controls (U)
    :param сosts: Cost matrix |X| x |U|
    :param kernels: Transition kernels. Dimensionality |X| x |X| x |U|
    """
    tolerance = 10e-15

    X = range(states)
    U = range(controls)
    R = costs
    Q = kernels

    # Check costs
    # Check num of rows
    assert(len(R) == states)
    for row in R:
        # Check num of cols
        assert(len(row) == controls)

    # Check kernels
    # Check num of rows
    assert(len(Q) == states)
    for row in Q:
        # Check num of cols
        assert(len(row) == states)
        for items in row:
            # Check num of items
            assert(len(items) == controls)
        # Check if distribution is normed
        for dist in zip(*row):
            assert(sum(dist)-1 < tolerance)

    # LP object
    optm = LpProblem("Optimal strategy", sense=LpMinimize)

    # Variables (continuous in range [0, 1])
    Z = [[LpVariable("z({},{})".format(x, u), 0, 1) \
                for u in U] for x in X]

    # Objective
    optm.objective = sum(np.dot(Z[x], R[x]) for x in X)

    # Constraints
    for x in X:
        cn = (sum(Z[x]) == sum(Q[y][x][u]*Z[y][u] for u in U for y in X))
        optm.add(cn)
    cn = sum(Z[x][u] for u in U for x in X) == 1
    optm.add(cn)

    optm.solve(solver)

    return [(x, u) for u in U for x in X if value(Z[x][u]) != 0]
开发者ID:bogdan-kulynych,项目名称:mrf-in-economics,代码行数:56,代码来源:strategy.py


注:本文中的pulp.LpProblem.add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。