Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/19/17 16:53:09 (8 years ago)
Author:
bburlacu
Message:

#1772: Merge trunk changes.

Location:
branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic

  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisAlleleFrequencyAnalyzer.cs

    r14312 r14878  
    8989      var varTreeNode = tree as VariableTreeNode;
    9090      var constTreeNode = tree as ConstantTreeNode;
     91      var factorVarTreeNode = tree as FactorVariableTreeNode;
     92      var binFactorVarTreeNode = tree as BinaryFactorVariableTreeNode;
    9193      if (varTreeNode != null) {
    9294        builder.Append("(var " + varTreeNode.VariableName);
     95      } else if (factorVarTreeNode != null) {
     96        builder.Append("(factor " + factorVarTreeNode.VariableName);
     97      } else if (binFactorVarTreeNode != null) {
     98        builder.Append("(factor " + binFactorVarTreeNode.VariableName + "=" + binFactorVarTreeNode.VariableValue);
    9399      } else if (constTreeNode != null) {
    94100        builder.Append("(const");
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer.cs

    r14312 r14878  
    3535  [Item("SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer", "An operator that analyzes the training best symbolic data analysis solution for single objective symbolic data analysis problems.")]
    3636  [StorableClass]
    37   public abstract class SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer<T> : SymbolicDataAnalysisSingleObjectiveAnalyzer, IIterationBasedOperator
    38 
    39     where T : class, ISymbolicDataAnalysisSolution {
     37  public abstract class SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer<T> : SymbolicDataAnalysisSingleObjectiveAnalyzer, IIterationBasedOperator where T : class, ISymbolicDataAnalysisSolution {
    4038    private const string TrainingBestSolutionParameterName = "Best training solution";
    4139    private const string TrainingBestSolutionQualityParameterName = "Best training solution quality";
    4240    private const string TrainingBestSolutionGenerationParameterName = "Best training solution generation";
     41    private const string TrainingBestSolutionsHistoryParameterName = "Best training solutions history";
    4342    private const string UpdateAlwaysParameterName = "Always update best solution";
    4443    private const string IterationsParameterName = "Iterations";
    4544    private const string MaximumIterationsParameterName = "Maximum Iterations";
     45    private const string StoreHistoryParameterName = "Store History";
    4646
    4747    #region parameter properties
     
    5555      get { return (ILookupParameter<IntValue>)Parameters[TrainingBestSolutionGenerationParameterName]; }
    5656    }
     57    public ILookupParameter<ItemList<T>> TrainingBestSolutionsHistoryParameter {
     58      get { return (ILookupParameter<ItemList<T>>)Parameters[TrainingBestSolutionsHistoryParameterName]; }
     59    }
    5760    public IFixedValueParameter<BoolValue> UpdateAlwaysParameter {
    5861      get { return (IFixedValueParameter<BoolValue>)Parameters[UpdateAlwaysParameterName]; }
     
    6366    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
    6467      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumIterationsParameterName]; }
     68    }
     69
     70    public IFixedValueParameter<BoolValue> StoreHistoryParameter {
     71      get { return (IFixedValueParameter<BoolValue>)Parameters[StoreHistoryParameterName]; }
    6572    }
    6673    #endregion
     
    7481      set { TrainingBestSolutionQualityParameter.ActualValue = value; }
    7582    }
    76     public BoolValue UpdateAlways {
    77       get { return UpdateAlwaysParameter.Value; }
     83    public bool UpdateAlways {
     84      get { return UpdateAlwaysParameter.Value.Value; }
     85      set { UpdateAlwaysParameter.Value.Value = value; }
     86    }
     87
     88    public bool StoreHistory {
     89      get { return StoreHistoryParameter.Value.Value; }
     90      set { StoreHistoryParameter.Value.Value = value; }
    7891    }
    7992    #endregion
     93
    8094
    8195    [StorableConstructor]
     
    8498    public SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer()
    8599      : base() {
    86       Parameters.Add(new LookupParameter<T>(TrainingBestSolutionParameterName, "The training best symbolic data analyis solution."));
     100      Parameters.Add(new LookupParameter<T>(TrainingBestSolutionParameterName, "The best training symbolic data analyis solution."));
    87101      Parameters.Add(new LookupParameter<DoubleValue>(TrainingBestSolutionQualityParameterName, "The quality of the training best symbolic data analysis solution."));
    88102      Parameters.Add(new LookupParameter<IntValue>(TrainingBestSolutionGenerationParameterName, "The generation in which the best training solution was found."));
     
    90104      Parameters.Add(new LookupParameter<IntValue>(IterationsParameterName, "The number of performed iterations."));
    91105      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsParameterName, "The maximum number of performed iterations.") { Hidden = true });
     106      Parameters.Add(new FixedValueParameter<BoolValue>(StoreHistoryParameterName, "Flag that determines whether all encountered best solutions should be stored as results.", new BoolValue(false)));
     107      Parameters.Add(new LookupParameter<ItemList<T>>(TrainingBestSolutionsHistoryParameterName, "The history of the best training symbolic data analysis solutions."));
    92108      UpdateAlwaysParameter.Hidden = true;
    93109    }
     
    105121      if (!Parameters.ContainsKey(MaximumIterationsParameterName))
    106122        Parameters.Add(new ValueLookupParameter<IntValue>(MaximumIterationsParameterName, "The maximum number of performed iterations.") { Hidden = true });
     123      if (!Parameters.ContainsKey(StoreHistoryParameterName))
     124        Parameters.Add(new FixedValueParameter<BoolValue>(StoreHistoryParameterName, "Flag that determines whether all encountered best solutions should be stored as results.", new BoolValue(false)));
     125      if (!Parameters.ContainsKey(TrainingBestSolutionsHistoryParameterName))
     126        Parameters.Add(new LookupParameter<ItemList<T>>(TrainingBestSolutionsHistoryParameterName, "The history of the best training symbolic data analysis solutions."));
    107127    }
    108128
    109129    public override IOperation Apply() {
     130      var results = ResultCollection;
     131      #region create results
     132      if (!results.ContainsKey(TrainingBestSolutionParameter.Name))
     133        results.Add(new Result(TrainingBestSolutionParameter.Name, TrainingBestSolutionParameter.Description, typeof(T)));
     134      if (!results.ContainsKey(TrainingBestSolutionQualityParameter.Name))
     135        results.Add(new Result(TrainingBestSolutionQualityParameter.Name, TrainingBestSolutionQualityParameter.Description, typeof(DoubleValue)));
     136      if (!results.ContainsKey(TrainingBestSolutionGenerationParameter.Name) && IterationsParameter.ActualValue != null)
     137        results.Add(new Result(TrainingBestSolutionGenerationParameter.Name, TrainingBestSolutionGenerationParameter.Description, typeof(IntValue)));
     138      if (StoreHistory && !results.ContainsKey(TrainingBestSolutionsHistoryParameter.Name)) {
     139
     140        results.Add(new Result(TrainingBestSolutionsHistoryParameter.Name, TrainingBestSolutionsHistoryParameter.Description, typeof(ItemList<T>)));
     141        TrainingBestSolutionsHistoryParameter.ActualValue = new ItemList<T>();
     142        results[TrainingBestSolutionsHistoryParameter.Name].Value = TrainingBestSolutionsHistoryParameter.ActualValue;
     143      }
     144      #endregion
     145
    110146      #region find best tree
    111147      double bestQuality = Maximization.Value ? double.NegativeInfinity : double.PositiveInfinity;
     
    121157      #endregion
    122158
    123       var results = ResultCollection;
    124       if (bestTree != null && (UpdateAlways.Value || TrainingBestSolutionQuality == null ||
     159      if (bestTree != null && (UpdateAlways || TrainingBestSolutionQuality == null ||
    125160        IsBetter(bestQuality, TrainingBestSolutionQuality.Value, Maximization.Value))) {
    126161        TrainingBestSolution = CreateSolution(bestTree, bestQuality);
     
    129164          TrainingBestSolutionGenerationParameter.ActualValue = new IntValue(IterationsParameter.ActualValue.Value);
    130165
    131         if (!results.ContainsKey(TrainingBestSolutionParameter.Name)) {
    132           results.Add(new Result(TrainingBestSolutionParameter.Name, TrainingBestSolutionParameter.Description, TrainingBestSolution));
    133           results.Add(new Result(TrainingBestSolutionQualityParameter.Name, TrainingBestSolutionQualityParameter.Description, TrainingBestSolutionQuality));
    134           if (TrainingBestSolutionGenerationParameter.ActualValue != null)
    135             results.Add(new Result(TrainingBestSolutionGenerationParameter.Name, TrainingBestSolutionGenerationParameter.Description, TrainingBestSolutionGenerationParameter.ActualValue));
    136         } else {
    137           results[TrainingBestSolutionParameter.Name].Value = TrainingBestSolution;
    138           results[TrainingBestSolutionQualityParameter.Name].Value = TrainingBestSolutionQuality;
    139           if (TrainingBestSolutionGenerationParameter.ActualValue != null)
    140             results[TrainingBestSolutionGenerationParameter.Name].Value = TrainingBestSolutionGenerationParameter.ActualValue;
     166        results[TrainingBestSolutionParameter.Name].Value = TrainingBestSolution;
     167        results[TrainingBestSolutionQualityParameter.Name].Value = TrainingBestSolutionQuality;
     168        if (TrainingBestSolutionGenerationParameter.ActualValue != null)
     169          results[TrainingBestSolutionGenerationParameter.Name].Value = TrainingBestSolutionGenerationParameter.ActualValue;
    141170
     171        if (StoreHistory) {
     172          TrainingBestSolutionsHistoryParameter.ActualValue.Add(TrainingBestSolution);
    142173        }
    143174      }
  • branches/HeuristicLab.EvolutionTracking/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisVariableFrequencyAnalyzer.cs

    r14312 r14878  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Globalization;
    2425using System.Linq;
    2526using HeuristicLab.Analysis;
     
    4142    private const string VariableFrequenciesParameterName = "VariableFrequencies";
    4243    private const string AggregateLaggedVariablesParameterName = "AggregateLaggedVariables";
     44    private const string AggregateFactorVariablesParameterName = "AggregateFactorVariables";
    4345    private const string VariableImpactsParameterName = "VariableImpacts";
    4446
     
    5254    public IValueLookupParameter<BoolValue> AggregateLaggedVariablesParameter {
    5355      get { return (IValueLookupParameter<BoolValue>)Parameters[AggregateLaggedVariablesParameterName]; }
     56    }
     57    public IValueLookupParameter<BoolValue> AggregateFactorVariablesParameter {
     58      get { return (IValueLookupParameter<BoolValue>)Parameters[AggregateFactorVariablesParameterName]; }
    5459    }
    5560    #endregion
     
    5964      set { AggregateLaggedVariablesParameter.Value = value; }
    6065    }
     66    public BoolValue AggregateFactorVariables {
     67      get { return AggregateFactorVariablesParameter.ActualValue; }
     68      set { AggregateFactorVariablesParameter.Value = value; }
     69    }
    6170    #endregion
    6271    [StorableConstructor]
     
    7079      Parameters.Add(new LookupParameter<DoubleMatrix>(VariableImpactsParameterName, "The relative variable relevance calculated as the average relative variable frequency over the whole run."));
    7180      Parameters.Add(new ValueLookupParameter<BoolValue>(AggregateLaggedVariablesParameterName, "Switch that determines whether all references to a variable should be aggregated regardless of time-offsets. Turn off to analyze all variable references with different time offsets separately.", new BoolValue(true)));
     81      Parameters.Add(new ValueLookupParameter<BoolValue>(AggregateFactorVariablesParameterName, "Switch that determines whether all references to factor variables should be aggregated regardless of the value. Turn off to analyze all factor variable references with different values separately.", new BoolValue(true)));
     82    }
     83
     84    [StorableHook(HookType.AfterDeserialization)]
     85    private void AfterDeserialization() {
     86      // BackwardsCompatibility3.3
     87      #region Backwards compatible code, remove with 3.4
     88      if (!Parameters.ContainsKey(AggregateFactorVariablesParameterName)) {
     89        Parameters.Add(new ValueLookupParameter<BoolValue>(AggregateFactorVariablesParameterName, "Switch that determines whether all references to factor variables should be aggregated regardless of the value. Turn off to analyze all factor variable references with different values separately.", new BoolValue(true)));
     90      }
     91      #endregion
    7292    }
    7393
     
    93113      int numberOfValues = datatable.Rows.Select(r => r.Values.Count).DefaultIfEmpty().First();
    94114
    95       foreach (var pair in SymbolicDataAnalysisVariableFrequencyAnalyzer.CalculateVariableFrequencies(expressions, AggregateLaggedVariables.Value)) {
     115      foreach (var pair in CalculateVariableFrequencies(expressions, AggregateLaggedVariables.Value, AggregateFactorVariables.Value)) {
    96116        if (!datatable.Rows.ContainsKey(pair.Key)) {
    97117          // initialize a new row for the variable and pad with zeros
     
    128148    }
    129149
    130     public static IEnumerable<KeyValuePair<string, double>> CalculateVariableFrequencies(IEnumerable<ISymbolicExpressionTree> trees, bool aggregateLaggedVariables = true) {
     150    public static IEnumerable<KeyValuePair<string, double>> CalculateVariableFrequencies(IEnumerable<ISymbolicExpressionTree> trees,
     151      bool aggregateLaggedVariables = true, bool aggregateFactorVariables = true) {
    131152
    132153      var variableFrequencies = trees
    133         .SelectMany(t => GetVariableReferences(t, aggregateLaggedVariables))
     154        .SelectMany(t => GetVariableReferences(t, aggregateLaggedVariables, aggregateFactorVariables))
    134155        .GroupBy(pair => pair.Key, pair => pair.Value)
    135156        .ToDictionary(g => g.Key, g => (double)g.Sum());
     
    141162    }
    142163
    143     private static IEnumerable<KeyValuePair<string, int>> GetVariableReferences(ISymbolicExpressionTree tree, bool aggregateLaggedVariables = true) {
     164    private static IEnumerable<KeyValuePair<string, int>> GetVariableReferences(ISymbolicExpressionTree tree,
     165      bool aggregateLaggedVariables = true, bool aggregateFactorVariables = true) {
    144166      Dictionary<string, int> references = new Dictionary<string, int>();
    145167      if (aggregateLaggedVariables) {
    146168        tree.Root.ForEachNodePrefix(node => {
    147           if (node.Symbol is Variable) {
    148             var varNode = node as VariableTreeNode;
    149             IncReferenceCount(references, varNode.VariableName);
    150           } else if (node.Symbol is VariableCondition) {
    151             var varCondNode = node as VariableConditionTreeNode;
    152             IncReferenceCount(references, varCondNode.VariableName);
     169          if (node is IVariableTreeNode) {
     170            var factorNode = node as BinaryFactorVariableTreeNode;
     171            if (factorNode != null && !aggregateFactorVariables) {
     172              IncReferenceCount(references, factorNode.VariableName + "=" + factorNode.VariableValue);
     173            } else {
     174              var varNode = node as IVariableTreeNode;
     175              IncReferenceCount(references, varNode.VariableName);
     176            }
    153177          }
    154178        });
    155179      } else {
    156         GetVariableReferences(references, tree.Root, 0);
     180        GetVariableReferences(references, tree.Root, 0, aggregateFactorVariables);
    157181      }
    158182      return references;
    159183    }
    160184
    161     private static void GetVariableReferences(Dictionary<string, int> references, ISymbolicExpressionTreeNode node, int currentLag) {
    162       if (node.Symbol is LaggedVariable) {
    163         var laggedVarNode = node as LaggedVariableTreeNode;
    164         IncReferenceCount(references, laggedVarNode.VariableName, currentLag + laggedVarNode.Lag);
    165       } else if (node.Symbol is Variable) {
    166         var varNode = node as VariableTreeNode;
    167         IncReferenceCount(references, varNode.VariableName, currentLag);
    168       } else if (node.Symbol is VariableCondition) {
    169         var varCondNode = node as VariableConditionTreeNode;
    170         IncReferenceCount(references, varCondNode.VariableName, currentLag);
    171         GetVariableReferences(references, node.GetSubtree(0), currentLag);
    172         GetVariableReferences(references, node.GetSubtree(1), currentLag);
     185    private static void GetVariableReferences(Dictionary<string, int> references, ISymbolicExpressionTreeNode node, int currentLag, bool aggregateFactorVariables) {
     186      if (node is IVariableTreeNode) {
     187        var laggedVarTreeNode = node as LaggedVariableTreeNode;
     188        var binFactorVariableTreeNode = node as BinaryFactorVariableTreeNode;
     189        var varConditionTreeNode = node as VariableConditionTreeNode;
     190        if (laggedVarTreeNode != null) {
     191          IncReferenceCount(references, laggedVarTreeNode.VariableName, currentLag + laggedVarTreeNode.Lag);
     192        } else if (binFactorVariableTreeNode != null) {
     193          if (aggregateFactorVariables) {
     194            IncReferenceCount(references, binFactorVariableTreeNode.VariableName, currentLag);
     195          } else {
     196            IncReferenceCount(references, binFactorVariableTreeNode.VariableName + "=" + binFactorVariableTreeNode.VariableValue, currentLag);
     197          }
     198        } else if (varConditionTreeNode != null) {
     199          IncReferenceCount(references, varConditionTreeNode.VariableName, currentLag);
     200          GetVariableReferences(references, node.GetSubtree(0), currentLag, aggregateFactorVariables);
     201          GetVariableReferences(references, node.GetSubtree(1), currentLag, aggregateFactorVariables);
     202        } else {
     203          var varNode = node as IVariableTreeNode;
     204          IncReferenceCount(references, varNode.VariableName, currentLag);
     205        }
    173206      } else if (node.Symbol is Integral) {
    174207        var laggedNode = node as LaggedTreeNode;
    175208        for (int l = laggedNode.Lag; l <= 0; l++) {
    176           GetVariableReferences(references, node.GetSubtree(0), currentLag + l);
     209          GetVariableReferences(references, node.GetSubtree(0), currentLag + l, aggregateFactorVariables);
    177210        }
    178211      } else if (node.Symbol is Derivative) {
    179212        for (int l = -4; l <= 0; l++) {
    180           GetVariableReferences(references, node.GetSubtree(0), currentLag + l);
     213          GetVariableReferences(references, node.GetSubtree(0), currentLag + l, aggregateFactorVariables);
    181214        }
    182215      } else if (node.Symbol is TimeLag) {
    183216        var laggedNode = node as LaggedTreeNode;
    184         GetVariableReferences(references, node.GetSubtree(0), currentLag + laggedNode.Lag);
     217        GetVariableReferences(references, node.GetSubtree(0), currentLag + laggedNode.Lag, aggregateFactorVariables);
    185218      } else {
    186219        foreach (var subtree in node.Subtrees) {
    187           GetVariableReferences(references, subtree, currentLag);
     220          GetVariableReferences(references, subtree, currentLag, aggregateFactorVariables);
    188221        }
    189222      }
Note: See TracChangeset for help on using the changeset viewer.