variable_settings.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
35 
36 
37 
39 
40 
41 
42 //
43 // PUBLIC MEMBER FUNCTIONS:
44 //
45 
47 {
48  dim = 0;
49  type = VT_UNKNOWN;
50 
51  names = 0;
52  units = 0;
53 
55 }
56 
57 
59  VariableType _type,
60  const char** const _names,
61  const char** const _units,
62  const DVector& _scaling,
63  const DVector& _lb,
64  const DVector& _ub,
65  BooleanType _autoInit
66  )
67 {
68  names = 0;
69  units = 0;
70 
71  init( _dim,_type,_names,_units,_scaling,_lb,_ub,_autoInit );
72 }
73 
74 
76 {
77  uint i;
78 
79  dim = rhs.dim;
80  type = rhs.type;
81 
82  if ( rhs.names != 0 )
83  {
84  names = (char**) calloc( dim,sizeof(char*) );
85  for( i=0; i<dim; ++i )
86  {
87  names[i] = new char[MAX_LENGTH_NAME+1];
88  setName( i,rhs.names[i] );
89  }
90  }
91  else
92  {
93  names = 0;
94  }
95 
96  if ( rhs.units != 0 )
97  {
98  units = (char**) calloc( dim,sizeof(char*) );
99  for( i=0; i<dim; ++i )
100  {
101  units[i] = new char[MAX_LENGTH_NAME+1];
102  setUnit( i,rhs.units[i] );
103  }
104  }
105  else
106  {
107  units = 0;
108  }
109 
110 
111  if ( rhs.scaling.isEmpty( ) == BT_FALSE )
112  {
113  scaling.init( dim );
114  setScaling( rhs.scaling );
115  }
116  else
117  {
118  scaling.init( );
119  }
120 
121  if ( rhs.lb.isEmpty( ) == BT_FALSE )
122  {
123  lb.init( dim );
124  setLowerBounds( rhs.lb );
125  }
126  else
127  {
128  lb.init( );
129  }
130 
131  if ( rhs.ub.isEmpty( ) == BT_FALSE )
132  {
133  ub.init( dim );
134  setUpperBounds( rhs.ub );
135  }
136  else
137  {
138  ub.init( );
139  }
140 
141  autoInit = rhs.autoInit;
142 }
143 
144 
146 {
147  clear( );
148 }
149 
150 
152 {
153  uint i;
154 
155  if ( this != &rhs )
156  {
157  clear( );
158 
159  dim = rhs.dim;
160  type = rhs.type;
161 
162  if ( rhs.names != 0 )
163  {
164  names = (char**) calloc( dim,sizeof(char*) );
165  for( i=0; i<dim; ++i )
166  {
167  names[i] = new char[MAX_LENGTH_NAME+1];
168  setName( i,rhs.names[i] );
169  }
170  }
171  else
172  {
173  names = 0;
174  }
175 
176  if ( rhs.units != 0 )
177  {
178  units = (char**) calloc( dim,sizeof(char*) );
179  for( i=0; i<dim; ++i )
180  {
181  units[i] = new char[MAX_LENGTH_NAME+1];
182  setUnit( i,rhs.units[i] );
183  }
184  }
185  else
186  {
187  units = 0;
188  }
189 
190  if ( rhs.scaling.isEmpty( ) == BT_FALSE )
191  {
192  scaling.init( dim );
193  setScaling( rhs.scaling );
194  }
195  else
196  {
197  scaling.init( );
198  }
199 
200  if ( rhs.lb.isEmpty( ) == BT_FALSE )
201  {
202  lb.init( dim );
203  setLowerBounds( rhs.lb );
204  }
205  else
206  {
207  lb.init( );
208  }
209 
210  if ( rhs.ub.isEmpty( ) == BT_FALSE )
211  {
212  ub.init( dim );
213  setUpperBounds( rhs.ub );
214  }
215  else
216  {
217  ub.init( );
218  }
219 
220  autoInit = rhs.autoInit;
221  }
222 
223  return *this;
224 }
225 
226 
227 
229 {
230  clear( );
231 
232  dim = 0;
233  type = VT_UNKNOWN;
234 
235  names = 0;
236  units = 0;
237 
239 
240  return SUCCESSFUL_RETURN;
241 }
242 
243 
245  VariableType _type,
246  const char** const _names,
247  const char** const _units,
248  const DVector& _scaling,
249  const DVector& _lb,
250  const DVector& _ub,
251  BooleanType _autoInit
252  )
253 {
254  init( );
255 
256  type = _type;
257  autoInit = _autoInit;
258 
259  return appendSettings( _dim,_names,_units,_scaling,_lb,_ub );
260 }
261 
262 
263 
265  )
266 {
267  return appendSettings( rhs.dim,(const char**)rhs.names,(const char**)rhs.units,rhs.scaling,rhs.lb,rhs.ub );
268 }
269 
270 
272  const char** const _names,
273  const char** const _units,
274  const DVector& _scaling,
275  const DVector& _lb,
276  const DVector& _ub
277  )
278 {
279  uint i;
280 
281  uint newDim = dim + _dim;
282 
283  if ( names != 0 )
284  {
285  names = (char**) realloc( names,newDim*sizeof(char*) );
286 
287  if ( _names != 0 )
288  {
289  for( i=0; i<_dim; ++i )
290  {
291  names[dim+i] = new char[MAX_LENGTH_NAME+1];
292  setName( dim+i,_names[i] );
293  }
294  }
295  else
296  {
297  for( i=0; i<_dim; ++i )
298  {
299  names[dim+i] = new char[MAX_LENGTH_NAME+1];
300  setName( dim+i,defaultName );
301  }
302  }
303  }
304  else
305  {
306  if ( _names != 0 )
307  {
308  names = (char**) realloc( names,newDim*sizeof(char*) );
309 
310  for( i=0; i<dim; ++i )
311  {
312  names[i] = new char[MAX_LENGTH_NAME+1];
313  setName( i,defaultName );
314  }
315 
316  for( i=0; i<_dim; ++i )
317  {
318  names[dim+i] = new char[MAX_LENGTH_NAME+1];
319  setName( dim+i,_names[i] );
320  }
321  }
322  }
323 
324  if ( units != 0 )
325  {
326  units = (char**) realloc( units,newDim*sizeof(char*) );
327 
328  if ( _units != 0 )
329  {
330  for( i=0; i<_dim; ++i )
331  {
332  units[dim+i] = new char[MAX_LENGTH_NAME+1];
333  setUnit( dim+i,_units[i] );
334  }
335  }
336  else
337  {
338  for( i=0; i<_dim; ++i )
339  {
340  units[dim+i] = new char[MAX_LENGTH_NAME+1];
341  setUnit( dim+i,defaultUnit );
342  }
343  }
344  }
345  else
346  {
347  if ( _units != 0 )
348  {
349  units = (char**) realloc( units,newDim*sizeof(char*) );
350 
351  for( i=0; i<dim; ++i )
352  {
353  units[i] = new char[MAX_LENGTH_NAME+1];
354  setUnit( i,defaultUnit );
355  }
356 
357  for( i=0; i<_dim; ++i )
358  {
359  units[dim+i] = new char[MAX_LENGTH_NAME+1];
360  setUnit( dim+i,_units[i] );
361  }
362  }
363  }
364 
365  if ( scaling.isEmpty( ) == BT_FALSE )
366  {
367  if ( _scaling.isEmpty( ) == BT_FALSE )
368  {
369  scaling.append( _scaling );
370  }
371  else
372  {
373  DVector tmp( _dim );
374  tmp.setAll( defaultScaling );
375  scaling.append( tmp );
376  }
377  }
378  else
379  {
380  if ( _scaling.isEmpty( ) == BT_FALSE )
381  {
382  scaling.init( dim );
384  scaling.append( _scaling );
385  }
386  }
387 
388  if ( lb.isEmpty( ) == BT_FALSE )
389  {
390  if ( _lb.isEmpty( ) == BT_FALSE )
391  {
392  lb.append( _lb );
393  }
394  else
395  {
396  DVector tmp( _dim );
397  tmp.setAll( defaultLowerBound );
398  lb.append( tmp );
399  }
400  }
401  else
402  {
403  if ( _lb.isEmpty( ) == BT_FALSE )
404  {
405  lb.init( dim );
407  lb.append( _lb );
408  }
409  }
410 
411  if ( ub.isEmpty( ) == BT_FALSE )
412  {
413  if ( _ub.isEmpty( ) == BT_FALSE )
414  {
415  ub.append( _ub );
416  }
417  else
418  {
419  DVector tmp( _dim );
420  tmp.setAll( defaultUpperBound );
421  ub.append( tmp );
422  }
423  }
424  else
425  {
426  if ( _ub.isEmpty( ) == BT_FALSE )
427  {
428  ub.init( dim );
430  ub.append( _ub );
431  }
432  }
433 
434  dim = newDim;
435 
436  return SUCCESSFUL_RETURN;
437 }
438 
439 
440 
442  char* _name
443  ) const
444 {
445  if ( hasNames( ) != BT_TRUE )
447 
448  if ( idx >= dim )
450 
451  strncpy( _name,names[idx],MAX_LENGTH_NAME );
452  _name[MAX_LENGTH_NAME] = '\0';
453 
454  return SUCCESSFUL_RETURN;
455 }
456 
457 
459  const char* const _name
460  )
461 {
462  if ( idx >= dim )
464 
465  if ( _name == 0 )
467 
468  if ( hasNames( ) != BT_TRUE )
469  {
470  /* allocate names if necessary */
471  names = new char*[dim];
472 
473  for( uint i=0; i<dim; ++i )
474  {
475  names[i] = new char[MAX_LENGTH_NAME+1];
476  strncpy( names[i],defaultName,MAX_LENGTH_NAME );
477  }
478  }
479 
480  strncpy( names[idx],_name,MAX_LENGTH_NAME );
481  names[MAX_LENGTH_NAME] = NULL;
482 
483  return SUCCESSFUL_RETURN;
484 }
485 
486 
487 
489  char* _unit
490  ) const
491 {
492  if ( idx >= dim )
494 
495  if ( hasUnits( ) != BT_TRUE )
497 
498  strncpy( _unit,units[idx],MAX_LENGTH_NAME );
499  _unit[MAX_LENGTH_NAME] = '\0';
500 
501  return SUCCESSFUL_RETURN;
502 }
503 
504 
506  const char* const _unit
507  )
508 {
509  if ( idx >= dim )
511 
512  if ( _unit == 0 )
514 
515  if ( hasUnits( ) != BT_TRUE )
516  {
517  /* allocate units if necessary */
518  units = new char*[dim];
519 
520  for( uint i=0; i<dim; ++i )
521  {
522  units[i] = new char[MAX_LENGTH_NAME+1];
523  strncpy( units[i],defaultUnit,MAX_LENGTH_NAME );
524  }
525  }
526 
527  strncpy( units[idx],_unit,MAX_LENGTH_NAME );
528  units[MAX_LENGTH_NAME] = NULL;
529 
530  return SUCCESSFUL_RETURN;
531 }
532 
533 
534 
535 //
536 // PROTECTED MEMBER FUNCTIONS:
537 //
538 
540 {
541  uint i;
542 
543  if ( names != 0 )
544  {
545  for( i=0; i<dim; ++i )
546  {
547  if ( names[i] != 0 )
548  delete[] (names[i]);
549  }
550 
551  free( names );
552  names = 0;
553  }
554 
555  if ( units != 0 )
556  {
557  for( i=0; i<dim; ++i )
558  {
559  if ( units[i] != 0 )
560  delete[] (units[i]);
561  }
562 
563  free( units );
564  units = 0;
565  }
566 
567  return SUCCESSFUL_RETURN;
568 }
569 
570 
571 
573 
574 /*
575  * end of file
576  */
returnValue appendSettings(const VariableSettings &rhs)
returnValue setUnit(uint idx, const char *const _unit)
const double defaultLowerBound
returnValue getUnit(uint idx, char *_unit) const
void init(unsigned _dim=0)
Definition: vector.hpp:155
returnValue setName(uint idx, const char *const _name)
Allows to pass back messages to the calling function.
returnValue getName(uint idx, char *_name) const
BEGIN_NAMESPACE_ACADO typedef unsigned int uint
Definition: acado_types.hpp:42
returnValue setScaling(const DVector &_scaling)
#define CLOSE_NAMESPACE_ACADO
bool isEmpty() const
Definition: vector.hpp:176
VariableType
Definition: acado_types.hpp:95
VariableSettings & operator=(const VariableSettings &rhs)
BEGIN_NAMESPACE_ACADO const char defaultName[]
const unsigned int MAX_LENGTH_NAME
const BooleanType defaultAutoInit
returnValue setLowerBounds(const DVector &_lb)
BooleanType hasNames() const
Provides variable-specific settings for vector- or matrix-valued optimization variables (for internal...
void rhs(const real_t *x, real_t *f)
#define BT_TRUE
Definition: acado_types.hpp:47
GenericVector & append(const GenericVector &_arg)
Definition: vector.cpp:42
void setAll(const T &_value)
Definition: vector.hpp:160
#define BEGIN_NAMESPACE_ACADO
const double defaultScaling
#define BT_FALSE
Definition: acado_types.hpp:49
const char defaultUnit[]
returnValue setUpperBounds(const DVector &_ub)
#define ACADOERROR(retval)
const double defaultUpperBound
BooleanType hasUnits() const


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:35:16