Search code examples
juliajulia-jump

How can I remove the "type VariableIndex has no field head" error?


Would you please help me, why the error"type VariableIndex has no field head" was happened when I run the bellow code

using JuMP,CPUTime, Distributions, Ipopt,Juniper,Gurobi
#-----Model parameters--------------------------------------------------------
sig, C1, c0 = 2, 300, 10;
E, landa, T0, T1, T2, gam1, gam2, a1, a2, a3, ap = 0.05, 0.01, 0, 2, 2, 1, 1, 0.5, 0.1, 50, 25;
f(x) = cdf(Normal(0, 1), x);
#---------------------------------------------------------------------------
ALT= Model(optimizer_with_attributes(Juniper.Optimizer, "nl_solver"=>optimizer_with_attributes(Ipopt.Optimizer, "print_level" => 0),

       "mip_solver"=>optimizer_with_attributes(Gurobi.Optimizer, "logLevel" => 0),"registered_functions" =>[Juniper.register( :f, 1, f; autodiff = true)])

       );

# variables-----------------------------------------------------------------
JuMP.register(ALT, :f, 1, f; autodiff = true);
@variable(ALT, 0.001 <= h <= 40);
@variable(ALT, 0.000001 <= L <= 4);
@variable(ALT, 2 <= n <= 30, Int);
@variable(ALT, y);
#---------------------------------------------------------------------------

@NLexpression(ALT,k1,h/(1-f(L-sig*sqrt(n))+f(-L - sig*sqrt(n)))); # HARL1

@NLexpression(ALT,k2,(1-(1+landa*h)*exp(-landa*h))/(landa*(1-exp(-landa*h)))); #to

@NLexpression(ALT,k3,E*n+T1*gam1+T2*gam2);

@NLexpression(ALT,k4,1/landa+h/(1-f(L-sig*sqrt(n))+f(-L-sig*sqrt(n))));

@NLexpression(ALT,k5,-(1-(1+landa*h)*exp(-landa*h))/(landa*(1-exp(-landa*h)))+E*n+T1*gam1+T2*gam2);

@NLexpression(ALT,k6,(exp(-landa*h)/1-exp(-landa*h))*(a3/(2*f(-L)))+ap); #SF/ARL0+W F=a3 ap=W

@NLexpression(ALT,k7,1-f(L-sig*sqrt(n))+f(-L-sig*sqrt(n)));

@NLexpression(ALT,F,c0/landa+C1*(k1-k2+k3)+((a1+a2*n)/h)*(k4+k5+k3)+k6);

@NLexpression(ALT,FF,k4-k2+E*n+T1+T2+(1-gam1)*((exp(-landa*h)/1-exp(-landa*h)*T0)/(2*f(-L)))); #ST0/ARL0

#constraints--------------------------------------------------------
@NLconstraint(ALT, f(-L) <= 1/400);
@NLconstraint(ALT, f1 <=y);
#objectivefunction---------------------------------------------------------
f1=@NLexpression(ALT,F/FF);
f2=@NLexpression(ALT,1/k7);
#-------------------------------------------------------------------------
@NLobjective(ALT,Min,y);
optimize!(ALT);
#--------------------------------------------------------------------------
f1min=JuMP.value(y);
LL=JuMP.value(L);
nn=JuMP.value(n);
hh=JuMP.value(h);

This is the section of a code. If it is needed, I can replace all the code. the error was happened is following:

ERROR: type VariableIndex has no field head
Stacktrace:
 [1] getproperty(::MathOptInterface.VariableIndex, ::Symbol) at .\Base.jl:33
 [2] expr_dereferencing!(::Expr, ::Model) at C:\Users\admin\.julia\packages\Juniper\dNHnx\src\util.jl:8
 [3] expr_dereferencing(::Expr, ::Model) at C:\Users\admin\.julia\packages\Juniper\dNHnx\src\util.jl:21
 [4] create_root_model!(::Juniper.Optimizer, ::Juniper.JuniperProblem; fix_start::Bool) at 
