NDOSolver / FiOracle
Interfaces and Solvers for NonDifferentiable Optimization
Loading...
Searching...
No Matches
OPTUtils.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File OPTUtils.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
22/*--------------------------------------------------------------------------*/
23/*--------------------------------------------------------------------------*/
24/*--------------------------------------------------------------------------*/
25
26#ifndef __OPTUtils
27 #define __OPTUtils /* self-identification: #endif at the end of the file */
28
29/*--------------------------------------------------------------------------*/
30/*----------------------------- MACROS -------------------------------------*/
31/*--------------------------------------------------------------------------*/
39/*---------------------------- OPT_TIMERS ----------------------------------*/
68#define OPT_TIMERS 5
69
70/*---------------------------- OPT_RANDOM ---------------------------------*/
89#define OPT_RANDOM 1
90
92/*------------------------------ INCLUDES ----------------------------------*/
93/*--------------------------------------------------------------------------*/
94
95#include <limits> /* For Inf() and Eps() */
96
97#if( OPT_RANDOM )
98 #include <stdlib.h> /* For some random routines, see OPTrand() below. */
99#endif
100
101/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
102
103#if( OPT_TIMERS <= 4 )
104 #if( ( OPT_TIMERS == 1 ) || ( OPT_TIMERS == 3 ) )
105 #include <sys/times.h>
106 #else
107 #include <sys/timeb.h>
108 #endif
109#elif( ( OPT_TIMERS == 5 ) || ( OPT_TIMERS == 7 ) )
110 #include <time.h>
111#elif( OPT_TIMERS == 6 )
112 #include <sys/time.h>
113#endif
114
115/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
116
117#include <iostream>
118/* For istream and the >> operator, used in DfltdSfInpt(). */
119
120/*--------------------------------------------------------------------------*/
121/*--------------------------- NAMESPACE ------------------------------------*/
122/*--------------------------------------------------------------------------*/
123
124namespace OPTtypes_di_unipi_it
125{
131 using namespace std; // I know it's not elegant, but ...
132
133/*--------------------------------------------------------------------------*/
134/*--------------------------- OPT_TIMERS -----------------------------------*/
135/*--------------------------------------------------------------------------*/
139#if( OPT_TIMERS )
140
145
146 public: //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
147
149 OPTtimers( void ) { ReSet(); }
150
152 void Start( void ) {
153 if( ! ticking ) {
154 #if( ( OPT_TIMERS == 1 ) || ( OPT_TIMERS == 2 ) )
155 times( &buff );
156 t_u = buff.tms_utime;
157 t_s = buff.tms_stime;
158 #elif( ( OPT_TIMERS == 3 ) || ( OPT_TIMERS == 4 ) )
159 t_u = times( &buff );
160 #elif( OPT_TIMERS == 5 )
161 t_u = clock();
162 #elif( OPT_TIMERS == 6 )
163 struct timeval t;
164 gettimeofday( &t , NULL );
165 t_u = double( t.tv_sec + t.tv_usec * 1e-6 );
166 #elif( OPT_TIMERS == 7 )
167 time( &tu );
168 #endif
169
170 ticking = true;
171 }
172 }
173
175 void Stop( void ) {
176 if( ticking ) { Read( u , s ); ticking = false; }
177 }
178
184 double Read( void ) {
185 double tu = 0;
186 double ts = 0;
187 Read( tu , ts );
188 return( tu + ts );
189 }
190
192 void Read( double &tu , double &ts ) {
193 if( ticking ) {
194 #if( ( OPT_TIMERS == 1 ) || ( OPT_TIMERS == 2 ) )
195 times( &buff );
196 tu += ( double( buff.tms_utime - t_u ) ) / double( CLOCKS_PER_SEC );
197 ts += ( double( buff.tms_stime - t_s ) ) / double( CLOCKS_PER_SEC );
198 #elif( ( OPT_TIMERS == 3 ) || ( OPT_TIMERS == 4 ) )
199 tu += ( double( times( &buff ) - t_u ) ) / double( CLOCKS_PER_SEC );
200 #elif( OPT_TIMERS == 5 )
201 tu += double( clock() - t_u ) / double( CLOCKS_PER_SEC );
202 #elif( OPT_TIMERS == 6 )
203 struct timeval t;
204 gettimeofday( &t , NULL );
205 tu += double( t.tv_sec + t.tv_usec * 1e-6 ) - t_u;
206 #elif( OPT_TIMERS == 7 )
207 tu += difftime( time( NULL ) , t_u );
208 #endif
209 }
210 else { tu += u; ts += s; }
211 }
212
214 void ReSet( void ) { u = s = 0; ticking = false; }
215
216 private: //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
217
218 double u; // elapsed *user* time, in seconds
219 double s; // elapsed *system* time, in seconds
220 bool ticking; // if the clock is ticking
221
222 #if( ( OPT_TIMERS > 0 ) && ( OPT_TIMERS <= 5 ) )
223 clock_t t_u;
224
225 #if( OPT_TIMERS <= 4 )
226 struct tms buff;
227
228 #if( ( OPT_TIMERS == 1 ) || ( OPT_TIMERS == 3 ) )
229 clock_t t_s;
230 #endif
231 #endif
232 #elif( OPT_TIMERS == 6 )
233 double t_u;
234 #elif( OPT_TIMERS == 7 )
235 time_t t_u;
236 #endif
237
238 }; // end( class OPTtimers );
239
240#endif
241
242/*--------------------------------------------------------------------------*/
243/*------------------------------ OPTrand() ---------------------------------*/
244/*--------------------------------------------------------------------------*/
248class OPTrand {
249
250 public: //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
251
253 OPTrand( void )
254 {
255 #if( OPT_RANDOM == 0 )
256 A[ 0 ] = -1;
257 srand( long( 123456789 ) );
258 #else
259 OPTrand::srand( long( 1 ) );
260 #endif
261 }
262
269 double rand( void )
270 {
271 #if( OPT_RANDOM == 0 )
272 long nmbr = *(gb_fptr--);
273 if( nmbr < 0 )
274 nmbr = gb_flip_cycle();
275 return( double( nmbr ) / double( (unsigned long)0x80000000 ) );
276 #elif( OPT_RANDOM == 1 )
277 ::srand( myseed );
278 myseed = ::rand();
279 return( double( myseed ) / double( RAND_MAX ) );
280 #elif( OPT_RANDOM == 2 )
281 return( erand48( myseed ) );
282 #else
283 return( 0 ); // random, eh?
284 #endif
285 }
286
288 void srand( long seed )
289 {
290 #if( OPT_RANDOM == 0 )
291 long prev = seed , next = 1;
292 seed = prev = mod_diff( prev , 0 );
293 A[ 55 ] = prev;
294
295 for( long i = 21 ; i ; i = ( i + 21 ) % 55 ) {
296 A[ i ] = next;
297 next = mod_diff( prev , next );
298 if( seed & 1 )
299 seed = 0x40000000 + ( seed >> 1 );
300 else
301 seed >>= 1;
302
303 next = mod_diff( next , seed );
304 prev = A[ i ];
305 }
306
307 gb_flip_cycle();
308 gb_flip_cycle();
309 gb_flip_cycle();
310 gb_flip_cycle();
311 gb_flip_cycle();
312 #elif( OPT_RANDOM == 1 )
313 myseed = int( seed );
314 #elif( OPT_RANDOM == 2 )
315 long *sp = (long*)( &myseed );
316 *sp = seed; // copy higher 32 bits
317 myseed[ 2 ] = 0x330E; // initialize lower 16 bits
318 #endif
319 }
320
321 private: //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
322
323 #if( OPT_RANDOM == 0 )
324 long A[ 56 ];
325 long *gb_fptr;
326
327 long mod_diff( long x , long y ) {
328 return( ( ( x ) - ( y ) ) & 0x7fffffff );
329 }
330
331 long gb_flip_cycle( void ) {
332 long *ii, *jj;
333 for( ii = &A[ 1 ] , jj = &A[ 32 ] ; jj <= &A[ 55 ] ; ii++ , jj++ )
334 *ii = mod_diff( *ii , *jj );
335
336 for( jj = &A[ 1 ] ; ii <= &A[ 55 ] ; ii++ , jj++ )
337 *ii = mod_diff( *ii , *jj );
338
339 gb_fptr = &A[ 54 ];
340
341 return A[ 55 ];
342 }
343 #elif( OPT_RANDOM == 1 )
344 int myseed;
345 #elif( OPT_RANDOM == 2 )
346 unsigned short int myseed[ 3 ];
347 #endif
348
349 }; // end( class( OPTrand ) )
350
351/*--------------------------------------------------------------------------*/
352/*--------------------------- Inf() and Eps() ------------------------------*/
353/*--------------------------------------------------------------------------*/
355
356 template< typename T >
357 static constexpr T Inf( void ) noexcept {
358 return( std::numeric_limits< T >::has_infinity ?
359 std::numeric_limits< T >::infinity() :
360 std::numeric_limits< T >::max() );
361 }
362
363/*--------------------------------------------------------------------------*/
365
366 template< typename T >
367 static constexpr T Eps( void ) noexcept {
368 return( std::numeric_limits< T >::epsilon() );
369 }
370
372/*--------------------------------------------------------------------------*/
373/*--------------------------- DfltdSfInpt() --------------------------------*/
374/*--------------------------------------------------------------------------*/
391template< class T >
392inline void DfltdSfInpt( istream *iStrm , T &Param , const T Dflt ,
393 const char cmntc = '#' )
394{
395 char buf[ 1024 ];
396
397 if( iStrm && ( ! ( ! (*iStrm) ) ) )
398 // the "! ! stream" trick is there to force the compiler to apply the
399 // stream -> bool conversion, in case it is too dumb to do it by itself
400 for( char c ;; (*iStrm).getline( buf , 1023 ) ) {
401 if( ! ( (*iStrm) >> ws ) ) // skip whitespace
402 break;
403
404 if( ! ( (*iStrm).get( c ) ) ) // read first non-whitespace
405 break;
406
407 if( c != cmntc ) { // that's not a comment
408 (*iStrm).seekg( -1 , ios::cur ); // backtrack
409 if( ( (*iStrm) >> Param ) ) // try reading it
410 (*iStrm).getline( buf , 1023 ); // upon success, skip the rest of line
411
412 return; // done
413 }
414 }
415
416 Param = Dflt;
417
418 } // end( DfltdSfInpt )
419
421/*--------------------------------------------------------------------------*/
422
423 } // end( namespace OPTtypes_di_unipi_it )
424
425/*--------------------------------------------------------------------------*/
426/*--------------------------------------------------------------------------*/
427
428#endif /* OPTUtils.h included */
429
430/*--------------------------------------------------------------------------*/
431/*---------------------- End File OPTUtils.h -------------------------------*/
432/*--------------------------------------------------------------------------*/
Definition OPTUtils.h:248
OPTrand(void)
constructor of the class
Definition OPTUtils.h:253
void srand(long seed)
Seeds the random generator for this instance of OPTrand.
Definition OPTUtils.h:288
double rand(void)
Definition OPTUtils.h:269
Definition OPTUtils.h:144
OPTtimers(void)
constructor of the class
Definition OPTUtils.h:149
void Stop(void)
stop the timer
Definition OPTUtils.h:175
void ReSet(void)
reset the timer
Definition OPTUtils.h:214
void Start(void)
start the timer
Definition OPTUtils.h:152
void Read(double &tu, double &ts)
As Read( void ) but adds user and system time to tu and ts.
Definition OPTUtils.h:192
double Read(void)
Definition OPTUtils.h:184
static constexpr T Inf(void) noexcept
Inf< T >() = infinity value for T.
Definition OPTUtils.h:357
static constexpr T Eps(void) noexcept
Eps< T >() = "machine epsilon" for T.
Definition OPTUtils.h:367
void DfltdSfInpt(istream *iStrm, T &Param, const T Dflt, const char cmntc='#')
Definition OPTUtils.h:392
Definition OPTtypes.h:41