Commit 9e1680faed6ca40e5f8f212197a0bf627aad4650

Authored by Jürgen Knödlseder
1 parent f9774f2b

Complete GOptimizerPars container.

ChangeLog
1   -2013-12-13 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu>
  1 +2013-12-19 Juergen Knoedlseder <jurgen.knodlseder@irap.omp.eu>
2 2  
3 3 * Version 0.8.0 released
4 4 ========================
... ... @@ -19,6 +19,7 @@
19 19 Add support for variable-length FITS columns
20 20 Generalise GSkyPixel to 1D and 2D pixelisations
21 21 Added King profile PSF for CTA
  22 + Reorganise GOptimizerPars class and add GOptimizerPar class
22 23 Renamed the following classes:
23 24 - GWcs => GSkyProjection
24 25 - GWcslib => GWcs
... ...
... ... @@ -2,7 +2,7 @@ New Features and Important Changes in GammaLib 00-08-00
2 2  
3 3 Author(s) : Juergen Knoedlseder
4 4  
5   -13 December 2013
  5 +19 December 2013
6 6  
7 7  
8 8 1. Introduction
... ... @@ -467,6 +467,15 @@ initialization problem of static variables and allow using of the
467 467 GRegistryPointer smart pointer, removing memory leaks on exit of
468 468 GammaLib.
469 469  
  470 +The GModels class no longer derives from GOptimizerPars, but now is
  471 +a full-blown container deriving from GContainer. The pars() method
  472 +has been added that returns an optimizer parameter container that
  473 +holds all model parameters in a plain array.
  474 +
  475 +The GModelPar class now derives from GOptimizerPar. For the moment,
  476 +the only methods that GModelPar implements with respect to
  477 +GOptimizerPar are the XML read() and write() methods.
  478 +
470 479  
471 480 9. Numerics module
472 481 ------------------
... ... @@ -492,7 +501,12 @@ objects.
492 501  
493 502 11. Optimization module
494 503 -----------------------
495   -None
  504 +Introduce the GOptimizerPar class to represent a general parameter of
  505 +the optimizer function. The class was essentially copied from GModelPar.
  506 +GModelPar now derives from this class.
  507 +
  508 +The GOptimizerPars is now a full blown container class that holds
  509 +GOptimizerPar objects.
496 510  
497 511  
498 512 12. Sky module
... ...
include/GOptimizerPars.hpp
... ... @@ -29,6 +29,7 @@
29 29  
30 30 /* __ Includes ___________________________________________________________ */
31 31 #include <string>
  32 +#include <vector>
32 33 #include "GContainer.hpp"
33 34 #include "GOptimizerPar.hpp"
34 35  
... ... @@ -64,15 +65,26 @@ public:
64 65 const GOptimizerPar* operator[](const std::string& name) const;
65 66  
66 67 // Methods
67   - void clear(void);
68   - GOptimizerPars* clone(void) const;
69   - int size(void) const;
70   - bool isempty(void) const;
71   - int nfree(void) const;
72   - void attach(GOptimizerPar *par);
73   - void remove(const int& index);
74   - void reserve(const int& num);
75   - std::string print(const GChatter& chatter = NORMAL) const;
  68 + void clear(void);
  69 + GOptimizerPars* clone(void) const;
  70 + GOptimizerPar* at(const int& index);
  71 + const GOptimizerPar* at(const int& index) const;
  72 + int size(void) const;
  73 + bool isempty(void) const;
  74 + int nfree(void) const;
  75 + GOptimizerPar* set(const int& index, const GOptimizerPar& par);
  76 + GOptimizerPar* set(const std::string& name, const GOptimizerPar& par);
  77 + void attach(GOptimizerPar* par);
  78 + void attach(const int& index, GOptimizerPar* par);
  79 + void attach(const std::string& name, GOptimizerPar* par);
  80 + GOptimizerPar* insert(const int& index, const GOptimizerPar& par);
  81 + GOptimizerPar* insert(const std::string& name, const GOptimizerPar& par);
  82 + void remove(const int& index);
  83 + void remove(const std::string& name);
  84 + void reserve(const int& num);
  85 + void extend(const GOptimizerPars& pars);
  86 + bool contains(const std::string& name) const;
  87 + std::string print(const GChatter& chatter = NORMAL) const;
