BioDynaMo  v1.05.119-a4ff3934
experiment.h
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 #ifndef CORE_MULTI_SIMULATION_EXPERIMENT_H_
16 #define CORE_MULTI_SIMULATION_EXPERIMENT_H_
17 
18 #include <functional>
19 #include <vector>
20 
21 #include "TMath.h"
22 
24 #include "core/functor.h"
26 #include "core/param/param.h"
27 #include "core/real_t.h"
28 
29 namespace bdm {
30 namespace experimental {
31 
32 // Runs the given `simulation` for `iterations` amount of times` and computes
33 // the mean of the simulated results. If a real (experimental / analytical)
34 // dataset is presented (either as the argument or through a database), we
35 // compute the average error and return it
37  Functor<void, Param*, TimeSeries*>& simulation, size_t iterations,
38  const Param* param, TimeSeries* real_ts = nullptr,
39  Functor<void, const std::vector<TimeSeries>&, const TimeSeries&,
40  const TimeSeries&>* post_simulation = nullptr) {
41  // If no experimental / analytical data is given, we try to extract it from
42  // the database
43  bool use_real_data = true;
44  if (!real_ts) {
45  real_ts = Database::GetInstance()->data_;
46  // If also no real data is present in the database, we just run the
47  // simulation
48  if (!real_ts) {
49  use_real_data = false;
50  }
51  }
52 
53  // Run the simulation with the input parameters for N iterations
54  std::vector<TimeSeries> results(iterations);
55  for (size_t i = 0; i < iterations; i++) {
56  Param param_copy = *param;
57  simulation(&param_copy, &results[i]);
58  }
59 
60  // Compute the mean result values of the N iterations
61  TimeSeries simulated;
62  TimeSeries::Merge(&simulated, results,
63  [](const std::vector<real_t> all_y_values, real_t* y,
64  real_t* eh, real_t* el) {
65  *y =
66  TMath::Mean(all_y_values.begin(), all_y_values.end());
67  });
68 
69  // Execute post-simulation lambda (e.g. plotting or exporting of simulated
70  // data)
71  if (post_simulation) {
72  (*post_simulation)(results, simulated, *real_ts);
73  }
74 
75  if (use_real_data) {
76  // Compute and return the error between the real and simulated data
77  real_t err = TimeSeries::ComputeError(*real_ts, simulated);
78 
79  return err;
80  }
81  return 0.0;
82 }
83 
84 } // namespace experimental
85 } // namespace bdm
86 
87 #endif // CORE_MULTI_SIMULATION_EXPERIMENT_H_
bdm::experimental::Experiment
real_t Experiment(Functor< void, Param *, TimeSeries * > &simulation, size_t iterations, const Param *param, TimeSeries *real_ts=nullptr, Functor< void, const std::vector< TimeSeries > &, const TimeSeries &, const TimeSeries & > *post_simulation=nullptr)
Definition: experiment.h:36
bdm::experimental::TimeSeries::ComputeError
static real_t ComputeError(const TimeSeries &ts1, const TimeSeries &ts2)
Computes the mean squared error between ts1 and ts2
Definition: time_series.cc:143
bdm
Definition: agent.cc:39
bdm::real_t
double real_t
Definition: real_t.h:21
bdm::experimental::Database::GetInstance
static Database * GetInstance()
Definition: database.h:31
bdm::Functor
Definition: functor.h:24
time_series.h
bdm::experimental::Database::data_
TimeSeries * data_
Definition: database.h:29
bdm::experimental::TimeSeries
Definition: time_series.h:123
database.h
bdm::experimental::TimeSeries::Merge
static void Merge(TimeSeries *merged, const std::vector< TimeSeries > &time_series, const std::function< void(const std::vector< real_t > &, real_t *, real_t *, real_t *)> &merger)
Definition: time_series.cc:198
real_t.h
param.h
bdm::Param
Definition: param.h:35
functor.h