bpp-core  2.2.0
ParameterList.cpp
Go to the documentation of this file.
1 //
2 // File: ParameterList.cpp
3 // Created by: Julien Dutheil
4 // Created on: Wed Oct 15 18:17:29 2003
5 //
6 
7 /*
8  Copyright or © or Copr. Bio++ Development Team, (November 19, 2004)
9 
10  This software is a computer program whose purpose is to provide classes
11  for numerical calculus.
12 
13  This software is governed by the CeCILL license under French law and
14  abiding by the rules of distribution of free software. You can use,
15  modify and/ or redistribute the software under the terms of the CeCILL
16  license as circulated by CEA, CNRS and INRIA at the following URL
17  "http://www.cecill.info".
18 
19  As a counterpart to the access to the source code and rights to copy,
20  modify and redistribute granted by the license, users are provided only
21  with a limited warranty and the software's author, the holder of the
22  economic rights, and the successive licensors have only limited
23  liability.
24 
25  In this respect, the user's attention is drawn to the risks associated
26  with loading, using, modifying and/or developing or reproducing the
27  software by the user in light of its specific status of free software,
28  that may mean that it is complicated to manipulate, and that also
29  therefore means that it is reserved for developers and experienced
30  professionals having in-depth computer knowledge. Users are therefore
31  encouraged to load and test the software's suitability as regards their
32  requirements in conditions enabling the security of their systems and/or
33  data to be ensured and, more generally, to use and operate it in the
34  same conditions as regards security.
35 
36  The fact that you are presently reading this means that you have had
37  knowledge of the CeCILL license and that you accept its terms.
38  */
39 
40 #include "ParameterList.h"
41 #include "../Text/StringTokenizer.h"
42 
43 using namespace bpp;
44 
45 #include <iostream>
46 #include <algorithm>
47 using namespace std;
48 
52  parameters_(pl.size())
53 {
54  // Now copy all parameters:
55  for (unsigned int i = 0; i < size(); i++)
56  {
57  parameters_[i] = dynamic_cast<Parameter*>(pl.parameters_[i]->clone());
58  }
59 }
60 
64 {
65  // First delete all parameters:
66  reset();
67 
68  // Then resize the vector:
69  parameters_.resize(pl.size());
70 
71  // Now copy all parameters:
72  for (unsigned int i = 0; i < pl.size(); i++)
73  {
74  parameters_[i] = dynamic_cast<Parameter*>(pl.parameters_[i]->clone());
75  }
76 
77  return *this;
78 }
79 
83 {
84  // Delete all parameter objects.
85  reset();
86 }
87 
88 /******************************************************************************/
89 const Parameter& ParameterList::getParameter(const std::string& name) const throw (ParameterNotFoundException)
90 {
91  for (unsigned int i = 0; i < size(); i++)
92  {
93  const Parameter* p = parameters_[i];
94  if (p->getName() == name) return *p;
95  }
96  throw ParameterNotFoundException("ParameterList::getParameter('name').", name);
97 }
98 
99 /******************************************************************************/
100 double ParameterList::getParameterValue(const std::string& name) const throw (ParameterNotFoundException)
101 {
102  for (unsigned int i = 0; i < size(); i++)
103  {
104  const Parameter* p = parameters_[i];
105  if (p->getName() == name) return p->getValue();
106  }
107  throw ParameterNotFoundException("ParameterList::getParameterValue('name').", name);
108 }
109 
110 /******************************************************************************/
112 {
113  for (unsigned int i = 0; i < size(); i++)
114  {
115  Parameter* p = parameters_[i];
116  if (p->getName() == name) return *p;
117  }
118  throw ParameterNotFoundException("ParameterList::getParameter('name').", name);
119 }
120 
121 /******************************************************************************/
122 ParameterList ParameterList::subList(const std::vector<std::string>& names) const throw (ParameterNotFoundException)
123 {
124  ParameterList pl;
125  for (unsigned int i = 0; i < names.size(); i++)
126  {
127  Parameter param = getParameter(names[i]);
128  pl.addParameter(param);
129  }
130  return pl;
131 }
132 
133 /******************************************************************************/
135 {
136  ParameterList pl;
137  Parameter param = getParameter(name);
138  pl.addParameter(param);
139  return pl;
140 }
141 
142 /******************************************************************************/
143 ParameterList ParameterList::subList(const std::vector<size_t>& parameters) const
144 {
145  ParameterList pl;
146  for (unsigned int i = 0; i < parameters.size(); i++)
147  {
148  if (parameters[i] < size()) pl.parameters_.push_back(dynamic_cast<Parameter*>(parameters_[parameters[i]]->clone()));
149  }
150  return pl;
151 }
152 
153 /******************************************************************************/
154 ParameterList ParameterList::subList(size_t parameter) const
155 {
156  ParameterList pl;
157  if (parameter < size()) pl.parameters_.push_back(dynamic_cast<Parameter*>(parameters_[parameter]->clone()));
158  return pl;
159 }
160 
161 /******************************************************************************/
163 {
164  ParameterList pl;
165  for (unsigned int i = 0; i < params.size(); i++)
166  {
167  const Parameter& p = params[i];
168  if (hasParameter(p.getName()))
169  pl.parameters_.push_back(dynamic_cast<Parameter*>(p.clone()));
170  // We use push_back instead of addParameter because we are sure the name is not duplicated.
171  }
172 
173  return pl;
174 }
175 
176 /******************************************************************************/
177 
178 std::vector<std::string> ParameterList::getParameterNames() const
179 {
180  vector<string> pNames(size());
181  for (unsigned int i = 0; i < size(); i++)
182  {
183  pNames[i] = parameters_[i]->getName();
184  }
185  return pNames;
186 }
187 
188 /****************************************************************************/
189 
190 vector<string> ParameterList::getMatchingParameterNames(const string& pattern) const
191 {
192  vector<string> pNames;
193  for (unsigned int i = 0; i < size(); i++)
194  {
195  string name = parameters_[i]->getName();
196 
197  StringTokenizer stj(pattern, "*", true, false);
198  size_t pos1, pos2;
199  bool flag(true);
200  string g=stj.nextToken();
201  pos1=name.find(g);
202  if (pos1!=0)
203  flag=false;
204  pos1+=g.length();
205  while (flag && stj.hasMoreToken()){
206  g=stj.nextToken();
207  pos2=name.find(g,pos1);
208  if (pos2 == string::npos){
209  flag=false;
210  break;
211  }
212  pos1=pos2+g.length();
213  }
214  if (flag &&
215  ((g.length()==0) || (pos1==name.length()) || (name.rfind(g)==name.length()-g.length())))
216  pNames.push_back(name);
217  }
218 
219  return pNames;
220 }
221 
222 /******************************************************************************/
223 
225 {
226  if (hasParameter(param.getName()))
227  throw ParameterException("ParameterList::addParameter. Parameter with name '" + param.getName() + "' already exists.", &param);
228  parameters_.push_back(dynamic_cast<Parameter*>(param.clone()));
229 }
230 
231 /******************************************************************************/
232 
234 {
235  if (hasParameter(param->getName()))
236  throw ParameterException("ParameterList::addParameter. Parameter with name '" + param->getName() + "' already exists.", param);
237  parameters_.push_back(param);
238 }
239 
240 /******************************************************************************/
241 
242 void ParameterList::setParameter(size_t index, const Parameter& param) throw (IndexOutOfBoundsException)
243 {
244  if (index >= size()) throw IndexOutOfBoundsException("ParameterList::setParameter.", index, 0, size());
245  delete parameters_[index];
246  parameters_[index] = dynamic_cast<Parameter*>(param.clone());
247 }
248 
249 /******************************************************************************/
250 
252 {
253  for (unsigned int i = 0; i < params.size(); i++)
254  {
255  if (hasParameter(params[i].getName()))
256  setParameterValue(params[i].getName(), params[i].getValue());
257  else
258  parameters_.push_back(dynamic_cast<Parameter*>(params[i].clone()));
259  }
260 }
261 
262 /******************************************************************************/
263 
265 throw (ParameterException)
266 {
267  for (unsigned int i = 0; i < params.size(); i++)
268  {
269  addParameter(params[i]);
270  }
271 }
272 
273 /******************************************************************************/
274 
275 void ParameterList::setParameterValue(const string& name, double value)
277 {
278  Parameter* p = &getParameter(name);
279  p->setValue(value);
280 }
281 
282 /******************************************************************************/
283 
286 {
287  // First we check if all values are correct:
288  for (vector<Parameter*>::iterator it = parameters_.begin(); it < parameters_.end(); it++)
289  {
290  const Parameter* p = &params.getParameter((*it)->getName());
291  if ((*it)->hasConstraint() && !(*it)->getConstraint()->isCorrect(p->getValue()))
292  throw ConstraintException("ParameterList::setParametersValues()", *it, p->getValue());
293  }
294 
295  // If all values are ok, we set them:
296  for (vector<Parameter*>::iterator it = parameters_.begin(); it < parameters_.end(); it++)
297  {
298  const Parameter* p = &params.getParameter((*it)->getName());
299  (*it)->setValue(p->getValue());
300  }
301 }
302 
303 /******************************************************************************/
304 
306 {
307  // First we check if all values are correct:
308  for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
309  {
310  if (hasParameter((*it)->getName()))
311  {
312  Parameter* p = &getParameter((*it)->getName());
313  if (p->hasConstraint() && !p->getConstraint()->isCorrect((*it)->getValue()))
314  throw ConstraintException("ParameterList::setParametersValues()", p, (*it)->getValue());
315  }
316  }
317 
318  // If all values are ok, we set them:
319  {
320  for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
321  {
322  if (hasParameter((*it)->getName()))
323  {
324  Parameter* p = &getParameter((*it)->getName());
325  p->setValue((*it)->getValue());
326  }
327  }
328  }
329 }
330 
331 /******************************************************************************/
332 
334 {
335  // First we check if all values are correct:
336  for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
337  {
338  if (hasParameter((*it)->getName()))
339  {
340  const Parameter* p = &getParameter((*it)->getName());
341  if (p->hasConstraint() && !p->getConstraint()->isCorrect((*it)->getValue()))
342  throw ConstraintException("ParameterList::matchParametersValues()", p, (*it)->getValue());
343  }
344  }
345 
346  // If all values are ok, we test them:
347  bool ch = 0;
348 
349  for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
350  {
351  if (hasParameter((*it)->getName()))
352  {
353  const Parameter* p = &getParameter((*it)->getName());
354  if (p->getValue() != (*it)->getValue())
355  ch |= 1;
356  }
357  }
358  return ch;
359 }
360 
361 /******************************************************************************/
362 
363 bool ParameterList::matchParametersValues(const ParameterList& params, vector<size_t>* updatedParameters)
364 throw (ConstraintException)
365 {
366  // First we check if all values are correct:
367  for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
368  {
369  if (hasParameter((*it)->getName()))
370  {
371  Parameter* p = &getParameter((*it)->getName());
372  if (p->hasConstraint() && !p->getConstraint()->isCorrect((*it)->getValue()))
373  throw ConstraintException("ParameterList::matchParametersValues()", p, (*it)->getValue());
374  }
375  }
376 
377  // If all values are ok, we set them:
378  bool ch = 0;
379 
380  size_t pos = 0;
381  for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
382  {
383  if (hasParameter((*it)->getName()))
384  {
385  Parameter* p = &getParameter((*it)->getName());
386  if (p->getValue() != (*it)->getValue()) {
387  ch |= 1;
388  p->setValue((*it)->getValue());
389  if (updatedParameters)
390  updatedParameters->push_back(pos);
391  }
392  }
393  pos++;
394  }
395  return ch;
396 }
397 
398 /******************************************************************************/
401 {
402  for (vector<Parameter*>::iterator it = parameters_.begin(); it < parameters_.end(); it++)
403  {
404  const Parameter* p = &params.getParameter((*it)->getName());
405  **it = *p;
406  }
407 }
408 
409 /******************************************************************************/
412 {
413  for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
414  {
415  Parameter* p = &getParameter((*it)->getName());
416  *p = **it;
417  }
418 }
419 
420 /******************************************************************************/
421 bool ParameterList::hasParameter(const std::string& name) const
422 {
423  for (unsigned int i = 0; i < size(); i++)
424  {
425  const Parameter* p = parameters_[i];
426  if (p->getName() == name)
427  return true;
428  }
429  return false;
430 }
431 
432 /******************************************************************************/
434 {
435  for (vector<Parameter*>::const_iterator it = params.parameters_.begin(); it < params.parameters_.end(); it++)
436  {
437  if (hasParameter((*it)->getName()))
438  {
439  Parameter* p = &getParameter((*it)->getName());
440  *p = **it;
441  }
442  }
443 }
444 
445 /******************************************************************************/
446 void ParameterList::deleteParameter(const std::string& name) throw (ParameterNotFoundException)
447 {
448  for (unsigned int i = 0; i < size(); i++)
449  {
450  Parameter* p = parameters_[i];
451  if (p->getName() == name)
452  {
453  delete p;
454  parameters_.erase(parameters_.begin() + i);
455  return;
456  }
457  }
458  throw ParameterNotFoundException("ParameterList::deleteParameter", name);
459 }
460 
461 /******************************************************************************/
462 void ParameterList::deleteParameters(const std::vector<std::string>& names, bool mustExist) throw (ParameterNotFoundException)
463 {
464  for (unsigned int i = 0; i < names.size(); i++)
465  {
466  try
467  {
468  deleteParameter(names[i]);
469  }
470  catch (ParameterNotFoundException& e)
471  {
472  if (mustExist)
474  else
475  continue;
476  }
477  }
478 }
479 
480 /******************************************************************************/
482 {
483  if (index >= size()) throw IndexOutOfBoundsException("ParameterList::deleteParameter.", index, 0, size());
484  Parameter* p = parameters_[index];
485  delete p;
486  parameters_.erase(parameters_.begin() + static_cast<ptrdiff_t>(index));
487 }
488 
489 /******************************************************************************/
490 void ParameterList::deleteParameters(const std::vector<size_t>& indices) throw (IndexOutOfBoundsException)
491 {
492  vector<size_t> tmp(indices);
493  sort(tmp.begin(), tmp.end());
494  for (vector<size_t>::reverse_iterator i = tmp.rbegin(); i != tmp.rend(); i++)
495  {
496  size_t index = *i;
497  if (index >= size()) throw IndexOutOfBoundsException("ParameterList::deleteParameter.", index, 0, size());
498  Parameter* p = parameters_[index];
499  delete p;
500  parameters_.erase(parameters_.begin() + static_cast<ptrdiff_t>(index));
501  }
502 }
503 
504 /******************************************************************************/
505 size_t ParameterList::whichParameterHasName(const std::string& name) const throw (ParameterNotFoundException)
506 {
507  for (size_t i = 0; i < size(); i++)
508  {
509  if (parameters_[i]->getName() == name) return i;
510  }
511  throw ParameterNotFoundException("ParameterList::whichParameterHasName.", name);
512 }
513 
514 /******************************************************************************/
516 {
517  (out << "Name:\tValue:\tConstraint:").endLine();
518  (out << "_________________________________________________").endLine();
519  for (unsigned int i = 0; i < size(); i++)
520  {
521  out << parameters_[i]->getName();
522  out << "\t" << parameters_[i]->getValue();
523  out << (parameters_[i]->hasConstraint() ? "\t" + parameters_[i]->getConstraint()->getDescription() : string(""));
524  out.endLine();
525  }
526 }
527 
528 /******************************************************************************/
530 {
531  for (unsigned int i = 0; i < size(); i++)
532  {
533  delete parameters_[i];
534  }
535  parameters_.resize(0);
536 }
537 
538 /******************************************************************************/
539 
Exception thrown when a parameter is not found, for instance in a ParameterList.
virtual ~ParameterList()
virtual ParameterList subList(const std::vector< std::string > &names) const
Get given parameters as a sublist.
Parameter * clone() const
Create a copy of this object and send a pointer to it.
Definition: Parameter.h:199
virtual const Constraint * getConstraint() const
Return the constraint associated to this parameter if there is one.
Definition: Parameter.h:255
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 double getParameterValue(const std::string &name) const
Get the value of the parameter with name name.
This class allows to perform a correspondence analysis.
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:71
size_t size() const
Definition: ParameterList.h:90
STL namespace.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135
virtual void setAllParametersValues(const ParameterList &params)
Set the parameters to be equals to params.
const std::string & nextToken()
Get the next available token. If no token is availbale, throw an Exception.
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.
virtual bool isCorrect(double value) const =0
Tell if a given value is correct.
The parameter list object.
Definition: ParameterList.h:61
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:269
virtual void setValue(double value)
Set the value of this parameter.
Definition: Parameter.cpp:123
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:82
virtual bool hasParameter(const std::string &name) const
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:234
virtual OutputStream & endLine()=0
OutputStream interface.
Definition: OutputStream.h:64
virtual void setParameter(size_t index, const Parameter &param)
Change given parameter.
virtual void setParameters(const ParameterList &params)
Update the parameters from params.
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 const Parameter & getParameter(const std::string &name) const
Get the parameter with name name.
virtual double getValue() const
Get the value of this parameter.
Definition: Parameter.h:241
virtual void printParameters(OutputStream &out) const
Print all parameters.
std::vector< Parameter * > parameters_
Definition: ParameterList.h:65
Exception thrown when a value do not match a given constraint.
Index out of bounds exception class.
Definition: Exceptions.h:298
The parameter exception base class.
virtual void setParametersValues(const ParameterList &params)
Update the parameters from the ones in params that have matching names.