Skip to content

Commit 5500507

Browse files
authored
More efficient isinterior and isfeasible (#1200)
1 parent dc8d2db commit 5500507

File tree

1 file changed

+21
-21
lines changed
  • src/multivariate/solvers/constrained/ipnewton

1 file changed

+21
-21
lines changed

src/multivariate/solvers/constrained/ipnewton/interior.jl

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -994,22 +994,21 @@ specify bounds `lx`, `ux`, `lc`, and `uc`. `x` is feasible if
994994
995995
for all possible `i`.
996996
"""
997-
function isfeasible(bounds::ConstraintBounds, x, c)
998-
isf = true
999-
for (i, j) in enumerate(bounds.eqx)
1000-
isf &= x[j] == bounds.valx[i]
1001-
end
1002-
for (i, j) in enumerate(bounds.ineqx)
1003-
isf &= bounds.σx[i] * (x[j] - bounds.bx[i]) >= 0
1004-
end
1005-
for (i, j) in enumerate(bounds.eqc)
1006-
isf &= c[j] == bounds.valc[i]
997+
function isfeasible(bounds::ConstraintBounds, x::Vector{<:Real}, c::Vector{<:Real})
998+
return _isfeasible(x, bounds.eqx, bounds.valx, bounds.ineqx, bounds.σx, bounds.bx) &&
999+
_isfeasible(c, bounds.eqc, bounds.valc, bounds.ineqc, bounds.σc, bounds.bc)
1000+
end
1001+
function _isfeasible(x::Vector{<:Real}, eqx::Vector{Int}, valx::Vector{<:Real}, ineqx::Vector{Int}, σx::Vector{Int8}, bx::Vector{<:Real})
1002+
for (i, v) in zip(eqx, valx)
1003+
x[i] == v || return false
10071004
end
1008-
for (i, j) in enumerate(bounds.ineqc)
1009-
isf &= bounds.σc[i] * (c[j] - bounds.bc[i]) >= 0
1005+
for (i, σ, b) in zip(ineqx, σx, bx)
1006+
y = x[i] - b
1007+
iszero(y) || sign(y) == σ || return false
10101008
end
1011-
isf
1009+
return true
10121010
end
1011+
10131012
isfeasible(constraints, state::AbstractBarrierState) =
10141013
isfeasible(constraints, state.x, state.constraints_c)
10151014
function isfeasible(constraints, x)
@@ -1037,16 +1036,17 @@ given the `constraints` which specify bounds `lx`, `ux`, `lc`, and
10371036
10381037
for all possible `i`.
10391038
"""
1040-
function isinterior(bounds::ConstraintBounds, x, c)
1041-
isi = true
1042-
for (i, j) in enumerate(bounds.ineqx)
1043-
isi &= bounds.σx[i] * (x[j] - bounds.bx[i]) > 0
1044-
end
1045-
for (i, j) in enumerate(bounds.ineqc)
1046-
isi &= bounds.σc[i] * (c[j] - bounds.bc[i]) > 0
1039+
function isinterior(bounds::ConstraintBounds, x::Vector{<:Real}, c::Vector{<:Real})
1040+
return _isinterior(x, bounds.ineqx, bounds.σx, bounds.bx) &&
1041+
_isinterior(c, bounds.ineqc, bounds.σc, bounds.bc)
1042+
end
1043+
function _isinterior(x::Vector{<:Real}, ineqx::Vector{Int}, σx::Vector{Int8}, bx::Vector{<:Real})
1044+
for (i, σ, b) in zip(ineqx, σx, bx)
1045+
sign(x[i] - b) == σ || return false
10471046
end
1048-
isi
1047+
return true
10491048
end
1049+
10501050
isinterior(constraints, state::AbstractBarrierState) =
10511051
isinterior(constraints, state.x, state.constraints_c)
10521052
function isinterior(constraints, x)

0 commit comments

Comments
 (0)