无法访问JuMP.variable的第一个元素

时间:2019-12-05 11:06:58

标签: julia

我有一个线性编程优化代码,目前我想在其他代码中使用它,当我从代码中调用函数时,我正在努力在函数中修复BoundsError:< / p>

using JuMP
function scan_maker(A)
    m = JuMP.Model(solver=ClpSolver(PrimalTolerance=1e-3, DualTolerance=1e-3, InfeasibleReturn=1, PresolveType=1))
    # m = Model(solver=GurobiSolver())
    level = size(A, 2)
    v = zeros(Int, level)
    ub = zeros(Int, level)
    lb = zeros(Int, level)

    @variable(m, x[1:level])
    @constraint(m, con, A*x.>=0)

    function setc(c)
        for i = 1:size(A, 1)
            m.linconstr[i].lb = float(c[i])
        end
    end

    function scan(c::Channel)
        i = 1
        init = 1
        while i > 0
            if i >= init
                @objective(m, Max, x[i])
                res = JuMP.solve(m, suppress_warnings=true)
                if res==:Optimal || res==:Unbounded
                    ub[i] = round(Int, getvalue(x[i]))
                    setobjectivesense(m, :Min)
                    res = JuMP.solve(m, suppress_warnings=true)
                    @assert res==:Optimal || res==:Unbounded
                    lb[i] = round(Int, getvalue(x[i]))

                    v[i] = lb[i]
                    init += 1
                else
                    @assert res==:Infeasible
                    i -= 1
                    continue
                end
            elseif v[i] < ub[i]
                v[i] += 1
            else
                setupperbound(x[i], Inf)
                setlowerbound(x[i], -Inf)
                init -= 1
                i -= 1
                continue
            end

            if i >= level
                put!(c, v)
                continue
            else
                setupperbound(x[i], v[i])
                setlowerbound(x[i], v[i])
                i += 1
            end
        end
        close(c)
    end

    return setc, scan
end

现在这段代码可以按照我想要的方式工作,但是当我从下面的函数中将此代码调用函数scan到另一个文件时:

function prob(na)
            @assert count(!iszero, ui2*na) == 0
            b = T0*na
            setc(-b)
            total = 0.0
            for x in Channel(scan)
                nab = vi2*x + b #the photon numbers for each item in the sum
                total += prod([c.^complex(n)/factorial(n) for (c, n) in zip(coef, nab)])
            end
            return abs(total*omega)^2
        end

之后,我调用函数:prob(200),它向我显示此错误: BoundsError: attempt to access 0-element Array{JuMP.Variable,1} at index [1] 我知道错误在prob函数的表达式中引起:nab = vi2*x + b,而正是在我在本文中发布的第一个函数中创建的变量x中: {1}},但我仍然无法解决此问题。 要检查更多我遇到此问题的功能,可以检查以下链接:enter link description here

1 个答案:

答案 0 :(得分:0)

虽然您的问题太长了(距离MWE尚远-最小的工作示例),并且不清楚,让我们假设您要使用JuMP处理数组变量。这是一个最小的示例:

using JuMP
using GLPK
m = Model(with_optimizer(GLPK.Optimizer))
@variable(m, x[1:2] >= 0)
@constraint(m, x[1]+2x[2] <= 100)
@objective(m, Max, 3x[1]+7x[2])
optimize!(m)

现在让我们得到结果

julia> println(m)
Max 3 x[1] + 7 x[2]
Subject to
 x[1] + 2 x[2] <= 100.0
 x[1] >= 0.0
 x[2] >= 0.0

julia> value.(x)
2-element Array{Float64,1}:
  0.0
 50.0

如果要获取x [2]的值,只需编写value(x[2])