Changeset 12794


Ignore:
Timestamp:
07/21/15 16:46:19 (5 years ago)
Author:
gkronber
Message:

#2439: handle maximization problems correctly in BFGS

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.GradientDescent/3.3/LbfgsUpdateResults.cs

    r12012 r12794  
    3737    private const string StateParameterName = "State";
    3838    private const string ApproximateGradientsParameterName = "ApproximateGradients";
     39    private const string MaximizationParameterName = "Maximization";
    3940
    4041    #region Parameter Properties
     
    5152      get { return (ILookupParameter<LbfgsState>)Parameters[StateParameterName]; }
    5253    }
     54    public ILookupParameter<BoolValue> MaximizationParameter {
     55      get { return (ILookupParameter<BoolValue>)Parameters[MaximizationParameterName]; }
     56    }
    5357    #endregion
    5458
     
    5862    private DoubleValue Quality { get { return QualityParameter.ActualValue; } }
    5963    private LbfgsState State { get { return StateParameter.ActualValue; } }
     64    private BoolValue Maximization { get { return MaximizationParameter.ActualValue; } }
    6065    #endregion
    6166
     
    7075      Parameters.Add(new LookupParameter<BoolValue>(ApproximateGradientsParameterName,
    7176                                                    "Flag that indicates if gradients should be approximated."));
     77      Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "Flag that indicates if we solve a maximization problem."));
    7278      // in & out
    7379      Parameters.Add(new LookupParameter<LbfgsState>(StateParameterName, "The state of the LM-BFGS algorithm."));
     80    }
     81
     82    [StorableHook(HookType.AfterDeserialization)]
     83    private void AfterDeserialization() {
     84      // BackwardsCompatibility3.3
     85
     86      #region Backwards compatible code, remove with 3.4
     87      if (!Parameters.ContainsKey(MaximizationParameterName)) {
     88        // previous behaviour defaulted to minimization
     89        Parameters.Add(new LookupParameter<BoolValue>(MaximizationParameterName, "Flag that indicates if we solve a maximization problem."));
     90      }
     91      #endregion
    7492    }
    7593
     
    8098    public override IOperation Apply() {
    8199      var state = State;
    82       var f = Quality.Value;
     100      var sign = Maximization.Value ? -1.0 : 1.0;
     101      var f = sign * Quality.Value;
    83102      state.State.f = f;
    84103      if (!ApproximateGradients.Value) {
    85         var g = QualityGradients.ToArray();
     104        var g = QualityGradients.Select(gi => sign * gi).ToArray();
    86105        state.State.g = g;
    87106      }
Note: See TracChangeset for help on using the changeset viewer.