我创建了一个模型来解决Julia中的线性程序。我第一次解决了线性程序,但是相同的代码对修改的程序没有用。你能弄明白发生了什么吗? 谢谢! 工作正常的代码:
m = Model()
@variable(m, x[1:77] >= 0)
for i in nutrients
@constraint(m, dot(data[:, i], x) >= lower[i])
end
@objective(m, Min, sum(dot(c, x)))
status = solve(m)
for i in 1:77
if getvalue(x[i]) > 0
println("Take ", getvalue(x[i]), " dollars of ", foods[i], " every day.")
end
end
println("The total cost for each day is ", getobjectivevalue(m), " dollars.")
println("The total cost for the whole year is ", getobjectivevalue(m) * 365, " dollars.")
结果如下:
Take 0.02951906167648827 dollars of Wheat Flour (Enriched) every day.
Take 0.0018925572907052643 dollars of Liver (Beef) every day.
Take 0.011214435246144865 dollars of Cabbage every day.
Take 0.005007660466725203 dollars of Spinach every day.
Take 0.061028563526693246 dollars of Navy Beans, Dried every day.
The total cost for each day is 0.10866227820675685 dollars.
The total cost for the whole year is 39.66173154546625 dollars.
这是不起作用的代码:
m1 = Model()
for i in 1:77
if i == a
@variable(m1, x[i] == 0)
elseif i == b
@variable(m1, x[i] == 0)
else
@variable(m1, x[i] >= 0)
end
end
for i in nutrients
@constraint(m1, dot(data[:, i], x) >= lower[i])
end
@objective(m1, Min, sum(dot(c, x)))
status = solve(m1)
for i in 1:77
if getvalue(x[i]) > 0
println("Take ", getvalue(x[i]), " dollars of ", foods[i], " every day.")
end
end
println("The total cost for each day is ", getobjectivevalue(m1), " dollars.")
println("The total cost for the whole year is ", getobjectivevalue(m1) * 365, " dollars.")
以下是错误消息:
MethodError: no method matching dot(::NamedArrays.NamedArray{Any,1,Array{Any,1},Tuple{DataStructures.OrderedDict{Any,Int64}}}, ::JuMP.JuMPArray{JuMP.Variable,1,Tuple{Int64}})
Closest candidates are:
dot(::AbstractArray{T,1}, ::AbstractArray{T,1}) at linalg/generic.jl:302
dot{T,S,N}(::Array{T,N}, ::JuMP.JuMPArray{S,N,NT<:Tuple{Vararg{T,N}}}) at /Users/yiboliu/.julia/v0.5/JuMP/src/operators.jl:299
dot{T,S,N}(::JuMP.JuMPArray{T,N,NT<:Tuple{Vararg{T,N}}}, ::JuMP.JuMPArray{S,N,NT<:Tuple{Vararg{T,N}}}) at /Users/yiboliu/.julia/v0.5/JuMP/src/operators.jl:301
in macro expansion; at /Users/yiboliu/.julia/v0.5/JuMP/src/macros.jl:400 [inlined]
in macro expansion; at ./In[23]:13 [inlined]
in anonymous at ./<missing>:?
我知道问题是在约束条件下,但是这部分代码是相同的,并且不起作用。你能不能告诉我发生了什么?
答案 0 :(得分:2)
错误消息表示调用dot()
函数的尝试不符合函数被告知期望的内容。这可能是因为输入的数量是错误的,或者他们的类型不是预期的。由于代码在顶层是相同的,我们可以假设它是后一种解释,可能存在类型问题。
首先检查错误输出以确切地看到对点函数的哪个调用负责(您似乎有两个),然后在调用之前插入一行来输出有关将要使用的输入的信息。您可以使用typeof()
功能。然后将您发送的内容与dot()
真正想要的内容进行比较。
答案 1 :(得分:0)
您不能在同一模型中进行两次$ git --version
git version 2.7.0.windows.1
方法调用,因为它们每次都会定义不同的变量。因此,mac231@UH-WF-320-61 MINGW64 /c/git/test (master)
$ git status
On branch master
Your branch is ahead of 'origin/master' by 4 commits.
(use "git push" to publish your local commits)
nothing to commit, working directory clean
中的第一个@variable
循环必须由
for ... end
(我认为m1
和@variable(m1, x[1:77] >= 0)
@constraint(m1, x[a] == 0)
@constraint(m1, x[b] == 0)
已在别处定义)