Skip to content

Commit 0aaad31

Browse files
authored
Updates for MOI 0.8.2 (#84)
* Updates for MOI 0.8.2 * Bump versions of test/MINLPTests
1 parent b03be7f commit 0aaad31

File tree

4 files changed

+28
-59
lines changed

4 files changed

+28
-59
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
julia 0.6
22
Compat 1.0
33
MathProgBase 0.5 0.8
4-
MathOptInterface 0.8.1 0.9
4+
MathOptInterface 0.8.2 0.9

src/MOI_wrapper.jl

Lines changed: 21 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
# TODO(odow):
2-
# - remove the `try-catch`es once the following PR is merged
3-
# https://github.com/JuliaOpt/MathOptInterface.jl/pull/623
4-
5-
using MathOptInterface
1+
import MathOptInterface
62
const MOI = MathOptInterface
73
const MOIU = MOI.Utilities
84

@@ -63,9 +59,9 @@ end
6359
Optimizer(Ipopt.amplexe, ["print_level=0"])
6460
"""
6561
function Optimizer(
66-
solver_command::String,
67-
options::Vector{String} = String[];
68-
filename::String = "")
62+
solver_command::String,
63+
options::Vector{String} = String[];
64+
filename::String = "")
6965
model = MOIU.UniversalFallback(InnerModel{Float64}())
7066
MOI.set(model, MPBSolver(),
7167
AmplNLSolver(solver_command, options, filename = filename)
@@ -136,7 +132,6 @@ function replace_variableindex_by_int(variable_map, expr::MOI.VariableIndex)
136132
end
137133
replace_variableindex_by_int(variable_map, expr) = expr
138134

139-
# ==============================================================================
140135
# In the next section we match up the MPB nonlinear functions to the MOI API.
141136
# This is basically just a rename.
142137
function MPB.initialize(d::NLPEvaluator, requested_features::Vector{Symbol})
@@ -184,7 +179,6 @@ function MPB.constr_expr(d::NLPEvaluator, i)
184179
end
185180
end
186181

187-
# ==============================================================================
188182
# In the next section, we need to convert MOI functions and sets into expression
189183
# graphs. First, we convert functions (SingleVariable, ScalarAffine, and
190184
# ScalarQuadratic) into expression graphs.
@@ -251,7 +245,6 @@ function moi_to_expr_graph(func, set, variable_map)
251245
return funcset_to_expr_graph(func_expr, set)
252246
end
253247

254-
# ==============================================================================
255248
# Now we're ready to optimize model.
256249
function MOI.optimize!(model::Model)
257250
# Extract the MathProgBase solver from the model. Recall it's a MOI
@@ -269,7 +262,6 @@ function MOI.optimize!(model::Model)
269262
nothing
270263
end
271264

272-
# ==========================================================================
273265
# Extract the nonlinear constraint bounds. We're going to append to these
274266
# g_l and g_u vectors later.
275267
num_con = 0
@@ -286,7 +278,6 @@ function MOI.optimize!(model::Model)
286278
end
287279
end
288280

289-
# ==========================================================================
290281
# Intialize the variables. We need to form a mapping between the MOI
291282
# VariableIndex and an Int in order to replace instances of
292283
# `x[VariableIndex]` with `x[i]` in the expression graphs.
@@ -297,7 +288,6 @@ function MOI.optimize!(model::Model)
297288
variable_map[variable] = i
298289
end
299290

300-
# ==========================================================================
301291
# Extract variable bounds.
302292
x_l = fill(-Inf, num_var)
303293
x_u = fill(Inf, num_var)
@@ -320,7 +310,6 @@ function MOI.optimize!(model::Model)
320310
end
321311
end
322312

323-
# ==========================================================================
324313
# We have to convert all ScalarAffineFunction-in-Set constraints to an
325314
# expression graph.
326315
scalar_constraint_expr = Expr[]
@@ -339,7 +328,6 @@ function MOI.optimize!(model::Model)
339328
end
340329
end
341330

342-
# ==========================================================================
343331
# MOI objective
344332
obj_type = MOI.get(model, MOI.ObjectiveFunctionType())
345333
obj_func = MOI.get(model, MOI.ObjectiveFunction{obj_type}())
@@ -348,20 +336,17 @@ function MOI.optimize!(model::Model)
348336
obj_func_expr = nothing
349337
end
350338

351-
# ==========================================================================
352339
# Build the nlp_evaluator
353340
nlp_evaluator = NLPEvaluator(moi_nlp_evaluator, variable_map, num_con,
354341
obj_func_expr, scalar_constraint_expr)
355342

356-
# ==========================================================================
357343
# Create the MathProgBase model. Note that we pass `num_con` and the number
358344
# of linear constraints.
359345
mpb_model = MPB.NonlinearModel(mpb_solver)
360346
MPB.loadproblem!(mpb_model, num_var,
361347
num_con + length(scalar_constraint_expr), x_l, x_u, g_l, g_u, sense,
362348
nlp_evaluator)
363349

364-
# ==========================================================================
365350
# Set any variables to :Bin if they are in ZeroOne and :Int if they are
366351
# Integer. The default is just :Cont.
367352
x_cat = fill(:Cont, num_var)
@@ -376,25 +361,19 @@ function MOI.optimize!(model::Model)
376361
end
377362
MPB.setvartype!(mpb_model, x_cat)
378363

379-
# ==========================================================================
380364
# Set the VariablePrimalStart attributes for variables.
381365
variable_primal_start = fill(0.0, num_var)
382366
for (i, variable) in enumerate(variables)
383-
try
384-
start_val = MOI.get(model, MOI.VariablePrimalStart(), variable)
385-
if start_val !== nothing
386-
variable_primal_start[i] = start_val
387-
end
388-
catch
367+
start_val = MOI.get(model, MOI.VariablePrimalStart(), variable)
368+
if start_val !== nothing
369+
variable_primal_start[i] = start_val
389370
end
390371
end
391372
MPB.setwarmstart!(mpb_model, variable_primal_start)
392373

393-
# ==========================================================================
394-
# Set the VariablePrimalStart attributes for variables.
374+
# Solve the model!
395375
MPB.optimize!(mpb_model)
396376

397-
# ==========================================================================
398377
# Extract and save the MathProgBase solution.
399378
primal_solution = Dict{MOI.VariableIndex, Float64}()
400379
for (variable, sol) in zip(variables, MPB.getsolution(mpb_model))
@@ -409,17 +388,18 @@ function MOI.optimize!(model::Model)
409388
return
410389
end
411390

412-
# ==============================================================================
413391
# MOI accessors for the solution info.
414392

393+
function mpb_solution_attribute(model::Model)
394+
# We'd like to call MOI.get(model, MPBSolutionAttribute()), but it throws an
395+
# error if not set rather than return `nothing`.
396+
return get(model.modattr, MPBSolutionAttribute(), nothing)
397+
end
398+
415399
function MOI.get(model::Model, ::MOI.VariablePrimal, var::MOI.VariableIndex)
416-
mpb_solution = try
417-
MOI.get(model, MPBSolutionAttribute())
418-
catch
419-
nothing
420-
end
400+
mpb_solution = mpb_solution_attribute(model)
421401
if mpb_solution === nothing
422-
return nothing
402+
return
423403
end
424404
return mpb_solution.primal_solution[var]
425405
end
@@ -429,23 +409,15 @@ function MOI.get(model::Model, ::MOI.ConstraintPrimal, idx::MOI.ConstraintIndex)
429409
end
430410

431411
function MOI.get(model::Model, ::MOI.ObjectiveValue)
432-
mpb_solution = try
433-
MOI.get(model, MPBSolutionAttribute())
434-
catch
435-
nothing
436-
end
412+
mpb_solution = mpb_solution_attribute(model)
437413
if mpb_solution === nothing
438-
return nothing
414+
return
439415
end
440416
return mpb_solution.objective_value
441417
end
442418

443419
function MOI.get(model::Model, ::MOI.TerminationStatus)
444-
mpb_solution = try
445-
MOI.get(model, MPBSolutionAttribute())
446-
catch
447-
nothing
448-
end
420+
mpb_solution = mpb_solution_attribute(model)
449421
if mpb_solution === nothing
450422
return MOI.OPTIMIZE_NOT_CALLED
451423
end
@@ -467,11 +439,7 @@ function MOI.get(model::Model, ::MOI.TerminationStatus)
467439
end
468440

469441
function MOI.get(model::Model, ::MOI.PrimalStatus)
470-
mpb_solution = try
471-
MOI.get(model, MPBSolutionAttribute())
472-
catch
473-
nothing
474-
end
442+
mpb_solution = mpb_solution_attribute(model)
475443
if mpb_solution === nothing
476444
return MOI.NO_SOLUTION
477445
end
@@ -482,7 +450,7 @@ function MOI.get(model::Model, ::MOI.PrimalStatus)
482450
return MOI.NO_SOLUTION
483451
end
484452

485-
function MOI.get(model::Model, ::MOI.DualStatus)
453+
function MOI.get(::Model, ::MOI.DualStatus)
486454
return MOI.NO_SOLUTION
487455
end
488456

test/MINLPTests/Manifest.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"
4747

4848
[[DiffResults]]
4949
deps = ["Compat", "StaticArrays"]
50-
git-tree-sha1 = "db8acf46717b13d6c48deb7a12007c7f85a70cf7"
50+
git-tree-sha1 = "34a4a1e8be7bc99bc9c611b895b5baf37a80584c"
5151
uuid = "163ba53b-c6d8-5494-b064-1a9d43ac40c5"
52-
version = "0.0.3"
52+
version = "0.0.4"
5353

5454
[[DiffRules]]
5555
deps = ["Random", "Test"]
@@ -79,7 +79,7 @@ version = "0.5.3"
7979

8080
[[JuMP]]
8181
deps = ["Calculus", "DataStructures", "ForwardDiff", "LinearAlgebra", "MathOptInterface", "NaNMath", "Random", "SparseArrays", "Statistics", "Test"]
82-
git-tree-sha1 = "fb4c9d5a3cf974859c2090eb3b71d06d948307af"
82+
git-tree-sha1 = "205bce386c5647adfac47f9b60de715748aabd20"
8383
repo-rev = "master"
8484
repo-url = "https://github.com/JuliaOpt/JuMP.jl.git"
8585
uuid = "4076af6c-e467-56ae-b986-b466b2749572"
@@ -112,9 +112,9 @@ uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"
112112

113113
[[MathOptInterface]]
114114
deps = ["Compat", "Unicode"]
115-
git-tree-sha1 = "6f3df4b5346485b299ae5d2b313ecb428bb46bb9"
115+
git-tree-sha1 = "e3c7fb842078f5cd9a21ecb911dc59a4282e3d01"
116116
uuid = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
117-
version = "0.8.1"
117+
version = "0.8.2"
118118

119119
[[MathProgBase]]
120120
deps = ["Compat"]

test/MOI_wrapper.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const CONFIG = MOIT.TestConfig(
1818
"solve_objbound_edge_cases", # ObjectiveBound not implemented
1919
"solve_integer_edge_cases", # Ipopt doesn't handle integer
2020
"solve_affine_deletion_edge_cases", # VectorAffineFunction
21+
"solve_duplicate_terms_vector_affine", # VectorAffineFunction
2122
# It seems that the AMPL NL reader declares NL files with no objective
2223
# and no constraints as corrupt, even if they have variable bounds. Yuk.
2324
"solve_blank_obj"

0 commit comments

Comments
 (0)