| Quintiq file version 2.0 | 
| #parent: #root | 
| Function CalcIsActiveTuningLevel | 
| { | 
|   Description: | 
|   [* | 
|     This level will be actively tuned if: | 
|     + If the oldest tuning run of any of the SSGInSL related to this strategy level is older than the oldest tuning run on any other level | 
|     + The oldest tuning run is equally old and the default duration of this level is longer than on the other levels with the oldest tuning runs | 
|   *] | 
|   TextBody: | 
|   [* | 
|     // Martijn Apr-7-2016 (created) | 
|      | 
|     value := false; | 
|      | 
|     if( this.SolverSettingGroup( relsize ) > 0     // If there are SSGInSL for this strategy level | 
|         and this.Strategy().HasTestedBestSSG() )   // And the combination of best settings has already been tested | 
|     { | 
|       // Oldest tuning run of any of the settings on this level | 
|       oldesttuningrun := guard( min( this, SSGInStrategyLevel, ssginsl, ssginsl.DateLastTuningRun() ), | 
|                                 DateTime::MaxDateTime() ); | 
|      | 
|       // Oldest tuning run on any of the other levnmels of this strategy | 
|       oldesttuningrunotherlevels := guard( min( this, Strategy.StrategyLevel.SSGInStrategyLevel, ssginsl, | 
|                                                      ssginsl.Level() <> this.Level()                                                        // Other level | 
|                                                      and exists( ssginsl, StrategyLevel.KPIWeight, kpiweight, kpiweight.Weight() > 0.0 ),   // That is active in the strategy | 
|                                                      ssginsl.DateLastTuningRun() ), | 
|                                                 DateTime::MaxDateTime() ); | 
|      | 
|      | 
|       // If the oldest tuning run of this strategy level is older than the oldest tuning run of any other strategy this is the active tuning level | 
|       if( oldesttuningrun < oldesttuningrunotherlevels ) | 
|       { | 
|         value := true; | 
|       } | 
|       // Else, if the oldest tuning run of this strategy level is as old as the oldest tuning run of any other strategy | 
|       else if( oldesttuningrun = oldesttuningrunotherlevels ) | 
|       { | 
|       // Last duration of the currently default settings on this level | 
|       durationdefaultrun := guard( select( this, SSGInStrategyLevel, ssginsl, | 
|                                            ssginsl.SolverSettingGroupName() = this.SolverSettingGroupName() ).LastDuration(), | 
|                                    Duration::Zero() ); | 
|      | 
|       // Select the levels with the oldest tuning runs | 
|       // This will initially include all levels since their DateLastTuningRun is initially set to MinDate time. | 
|       // After all settings have been tried on all levels this will only include a single level | 
|       oldesttuninglevels := selectset( this, Strategy.StrategyLevel, strategylevel, | 
|                                        strategylevel.Level() <> this.Level()                                                                                  // Other level | 
|                                        and min( strategylevel, SSGInStrategyLevel, ssginsl, ssginsl.DateLastTuningRun() ) = oldesttuningrunotherlevels   // Has a SSGinSL whose LastTuning run is as old as the oldest run | 
|                                        and exists( strategylevel, KPIWeight, kpiweight, | 
|                                                    kpiweight.Weight() > 0.0 and kpiweight.Level() > 0.0 ) );                                                  // The strategy level must be an active level | 
|      | 
|      | 
|       // Maximum Last duration of the default setting on any of the levels that have the oldest tuning run | 
|       maxdefaultrunotherlevels  := guard( max( oldesttuninglevels, Elements.SSGInStrategyLevel, ssginsl, | 
|                                                ssginsl.SolverSettingGroupName() = ssginsl.StrategyLevel().SolverSettingGroupName(), | 
|                                                ssginsl.LastDuration() ), | 
|                                           Duration::MinDuration() ); | 
|      | 
|       // Maximum Last duration of the default setting on any of the previous levels that have the oldest tuning run | 
|       maxdefaultrunpreviouslevels := guard( max( oldesttuninglevels, Elements.SSGInStrategyLevel, ssginsl, | 
|                                                  ssginsl.SolverSettingGroupName() = ssginsl.StrategyLevel().SolverSettingGroupName() | 
|                                                  and ssginsl.Level() <= this.Level(), | 
|                                                  ssginsl.LastDuration() ), | 
|                                             Duration::MinDuration() ); | 
|      | 
|       // If the duration with the default settings on this level is longer than on any other level with an equally long tuning run | 
|       // (or as long as any later level with an equally old tuning run), then this is the active tuning level | 
|       if( durationdefaultrun >= maxdefaultrunotherlevels | 
|           and durationdefaultrun > maxdefaultrunpreviouslevels ) | 
|         { | 
|           value := true | 
|         } | 
|       } | 
|     } | 
|      | 
|     this.IsActiveTuningLevel( value ); | 
|   *] | 
| } |