Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Aug 21, 2024
1 parent f6670b6 commit bb7be38
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions test/MOI_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,90 @@ function test_runtests()
return
end

function test_list_of_model_attributes_set()
attr = MOI.ListOfModelAttributesSet()
model = NLopt.Optimizer()
ret = MOI.AbstractModelAttribute[]
@test MOI.get(model, attr) == ret
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
push!(ret, MOI.ObjectiveSense())
@test MOI.get(model, attr) == ret
x = MOI.add_variable(model)
MOI.set(model, MOI.ObjectiveFunction{MOI.VariableIndex}(), x)
push!(ret, MOI.ObjectiveFunction{MOI.VariableIndex}())
@test MOI.get(model, attr) == ret
return
end

function test_list_and_number_of_constraints()
model = NLopt.Optimizer()
x = MOI.add_variable(model)
F1, S1 = MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}
F2, S2 = MOI.ScalarQuadraticFunction{Float64}, MOI.LessThan{Float64}
@test MOI.get(model, MOI.NumberOfConstraints{F1,S1}()) == 0
@test MOI.get(model, MOI.NumberOfConstraints{F2,S2}()) == 0
@test MOI.get(model, MOI.ListOfConstraintIndices{F1,S1}()) == []
@test MOI.get(model, MOI.ListOfConstraintIndices{F2,S2}()) == []
c1 = MOI.add_constraint(model, 1.0 * x, MOI.EqualTo(2.0))
@test MOI.get(model, MOI.NumberOfConstraints{F1,S1}()) == 1
@test MOI.get(model, MOI.NumberOfConstraints{F2,S2}()) == 0
@test MOI.get(model, MOI.ListOfConstraintIndices{F1,S1}()) == [c1]
@test MOI.get(model, MOI.ListOfConstraintIndices{F2,S2}()) == []
c2 = MOI.add_constraint(model, 1.0 * x * x, MOI.LessThan(2.0))
@test MOI.get(model, MOI.NumberOfConstraints{F1,S1}()) == 1
@test MOI.get(model, MOI.NumberOfConstraints{F2,S2}()) == 1
@test MOI.get(model, MOI.ListOfConstraintIndices{F1,S1}()) == [c1]
@test MOI.get(model, MOI.ListOfConstraintIndices{F2,S2}()) == [c2]
@test MOI.get(model, MOI.ConstraintSet(), c1) == MOI.EqualTo(2.0)
@test MOI.get(model, MOI.ConstraintSet(), c2) == MOI.LessThan(2.0)
return
end

function test_raw_optimizer_attribute()
model = NLopt.Optimizer()
attr = MOI.RawOptimizerAttribute("algorithm")
@test MOI.supports(model, attr)
@test MOI.get(model, attr) == :none
MOI.set(model, attr, :LD_MMA)
@test MOI.get(model, attr) == :LD_MMA
bad_attr = MOI.RawOptimizerAttribute("foobar")
@test !MOI.supports(model, bad_attr)
@test_throws MOI.GetAttributeNotAllowed MOI.get(model, bad_attr)
return
end

function test_list_of_variable_attributes_set()
model = NLopt.Optimizer()
@test MOI.get(model, MOI.ListOfVariableAttributesSet()) ==
MOI.AbstractVariableAttribute[]
x = MOI.add_variables(model, 2)
MOI.supports(model, MOI.VariablePrimalStart(), MOI.VariableIndex)
MOI.set(model, MOI.VariablePrimalStart(), x[2], 1.0)
@test MOI.get(model, MOI.ListOfVariableAttributesSet()) ==
MOI.AbstractVariableAttribute[MOI.VariablePrimalStart()]
@test MOI.get(model, MOI.VariablePrimalStart(), x[1]) === nothing
@test MOI.get(model, MOI.VariablePrimalStart(), x[2]) === 1.0
return
end

function test_list_of_variable_attributes_set()
model = NLopt.Optimizer()
F, S = MOI.ScalarAffineFunction{Float64}, MOI.EqualTo{Float64}
@test MOI.get(model, MOI.ListOfConstrainAttributesSet{F,S}()) ==
MOI.AbstractConstraintAttribute[]
return
end

function test_get_objective_function()
model = NLopt.Optimizer()
x = MOI.add_variable(model)
MOI.set(model, MOI.ObjectiveFunction{MOI.VariableIndex}(), x)
@test MOI.get(model, MOI.ObjectiveFunction{MOI.VariableIndex}()) == x
F = MOI.ScalarAffineFunction{Float64}
@test MOI.get(model, MOI.ObjectiveFunction{F}()) == 1.0 * x
return
end

end # module

TestMOIWrapper.runtests()

0 comments on commit bb7be38

Please sign in to comment.