NDOSolver / FiOracle
Interfaces and Solvers for NonDifferentiable Optimization
Loading...
Searching...
No Matches
NDOSlver.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File NDOSlver.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
20/*--------------------------------------------------------------------------*/
21/*----------------------------- DEFINITIONS --------------------------------*/
22/*--------------------------------------------------------------------------*/
23
24#ifndef __NDOSolver
25 #define __NDOSolver /* self-identification: #endif at the end of the file */
26
27/*--------------------------------------------------------------------------*/
28/*------------------------------ INCLUDES ----------------------------------*/
29/*--------------------------------------------------------------------------*/
30
31#include "FiOracle.h"
32
33/*--------------------------------------------------------------------------*/
34/*----------------------------- NAMESPACE ----------------------------------*/
35/*--------------------------------------------------------------------------*/
41namespace NDO_di_unipi_it
42{
43/*--------------------------------------------------------------------------*/
44/*-------------------------- CLASS NDOSolver -------------------------------*/
45/*--------------------------------------------------------------------------*/
46/*--------------------------- GENERAL NOTES --------------------------------*/
47/*--------------------------------------------------------------------------*/
60{
61/*--------------------------------------------------------------------------*/
62/*----------------------- PUBLIC PART OF THE CLASS -------------------------*/
63/*--------------------------------------------------------------------------*/
64
65 public:
66
67/*--------------------------------------------------------------------------*/
68/*---------------------------- PUBLIC TYPES --------------------------------*/
69/*--------------------------------------------------------------------------*/
80 enum NDOParam { kMaxItr = 0 , kMaxTme ,
81 ktStar , kEpsLin ,
82 kEInit , kEFnal , kEDcrs , kEStps ,
83 kLastNDOParam
84 };
85
86/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
87
91 enum NDOStatus { kOK = 0 ,
92 kUnbndd , kUnfsbl ,
93 kStopped , kLwPrcsn , kStpIter , kStpTime ,
94 kError
95 };
96
98/*--------------------- PUBLIC METHODS OF THE CLASS ------------------------*/
99/*--------------------------------------------------------------------------*/
100/*---------------------------- CONSTRUCTOR ---------------------------------*/
101/*--------------------------------------------------------------------------*/
105 NDOSolver( std::istream *iStrm = 0 );
106
188/*-------------------------- OTHER INITIALIZATIONS -------------------------*/
189/*--------------------------------------------------------------------------*/
214 virtual void SetFiOracle( FiOracle *Fi = 0 ) {
215 if( ( Oracle = Fi ) ) {
216 NrFi = Fi->GetNrFi();
217 NumVar = Fi->GetNumVar();
218
220 }
221 }
222
223/*--------------------------------------------------------------------------*/
224
225 virtual void SetLambda( cLMRow tLambda = 0 ) = 0;
226
248/*--------------------------------------------------------------------------*/
249
250 virtual void KeepBestLambda( const bool KBL = true ) {};
251
260/*--------------------------------------------------------------------------*/
261
262 virtual void SetPar( const int wp , const int value );
263
277/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
278
279 virtual void SetPar( const int wp , cHpNum value );
280
294/*--------------------------------------------------------------------------*/
301 virtual void SetNDOLog( ostream *outs = 0 , const char lvl = 0 ) {
302 if( ( NDOLog = outs ) )
303 NDOLLvl = lvl;
304 else
305 NDOLLvl = 0;
306 }
307
308/*--------------------------------------------------------------------------*/
320 virtual void SetNDOTime( const bool TimeIt = true ) {
321 if( TimeIt )
322 if( NDOt )
323 NDOt->ReSet();
324 else
325 NDOt = new OPTtimers();
326 else {
327 delete NDOt;
328 NDOt = 0;
329 }
330 }
331
333/*-------------------- METHODS FOR SOLVING THE PROBLEM ---------------------*/
334/*--------------------------------------------------------------------------*/
338 virtual NDOStatus Solve( void ) = 0;
339
383/*--------------------------------------------------------------------------*/
384
385 virtual void ReSetAlg( unsigned char RstLvl = 0 ) {}
386
422/*---------------------- METHODS FOR READING RESULTS -----------------------*/
423/*--------------------------------------------------------------------------*/
427 virtual cLMRow ReadSol( cIndex_Set &I , Index &D ) = 0;
428
441/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
451 virtual cLMRow ReadBestSol( cIndex_Set &I , Index &D ) {
452 return( ReadSol( I , D ) );
453 }
454
455/*--------------------------------------------------------------------------*/
456
457 virtual HpNum ReadFiVal( cIndex wFi = Inf< Index >() ) = 0;
458
472/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
484 return( ReadFiVal( wFi ) );
485 }
486
487/*--------------------------------------------------------------------------*/
497 virtual bool IsOptimal( HpNum eps = 0 ) const {
498 return( ( Result == kOK ) ); // a standard very naive implementation is
499 // provided, but it should probably be
500 // redefined in real classes
501 }
502
503/*--------------------------------------------------------------------------*/
603 virtual cHpRow ReadMult( cIndex_Set &I , Index &D ,
604 cIndex wFi = Inf< Index >() ) {
605 I = nullptr; D = 0; return( 0 );
606 }
607
608/*--------------------------------------------------------------------------*/
633 virtual HpNum ReadLBMult( cIndex wFi = Inf< Index >() ) { return( 0 ); }
634
635/*--------------------------------------------------------------------------*/
641 Index FiEval( void ) const { return( FiEvaltns ); }
642
643/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
649 Index GiEval( void ) const { return( GiEvaltns ); }
650
651/*--------------------------------------------------------------------------*/
657 Index NrCalls( void ) const { return( SCalls ); }
658
659/*--------------------------------------------------------------------------*/
667 Index NrIter( void ) const { return( ParIter ); }
668
669/*--------------------------------------------------------------------------*/
684 void NDOTime( double & t_us , double & t_ss ) {
685 t_us = t_ss = 0;
686 if( NDOt )
687 NDOt->Read( t_us , t_ss );
688 }
689
690/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
694 double NDOTime( void ) { return( NDOt ? NDOt->Read() : 0 ); }
695
697/*-------------- METHODS FOR READING THE DATA OF THE PROBLEM ---------------*/
698/*--------------------------------------------------------------------------*/
705 Index GetNumVar( void ) const { return( NumVar ); }
706
707/*--------------------------------------------------------------------------*/
708
709 virtual void GetPar( const int wp , int &value );
710
714/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
715
716 virtual void GetPar( const int wp , HpNum &value );
717
722/*------------- METHODS FOR ADDING / REMOVING / CHANGING DATA --------------*/
723/*--------------------------------------------------------------------------*/
797 virtual void AddVariables( Index NNwVrs , cLMRow IVs = 0 ) {
798 throw( NDOException( "AddVariables: adding variables is not supported" )
799 );
800 }
801
802/*--------------------------------------------------------------------------*/
871 virtual void RemoveVariables( cIndex_Set whch = 0 , Index hwmny = 0 ) {
872 throw( NDOException(
873 "RemoveVariables: removing variables is not supported" ) );
874 }
875
876/*--------------------------------------------------------------------------*/
911 virtual void ChgFiV( cIndex wFi = Inf< Index >() ) {
912 throw( NDOException( "ChgFiV: changing Fi-values is not supported" ) );
913 }
914
915/*--------------------------------------------------------------------------*/
943 virtual void ChgSbG( cIndex strt = 0 , Index stp = Inf< Index >() ,
944 cIndex wFi = Inf< Index >() ) {
945 throw( NDOException( "ChgSbG: changing subgradients is not supported" ) );
946 }
947
949/*------------------------------ DESTRUCTOR --------------------------------*/
950/*--------------------------------------------------------------------------*/
959 virtual ~NDOSolver() { delete NDOt; }
960
962/*-------------------- PROTECTED PART OF THE CLASS -------------------------*/
963/*--------------------------------------------------------------------------*/
964
965 protected:
966
967/*--------------------------------------------------------------------------*/
968/*----------------------- PROTECTED DATA STRUCTURES -----------------------*/
969/*--------------------------------------------------------------------------*/
979
982
985
989 int EStps;
990
992
995
1000
1001
1002 ostream *NDOLog;
1003 char NDOLLvl;
1004
1006
1008/*--------------------------------------------------------------------------*/
1009
1010 }; // end( class NDOSolver )
1011
1012/*--------------------------------------------------------------------------*/
1013/*------------------- inline methods implementation ------------------------*/
1014/*--------------------------------------------------------------------------*/
1015
1016inline NDOSolver::NDOSolver( std::istream *iStrm )
1017{
1018 // initialize algorithmic parameters - - - - - - - - - - - - - - - - - - - -
1019
1020 DfltdSfInpt( iStrm , MaxIter , Index( 0 ) );
1021 DfltdSfInpt( iStrm , MaxTime , HpNum( 0 ) );
1022
1023 DfltdSfInpt( iStrm , tStar , HpNum( 100 ) );
1024 DfltdSfInpt( iStrm , EpsLin , HpNum( 1e-6 ) );
1025
1026 DfltdSfInpt( iStrm , EInit , HpNum( 1e-2 ) );
1027 DfltdSfInpt( iStrm , EFnal , HpNum( 1e-6 ) );
1028 DfltdSfInpt( iStrm , EDcrs , HpNum( .95 ) );
1029 DfltdSfInpt( iStrm , EStps , int( 0 ) );
1030
1031 // other little initializations- - - - - - - - - - - - - - - - - - - - - - -
1032
1033 Result = kOK;
1034
1035 Oracle = 0;
1036 NumVar = NrFi = 0;
1037
1038 NDOLog = 0;
1039 NDOLLvl = 0;
1040
1041 NDOt = 0;
1042
1043 } // end( NDOSolver::NDOSolver )
1044
1045/*--------------------------------------------------------------------------*/
1046
1047inline void NDOSolver::SetPar( const int wp , const int value )
1048{
1049 switch( wp ) {
1050 case( kMaxItr ): MaxIter = Index( value ); break;
1051 case( kEStps ): EStps = value; break;
1052 default: throw(
1053 NDOException( "SetPar( Index ): unknown parameter" ) );
1054 }
1055 } // end( NDOSolver::SetPar( Index ) )
1056
1057/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
1058
1059inline void NDOSolver::SetPar( const int wp , cHpNum value )
1060{
1061 switch( wp ) {
1062 case( kMaxTme ): MaxTime = value; break;
1063 case( ktStar ): tStar = value; break;
1064 case( kEpsLin ): EpsLin = value; break;
1065 case( kEInit ): EInit = value; break;
1066 case( kEFnal ): EFnal = value; break;
1067 case( kEDcrs ): EDcrs = value; break;
1068 default: throw(
1069 NDOException( "SetPar( HpNum ): unknown parameter" ) );
1070 }
1071 } // end( NDOSolver::SetPar( HpNum ) )
1072
1073/*--------------------------------------------------------------------------*/
1074
1075inline void NDOSolver::GetPar( const int wp , int &value )
1076{
1077 switch( wp ) {
1078 case( kMaxItr ): value = int( MaxIter ); break;
1079 case( kEStps ): value = EStps; break;
1080 default: throw(
1081 NDOException( "GetPar( Index ): unknown parameter" ) );
1082 }
1083 } // end( NDOSolver::GetPar( Index ) )
1084
1085/*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
1086
1087inline void NDOSolver::GetPar( const int wp , HpNum &value )
1088{
1089 switch( wp ) {
1090 case( kMaxTme ): value = MaxTime; break;
1091 case( ktStar ): value = tStar; break;
1092 case( kEpsLin ): value = EpsLin; break;
1093 case( kEInit ): value = EInit; break;
1094 case( kEFnal ): value = EFnal; break;
1095 case( kEDcrs ): value = EDcrs; break;
1096 default: throw(
1097 NDOException( "GetPar( HpNum ): unknown parameter" ) );
1098 }
1099 } // end( NDOSolver::GetPar( HpNum ) )
1100
1101/*--------------------------------------------------------------------------*/
1102
1103 } // end( namespace NDO_di_unipi_it )
1104
1105/*--------------------------------------------------------------------------*/
1106/*--------------------------------------------------------------------------*/
1107
1108#endif /* NDOSlver.h included */
1109
1110/*--------------------------------------------------------------------------*/
1111/*------------------------- End File NDOSlver.h ----------------------------*/
1112/*--------------------------------------------------------------------------*/
Definition FiOracle.h:231
Definition FiOracle.h:66
Definition NDOSlver.h:60
Index MaxIter
maximum number of iterations
Definition NDOSlver.h:980
NDOSolver(std::istream *iStrm=0)
Definition NDOSlver.h:1016
Index NrIter(void) const
Definition NDOSlver.h:667
virtual void SetPar(const int wp, const int value)
Definition NDOSlver.h:1047
virtual void SetNDOTime(const bool TimeIt=true)
Definition NDOSlver.h:320
virtual cLMRow ReadBestSol(cIndex_Set &I, Index &D)
Definition NDOSlver.h:451
virtual HpNum ReadBestFiVal(cIndex wFi=Inf< Index >())
Definition NDOSlver.h:483
virtual void SetLambda(cLMRow tLambda=0)=0
Index NrFi
number of components of Fi()
Definition NDOSlver.h:994
NDOStatus
Definition NDOSlver.h:91
virtual void RemoveVariables(cIndex_Set whch=0, Index hwmny=0)
Definition NDOSlver.h:871
NDOParam
Definition NDOSlver.h:80
int EStps
precision-related parameter: number of steps
Definition NDOSlver.h:989
Index NrCalls(void) const
Definition NDOSlver.h:657
virtual ~NDOSolver()
Definition NDOSlver.h:959
Index GiEval(void) const
Definition NDOSlver.h:649
Index SCalls
nuber of calls to Solve() (the current included)
Definition NDOSlver.h:996
virtual void GetPar(const int wp, int &value)
Definition NDOSlver.h:1075
Index FiEvaltns
total number of Fi() calls
Definition NDOSlver.h:998
char NDOLLvl
the "level of verbosity" of the log
Definition NDOSlver.h:1003
virtual HpNum ReadLBMult(cIndex wFi=Inf< Index >())
Definition NDOSlver.h:633
HpNum EDcrs
precision-related parameter: rate of decrease
Definition NDOSlver.h:988
FiOracle * Oracle
(pointer to) the oracle for Fi
Definition NDOSlver.h:978
Index GiEvaltns
total number of Gi() calls
Definition NDOSlver.h:999
double NDOTime(void)
Definition NDOSlver.h:694
virtual HpNum ReadFiVal(cIndex wFi=Inf< Index >())=0
Index FiEval(void) const
Definition NDOSlver.h:641
HpNum EInit
precision-related parameter: initial precision
Definition NDOSlver.h:986
virtual void AddVariables(Index NNwVrs, cLMRow IVs=0)
Definition NDOSlver.h:797
HpNum EpsLin
optimality related parameter: relative precision
Definition NDOSlver.h:984
virtual bool IsOptimal(HpNum eps=0) const
Definition NDOSlver.h:497
virtual NDOStatus Solve(void)=0
virtual void SetNDOLog(ostream *outs=0, const char lvl=0)
Definition NDOSlver.h:301
Index GetNumVar(void) const
Definition NDOSlver.h:705
virtual void ReSetAlg(unsigned char RstLvl=0)
Definition NDOSlver.h:385
HpNum EFnal
precision-related parameter: final precision
Definition NDOSlver.h:987
HpNum tStar
optimality related parameter: "scaling" of Fi
Definition NDOSlver.h:983
virtual cLMRow ReadSol(cIndex_Set &I, Index &D)=0
HpNum MaxTime
maximum time (in seconds) for each call to Solve()
Definition NDOSlver.h:981
Index ParIter
nuber of iterations in this run
Definition NDOSlver.h:997
ostream * NDOLog
the output stream object for log purposes
Definition NDOSlver.h:1002
OPTtimers * NDOt
OPTtimer for timing purposes.
Definition NDOSlver.h:1005
virtual void ChgFiV(cIndex wFi=Inf< Index >())
Definition NDOSlver.h:911
virtual void SetFiOracle(FiOracle *Fi=0)
Definition NDOSlver.h:214
NDOStatus Result
result of the latest call to Solve()
Definition NDOSlver.h:991
virtual cHpRow ReadMult(cIndex_Set &I, Index &D, cIndex wFi=Inf< Index >())
Definition NDOSlver.h:603
Index NumVar
(current) number of variables
Definition NDOSlver.h:993
virtual void ChgSbG(cIndex strt=0, Index stp=Inf< Index >(), cIndex wFi=Inf< Index >())
Definition NDOSlver.h:943
void NDOTime(double &t_us, double &t_ss)
Definition NDOSlver.h:684
Definition OPTUtils.h:144
void ReSet(void)
reset the timer
Definition OPTUtils.h:214
double Read(void)
Definition OPTUtils.h:184
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
unsigned int Index
Index in a vector ( >= 0 )
Definition OPTtypes.h:60
cHpNum * cHpRow
read-only array
Definition OPTtypes.h:103
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
void DfltdSfInpt(istream *iStrm, T &Param, const T Dflt, const char cmntc='#')
Definition OPTUtils.h:392
Definition Bundle.h:68