Skip to content

Commit ffff817

Browse files
authored
Add format action (#281)
* Add format action * Final newline * Fix tests * Fix
1 parent 116410f commit ffff817

File tree

109 files changed

+4430
-1808
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+4430
-1808
lines changed

.JuliaFormatter.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Configuration file for JuliaFormatter.jl
2+
# For more information, see: https://domluna.github.io/JuliaFormatter.jl/stable/config/
3+
4+
always_for_in = true
5+
always_use_return = true
6+
margin = 80
7+
remove_extra_newlines = true
8+
short_to_long_function_def = true

.github/workflows/format_check.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
name: format-check
3+
on:
4+
push:
5+
branches:
6+
- master
7+
- release-*
8+
pull_request:
9+
types: [opened, synchronize, reopened]
10+
jobs:
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: julia-actions/setup-julia@latest
15+
with:
16+
version: '1'
17+
- uses: actions/checkout@v1
18+
- name: Format check
19+
shell: julia --color=yes {0}
20+
run: |
21+
using Pkg
22+
Pkg.add(PackageSpec(name="JuliaFormatter", version="1"))
23+
using JuliaFormatter
24+
format("src", verbose=true)
25+
format("test", verbose=true)
26+
out = String(read(Cmd(`git diff`)))
27+
if isempty(out)
28+
exit(0)
29+
end
30+
@error "Some files have not been formatted !!!"
31+
write(stdout, out)
32+
exit(1)

src/Bridges/Constraint/Constraint.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ include("utilities.jl")
2525
include("sos_polynomial.jl")
2626
include("sos_polynomial_in_semialgebraic_set.jl")
2727

28-
2928
# TODO bridges should redirect to `MOI.get_fallback` as well so that
3029
# we can just use `Union{MOI.ConstraintIndex,MOI.Bridges.AbstractBridge}` in the `get_fallback` in `attributes.jl`
3130
function MOI.get(

src/Bridges/Constraint/diagonally_dominant.jl

Lines changed: 101 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
struct DiagonallyDominantBridge{T, F, G} <: MOI.Bridges.Constraint.AbstractBridge
1+
struct DiagonallyDominantBridge{T,F,G} <: MOI.Bridges.Constraint.AbstractBridge
22
# |Qij| variables
33
abs_vars::Vector{MOI.VariableIndex}
44
# |Qij| ≥ +Qij
5-
abs_plus::Vector{MOI.ConstraintIndex{MOI.ScalarAffineFunction{T},
6-
MOI.GreaterThan{T}}}
5+
abs_plus::Vector{
6+
MOI.ConstraintIndex{MOI.ScalarAffineFunction{T},MOI.GreaterThan{T}},
7+
}
78
# |Qij| ≥ -Qij
8-
abs_minus::Vector{MOI.ConstraintIndex{MOI.ScalarAffineFunction{T},
9-
MOI.GreaterThan{T}}}
9+
abs_minus::Vector{
10+
MOI.ConstraintIndex{MOI.ScalarAffineFunction{T},MOI.GreaterThan{T}},
11+
}
1012
# inequalities Qjj ≥ sum_{i ≠ j} |Qij|
11-
dominance::Vector{MOI.ConstraintIndex{F, MOI.GreaterThan{T}}}
13+
dominance::Vector{MOI.ConstraintIndex{F,MOI.GreaterThan{T}}}
1214
end
1315

1416
function MOI.Bridges.Constraint.bridge_constraint(
15-
::Type{DiagonallyDominantBridge{T, F, G}},
16-
model::MOI.ModelLike, f::MOI.AbstractVectorFunction,
17-
s::SOS.DiagonallyDominantConeTriangle) where {T, F, G}
18-
17+
::Type{DiagonallyDominantBridge{T,F,G}},
18+
model::MOI.ModelLike,
19+
f::MOI.AbstractVectorFunction,
20+
s::SOS.DiagonallyDominantConeTriangle,
21+
) where {T,F,G}
1922
@assert MOI.output_dimension(f) == MOI.dimension(s)
2023
n = s.side_dimension
2124
g = F[zero(F) for i in 1:n]
2225
fs = MOI.Utilities.eachscalar(f)
2326
num_off_diag = MOI.dimension(s) - n
24-
CI = MOI.ConstraintIndex{MOI.ScalarAffineFunction{T}, MOI.GreaterThan{T}}
27+
CI = MOI.ConstraintIndex{MOI.ScalarAffineFunction{T},MOI.GreaterThan{T}}
2528
abs_vars = Vector{MOI.VariableIndex}(undef, num_off_diag)
2629
abs_plus = Vector{CI}(undef, num_off_diag)
2730
abs_minus = Vector{CI}(undef, num_off_diag)
@@ -38,27 +41,43 @@ function MOI.Bridges.Constraint.bridge_constraint(
3841
MOI.Utilities.operate!(-, T, g[j], fabs)
3942
MOI.Utilities.operate!(-, T, g[i], fabs)
4043
abs_plus[koff] = MOI.add_constraint(
41-
model, MOI.Utilities.operate(+, T, fabs, fs[k]), MOI.GreaterThan(0.0))
44+
model,
45+
MOI.Utilities.operate(+, T, fabs, fs[k]),
46+
MOI.GreaterThan(0.0),
47+
)
4248
abs_minus[koff] = MOI.add_constraint(
43-
model, MOI.Utilities.operate(-, T, fabs, fs[k]), MOI.GreaterThan(0.0))
49+
model,
50+
MOI.Utilities.operate(-, T, fabs, fs[k]),
51+
MOI.GreaterThan(0.0),
52+
)
4453
end
4554
k += 1
4655
MOI.Utilities.operate!(+, T, g[j], fs[k])
4756
end
4857
dominance = map(f -> MOI.add_constraint(model, f, MOI.GreaterThan(0.0)), g)
49-
return DiagonallyDominantBridge{T, F, G}(abs_vars, abs_plus, abs_minus,
50-
dominance)
58+
return DiagonallyDominantBridge{T,F,G}(
59+
abs_vars,
60+
abs_plus,
61+
abs_minus,
62+
dominance,
63+
)
5164
end
5265

