### Warm Start Optimization with ipopt Source: https://context7.com/juliasmoothoptimizers/nlpmodelsipopt.jl/llms.txt Enable warm starting by providing initial guesses for primal variables (`x0`), constraint multipliers (`y0`), and bound multipliers (`zL0`, `zU0`). This can significantly reduce solve time if the new problem is similar to a previous one. If all four are provided, IPOPT automatically enables warm starting. ```julia using ADNLPModels, NLPModelsIpopt nlp = ADNLPModel( x -> (x[1] - 1)^2 + 4 * (x[2] - 3)^2, zeros(2), x -> [sum(x) - 1.0], [0.0], [0.0] ) # First solve stats = ipopt(nlp, print_level = 0) println("First solve iterations: ", stats.iter) # Multiple iterations # Warm start from previous solution x0 = copy(stats.solution) y0 = copy(stats.multipliers) zL = zeros(2) # Bound multipliers (zeros if no variable bounds) zU = zeros(2) stats = ipopt(nlp, x0 = x0, y0 = y0, zL0 = zL, zU0 = zU, print_level = 0) println("Warm start iterations: ", stats.iter) # 0 iterations (already at solution) ``` -------------------------------- ### Install NLPModelsIpopt.jl Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/index.md Install NLPModelsIpopt.jl using the Julia package manager. ```julia pkg> add NLPModelsIpopt ``` -------------------------------- ### Use HSL Linear Solvers MA27 and MA57 Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tips.md Example of using Ipopt with HSL linear solvers MA27 and MA57. Ensure HSL_jll is installed and configured. ```julia using HSL_jll, NLPModelsIpopt stats_ma27 = ipopt(nlp, linear_solver="ma27") stats_ma57 = ipopt(nlp, linear_solver="ma57") ``` -------------------------------- ### Switch to Intel MKL BLAS Backend Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tips.md Replace the default OpenBLAS with Intel MKL for improved performance. Requires MKL.jl to be installed. ```julia using MKL # Replace OpenBLAS by Intel MKL using NLPModelsIpopt ``` -------------------------------- ### JuMP Example for Rosenbrock Function Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tutorial.md Present the Rosenbrock function problem and its solution using JuMP and Ipopt.Optimizer. ```julia using JuMP, Ipopt model = Model(Ipopt.Optimizer) x0 = [-1.2; 1.0] @variable(model, x[i=1:2], start=x0[i]) @NLobjective(model, Min, (x[1] - 1)^2 + 100 * (x[2] - x[1]^2)^2) optimize!(model) ``` -------------------------------- ### Develop HSL_jll.jl Package Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tips.md Install the HSL_jll.jl package by developing it from a local path. Ensure you have obtained the necessary license and downloaded the package. ```julia import Pkg Pkg.develop(path = "/full/path/to/HSL_jll.jl") ``` -------------------------------- ### Resetting IpoptSolver with New Initial Guess Source: https://context7.com/juliasmoothoptimizers/nlpmodelsipopt.jl/llms.txt Reset an IpoptSolver for a new initial guess using reset!(solver). The model remains the same, only the starting point changes. ```julia using ADNLPModels, NLPModelsIpopt, SolverCore f(x) = (x[1] - 1)^2 + 4 * (x[2] - x[1]^2)^2 nlp = ADNLPModel(f, [-1.2; 1.0]) stats = GenericExecutionStats(nlp) solver = IpoptSolver(nlp) # First solve stats = solve!(solver, nlp, stats, print_level = 0) # Change initial guess and reset solver (same model) get_x0(nlp) .= 2.0 SolverCore.reset!(solver) # Solve again with new initial guess stats = solve!(solver, nlp, stats, print_level = 0) ``` -------------------------------- ### Custom NLPModel Implementation for Logistic Regression Source: https://context7.com/juliasmoothoptimizers/nlpmodelsipopt.jl/llms.txt Implement the NLPModels API manually for full control over derivatives. This example shows a custom LogisticRegression model. ```julia using DataFrames, LinearAlgebra, NLPModels, NLPModelsIpopt, Random # Custom logistic regression model mutable struct LogisticRegression <: AbstractNLPModel{Float64, Vector{Float64}} X :: Matrix y :: Vector λ :: Real meta :: NLPModelMeta{Float64, Vector{Float64}} counters :: Counters end function LogisticRegression(X, y, λ = 0.0) m, n = size(X) meta = NLPModelMeta(n, name="LogisticRegression", nnzh=div(n * (n+1), 2) + n) return LogisticRegression(X, y, λ, meta, Counters()) end function NLPModels.obj(nlp::LogisticRegression, β::AbstractVector) hβ = 1 ./ (1 .+ exp.(-nlp.X * β)) return -sum(nlp.y .* log.(hβ .+ 1e-8) .+ (1 .- nlp.y) .* log.(1 .- hβ .+ 1e-8)) + nlp.λ * dot(β, β) / 2 end function NLPModels.grad!(nlp::LogisticRegression, β::AbstractVector, g::AbstractVector) hβ = 1 ./ (1 .+ exp.(-nlp.X * β)) g .= nlp.X' * (hβ .- nlp.y) + nlp.λ * β end function NLPModels.hess_structure!(nlp::LogisticRegression, rows::AbstractVector{<:Integer}, cols::AbstractVector{<:Integer}) n = nlp.meta.nvar I = ((i,j) for i = 1:n, j = 1:n if i ≥ j) rows[1:nlp.meta.nnzh] .= [getindex.(I, 1); 1:n] cols[1:nlp.meta.nnzh] .= [getindex.(I, 2); 1:n] return rows, cols end function NLPModels.hess_coord!(nlp::LogisticRegression, β::AbstractVector, vals::AbstractVector; obj_weight=1.0, y=Float64[]) n, m = nlp.meta.nvar, length(nlp.y) hβ = 1 ./ (1 .+ exp.(-nlp.X * β)) fill!(vals, 0.0) for k = 1:m hk = hβ[k] p = 1 for j = 1:n, i = j:n vals[p] += obj_weight * hk * (1 - hk) * nlp.X[k,i] * nlp.X[k,j] p += 1 end end vals[nlp.meta.nnzh+1:end] .= nlp.λ * obj_weight return vals end # Train logistic regression model Random.seed!(0) m = 1000 df = DataFrame(:age => rand(18:60, m), :salary => rand(40:180, m) * 1000) df.buy = (df.age .> 40 .+ randn(m) * 5) .| (df.salary .> 120_000 .+ randn(m) * 10_000) X = [ones(m) df.age df.age.^2 df.salary df.salary.^2 df.age .* df.salary] y = df.buy λ = 1.0e-2 nlp = LogisticRegression(X, y, λ) stats = ipopt(nlp, print_level = 0) β = stats.solution println("Trained coefficients: ", β) ``` -------------------------------- ### Solve Constrained Optimization with ipopt Source: https://context7.com/juliasmoothoptimizers/nlpmodelsipopt.jl/llms.txt Define constrained optimization problems by providing constraint functions and their lower/upper bounds. Equality constraints are specified when lower and upper bounds are equal. This example demonstrates a simple linear constraint and a more complex chain of constraints. ```julia using ADNLPModels, NLPModelsIpopt # Minimize f(x) = (x₁ - 1)² + 4(x₂ - 3)² subject to x₁ + x₂ = 1 nlp = ADNLPModel( x -> (x[1] - 1)^2 + 4 * (x[2] - 3)^2, # Objective zeros(2), # Initial guess x -> [sum(x) - 1.0], # Constraint: x₁ + x₂ - 1 = 0 [0.0], # Lower bound on constraint [0.0] # Upper bound on constraint (equality) ) stats = ipopt(nlp, print_level = 0) println("Solution: ", stats.solution) # [-1.4, 2.4] println("Multipliers: ", stats.multipliers) # Lagrange multipliers for constraints ``` ```julia # Constrained problem with chain of Rosenbrock functions n = 10 x0 = ones(n) x0[1:2:end] .= -1.2 lcon = ucon = zeros(n - 2) # Equality constraints nlp = ADNLPModel( x -> sum((x[i] - 1)^2 + 100 * (x[i+1] - x[i]^2)^2 for i = 1:n-1), x0, x -> [3 * x[k+1]^3 + 2 * x[k+2] - 5 + sin(x[k+1] - x[k+2]) * sin(x[k+1] + x[k+2]) + 4 * x[k+1] - x[k] * exp(x[k] - x[k+1]) - 3 for k = 1:n-2], lcon, ucon ) stats = ipopt(nlp, print_level = 0) println("Status: ", stats.status) # :first_order ``` -------------------------------- ### Set SPRAL Environment Variables Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tips.md Environment variables required before starting Julia when using the SPRAL linear solver. These enable cancellation and process binding for OpenMP. ```shell export OMP_CANCELLATION=TRUE export OMP_PROC_BIND=TRUE ``` -------------------------------- ### Implement Custom Stopping Criteria with Callbacks Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tutorial.md Use callbacks to define custom stopping criteria for optimization. This example stops if the objective value falls below a threshold. ```julia function custom_stopping_callback(alg_mod, iter_count, obj_value, inf_pr, inf_du, mu, d_norm, regularization_size, alpha_du, alpha_pr, ls_trials, args...) # Custom stopping criterion: stop if objective gets close to optimum if obj_value < 0.01 println("Custom stopping criterion met at iteration $iter_count") return false # Stop optimization end return true # Continue optimization end nlp = ADNLPModel(x -> (x[1] - 1)^2 + 100 * (x[2] - x[1]^2)^2, [-1.2; 1.0]) stats = ipopt(nlp, callback = custom_stopping_callback, print_level = 0) ``` -------------------------------- ### Monitor Optimization with Advanced Solver Interface Callback Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tutorial.md Use callbacks with the advanced solver interface (IpoptSolver) to monitor optimization. Requires ADNLPModels and NLPModelsIpopt. ```julia # Advanced usage with IpoptSolver solver = IpoptSolver(nlp) stats = solve!(solver, nlp, callback = my_callback, print_level = 0) ``` -------------------------------- ### Advanced Solver Interface with IpoptSolver Source: https://context7.com/juliasmoothoptimizers/nlpmodelsipopt.jl/llms.txt Create an IpoptSolver instance for solving multiple problems or reusing solver memory efficiently. Use solve! to execute the optimization. ```julia using ADNLPModels, NLPModelsIpopt, SolverCore nlp = ADNLPModel(x -> (x[1] - 1)^2 + 100 * (x[2] - x[1]^2)^2, [-1.2; 1.0]) stats = GenericExecutionStats(nlp) solver = IpoptSolver(nlp) # First solve stats = solve!(solver, nlp, stats, print_level = 0) # Solve a different problem with same structure nlp2 = ADNLPModel(x -> (x[1])^2 + 100 * (x[2] - x[1]^2)^2, [-1.2; 1.0]) SolverCore.reset!(solver, nlp2) stats = solve!(solver, nlp2, stats, print_level = 0) ``` -------------------------------- ### Set IPOPT Options and Tolerances Source: https://context7.com/juliasmoothoptimizers/nlpmodelsipopt.jl/llms.txt Configure IPOPT solver behavior by passing any valid IPOPT option as a keyword argument to the `ipopt` function. This includes setting convergence tolerances, iteration limits, and output verbosity. Refer to the IPOPT documentation for a comprehensive list of available options. ```julia using ADNLPModels, NLPModelsIpopt nlp = ADNLPModel(x -> (x[1] - 1)^2 + 100 * (x[2] - x[1]^2)^2, [-1.2; 1.0]) ``` -------------------------------- ### Switch BLAS Backends at Runtime Source: https://context7.com/juliasmoothoptimizers/nlpmodelsipopt.jl/llms.txt Configure BLAS backends at runtime for improved performance using libblastrampoline. This is available in Julia 1.9+. ```julia # Use Intel MKL for potentially better performance using MKL using NLPModelsIpopt # Or use Apple Accelerate on macOS 13.4+ using AppleAccelerate using NLPModelsIpopt # Check current BLAS configuration import LinearAlgebra LinearAlgebra.BLAS.lbt_get_config() ``` -------------------------------- ### Display Loaded BLAS/LAPACK Backends Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tips.md Query the loaded BLAS and LAPACK backends configuration using libblastrampoline. ```julia import LinearAlgebra LinearAlgebra.BLAS.lbt_get_config() ``` -------------------------------- ### Monitor Optimization with Callback Function Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tutorial.md Use a callback function to monitor the optimization process, accessing iterate and constraint violations at each iteration. The callback function receives several parameters from Ipopt. Requires ADNLPModels and NLPModelsIpopt. ```julia using ADNLPModels, NLPModelsIpopt function my_callback(alg_mod, iter_count, obj_value, inf_pr, inf_du, mu, d_norm, regularization_size, alpha_du, alpha_pr, ls_trials, args...) # Log iteration information (these are the standard parameters passed by Ipopt) println("Iteration $iter_count:") println(" Objective value = ", obj_value) println(" Primal infeasibility = ", inf_pr) println(" Dual infeasibility = ", inf_du) println(" Complementarity = ", mu) # Return true to continue, false to stop return iter_count < 5 # Stop after 5 iterations for this example end nlp = ADNLPModel(x -> (x[1] - 1)^2 + 100 * (x[2] - x[1]^2)^2, [-1.2; 1.0]) stats = ipopt(nlp, callback = my_callback, print_level = 0) ``` -------------------------------- ### Switch to Apple Accelerate BLAS Backend Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tips.md Replace the default OpenBLAS with Apple Accelerate for optimized performance on macOS. Requires AppleAccelerate.jl and macOS version 13.4 or later. ```julia using AppleAccelerate # Replace OpenBLAS by Apple Accelerate using NLPModelsIpopt ``` -------------------------------- ### NLPModelsIpopt Module Documentation Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/reference.md This section provides autodocumented API references for the NLPModelsIpopt module. ```APIDOC ## NLPModelsIpopt Module ### Description Autodocumented API references for the NLPModelsIpopt module. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response #### Success Response (200) - **Module Documentation** (string) - Detailed documentation for the NLPModelsIpopt module and its exported functions. #### Response Example ``` # Documentation for NLPModelsIpopt.jl functions and types will be listed here. ``` ``` -------------------------------- ### Nonlinear Least-Squares with IPOPT Source: https://context7.com/juliasmoothoptimizers/nlpmodelsipopt.jl/llms.txt Solve nonlinear least-squares problems by passing an AbstractNLSModel to ipopt. The package converts it to a feasibility form suitable for IPOPT. ```julia using ADNLPModels, NLPModelsIpopt # Solve min ½‖F(x)‖² where F(x) = [x₁ - 1, x₂ - 2] nls = ADNLSModel(x -> [x[1] - 1, x[2] - 2], [0.0, 0.0], 2) stats = ipopt(nls, print_level = 0) ``` -------------------------------- ### Use SPRAL Linear Solver Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tips.md Configure Ipopt to use the SPRAL linear solver. This requires Julia version 1.9 or later and the SPRAL library. ```julia using NLPModelsIpopt stats_spral = ipopt(nlp, linear_solver="spral") ``` -------------------------------- ### High Precision Solve with Custom Options Source: https://context7.com/juliasmoothoptimizers/nlpmodelsipopt.jl/llms.txt Set custom convergence tolerance, maximum iterations, and print level for IPOPT. Suppress output by setting print_level to 0. ```julia stats = ipopt(nlp, tol = 1e-12, # Convergence tolerance max_iter = 100, # Maximum iterations print_level = 0, # Suppress output (0-12) print_timing_statistics = "yes" ) ``` -------------------------------- ### Solve Rosenbrock Function with Ipopt Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tutorial.md Create an NLPModel for the Rosenbrock function and solve it using Ipopt. Requires ADNLPModels, NLPModels, and NLPModelsIpopt. ```julia using ADNLPModels, NLPModels, NLPModelsIpopt nlp = ADNLPModel(x -> (x[1] - 1)^2 + 100 * (x[2] - x[1]^2)^2, [-1.2; 1.0]) stats = ipopt(nlp) print(stats) ``` -------------------------------- ### Callback Functions for Monitoring Optimization Source: https://context7.com/juliasmoothoptimizers/nlpmodelsipopt.jl/llms.txt Monitor optimization progress using callback functions. The callback receives iteration information and can stop optimization by returning false. Custom stopping criteria can be implemented. ```julia using ADNLPModels, NLPModelsIpopt function my_callback(alg_mod, iter_count, obj_value, inf_pr, inf_du, mu, d_norm, regularization_size, alpha_du, alpha_pr, ls_trials, args...) # Log iteration information println("Iteration $iter_count: obj=$obj_value, primal_inf=$inf_pr, dual_inf=$inf_du") # Return true to continue, false to stop return iter_count < 5 # Stop after 5 iterations end nlp = ADNLPModel(x -> (x[1] - 1)^2 + 100 * (x[2] - x[1]^2)^2, [-1.2; 1.0]) stats = ipopt(nlp, callback = my_callback, print_level = 0) # Custom stopping criterion based on objective value function stop_on_objective(alg_mod, iter_count, obj_value, args...) if obj_value < 0.01 println("Objective threshold reached at iteration $iter_count") return false end return true end stats = ipopt(nlp, callback = stop_on_objective, print_level = 0) ``` -------------------------------- ### Linear Solvers Configuration with IPOPT Source: https://context7.com/juliasmoothoptimizers/nlpmodelsipopt.jl/llms.txt Configure IPOPT to use different linear solvers like SPRAL or HSL solvers (MA27, MA57) for potentially better performance. SPRAL requires Julia ≥ 1.9. ```julia using NLPModelsIpopt # Using SPRAL (available with Julia ≥ 1.9) # Note: Requires environment variables: # export OMP_CANCELLATION=TRUE # export OMP_PROC_BIND=TRUE stats_spral = ipopt(nlp, linear_solver = "spral") # Using HSL solvers (requires HSL_jll.jl license) # import Pkg # Pkg.develop(path = "/path/to/HSL_jll.jl") using HSL_jll, NLPModelsIpopt stats_ma27 = ipopt(nlp, linear_solver = "ma27") stats_ma57 = ipopt(nlp, linear_solver = "ma57") ``` -------------------------------- ### Implement Logistic Regression NLPModel Manually Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tutorial.md Manually implement the NLPModel API for a logistic regression problem. This includes defining objective, gradient, and Hessian. ```julia using DataFrames, LinearAlgebra, NLPModels, NLPModelsIpopt, Random mutable struct LogisticRegression <: AbstractNLPModel{Float64, Vector{Float64}} X :: Matrix y :: Vector λ :: Real meta :: NLPModelMeta{Float64, Vector{Float64}} # required by AbstractNLPModel counters :: Counters # required by AbstractNLPModel end function LogisticRegression(X, y, λ = 0.0) m, n = size(X) meta = NLPModelMeta(n, name="LogisticRegression", nnzh=div(n * (n+1), 2) + n) # nnzh is the length of the coordinates vectors return LogisticRegression(X, y, λ, meta, Counters()) end function NLPModels.obj(nlp :: LogisticRegression, β::AbstractVector) hβ = 1 ./ (1 .+ exp.(-nlp.X * β)) return -sum(nlp.y .* log.(hβ .+ 1e-8) .+ (1 .- nlp.y) .* log.(1 .- hβ .+ 1e-8)) + nlp.λ * dot(β, β) / 2 end function NLPModels.grad!(nlp :: LogisticRegression, β::AbstractVector, g::AbstractVector) hβ = 1 ./ (1 .+ exp.(-nlp.X * β)) g .= nlp.X' * (hβ .- nlp.y) + nlp.λ * β end function NLPModels.hess_structure!(nlp :: LogisticRegression, rows :: AbstractVector{<:Integer}, cols :: AbstractVector{<:Integer}) n = nlp.meta.nvar I = ((i,j) for i = 1:n, j = 1:n if i ≥ j) rows[1 : nlp.meta.nnzh] .= [getindex.(I, 1); 1:n] cols[1 : nlp.meta.nnzh] .= [getindex.(I, 2); 1:n] return rows, cols end function NLPModels.hess_coord!(nlp :: LogisticRegression, β::AbstractVector, vals::AbstractVector; obj_weight=1.0, y=Float64[]) n, m = nlp.meta.nvar, length(nlp.y) hβ = 1 ./ (1 .+ exp.(-nlp.X * β)) fill!(vals, 0.0) for k = 1:m hk = hβ[k] p = 1 for j = 1:n, i = j:n vals[p] += obj_weight * hk * (1 - hk) * nlp.X[k,i] * nlp.X[k,j] p += 1 end end vals[nlp.meta.nnzh+1:end] .= nlp.λ * obj_weight return vals end Random.seed!(0) # Training set m = 1000 df = DataFrame(:age => rand(18:60, m), :salary => rand(40:180, m) * 1000) df.buy = (df.age .> 40 .+ randn(m) * 5) .| (df.salary .> 120_000 .+ randn(m) * 10_000) X = [ones(m) df.age df.age.^2 df.salary df.salary.^2 df.age .* df.salary] y = df.buy λ = 1.0e-2 nlp = LogisticRegression(X, y, λ) stats = ipopt(nlp, print_level=0) β = stats.solution ``` -------------------------------- ### Solve Constrained Problem with Ipopt Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tutorial.md Solve a constrained optimization problem using Ipopt. The problem is defined with an objective function and constraints. Requires ADNLPModels, NLPModels, and NLPModelsIpopt. ```julia n = 10 x0 = ones(n) x0[1:2:end] .= -1.2 lcon = ucon = zeros(n-2) nlp = ADNLPModel(x -> sum((x[i] - 1)^2 + 100 * (x[i+1] - x[i]^2)^2 for i = 1:n-1), x0, x -> [3 * x[k+1]^3 + 2 * x[k+2] - 5 + sin(x[k+1] - x[k+2]) * sin(x[k+1] + x[k+2]) + 4 * x[k+1] - x[k] * exp(x[k] - x[k+1]) - 3 for k = 1:n-2], lcon, ucon) stats = ipopt(nlp, print_level=0) print(stats) ``` -------------------------------- ### Use L-BFGS Hessian Approximation with Ipopt Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tutorial.md Call Ipopt with L-BFGS Hessian approximation by passing specific options. This is the default behavior if hess_available is false. ```julia stats_ipopt = ipopt(nlp, hessian_approximation="limited-memory", limited_memory_update_type="bfgs", limited_memory_max_history=6) ``` -------------------------------- ### Use L-BFGS Hessian Approximation with IPOPT Source: https://context7.com/juliasmoothoptimizers/nlpmodelsipopt.jl/llms.txt Configure IPOPT to use the L-BFGS Hessian approximation for optimization. This is useful for problems where computing the exact Hessian is expensive. ```julia stats = ipopt(nlp, hessian_approximation = "limited-memory", limited_memory_update_type = "bfgs", limited_memory_max_history = 6, print_level = 0 ) println("Solution: ", stats.solution) println("Status: ", stats.status) ``` -------------------------------- ### Visualize Decision Boundary and Data Points Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tutorial.md Visualizes the generated data points and the decision boundary of the logistic regression model. Requires Plots.jl and gr() backend. Ensure 'β' is defined. ```julia using Plots gr() f(a, b) = dot(β, [1.0; a; a^2; b; b^2; a * b]) P = findall(df.buy .== true) scatter(df.age[P], df.salary[P], c=:blue, m=:square) P = findall(df.buy .== false) scatter!(df.age[P], df.salary[P], c=:red, m=:xcross, ms=7) contour!(range(18, 60, step=0.1), range(40_000, 180_000, step=1.0), f, levels=[0.5]) ``` -------------------------------- ### Maximization Problem Source: https://context7.com/juliasmoothoptimizers/nlpmodelsipopt.jl/llms.txt Solve a maximization problem by setting minimize = false in the model. The objective value will be the maximum value found. ```julia nlp_max = ADNLPModel(x -> x[1], rand(1), zeros(1), ones(1), minimize = false) stats = ipopt(nlp_max, print_level = 0) ``` -------------------------------- ### Access Ipopt Solver Specific Message Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tutorial.md Access the detailed Ipopt output message stored in the solver_specific field of the GenericExecutionStats instance. ```julia stats.solver_specific[:internal_msg] ``` -------------------------------- ### L-BFGS Hessian Approximation Source: https://context7.com/juliasmoothoptimizers/nlpmodelsipopt.jl/llms.txt When the exact Hessian is unavailable, IPOPT automatically uses L-BFGS approximation. It can also be explicitly enabled or customized. ```julia using ADNLPModels, NLPModelsIpopt nlp = ADNLPModel(x -> sum(x.^2), ones(10)) ``` -------------------------------- ### Solve Rosenbrock Function with ipopt Source: https://context7.com/juliasmoothoptimizers/nlpmodelsipopt.jl/llms.txt Use the `ipopt` function to solve an unconstrained nonlinear programming problem. Pass IPOPT options as keyword arguments, such as `print_level` for output control. Results are returned in a `GenericExecutionStats` object. ```julia using ADNLPModels, NLPModelsIpopt # Solve the Rosenbrock function: min (x₁ - 1)² + 100(x₂ - x₁²)² nlp = ADNLPModel(x -> (x[1] - 1)^2 + 100 * (x[2] - x[1]^2)^2, [-1.2; 1.0]) stats = ipopt(nlp, print_level = 0) # Access solution results println("Solution: ", stats.solution) # [1.0, 1.0] println("Objective: ", stats.objective) # ≈ 0.0 println("Status: ", stats.status) # :first_order println("Iterations: ", stats.iter) # 21 println("Primal feasibility: ", stats.primal_feas) # 0.0 println("Dual feasibility: ", stats.dual_feas) # ≈ 0.0 println("Elapsed time: ", stats.elapsed_time) println("Internal message: ", stats.solver_specific[:internal_msg]) # :Solve_Succeeded ``` -------------------------------- ### Generate and Predict with Logistic Regression Model Source: https://github.com/juliasmoothoptimizers/nlpmodelsipopt.jl/blob/main/docs/src/tutorial.md Generates a synthetic dataset and predicts outcomes using a logistic regression model. Ensure 'β' is defined before running. ```julia m = 100 df = DataFrame(:age => rand(18:60, m), :salary => rand(40:180, m) * 1000) df.buy = (df.age .> 40 .+ randn(m) * 5) .| (df.salary .> 120_000 .+ randn(m) * 10_000) X = [ones(m) df.age df.age.^2 df.salary df.salary.^2 df.age .* df.salary] hβ = 1 ./ (1 .+ exp.(-X * β)) ypred = hβ .> 0.5 acc = count(df.buy .== ypred) / m println("acc = $acc") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.