Skip to content

Commit

Permalink
Update compat bounds for dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
lassepe committed Dec 5, 2024
1 parent a1a5dbc commit 146b5ad
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"

[compat]
ChainRulesCore = "1"
FastDifferentiation = "0.4"
ForwardDiff = "0.10"
PATHSolver = "1.4"
Symbolics = "4,5"
FastDifferentiation = "0.3"
Symbolics = "4,5,6"
julia = "1.8"
4 changes: 2 additions & 2 deletions src/sparse_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ function get_constant_entries(
M_symbolic::AbstractMatrix{<:FD.Node},
z_symbolic::AbstractVector{<:FD.Node},
)
_z_syms = FD.variables(z_symbolic)
_z_syms = [zs.node_value for zs in FD.variables(z_symbolic)]
# find all entries that are not a function of any of the symbols in z
findall(SparseArrays.nonzeros(M_symbolic)) do v
_vars_syms = FD.variables(v)
_vars_syms = [vs.node_value for vs in FD.variables(v)]
isempty(intersect(_vars_syms, _z_syms))
end
end
35 changes: 27 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ using Random: Random
using LinearAlgebra: norm
using Zygote: Zygote
using FiniteDiff: FiniteDiff
using Symbolics: Symbolics

@testset "ParametricMCPs.jl" begin
for backend in [ParametricMCPs.SymbolicUtils.SymbolicsBackend(), ParametricMCPs.SymbolicUtils.FastDifferentiationBackend()]
for backend in [
ParametricMCPs.SymbolicUtils.SymbolicsBackend(),
ParametricMCPs.SymbolicUtils.FastDifferentiationBackend(),
]
rng = Random.MersenneTwister(1)
parameter_dimension = 2
# setup a dummy MCP which represents a QP with:
Expand All @@ -16,8 +18,21 @@ using Symbolics: Symbolics
f(z, θ) = [2z[1:2] - z[3:4] - 2θ; z[1:2]]
lower_bounds = [-Inf, -Inf, 0, 0]
upper_bounds = [Inf, Inf, Inf, Inf]
problem = ParametricMCPs.ParametricMCP(f, lower_bounds, upper_bounds, parameter_dimension; backend)
problem_no_jacobian = ParametricMCPs.ParametricMCP(f, lower_bounds, upper_bounds, parameter_dimension; compute_sensitivities=false, backend)
problem = ParametricMCPs.ParametricMCP(
f,
lower_bounds,
upper_bounds,
parameter_dimension;
backend,
)
problem_no_jacobian = ParametricMCPs.ParametricMCP(
f,
lower_bounds,
upper_bounds,
parameter_dimension;
compute_sensitivities = false,
backend,
)

feasible_parameters = [[0.0, 0.0], [1.0, 0.0], [0.0, 1.0], [rand(rng, 2) for _ in 1:10]...]
infeasible_parameters = -feasible_parameters
Expand All @@ -42,10 +57,11 @@ using Symbolics: Symbolics

for θ in [feasible_parameters; infeasible_parameters]
∇_autodiff_reverse = only(Zygote.gradient(dummy_pipeline, θ))
∇_autodiff_forward = only(Zygote.gradient-> Zygote.forwarddiff(dummy_pipeline, θ), θ))
∇_autodiff_forward =
only(Zygote.gradient-> Zygote.forwarddiff(dummy_pipeline, θ), θ))
∇_finitediff = FiniteDiff.finite_difference_gradient(dummy_pipeline, θ)
@test isapprox(∇_autodiff_reverse, ∇_finitediff; atol=1e-4)
@test isapprox(∇_autodiff_reverse, ∇_autodiff_forward; atol=1e-4)
@test isapprox(∇_autodiff_reverse, ∇_finitediff; atol = 1e-4)
@test isapprox(∇_autodiff_reverse, ∇_autodiff_forward; atol = 1e-4)
end
end

Expand All @@ -55,7 +71,10 @@ using Symbolics: Symbolics
sum(solution.z .^ 2)
end

@test_throws ArgumentError Zygote.gradient-> dummy_pipeline(θ, problem_no_jacobian), feasible_parameters[1])
@test_throws ArgumentError Zygote.gradient(
θ -> dummy_pipeline(θ, problem_no_jacobian),
feasible_parameters[1],
)
end
end
end

0 comments on commit 146b5ad

Please sign in to comment.