53-
function MOI.supports_constraint(::Type{<:DiagonallyDominantBridge},
54-
::Type{<:MOI.AbstractVectorFunction},
55-
::Type{<:SOS.DiagonallyDominantConeTriangle})
66+
function MOI.supports_constraint(
67+
::Type{<:DiagonallyDominantBridge},
68+
::Type{<:MOI.AbstractVectorFunction},
69+
::Type{<:SOS.DiagonallyDominantConeTriangle},
70+
)
5671
return true
5772
end
58-
function MOI.Bridges.added_constrained_variable_types(::Type{<:DiagonallyDominantBridge})
73+
function MOI.Bridges.added_constrained_variable_types(
74+
::Type{<:DiagonallyDominantBridge},
75+
)
5976
return Tuple{DataType}[]
6077
end
61-
function MOI.Bridges.added_constraint_types(::Type{<:DiagonallyDominantBridge{T, F}}) where {T, F}
78+
function MOI.Bridges.added_constraint_types(
79+
::Type{<:DiagonallyDominantBridge{T,F}},
80+
) where {T,F}
6281
added = [(F, MOI.GreaterThan{T})]
6382
if F != MOI.ScalarAffineFunction{T}
6483
push!(added, (MOI.ScalarAffineFunction{T}, MOI.GreaterThan{T}))
@@ -68,11 +87,11 @@ end
6887
function MOI.Bridges.Constraint.concrete_bridge_type(
6988
::Type{<:DiagonallyDominantBridge{T}},
7089
G::Type{<:MOI.AbstractVectorFunction},
71-
::Type{SOS.DiagonallyDominantConeTriangle}) where T
72-
90+
::Type{SOS.DiagonallyDominantConeTriangle},
91+
) where {T}
7392
S = MOI.Utilities.scalar_type(G)
7493
F = MOI.Utilities.promote_operation(-, T, S, MOI.VariableIndex)
75-
return DiagonallyDominantBridge{T, F, G}
94+
return DiagonallyDominantBridge{T,F,G}
7695
end
7796

