2014-07-12 78 views
3

我寫了下面的代碼方法匹配*(::變量):錯誤:`*`沒有

using JuMP 

m = Model() 

const A = 
[ :a0 , 
    :a1 , 
    :a2 ] 

const T = [1:5] 

const U = 
[ 
    :a0 => [9 9 9 9 999], 
    :a1 => [11 11 11 11 11], 
    :a2 => [1 1 1 1 1] 
] 

@defVar(m, x[A,T], Bin) 

@setObjective(m, Max, sum{sum{x[i,j] * U[i,j], i=A}, j=T}) 

print(m) 

status = solve(m) 

println("Objective value: ", getObjectiveValue(m)) 
println("x = ", getValue(x)) 

當我運行它,我得到以下錯誤

ERROR: `*` has no method matching *(::Variable) 
in anonymous at /home/username/.julia/v0.3/JuMP/src/macros.jl:71 
in include at ./boot.jl:245 
in include_from_node1 at loading.jl:128 
in process_options at ./client.jl:285 
in _start at ./client.jl:354 
while loading /programs/julia-0.2.1/models/a003.jl, in expression starting on line 21 

什麼是正確的這樣做的方式?

回答

5

由於the manual說:

There is one key restriction on the form of the expression in the second case: if there is a product between coefficients and variables, the variables must appear last. That is, Coefficient times Variable is good, but Variable times Coefficient is bad

讓我知道如果有,我可以把這個會幫助你了另一個地方。

這種情況並不理想,但不幸的是我們還沒有一個好的解決方案,但仍保留了JuMP的快速建模功能。

我相信U的問題在於它是一個數組字典,因此您首先需要索引到字典中以返回正確的數組,然後索引到數組中。 JuMP的變量具有更強大的索引功能,因此允許您在一組[]中執行此操作。

+0

謝謝你的回答。手冊很清楚也許你可以更加強調這樣一個事實,即係數的意圖是數字和常量。無論如何,這足夠清晰,因爲我在短時間內解決了我的問題,只是推理而已。 – HAL9000

3

我解決了我的問題:常量必須位於變量之前,因爲我在某處讀取,而且它似乎必須將一個常量數組用作數組數組,同時將變量用作矩陣。

這裏是正確的路線:

@setObjective(m, Max, sum{sum{U[i][j]*x[i,j], i=A}, j=T})