NDOSolver / FiOracle
Interfaces and Solvers for NonDifferentiable Optimization
Loading...
Searching...
No Matches
ColorTV.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File ColorTV.h -------------------------------*/
3/*--------------------------------------------------------------------------*/
24/*--------------------------------------------------------------------------*/
25/*-------------------- DEFINITIONS & IMPLEMENTATION ------------------------*/
26/*--------------------------------------------------------------------------*/
27
28#ifndef __ColorTV
29 #define __ColorTV /* self-identification: #endif at the end of the file */
30
31/*--------------------------------------------------------------------------*/
32/*------------------------------ INCLUDES ----------------------------------*/
33/*--------------------------------------------------------------------------*/
34
35#include "Stepsize.h"
36
37/*--------------------------------------------------------------------------*/
38/*------------------------------- MACROS ----------------------------------*/
39/*--------------------------------------------------------------------------*/
40
41#define LOG_STP 0
42
43/* If LOG_STP > 0, the ColorTV class produces a log of its activities on the
44 ostream object and at the "level of verbosity" set with the method
45 SetSTPLog() [see below]. */
46
47#if( LOG_STP )
48 #define STPLOG( l , x ) if( STPLLvl > l ) *STPLog << x
49 #define STPLOG2( l , c , x ) if( ( STPLLvl > l ) && c ) *STPLog << x
50#else
51 #define STPLOG( l , x )
52 #define STPLOG2( l , c , x )
53#endif
54
55/*--------------------------------------------------------------------------*/
56/*----------------------------- NAMESPACE ----------------------------------*/
57/*--------------------------------------------------------------------------*/
58
59namespace NDO_di_unipi_it
60{
61
62/*--------------------------------------------------------------------------*/
63/*--------------------------- GENERAL NOTES --------------------------------*/
64/*--------------------------------------------------------------------------*/
69class ColorTV : public Stepsize
70{
71
72/*--------------------------------------------------------------------------*/
73/*----------------------- PUBLIC PART OF THE CLASS -------------------------*/
74/*--------------------------------------------------------------------------*/
75
76 public:
77
78/*--------------------------------------------------------------------------*/
79/*---------------------------- PUBLIC TYPES --------------------------------*/
80/*--------------------------------------------------------------------------*/
84 enum condition { green ,
86 red
87 };
88
90/*--------------------- PUBLIC METHODS OF THE CLASS ------------------------*/
91/*--------------------------------------------------------------------------*/
92/*---------------------------- CONSTRUCTOR ---------------------------------*/
93/*--------------------------------------------------------------------------*/
110 ColorTV( SubGrad *slvr , std::istream *iStrm = 0 )
111 : Stepsize( slvr , iStrm )
112 {
113 DfltdSfInpt( iStrm , BetaZero , HpNum( 1 ) ); // initial value of beta
114 DfltdSfInpt( iStrm , greentestinvl , Index( 1 ) );
115 DfltdSfInpt( iStrm , yellowtestinvl , Index( 400 ) );
116 DfltdSfInpt( iStrm , redtestinvl , Index( 10 ) );
117 }
118
119/*--------------------------------------------------------------------------*/
120/*-------------------------- OTHER INITIALIZATIONS -------------------------*/
121/*--------------------------------------------------------------------------*/
122
123 inline void SetSTPLog( ostream *outs = 0 , const char lvl = 0 );
124
125/*--------------------------------------------------------------------------*/
126
127 inline void Format( void ) ;
128
129/*--------------------------------------------------------------------------*/
130/*-------------------- METHODS FOR STEPSIZE COMPUTATION --------------------*/
131/*--------------------------------------------------------------------------*/
132
133 inline void NewStep( void );
134
135/*--------------------------------------------------------------------------*/
136
137 bool NeedsdkM1Gk( void ) { return( true ); }
138
139/*--------------------------------------------------------------------------*/
140/*---------------------- PROTECTED PART OF THE CLASS -----------------------*/
141/*--------------------------------------------------------------------------*/
142
143 protected:
144
145 inline bool UpdateTargetLevel( void );
146
147/*--------------------------------------------------------------------------*/
148/*--------------------- PRIVATE PART OF THE CLASS --------------------------*/
149/*--------------------------------------------------------------------------*/
150
151 private:
152
153/*--------------------------------------------------------------------------*/
154/*----------------------- PRIVATE DATA STRUCTURES -------------------------*/
155/*--------------------------------------------------------------------------*/
156
157 inline condition coloring( void );
158
159/*--------------------------------------------------------------------------*/
160/*----------------------- PRIVATE DATA STRUCTURES -------------------------*/
161/*--------------------------------------------------------------------------*/
162
166
170
171 HpNum BetaZero;
174
176 Index NrIter;
177
178/*--------------------------------------------------------------------------*/
179
180 }; // end( class ColorTV )
181
182/*--------------------------------------------------------------------------*/
183/*------------------- inline methods implementation ------------------------*/
184/*--------------------------------------------------------------------------*/
185
186inline void ColorTV::Format( void )
187{
189
191 LwrBnd = - Inf< HpNum >(); // lower bound, target level are unknown
192 lastvalue = FiLmbd = Inf< HpNum >(); // \f$ \bar{f}_{i-1} \f$ and \f$ f_i \f$
193 // are unknown
194 Beta = BetaZero;
195 NrIter = 0;
196 }
197
198/*--------------------------------------------------------------------------*/
199
200inline void ColorTV::NewStep( void )
201{
202 // get the function value - - - - - - - - - - - - - - - - - - - - - - - - - -
203 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
204
205 FiLmbd = ReadFkVal();
206
207 if( FiLmbd == Inf< HpNum >() )
208 throw( NDOException( "ColorTV::GetStepsize: this should not happen" ) );
209
210 // eventually, improve the target level - - - - - - - - - - - - - - - - - -
211 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
212
214
215 // which is it the color? - - - - - - - - - - - - - - - - - - - - - - - - -
216 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
217
218 coloring();
219 NrIter++;
220
221 // print the level - - - - - - - - - - - - - - - - - - - - - - - - - - - -
222
223 STPLOG( 1 , std::endl << " " << " level = " << -FiLev
224 << " ~ beta = " << Beta << std::endl << " "
225 );
226
227 } // end( ColorTV::NewStep )
228
229/*--------------------------------------------------------------------------*/
230
231inline void ColorTV::SetSTPLog( ostream *outs , const char lvl )
232{
233 Stepsize::SetSTPLog( outs , lvl );
234
235 #if( LOG_STP )
236 if( STPLLvl > 1 ) {
237 *STPLog << std::endl << "ColorTV: ~ BetaZero = " << BetaZero
238 << " ~ Color (g , y , r) = (" << greentestinvl << " , "
239 << yellowtestinvl << " , " << redtestinvl << ")" << std::endl;
240 }
241 #endif
242 }
243
244/*--------------------------------------------------------------------------*/
245
246inline bool ColorTV::UpdateTargetLevel( void )
247{
248 bool TLchgd = false;
249
250 // the target level is updated after a change of the lower bound - - - - - -
251 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
252
253 cHpNum LwrBnd_ = GetOracle()->GetLowerBound();
254 if( LwrBnd_ > LwrBnd )
255 LwrBnd = LwrBnd_;
256
257 // some exception - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
258 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
259
260 if( LwrBnd == -Inf< HpNum >() )
261 throw( NDOException(
262 "ColorTV::UpdateTargetLevel: no lower bound is given" ) );
263
264 // target level updating - - - - - - - - - - - - - - - - - - - - - - - - - -
265 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
266
267 if( ( FiLev < -10 ) || ( FiLev > 10 ) ) { // nicely away from 0
268 if( FiLmbd <= FiLev + ABS( FiLev ) * 0.05 ) {
269 if( ( FiLmbd < 10 ) && ( FiLmbd > - 9.5 ) )
270 FiLev= - 10;
271 else {
272 FiLev -= 0.025 * ABS( FiLev );
273 FiLev = std::min( FiLev , FiLmbd - 0.05 * ABS( FiLmbd ) );
274 }
275 TLchgd = true;
276 }
277 }
278 else // near 0, must be careful
279 if( FiLmbd < 10 ) {
280 FiLev = ( FiLmbd < - 9.5 ) ? - 10 :
281 ( FiLmbd > 0 ? FiLmbd * 0.95 : FiLmbd * 1.05 );
282 TLchgd = true;
283 }
284
285 // failure of the target level updating - - - - - - - - - - - - - - - - - - -
286 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
287
288 if( FiLev < LwrBnd ) { // change the level
289 FiLev = LwrBnd;
290 TLchgd = true;
291 }
292
293 return( TLchgd );
294
295 } // end( ColorTV::UpdateTargetLevel )
296
297/*--------------------------------------------------------------------------*/
298
299inline ColorTV::condition ColorTV::coloring( void )
300{
301 Index ConColor; // number of consecutive iteration with the same color
302
303 // green, yellow, red - - - - - - - - - - - - - - - - - - - - - - - - - - -
304 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
305
306 condition lastswing;
307 if( ( -GetdGk() <= -1e-6 ) && ( lastvalue - FiLmbd >=
308 1e-6 * std::max( ABS( Solver->ReadBestFiVal() ) , 1.0 ) ) ) {
309 lastswing = green;
310 lastgreeniter = NrIter;
311 }
312 else
313 if( ( -GetdGk() > -1e-6 ) && ( lastvalue > FiLmbd ) ) {
314 lastswing = yellow;
315 lastyellowiter = NrIter;
316 }
317 else {
318 lastswing = red;
319 lastrediter = NrIter;
320 }
321
322 // change beta, if needed - - - - - - - - - - - - - - - - - - - - - - - - -
323 //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
324
325 STPLOG( 1 , std::endl << " " );
326
327 switch( lastswing ) {
328 case( green ): // - - - - - - - - - - - - - - - - - - - - - - - - - - - -
329 ConColor = NrIter - std::max( lastyellowiter , lastrediter );
330 STPLOG( 1 , " # green = " << ConColor << " ~ ScPr = "
331 << GetdGk() << " ~ DeltaFi = " << lastvalue - FiLmbd );
332 if( ( ConColor >= greentestinvl ) && ( Beta < 2 ) ) {
334 Beta *= 2.0;
335 }
336 break;
337
338 case( yellow ): //- - - - - - - - - - - - - - - - - - - - - - - - - - - -
339 ConColor = NrIter - std::max( lastgreeniter , lastrediter );
340 STPLOG( 2 , " # yellow = " << ConColor << " ~ ScPr = "
341 << GetdGk() << " ~ DeltaFi = " << lastvalue - FiLmbd );
342 if( ConColor >= yellowtestinvl ) {
344 Beta *= 1.1;
345 }
346 break;
347
348 case( red ): //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
349 ConColor = NrIter - std::max( lastgreeniter , lastyellowiter );
350 STPLOG( 1 , " # red = " << ConColor << " ~ ScPr = " << GetdGk() );
351 if( ( ConColor >= redtestinvl ) && ( Beta > HpNum( 5e-4 ) ) ) {
353 Beta *= 0.67;
354 }
355 break;
356
357 } // end( switch( lastswing ) )- - - - - - - - - - - - - - - - - - - - - -
358
359 // update the last value - - - - - - - - - - - - - - - - - - - - - - - - - -
360
362 return( lastswing );
363
364 } // end( ColorTV::coloring )
365
366/*--------------------------------------------------------------------------*/
367
368}; // end( namespace NDO_di_unipi_it )
369
370/*--------------------------------------------------------------------------*/
371/*--------------------------------------------------------------------------*/
372
373#endif /* ColorTV.h included */
374
375/*--------------------------------------------------------------------------*/
376/*------------------------- End File ColorTV.h -----------------------------*/
377/*--------------------------------------------------------------------------*/
Definition ColorTV.h:70
HpNum FiLmbd
current FiLambda
Definition ColorTV.h:175
condition
Definition ColorTV.h:84
@ green
good step
Definition ColorTV.h:84
@ red
bad step
Definition ColorTV.h:86
@ yellow
not bad, not good step
Definition ColorTV.h:85
HpNum lastvalue
the previous function value
Definition ColorTV.h:173
ColorTV(SubGrad *slvr, std::istream *iStrm=0)
Definition ColorTV.h:110
Index yellowtestinvl
maximum number of consecutive yellow iteration
Definition ColorTV.h:164
bool NeedsdkM1Gk(void)
Definition ColorTV.h:137
bool UpdateTargetLevel(void)
Definition ColorTV.h:246
void Format(void)
Definition ColorTV.h:186
Index lastgreeniter
the last green iteration
Definition ColorTV.h:167
HpNum LwrBnd
lower bound
Definition ColorTV.h:172
Index lastyellowiter
the last yellow iteration
Definition ColorTV.h:168
Index lastrediter
the last red iteration
Definition ColorTV.h:169
Index redtestinvl
maximum number of consecutive red iteration
Definition ColorTV.h:165
void NewStep(void)
Definition ColorTV.h:200
Index greentestinvl
maximum number of consecutive green iteration
Definition ColorTV.h:163
virtual HpNum GetLowerBound(cIndex wFi=Inf< Index >())
Definition FiOracle.h:1601
Definition FiOracle.h:66
Definition Stepsize.h:63
std::ostream * STPLog
the output stream object
Definition Stepsize.h:342
virtual void SetSTPLog(std::ostream *outs=0, const char lvl=0)
Definition Stepsize.h:126
HpNum Beta
beta factor
Definition Stepsize.h:346
char STPLLvl
the "level of verbosity"
Definition Stepsize.h:343
SubGrad * Solver
(pointer to) the SubGrad solver
Definition Stepsize.h:341
HpNum FiLev
the target level
Definition Stepsize.h:345
virtual void Format(void)
Definition Stepsize.h:137
FiOracle * GetOracle(void)
Definition SubGrad.h:148
HpNum ReadFiVal(cIndex wFi=Inf< Index >())
HpNum ReadBestFiVal(cIndex wFi=Inf< Index >())
const HpNum cHpNum
a read-only HpNum
Definition OPTtypes.h:102
double HpNum
"finer" floating point numbers
Definition OPTtypes.h:98
unsigned int Index
Index in a vector ( >= 0 )
Definition OPTtypes.h:60
static constexpr T Inf(void) noexcept
Inf< T >() = infinity value for T.
Definition OPTUtils.h:357
void DfltdSfInpt(istream *iStrm, T &Param, const T Dflt, const char cmntc='#')
Definition OPTUtils.h:392
Definition Bundle.h:68