Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JuliaFormatter #10

Merged
merged 1 commit into from
Feb 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.15.11"))
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)
93 changes: 64 additions & 29 deletions src/Bridges/Constraint/split_zero.jl
Original file line number Diff line number Diff line change
@@ -1,54 +1,76 @@
struct SplitZeroBridge{T, F<:MOI.Utilities.TypedLike{T}, G<:MOI.Utilities.TypedLike{Complex{T}}} <: MOI.Bridges.Constraint.AbstractBridge
struct SplitZeroBridge{
T,
F<:MOI.Utilities.TypedLike{T},
G<:MOI.Utilities.TypedLike{Complex{T}},
} <: MOI.Bridges.Constraint.AbstractBridge
dimension::Int
constraint::MOI.ConstraintIndex{F, MOI.Zeros}
constraint::MOI.ConstraintIndex{F,MOI.Zeros}
real_indices::Vector{Int}
imag_indices::Vector{Int}
end
function _nonzero_indices(func::MOI.AbstractVectorFunction)
return [i for (i, scalar_func) in enumerate(MOIU.scalarize(func)) if !iszero(scalar_func)]
return [
i for (i, scalar_func) in enumerate(MOIU.scalarize(func)) if !iszero(scalar_func)
]
end
function MOI.Bridges.Constraint.bridge_constraint(
::Type{SplitZeroBridge{T, F, G}}, model::MOI.ModelLike,
::Type{SplitZeroBridge{T,F,G}},
model::MOI.ModelLike,
f::G,
set::MOI.Zeros
) where {T, F, G}
set::MOI.Zeros,
) where {T,F,G}
real_part = real(f)
imag_part = MOI.Utilities.operate(imag, T, f)
real_indices = _nonzero_indices(real_part)
imag_indices = _nonzero_indices(imag_part)
func = MOIU.operate(
vcat, T,
vcat,
T,
MOIU.eachscalar(real_part)[real_indices],
MOIU.eachscalar(imag_part)[imag_indices]
MOIU.eachscalar(imag_part)[imag_indices],
)
constraint = MOI.add_constraint(
model,
func,
MOI.Zeros(length(real_indices) + length(imag_indices)),
)
return SplitZeroBridge{T,F,G}(
MOI.dimension(set),
constraint,
real_indices,
imag_indices,
)
constraint = MOI.add_constraint(model, func, MOI.Zeros(length(real_indices) + length(imag_indices)))
return SplitZeroBridge{T, F, G}(MOI.dimension(set), constraint, real_indices, imag_indices)
end

# We don't support `MOI.VectorOfVariables` as it would be a self-loop in the bridge graph
function MOI.supports_constraint(
::Type{SplitZeroBridge{T}}, ::Type{<:MOI.Utilities.TypedLike{Complex{T}}},
::Type{MOI.Zeros}) where T
::Type{SplitZeroBridge{T}},
::Type{<:MOI.Utilities.TypedLike{Complex{T}}},
::Type{MOI.Zeros},
) where {T}
return true
end
MOIB.added_constrained_variable_types(::Type{<:SplitZeroBridge}) = Tuple{DataType}[]
function MOIB.added_constraint_types(::Type{SplitZeroBridge{T, F, G}}) where {T, F, G}
return Tuple{DataType, DataType}[(F, MOI.Zeros)]
function MOIB.added_constraint_types(::Type{SplitZeroBridge{T,F,G}}) where {T,F,G}
return Tuple{DataType,DataType}[(F, MOI.Zeros)]
end
function MOI.Bridges.Constraint.concrete_bridge_type(
::Type{<:SplitZeroBridge{T}}, G::Type{<:MOI.Utilities.TypedLike},
::Type{MOI.Zeros}) where T
::Type{<:SplitZeroBridge{T}},
G::Type{<:MOI.Utilities.TypedLike},
::Type{MOI.Zeros},
) where {T}
F = MA.promote_operation(imag, G)
return SplitZeroBridge{T, F, G}
return SplitZeroBridge{T,F,G}
end

# Attributes, Bridge acting as a model
function MOI.get(::SplitZeroBridge{T, F},
::MOI.NumberOfConstraints{F, MOI.Zeros}) where {T, F}
function MOI.get(::SplitZeroBridge{T,F}, ::MOI.NumberOfConstraints{F,MOI.Zeros}) where {T,F}
return 1
end
function MOI.get(bridge::SplitZeroBridge{T, F},
::MOI.ListOfConstraintIndices{F, MOI.Zeros}) where {T, F}
function MOI.get(
bridge::SplitZeroBridge{T,F},
::MOI.ListOfConstraintIndices{F,MOI.Zeros},
) where {T,F}
return [bridge.constraint]
end

Expand All @@ -60,31 +82,44 @@ end
# Attributes, Bridge acting as a constraint
function MOI.supports(
::MOI.ModelLike,
::Union{MOI.ConstraintPrimalStart, MOI.ConstraintDualStart},
::Type{<:SplitZeroBridge})
::Union{MOI.ConstraintPrimalStart,MOI.ConstraintDualStart},
::Type{<:SplitZeroBridge},
)

return true
end
function MOI.get(model::MOI.ModelLike, attr::Union{MOI.ConstraintPrimal, MOI.ConstraintPrimalStart, MOI.ConstraintDual, MOI.ConstraintDualStart},
bridge::SplitZeroBridge)
function MOI.get(
model::MOI.ModelLike,
attr::Union{
MOI.ConstraintPrimal,
MOI.ConstraintPrimalStart,
MOI.ConstraintDual,
MOI.ConstraintDualStart,
},
bridge::SplitZeroBridge,
)
values = MOI.get(model, attr, bridge.constraint)
output = zeros(Complex{eltype(values)}, bridge.dimension)
for (i, idx) in enumerate(bridge.real_indices)
output[idx] = values[i]
end
for (i, idx) in enumerate(bridge.imag_indices)
output[idx] = values[length(bridge.real_indices) + i] * im
output[idx] = values[length(bridge.real_indices)+i] * im
end
return output
end
function MOI.set(model::MOI.ModelLike, attr::Union{MOI.ConstraintPrimalStart, MOI.ConstraintDualStart},
bridge::SplitZeroBridge{T}, value) where T
function MOI.set(
model::MOI.ModelLike,
attr::Union{MOI.ConstraintPrimalStart,MOI.ConstraintDualStart},
bridge::SplitZeroBridge{T},
value,
) where {T}
input = Vector{T}(undef, length(bridge.real_indices) + length(bridge.imag_indices))
for (i, idx) in enumerate(bridge.real_indices)
input[i] = real(value[idx])
end
for (i, idx) in enumerate(bridge.imag_indices)
input[length(bridge.real_indices) + i] = imag(value[idx])
input[length(bridge.real_indices)+i] = imag(value[idx])
end
MOI.set(model, attr, bridge.constraint, input)
end
Loading