00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "freGradientDescentOptimizerController.h"
00024 #include "freExceptions.h"
00025
00026
00027 namespace FREE
00028 {
00029
00033
00034
00035 GradientDescentOptimizerController::
00036 GradientDescentOptimizerController()
00037 {
00038
00039 this->UpdateControllerID(ControllerID::GradientDescentOptimizerController);
00040 this->m_Description = "Optimizes by a gradient descent approach.";
00041 };
00042
00043 void
00044 GradientDescentOptimizerController::
00045 GenerateProfile(CtrlProfile::ControllerProfile& profile,
00046 const SessionComponentCache* pComponentCache,
00047 bool bRegardOldSetup) const
00048 {
00049 Superclass::GenerateProfile(profile,pComponentCache,bRegardOldSetup);
00050
00051
00052 profile.Parameters().AddParameter(cParam_LearningRate,CtrlProfile::Parameter::PVTDouble,cParamDsc_LearningRate,1,"20",-1,true);
00053 profile.Parameters().AddParameter(cParam_Iterations,Parameter::PVTULong,cParamDsc_Iterations,1,"100",-1,true);
00054 };
00055
00056 void
00057 GradientDescentOptimizerController::
00058 ActualizeMainComponent(ComponentType* pMainComponent,
00059 SessionComponentCache* pComponentCache, SessionInfo* pSessionInfo,
00060 const unsigned int& iActLevel) const
00061 {
00062 unsigned long lNrOfIterations;
00063 double dLearningRate;
00064 try
00065 {
00066 SessionAccessor::GetParameterValue(pComponentCache,cParam_Iterations,lNrOfIterations,0,iActLevel,true);
00067 SessionAccessor::GetParameterValue(pComponentCache,cParam_LearningRate,dLearningRate,0,iActLevel,true);
00068 }
00069 catchAllNPassMacro("Error while retrieving parameter values.");
00070
00071 ParameterArrayType scales = this->GetTransformScales(pComponentCache,iActLevel);
00072 pMainComponent->SetScales(scales);
00073
00074 pMainComponent->SetNumberOfIterations(lNrOfIterations);
00075 pMainComponent->SetLearningRate(dLearningRate);
00076
00077 Parameter::Pointer smpDirection = pComponentCache->GetParentCache()->Controller()->GetParameter(pComponentCache->GetParentCache(),OptimizerOwnerControllerInterface::cParam_MinimizeToOptimize);
00078 if (smpDirection.IsNull()) throwCtrlExceptionMacro("","Cannot retrieve optimization direction via optimizer owner. Parameter \"MinimizeToOptimize\" is unknown. Please ensure valid session and setup");
00079 bool bMinimize;
00080 smpDirection->GetParameterValue(bMinimize);
00081 pMainComponent->SetMaximize( !bMinimize);
00082 };
00083
00084 void
00085 GradientDescentOptimizerController::
00086 SetStatisticEntryMainComponent(StatisticEntry& rStatisticEntry,
00087 ComponentType* pMainComponent,
00088 SessionComponentCache* pMainComponentCache,
00089 SessionInfo* pSessionInfo,
00090 StatisticDictionary& rDictionary) const
00091 {
00092 Superclass::SetStatisticEntryMainComponent(rStatisticEntry, pMainComponent, pMainComponentCache,
00093 pSessionInfo, rDictionary);
00094 std::string sName = "OptimizerValue";
00095 std::string sIDPath = pMainComponentCache->GetIDPath();
00096 StatisticValueDefinition* pEntry = rDictionary.GetValueDefinitionByName(sIDPath, sName);
00097
00098 if (!pEntry)
00099 {
00100 pEntry = rDictionary.AddValueDefinition(sName, sIDPath, "Value of the optimizer");
00101 }
00102
00103 rStatisticEntry.AddValue(Convert::ToStr(pMainComponent->GetValue()),pEntry->GetRefID());
00104 };
00105
00106 GradientDescentOptimizerController::MeasuresType
00107 GradientDescentOptimizerController::
00108 GetCurrentValue(ComponentType* pOptimizer) const
00109 {
00110 MeasuresType values(1);
00111 values.Fill(pOptimizer->GetValue());
00112 return values;
00113 };
00114
00115 }