-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDFGFactor.jl
More file actions
289 lines (257 loc) · 10.1 KB
/
DFGFactor.jl
File metadata and controls
289 lines (257 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
##==============================================================================
## Abstract Types
##==============================================================================
abstract type AbstractObservation end
const Observation = AbstractObservation
abstract type AbstractPriorObservation <: AbstractObservation end
const PriorObservation = AbstractPriorObservation
abstract type AbstractRelativeObservation <: AbstractObservation end
const RelativeObservation = AbstractRelativeObservation
# TODO https://github.com/JuliaRobotics/DistributedFactorGraphs.jl/pull/1127#discussion_r2154672975 and #1138
abstract type AbstractFactorCache end
const FactorCache = AbstractFactorCache #
#TODO consider making AbstractFactorCache{T <: AbstractObservation}
##==============================================================================
## Factor State
##==============================================================================
#TODO is this mutable
@kwdef mutable struct Recipehyper
multihypo::Vector{Float64} = Float64[] # TODO re-evaluate after refactoring w #477
nullhypo::Float64 = 0.0
inflation::Float64 = 0.0
end
@kwdef mutable struct Recipestate
eliminated::Bool = false
potentialused::Bool = false
end
##==============================================================================
## Factors
##==============================================================================
#
# | | label | tags | timestamp | solvable | solverData |
# |-------------------|:-----:|:----:|:---------:|:--------:|:----------:|
# | FactorSkeleton | X | x | | | |
# | FactorSummary | X | X | X | | |
# | FactorDFG | X | X | X | X | X* |
# *not available without reconstruction
# Packed Factor constructor
function assembleFactorName(xisyms::Union{Vector{String}, Vector{Symbol}})
return Symbol(xisyms..., "_f", randstring(4))
end
"""
$(TYPEDEF)
Complete factor structure for a DistributedFactorGraph factor.
Fields:
$(TYPEDFIELDS)
"""
StructUtils.@kwarg struct FactorDFG{T <: AbstractObservation, N} <: AbstractGraphFactor
# """The ID for the factor"""
# id::Union{UUID, Nothing} = nothing #NOTE v0.29 REMOVED
"""Factor label, e.g. :x1f1.
Accessor: [`getLabel`](@ref)"""
label::Symbol
"""Factor tags, e.g [:FACTOR].
Accessors: [`getTags`](@ref), [`mergeTags!`](@ref), and [`deleteTags!`](@ref)"""
tags::Set{Symbol} = Set{Symbol}([:FACTOR])
"""Ordered list of the neighbor variables.
Accessors: [`getVariableOrder`](@ref)"""
variableorder::NTuple{N, Symbol} & (choosetype = x->NTuple{length(x), Symbol},) # NOTE v0.29 renamed from _variableOrderSymbols
"""Variable timestamp.
Accessors: [`getTimestamp`](@ref)"""
timestamp::TimeDateZone = tdz_now() # NOTE v0.29 changed from ZonedDateTime
# TODO
# """(Optional) Steady (monotonic) time in nanoseconds `Nanosecond` (`Int64``)"""
# nstime::Nanosecond #NOTE v0.29 REMOVED as not used, add when needed, or now as steadytime.
"""Solvable flag for the factor.
Accessors: [`getSolvable`](@ref), [`setSolvable!`](@ref)"""
solvable::Base.RefValue{Int} = Ref{Int}(1) #& (lower = getindex, lift = Ref)
"""Dictionary of small data associated with this variable.
Accessors: [`getBloblet`](@ref), [`addBloblet!`](@ref)"""
bloblets::Bloblets = Bloblets() #NOTE v0.29 changed from smallData::Dict{Symbol, MetadataTypes} = Dict{Symbol, MetadataTypes}()
"""Observation function or measurement for this factor.
Accessors: [`getObservation`](@ref)(@ref)"""
observation::T & (lower = pack_lower, choosetype = DFG.resolvePackedType)#TODO finalise serializd type
"""Hyperparameters associated with this factor."""
hyper::Recipehyper = Recipehyper()
"""Describes the current state of the factor. Persisted in serialization."""
state::Recipestate = Recipestate()
"""Temporary, non-persistent memory used internally by the solver for intermediate numerical computations and buffers.
`solvercache` is lazily allocated and only used during factor operations; it is not serialized or retained after solving.
Accessors: [`getCache`](@ref), [`setCache!`](@ref)"""
solvercache::Base.RefValue{<:FactorCache} = Ref{FactorCache}() & (ignore = true,)#TODO easy of use vs. performance as container is abstract in any case.
"""Blobentries associated with this factor."""
blobentries::Blobentries = Blobentries() #NOTE v0.29 added
"""Internal: used for automatic type metadata generation."""
_autotype::Nothing = nothing & (name = :type, lower = _ -> TypeMetadata(FactorDFG))
end
version(::Type{<:FactorDFG}) = v"0.29.0"
##------------------------------------------------------------------------------
## Constructors - IIF like
function FactorDFG(
variableorder::Union{<:Tuple, Vector{Symbol}},
observation::AbstractObservation;
label::Symbol = assembleFactorName(variableorder),
timestamp::Union{TimeDateZone, ZonedDateTime} = tdz_now(),
tags::Union{Set{Symbol}, Vector{Symbol}} = Set{Symbol}([:FACTOR]),
bloblets::Bloblets = Bloblets(),
multihypo::Vector{Float64} = Float64[],
nullhypo::Float64 = 0.0,
inflation::Real = 3.0,
solvable::Int = 1,
nstime = nothing,
metadata = nothing,
)
# deprecated in v0.29
if !isnothing(nstime)
Base.depwarn("`FactorDFG` nstime is deprecated", :FactorDFG)
end
# deprecated in v0.29
if !isnothing(metadata)
Base.depwarn("`FactorDFG` metadata is deprecated, use bloblets instead", :FactorDFG)
end
if timestamp isa ZonedDateTime
Base.depwarn(
"`FactorDFG` timestamp as `ZonedDateTime` is deprecated, use `TimeDateZone` instead",
:FactorDFG,
)
timestamp = TimeDateZone(timestamp.utc_datetime)
end
# create factor data
hyper = Recipehyper(; multihypo, nullhypo, inflation)
state = Recipestate()
union!(tags, [:FACTOR])
# create factor
factor = FactorDFG(;
label,
tags = Set(tags),
variableorder = Tuple(variableorder),
timestamp,
solvable = Ref(solvable),
bloblets,
hyper,
state,
observation,
)
return factor
end
# TODO standardize new fields in kw constructors, .id
function FactorDFG(
label::Symbol,
variableorder::Union{Vector{Symbol}, Tuple},
observation::AbstractObservation,
hyper::Recipehyper = Recipehyper(),
state::Recipestate = Recipestate(),
cache = nothing;
tags::Set{Symbol} = Set{Symbol}([:FACTOR]),
timestamp::Union{DateTime, ZonedDateTime, TimeDateZone} = tdz_now(),
solvable::Int = 1,
bloblets::Bloblets = Bloblets(),
blobentries::Blobentries = Blobentries(),
nstime = nothing,
smallData = nothing,
)
#TODO deprecated in v0.29
if !isnothing(nstime)
Base.depwarn("`FactorDFG` nstime is deprecated", :FactorDFG)
end
if !isnothing(smallData)
Base.depwarn(
"`FactorDFG` smallData is deprecated, use bloblets instead",
:FactorDFG,
)
end
if isnothing(cache)
solvercache = Ref{FactorCache}()
else
solvercache = Ref(cache)
end
# TODO v0.29 deprecate ZonedDateTime or convert internally
if timestamp isa ZonedDateTime
timestamp = TimeDateZone(timestamp)
end
return FactorDFG(
label,
tags,
Tuple(variableorder),
timestamp,
Ref(solvable),
bloblets,
observation,
hyper,
state,
solvercache,
blobentries,
nothing,
)
end
##------------------------------------------------------------------------------
## FactorSummary lv1
##------------------------------------------------------------------------------
"""
$(TYPEDEF)
Read-only summary factor structure for a DistributedFactorGraph factor.
---
Fields:
$(TYPEDFIELDS)
"""
@tags struct FactorSummary <: AbstractGraphFactor
"""Factor label, e.g. :x1f1.
Accessor: [`getLabel`](@ref)"""
label::Symbol
"""Factor tags, e.g [:FACTOR].
Accessors: [`getTags`](@ref), [`mergeTags!`](@ref), and [`deleteTags!`](@ref)"""
tags::Set{Symbol}
"""Ordered list of the neighbor variables.
Accessors: [`getVariableOrder`](@ref)"""
variableorder::Tuple{Vararg{Symbol}} & (choosetype = x->NTuple{length(x), Symbol},) #TODO changed to NTuple
"""Variable timestamp.
Accessors: [`getTimestamp`](@ref)"""
timestamp::TimeDateZone
end
function FactorSummary(
label::Symbol,
variableorder::Union{Vector{Symbol}, Tuple};
timestamp::TimeDateZone = tdz_now(),
tags::Set{Symbol} = Set{Symbol}(),
)
return FactorSummary(label, tags, Tuple(variableorder), timestamp)
end
##------------------------------------------------------------------------------
## FactorSkeleton lv0
##------------------------------------------------------------------------------
"""
$(TYPEDEF)
Skeleton factor structure for a DistributedFactorGraph factor.
---
Fields:
$(TYPEDFIELDS)
"""
@tags struct FactorSkeleton <: AbstractGraphFactor
"""Factor label, e.g. :x1f1.
Accessor: [`getLabel`](@ref)"""
label::Symbol
"""Factor tags, e.g [:FACTOR].
Accessors: [`getTags`](@ref), [`mergeTags!`](@ref), and [`deleteTags!`](@ref)"""
tags::Set{Symbol}
"""Ordered list of the neighbor variables.
Accessors: [`getVariableOrder`](@ref)"""
variableorder::Tuple{Vararg{Symbol}} & (choosetype = x->NTuple{length(x), Symbol},)
end
##------------------------------------------------------------------------------
## Constructors
function FactorSkeleton(
label::Symbol,
variableorder::Union{Vector{Symbol}, Tuple};
tags = Set{Symbol}([:FACTOR]),
)
return FactorSkeleton(label, tags, Tuple(variableorder))
end
##==============================================================================
## Conversion constructors
##==============================================================================
function FactorSummary(f::FactorDFG)
return FactorSummary(f.label, copy(f.tags), f.variableorder, f.timestamp)
end
function FactorSkeleton(f::AbstractGraphFactor)
return FactorSkeleton(f.label, copy(f.tags), f.variableorder)
end