2017-05-05 16 views
0

私はPyomo 5.1.1で抽象モデルを作成し、Python内で値を入力しようとしています(つまり、AMPLファイルを使用していません)。私は基本的にPyomo documentation exampleに従っていますが、 "Constant objective detected"を取得しています。抽象的なpyomoモデルから具体的なモデルを具体化する

import pyomo.environ as oe 
model = oe.AbstractModel() 
model.I = oe.Set() 
model.J = oe.Set() 
model.a = oe.Param(model.I,model.J) 
model.b = oe.Param(model.I) 
model.c = oe.Param(model.J) 
model.x = oe.Var(model.J,domain=oe.NonNegativeReals) 
def obj_expression(model): 
    return oe.summation(model.c,model.x) 

model.OBJ = oe.Objective(rule=obj_expression) 
def ax_constraint_rule(model,i): 
    return sum(model.a[i,j]*model.x[j] for j in model.J) >= model.b[i] 

model.AxbConstraint = oe.Constraint(model.I,rule=ax_constraint_rule) 

そして、私は次のエラーを取得する実際の値

aa = np.array([[1,2,1,4],[5,2,2,4]]) 
bb = np.array([2,4]) 
cc = np.array([1,2,4,2]) 

cmodel = model.create_instance() 
cmodel.a.values = aa 
cmodel.b.values = bb 
cmodel.c.values = cc 

opt = oe.SolverFactory("glpk") 
results = opt.solve(cmodel) 

でこのモデルを初期化しよう:

WARNING:pyomo.core:Constant objective detected, replacing with a placeholder to prevent solver failure. WARNING:pyomo.core:Empty constraint block written in LP format - solver may error WARNING: Constant objective detected, replacing with a placeholder to prevent solver failure. WARNING: Empty constraint block written in LP format - solver may error

は明らかに間違って何かが私は道でありますcmodelを初期化していますが、私はPython内で初期化を記述する文書を見つけることができません。

答えて

1

AMPLの.datファイルからデータをロードする必要がない場合は、ConcreteModelから開始することをお勧めします。その場合、それらを変更可能にする必要がない限り、Paramオブジェクトにデータを格納する必要はありません。コンポーネントを索引付けするためのSetオブジェクトの作成は依然として推奨されています。それ以外の場合は、モデルに追加するコンポーネントと衝突する可能性のある名前でSetオブジェクトが暗黙的に作成されます。

ConcreteModelの定義を、データを入力とする関数の中に置くことによって、AbstractModelとそのcreate_instanceメソッドによって提供される機能を本質的に複製しています。例えば、

import pyomo.environ as oe 

def build_model(a, b, c): 
    m = len(b) 
    n = len(c) 
    model = oe.ConcreteModel() 
    model.I = oe.Set(initialize=range(m)) 
    model.J = oe.Set(initialize=range(n)) 
    model.x = oe.Var(model.J,domain=oe.NonNegativeReals) 

    model.OBJ = oe.Objective(expr= oe.summation(c,model.x)) 
    def ax_constraint_rule(model,i): 
     arow = a[i] 
     return sum(arow[j]*model.x[j] for j in model.J) >= b[i] 
    model.AxbConstraint = oe.Constraint(model.I,rule=ax_constraint_rule) 
    return model 

# Note that there is no need to call create_instance on a ConcreteModel 
m = build_model(...) 
opt = oe.SolverFactory("glpk") 
results = opt.solve(m) 

また、最初Pyomo式を構築するためにそれらを使用する前にarray.tolist()方法を使用してPythonのリストにすべてのnumpyのアレイに変換することをお勧めします。 Pyomoにはまだ配列演算のコンセプトが表現システムに組み込まれておらず、Numpy配列の使い方は、というPythonリストを使うよりも遅くなる可能性があります。

関連する問題