jump-dev / KNITRO.jl

A Julia interface to the Artelys Knitro solver
https://www.artelys.com/knitro
Other
78 stars 23 forks source link

SYSTEM: show(lasterr) caused an error #231

Closed GabrielPonte closed 2 years ago

GabrielPonte commented 2 years ago

I was trying to use KNITRO with MOI, and I tested this example here using KNITRO.jl. I tried a Cutting Plane method, so after optimizing the model, I added the constraint below MOI.add_constraint(optimizer,x[1],MathOptInterface.LessThan{Float64}(0.0)); And this caused the error: SYSTEM: show(lasterr) caused an error I tried to use GLPK and the code worked with it. Here is the code that I used

using MathOptInterface
const MOI = MathOptInterface
using KNITRO
optimizer = KNITRO.Optimizer()
c = [1.0, 2.0, 3.0]
w = [0.3, 0.5, 1.0]
C = 3.2
x = MOI.add_variables(optimizer, length(c));
MOI.set(
           optimizer,
           MOI.ObjectiveFunction{MOI.ScalarAffineFunction{Float64}}(),
           MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(c, x), 0.0),
       );
MOI.set(optimizer, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.add_constraint(
           optimizer,
           MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.(w, x), 0.0),
           MOI.LessThan(C),
       );
for x_i in x
    MOI.add_constraint(optimizer, x_i, MOI.ZeroOne())
end
MOI.optimize!(optimizer)
MOI.add_constraint(optimizer,x[1],MathOptInterface.LessThan{Float64}(0.0)); # ERROR
MOI.optimize!(optimizer)
MOI.get(optimizer, MOI.VariablePrimal(), x)

And here is the log

=======================================
           Academic License
       (NOT FOR COMMERCIAL USE)
         Artelys Knitro 12.4.0
=======================================

No start point provided -- Knitro computing one.

datacheck:               0
hessian_no_f:            1
The problem is identified as an MILP.
Knitro changing mip_method from AUTO to 1.
Knitro changing mip_rootalg from AUTO to 1.
Knitro changing mip_lpalg from AUTO to 3.
Knitro changing mip_nodealg from AUTO to 3.
Knitro changing mip_branchrule from AUTO to 2.
Knitro changing mip_selectrule from AUTO to 2.
Knitro changing mip_mir from AUTO to 0.
Knitro changing mip_rounding from AUTO to 3.
Knitro changing mip_heuristic_strategy from AUTO to 1.
Knitro changing mip_heuristic_feaspump from AUTO to 1.
Knitro changing mip_heuristic_mpec from AUTO to 0.
Knitro changing mip_heuristic_diving from AUTO to 0.
Knitro changing mip_pseudoinit from AUTO to 1.

Problem Characteristics
-----------------------
Objective goal:  Maximize
Objective type:  linear
Number of variables:                                  3
    bounded below only:                               0
    bounded above only:                               0
    bounded below and above:                          3
    fixed:                                            0
    free:                                             0
Number of binary variables:                           3
Number of integer variables:                          0
Number of constraints:                                1
    linear equalities:                                0
    quadratic equalities:                             0
    gen. nonlinear equalities:                        0
    linear one-sided inequalities:                    1
    quadratic one-sided inequalities:                 0
    gen. nonlinear one-sided inequalities:            0
    linear two-sided inequalities:                    0
    quadratic two-sided inequalities:                 0
    gen. nonlinear two-sided inequalities:            0
Number of nonzeros in Jacobian:                       3
Number of nonzeros in Hessian:                        0

Knitro detected 0 GUB constraints
Knitro derived 0 knapsack covers after examining 1 constraint
Knitro solving root node relaxation

   Node    Left    Iinf     Objective         Best Relaxatn  Best Incumbent
  ------  ------  ------  --------------     --------------  --------------
*      1       0       0    6.000000e+00       6.000000e+00    6.000000e+00

EXIT: Optimal solution found.

Final Statistics for MIP
------------------------
Final objective value               =   6.00000000000000e+00
Final integrality gap (abs / rel)   =  0.00e+00 /   0.00e+00 ( 0.00%)
# of nodes processed                =        1
# of subproblems processed          =        2
# of LP iterations                  =        0
Total program time (secs)           =       0.006 (     0.016 CPU time)
Time spent in evaluations (secs)    =       0.000

===========================================================================

SYSTEM: show(lasterr) caused an error

Stacktrace:
 [1] add_constraint(model::KNITRO.Optimizer, vi::MathOptInterface.VariableIndex, lt::MathOptInterface.LessThan{Float64})
   @ KNITRO C:\Users\gabponte\.julia\packages\KNITRO\6cfYO\src\MOI_wrapper\constraints.jl:150
 [2] top-level scope
   @ In[3]:24
 [3] eval
   @ .\boot.jl:373 [inlined]
 [4] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base .\loading.jl:1196
odow commented 2 years ago

I don't know what the show problem is (we're likely missing a method), but KNITRO doesn't support adding constraints after optimize!: https://github.com/jump-dev/KNITRO.jl/blob/d91f31830578e9928ab87320e53b3b8c62a2f2ca/src/MOI_wrapper/constraints.jl#L145-L150

One solution is to use a caching optimizer:

model = MOI.Utilities.CachingOptimizer(
    MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
    KNITRO.Optimizer(),
)
GabrielPonte commented 2 years ago

Thanks, I replaced optimizer = KNITRO.Optimizer() to

optimizer = MOI.Utilities.CachingOptimizer(
    MOI.Utilities.UniversalFallback(MOI.Utilities.Model{Float64}()),
    KNITRO.Optimizer(),
)

but I got this error

UndefVarError: automatic_copy_to not defined

Stacktrace:
 [1] getproperty
   @ .\Base.jl:35 [inlined]
 [2] copy_to(model::KNITRO.Optimizer, src::MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}})
   @ KNITRO C:\Users\gabponte\.julia\packages\KNITRO\6cfYO\src\MOI_wrapper.jl:206
 [3] optimize!
   @ C:\Users\gabponte\.julia\packages\MathOptInterface\kCmJV\src\MathOptInterface.jl:86 [inlined]
 [4] optimize!(m::MathOptInterface.Utilities.CachingOptimizer{KNITRO.Optimizer, MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}})
   @ MathOptInterface.Utilities C:\Users\gabponte\.julia\packages\MathOptInterface\kCmJV\src\Utilities\cachingoptimizer.jl:316
 [5] top-level scope
   @ In[15]:23
 [6] eval
   @ .\boot.jl:373 [inlined]
 [7] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base .\loading.jl:1196

Actually, the main reason that I opened this issue is because I'm getting the same error using Juniper with KNITRO.jl. Should I open another issue in here?

odow commented 2 years ago

No need to open another issue. There are a bunch of issues with the KNITRO wrapper that need fixing. We're mainly constrained because we don't have CI, although I hope to sort that today or tomorrow.

GabrielPonte commented 2 years ago

Thank you!