7897
# Attributes, Bridge acting as an model
@@ -82,32 +101,48 @@ end
82101
function MOI.get(bridge::DiagonallyDominantBridge, ::MOI.ListOfVariableIndices)
83102
return bridge.abs_vars
84103
end
85-
function MOI.get(bridge::DiagonallyDominantBridge{T, MOI.ScalarAffineFunction{T}},
86-
::MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T},
87-
MOI.GreaterThan{T}}) where T
88-
return length(bridge.abs_plus) + length(bridge.abs_minus) + length(bridge.dominance)
89-
end
90-
function MOI.get(bridge::DiagonallyDominantBridge{T},
91-
::MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T},
92-
MOI.GreaterThan{T}}) where T
104+
function MOI.get(
105+
bridge::DiagonallyDominantBridge{T,MOI.ScalarAffineFunction{T}},
106+
::MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T},MOI.GreaterThan{T}},
107+
) where {T}
108+
return length(bridge.abs_plus) +
109+
length(bridge.abs_minus) +
110+
length(bridge.dominance)
111+
end
112+
function MOI.get(
113+
bridge::DiagonallyDominantBridge{T},
114+
::MOI.NumberOfConstraints{MOI.ScalarAffineFunction{T},MOI.GreaterThan{T}},
115+
) where {T}
93116
return length(bridge.abs_plus) + length(bridge.abs_minus)
94117
end
95-
function MOI.get(bridge::DiagonallyDominantBridge{T, F},
96-
::MOI.NumberOfConstraints{F, MOI.GreaterThan{T}}) where {T, F}
118+
function MOI.get(
119+
bridge::DiagonallyDominantBridge{T,F},
120+
::MOI.NumberOfConstraints{F,MOI.GreaterThan{T}},
121+
) where {T,F}
97122
return length(bridge.dominance)
98123
end
99-
function MOI.get(bridge::DiagonallyDominantBridge{T, MOI.ScalarAffineFunction{T}},
100-
::MOI.ListOfConstraintIndices{MOI.ScalarAffineFunction{T},
101-
MOI.GreaterThan{T}}) where T
124+
function MOI.get(
125+
bridge::DiagonallyDominantBridge{T,MOI.ScalarAffineFunction{T}},
126+
::MOI.ListOfConstraintIndices{
127+
MOI.ScalarAffineFunction{T},
128+
MOI.GreaterThan{T},
129+
},
130+
) where {T}
102131
return vcat(bridge.abs_plus, bridge.abs_minus, bridge.dominance)
103132
end
104-
function MOI.get(bridge::DiagonallyDominantBridge{T},
105-
::MOI.ListOfConstraintIndices{MOI.ScalarAffineFunction{T},
106-
MOI.GreaterThan{T}}) where T
133+
function MOI.get(
134+
bridge::DiagonallyDominantBridge{T},
135+
::MOI.ListOfConstraintIndices{
136+
MOI.ScalarAffineFunction{T},
137+
MOI.GreaterThan{T},
138+
},
139+
) where {T}
107140
return vcat(bridge.abs_plus, bridge.abs_minus)
108141
end
109-
function MOI.get(bridge::DiagonallyDominantBridge{T, F},
110-
::MOI.ListOfConstraintIndices{F, MOI.GreaterThan{T}}) where {T, F}
142+
function MOI.get(
143+
bridge::DiagonallyDominantBridge{T,F},
144+
::MOI.ListOfConstraintIndices{F,MOI.GreaterThan{T}},
145+
) where {T,F}
111146
return bridge.dominance
112147
end
113148

@@ -128,37 +163,50 @@ function MOI.delete(model::MOI.ModelLike, bridge::DiagonallyDominantBridge)
128163
end
129164

130165
# Attributes, Bridge acting as a constraint
131-
function MOI.get(::MOI.ModelLike, ::MOI.ConstraintSet,
132-
bridge::DiagonallyDominantBridge)
166+
function MOI.get(
167+
::MOI.ModelLike,
168+
::MOI.ConstraintSet,
169+
bridge::DiagonallyDominantBridge,
170+
)
133171
return SOS.DiagonallyDominantConeTriangle(length(bridge.dominance))
134172
end
135-
function MOI.get(model::MOI.ModelLike, attr::MOI.ConstraintFunction,
136-
bridge::DiagonallyDominantBridge{T, F, G}) where {T, F, G}
173+
function MOI.get(
174+
model::MOI.ModelLike,
175+
attr::MOI.ConstraintFunction,
176+
bridge::DiagonallyDominantBridge{T,F,G},
177+
) where {T,F,G}
137178
set = MOI.get(model, MOI.ConstraintSet(), bridge)
138179
H = MOI.Utilities.scalar_type(G)
139180
g = Vector{H}(undef, MOI.dimension(set))
140181
k = 0
141182
koff = 0
142183
for j in 1:MOI.side_dimension(set)
143-
for i in 1:(j - 1)
184+
for i in 1:(j-1)
144185
k += 1
145186
koff += 1
146187
func = MOI.get(model, attr, bridge.abs_plus[koff])
147-
g[k] = MOI.Utilities.convert_approx(H, MOI.Utilities.remove_variable(
148-
func, bridge.abs_vars))
188+
g[k] = MOI.Utilities.convert_approx(
189+
H,
190+
MOI.Utilities.remove_variable(func, bridge.abs_vars),
191+
)
149192
end
150193
k += 1
151194
func = MOI.get(model, attr, bridge.dominance[j])
152-
g[k] = MOI.Utilities.convert_approx(H, MOI.Utilities.remove_variable(
153-
func, bridge.abs_vars))
195+
g[k] = MOI.Utilities.convert_approx(
196+
H,
197+
MOI.Utilities.remove_variable(func, bridge.abs_vars),
198+
)
154199
end
155200
return MOI.Utilities.vectorize(g)
156201
end
157202