76 88  
77 89 protected:
78 90 // Protected methods
... ... @@ -88,11 +100,12 @@ protected:
88 100  
89 101  
90 102 /***********************************************************************//**
91   - * @brief Return pointer to model
  103 + * @brief Return pointer to parameter
92 104 *
93   - * @param[in] index Model index [0,...,size()-1].
  105 + * @param[in] index Parameter index [0,...,size()-1].
  106 + * @return Pointer to parameter.
94 107 *
95   - * Returns a pointer to the model with the specified @p index.
  108 + * Returns a pointer to the parameter with the specified @p index.
96 109 ***************************************************************************/
97 110 inline
98 111 GOptimizerPar* GOptimizerPars::operator[](const int& index)
... ... @@ -102,11 +115,12 @@ GOptimizerPar* GOptimizerPars::operator[](const int&amp; index)
102 115  
103 116  
104 117 /***********************************************************************//**
105   - * @brief Return pointer to model (const version)
  118 + * @brief Return pointer to parameter (const version)
106 119 *
107   - * @param[in] index Model index [0,...,size()-1].
  120 + * @param[in] index Parameter index [0,...,size()-1].
  121 + * @return Pointer to parameter.
108 122 *
109   - * Returns a const pointer to the model with the specified @p index.
  123 + * Returns a pointer to the parameter with the specified @p index.
110 124 ***************************************************************************/
111 125 inline
112 126 const GOptimizerPar* GOptimizerPars::operator[](const int& index) const
... ... @@ -116,11 +130,11 @@ const GOptimizerPar* GOptimizerPars::operator[](const int&amp; index) const
116 130  
117 131  
118 132 /***********************************************************************//**
119   - * @brief Return number of models in container
  133 + * @brief Return number of parameters in container
120 134 *
121   - * @return Number of models in container.
  135 + * @return Number of parameters in container.
122 136 *
123   - * Returns the number of models in the model container.
  137 + * Returns the number of parameters in the parameter container.
124 138 ***************************************************************************/
125 139 inline
126 140 int GOptimizerPars::size(void) const
... ... @@ -130,11 +144,11 @@ int GOptimizerPars::size(void) const
130 144  
131 145  
132 146 /***********************************************************************//**
133   - * @brief Signals if there are no models in container
  147 + * @brief Signals if there are no parameters in container
134 148 *
135 149 * @return True if container is empty, false otherwise.
136 150 *
137   - * Signals if the model container does not contain any model.
  151 + * Signals if the parameters container does not contain any parameter.
138 152 ***************************************************************************/
139 153 inline
140 154 bool GOptimizerPars::isempty(void) const
... ... @@ -144,11 +158,11 @@ bool GOptimizerPars::isempty(void) const
144 158  
145 159  
146 160 /***********************************************************************//**
147   - * @brief Reserves space for models in container
  161 + * @brief Reserves space for parameters in container
148 162 *
149   - * @param[in] num Number of models
  163 + * @param[in] num Number of parameters.
150 164 *
151   - * Reserves space for @p num models in the container.
  165 + * Reserves space for @p num parameters in the container.
152 166 ***************************************************************************/
153 167 inline
154 168 void GOptimizerPars::reserve(const int& num)
... ...
pyext/GOptimizerPars.i
... ... @@ -45,12 +45,22 @@ public:
45 45 // Methods
46 46 void clear(void);
47 47 GOptimizerPars* clone(void) const;
  48 + GOptimizerPar* at(const int& index);
48 49 int size(void) const;
49 50 bool isempty(void) const;
50 51 int nfree(void) const;
  52 + GOptimizerPar* set(const int& index, const GOptimizerPar& par);
  53 + GOptimizerPar* set(const std::string& name, const GOptimizerPar& par);
