Skip to content

Commit

Permalink
Apply JuliaFormatter (#31)
Browse files Browse the repository at this point in the history
* Apply JuliaFormatter

* Update Project.toml
  • Loading branch information
odow authored Apr 12, 2021
1 parent 030b561 commit 191eaab
Show file tree
Hide file tree
Showing 96 changed files with 2,267 additions and 1,715 deletions.
8 changes: 8 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Configuration file for JuliaFormatter.jl
# For more information, see: https://domluna.github.io/JuliaFormatter.jl/stable/config/

always_for_in = true
always_use_return = true
margin = 80
remove_extra_newlines = true
short_to_long_function_def = true
31 changes: 31 additions & 0 deletions .github/workflows/format_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: format-check
on:
push:
branches:
- master
- release-*
pull_request:
types: [opened, synchronize, reopened]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: julia-actions/setup-julia@latest
with:
version: '1'
- uses: actions/checkout@v1
- name: Format check
shell: julia --color=yes {0}
run: |
using Pkg
Pkg.add(PackageSpec(name="JuliaFormatter", version="0.13.9"))
using JuliaFormatter
format("src", verbose=true)
format("test", verbose=true)
out = String(read(Cmd(`git diff`)))
if isempty(out)
exit(0)
end
@error "Some files have not been formatted !!!"
write(stdout, out)
exit(1)
131 changes: 89 additions & 42 deletions src/MINLPTests.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
module MINLPTests

using JuMP, Test
using JuMP
using Test

###
### Default tolerances that are used in the tests.
###

# Absolute tolerance when checking the objective value.
const OPT_TOL = 1e-6
const OPT_TOL = 1e-6

# Absolute tolerance when checking the primal solution value.
const PRIMAL_TOL = 1e-6

# Absolue tolerance when checking the dual solution value.
const DUAL_TOL = 1e-6
const DUAL_TOL = 1e-6

###
### Default expected status codes for different types of problems and solvers.
Expand Down Expand Up @@ -46,9 +47,12 @@ const PRIMAL_TARGET_GLOBAL = Dict(
### Helper functions for the tests.
###

function check_status(model, problem_type::ProblemTypeCode,
termination_target=TERMINATION_TARGET_LOCAL,
primal_target=PRIMAL_TARGET_LOCAL)
function check_status(
model,
problem_type::ProblemTypeCode,
termination_target = TERMINATION_TARGET_LOCAL,
primal_target = PRIMAL_TARGET_LOCAL,
)
@test JuMP.termination_status(model) == termination_target[problem_type]
@test JuMP.primal_status(model) == primal_target[problem_type]
end
Expand Down Expand Up @@ -82,7 +86,8 @@ end
###

for directory in ["nlp", "nlp-cvx", "nlp-mi"]
for file_name in filter(f -> endswith(f, ".jl"), readdir(joinpath(@__DIR__, directory)))
files = readdir(joinpath(@__DIR__, directory))
for file_name in filter(f -> endswith(f, ".jl"), files)
include(joinpath(@__DIR__, directory, file_name))
end
end
Expand All @@ -99,22 +104,37 @@ end
test_directory("nlp", optimizer; include = ["001_010"])
"""
function test_directory(
directory, optimizer; exclude=String[], include=String[],
objective_tol = OPT_TOL, primal_tol = PRIMAL_TOL, dual_tol = DUAL_TOL,
termination_target = TERMINATION_TARGET_LOCAL,
primal_target = PRIMAL_TARGET_LOCAL)

directory,
optimizer;
exclude = String[],
include = String[],
objective_tol = OPT_TOL,
primal_tol = PRIMAL_TOL,
dual_tol = DUAL_TOL,
termination_target = TERMINATION_TARGET_LOCAL,
primal_target = PRIMAL_TARGET_LOCAL,
)
@testset "$(directory)" begin
@testset "$(model_name)" for model_name in list_of_models(directory, exclude, include)
function_name = string(replace(directory, "-" => "_"), "_", model_name)
model_function = getfield(MINLPTests, Symbol(function_name))
model_function(optimizer, objective_tol, primal_tol, dual_tol,
termination_target, primal_target)
models = _list_of_models(directory, exclude, include)
dir = replace(directory, "-" => "_")
@testset "$(model_name)" for model_name in models
getfield(MINLPTests, Symbol("$(dir)_$(model_name)"))(
optimizer,
objective_tol,
primal_tol,
dual_tol,
termination_target,
primal_target,
)
end
end
end

function list_of_models(directory, exclude::Vector{String}, include::Vector{String})
function _list_of_models(
directory,
exclude::Vector{String},
include::Vector{String},
)
if length(include) > 0
return include
else
Expand All @@ -134,39 +154,66 @@ end
###

function test_nlp(
optimizer; exclude = String[], objective_tol = OPT_TOL,
primal_tol = PRIMAL_TOL, dual_tol = DUAL_TOL,
termination_target = TERMINATION_TARGET_LOCAL,
primal_target = PRIMAL_TARGET_LOCAL)
test_directory("nlp", optimizer;
exclude = exclude, objective_tol = objective_tol,
primal_tol = primal_tol, dual_tol = dual_tol,
optimizer;
exclude = String[],
objective_tol = OPT_TOL,
primal_tol = PRIMAL_TOL,
dual_tol = DUAL_TOL,
termination_target = TERMINATION_TARGET_LOCAL,
primal_target = PRIMAL_TARGET_LOCAL,
)
return test_directory(
"nlp",
optimizer;
exclude = exclude,
objective_tol = objective_tol,
primal_tol = primal_tol,
dual_tol = dual_tol,
termination_target = termination_target,
primal_target = primal_target)
primal_target = primal_target,
)
end

function test_nlp_cvx(
optimizer; exclude = String[], objective_tol = OPT_TOL,
primal_tol = PRIMAL_TOL, dual_tol = DUAL_TOL,
termination_target = TERMINATION_TARGET_LOCAL,
primal_target = PRIMAL_TARGET_LOCAL)
test_directory("nlp-cvx", optimizer;
exclude = exclude, objective_tol = objective_tol,
primal_tol = primal_tol, dual_tol = dual_tol,
optimizer;
exclude = String[],
objective_tol = OPT_TOL,
primal_tol = PRIMAL_TOL,
dual_tol = DUAL_TOL,
termination_target = TERMINATION_TARGET_LOCAL,
primal_target = PRIMAL_TARGET_LOCAL,
)
return test_directory(
"nlp-cvx",
optimizer;
exclude = exclude,
objective_tol = objective_tol,
primal_tol = primal_tol,
dual_tol = dual_tol,
termination_target = termination_target,
primal_target = primal_target)
primal_target = primal_target,
)
end

function test_nlp_mi(
optimizer; exclude = String[], objective_tol = OPT_TOL,
primal_tol = PRIMAL_TOL, dual_tol = DUAL_TOL,
termination_target = TERMINATION_TARGET_LOCAL,
primal_target = PRIMAL_TARGET_LOCAL)
test_directory("nlp-mi", optimizer;
exclude = exclude, objective_tol = objective_tol,
primal_tol = primal_tol, dual_tol = dual_tol,
optimizer;
exclude = String[],
objective_tol = OPT_TOL,
primal_tol = PRIMAL_TOL,
dual_tol = DUAL_TOL,
termination_target = TERMINATION_TARGET_LOCAL,
primal_target = PRIMAL_TARGET_LOCAL,
)
return test_directory(
"nlp-mi",
optimizer;
exclude = exclude,
objective_tol = objective_tol,
primal_tol = primal_tol,
dual_tol = dual_tol,
termination_target = termination_target,
primal_target = primal_target)
primal_target = primal_target,
)
end

### Tests that haven't been updated.
Expand Down
55 changes: 31 additions & 24 deletions src/nlp-cvx/001_010.jl
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
function nlp_cvx_001_010(optimizer, objective_tol, primal_tol, dual_tol,
termination_target = TERMINATION_TARGET_LOCAL,
primal_target = PRIMAL_TARGET_LOCAL)
function nlp_cvx_001_010(
optimizer,
objective_tol,
primal_tol,
dual_tol,
termination_target = TERMINATION_TARGET_LOCAL,
primal_target = PRIMAL_TARGET_LOCAL,
)
# Test Goals:
# - linear objective
# - quadratic objective
# - linear constraints forming a closed set
# Variants
# 010 - binding constraints
# 011 - non-binding constraints

m = Model(optimizer)

@variable(m, x)
@variable(m, y)

@objective(m, Min, x)
@constraint(m, x+y <= 5)
@constraint(m, 2*x-y <= 3)
@constraint(m, 3*x+9*y >= -10)
@constraint(m, 10*x-y >= -20)
@constraint(m, -x+2*y <= 8)

optimize!(m)

check_status(m, FEASIBLE_PROBLEM, termination_target, primal_target)
check_objective(m, -2.0430107680954848, tol = objective_tol)
check_solution([x,y], [-2.0430107680954848, -0.4301075068564087], tol = primal_tol)

end
# 011 - non-binding constraints

model = Model(optimizer)

@variable(model, x)
@variable(model, y)

@objective(model, Min, x)
@constraint(model, x + y <= 5)
@constraint(model, 2 * x - y <= 3)
@constraint(model, 3 * x + 9 * y >= -10)
@constraint(model, 10 * x - y >= -20)
@constraint(model, -x + 2 * y <= 8)

optimize!(model)

check_status(model, FEASIBLE_PROBLEM, termination_target, primal_target)
check_objective(model, -2.0430107680954848, tol = objective_tol)
return check_solution(
[x, y],
[-2.0430107680954848, -0.4301075068564087],
tol = primal_tol,
)
end
47 changes: 25 additions & 22 deletions src/nlp-cvx/001_011.jl
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
function nlp_cvx_001_011(optimizer, objective_tol, primal_tol, dual_tol,
termination_target = TERMINATION_TARGET_LOCAL,
primal_target = PRIMAL_TARGET_LOCAL)
m = Model(optimizer)

@variable(m, x)
@variable(m, y)

@objective(m, Min, (x-1)^2 + (y-2)^2)
@constraint(m, x+y <= 5)
@constraint(m, 2*x-y <= 3)
@constraint(m, 3*x+9*y >= -10)
@constraint(m, 10*x-y >= -20)
@constraint(m, -x+2*y <= 8)

optimize!(m)

check_status(m, FEASIBLE_PROBLEM, termination_target, primal_target)
check_objective(m, 0.0, tol = objective_tol)
check_solution([x,y], [1.0, 2.0], tol = primal_tol)

end
function nlp_cvx_001_011(
optimizer,
objective_tol,
primal_tol,
dual_tol,
termination_target = TERMINATION_TARGET_LOCAL,
primal_target = PRIMAL_TARGET_LOCAL,
)
model = Model(optimizer)

@variable(model, x)
@variable(model, y)

@objective(model, Min, (x - 1)^2 + (y - 2)^2)
@constraint(model, x + y <= 5)
@constraint(model, 2 * x - y <= 3)
@constraint(model, 3 * x + 9 * y >= -10)
@constraint(model, 10 * x - y >= -20)
@constraint(model, -x + 2 * y <= 8)

optimize!(model)

check_status(model, FEASIBLE_PROBLEM, termination_target, primal_target)
check_objective(model, 0.0, tol = objective_tol)
return check_solution([x, y], [1.0, 2.0], tol = primal_tol)
end
Loading

0 comments on commit 191eaab

Please sign in to comment.