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
62const MOI = MathOptInterface
73const MOIU = MOI. Utilities
84
6359 Optimizer(Ipopt.amplexe, ["print_level=0"])
6460"""
6561function 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)
136132end
137133replace_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.
142137function MPB. initialize (d:: NLPEvaluator , requested_features:: Vector{Symbol} )
@@ -184,7 +179,6 @@ function MPB.constr_expr(d::NLPEvaluator, i)
184179 end
185180end
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)
252246end
253247
254- # ==============================================================================
255248# Now we're ready to optimize model.
256249function 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
410389end
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+
415399function 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]
425405end
@@ -429,23 +409,15 @@ function MOI.get(model::Model, ::MOI.ConstraintPrimal, idx::MOI.ConstraintIndex)
429409end
430410
431411function 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
441417end
442418
443419function 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)
467439end
468440
469441function 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
483451end
484452
485- function MOI. get (model :: Model , :: MOI.DualStatus )
453+ function MOI. get (:: Model , :: MOI.DualStatus )
486454 return MOI. NO_SOLUTION
487455end
488456
0 commit comments