Kevin Kok Khah Whey
2023-11-07 5ae534ab606e6f2ba5ea60914224d665b0447d5a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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 );
  *]
}