Skip to content

Commit b3b600d

Browse files
authored
Add JuliaFormatter (#10)
1 parent 3b726df commit b3b600d

File tree

5 files changed

+271
-101
lines changed

5 files changed

+271
-101
lines changed

.github/workflows/format_check.yml

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

src/Bridges/Constraint/split_zero.jl

+64-29
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,76 @@
1-
struct SplitZeroBridge{T, F<:MOI.Utilities.TypedLike{T}, G<:MOI.Utilities.TypedLike{Complex{T}}} <: MOI.Bridges.Constraint.AbstractBridge
1+
struct SplitZeroBridge{
2+
T,
3+
F<:MOI.Utilities.TypedLike{T},
4+
G<:MOI.Utilities.TypedLike{Complex{T}},
5+
} <: MOI.Bridges.Constraint.AbstractBridge
26
dimension::Int
3-
constraint::MOI.ConstraintIndex{F, MOI.Zeros}
7+
constraint::MOI.ConstraintIndex{F,MOI.Zeros}
48
real_indices::Vector{Int}
59
imag_indices::Vector{Int}
610
end
711
function _nonzero_indices(func::MOI.AbstractVectorFunction)
8-
return [i for (i, scalar_func) in enumerate(MOIU.scalarize(func)) if !iszero(scalar_func)]
12+
return [
13+
i for (i, scalar_func) in enumerate(MOIU.scalarize(func)) if !iszero(scalar_func)
14+
]
915
end
1016
function MOI.Bridges.Constraint.bridge_constraint(
11-
::Type{SplitZeroBridge{T, F, G}}, model::MOI.ModelLike,
17+
::Type{SplitZeroBridge{T,F,G}},
18+
model::MOI.ModelLike,
1219
f::G,
13-
set::MOI.Zeros
14-
) where {T, F, G}
20+
set::MOI.Zeros,
21+
) where {T,F,G}
1522
real_part = real(f)
1623
imag_part = MOI.Utilities.operate(imag, T, f)
1724
real_indices = _nonzero_indices(real_part)
1825
imag_indices = _nonzero_indices(imag_part)
1926
func = MOIU.operate(
20-
vcat, T,
27+
vcat,
28+
T,
2129
MOIU.eachscalar(real_part)[real_indices],
22-
MOIU.eachscalar(imag_part)[imag_indices]
30+
MOIU.eachscalar(imag_part)[imag_indices],
31+
)
32+
constraint = MOI.add_constraint(
33+
model,
34+
func,
35+
MOI.Zeros(length(real_indices) + length(imag_indices)),
36+
)
37+
return SplitZeroBridge{T,F,G}(
38+
MOI.dimension(set),
39+
constraint,
40+
real_indices,
41+
imag_indices,
2342
)
24-
constraint = MOI.add_constraint(model, func, MOI.Zeros(length(real_indices) + length(imag_indices)))
25-
return SplitZeroBridge{T, F, G}(MOI.dimension(set), constraint, real_indices, imag_indices)
2643
end
2744

2845
# We don't support `MOI.VectorOfVariables` as it would be a self-loop in the bridge graph
2946
function MOI.supports_constraint(
30-
::Type{SplitZeroBridge{T}}, ::Type{<:MOI.Utilities.TypedLike{Complex{T}}},
31-
::Type{MOI.Zeros}) where T
47+
::Type{SplitZeroBridge{T}},
48+
::Type{<:MOI.Utilities.TypedLike{Complex{T}}},
49+
::Type{MOI.Zeros},
50+
) where {T}
3251
return true
3352
end
3453
MOIB.added_constrained_variable_types(::Type{<:SplitZeroBridge}) = Tuple{DataType}[]
35-
function MOIB.added_constraint_types(::Type{SplitZeroBridge{T, F, G}}) where {T, F, G}
36-
return Tuple{DataType, DataType}[(F, MOI.Zeros)]
54+
function MOIB.added_constraint_types(::Type{SplitZeroBridge{T,F,G}}) where {T,F,G}
55+
return Tuple{DataType,DataType}[(F, MOI.Zeros)]
3756
end
3857
function MOI.Bridges.Constraint.concrete_bridge_type(
39-
::Type{<:SplitZeroBridge{T}}, G::Type{<:MOI.Utilities.TypedLike},
40-
::Type{MOI.Zeros}) where T
58+
::Type{<:SplitZeroBridge{T}},
59+
G::Type{<:MOI.Utilities.TypedLike},
60+
::Type{MOI.Zeros},
61+
) where {T}
4162
F = MA.promote_operation(imag, G)
42-
return SplitZeroBridge{T, F, G}
63+
return SplitZeroBridge{T,F,G}
4364
end
4465

