4

私は Python 2.7 で作業しており、PuLP ライブラリを使用して問題をセットアップしています。変数、目的、および制約が定義されたら、LpProblem オブジェクトをピクルして、別の場所の Solver に送信します。問題を解凍すると、すべての変数が重複していることに気付きました。

import pulp
import pickle

prob = pulp.LpProblem('test problem', pulp.LpMaximize)
x = pulp.LpVariable('x', 0, 10)
y = pulp.LpVariable('y', 3, 6)
prob += x + y
prob += x <= 5

print prob
print pickle.loads(pickle.dumps(prob))

最初の print ステートメントの出力は次のとおりです。

>>> print prob
test problem:
MAXIMIZE
1*x + 1*y + 0
SUBJECT TO
_C1: x <= 5

VARIABLES
x <= 10 Continuous
3 <= y <= 6 Continuous

2番目の印刷中:

>>> print pickle.loads(pickle.dumps(prob))
test problem:
MAXIMIZE
1*x + 1*y + 0
SUBJECT TO
_C1: x <= 5

VARIABLES
x <= 10 Continuous
x <= 10 Continuous
3 <= y <= 6 Continuous
3 <= y <= 6 Continuous

ご覧のとおり、目的と制約は問題ありませんが、すべての変数が重複しています。この動作の原因は何ですか?また、これを防ぐにはどうすればよいですか?

4

1 に答える 1

2

したがって、なぜこれが起こるのか正確にはわかりませんが、同じ状況に陥った人のために回避策があります。

def UnpicklePulpProblem(pickled_problem):
    wrong_result = pickle.loads(pickled_problem)
    result = pulp.LpProblem(wrong_result.name, wrong_result.sense)
    result += wrong_result.objective
    for i in wrong_result.constraints: result += wrong_result.constraints[i], i
    return result

このように目的と制約を追加すると、基本的にこの方法で問題をゼロから再構築するため、変数が問題内で 1 回だけ定義されるようになります。

于 2014-04-03T22:06:58.290 に答える