Free cookie consent management tool by TermsFeed Policy Generator

Changeset 6814


Ignore:
Timestamp:
09/21/11 15:51:43 (13 years ago)
Author:
mkommend
Message:

#1479: Adapted symbolic expression encoding unit tests.

Location:
trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs

    r6803 r6814  
    5252    [Storable(Name = "AllowedChildSymbols")]
    5353    private IEnumerable<KeyValuePair<ISymbol, IEnumerable<ISymbol>>> StorableAllowedChildSymbols {
    54       get { return allowedChildSymbols.Select(x => new KeyValuePair<ISymbol, IEnumerable<ISymbol>>(GetSymbol(x.Key), x.Value.Select(y => GetSymbol(y)).ToArray())).ToArray(); }
     54      get { return allowedChildSymbols.Select(x => new KeyValuePair<ISymbol, IEnumerable<ISymbol>>(GetSymbol(x.Key), x.Value.Select(GetSymbol).ToArray())).ToArray(); }
    5555      set { allowedChildSymbols = value.ToDictionary(x => x.Key.Name, x => x.Value.Select(y => y.Name).ToList()); }
    5656    }
     
    8383      cachedMinExpressionDepth = new Dictionary<string, int>();
    8484
     85      cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>();
     86      cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>();
     87
    8588      suppressEvents = false;
    8689    }
     
    9295      cachedMinExpressionDepth = new Dictionary<string, int>();
    9396
    94       symbols = original.symbols.ToDictionary(x => x.Key, y => (ISymbol)cloner.Clone(y.Value));
     97      cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>();
     98      cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>();
     99
     100      symbols = original.symbols.ToDictionary(x => x.Key, y => cloner.Clone(y.Value));
    95101      symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>(original.symbolSubtreeCount);
    96102
     
    111117      cachedMaxExpressionLength = new Dictionary<string, int>();
    112118      cachedMinExpressionDepth = new Dictionary<string, int>();
     119
     120      cachedIsAllowedChildSymbol = new Dictionary<Tuple<string, string>, bool>();
     121      cachedIsAllowedChildSymbolIndex = new Dictionary<Tuple<string, string, int>, bool>();
    113122
    114123      symbols = new Dictionary<string, ISymbol>();
     
    301310    }
    302311
     312    private readonly Dictionary<Tuple<string, string>, bool> cachedIsAllowedChildSymbol;
    303313    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child) {
    304314      if (!child.Enabled) return false;
    305315
     316      bool result;
     317      if (cachedIsAllowedChildSymbol.TryGetValue(Tuple.Create(parent.Name, child.Name), out result)) return result;
    306318      List<string> temp;
    307319      if (allowedChildSymbols.TryGetValue(parent.Name, out temp)) {
    308         if (temp.Contains(child.Name)) return true;
    309         if (temp.SelectMany(s => GetSymbol(s).Flatten().Select(n => n.Name)).Contains(child.Name)) return true;
    310       }
     320        //if (temp.Contains(child.Name)) return true;
     321        if (temp.SelectMany(s => GetSymbol(s).Flatten()).Where(s => s.Name == child.Name).Any()) {
     322          cachedIsAllowedChildSymbol.Add(Tuple.Create(parent.Name, child.Name), true);
     323          return true;
     324        }
     325      }
     326      cachedIsAllowedChildSymbol.Add(Tuple.Create(parent.Name, child.Name), false);
    311327      return false;
    312328    }
    313329
     330    private readonly Dictionary<Tuple<string, string, int>, bool> cachedIsAllowedChildSymbolIndex;
    314331    public virtual bool IsAllowedChildSymbol(ISymbol parent, ISymbol child, int argumentIndex) {
    315332      if (!child.Enabled) return false;
    316333      if (IsAllowedChildSymbol(parent, child)) return true;
    317334
     335      bool result;
     336      if (cachedIsAllowedChildSymbolIndex.TryGetValue(Tuple.Create(parent.Name, child.Name, argumentIndex), out result)) return result;
    318337      List<string> temp;
    319338      var key = Tuple.Create(parent.Name, argumentIndex);
    320339      if (allowedChildSymbolsPerIndex.TryGetValue(key, out temp)) {
    321         if (temp.Contains(child.Name)) return true;
    322         if (temp.SelectMany(s => GetSymbol(s).Flatten().Select(n => n.Name)).Contains(child.Name)) return true;
    323       }
     340        //if (temp.Contains(child.Name)) return true;
     341        if (temp.SelectMany(s => GetSymbol(s).Flatten()).Where(s => s.Name == child.Name).Any()) {
     342          cachedIsAllowedChildSymbolIndex.Add(Tuple.Create(parent.Name, child.Name, argumentIndex), true);
     343          return true;
     344        }
     345      }
     346      cachedIsAllowedChildSymbolIndex.Add(Tuple.Create(parent.Name, child.Name, argumentIndex), false);
    324347      return false;
    325348    }
     
    330353        return Enumerable.Empty<ISymbol>();
    331354
    332       return childs.Select(x => GetSymbol(x)).Where(s => s.Enabled);
     355      return childs.Select(GetSymbol).Where(s => s.Enabled);
    333356    }
    334357
     
    343366        result = result.Union(temp);
    344367
    345       return result.Select(x => GetSymbol(x)).Where(s => s.Enabled);
     368      return result.Select(GetSymbol).Where(s => s.Enabled);
    346369    }
    347370
     
    357380      cachedMaxExpressionLength.Clear();
    358381      cachedMinExpressionDepth.Clear();
    359     }
    360 
    361     private Dictionary<string, int> cachedMinExpressionLength;
     382
     383      cachedIsAllowedChildSymbol.Clear();
     384      cachedIsAllowedChildSymbolIndex.Clear();
     385    }
     386
     387    private readonly Dictionary<string, int> cachedMinExpressionLength;
    362388    public int GetMinimumExpressionLength(ISymbol symbol) {
    363389      int temp;
     
    375401    }
    376402
    377     private Dictionary<string, int> cachedMaxExpressionLength;
     403    private readonly Dictionary<string, int> cachedMaxExpressionLength;
    378404    public int GetMaximumExpressionLength(ISymbol symbol) {
    379405      int temp;
     
    390416    }
    391417
    392     private Dictionary<string, int> cachedMinExpressionDepth;
     418    private readonly Dictionary<string, int> cachedMinExpressionDepth;
    393419    public int GetMinimumExpressionDepth(ISymbol symbol) {
    394420      int temp;
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeGrammar.cs

    r6803 r6814  
    8282    public override IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent) {
    8383      var symbols = grammar.GetAllowedChildSymbols(parent).Union(base.GetAllowedChildSymbols(parent));
    84       return symbols.SelectMany(s => s.Flatten()).Where(s => s.Enabled && !(s is GroupSymbol));
     84      return symbols.SelectMany(s => s.Flatten()).Where(s => s.Enabled).Distinct();
    8585    }
    8686    public override IEnumerable<ISymbol> GetAllowedChildSymbols(ISymbol parent, int argumentIndex) {
    8787      var symbols = grammar.GetAllowedChildSymbols(parent, argumentIndex).Union(base.GetAllowedChildSymbols(parent, argumentIndex));
    88       return symbols.SelectMany(s => s.Flatten()).Where(s => s.Enabled && !(s is GroupSymbol));
     88      return symbols.SelectMany(s => s.Flatten()).Where(s => s.Enabled).Distinct();
    8989    }
    9090
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/Grammars.cs

    r6803 r6814  
    2121
    2222using System.Collections.Generic;
     23using System.Linq;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2526using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     27using HeuristicLab.Problems.DataAnalysis.Symbolic;
    2628
    2729namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._4.Tests {
     
    212214
    213215    public static ISymbolicExpressionGrammar CreateSimpleArithmeticGrammar() {
    214       var g = new SimpleArithmeticGrammar();
     216      var g = new TypeCoherentExpressionGrammar();
     217      g.ConfigureAsDefaultRegressionGrammar();
     218      g.Symbols.OfType<Variable>().First().Enabled = false;
     219      //var g = new SimpleArithmeticGrammar();
    215220      g.MaximumFunctionArguments = 0;
    216221      g.MinimumFunctionArguments = 0;
     
    221226
    222227    public static ISymbolicExpressionGrammar CreateArithmeticAndAdfGrammar() {
    223       var g = new SimpleArithmeticGrammar();
     228      var g = new TypeCoherentExpressionGrammar();
     229      g.ConfigureAsDefaultRegressionGrammar();
     230      g.Symbols.OfType<Variable>().First().Enabled = false;
    224231      g.MaximumFunctionArguments = 3;
    225232      g.MinimumFunctionArguments = 0;
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.4.Tests.csproj

    r5809 r6814  
    149149      <Name>HeuristicLab.PluginInfrastructure-3.3</Name>
    150150    </ProjectReference>
     151    <ProjectReference Include="..\..\..\HeuristicLab.Problems.DataAnalysis.Symbolic\3.4\HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj">
     152      <Project>{3D28463F-EC96-4D82-AFEE-38BE91A0CA00}</Project>
     153      <Name>HeuristicLab.Problems.DataAnalysis.Symbolic-3.4</Name>
     154    </ProjectReference>
    151155    <ProjectReference Include="..\..\..\HeuristicLab.Random\3.3\HeuristicLab.Random-3.3.csproj">
    152156      <Project>{F4539FB6-4708-40C9-BE64-0A1390AEA197}</Project>
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/ProbabilisticTreeCreaterTest.cs

    r5809 r6814  
    7272        Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine
    7373        );
    74       Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 500); // must achieve more than 500 random trees / s
     74      Assert.IsTrue(Math.Round(1000.0 / (msPerRandomTreeCreation)) > 300); // must achieve more than 500 random trees / s
    7575    }
    7676  }
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Tests/Util.cs

    r6803 r6814  
    153153    public static void IsValid(ISymbolicExpressionTreeGrammar grammar) {
    154154      Assert.IsTrue(grammar.Symbols.Count() == grammar.Symbols.Distinct().Count());
    155       foreach (ISymbol symbol in grammar.Symbols) {
     155      foreach (ISymbol symbol in grammar.AllowedSymbols) {
    156156        Assert.IsTrue(grammar.GetMinimumSubtreeCount(symbol) <= grammar.GetMaximumExpressionLength(symbol));
    157157        Assert.IsTrue(grammar.GetAllowedChildSymbols(symbol).Count() == grammar.GetAllowedChildSymbols(symbol).Distinct().Count());
Note: See TracChangeset for help on using the changeset viewer.