51 54 void attach(GOptimizerPar *par);
  55 + void attach(const int& index, GOptimizerPar* par);
  56 + void attach(const std::string& name, GOptimizerPar* par);
  57 + GOptimizerPar* insert(const int& index, const GOptimizerPar& par);
  58 + GOptimizerPar* insert(const std::string& name, const GOptimizerPar& par);
52 59 void remove(const int& index);
  60 + void remove(const std::string& name);
53 61 void reserve(const int& num);
  62 + void extend(const GOptimizerPars& pars);
  63 + bool contains(const std::string& name) const;
54 64 };
55 65  
56 66  
... ... @@ -70,10 +80,9 @@ public:
70 80 GOptimizerPar* __getitem__(const std::string& name) {
71 81 return (*self)[name];
72 82 }
73   -/*
74   - void __setitem__(const int& index, const GOptimizerPar& val) {
  83 + void __setitem__(const int& index, GOptimizerPar* par) {
75 84 if (index >= 0 && index < self->size()) {
76   - self->set(index, val);
  85 + self->attach(index, par);
77 86 return;
78 87 }
79 88 else {
... ... @@ -81,11 +90,10 @@ public:
81 90 index, self->size());
82 91 }
83 92 }
84   - void __setitem__(const std::string& name, const GOptimizerPar& val) {
85   - self->set(name, val);
  93 + void __setitem__(const std::string& name, GOptimizerPar* par) {
  94 + self->attach(name, par);
86 95 return;
87 96 }
88   -*/
89 97 GOptimizerPars copy() {
90 98 return (*self);
91 99 }
... ...
src/opt/GOptimizerPars.cpp
... ... @@ -34,7 +34,16 @@
34 34  
35 35 /* __ Method name definitions ____________________________________________ */
36 36 #define G_ACCESS "GOptimizerPars::operator[](std::string&)"
  37 +#define G_AT1 "GOptimizerPars::at(int&)"
  38 +#define G_AT2 "GOptimizerPars::at(std::string&)"
  39 +#define G_SET1 "GOptimizerPars::set(int&, GOptimizerPar&)"
  40 +#define G_SET2 "GOptimizerPars::set(std::string&, GOptimizerPar&)"
  41 +#define G_ATTACH1 "GOptimizerPars::attach(int&, GOptimizerPar*)"
  42 +#define G_ATTACH2 "GOptimizerPars::attach(std::string&, GOptimizerPar*)"
  43 +#define G_INSERT1 "GOptimizerPars::insert(int&, GOptimizerPar&)"
  44 +#define G_INSERT2 "GOptimizerPars::insert(std::string&, GOptimizerPar&)"
37 45 #define G_REMOVE1 "GOptimizerPars::remove(int&)"
  46 +#define G_REMOVE2 "GOptimizerPars::remove(std::string&)"
