BioDynaMo  v1.05.159-3be850bb
parameter_sweep.cc
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #include <json.hpp>
16 
22 #include "core/simulation.h"
23 
24 using nlohmann::json;
25 
26 namespace bdm {
27 namespace experimental {
28 
30 struct ParameterSweep : public Algorithm {
32 
34  Param* default_params) override {
35  auto sweeping_params = default_params->Get<OptimizationParam>()->params;
36 
37  if (sweeping_params.empty()) {
38  Log::Error("ParameterSweep", "No sweeping parameters found!");
39  return;
40  }
41 
42  DynamicNestedLoop(sweeping_params, [&](const std::vector<uint32_t>& slots) {
43  json j_patch;
44 
45  int i = 0;
46  for (auto* param : sweeping_params) {
47  j_patch[param->GetGroupName()][param->GetParamName()] =
48  param->GetValue(slots[i]);
49  i++;
50  }
51 
52  Param final_params = *default_params;
53  final_params.MergeJsonPatch(j_patch.dump());
54 
55  dispatch_experiment(&final_params, nullptr);
56  });
57  };
58 };
59 
61 
62 } // namespace experimental
63 } // namespace bdm
static void Error(const std::string &location, const Args &... parts)
Prints error message.
Definition: log.h:79
void DynamicNestedLoop(const std::vector< OptimizationParamType * > &containers, const Lambda &action)
Definition: dynamic_loop.h:30
BDM_REGISTER_ALGO(ParameterSweep)
Definition: agent.cc:39
const TParamGroup * Get() const
Definition: param.h:59
void MergeJsonPatch(const std::string &patch)
Definition: param.cc:126
An interface for creating new optimization algorithms.
Definition: algorithm.h:30
Perform an exhaustive sweep across specified parameters.
void operator()(Functor< void, Param *, TimeSeries * > &dispatch_experiment, Param *default_params) override