158203
# TODO ConstraintPrimal
159204

160-
function MOI.get(model::MOI.ModelLike, attr::MOI.ConstraintDual,
161-
bridge::DiagonallyDominantBridge{T}) where T
205+
function MOI.get(
206+
model::MOI.ModelLike,
207+
attr::MOI.ConstraintDual,
208+
bridge::DiagonallyDominantBridge{T},
209+
) where {T}
162210
dominance_dual = MOI.get(model, attr, bridge.dominance)
163211
side_dim = length(dominance_dual)
164212
dim = MOI.dimension(SOS.DiagonallyDominantConeTriangle(side_dim))
@@ -169,7 +217,7 @@ function MOI.get(model::MOI.ModelLike, attr::MOI.ConstraintDual,
169217
k += 1
170218
# Need to divide by 2 because of the custom scalar product for this
171219
# cone
172-
dual[k] = (- dominance_dual[i] - dominance_dual[j]) / 2
220+
dual[k] = (-dominance_dual[i] - dominance_dual[j]) / 2
173221
end
174222
k += 1
175223
dual[k] = dominance_dual[j]

src/Bridges/Constraint/empty.jl

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
1-
struct EmptyBridge{T} <: MOI.Bridges.Constraint.AbstractBridge
2-
end
1+
struct EmptyBridge{T} <: MOI.Bridges.Constraint.AbstractBridge end
32

43
function MOI.Bridges.Constraint.bridge_constraint(
5-
::Type{EmptyBridge{T}}, model::MOI.ModelLike, f::MOI.AbstractVectorFunction,
6-
s::SOS.EmptyCone) where {T}
4+
::Type{EmptyBridge{T}},
5+
model::MOI.ModelLike,
6+
f::MOI.AbstractVectorFunction,
7+
s::SOS.EmptyCone,
8+
) where {T}
79
@assert MOI.output_dimension(f) == MOI.dimension(s)
810
return EmptyBridge{T}()
911
end
1012

1113
function MOI.supports_constraint(
12-
::Type{<:EmptyBridge}, ::Type{<:MOI.AbstractVectorFunction},
13-
::Type{<:SOS.EmptyCone})
14+
::Type{<:EmptyBridge},
15+
::Type{<:MOI.AbstractVectorFunction},
16+
::Type{<:SOS.EmptyCone},
17+
)
1418
return true
1519
end
1620
function MOI.Bridges.added_constrained_variable_types(::Type{<:EmptyBridge})
1721
return Tuple{DataType}[]
1822
end
1923
function MOI.Bridges.added_constraint_types(::Type{<:EmptyBridge})
20-
return Tuple{DataType, DataType}[]
24+
return Tuple{DataType,DataType}[]
2125
end
2226
function MOI.Bridges.Constraint.concrete_bridge_type(
23-
::Type{<:EmptyBridge{T}}, ::Type{<:MOI.AbstractVectorFunction},
24-
::Type{SOS.EmptyCone}) where T
27+
::Type{<:EmptyBridge{T}},
28+
::Type{<:MOI.AbstractVectorFunction},
29+
::Type{SOS.EmptyCone},
30+
) where {T}
2531
return EmptyBridge{T}
2632
end
2733

2834
# Indices
2935
function MOI.delete(model::MOI.ModelLike, bridge::EmptyBridge) end
3036

31-
function MOI.get(::MOI.ModelLike,
32-
::Union{MOI.ConstraintDual, MOI.ConstraintPrimal},
33-
bridge::EmptyBridge{T}) where T
37+
function MOI.get(
38+
::MOI.ModelLike,
39+
::Union{MOI.ConstraintDual,MOI.ConstraintPrimal},
40+
bridge::EmptyBridge{T},
41+
) where {T}
3442
return T[]
3543
end

0 commit comments

Comments
 (0)