VPTissue Reference Manual
ParameterExploration.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "ParameterExploration.h"
21 
22 #include "Exploration.h"
23 #include "ListSweep.h"
24 #include "RangeSweep.h"
25 #include "SimTask.h"
26 #include "ptree/PTreeUtils.h"
27 
28 #include <iostream>
29 
31 using boost::property_tree::ptree;
32 using namespace std;
33 
34 namespace SimPT_Parex {
35 
36 ParameterExploration::ParameterExploration(const string& name,
37  const ptree& original, const ptree& preferences)
38  : Exploration(name, preferences), m_original(original)
39 {
40 }
41 
42 ParameterExploration::ParameterExploration(const ptree& pt)
43  : Exploration(pt)
44 {
45  ReadPtree(pt);
46 }
47 
48 ParameterExploration::ParameterExploration(const ParameterExploration &other)
49  : Exploration(other.m_name, other.m_preferences), m_original(other.m_original)
50 {
51  for (const auto& pair : other.m_sweeps) {
52  m_sweeps.push_back(make_pair(pair.first, pair.second->Clone()));
53  }
54 }
55 
57 {
58  if (this != &other) {
59  this->Exploration::operator=(other);
60 
61  m_original = other.m_original;
62 
63  for (const auto &pair : m_sweeps) {
64  delete pair.second;
65  }
66  m_sweeps.clear();
67 
68  for (const auto &pair : other.m_sweeps) {
69  m_sweeps.push_back(make_pair(pair.first, pair.second->Clone()));
70  }
71  }
72 
73  return *this;
74 }
75 
77 {
78  for (const auto &pair : m_sweeps) {
79  delete pair.second;
80  }
81 }
82 
84 {
85  return new ParameterExploration(*this);
86 }
87 
89 {
90  vector<string> parameters;
91  for (auto pair : m_sweeps) {
92  parameters.push_back(pair.first);
93  }
94  return parameters;
95 }
96 
97 vector<string> ParameterExploration::GetValues(unsigned int index) const
98 {
99  assert(index < GetNumberOfTasks() && "The given index doesn't exist.");
100 
101  vector<string> values(m_sweeps.size());
102 
103  unsigned int i = m_sweeps.size();
104  for (auto it = m_sweeps.rbegin(); it != m_sweeps.rend(); it++) {
105  ISweep* sweep = it->second;
106  unsigned int nrOfValues = sweep->GetNumberOfValues();
107 
108  values[--i] = sweep->GetValue(index % nrOfValues);
109  index /= nrOfValues;
110  }
111 
112  return values;
113 }
114 
116 {
117  unsigned int tasks = 1;
118  for (auto pair : m_sweeps) {
119  tasks *= pair.second->GetNumberOfValues();
120  }
121 
122  return tasks;
123 }
124 
125 SimTask* ParameterExploration::CreateTask(unsigned int index) const
126 {
127  assert(index < GetNumberOfTasks() && "The given index doesn't exist.");
128 
129  // Find the correct values per parameter.
130  vector<string> parameterValues = GetValues(index);
131 
132  // Create the exploration tree.
133  ptree pt(m_original);
134  auto& parameter_pt = pt.get_child("vleaf2.parameters");
135  auto value_it = parameterValues.begin();
136  for (auto it = m_sweeps.begin(); it != m_sweeps.end(); it++) {
137  PTreeUtils::PutIndexedChild(parameter_pt, it->first, ptree(*value_it));
138  value_it++;
139  }
140 
141  return new SimTask(index, pt, m_preferences, m_name);
142 }
143 
145 {
146  vector<string> parameters;
147  for (auto pair : PTreeUtils::Flatten(m_original.get_child("vleaf2.parameters"), true)) {
148  parameters.push_back(pair.first);
149  }
150  return parameters;
151 }
152 
153 string ParameterExploration::GetOriginalValue(const string& parameter) const
154 {
155  return PTreeUtils::GetIndexedChild(m_original, "vleaf2.parameters." + parameter).data();
156 }
157 
158 const ISweep* ParameterExploration::GetSweep(const string& parameter) const
159 {
160  auto entry = find_if(m_sweeps.begin(), m_sweeps.end(),
161  [parameter](const pair<string, ISweep*>& pair) {return pair.first == parameter;});
162  if (entry != m_sweeps.end()) {
163  return entry->second;
164  } else {
165  return nullptr;
166  }
167 }
168 
169 void ParameterExploration::SetSweep(const string& parameter, ISweep* sweep)
170 {
171  assert(sweep != nullptr && "The sweep wasn't initialized.");
172  assert(sweep->GetNumberOfValues() > 0 && "The sweep doesn't contain any value.");
173 
174  RemoveSweep(parameter);
175  m_sweeps.push_back(make_pair(parameter, sweep));
176 }
177 
178 void ParameterExploration::RemoveSweep(const string& parameter)
179 {
180  auto entry = find_if(m_sweeps.begin(), m_sweeps.end(),
181  [parameter](const pair<string, ISweep*>& pair) {return pair.first == parameter;});
182  if (entry != m_sweeps.end()) {
183  delete entry->second;
184  m_sweeps.erase(entry);
185  }
186 }
187 
188 boost::property_tree::ptree ParameterExploration::ToPtree() const
189 {
190  ptree pt = Exploration::ToPtree();
191  pt.put_child("original", m_original);
192 
193  for (const auto& entry : m_sweeps) {
194  ptree sweep_pt;
195  sweep_pt.put("parameter", entry.first);
196  for (auto pair : entry.second->ToPtree()) {
197  sweep_pt.push_back(pair);
198  }
199  pt.add_child("sweeps.sweep", sweep_pt);
200  }
201 
202  return pt;
203 }
204 
205 void ParameterExploration::ReadPtree(const boost::property_tree::ptree& pt)
206 {
207  m_original = pt.get_child("original");
208 
209  for (const auto& pair : m_sweeps) {
210  delete pair.second;
211  }
212  m_sweeps.clear();
213 
214  const auto &sweeps_pt = pt.get_child_optional("sweeps");
215  if (sweeps_pt) {
216  for (const auto& pair : sweeps_pt.get()) {
217  assert(pair.first == "sweep" && "Found ptree in sweeps with that isn't a sweep");
218  if (pair.first != "sweep") {
219  continue;
220  }
221 
222  const auto &sweep_pt = pair.second;
223  string parameter = sweep_pt.get<string>("parameter");
224 
225  if (sweep_pt.find("list") != sweep_pt.not_found()) {
226  m_sweeps.push_back(make_pair(parameter, new ListSweep(sweep_pt)));
227  }
228  else if (sweep_pt.find("range") != sweep_pt.not_found()) {
229  m_sweeps.push_back(make_pair(parameter, new RangeSweep(sweep_pt)));
230  }
231  else {
232  assert(false && "The sweep must be a list or range sweep.");
233  }
234  }
235  }
236 }
237 
238 } // namespace
virtual std::vector< std::string > GetPossibleParameters() const
Returns all the possible parameters for the exploration.
Interface for Exploration.
STL namespace.
static const boost::property_tree::ptree & GetIndexedChild(const boost::property_tree::ptree &pt, const std::string &path)
Returns a reference to the subptree in the given ptree, taking indexes of array-elements (of PTreeUti...
Definition: PTreeUtils.cpp:152
virtual unsigned int GetNumberOfTasks() const
Returns the number of tasks the exploration currently contains.
const ISweep * GetSweep(const std::string &parameter) const
Returns the sweep associated with the given parameter.
A collection of functions for manipulating the structure of ptrees.
Definition: PTreeUtils.h:35
ParameterExploration(const std::string &name, const boost::property_tree::ptree &original, const boost::property_tree::ptree &preferences)
Constructor.
virtual std::string GetValue(unsigned int index) const =0
Returns the value on the given index.
static void PutIndexedChild(boost::property_tree::ptree &pt, const std::string &path, const boost::property_tree::ptree &child)
Put the child in the given ptree on the given path.
Definition: PTreeUtils.cpp:193
PTreeUtils interface.
An interface class for a parameter sweep.
Definition: ISweep.h:31
virtual boost::property_tree::ptree ToPtree() const
Convert the exploration to a ptree.
Definition: Exploration.cpp:51
static std::list< std::pair< std::string, std::string > > Flatten(const boost::property_tree::ptree &pt, bool indexedArray=false, const std::string &path="")
Flattens a ptree from a given path.
Definition: PTreeUtils.cpp:130
virtual std::vector< std::string > GetParameters() const
Returns all the parameters in the exploration.
Interface for RangeSweep.
virtual boost::property_tree::ptree ToPtree() const
Convert the exploration to a ptree.
void SetSweep(const std::string &parameter, ISweep *sweep)
Set a sweep for the given parameter.
virtual unsigned int GetNumberOfValues() const =0
Returns the number of values in the sweep.
Exploration & operator=(const Exploration &other)
Assignment operator.
Definition: Exploration.cpp:41
virtual std::vector< std::string > GetValues(unsigned int index) const
Return the values of a certain task.
ParameterExploration & operator=(const ParameterExploration &other)
Assignment operator.
Interface for SimTask.
Interface for ListSweep.
Namespace for SimPT parameter explorer package.
Definition: Client.cpp:52
virtual SimTask * CreateTask(unsigned int index) const
Creates a task with the parameters on the given index.
Contains all information needed for a transmitable simulation task.
Definition: SimTask.h:31
std::string GetOriginalValue(const std::string &parameter) const
Returns the original value of the parameter.
Class describing a parameter exploration and its simulation tasks.
Class describing a generic exploration.
Definition: Exploration.h:33
void RemoveSweep(const std::string &parameter)
Removes the sweep (if any) for the given parameter.
Interface for ParameterExploration.
virtual ParameterExploration * Clone() const
Clone function.