Giter VIP home page Giter VIP logo

Comments (4)

mlubin avatar mlubin commented on August 16, 2024

Why don't we get a BoundsError() when using the vectors provided by ipopt?

from ipopt.jl.

tkelman avatar tkelman commented on August 16, 2024

Some of the backtrace was showing signs of LLVM, so it might have to do with codegen/LLVM compiling the function on the first call and having it hit an error while exception-handled by Ipopt instead of Julia? If I run the problematic code once with the solveProblem line commented out, then run it again with it uncommented, I get a backtrace from Ipopt that makes sense going through Ipopt::OrigIpoptNLP::InitializeStructures etc.

from ipopt.jl.

mlubin avatar mlubin commented on August 16, 2024

Hmm, the mix of julia and c++ exceptions seems pretty broken currently.

from ipopt.jl.

odow avatar odow commented on August 16, 2024

Closing because this seems to work okay.

function eval_f(x::Vector{Float64})
    # !!! The x[5] is out-of-bounds !!!
    return x[1] * x[4] * (x[1] + x[2] + x[3]) + x[5]
end

function eval_g(x::Vector{Float64}, g::Vector{Float64})
    g[1] = x[1]   * x[2]   * x[3]   * x[4]
    g[2] = x[1]^2 + x[2]^2 + x[3]^2 + x[4]^2
end

function eval_grad_f(x::Vector{Float64}, grad_f::Vector{Float64})
    grad_f[1] = x[1] * x[4] + x[4] * (x[1] + x[2] + x[3])
    grad_f[2] = x[1] * x[4]
    grad_f[3] = x[1] * x[4] + 1
    grad_f[4] = x[1] * (x[1] + x[2] + x[3])
end

function eval_jac_g(x::Vector{Float64}, mode, rows::Vector{Int32}, cols::Vector{Int32}, values::Vector{Float64})
    if mode == :Structure
        # Constraint (row) 1
        rows[1] = 1; cols[1] = 1
        rows[2] = 1; cols[2] = 2
        rows[3] = 1; cols[3] = 3
        rows[4] = 1; cols[4] = 4
        # Constraint (row) 2
        rows[5] = 2; cols[5] = 1
        rows[6] = 2; cols[6] = 2
        rows[7] = 2; cols[7] = 3
        rows[8] = 2; cols[8] = 4
    else
        # Constraint (row) 1
        values[1] = x[2]*x[3]*x[4]  # 1,1
        values[2] = x[1]*x[3]*x[4]  # 1,2
        values[3] = x[1]*x[2]*x[4]  # 1,3
        values[4] = x[1]*x[2]*x[3]  # 1,4
        # Constraint (row) 2
        values[5] = 2*x[1]  # 2,1
        values[6] = 2*x[2]  # 2,2
        values[7] = 2*x[3]  # 2,3
        values[8] = 2*x[4]  # 2,4
    end
end

function eval_h(x::Vector{Float64}, mode, rows::Vector{Int32}, cols::Vector{Int32}, obj_factor::Float64, lambda::Vector{Float64}, values::Vector{Float64})
    if mode == :Structure
        # Symmetric matrix, fill the lower left triangle only
        idx = 1
        for row = 1:4
            for col = 1:row
            rows[idx] = row
            cols[idx] = col
            idx += 1
            end
        end
    else
        # Again, only lower left triangle
        # Objective
        values[1] = obj_factor * (2*x[4])  # 1,1
        values[2] = obj_factor * (  x[4])  # 2,1
        values[3] = 0                      # 2,2
        values[4] = obj_factor * (  x[4])  # 3,1
        values[5] = 0                      # 3,2
        values[6] = 0                      # 3,3
        values[7] = obj_factor * (2*x[1] + x[2] + x[3])  # 4,1
        values[8] = obj_factor * (  x[1])  # 4,2
        values[9] = obj_factor * (  x[1])  # 4,3
        values[10] = 0                     # 4,4

        # First constraint
        values[2] += lambda[1] * (x[3] * x[4])  # 2,1
        values[4] += lambda[1] * (x[2] * x[4])  # 3,1
        values[5] += lambda[1] * (x[1] * x[4])  # 3,2
        values[7] += lambda[1] * (x[2] * x[3])  # 4,1
        values[8] += lambda[1] * (x[1] * x[3])  # 4,2
        values[9] += lambda[1] * (x[1] * x[2])  # 4,3

        # Second constraint
        values[1]  += lambda[2] * 2  # 1,1
        values[3]  += lambda[2] * 2  # 2,2
        values[6]  += lambda[2] * 2  # 3,3
        values[10] += lambda[2] * 2  # 4,4
    end
end

prob = createProblem(
    4, 
    [1.0, 1.0, 1.0, 1.0], 
    [5.0, 5.0, 5.0, 5.0], 
    2, 
    [25.0, 40.0], 
    [Inf, 40.0], 
    8, 
    10,
    eval_f, 
    eval_g,
    eval_grad_f, 
    eval_jac_g, 
    eval_h,
)

prob.x = [1.0, 5.0, 5.0, 1.0]
solvestat = solveProblem(prob)

julia> solvestat = solveProblem(prob)
This is Ipopt version 3.13.2, running with linear solver mumps.
NOTE: Other linear solvers might be more efficient (see Ipopt documentation).

Number of nonzeros in equality constraint Jacobian...:        4
Number of nonzeros in inequality constraint Jacobian.:        4
Number of nonzeros in Lagrangian Hessian.............:       10

Total number of variables............................:        4
                     variables with only lower bounds:        0
                variables with lower and upper bounds:        4
                     variables with only upper bounds:        0
Total number of equality constraints.................:        1
Total number of inequality constraints...............:        1
        inequality constraints with only lower bounds:        1
   inequality constraints with lower and upper bounds:        0
        inequality constraints with only upper bounds:        0

ERROR: BoundsError: attempt to access 4-element Array{Float64,1} at index [5]
Stacktrace:
 [1] getindex at ./array.jl:809 [inlined]
 [2] eval_f(::Array{Float64,1}) at ./REPL[38]:2
 [3] eval_f_wrapper(::Int32, ::Ptr{Float64}, ::Int32, ::Ptr{Float64}, ::Ptr{Nothing}) at /Users/oscar/.julia/dev/Ipopt/src/Ipopt.jl:143
 [4] solveProblem(::IpoptProblem) at /Users/oscar/.julia/dev/Ipopt/src/Ipopt.jl:361
 [5] top-level scope at REPL[45]:1

from ipopt.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.