[9262] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
| 29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 30 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 32 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
|
---|
| 33 | namespace HeuristicLab.Problems.TradeRules {
|
---|
| 34 | [StorableClass]
|
---|
| 35 | public abstract class TradeRulesSingleObjectiveEvaluator : TradeRulesAnalysisSingleObjectiveEvaluator<IRegressionProblemData>, ISymbolicRegressionSingleObjectiveEvaluator
|
---|
| 36 | {
|
---|
| 37 | private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
|
---|
| 38 | public IFixedValueParameter<BoolValue> ApplyLinearScalingParameter {
|
---|
| 39 | get { return (IFixedValueParameter<BoolValue>)Parameters[ApplyLinearScalingParameterName]; }
|
---|
| 40 | }
|
---|
| 41 | public bool ApplyLinearScaling {
|
---|
| 42 | get { return ApplyLinearScalingParameter.Value.Value; }
|
---|
| 43 | set { ApplyLinearScalingParameter.Value.Value = value; }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | [StorableConstructor]
|
---|
| 47 | protected TradeRulesSingleObjectiveEvaluator(bool deserializing) : base(deserializing) { }
|
---|
| 48 | protected TradeRulesSingleObjectiveEvaluator(TradeRulesSingleObjectiveEvaluator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 49 | protected TradeRulesSingleObjectiveEvaluator()
|
---|
| 50 | : base() {
|
---|
| 51 | Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating.", new BoolValue(false)));
|
---|
| 52 | ApplyLinearScalingParameter.Hidden = true;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 56 | private void AfterDeserialization() {
|
---|
| 57 | if (!Parameters.ContainsKey(ApplyLinearScalingParameterName)) {
|
---|
| 58 | Parameters.Add(new FixedValueParameter<BoolValue>(ApplyLinearScalingParameterName, "Flag that indicates if the individual should be linearly scaled before evaluating.", new BoolValue(false)));
|
---|
| 59 | ApplyLinearScalingParameter.Hidden = true;
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | [ThreadStatic]
|
---|
| 64 | private static double[] cache;
|
---|
| 65 |
|
---|
| 66 | protected static void CalculateWithScaling(IEnumerable<double> targetValues, IEnumerable<double> estimatedValues,
|
---|
| 67 | double lowerEstimationLimit, double upperEstimationLimit,
|
---|
| 68 | IOnlineCalculator calculator, int maxRows) {
|
---|
| 69 | if (cache == null || cache.GetLength(0) < maxRows) {
|
---|
| 70 | cache = new double[maxRows];
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | //calculate linear scaling
|
---|
| 74 | //the static methods of the calculator could not be used as it performs a check if the enumerators have an equal amount of elements
|
---|
| 75 | //this is not true if the cache is used
|
---|
| 76 | int i = 0;
|
---|
| 77 | var linearScalingCalculator = new OnlineLinearScalingParameterCalculator();
|
---|
| 78 | var targetValuesEnumerator = targetValues.GetEnumerator();
|
---|
| 79 | var estimatedValuesEnumerator = estimatedValues.GetEnumerator();
|
---|
| 80 | while (targetValuesEnumerator.MoveNext() && estimatedValuesEnumerator.MoveNext()) {
|
---|
| 81 | double target = targetValuesEnumerator.Current;
|
---|
| 82 | double estimated = estimatedValuesEnumerator.Current;
|
---|
| 83 | cache[i] = estimated;
|
---|
| 84 | linearScalingCalculator.Add(estimated, target);
|
---|
| 85 | i++;
|
---|
| 86 | }
|
---|
| 87 | double alpha = linearScalingCalculator.Alpha;
|
---|
| 88 | double beta = linearScalingCalculator.Beta;
|
---|
| 89 |
|
---|
| 90 | //calculate the quality by using the passed online calculator
|
---|
| 91 | targetValuesEnumerator = targetValues.GetEnumerator();
|
---|
| 92 | var scaledBoundedEstimatedValuesEnumerator = Enumerable.Range(0, i).Select(x => cache[x] * beta + alpha)
|
---|
| 93 | .LimitToRange(lowerEstimationLimit, upperEstimationLimit).GetEnumerator();
|
---|
| 94 |
|
---|
| 95 | while (targetValuesEnumerator.MoveNext() & scaledBoundedEstimatedValuesEnumerator.MoveNext()) {
|
---|
| 96 | calculator.Add(targetValuesEnumerator.Current, scaledBoundedEstimatedValuesEnumerator.Current);
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | }
|
---|