C:\Users\admin\.julia\packages\Juniper\dNHnx\src\model.jl:22
 [5] create_root_model! at C:\Users\admin\.julia\packages\Juniper\dNHnx\src\model.jl:5 [inlined]
 [6] optimize!(::Juniper.Optimizer) at C:\Users\admin\.julia\packages\Juniper\dNHnx\src\MOI_wrapper\MOI_wrapper.jl:371
 [7] optimize!(::MathOptInterface.Bridges.LazyBridgeOptimizer{Juniper.Optimizer}) at C:\Users\admin\.julia\packages\MathOptInterface\bygN7\src\Bridges\bridge_optimizer.jl:239        
 [8] optimize!(::MathOptInterface.Utilities.CachingOptimizer{MathOptInterface.AbstractOptimizer,MathOptInterface.Utilities.UniversalFallback{MathOptInterface.Utilities.Model{Float64}}}) at C:\Users\admin\.julia\packages\MathOptInterface\bygN7\src\Utilities\cachingoptimizer.jl:189
 [9] optimize!(::Model, ::Nothing; bridge_constraints::Bool, ignore_optimize_hook::Bool, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\admin\.julia\packages\JuMP\YXK4e\src\optimizer_interface.jl:131
 [10] optimize! at C:\Users\admin\.julia\packages\JuMP\YXK4e\src\optimizer_interface.jl:107 [inlined] (repeats 2 times)
 [11] top-level scope at none:1

The completed section of the code was replaced.


Solution

  • I cannot reproduce this on the latest version of JuMP and Juniper:

    julia> using JuMP, Distributions, Ipopt, Juniper, Gurobi
    
    julia> sig, C1, c0 = 2, 300, 10;
    
    julia> E, landa, T1, T2, gam1, gam2, a1, a2 = 0.05, 0.01, 2, 2, 1, 1, 0.5, 0.1;
    
    julia> f(x) = cdf(Normal(0, 1), x);
    
    julia> ALT= Model(
               optimizer_with_attributes(
                   Juniper.Optimizer, 
                   "nl_solver" =>  optimizer_with_attributes(Ipopt.Optimizer, "print_level" => 0),
                   "mip_solver" => optimizer_with_attributes(Gurobi.Optimizer, "logLevel" => 0),
                   "registered_functions" => [Juniper.register( :f, 1, f; autodiff = true)],
               ),
           )
    A JuMP Model
    Feasibility problem with:
    Variables: 0
    Model mode: AUTOMATIC
    CachingOptimizer state: EMPTY_OPTIMIZER
    Solver name: Juniper
    
    julia> JuMP.register(ALT, :f, 1, f; autodiff = true);
    
    julia> @variable(ALT, h >= 0.001);
    
    julia> @variable(ALT, L >= 0.000001);
    
    julia> @variable(ALT, n>=2, Int);
    
    julia> @variable(ALT, y);
    
    julia> @NLexpression(ALT,k1,h/(1-f(L-sig*sqrt(n))+f(-L - sig*sqrt(n)))); # HARL1
    
    julia> @NLexpression(ALT,k2,(1-(1+landa*h)*exp(-landa*h))/(landa*(1-exp(-landa*h)))); #to
    
    julia> @NLexpression(ALT,k3,E*n+T1*gam1+T2*gam2);
    
    julia> f1=@NLexpression(ALT,F,c0/landa+C1*(k1-k2+k3));
    
    julia> @NLconstraint(ALT, f(-L) <= 1/400)
    f(-L) - 1.0 / 400.0 ≤ 0
    
    julia> @NLconstraint(ALT, L <= 4)
    L - 4.0 ≤ 0
    
    julia> @NLconstraint(ALT, n <= 30)
    n - 30.0 ≤ 0
    
    julia> @NLconstraint(ALT, h <= 40)
    h - 40.0 ≤ 0
    
    julia> @NLconstraint(ALT, f1 <=y)
    subexpression[4] - y ≤ 0
    
    julia> @NLobjective(ALT,Min,y)
    
    julia> optimize!(ALT)
    nl_solver             : MathOptInterface.OptimizerWithAttributes(Ipopt.Optimizer, Pair{MathOptInterface.AbstractOptimizerAttribute, Any}[MathOptInterface.RawOptimizerAttribute("print_level") => 0])
    mip_solver            : MathOptInterface.OptimizerWithAttributes(Gurobi.Optimizer, Pair{MathOptInterface.AbstractOptimizerAttribute, Any}[MathOptInterface.RawOptimizerAttribute("logLevel") => 0])
    log_levels            : [:Options, :Table, :Info]
    registered_functions  : Juniper.RegisteredFunction[Juniper.RegisteredFunction(:f, 1, f, nothing, nothing, true)]
    
    #Variables: 4
    #IntBinVar: 1
    Obj Sense: Min
    
    Incumbent using start values: 2230.439925920267
    Status of relaxation: LOCALLY_SOLVED
    Time for relaxation: 0.13505101203918457
    Relaxation Obj: 2230.439925920267
    Obj: 2230.439925920267
    
    (juniper) pkg> st
          Status `/private/tmp/juniper/Project.toml`
      [31c24e10] Distributions v0.25.31
      [2e9cd046] Gurobi v0.10.0
      [b6b21f68] Ipopt v0.8.0
      [4076af6c] JuMP v0.22.0
      [2ddba703] Juniper v0.8.0
    

    Note that for performance reasons you don't want to add variable bounds as nonlinear constraints:

    @NLconstraint(ALT, L <= 4);
    @NLconstraint(ALT, n <= 30);
    @NLconstraint(ALT, h <= 40);
    

    Use instead:

    @variable(ALT, 0.001 <= h <= 40)
    @variable(ALT, 0.000001 <= L <= 4)
    @variable(ALT, 2 <= n <= 30, Int)