本文整理汇总了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]