bpp-core3  3.0.0
ParameterList.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include "../Text/StringTokenizer.h"
6 #include "ParameterList.h"
7 
8 using namespace bpp;
9 
10 #include <iostream>
11 #include <algorithm>
12 using namespace std;
13 
17  parameters_(pl.size())
18 {
19  // Now copy all parameters:
20  for (size_t i = 0; i < size(); ++i)
21  {
22  parameters_[i] = shared_ptr<Parameter>(pl.parameters_[i]->clone());
23  }
24 }
25 
29 {
30  // Then resize the vector:
31  parameters_.resize(pl.size());
32 
33  // Now copy all parameters:
34  for (size_t i = 0; i < pl.size(); ++i)
35  {
36  parameters_[i] = shared_ptr<Parameter>(pl.parameters_[i]->clone());
37  }
38 
39  return *this;
40 }
41 
45 {
46  // Delete all parameter objects.
47  reset();
48 }
49 
50 /******************************************************************************/
51 
52 const Parameter& ParameterList::parameter(const std::string& name) const
53 {
54  for (size_t i = 0; i < size(); i++)
55  {
56  const Parameter* p = parameters_[i].get();
57  if (p->getName() == name)
58  return *p;
59  }
60  throw ParameterNotFoundException("ParameterList::parameter('name').", name);
61 }
62 
63 /******************************************************************************/
64 
65 const shared_ptr<Parameter>& ParameterList::getParameter(const std::string& name) const
66 {
67  for (size_t i = 0; i < size(); i++)
68  {
69  const shared_ptr<Parameter>& p = parameters_[i];
70  if (p->getName() == name)
71  return p;
72  }
73  throw ParameterNotFoundException("ParameterList::getParameter('name').", name);
74 }
75 
76 
77 /******************************************************************************/
78 
79 double ParameterList::getParameterValue(const std::string& name) const
80 {
81  for (size_t i = 0; i < size(); i++)
82  {
83  const Parameter* p = parameters_[i].get();
84  if (p->getName() == name)
85  return p->getValue();
86  }
87  throw ParameterNotFoundException("ParameterList::getParameterValue('name').", name);
88 }
89 
90 /******************************************************************************/
91 
92 Parameter& ParameterList::parameter(const std::string& name)
93 {
94  for (size_t i = 0; i < size(); i++)
95  {
96  Parameter* p = parameters_[i].get();
97  if (p->getName() == name)
98  return *p;
99  }
100  throw ParameterNotFoundException("ParameterList::parameter('name').", name);
101 }
102 
103 /******************************************************************************/
104 
105 shared_ptr<Parameter>& ParameterList::getParameter(const std::string& name)
106 {
107  for (size_t i = 0; i < size(); i++)
108  {
109  shared_ptr<Parameter>& p = parameters_[i];
110  if (p->getName() == name)
111  return p;
112  }
113  throw ParameterNotFoundException("ParameterList::getParameter('name').", name);
114 }
115 
116 
117 /******************************************************************************/
118 
119 ParameterList ParameterList::createSubList(const std::vector<std::string>& names) const
120 {
121  ParameterList pl;
122  for (size_t i = 0; i < names.size(); i++)
123  {
124  Parameter param = parameter(names[i]);
125  pl.addParameter(param);
126  }
127  return pl;
128 }
129 
130 /******************************************************************************/
131 
132 ParameterList ParameterList::shareSubList(const std::vector<std::string>& names) const
133 {
134  ParameterList pl;
135  for (const auto& name:names)
136  {
137  pl.shareParameter(getParameter(name));
138  }
139 
140  return pl;
141 }
142 
143 /******************************************************************************/
144 
145 ParameterList ParameterList::createSubList(const std::string& name) const
146 {
147  ParameterList pl;
148  Parameter param = parameter(name);
149  pl.addParameter(param);
150  return pl;
151 }
152 
153 /******************************************************************************/
154 
155 ParameterList ParameterList::createSubList(const std::vector<size_t>& parameters) const
156 {
157  ParameterList pl;
158  for (auto iparam : parameters)
159  {
160  if (iparam < size())
161  pl.parameters_.push_back(shared_ptr<Parameter>(parameters_[iparam]->clone()));
162  }
163  return pl;
164 }
165 
166 /******************************************************************************/
167 
168 ParameterList ParameterList::shareSubList(const std::vector<size_t>& parameters) const
169 {
170  ParameterList pl;
171  for (auto iparam : parameters)
172  {
173  if (iparam < size())
174  pl.shareParameter(parameters_[iparam]);
175  }
176 
177  return pl;
178 }
179 
180 /******************************************************************************/
181 
183 {
184  ParameterList pl;
185  if (parameter < size())
186  pl.parameters_.push_back(shared_ptr<Parameter>(parameters_[parameter]->clone()));
187  return pl;
188 }
189 
190 /******************************************************************************/
191 
193 {
194  ParameterList pl;
195  for (size_t i = 0; i < params.size(); i++)
196  {
197  const Parameter& p = params[i];
198  if (hasParameter(p.getName()))
199  pl.parameters_.push_back(shared_ptr<Parameter>(p.clone()));
200  // We use push_back instead of addParameter because we are sure the name is not duplicated.
201  }
202 
203  return pl;
204 }
205 
206 /******************************************************************************/
207 
208 std::vector<std::string> ParameterList::getParameterNames() const
209 {
210  vector<string> pNames(size());
211  for (size_t i = 0; i < size(); i++)
212  {
213  pNames[i] = parameters_[i]->getName();
214  }
215  return pNames;
216 }
217 
218 /****************************************************************************/
219 
220 vector<string> ParameterList::getMatchingParameterNames(const string& pattern) const
221 {
222  vector<string> pNames;
223  for (size_t i = 0; i < size(); i++)
224  {
225  string name = parameters_[i]->getName();
226 
227  StringTokenizer stj(pattern, "*", true, false);
228  size_t pos1, pos2;
229  bool flag(true);
230  string g = stj.nextToken();
231  pos1 = name.find(g);
232  if (pos1 != 0)
233  flag = false;
234  pos1 += g.length();
235  while (flag && stj.hasMoreToken())
236  {
237  g = stj.nextToken();
238  pos2 = name.find(g, pos1);
239  if (pos2 == string::npos)
240  {
241  flag = false;
242  break;
243  }
244  pos1 = pos2 + g.length();
245  }
246  if (flag &&
247  ((g.length() == 0) || (pos1 == name.length()) || (name.rfind(g) == name.length() - g.length())))
248  pNames.push_back(name);
249  }
250 
251  return pNames;
252 }
253 
254 /******************************************************************************/
255 
257 {
258  if (hasParameter(param.getName()))
259  throw ParameterException("ParameterList::addParameter. Parameter with name '" + param.getName() + "' already exists.", &param);
260  parameters_.push_back(shared_ptr<Parameter>(param.clone()));
261 }
262 
263 /******************************************************************************/
264 
266 {
267  if (hasParameter(param->getName()))
268  throw ParameterException("ParameterList::addParameter. Parameter with name '" + param->getName() + "' already exists.", param);
269  parameters_.push_back(shared_ptr<Parameter>(param));
270 }
271 
272 /******************************************************************************/
273 
274 void ParameterList::shareParameter(const std::shared_ptr<Parameter>& param)
275 {
276  if (hasParameter(param->getName()))
277  setParameterValue(param->getName(), param->getValue());
278  else
279  parameters_.push_back(param);
280 }
281 
282 
283 /******************************************************************************/
284 
285 void ParameterList::setParameter(size_t index, const Parameter& param)
286 {
287  if (index >= size())
288  throw IndexOutOfBoundsException("ParameterList::setParameter.", index, 0, size());
289  parameters_[index] = shared_ptr<Parameter>(param.clone());
290 }
291 
292 
293 /******************************************************************************/
294 
296 {
297  for (size_t i = 0; i < params.size(); i++)
298  {
299  if (hasParameter(params[i].getName()))
300  setParameterValue(params[i].getName(), params[i].getValue());
301  else
302  parameters_.push_back(shared_ptr<Parameter>(params[i].clone()));
303  }
304 }
305 
306 /******************************************************************************/
307 
309 {
310  for (size_t i = 0; i < params.size(); i++)
311  {
312  addParameter(params[i]);
313  }
314 }
315 
316 /******************************************************************************/
317 
319 {
320  for (size_t i = 0; i < params.size(); i++)
321  {
322  shareParameter(params.getParameter(i));
323  }
324 }
325 
326 /******************************************************************************/
327 
328 void ParameterList::setParameterValue(const string& name, double value)
329 {
330  Parameter* p = &parameter(name);
331  p->setValue(value);
332 }
333 
334 /******************************************************************************/
335 
337 {
338  // First we check if all values are correct:
339  for (vector<shared_ptr<Parameter>>::iterator it = parameters_.begin(); it < parameters_.end(); it++)
340  {
341  const Parameter* p = &params.parameter((*it)->getName());
342  if ((*it)->hasConstraint() && !(*it)->getConstraint()->isCorrect(p->getValue()))
343  throw ConstraintException("ParameterList::setParametersValues()", (*it).get(), p->getValue());
344  }
345 
346  // If all values are ok, we set them:
347  for (vector<shared_ptr<Parameter>>::iterator it = parameters_.begin(); it < parameters_.end(); it++)
348  {
349  const Parameter* p = &params.parameter((*it)->getName());
350  (*it)->setValue(p->getValue());
351  }
352 }
353 
354 /******************************************************************************/
355 
357 {
358  // First we check if all values are correct:
359  for (vector<shared_ptr<Parameter>>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
360  {
361  if (hasParameter((*it)->getName()))
362  {
363  Parameter* p = &parameter((*it)->getName());
364  if (p->hasConstraint() && !p->getConstraint()->isCorrect((*it)->getValue()))
365  throw ConstraintException("ParameterList::setParametersValues()", p, (*it)->getValue());
366  }
367  }
368 
369  // If all values are ok, we set them:
370  {
371  for (vector<shared_ptr<Parameter>>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
372  {
373  if (hasParameter((*it)->getName()))
374  {
375  Parameter* p = &parameter((*it)->getName());
376  p->setValue((*it)->getValue());
377  }
378  }
379  }
380 }
381 
382 /******************************************************************************/
383 
385 {
386  // First we check if all values are correct:
387  for (vector<shared_ptr<Parameter>>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
388  {
389  if (hasParameter((*it)->getName()))
390  {
391  const Parameter* p = &parameter((*it)->getName());
392  if (p->hasConstraint() && !p->getConstraint()->isCorrect((*it)->getValue()))
393  throw ConstraintException("ParameterList::testParametersValues()", p, (*it)->getValue());
394  }
395  }
396 
397  // If all values are ok, we test them:
398  bool ch = 0;
399 
400  for (vector<shared_ptr<Parameter>>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
401  {
402  if (hasParameter((*it)->getName()))
403  {
404  const Parameter* p = &parameter((*it)->getName());
405  if (p->getValue() != (*it)->getValue())
406  ch |= 1;
407  }
408  }
409  return ch;
410 }
411 
412 /******************************************************************************/
413 
414 bool ParameterList::matchParametersValues(const ParameterList& params, vector<size_t>* updatedParameters)
415 {
416  // First we check if all values are correct:
417  for (vector<shared_ptr<Parameter>>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
418  {
419  if (hasParameter((*it)->getName()))
420  {
421  Parameter* p = &parameter((*it)->getName());
422  if (p->hasConstraint() && !p->getConstraint()->isCorrect((*it)->getValue()))
423  throw ConstraintException("ParameterList::matchParametersValues()", p, (*it)->getValue());
424  }
425  }
426 
427  // If all values are ok, we set them:
428  bool ch = 0;
429 
430  size_t pos = 0;
431  for (vector<shared_ptr<Parameter>>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
432  {
433  if (hasParameter((*it)->getName()))
434  {
435  Parameter* p = &parameter((*it)->getName());
436  if (p->getValue() != (*it)->getValue())
437  {
438  ch |= 1;
439  p->setValue((*it)->getValue());
440  if (updatedParameters)
441  updatedParameters->push_back(pos);
442  }
443  }
444  pos++;
445  }
446  return ch;
447 }
448 
449 /******************************************************************************/
451 {
452  for (vector<shared_ptr<Parameter>>::iterator it = parameters_.begin(); it < parameters_.end(); it++)
453  {
454  const Parameter* p = &params.parameter((*it)->getName());
455  **it = *p;
456  }
457 }
458 
459 /******************************************************************************/
461 {
462  for (vector<shared_ptr<Parameter>>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
463  {
464  Parameter* p = &parameter((*it)->getName());
465  *p = **it;
466  }
467 }
468 
469 /******************************************************************************/
470 bool ParameterList::hasParameter(const std::string& name) const
471 {
472  for (unsigned int i = 0; i < size(); i++)
473  {
474  const Parameter* p = parameters_[i].get();
475  if (p->getName() == name)
476  return true;
477  }
478  return false;
479 }
480 
481 /******************************************************************************/
483 {
484  for (vector<shared_ptr<Parameter>>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
485  {
486  if (hasParameter((*it)->getName()))
487  {
488  Parameter* p = &parameter((*it)->getName());
489  *p = **it;
490  }
491  }
492 }
493 
494 /******************************************************************************/
495 void ParameterList::deleteParameter(const std::string& name)
496 {
497  for (auto it = parameters_.begin(); it != parameters_.end(); ++it)
498  {
499  if ((*it)->getName() == name)
500  {
501  it = parameters_.erase(it);
502  return;
503  }
504  }
505  throw ParameterNotFoundException("ParameterList::deleteParameter", name);
506 }
507 
508 /******************************************************************************/
509 void ParameterList::deleteParameters(const std::vector<std::string>& names, bool mustExist)
510 {
511  for (auto it = names.begin(); it != names.end(); ++it)
512  {
513  try
514  {
515  deleteParameter(*it);
516  }
517  catch (ParameterNotFoundException& e)
518  {
519  if (mustExist)
521  else
522  continue;
523  }
524  }
525 }
526 
527 /******************************************************************************/
529 {
530  if (index >= size())
531  throw IndexOutOfBoundsException("ParameterList::deleteParameter.", index, 0, size());
532  parameters_.erase(parameters_.begin() + static_cast<ptrdiff_t>(index));
533 }
534 
535 /******************************************************************************/
536 void ParameterList::deleteParameters(const std::vector<size_t>& indices)
537 {
538  vector<size_t> tmp(indices);
539  sort(tmp.begin(), tmp.end());
540  for (vector<size_t>::reverse_iterator i = tmp.rbegin(); i != tmp.rend(); i++)
541  {
542  size_t index = *i;
543  if (index >= size())
544  throw IndexOutOfBoundsException("ParameterList::deleteParameter.", index, 0, size());
545 // Parameter* p = parameters_[index].get();
546 // delete p;
547  parameters_.erase(parameters_.begin() + static_cast<ptrdiff_t>(index));
548  }
549 }
550 
551 /******************************************************************************/
552 size_t ParameterList::whichParameterHasName(const std::string& name) const
553 {
554  for (size_t i = 0; i < size(); ++i)
555  {
556  if (parameters_[i]->getName() == name)
557  return i;
558  }
559  throw ParameterNotFoundException("ParameterList::whichParameterHasName.", name);
560 }
561 
562 /******************************************************************************/
564 {
565  (out << "Name:\tValue:\tConstraint:").endLine();
566  (out << "_________________________________________________").endLine();
567  for (unsigned int i = 0; i < size(); ++i)
568  {
569  out << parameters_[i]->getName();
570  out << "\t" << parameters_[i]->getValue();
571  out << (parameters_[i]->hasConstraint() ? "\t" + parameters_[i]->getConstraint()->getDescription() : string(""));
572  out.endLine();
573  }
574 }
575 
576 /******************************************************************************/
578 {
579  parameters_.resize(0);
580 }
581 
582 /******************************************************************************/
Exception thrown when a parameter is not found, for instance in a ParameterList.
virtual ~ParameterList()
virtual const Parameter & parameter(const std::string &name) const
Get the parameter with name name.
Parameter * clone() const
Create a copy of this object and send a pointer to it.
Definition: Parameter.h:140
A tokenizer for strings.
virtual void includeParameters(const ParameterList &params)
Add parameters to the list. If the parameter already exists, only the value is updated, otherwise the new parameter is added at the end of the list.
virtual bool testParametersValues(const ParameterList &params) const
Tests the parameters from params.
virtual void reset()
Reset the list: delete all parameters.
virtual std::vector< std::string > getMatchingParameterNames(const std::string &pattern) const
Get all parameter names matching with the given name. Up to now, only "*" jokers are available...
virtual ParameterList createSubList(const std::vector< std::string > &names) const
Get given parameters as a sublist.
virtual double getParameterValue(const std::string &name) const
Get the value of the parameter with name name.
const std::string & nextToken()
Get the next available token. If no token is availbale, throw an Exception.
virtual bool matchParametersValues(const ParameterList &params, std::vector< size_t > *updatedParameters=0)
Update the parameters from params.
bool hasMoreToken() const
Tell if some tokens are still available.
virtual void setAllParameters(const ParameterList &params)
Set the parameters to be equals to params.
virtual void deleteParameters(const std::vector< std::string > &names, bool mustExist=true)
Delete several parameters from the list.
ParameterList & operator=(const ParameterList &pl)
ParameterList()
Build a new ParameterList object.
Definition: ParameterList.h:37
virtual void shareParameter(const std::shared_ptr< Parameter > &param)
Share a parameter at the end of the list.
size_t size() const
Definition: ParameterList.h:56
STL namespace.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:97
virtual void setAllParametersValues(const ParameterList &params)
Set the parameters to be equals to params.
virtual void deleteParameter(const std::string &name)
Delete a parameter from the list.
virtual void setParameterValue(const std::string &name, double value)
Set the value of parameter with name name to be equal to value.
virtual ParameterList getCommonParametersWith(const ParameterList &params) const
Get the sublist containing all common parameter between this list and pl.
The parameter list object.
Definition: ParameterList.h:27
virtual void addParameters(const ParameterList &params)
Add new parameters at the end of the list.
virtual bool hasConstraint() const
Tells if this parameter has a constraint.
Definition: Parameter.h:238
virtual std::shared_ptr< const ConstraintInterface > getConstraint() const
Return the constraint associated to this parameter if there is one.
Definition: Parameter.h:196
virtual void setValue(double value)
Set the value of this parameter.
Definition: Parameter.cpp:55
virtual std::vector< std::string > getParameterNames() const
Get all parameter names in the list.
ParameterList * clone() const
Create a copy of this object and send a pointer to it.
Definition: ParameterList.h:48
virtual bool hasParameter(const std::string &name) const
virtual void shareParameters(const ParameterList &params)
Share parameters with a given list. They are added the end of this list.
virtual size_t whichParameterHasName(const std::string &name) const
Get the position of a given parameter according to its name.
virtual const std::string & getName() const
Get the name of this parameter.
Definition: Parameter.h:174
virtual OutputStream & endLine()=0
OutputStream interface.
Definition: OutputStream.h:29
virtual void setParameter(size_t index, const Parameter &param)
Change given parameter.
virtual void setParameters(const ParameterList &params)
Update the parameters from params.
virtual ParameterList shareSubList(const std::vector< std::string > &names) const
Share given parameters as a sublist.
virtual void addParameter(const Parameter &param)
Add a new parameter at the end of the list.
virtual void matchParameters(const ParameterList &params)
Update the parameters from params.
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:181
virtual void printParameters(OutputStream &out) const
Print all parameters.
Exception thrown when a value do not match a given constraint.
Index out of bounds exception class.
Definition: Exceptions.h:131
std::vector< std::shared_ptr< Parameter > > parameters_
Definition: ParameterList.h:31
The parameter exception base class.
virtual const std::shared_ptr< Parameter > & getParameter(size_t i) const
Definition: ParameterList.h:69
virtual void setParametersValues(const ParameterList &params)
Update the parameters from the ones in params that have matching names.