Skip to content
Closed
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
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ DiscreteMeasures = "7766d772-2108-41ee-a4bd-11c51440a39b"
OUQBase = "01930cae-99d2-7439-8f4f-ace2ece9f1b9"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"

[sources]
CanonicalMoments = { path = "lib/CanonicalMoments" }
Expand All @@ -24,6 +25,7 @@ Pkg = "1.10"
PrecompileTools = "1.2"
Reexport = "1.2.2"
SafeTestsets = "0.1"
Symbolics = "7.37"
Test = "1.10"
SciMLTesting = "2"

Expand Down
3 changes: 1 addition & 2 deletions lib/DiscreteMeasures/ext/IntervalArithmeticExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ module IntervalArithmeticExt

using DiscreteMeasures, IntervalArithmetic

println("Load Ext")
function DiscreteMeasures.clamp_domain(x::Interval, lb, ub)
bounds = interval(lb, ub)
return intersect(x, bounds)
return intersect_interval(x, bounds)
end

function DiscreteMeasures.clamp_weight(w::Interval{T}, maxw::T = one(T)) where {T}
Expand Down
11 changes: 6 additions & 5 deletions lib/DiscreteMeasures/test/Core/discrete_measures_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ end
end

@testset "Interval Ext" begin
@test clamp_domain(interval(-2, -1), 0, 2) == emptyinterval()
@test clamp_domain(interval(-2, 1), 0, 2) == interval(0, 1)
@test clamp_domain(interval(0.1, 0.5), 0, 2) == interval(0.1, 0.5)
@test clamp_domain(interval(0.5, 5), 0, 2) == interval(0.5, 2)
@test clamp_domain(interval(3, 4), 0, 2) == emptyinterval()
# IntervalArithmetic 1.x: empty intervals compare false under `==` even when equal.
@test isequal_interval(clamp_domain(interval(-2, -1), 0, 2), emptyinterval())
@test isequal_interval(clamp_domain(interval(-2, 1), 0, 2), interval(0, 1))
@test isequal_interval(clamp_domain(interval(0.1, 0.5), 0, 2), interval(0.1, 0.5))
@test isequal_interval(clamp_domain(interval(0.5, 5), 0, 2), interval(0.5, 2))
@test isequal_interval(clamp_domain(interval(3, 4), 0, 2), emptyinterval())
end

end
16 changes: 14 additions & 2 deletions lib/OUQBase/src/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,18 +203,30 @@ function get_group_name(var, ouq_sys::OUQSystem)
return get_group_name(var, ouq_sys.admissible_set)
end

# `𝔼`/`β„™` are modeled as `Symbolics.Operator`s (the same abstraction used for
# e.g. `Differential`). `SymbolicUtils.default_is_atomic` therefore treats any
# `𝔼(Q)`/`β„™(Q)` application as an atomic leaf (correct for `D(x)`, but not for
# us): we need to recurse through it to discover the underlying random
# variable `Q` that it wraps.
function _random_variable_is_atomic(ex)
if SymbolicUtils.iscall(ex) && SymbolicUtils.operation(ex) isa Operator
return false
end
return SymbolicUtils.default_is_atomic(ex)
end

function get_ordered_group_names(
expression,
admissible_set::AdmissibleSet;
ensure_singleton = true,
ensure_all = false,
)
if ensure_singleton
vars = get_variables(expression)
vars = get_variables(expression; is_atomic = _random_variable_is_atomic)
@assert length(vars) == 1 "Expression $expression has multiple random variables $vars. Disable `ensure_singleton` if this is intended."
return unique([get_group_name(only(vars), admissible_set)])
else
vars = get_variables(expression)
vars = get_variables(expression; is_atomic = _random_variable_is_atomic)
@debug "Expression $expression has multiple random variables $vars"
unordered_group_names = Set([get_group_name(var, admissible_set) for var in vars])
if ensure_all
Expand Down
13 changes: 10 additions & 3 deletions lib/OUQBase/src/operators.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import Symbolics: Operator, value
import SymbolicUtils: Term, symtype
import SymbolicUtils: symtype, term

struct 𝔼_ <: Operator end

const 𝔼 = 𝔼_() # To get same object

(::𝔼_)(x) = Term{symtype(x)}(𝔼, Any[x])
# `term` (rather than constructing `Term{T}` directly) picks the SymVariant
# type parameter itself and infers the symbolic type via `promote_symtype`.
(::𝔼_)(x) = term(𝔼, x)
(::𝔼_)(x::Num) = Num(𝔼(value(x)))

SymbolicUtils.promote_symtype(::𝔼_, x) = x
# Disambiguates against SymbolicUtils's generic `promote_symtype(::Operator,
# ::Type{T}) where T`, which is equally specific to the untyped method above
# when called with a `Type` argument (e.g. `symtype(x) === Real`).
SymbolicUtils.promote_symtype(::𝔼_, ::Type{T}) where {T} = T
Base.nameof(::𝔼_) = :𝔼
SymbolicUtils.isbinop(::𝔼_) = false

Expand All @@ -28,10 +34,11 @@ struct β„™_ <: Operator end

const β„™ = β„™_() # To get same object

(::β„™_)(x) = Term{symtype(x)}(β„™, Any[x])
(::β„™_)(x) = term(β„™, x)
(::β„™_)(x::Num) = Num(β„™(value(x)))

SymbolicUtils.promote_symtype(::β„™_, x) = x
SymbolicUtils.promote_symtype(::β„™_, ::Type{T}) where {T} = T
Base.nameof(::β„™_) = :β„™
SymbolicUtils.isbinop(::β„™_) = false

Expand Down
7 changes: 6 additions & 1 deletion src/OptimalUncertaintyQuantification.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ module OptimalUncertaintyQuantification
using Reexport: @reexport
@reexport using OUQBase

# `@random_variables` expands to code that references `Symbolics.@variables`
# in the caller's scope (via `esc`), so `Symbolics` must be bound here for the
# precompile workload below to expand successfully.
using Symbolics: Symbolics

using PrecompileTools: @compile_workload, @setup_workload

@setup_workload begin
Expand All @@ -11,7 +16,7 @@ using PrecompileTools: @compile_workload, @setup_workload
Independent(Q, bounds = (0.0, 1.0))
end
admissible_set = AdmissibleSet(random_vars, [𝔼(Q) ~ 0.5])
OUQSystem(
OUQSystem(;
objective = 𝔼(Q),
admissible_set,
reduction_alg = WinklerExtremalMeasures(),
Expand Down
Loading