本文整理匯總了Python中pulp.LpProblem.objective方法的典型用法代碼示例。如果您正苦於以下問題:Python LpProblem.objective方法的具體用法?Python LpProblem.objective怎麽用?Python LpProblem.objective使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pulp.LpProblem
的用法示例。
在下文中一共展示了LpProblem.objective方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: find_optimal_strategy
# 需要導入模塊: from pulp import LpProblem [as 別名]
# 或者: from pulp.LpProblem import objective [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]