src/regularisation.rs

branch
dev
changeset 63
7a8a55fd41c0
parent 61
4f468d35fa29
equal deleted inserted replaced
61:4f468d35fa29 63:7a8a55fd41c0
126 mA_normest: F, 126 mA_normest: F,
127 ε: F, 127 ε: F,
128 config: &InsertionConfig<F>, 128 config: &InsertionConfig<F>,
129 ) -> usize; 129 ) -> usize;
130 130
131 /// Approximately solve the problem
132 /// <div>$$
133 /// \min_{x ∈ ℝ^n} \frac{1}{2} |x-y|_1^2 - g^⊤ x + τ G(x)
134 /// $$</div>
135 /// for $G$ depending on the trait implementation.
136 ///
137 /// Returns the number of iterations taken.
138 fn solve_findim_l1squared(
139 &self,
140 y: &DVector<F::MixedType>,
141 g: &DVector<F::MixedType>,
142 τ: F,
143 x: &mut DVector<F::MixedType>,
144 ε: F,
145 config: &InsertionConfig<F>,
146 ) -> usize;
147
148 /// Find a point where `d` may violate the tolerance `ε`. 131 /// Find a point where `d` may violate the tolerance `ε`.
149 /// 132 ///
150 /// If `skip_by_rough_check` is set, do not find the point if a rough check indicates that we 133 /// If `skip_by_rough_check` is set, do not find the point if a rough check indicates that we
151 /// are in bounds. `ε` is the current main tolerance and `τ` a scaling factor for the 134 /// are in bounds. `ε` is the current main tolerance and `τ` a scaling factor for the
152 /// regulariser. 135 /// regulariser.
161 ε: F, 144 ε: F,
162 skip_by_rough_check: bool, 145 skip_by_rough_check: bool,
163 config: &InsertionConfig<F>, 146 config: &InsertionConfig<F>,
164 ) -> Option<(Domain, F, bool)> 147 ) -> Option<(Domain, F, bool)>
165 where 148 where
166 M: MinMaxMapping<Domain, F>,
167 {
168 self.find_tolerance_violation_slack(d, τ, ε, skip_by_rough_check, config, F::ZERO)
169 }
170
171 /// Find a point where `d` may violate the tolerance `ε`.
172 ///
173 /// This version includes a `slack` parameter to expand the tolerances.
174 /// It is used for Radon-norm squared proximal term in [`crate::prox_penalty::radon_squared`].
175 ///
176 /// If `skip_by_rough_check` is set, do not find the point if a rough check indicates that we
177 /// are in bounds. `ε` is the current main tolerance and `τ` a scaling factor for the
178 /// regulariser.
179 ///
180 /// Returns `None` if `d` is in bounds either based on the rough check, or a more precise check
181 /// terminating early. Otherwise returns a possibly violating point, the value of `d` there,
182 /// and a boolean indicating whether the found point is in bounds.
183 fn find_tolerance_violation_slack<M>(
184 &self,
185 d: &mut M,
186 τ: F,
187 ε: F,
188 skip_by_rough_check: bool,
189 config: &InsertionConfig<F>,
190 slack: F,
191 ) -> Option<(Domain, F, bool)>
192 where
193 M: MinMaxMapping<Domain, F>; 149 M: MinMaxMapping<Domain, F>;
194 150
195 /// Verify that `d` is in bounds `ε` for a merge candidate `μ` 151 /// Verify that `d` is in bounds `ε` for a merge candidate `μ`
196 /// 152 ///
197 /// `ε` is the current main tolerance and `τ` a scaling factor for the regulariser. 153 /// `ε` is the current main tolerance and `τ` a scaling factor for the regulariser.
203 ε: F, 159 ε: F,
204 config: &InsertionConfig<F>, 160 config: &InsertionConfig<F>,
205 ) -> bool 161 ) -> bool
206 where 162 where
207 M: MinMaxMapping<Domain, F>; 163 M: MinMaxMapping<Domain, F>;
164
165 /// TODO: document this
166 fn target_bounds(&self, τ: F, ε: F) -> Option<Bounds<F>>;
167
168 /// Returns a scaling factor for the tolerance sequence.
169 ///
170 /// Typically this is the regularisation parameter.
171 fn tolerance_scaling(&self) -> F;
172 }
173
174 /// Regularisation term that can be used with [`crate::prox_penalty::radon_squared::RadonSquared`]
175 /// proximal penalty.
176 pub trait RadonSquaredRegTerm<Domain, F = f64>: RegTerm<Domain, F>
177 where
178 Domain: Space + Clone,
179 F: Float + ToNalgebraRealField,
180 {
181 /// Adapt weights of μ, possibly insertion a new point at tolerance_violation (which should
182 /// be returned by [`RegTerm::find_tolerance_violation`].
183 fn solve_oc_radonsq<M>(
184 &self,
185 μ: &mut DiscreteMeasure<Domain, F>,
186 τv: &mut M,
187 τ: F,
188 ε: F,
189 tolerance_violation: Option<(Domain, F, bool)>,
190 config: &InsertionConfig<F>,
191 stats: &mut IterInfo<F>,
192 ) where
193 M: Mapping<Domain, Codomain = F>;
208 194
209 /// Verify that `d` is in bounds `ε` for a merge candidate `μ` 195 /// Verify that `d` is in bounds `ε` for a merge candidate `μ`
210 /// 196 ///
211 /// This version is s used for Radon-norm squared proximal term in 197 /// This version is s used for Radon-norm squared proximal term in
212 /// [`crate::prox_penalty::radon_squared`]. 198 /// [`crate::prox_penalty::radon_squared`].
223 config: &InsertionConfig<F>, 209 config: &InsertionConfig<F>,
224 radon_μ: &DiscreteMeasure<Domain, F>, 210 radon_μ: &DiscreteMeasure<Domain, F>,
225 ) -> bool 211 ) -> bool
226 where 212 where
227 M: MinMaxMapping<Domain, F>; 213 M: MinMaxMapping<Domain, F>;
228
229 /// TODO: document this
230 fn target_bounds(&self, τ: F, ε: F) -> Option<Bounds<F>>;
231
232 /// Returns a scaling factor for the tolerance sequence.
233 ///
234 /// Typically this is the regularisation parameter.
235 fn tolerance_scaling(&self) -> F;
236 } 214 }
237 215
238 /// Abstraction of regularisation terms for [`pointsource_sliding_fb_reg`]. 216 /// Abstraction of regularisation terms for [`pointsource_sliding_fb_reg`].
239 pub trait SlidingRegTerm<Domain, F = f64>: RegTerm<Domain, F> 217 pub trait SlidingRegTerm<Domain, F = f64>: RegTerm<Domain, F>
240 where 218 where
277 let inner_tolerance = ε * config.inner.tolerance_mult; 255 let inner_tolerance = ε * config.inner.tolerance_mult;
278 let inner_it = config.inner.iterator_options.stop_target(inner_tolerance); 256 let inner_it = config.inner.iterator_options.stop_target(inner_tolerance);
279 quadratic_nonneg(mA, g, τ * self.α(), x, mA_normest, &config.inner, inner_it) 257 quadratic_nonneg(mA, g, τ * self.α(), x, mA_normest, &config.inner, inner_it)
280 } 258 }
281 259
282 fn solve_findim_l1squared(
283 &self,
284 y: &DVector<F::MixedType>,
285 g: &DVector<F::MixedType>,
286 τ: F,
287 x: &mut DVector<F::MixedType>,
288 ε: F,
289 config: &InsertionConfig<F>,
290 ) -> usize {
291 let inner_tolerance = ε * config.inner.tolerance_mult;
292 let inner_it = config.inner.iterator_options.stop_target(inner_tolerance);
293 l1squared_nonneg(y, g, τ * self.α(), 1.0, x, &config.inner, inner_it)
294 }
295
296 #[inline] 260 #[inline]
297 fn find_tolerance_violation_slack<M>( 261 fn find_tolerance_violation<M>(
298 &self, 262 &self,
299 d: &mut M, 263 d: &mut M,
300 τ: F, 264 τ: F,
301 ε: F, 265 ε: F,
302 skip_by_rough_check: bool, 266 skip_by_rough_check: bool,
303 config: &InsertionConfig<F>, 267 config: &InsertionConfig<F>,
304 slack: F,
305 ) -> Option<(Loc<N, F>, F, bool)> 268 ) -> Option<(Loc<N, F>, F, bool)>
306 where 269 where
307 M: MinMaxMapping<Loc<N, F>, F>, 270 M: MinMaxMapping<Loc<N, F>, F>,
308 { 271 {
309 let τα = τ * self.α(); 272 let τα = τ * self.α();
310 let keep_above = -τα - slack - ε; 273 let keep_above = -τα - ε;
311 let minimise_below = -τα - slack - ε * config.insertion_cutoff_factor; 274 let minimise_below = -τα - ε * config.insertion_cutoff_factor;
312 let refinement_tolerance = ε * config.refinement.tolerance_mult; 275 let refinement_tolerance = ε * config.refinement.tolerance_mult;
313
314 // println!(
315 // "keep_above: {keep_above}, rough lower bound: {}, tolerance: {ε}, slack: {slack}, τα: {τα}",
316 // d.bounds().lower()
317 // );
318 276
319 // If preliminary check indicates that we are in bounds, and if it otherwise matches 277 // If preliminary check indicates that we are in bounds, and if it otherwise matches
320 // the insertion strategy, skip insertion. 278 // the insertion strategy, skip insertion.
321 if skip_by_rough_check && d.bounds().lower() >= keep_above { 279 if skip_by_rough_check && d.bounds().lower() >= keep_above {
322 None 280 None
358 || d.has_lower_bound( 316 || d.has_lower_bound(
359 keep_above, 317 keep_above,
360 refinement_tolerance, 318 refinement_tolerance,
361 config.refinement.max_steps, 319 config.refinement.max_steps,
362 )); 320 ));
321 }
322
323 fn target_bounds(&self, τ: F, ε: F) -> Option<Bounds<F>> {
324 let τα = τ * self.α();
325 Some(Bounds(τα - ε, τα + ε))
326 }
327
328 fn tolerance_scaling(&self) -> F {
329 self.α()
330 }
331 }
332
333 #[replace_float_literals(F::cast_from(literal))]
334 impl<F: Float + ToNalgebraRealField, const N: usize> RadonSquaredRegTerm<Loc<N, F>, F>
335 for NonnegRadonRegTerm<F>
336 {
337 fn solve_oc_radonsq<M>(
338 &self,
339 μ: &mut DiscreteMeasure<Loc<N, F>, F>,
340 τv: &mut M,
341 τ: F,
342 ε: F,
343 tolerance_violation: Option<(Loc<N, F>, F, bool)>,
344 config: &InsertionConfig<F>,
345 stats: &mut IterInfo<F>,
346 ) where
347 M: Mapping<Loc<N, F>, Codomain = F>,
348 {
349 let τα = τ * self.α();
350 let mut g: Vec<_> = μ
351 .iter_locations()
352 .map(|ζ| F::to_nalgebra_mixed(-τv.apply(ζ)))
353 .collect();
354
355 let new_spike_initial_weight = if let Some((ξ, v_ξ, _in_bounds)) = tolerance_violation {
356 // Don't insert if existing spikes are almost as good
357 if g.iter().all(|minus_τv| {
358 -F::from_nalgebra_mixed(*minus_τv) > v_ξ + ε * config.refinement.tolerance_mult
359 }) {
360 // Weight is found out by running the finite-dimensional optimisation algorithm
361 // above
362 // NOTE: cannot set α here before y is extracted
363 *μ += DeltaMeasure { x: ξ, α: 0.0 /*-v_ξ - τα*/ };
364 g.push(F::to_nalgebra_mixed(-v_ξ));
365 Some(-v_ξ - τα)
366 } else {
367 None
368 }
369 } else {
370 None
371 };
372
373 // Optimise weights
374 if μ.len() > 0 {
375 // Form finite-dimensional subproblem. The subproblem references to the original μ^k
376 // from the beginning of the iteration are all contained in the immutable c and g.
377 // TODO: observe negation of -τv after switch from minus_τv: finite-dimensional
378 // problems have not yet been updated to sign change.
379 let y = μ.masses_dvector();
380 let mut x = y.clone();
381 let g_na = DVector::from_vec(g);
382 if let (Some(β), Some(dest)) = (new_spike_initial_weight, x.as_mut_slice().last_mut())
383 {
384 *dest = F::to_nalgebra_mixed(β);
385 }
386 // Solve finite-dimensional subproblem.
387 let inner_tolerance = ε * config.inner.tolerance_mult;
388 let inner_it = config.inner.iterator_options.stop_target(inner_tolerance);
389 stats.inner_iters +=
390 l1squared_nonneg(&y, &g_na, τα, 1.0, &mut x, &config.inner, inner_it);
391
392 // Update masses of μ based on solution of finite-dimensional subproblem.
393 μ.set_masses_dvector(&x);
394 }
363 } 395 }
364 396
365 fn verify_merge_candidate_radonsq<M>( 397 fn verify_merge_candidate_radonsq<M>(
366 &self, 398 &self,
367 d: &mut M, 399 d: &mut M,
405 refinement_tolerance, 437 refinement_tolerance,
406 config.refinement.max_steps, 438 config.refinement.max_steps,
407 ) 439 )
408 }; 440 };
409 } 441 }
410
411 fn target_bounds(&self, τ: F, ε: F) -> Option<Bounds<F>> {
412 let τα = τ * self.α();
413 Some(Bounds(τα - ε, τα + ε))
414 }
415
416 fn tolerance_scaling(&self) -> F {
417 self.α()
418 }
419 } 442 }
420 443
421 #[replace_float_literals(F::cast_from(literal))] 444 #[replace_float_literals(F::cast_from(literal))]
422 impl<F: Float + ToNalgebraRealField, const N: usize> SlidingRegTerm<Loc<N, F>, F> 445 impl<F: Float + ToNalgebraRealField, const N: usize> SlidingRegTerm<Loc<N, F>, F>
423 for NonnegRadonRegTerm<F> 446 for NonnegRadonRegTerm<F>
459 let inner_tolerance = ε * config.inner.tolerance_mult; 482 let inner_tolerance = ε * config.inner.tolerance_mult;
460 let inner_it = config.inner.iterator_options.stop_target(inner_tolerance); 483 let inner_it = config.inner.iterator_options.stop_target(inner_tolerance);
461 quadratic_unconstrained(mA, g, τ * self.α(), x, mA_normest, &config.inner, inner_it) 484 quadratic_unconstrained(mA, g, τ * self.α(), x, mA_normest, &config.inner, inner_it)
462 } 485 }
463 486
464 fn solve_findim_l1squared( 487 fn find_tolerance_violation<M>(
465 &self,
466 y: &DVector<F::MixedType>,
467 g: &DVector<F::MixedType>,
468 τ: F,
469 x: &mut DVector<F::MixedType>,
470 ε: F,
471 config: &InsertionConfig<F>,
472 ) -> usize {
473 let inner_tolerance = ε * config.inner.tolerance_mult;
474 let inner_it = config.inner.iterator_options.stop_target(inner_tolerance);
475 l1squared_unconstrained(y, g, τ * self.α(), 1.0, x, &config.inner, inner_it)
476 }
477
478 fn find_tolerance_violation_slack<M>(
479 &self, 488 &self,
480 d: &mut M, 489 d: &mut M,
481 τ: F, 490 τ: F,
482 ε: F, 491 ε: F,
483 skip_by_rough_check: bool, 492 skip_by_rough_check: bool,
484 config: &InsertionConfig<F>, 493 config: &InsertionConfig<F>,
485 slack: F,
486 ) -> Option<(Loc<N, F>, F, bool)> 494 ) -> Option<(Loc<N, F>, F, bool)>
487 where 495 where
488 M: MinMaxMapping<Loc<N, F>, F>, 496 M: MinMaxMapping<Loc<N, F>, F>,
489 { 497 {
490 let τα = τ * self.α(); 498 let τα = τ * self.α();
491 let keep_below = τα + slack + ε; 499 let keep_below = τα + ε;
492 let keep_above = -(τα + slack) - ε; 500 let keep_above = -τα - ε;
493 let maximise_above = τα + slack + ε * config.insertion_cutoff_factor; 501 let maximise_above = τα + ε * config.insertion_cutoff_factor;
494 let minimise_below = -(τα + slack) - ε * config.insertion_cutoff_factor; 502 let minimise_below = -τα - ε * config.insertion_cutoff_factor;
495 let refinement_tolerance = ε * config.refinement.tolerance_mult; 503 let refinement_tolerance = ε * config.refinement.tolerance_mult;
496 504
497 // If preliminary check indicates that we are in bonds, and if it otherwise matches 505 // If preliminary check indicates that we are in bonds, and if it otherwise matches
498 // the insertion strategy, skip insertion. 506 // the insertion strategy, skip insertion.
499 if skip_by_rough_check && Bounds(keep_above, keep_below).superset(&d.bounds()) { 507 if skip_by_rough_check && Bounds(keep_above, keep_below).superset(&d.bounds()) {
564 || d.has_lower_bound( 572 || d.has_lower_bound(
565 keep_above, 573 keep_above,
566 refinement_tolerance, 574 refinement_tolerance,
567 config.refinement.max_steps, 575 config.refinement.max_steps,
568 )); 576 ));
577 }
578
579 fn target_bounds(&self, τ: F, ε: F) -> Option<Bounds<F>> {
580 let τα = τ * self.α();
581 Some(Bounds(-τα - ε, τα + ε))
582 }
583
584 fn tolerance_scaling(&self) -> F {
585 self.α()
586 }
587 }
588
589 #[replace_float_literals(F::cast_from(literal))]
590 impl<F: Float + ToNalgebraRealField, const N: usize> RadonSquaredRegTerm<Loc<N, F>, F>
591 for RadonRegTerm<F>
592 {
593 fn solve_oc_radonsq<M>(
594 &self,
595 μ: &mut DiscreteMeasure<Loc<N, F>, F>,
596 τv: &mut M,
597 τ: F,
598 ε: F,
599 tolerance_violation: Option<(Loc<N, F>, F, bool)>,
600 config: &InsertionConfig<F>,
601 stats: &mut IterInfo<F>,
602 ) where
603 M: Mapping<Loc<N, F>, Codomain = F>,
604 {
605 let τα = τ * self.α();
606 let mut g: Vec<_> = μ
607 .iter_locations()
608 .map(|ζ| F::to_nalgebra_mixed(τv.apply(-ζ)))
609 .collect();
610
611 let new_spike_initial_weight = if let Some((ξ, v_ξ, _in_bounds)) = tolerance_violation {
612 // Don't insert if existing spikes are almost as good
613 let n = v_ξ.abs();
614 if g.iter().all(|minus_τv| {
615 F::from_nalgebra_mixed(*minus_τv).abs() < n - ε * config.refinement.tolerance_mult
616 }) {
617 // Weight is found out by running the finite-dimensional optimisation algorithm
618 // above
619 // NOTE: cannot initialise α before y is extracted.
620 *μ += DeltaMeasure { x: ξ, α: 0.0 /*-(n + τα) * v_ξ.signum()*/ };
621 g.push(F::to_nalgebra_mixed(-v_ξ));
622 Some(-(n + τα) * v_ξ.signum())
623 } else {
624 None
625 }
626 } else {
627 None
628 };
629
630 // Optimise weights
631 if μ.len() > 0 {
632 // Form finite-dimensional subproblem. The subproblem references to the original μ^k
633 // from the beginning of the iteration are all contained in the immutable c and g.
634 // TODO: observe negation of -τv after switch from minus_τv: finite-dimensional
635 // problems have not yet been updated to sign change.
636 let y = μ.masses_dvector();
637 let mut x = y.clone();
638 if let (Some(β), Some(dest)) = (new_spike_initial_weight, x.as_mut_slice().last_mut())
639 {
640 *dest = F::to_nalgebra_mixed(β);
641 }
642 let g_na = DVector::from_vec(g);
643 // Solve finite-dimensional subproblem.
644 let inner_tolerance = ε * config.inner.tolerance_mult;
645 let inner_it = config.inner.iterator_options.stop_target(inner_tolerance);
646 stats.inner_iters +=
647 l1squared_unconstrained(&y, &g_na, τα, 1.0, &mut x, &config.inner, inner_it);
648
649 // Update masses of μ based on solution of finite-dimensional subproblem.
650 μ.set_masses_dvector(&x);
651 }
569 } 652 }
570 653
571 fn verify_merge_candidate_radonsq<M>( 654 fn verify_merge_candidate_radonsq<M>(
572 &self, 655 &self,
573 d: &mut M, 656 d: &mut M,
617 refinement_tolerance, 700 refinement_tolerance,
618 config.refinement.max_steps, 701 config.refinement.max_steps,
619 ) 702 )
620 }; 703 };
621 } 704 }
622
623 fn target_bounds(&self, τ: F, ε: F) -> Option<Bounds<F>> {
624 let τα = τ * self.α();
625 Some(Bounds(-τα - ε, τα + ε))
626 }
627
628 fn tolerance_scaling(&self) -> F {
629 self.α()
630 }
631 } 705 }
632 706
633 #[replace_float_literals(F::cast_from(literal))] 707 #[replace_float_literals(F::cast_from(literal))]
634 impl<F: Float + ToNalgebraRealField, const N: usize> SlidingRegTerm<Loc<N, F>, F> 708 impl<F: Float + ToNalgebraRealField, const N: usize> SlidingRegTerm<Loc<N, F>, F>
635 for RadonRegTerm<F> 709 for RadonRegTerm<F>

mercurial