有什么办法可以在一个函数内调用两个内部函数

时间:2019-10-30 10:00:32

标签: julia

我有一个函数,该函数具有两个我要调用的内部函数

我尝试使用闭包方法,但我认为我做错了

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()
        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
                produce(v)
                continue
            else
                setupperbound(x[i], v[i])
                setlowerbound(x[i], v[i])
                i += 1
            end
        end
    end

    return setc, scan
end

我想像函数setc(c)那样调用scan()scan_maker,它有一个非平方矩阵作为输入,并且有一个produce()函数我真的不知道如何使用Channel来处理它,因为它已被弃用 当我用{是非平方矩阵来调用scan_maker(A)时,得到的是:

 * 2 linear constraints
 * 4 variables
Solver is Clp), getfield(Main, Symbol("##4#6")){Model,Int64,Array{Int64,1},Array{Int64,1},Array{Int64,1},Array{Variable,1}}(Feasibility problem with:
 * 2 linear constraints
 * 4 variables
Solver is Clp, 4, [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], x[i] ∀ i ∈ {1,2,3,4}))```
which doesn't make sense

0 个答案:

没有答案