38 47  
39 48  
40 49 /* __ Macros _____________________________________________________________ */
... ... @@ -242,6 +251,56 @@ GOptimizerPars* GOptimizerPars::clone(void) const
242 251  
243 252  
244 253 /***********************************************************************//**
  254 + * @brief Return pointer to parameter
  255 + *
  256 + * @param[in] index Parameter index [0,...,size()-1].
  257 + * @return Pointer to parameter.
  258 + *
  259 + * @exception GException::out_of_range
  260 + * Extension number is out of range.
  261 + *
  262 + * Returns a pointer to the parameter with the specified @p index.
  263 + ***************************************************************************/
  264 +GOptimizerPar* GOptimizerPars::at(const int& index)
  265 +{
  266 + // Compile option: raise an exception if index is out of range
  267 + #if defined(G_RANGE_CHECK)
  268 + if (index < 0 || index >= size()) {
  269 + throw GException::out_of_range(G_AT1, "Parameter index", index, size());
  270 + }
  271 + #endif
  272 +
  273 + // Return pointer
  274 + return (m_pars[index]);
  275 +}
  276 +
  277 +
  278 +/***********************************************************************//**
  279 + * @brief Return pointer to parameter (const version)
  280 + *
  281 + * @param[in] index Parameter index [0,...,size()-1].
  282 + * @return Pointer to parameter.
  283 + *
  284 + * @exception GException::out_of_range
  285 + * Extension number is out of range.
  286 + *
  287 + * Returns a pointer to the parameter with the specified @p index.
  288 + ***************************************************************************/
  289 +const GOptimizerPar* GOptimizerPars::at(const int& index) const
  290 +{
  291 + // Compile option: raise an exception if index is out of range
  292 + #if defined(G_RANGE_CHECK)
  293 + if (index < 0 || index >= size()) {
  294 + throw GException::out_of_range(G_AT1, "Parameter index", index, size());
  295 + }
  296 + #endif
  297 +
  298 + // Return pointer
  299 + return (m_pars[index]);
  300 +}
  301 +
  302 +
  303 +/***********************************************************************//**
245 304 * @brief Return number of free parameters
246 305 *
247 306 * @return Number of free parameters.
... ... @@ -267,11 +326,77 @@ int GOptimizerPars::nfree(void) const
267 326  
268 327  
269 328 /***********************************************************************//**
  329 + * @brief Set parameter in container
  330 + *
  331 + * @param[in] index Parameter index [0,...,size()-1].
  332 + * @param[in] par Parameter.
  333 + * @return Pointer to deep copy of parameter.
  334 + *
  335 + * @exception GException::out_of_range
  336 + * Parameter index is out of range.
  337 + *
  338 + * Set parameter in the container. A deep copy of the parameter will be made.
  339 + ***************************************************************************/
  340 +GOptimizerPar* GOptimizerPars::set(const int& index, const GOptimizerPar& par)
  341 +{
  342 + // Compile option: raise an exception if index is out of range
  343 + #if defined(G_RANGE_CHECK)
  344 + if (index < 0 || index >= size()) {
  345 + throw GException::out_of_range(G_SET1, "Parameter index", index, size());
  346 + }
  347 + #endif
  348 +
  349 + // Delete parameter if it has been allocated
  350 + if (m_alloc[index]) {
  351 + delete m_pars[index];
  352 + }
  353 +
  354 + // Clone parameter
  355 + GOptimizerPar* ptr = par.clone();
  356 +
  357 + // Set parameter
  358 + m_alloc[index] = true;
  359 + m_pars[index] = ptr;
  360 +
  361 + // Return parameter pointer
  362 + return ptr;
  363 +}
  364 +
  365 +
  366 +/***********************************************************************//**
  367 + * @brief Set parameter in container
  368 + *
  369 + * @param[in] name Parameter name.
  370 + * @param[in] par Parameter.
  371 + * @return Pointer to deep copy of parameter.
  372 + *
  373 + * @exception GException::invalid_argument
  374 + * Specified parameter @p name not found in container.
  375 + ***************************************************************************/
  376 +GOptimizerPar* GOptimizerPars::set(const std::string& name, const GOptimizerPar& par)
  377 +{
  378 + // Get parameter index
  379 + int index = get_index(name);
  380 +
  381 + // Throw exception if model name was not found
  382 + if (index == -1) {
  383 + std::string msg = "Parameter \""+name+"\" not found in parameter"
  384 + " container.\nPlease specify a valid parameter"
  385 + " name.";
  386 + throw GException::invalid_argument(G_SET2, msg);
  387 + }
  388 +
  389 + // Set parameter
  390 + return set(index, par);
  391 +}
  392 +
  393 +
  394 +/***********************************************************************//**
270 395 * @brief Attach parameter to container
271 396 *
272 397 * @param[in] par Parameter pointer.
273 398 ***************************************************************************/
274   -void GOptimizerPars::attach(GOptimizerPar *par)
  399 +void GOptimizerPars::attach(GOptimizerPar* par)