4566
# Attributes, Bridge acting as a model
46-
function MOI.get(::SplitZeroBridge{T, F},
47-
::MOI.NumberOfConstraints{F, MOI.Zeros}) where {T, F}
67+
function MOI.get(::SplitZeroBridge{T,F}, ::MOI.NumberOfConstraints{F,MOI.Zeros}) where {T,F}
4868
return 1
4969
end
50-
function MOI.get(bridge::SplitZeroBridge{T, F},
51-
::MOI.ListOfConstraintIndices{F, MOI.Zeros}) where {T, F}
70+
function MOI.get(
71+
bridge::SplitZeroBridge{T,F},
72+
::MOI.ListOfConstraintIndices{F,MOI.Zeros},
73+
) where {T,F}
5274
return [bridge.constraint]
5375
end
5476

@@ -60,31 +82,44 @@ end
6082
# Attributes, Bridge acting as a constraint
6183
function MOI.supports(
6284
::MOI.ModelLike,
63-
::Union{MOI.ConstraintPrimalStart, MOI.ConstraintDualStart},
64-
::Type{<:SplitZeroBridge})
85+
::Union{MOI.ConstraintPrimalStart,MOI.ConstraintDualStart},
86+
::Type{<:SplitZeroBridge},
87+
)
6588

6689
return true
6790
end
68-
function MOI.get(model::MOI.ModelLike, attr::Union{MOI.ConstraintPrimal, MOI.ConstraintPrimalStart, MOI.ConstraintDual, MOI.ConstraintDualStart},
69-
bridge::SplitZeroBridge)
91+
function MOI.get(
92+
model::MOI.ModelLike,
93+
attr::Union{
94+
MOI.ConstraintPrimal,
95+
MOI.ConstraintPrimalStart,
96+
MOI.ConstraintDual,
97+
MOI.ConstraintDualStart,
98+
},
99+
bridge::SplitZeroBridge,
100+
)
70101
values = MOI.get(model, attr, bridge.constraint)
71102
output = zeros(Complex{eltype(values)}, bridge.dimension)
72103
for (i, idx) in enumerate(bridge.real_indices)
73104
output[idx] = values[i]
74105
end
75106
for (i, idx) in enumerate(bridge.imag_indices)
76-
output[idx] = values[length(bridge.real_indices) + i] * im
107+
output[idx] = values[length(bridge.real_indices)+i] * im
77108
end
78109
return output
79110
end
80-
function MOI.set(model::MOI.ModelLike, attr::Union{MOI.ConstraintPrimalStart, MOI.ConstraintDualStart},
81-
bridge::SplitZeroBridge{T}, value) where T
111+
function MOI.set(
112+
model::MOI.ModelLike,
113+
attr::Union{MOI.ConstraintPrimalStart,MOI.ConstraintDualStart},
114+
bridge::SplitZeroBridge{T},
115+
value,
116+
) where {T}
82117
input = Vector{T}(undef, length(bridge.real_indices) + length(bridge.imag_indices))
83118
for (i, idx) in enumerate(bridge.real_indices)
84119
input[i] = real(value[idx])
85120
end
86121
for (i, idx) in enumerate(bridge.imag_indices)
87-
input[length(bridge.real_indices) + i] = imag(value[idx])
122+
input[length(bridge.real_indices)+i] = imag(value[idx])
88123
end
89124
MOI.set(model, attr, bridge.constraint, input)
90125
end

0 commit comments

Comments
 (0)