LCOV - code coverage report
Current view: top level - src/samplers - GenericActiveLearningSampler.C (source / functions) Hit Total Coverage
Test: idaholab/moose stochastic_tools: #32971 (54bef8) with base c6cf66 Lines: 46 47 97.9 %
Date: 2026-05-29 20:40:35 Functions: 6 6 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //* This file is part of the MOOSE framework
       2             : //* https://www.mooseframework.org
       3             : //*
       4             : //* All rights reserved, see COPYRIGHT for full restrictions
       5             : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
       6             : //*
       7             : //* Licensed under LGPL 2.1, please see LICENSE for details
       8             : //* https://www.gnu.org/licenses/lgpl-2.1.html
       9             : 
      10             : #include "GenericActiveLearningSampler.h"
      11             : #include "Normal.h"
      12             : #include "Uniform.h"
      13             : 
      14             : registerMooseObject("StochasticToolsApp", GenericActiveLearningSampler);
      15             : 
      16             : InputParameters
      17          98 : GenericActiveLearningSampler::validParams()
      18             : {
      19          98 :   InputParameters params = Sampler::validParams();
      20          98 :   params.addClassDescription("A generic sampler to support parallel active learning.");
      21         196 :   params.addRequiredParam<unsigned int>(
      22             :       "num_parallel_proposals",
      23             :       "Number of proposals to make and corresponding subApps executed in "
      24             :       "parallel.");
      25         196 :   params.addRequiredParam<std::vector<DistributionName>>(
      26             :       "distributions",
      27             :       "The distribution names to be sampled, the number of distributions provided defines the "
      28             :       "number of columns per matrix.");
      29         196 :   params.addRequiredParam<ReporterName>(
      30             :       "sorted_indices", "The sorted sample indices in order of importance to evaluate the subApp.");
      31         196 :   params.addRequiredRangeCheckedParam<unsigned int>(
      32             :       "num_tries",
      33             :       "num_tries>0",
      34             :       "Number of samples to propose in each iteration (not all are sent for subApp evals).");
      35         196 :   params.addRequiredParam<std::vector<Real>>("initial_values",
      36             :                                              "The starting values of the inputs to be calibrated.");
      37         196 :   params.addParam<unsigned int>(
      38             :       "num_random_seeds",
      39         196 :       100000,
      40             :       "Initialize a certain number of random seeds. Change from the default only if you have to.");
      41          98 :   return params;
      42           0 : }
      43             : 
      44          54 : GenericActiveLearningSampler::GenericActiveLearningSampler(const InputParameters & parameters)
      45             :   : Sampler(parameters),
      46             :     TransientInterface(this),
      47         108 :     _num_parallel_proposals(getParam<unsigned int>("num_parallel_proposals")),
      48          54 :     _sorted_indices(getReporterValue<std::vector<unsigned int>>("sorted_indices")),
      49         108 :     _initial_values(getParam<std::vector<Real>>("initial_values")),
      50         162 :     _num_tries(getParam<unsigned int>("num_tries"))
      51             : {
      52             :   // Filling the `distributions` vector with the user-provided distributions.
      53         216 :   for (const DistributionName & name : getParam<std::vector<DistributionName>>("distributions"))
      54         108 :     _distributions.push_back(&getDistributionByName(name));
      55             : 
      56             :   // Setting the number of sampler rows to be equal to the number of parallel proposals
      57          54 :   setNumberOfRows(_num_parallel_proposals);
      58             : 
      59             :   // Setting the number of columns in the sampler matrix (equal to the number of distributions)
      60          54 :   setNumberOfCols(_distributions.size());
      61             : 
      62             :   // Setting the sizes for the different vectors enabling sampling and selection
      63          54 :   _inputs_all.resize(_num_tries, std::vector<Real>(_distributions.size(), 0.0));
      64          54 :   _new_samples.resize(_num_parallel_proposals);
      65             : 
      66         108 :   setNumberOfRandomSeeds(getParam<unsigned int>("num_random_seeds"));
      67          54 :   setAutoAdvanceGenerators(false);
      68          54 : }
      69             : 
      70             : const std::vector<std::vector<Real>> &
      71          44 : GenericActiveLearningSampler::getSampleTries() const
      72             : {
      73          44 :   return _inputs_all;
      74             : }
      75             : 
      76             : void
      77         222 : GenericActiveLearningSampler::executeSetUp()
      78             : {
      79         222 :   std::size_t rand_index = 0;
      80      222270 :   auto fill_vector = [&](std::vector<Real> & vector)
      81             :   {
      82      222270 :     vector.resize(getNumberOfCols());
      83      666810 :     for (const auto j : make_range(getNumberOfCols()))
      84      444540 :       vector[j] = _distributions[j]->quantile(getRand(rand_index++, _t_step + 1));
      85      222270 :   };
      86             : 
      87             :   /* If step is 1, randomly generate the samples.
      88             :   Else, generate the samples informed by the GP from the reporter "sorted_indices" */
      89        1332 :   for (const auto i : make_range(getNumberOfRows()))
      90             :   {
      91        1110 :     if (_t_step <= 0)
      92         270 :       fill_vector(_new_samples[i]);
      93             :     else
      94         840 :       _new_samples[i] = _inputs_all[_sorted_indices[i]];
      95             :   }
      96             : 
      97             :   /* Finally, generate several new samples randomly for the GP to try and pass it to the
      98             :   reporter */
      99      222222 :   for (const auto i : make_range(_num_tries))
     100      222000 :     fill_vector(_inputs_all[i]);
     101         222 : }
     102             : 
     103             : Real
     104        2120 : GenericActiveLearningSampler::computeSample(dof_id_type row_index, dof_id_type col_index)
     105             : {
     106        2120 :   if (_t_step < 1)
     107        1740 :     for (unsigned int i = 0; i < _num_parallel_proposals; ++i)
     108        1450 :       _new_samples[i] = _initial_values;
     109             : 
     110        2120 :   return _new_samples[row_index][col_index];
     111             : }

Generated by: LCOV version 1.14