NDOSolver / FiOracle
Interfaces and Solvers for NonDifferentiable Optimization
Loading...
Searching...
No Matches
SubGrad.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File SubGrad.h --------------------------------*/
3/*--------------------------------------------------------------------------*/
33/*--------------------------------------------------------------------------*/
34/*----------------------------- DEFINITIONS --------------------------------*/
35/*--------------------------------------------------------------------------*/
36
37#ifndef __SubGrad
38 #define __SubGrad /* self-identification: #endif at the end of the file */
39
40/*--------------------------------------------------------------------------*/
41/*-------------------------------- MACROS ----------------------------------*/
42/*--------------------------------------------------------------------------*/
46#define SubGrad_HANDLES_CONSTRAINTS 0
47
61/*--------------------------------------------------------------------------*/
62/*------------------------------ INCLUDES ----------------------------------*/
63/*--------------------------------------------------------------------------*/
64
65#include "NDOSlver.h" // NDOSolver is the base class
66
67#include "Deflection.h" // These classes are defined as friend classes, in
68#include "Stepsize.h" // order to allow them to use directly SubgGrad's
69 // data structures [see below].
70/* Note: a few methods of Stepsize and Deflection are implemented at the end of
71 SubGrad.C. These are the methods that allow to read protected data of
72 SubGrad, which may be useful to the objects. The methods are implemented
73 here for the base classes, so that any derived class can use them to
74 access to these data. This is crucial, as derived classes from friend
75 classes are not friend, and therefore they cannot access data of SubGrad
76 unless this capability is explicitly provided by the base classes, who are
77 friends. */
78
79#include "math.h"
80
81#include <vector>
82#include <random>
83
84#if SubGrad_HANDLES_CONSTRAINTS
85 #include "CQKnPClass.h" // used for managing the projection probelm
86#endif
87
88/*--------------------------------------------------------------------------*/
89/*----------------------------- NAMESPACE ----------------------------------*/
90/*--------------------------------------------------------------------------*/
91
92namespace NDO_di_unipi_it
93{
94
95 #if SubGrad_HANDLES_CONSTRAINTS
96 using namespace CQKnPClass_di_unipi_it;
97
98 // class CQKnPClass; // forward definition of CQKnPClass
99 #endif
100
101/*--------------------------------------------------------------------------*/
102/*----------------------------- CLASSES ------------------------------------*/
103/*--------------------------------------------------------------------------*/
107/*--------------------------------------------------------------------------*/
108/*--------------------------- CLASS SubGrad -------------------------------*/
109/*--------------------------------------------------------------------------*/
110/*--------------------------- GENERAL NOTES --------------------------------*/
111/*--------------------------------------------------------------------------*/
147class SubGrad : public NDOSolver
148{
149
150/*--------------------------------------------------------------------------*/
151/*----------------------- PUBLIC PART OF THE CLASS -------------------------*/
152/*--------------------------------------------------------------------------*/
153
154 public:
155
156/*--------------------------------------------------------------------------*/
157/*---------------------------- FRIEND CLASSES ------------------------------*/
158/*--------------------------------------------------------------------------*/
173 friend class Stepsize;
174 friend class Deflection;
175
177/*---------------------------- PUBLIC TYPES --------------------------------*/
178/*--------------------------------------------------------------------------*/
186 enum SGParam{ kSGPar1 = kLastNDOParam ,
187 kSGPar2 , kSGPar3 , kSGPar4 , kSGPar5 };
188
190/*--------------------- PUBLIC METHODS OF THE CLASS ------------------------*/
191/*--------------------------------------------------------------------------*/
192/*--------------------------------------------------------------------------*/
193/*---------------------------- CONSTRUCTOR ---------------------------------*/
194/*--------------------------------------------------------------------------*/
198 SubGrad( std::istream *iStrm = 0 );
199
271/*-------------------------- OTHER INITIALIZATIONS -------------------------*/
272/*--------------------------------------------------------------------------*/
276 void SetStepsize( Stepsize *STP = nullptr );
277
288/*--------------------------------------------------------------------------*/
289
290 void SetDeflection( Deflection *Vol = nullptr );
291
305/*--------------------------------------------------------------------------*/
306
307#if SubGrad_HANDLES_CONSTRAINTS
308
309 void SetQKNP( CQKnPClass *KNP = nullptr );
310
325#endif
326
327/*--------------------------------------------------------------------------*/
328
329 void SetFiOracle( FiOracle *Fi = nullptr );
330
331/*--------------------------------------------------------------------------*/
332
333 void SetLambda( cLMRow tLambda = nullptr );
334
335/*--------------------------------------------------------------------------*/
336
337 void KeepBestLambda( const bool KBL = true );
338
339/*--------------------------------------------------------------------------*/
340
341 void SetPar( const int wp , const int value );
342
347/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
348
349 void SetPar( const int wp , cHpNum value );
350
355/*--------------------------------------------------------------------------*/
356
357 void SetPar( const int wp , const bool value );
358
362/*--------------------------------------------------------------------------*/
363
364 void SetNDOLog( std::ostream *outs = 0 , const char lvl = 0 );
365
378/*-------------------- METHODS FOR SOLVING THE PROBLEM ---------------------*/
379/*--------------------------------------------------------------------------*/
384
427/*--------------------------------------------------------------------------*/
428
429 void ReSetAlg( unsigned char RstLvl = 0 );
430
454/*---------------------- METHODS FOR READING RESULTS -----------------------*/
455/*--------------------------------------------------------------------------*/
460
464/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
465
467
471/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
472
474
480/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
481
483
488/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
489
491
495/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
496
497 bool IsOptimal( HpNum eps = 0 );
498
518/*--------------------------------------------------------------------------*/
519
521
523
525/*-------------- METHODS FOR READING THE DATA OF THE PROBLEM ---------------*/
526/*--------------------------------------------------------------------------*/
530 inline void GetPar( const int wp , int &value );
531
532 inline void GetPar( const int wp , HpNum &value );
533
534 inline void GetPar( const int wp , bool &value );
535
537/*------------- METHODS FOR ADDING / REMOVING / CHANGING DATA --------------*/
538/*--------------------------------------------------------------------------*/
542 void AddVariables( Index NNwVrs , cLMRow IVs = 0 );
543
544 void RemoveVariables( cIndex_Set whch = 0 , Index hwmny = 0 );
545
546 void ChgFiV( cIndex wFi = Inf< Index >() );
547
548 void ChgSbG( cIndex strt = 0 , Index stp = Inf< Index >() ,
549 cIndex wFi = Inf< Index >() );
550
552/*------------------------------ DESTRUCTOR --------------------------------*/
553/*--------------------------------------------------------------------------*/
557 ~SubGrad();
558
560/*---------------------- PROTECTED PART OF THE CLASS -----------------------*/
561/*--------------------------------------------------------------------------*/
562
563 protected:
564
565/*--------------------------------------------------------------------------*/
566/*-------------------------- PROTECTED METHODS -----------------------------*/
567/*--------------------------------------------------------------------------*/
568
569 void FiAndGi( cIndex wFi = Inf< Index >() );
570
576/*--------------------------------------------------------------------------*/
577
578 void FormD( void );
579
592/*--------------------------------------------------------------------------*/
593
594 void SaveDir( void );
595
600/*--------------------------------------------------------------------------*/
601
602 void GotoLambda1( void );
603
606/*--------------------------------------------------------------------------*/
607
608 void FormLambda1( void );
609
619/*--------------------------------------------------------------------------*/
620/*----------------------- PROTECTED DATA STRUCTURES -----------------------*/
621/*--------------------------------------------------------------------------*/
622
624
626
628
629 bool SGPar4;
630
632
634
636
638
641
645
648
649
650 bool LHasChgd;
652 bool LHasProj;
655 bool KpBstL;
658
660 bool TrueLB;
665
668
681
684
687
693
694 #if SubGrad_HANDLES_CONSTRAINTS
695 Index MaxNCnst;
696 Index MaxName;
697
698 CQKnPClass *Q2KNP;
699
700 Index_Set CnstBeg;
715 HpRow CnstVol;
718 SIndex_Set CnstNxt;
720 #endif
721
722 std::mt19937 myrandom;
723
724 bool ZeroComp;
727 bool InnIter;
728
729 vector< Index > Seq;
731 bool DirPos;
733 Index_Set MultBse;
734
737
739 bool DoSS;
740
742 bool EmptySet;
743
744/*--------------------------------------------------------------------------*/
745/*--------------------- PRIVATE PART OF THE CLASS --------------------------*/
746/*--------------------------------------------------------------------------*/
747
748 private:
749
750/*--------------------------------------------------------------------------*/
751/*-------------------------- PRIVATE METHODS -------------------------------*/
752/*--------------------------------------------------------------------------*/
753
754 inline void Log1( void );
755
756 inline void Log2( void );
757
758 inline void Log3( void );
759
760 inline void Log4( void );
761
762/*--------------------------------------------------------------------------*/
763
764 void AllocateUBLB( cIndex strt , cIndex num );
765
766/*--------------------------------------------------------------------------*/
767
768 inline LMNum lbnd( cIndex i ) {
769 return( lb ? lb[ i ] : - Inf< LMNum >() );
770 }
771
772/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
773
774 inline LMNum ubnd( cIndex i ) {
775 return( ub ? ub[ i ] : Inf< LMNum >() );
776 }
777
778/*--------------------------------------------------------------------------*/
779
780 #if SubGrad_HANDLES_CONSTRAINTS
781 void EvalConst( void );
782 #endif
783
784 void EvalGi( Index wFi = Inf< Index >() );
785
786/*--------------------------------------------------------------------------*/
787
788 bool ProjFsb( LMRow Lpt , bool & EmptySet );
789
790 void ProjTng( SgRow Gpt , cLMRow Lpt ,
791 cIndex strt = 0 , Index stp = Inf< Index >() );
792
793/*--------------------------------------------------------------------------*/
794
795 void InitStepsize( void );
796
797 void InitDeflection( void );
798
799/*--------------------------------------------------------------------------*/
800
801 void ChgLambdaHat( void );
802
803/*--------------------------------------------------------------------------*/
804
805 void UpdateSigma( void );
806
807/*--------------------------------------------------------------------------*/
808
809 void UpdtLowerBound( void );
810
811/*--------------------------------------------------------------------------*/
812
813 void MemDealloc( void );
814
815/*--------------------------------------------------------------------------*/
816/*--------------------------------------------------------------------------*/
817
818 }; // end( class SubGrad )
819
821/*--------------------------------------------------------------------------*/
822/*------------------- inline methods implementation ------------------------*/
823/*--------------------------------------------------------------------------*/
824
825inline void SubGrad::GetPar( const int wp , int &value )
826{
827 switch( wp ) {
828 case( kSGPar1 ): value = SGPar1; break;
829 case( kSGPar3 ): value = SGPar3; break;
830 case( kSGPar5 ): value = SGPar5; break;
831 default: NDOSolver::GetPar( wp , value );
832 }
833 } // end( SubGrad::GetPar( Index ) )
834
835/*--------------------------------------------------------------------------*/
836
837inline void SubGrad::GetPar( const int wp , HpNum &value )
838{
839 switch( wp ) {
840 case( kSGPar2 ): value = SGPar2; break;
841 default: NDOSolver::GetPar( wp , value );
842 }
843 } // end( SubGrad::GetPar( HpNum ) )
844
845/*--------------------------------------------------------------------------*/
846
847inline void SubGrad::GetPar( const int wp , bool &value )
848{
849 switch( wp ) {
850 case( kSGPar4 ): value = SGPar4; break;
851 default: throw( NDOException( "GetPar( bool ): unknown parameter" ) );
852 }
853 } // end( SubGrad::GetPar( HpNum ) )
854
855/*--------------------------------------------------------------------------*/
856/*--------------------------------------------------------------------------*/
857
858}; // end( namespace NDO_di_unipi_it )
859
860/*--------------------------------------------------------------------------*/
861/*--------------------------------------------------------------------------*/
862
863#endif /* __SubGrad */
864
865/*--------------------------------------------------------------------------*/
866/*------------------------- End File SubGrad.h -----------------------------*/
867/*--------------------------------------------------------------------------*/
Definition Deflection.h:63
Definition FiOracle.h:231
FiStatus
Definition FiOracle.h:248
Definition FiOracle.h:66
Definition NDOSlver.h:60
NDOStatus
Definition NDOSlver.h:91
virtual void GetPar(const int wp, int &value)
Definition NDOSlver.h:1075
Definition Stepsize.h:63
Definition SubGrad.h:148
bool ZeroComp
true if Fi() comes with the 0-th component
Definition SubGrad.h:724
bool DoSS
SS vs NS.
Definition SubGrad.h:739
LMRow LambdaBest
the best point found so far
Definition SubGrad.h:657
HpNum step
the stepsize
Definition SubGrad.h:667
LMRow lb
lower bounds on the variables
Definition SubGrad.h:692
HpNum SGPar2
incremental factor
Definition SubGrad.h:625
void ReSetAlg(unsigned char RstLvl=0)
bool LHasChgd
Definition SubGrad.h:650
void SetPar(const int wp, const int value)
HpNum Sigma
Definition SubGrad.h:669
LMRow LambdaBar
the stability center
Definition SubGrad.h:639
HpNum FiLambda
full function value at Lambda
Definition SubGrad.h:644
HpNum SigmaHat
Definition SubGrad.h:674
Index MaxNumVar
maximum number of variables
Definition SubGrad.h:637
bool DirPos
Definition SubGrad.h:731
LMRow LambdaHat
the point
Definition SubGrad.h:646
HpNum ReadFiVal(cIndex wFi=Inf< Index >())
void ChgSbG(cIndex strt=0, Index stp=Inf< Index >(), cIndex wFi=Inf< Index >())
HpNum dM1Gk
scalar product
Definition SubGrad.h:686
bool IsOptimal(HpNum eps=0)
bool LHasProj
Definition SubGrad.h:652
HpNum FiBest
the best value of found so far
Definition SubGrad.h:656
NDOStatus Solve(void)
Index NItIncr
Definition SubGrad.h:725
bool dM1GkDone
Definition SubGrad.h:688
Stepsize * stepsize
pointer to the Stepsize class
Definition SubGrad.h:633
Index_Set SGBase
the set of indices of Gi[ wFi ]( Lambda )
Definition SubGrad.h:680
vector< Index > Seq
Definition SubGrad.h:729
HpNum FiBar
full function value at LambdaBar
Definition SubGrad.h:640
void GetPar(const int wp, int &value)
Definition SubGrad.h:825
SgRow dir
the direction
Definition SubGrad.h:664
void RemoveVariables(cIndex_Set whch=0, Index hwmny=0)
bool EmptySet
true, if the feasible set is empty
Definition SubGrad.h:742
SgRow Gi
Gi[ wFi ]( Lambda ), the subgradient.
Definition SubGrad.h:663
void SetPar(const int wp, cHpNum value)
HpNum NrmDir
the (squared) direction's norm
Definition SubGrad.h:683
HpNum ReadBestFiVal(cIndex wFi=Inf< Index >())
bool SGPar4
control if is kept
Definition SubGrad.h:629
HpNum LowerBound
Lower Bound over the full function .
Definition SubGrad.h:659
Index CNSCntr
counter of consecutive NS
Definition SubGrad.h:736
SGParam
Definition SubGrad.h:186
cHpRow ReadMult(cIndex_Set &I, Index &D, cIndex wFi=Inf< Index >())
FiOracle::FiStatus fs
FiOracle status.
Definition SubGrad.h:741
LMRow ub
upper bounds on the variables
Definition SubGrad.h:691
LMRow Lambda
Definition SubGrad.h:642
void SetFiOracle(FiOracle *Fi=nullptr)
Index CSSCntr
counter of consecutive SS
Definition SubGrad.h:735
Index SGPar5
seed
Definition SubGrad.h:631
void SetLambda(cLMRow tLambda=nullptr)
HpNum alpha
the deflection parameter
Definition SubGrad.h:666
void SetPar(const int wp, const bool value)
HpNum ReadLBMult(cIndex wFi=Inf< Index >())
HpNum Epsilon
Definition SubGrad.h:671
bool KpBstL
if LambdaBest has to be kept
Definition SubGrad.h:655
bool TrueLB
Definition SubGrad.h:660
cLMRow ReadSol(cIndex_Set &I, Index &D)
cLMRow ReadBestSol(cIndex_Set &I, Index &D)
void AddVariables(Index NNwVrs, cLMRow IVs=0)
HpNum HatEpsilon
Definition SubGrad.h:676
void FiAndGi(cIndex wFi=Inf< Index >())
Index SGPar1
projection-strategy parameters
Definition SubGrad.h:623
void SetStepsize(Stepsize *STP=nullptr)
void SetNDOLog(std::ostream *outs=0, const char lvl=0)
Deflection * deflection
pointer to the Deflection class
Definition SubGrad.h:635
bool InnIter
if true, the current iteration is an inner one
Definition SubGrad.h:727
HpNum FiHat
full function value at HLmb
Definition SubGrad.h:647
Index CSmallStep
counter of consecutive short step
Definition SubGrad.h:738
void SetDeflection(Deflection *Vol=nullptr)
void ChgFiV(cIndex wFi=Inf< Index >())
Index SGPar3
scheme: stepsize(deflection)-restricted
Definition SubGrad.h:627
std::mt19937 myrandom
random generator for incremental steps
Definition SubGrad.h:722
HpNum NrmGi
the (squared) subgradient's norm
Definition SubGrad.h:682
SubGrad(std::istream *iStrm=0)
HpNum dGk
scalar product
Definition SubGrad.h:685
HpNum * HpRow
"finer" (fp) array
Definition OPTtypes.h:99
const HpNum cHpNum
a read-only HpNum
Definition OPTtypes.h:102
double HpNum
"finer" floating point numbers
Definition OPTtypes.h:98
const Index cIndex
a read-only Index
Definition OPTtypes.h:64
cIndex * cIndex_Set
read-only array
Definition OPTtypes.h:65
SIndex * SIndex_Set
set (array) of s. indices
Definition OPTtypes.h:71
Index * Index_Set
set (array) of indices
Definition OPTtypes.h:61
unsigned int Index
Index in a vector ( >= 0 )
Definition OPTtypes.h:60
cHpNum * cHpRow
read-only array
Definition OPTtypes.h:103
SgNum * SgRow
a subgradient
Definition OPTtypes.h:112
LMNum * LMRow
a vector of Lagrangean Multipliers
Definition OPTtypes.h:130
double LMNum
a Lagrangean Multiplier
Definition OPTtypes.h:129
cLMNum * cLMRow
a read-only vector of LMs
Definition OPTtypes.h:134
static constexpr T Inf(void) noexcept
Inf< T >() = infinity value for T.
Definition OPTUtils.h:357
Definition Bundle.h:68