275 400 {
276 401 // Push pointer in vector if it is valid
277 402 if (par != NULL) {
... ... @@ -285,6 +410,134 @@ void GOptimizerPars::attach(GOptimizerPar *par)
285 410  
286 411  
287 412 /***********************************************************************//**
  413 + * @brief Attach parameter to container at the specified index
  414 + *
  415 + * @param[in] index Parameter index [0,...,size()-1].
  416 + * @param[in] par Parameter pointer.
  417 + *
  418 + * @exception GException::out_of_range
  419 + * Parameter @p index is not valid.
  420 + *
  421 + * Attaches parameter pointer at the specified @p index.
  422 + ***************************************************************************/
  423 +void GOptimizerPars::attach(const int& index, GOptimizerPar* par)
  424 +{
  425 + // Compile option: raise an exception if index is out of range
  426 + #if defined(G_RANGE_CHECK)
  427 + if (index < 0 || index >= size()) {
  428 + throw GException::out_of_range(G_ATTACH1, "Parameter index", index, size());
  429 + }
  430 + #endif
  431 +
  432 + // Delete parameter if it has been allocated
  433 + if (m_alloc[index]) {
  434 + delete m_pars[index];
  435 + }
  436 +
  437 + // Set parameter
  438 + m_alloc[index] = false;
  439 + m_pars[index] = par;
  440 +
  441 + // Return
  442 + return;
  443 +}
  444 +
  445 +
  446 +/***********************************************************************//**
  447 + * @brief Attach parameter to container at the specified index
  448 + *
  449 + * @param[in] name Parameter name.
  450 + * @param[in] par Parameter pointer.
  451 + *
  452 + * @exception GException::out_of_range
  453 + * Parameter @p index is not valid.
  454 + *
  455 + * Attaches parameter pointer at the specified @p index.
  456 + ***************************************************************************/
  457 +void GOptimizerPars::attach(const std::string& name, GOptimizerPar* par)
  458 +{
  459 + // Get parameter index
  460 + int index = get_index(name);
  461 +
  462 + // Throw exception if model name was not found
  463 + if (index == -1) {
  464 + std::string msg = "Parameter \""+name+"\" not found in parameter"
  465 + " container.\nPlease specify a valid parameter"
  466 + " name.";
  467 + throw GException::invalid_argument(G_ATTACH2, msg);
  468 + }
  469 +
  470 + // Remove parameter
  471 + attach(index, par);
  472 +
  473 + // Return
  474 + return;
  475 +}
  476 +
  477 +
  478 +/***********************************************************************//**
  479 + * @brief Insert parameter into container
  480 + *
  481 + * @param[in] index Parameter index [0,...,size()-1].
  482 + * @param[in] par Parameter.
  483 + * @return Pointer to deep copy of parameter.
  484 + *
  485 + * @exception GException::out_of_range
  486 + * Parameter index is out of range.
  487 + *
  488 + * Set parameter in the container. A deep copy of the parameter will be made.
  489 + ***************************************************************************/
  490 +GOptimizerPar* GOptimizerPars::insert(const int& index, const GOptimizerPar& par)
  491 +{
  492 + // Compile option: raise an exception if index is out of range
  493 + #if defined(G_RANGE_CHECK)
  494 + if (index < 0 || index >= size()) {
  495 + throw GException::out_of_range(G_INSERT1, "Parameter index", index, size());
  496 + }
  497 + #endif
  498 +
  499 + // Clone parameter
  500 + GOptimizerPar* ptr = par.clone();
  501 +
  502 + // Inserts deep copy of parameter
  503 + m_alloc.insert(m_alloc.begin()+index, true);
  504 + m_pars.insert(m_pars.begin()+index, ptr);
  505 +
  506 + // Return parameter pointer
  507 + return ptr;
  508 +}
  509 +
  510 +
  511 +/***********************************************************************//**
  512 + * @brief Insert parameter into container
  513 + *
  514 + * @param[in] name Parameter name.
  515 + * @param[in] par Parameter.
  516 + * @return Pointer to deep copy of parameter.
  517 + *
  518 + * @exception GException::invalid_argument
  519 + * Specified parameter @p name not found in container.
  520 + ***************************************************************************/
  521 +GOptimizerPar* GOptimizerPars::insert(const std::string& name,
  522 + const GOptimizerPar& par)
  523 +{
  524 + // Get parameter index
  525 + int index = get_index(name);
  526 +
  527 + // Throw exception if model name was not found
  528 + if (index == -1) {
  529 + std::string msg = "Parameter \""+name+"\" not found in parameter"
  530 + " container.\nPlease specify a valid parameter"
  531 + " name.";
  532 + throw GException::invalid_argument(G_INSERT2, msg);
  533 + }
  534 +
  535 + // Insert parameter
  536 + return insert(index, par);
  537 +}
  538 +
  539 +
  540 +/***********************************************************************//**
288 541 * @brief Remove parameter from container
289 542 *
290 543 * @param[in] index Parameter index [0,...,size()-1].
... ... @@ -319,6 +572,90 @@ void GOptimizerPars::remove(const int&amp; index)
319 572  
320 573  
321 574 /***********************************************************************//**
  575 + * @brief Remove parameter from container
  576 + *
  577 + * @param[in] name Parameter name.
  578 + *
  579 + * @exception GException::invalid_argument
  580 + * Specified parameter @p name not found in container.
  581 + ***************************************************************************/
  582 +void GOptimizerPars::remove(const std::string& name)
  583 +{
  584 + // Get parameter index
  585 + int index = get_index(name);
  586 +
  587 + // Throw exception if model name was not found
  588 + if (index == -1) {
  589 + std::string msg = "Parameter \""+name+"\" not found in parameter"
  590 + " container.\nPlease specify a valid parameter"
  591 + " name.";
  592 + throw GException::invalid_argument(G_REMOVE2, msg);
  593 + }
  594 +
  595 + // Remove parameter
  596 + remove(index);
  597 +
  598 + // Return
  599 + return;
  600 +}
  601 +
  602 +
  603 +/***********************************************************************//**
  604 + * @brief Append parameter container
  605 + *
  606 + * @param[in] pars Parameter container.
  607 + *
  608 + * Append parameter container to the container. All parameters that were
  609 + * allocated within @p pars will be clones, for the other parameters, the
  610 + * pointers will be copied.
  611 + ***************************************************************************/
  612 +void GOptimizerPars::extend(const GOptimizerPars& pars)
  613 +{
  614 + // Do nothing if parameter container is empty
  615 + if (!pars.isempty()) {
  616 +
  617 + // Get size. Note that we extract the size first to avoid an
  618 + // endless loop that arises when a container is appended to
  619 + // itself.
  620 + int num = pars.size();
  621 +
  622 + // Reserve enough space
  623 + reserve(size() + num);
  624 +
  625 + // Loop over all parameters
  626 + for (int i = 0; i < num; ++i) {
  627 + GOptimizerPar* par = (pars.m_alloc[i]) ? pars.m_pars[i]->clone()
  628 + : pars.m_pars[i];
  629 + m_pars.push_back(par);
  630 + }
  631 +
  632 + } // endif: parameter container was not empty
  633 +
  634 + // Return
  635 + return;
  636 +}
  637 +
  638 +
  639 +/***********************************************************************//**
  640 + * @brief Signals if parameter name exists
  641 + *
  642 + * @param[in] name Parameter name.
  643 + * @return True if parameter with specified @p name exists.
  644 + *
  645 + * Searches all parameter names for a match with the specified @p name. If
  646 + * the specified name has been found, true is returned.
  647 + ***************************************************************************/
  648 +bool GOptimizerPars::contains(const std::string& name) const
  649 +{
  650 + // Get model index
  651 + int index = get_index(name);
  652 +
  653 + // Return
  654 + return (index != -1);
  655 +}
  656 +
  657 +
  658 +/***********************************************************************//**
322 659 * @brief Print parameters
323 660 *
324 661 * @param[in] chatter Chattiness (defaults to NORMAL).
... ...