Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15975


Ignore:
Timestamp:
06/28/18 16:56:41 (6 years ago)
Author:
bburlacu
Message:

#2886: address additional serialization issues, make Production implement IList<T> instead of deriving from List<T>

Location:
branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Grammar.cs

    r15974 r15975  
    2424    public Symbol StartSymbol { get; private set; }
    2525
    26     public Hasher<int> Hasher { get; }
     26    public Hasher<int> Hasher { get; private set; }
    2727
    2828    #region Symbols
     
    7777    public Grammar(string[] variables) : this(variables, Enum.GetValues(typeof(GrammarRule)).Cast<GrammarRule>()) { }
    7878
     79    [StorableConstructor]
     80    protected Grammar(bool deserializing) {
     81      InitTreeParser();
     82    }
     83
    7984    protected Grammar(Grammar original, Cloner cloner) : base(original, cloner) {
    8085      infixExpressionFormatter = cloner.Clone(original.infixExpressionFormatter);
     
    8388      VarTerminals = original.VarTerminals.Select(cloner.Clone).ToList();
    8489
    85       Var = cloner.Clone(original.Var);
    86       Expr = cloner.Clone(original.Expr);
    87       Term = cloner.Clone(original.Term);
    88       Factor = cloner.Clone(original.Factor);
    89       LogFactor = cloner.Clone(original.LogFactor);
    90       ExpFactor = cloner.Clone(original.ExpFactor);
    91       SinFactor = cloner.Clone(original.SinFactor);
    92       SimpleExpr = cloner.Clone(original.SimpleExpr);
    93       SimpleTerm = cloner.Clone(original.SimpleTerm);
    94       InvExpr = cloner.Clone(original.InvExpr);
    95       InvTerm = cloner.Clone(original.InvTerm);
    96 
    97       Addition = cloner.Clone(original.Addition);
    98       Multiplication = cloner.Clone(original.Multiplication);
    99       Log = cloner.Clone(original.Log);
    100       Exp = cloner.Clone(original.Exp);
    101       Sin = cloner.Clone(original.Sin);
    102       Inv = cloner.Clone(original.Inv);
    103       Const = cloner.Clone(original.Const);
     90      Var = (NonterminalSymbol)cloner.Clone(original.Var);
     91      Expr = (NonterminalSymbol)cloner.Clone(original.Expr);
     92      Term = (NonterminalSymbol)cloner.Clone(original.Term);
     93      Factor = (NonterminalSymbol)cloner.Clone(original.Factor);
     94      LogFactor = (NonterminalSymbol)cloner.Clone(original.LogFactor);
     95      ExpFactor = (NonterminalSymbol)cloner.Clone(original.ExpFactor);
     96      SinFactor = (NonterminalSymbol)cloner.Clone(original.SinFactor);
     97      SimpleExpr = (NonterminalSymbol)cloner.Clone(original.SimpleExpr);
     98      SimpleTerm = (NonterminalSymbol)cloner.Clone(original.SimpleTerm);
     99      InvExpr = (NonterminalSymbol)cloner.Clone(original.InvExpr);
     100      InvTerm = (NonterminalSymbol)cloner.Clone(original.InvTerm);
     101
     102      Addition = (TerminalSymbol)cloner.Clone(original.Addition);
     103      Multiplication = (TerminalSymbol)cloner.Clone(original.Multiplication);
     104      Log = (TerminalSymbol)cloner.Clone(original.Log);
     105      Exp = (TerminalSymbol)cloner.Clone(original.Exp);
     106      Sin = (TerminalSymbol)cloner.Clone(original.Sin);
     107      Inv = (TerminalSymbol)cloner.Clone(original.Inv);
     108      Const = (TerminalSymbol)cloner.Clone(original.Const);
    104109
    105110      StartSymbol = Expr;
     
    240245    public double EvaluatePhrase(SymbolString s, IRegressionProblemData problemData, bool optimizeConstants) {
    241246      SymbolicExpressionTree tree = ParseSymbolicExpressionTree(s);
     247
    242248      return RSquaredEvaluator.Evaluate(problemData, tree, optimizeConstants);
    243249    }
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/GrammarEnumerationAlgorithm.cs

    r15974 r15975  
    5252    }
    5353
    54     protected double VariableImportanceWeight {
     54    public double VariableImportanceWeight {
    5555      get { return VariableImportanceWeightParameter.Value.Value; }
     56      set { VariableImportanceWeightParameter.Value.Value = value; }
    5657    }
    5758
     
    133134    }
    134135
     136    [Storable]
    135137    public SymbolString BestTrainingSentence { get; set; }     // Currently set in RSquaredEvaluator: quite hacky, but makes testing much easier for now...
    136138    #endregion
     
    144146    [Storable]
    145147    internal SearchDataStore OpenPhrases { get; private set; }           // Stack/Queue/etc. for fetching the next node in the search tree. 
    146 
    147     [StorableHook(HookType.AfterDeserialization)]
    148     private void AfterDeserialization() {
    149       variableImportance = CalculateVariableImportances();
    150     }
    151148
    152149    #region execution stats
     
    162159    #endregion
    163160
     161    [Storable]
    164162    public Grammar Grammar { get; private set; }
    165163
     
    168166      return new GrammarEnumerationAlgorithm(this, cloner);
    169167    }
     168
     169    [StorableConstructor]
     170    protected GrammarEnumerationAlgorithm(bool deserializing) : base(deserializing) { }
    170171
    171172    public GrammarEnumerationAlgorithm() {
     
    237238    #endregion
    238239
     240    [Storable]
    239241    private Dictionary<VariableTerminalSymbol, double> variableImportance;
    240242
     
    250252      OpenPhrases = new SearchDataStore(SearchDataStructure, SearchDataStructureSize, SearchCacheSize); // Select search strategy
    251253
     254      CalculateVariableImportances();
     255
    252256      base.Prepare(); // this actually clears the results which will get reinitialized on Run()
    253257    }
    254258
    255     private Dictionary<VariableTerminalSymbol, double> CalculateVariableImportances() {
     259    private void CalculateVariableImportances() {
    256260      variableImportance = new Dictionary<VariableTerminalSymbol, double>();
    257261
     
    271275        variableImportance[varSym] = rfImpact.Item2 / sum;
    272276      }
    273       return variableImportance;
    274277    }
    275278
     
    277280      // do not reinitialize the algorithm if we're resuming from pause
    278281      if (previousExecutionState != ExecutionState.Paused) {
    279         CalculateVariableImportances();
    280282        InitResults();
    281283        var phrase0 = new SymbolString(new[] { Grammar.StartSymbol });
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/GrammarEnumeration/Symbol.cs

    r15974 r15975  
    11using System;
     2using System.Collections;
    23using System.Collections.Generic;
     4using System.Linq;
    35using HeuristicLab.Common;
    46using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     
    101103
    102104  [StorableClass]
    103   public class Production : List<Symbol>, IDeepCloneable {
     105  public class Production : DeepCloneable, IList<Symbol> {
     106    [Storable]
     107    private List<Symbol> symbols;
    104108
    105     public Production(params Symbol[] symbols) : base(symbols) { }
     109    public int Count { get { return symbols.Count; } }
    106110
    107     public Production(IEnumerable<Symbol> symbols) : base(symbols) { }
     111    public bool IsReadOnly => throw new NotImplementedException();
     112
     113    public Symbol this[int index] { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
     114
     115    public Production(params Symbol[] symbols) {
     116      this.symbols = symbols.ToList();
     117    }
     118
     119    public Production(IEnumerable<Symbol> symbols) {
     120      this.symbols = symbols.ToList();
     121    }
    108122
    109123    [StorableConstructor]
    110124    protected Production(bool deserializing) { }
    111125
    112     protected Production(Production original, Cloner cloner) : this(original) {
    113       cloner.RegisterClonedObject(original, this);
     126    protected Production(Production original, Cloner cloner) {
     127      symbols = original.symbols.Select(cloner.Clone).ToList();
    114128    }
    115129
    116     public IDeepCloneable Clone(Cloner cloner) {
     130    public override IDeepCloneable Clone(Cloner cloner) {
    117131      return new Production(this, cloner);
    118     }
    119 
    120     public object Clone() {
    121       return Clone(new Cloner());
    122132    }
    123133
     
    125135      return string.Join(" ", this);
    126136    }
     137
     138    #region IList<Symbol> methods
     139    public IEnumerator<Symbol> GetEnumerator() {
     140      return symbols.GetEnumerator();
     141    }
     142
     143    IEnumerator IEnumerable.GetEnumerator() {
     144      return GetEnumerator();
     145    }
     146
     147    public int IndexOf(Symbol item) {
     148      return symbols.IndexOf(item);
     149    }
     150
     151    public void Insert(int index, Symbol item) {
     152      symbols.Insert(index, item);
     153    }
     154
     155    public void RemoveAt(int index) {
     156      symbols.RemoveAt(index);
     157    }
     158
     159    public void Add(Symbol item) {
     160      symbols.Add(item);
     161    }
     162
     163    public void Clear() {
     164      symbols.Clear();
     165    }
     166
     167    public bool Contains(Symbol item) {
     168      return symbols.Contains(item);
     169    }
     170
     171    public void CopyTo(Symbol[] array, int arrayIndex) {
     172      symbols.CopyTo(array, arrayIndex);
     173    }
     174
     175    public void CopyTo(int index, Symbol[] array, int arrayIndex, int count) {
     176      symbols.CopyTo(index, array, arrayIndex, count);
     177    }
     178
     179    public bool Remove(Symbol item) {
     180      return symbols.Remove(item);
     181    }
     182    #endregion
    127183  }
    128184}
  • branches/2886_SymRegGrammarEnumeration/HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration/Hashing/Hasher.cs

    r15965 r15975  
    1717    }
    1818
    19     protected Grammar Grammar { get; }
     19    [Storable]
     20    protected Grammar Grammar { get; private set; }
    2021
    2122    protected abstract THashType AggregateHashes(Symbol operatorSym, IList<THashType> hashes);
Note: See TracChangeset for help on using the changeset viewer.