- Timestamp:
- 05/30/11 17:31:53 (14 years ago)
- Location:
- branches/histogram
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/histogram
- Property svn:mergeinfo changed
/trunk/sources merged: 6201,6205,6207-6209,6223,6233-6241,6250,6252,6254-6256,6259,6262,6265,6291,6298,6302
- Property svn:mergeinfo changed
-
branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/ArchitectureManipulators/SubroutineDuplicater.cs
r5809 r6340 83 83 subtree.Grammar.SetSubtreeCount(invokeSymbol, duplicatedDefunBranch.NumberOfArguments, duplicatedDefunBranch.NumberOfArguments); 84 84 85 foreach ( Symbol symbol in subtree.Grammar.Symbols) {85 foreach (ISymbol symbol in subtree.Grammar.Symbols) { 86 86 if (subtree.Grammar.IsAllowedChildSymbol(symbol, matchingInvokeSymbol)) 87 87 subtree.Grammar.AddAllowedChildSymbol(symbol, invokeSymbol); … … 93 93 } 94 94 95 foreach ( Symbol symbol in subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol))95 foreach (ISymbol symbol in subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol)) 96 96 if (symbol != invokeSymbol) //avoid duplicate entry invokesymbol / invokesymbol 97 97 subtree.Grammar.AddAllowedChildSymbol(invokeSymbol, symbol); 98 98 for (int i = 0; i < subtree.Grammar.GetMaximumSubtreeCount(matchingInvokeSymbol); i++) { 99 foreach ( Symbol symbol in subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol, i).Except(subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol)))99 foreach (ISymbol symbol in subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol, i).Except(subtree.Grammar.GetAllowedChildSymbols(matchingInvokeSymbol))) 100 100 subtree.Grammar.AddAllowedChildSymbol(invokeSymbol, symbol, i); 101 101 } -
branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/ProbabilisticTreeCreator.cs
r6011 r6340 38 38 private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth"; 39 39 private const string SymbolicExpressionTreeGrammarParameterName = "SymbolicExpressionTreeGrammar"; 40 private const string ClonedSymbolicExpressionTreeGrammarParameterName = "ClonedSymbolicExpressionTreeGrammar"; 40 41 #region Parameter Properties 41 42 public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeLengthParameter { … … 47 48 public IValueLookupParameter<ISymbolicExpressionGrammar> SymbolicExpressionTreeGrammarParameter { 48 49 get { return (IValueLookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionTreeGrammarParameterName]; } 50 } 51 public ILookupParameter<ISymbolicExpressionGrammar> ClonedSymbolicExpressionTreeGrammarParameter { 52 get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[ClonedSymbolicExpressionTreeGrammarParameterName]; } 49 53 } 50 54 #endregion … … 57 61 } 58 62 public ISymbolicExpressionGrammar SymbolicExpressionTreeGrammar { 59 get { return SymbolicExpressionTreeGrammarParameter.ActualValue; }63 get { return ClonedSymbolicExpressionTreeGrammarParameter.ActualValue; } 60 64 } 61 65 #endregion … … 69 73 Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0).")); 70 74 Parameters.Add(new ValueLookupParameter<ISymbolicExpressionGrammar>(SymbolicExpressionTreeGrammarParameterName, "The tree grammar that defines the correct syntax of symbolic expression trees that should be created.")); 75 Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees.")); 71 76 } 72 77 … … 74 79 return new ProbabilisticTreeCreator(this, cloner); 75 80 } 81 [StorableHook(HookType.AfterDeserialization)] 82 private void AfterDeserialization() { 83 if (!Parameters.ContainsKey(ClonedSymbolicExpressionTreeGrammarParameterName)) 84 Parameters.Add(new LookupParameter<ISymbolicExpressionGrammar>(ClonedSymbolicExpressionTreeGrammarParameterName, "An immutable clone of the concrete grammar that is actually used to create and manipulate trees.")); 85 } 86 87 public override IOperation Apply() { 88 if (ClonedSymbolicExpressionTreeGrammarParameter.ActualValue == null) { 89 SymbolicExpressionTreeGrammarParameter.ActualValue.ReadOnly = true; 90 IScope globalScope = ExecutionContext.Scope; 91 while (globalScope.Parent != null) 92 globalScope = globalScope.Parent; 93 94 globalScope.Variables.Add(new Variable(ClonedSymbolicExpressionTreeGrammarParameterName, (ISymbolicExpressionGrammar)SymbolicExpressionTreeGrammarParameter.ActualValue.Clone())); 95 } 96 return base.Apply(); 97 } 76 98 77 99 protected override ISymbolicExpressionTree Create(IRandom random) { 78 100 return Create(random, SymbolicExpressionTreeGrammar, MaximumSymbolicExpressionTreeLength.Value, MaximumSymbolicExpressionTreeDepth.Value); 79 80 101 } 81 102 … … 165 186 select s) 166 187 .ToList(); 188 if (allowedSymbols.Count == 0) return false; 167 189 var weights = allowedSymbols.Select(x => x.InitialFrequency).ToList(); 168 190 var selectedSymbol = allowedSymbols.SelectRandom(weights, random); -
branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Creators/SymbolicExpressionTreeCreator.cs
r5809 r6340 22 22 using HeuristicLab.Common; 23 23 using HeuristicLab.Core; 24 using HeuristicLab.Data;25 24 using HeuristicLab.Parameters; 26 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; … … 55 54 } 56 55 57 public sealedoverride IOperation Apply() {56 public override IOperation Apply() { 58 57 SymbolicExpressionTree = Create(Random); 59 58 return base.Apply(); -
branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Interfaces/ISymbolicExpressionGrammar.cs
r5809 r6340 21 21 22 22 23 using System; 24 using HeuristicLab.Core; 23 25 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { 24 public interface ISymbolicExpressionGrammar : ISymbolicExpressionGrammarBase {26 public interface ISymbolicExpressionGrammar : ISymbolicExpressionGrammarBase, IStatefulItem { 25 27 ISymbol ProgramRootSymbol { get; } 26 28 ISymbol StartSymbol { get; } … … 30 32 int MinimumFunctionArguments { get; set; } 31 33 int MaximumFunctionArguments { get; set; } 34 35 bool ReadOnly { get; set; } 36 event EventHandler ReadOnlyChanged; 32 37 } 33 38 } -
branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammar.cs
r5809 r6340 20 20 #endregion 21 21 22 using System .Linq;22 using System; 23 23 using HeuristicLab.Common; 24 using HeuristicLab.Core; 24 25 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 25 26 … … 28 29 public abstract class SymbolicExpressionGrammar : SymbolicExpressionGrammarBase, ISymbolicExpressionGrammar { 29 30 #region fields & properties 31 [Storable(DefaultValue = false)] 32 private bool readOnly; 33 public bool ReadOnly { 34 get { return readOnly; } 35 set { 36 if (readOnly != value) { 37 readOnly = value; 38 OnReadOnlyChanged(); 39 } 40 } 41 } 42 30 43 [Storable] 31 44 private int minimumFunctionDefinitions; … … 102 115 protected SymbolicExpressionGrammar(SymbolicExpressionGrammar original, Cloner cloner) 103 116 : base(original, cloner) { 104 programRootSymbol = (ProgramRootSymbol)cloner.Clone(original.programRootSymbol); 105 startSymbol = (StartSymbol)cloner.Clone(original.StartSymbol); 106 defunSymbol = (Defun)cloner.Clone(original.defunSymbol); 107 symbols = original.symbols 108 .ToDictionary(x => x.Key, y => (ISymbol)cloner.Clone(y.Value)); 117 programRootSymbol = cloner.Clone(original.programRootSymbol); 118 startSymbol = cloner.Clone(original.StartSymbol); 119 defunSymbol = cloner.Clone(original.defunSymbol); 120 109 121 maximumFunctionArguments = original.maximumFunctionArguments; 110 122 minimumFunctionArguments = original.minimumFunctionArguments; … … 140 152 } 141 153 } 154 155 public event EventHandler ReadOnlyChanged; 156 protected virtual void OnReadOnlyChanged() { 157 var handler = ReadOnlyChanged; 158 if (handler != null) 159 handler(this, EventArgs.Empty); 160 } 161 162 #region IStatefulItem 163 void IStatefulItem.InitializeState() { } 164 void IStatefulItem.ClearState() { 165 ReadOnly = false; 166 } 167 #endregion 142 168 } 143 169 } -
branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionGrammarBase.cs
r6011 r6340 81 81 cachedMinExpressionDepth = new Dictionary<string, int>(); 82 82 } 83 [StorableHook(HookType.AfterDeserialization)] 84 private void AfterDeserialization() { 85 foreach (ISymbol symbol in symbols.Values) 86 RegisterSymbolEvents(symbol); 87 } 88 83 89 protected SymbolicExpressionGrammarBase(SymbolicExpressionGrammarBase original, Cloner cloner) 84 90 : base(original, cloner) { … … 87 93 cachedMinExpressionDepth = new Dictionary<string, int>(); 88 94 95 96 symbols = original.symbols.ToDictionary(x => x.Key, y => (ISymbol)cloner.Clone(y.Value)); 89 97 symbolSubtreeCount = new Dictionary<string, Tuple<int, int>>(original.symbolSubtreeCount); 90 symbols = new Dictionary<string, ISymbol>(original.symbols);91 98 92 99 allowedChildSymbols = new Dictionary<string, List<string>>(); … … 97 104 foreach (var element in original.allowedChildSymbolsPerIndex) 98 105 allowedChildSymbolsPerIndex.Add(element.Key, new List<string>(element.Value)); 106 107 foreach (ISymbol symbol in symbols.Values) 108 RegisterSymbolEvents(symbol); 99 109 } 100 110 … … 114 124 protected void AddSymbol(ISymbol symbol) { 115 125 if (ContainsSymbol(symbol)) throw new ArgumentException("Symbol " + symbol + " is already defined."); 126 RegisterSymbolEvents(symbol); 116 127 symbols.Add(symbol.Name, symbol); 117 128 symbolSubtreeCount.Add(symbol.Name, Tuple.Create(0, 0)); 129 ClearCaches(); 130 } 131 132 private void RegisterSymbolEvents(ISymbol symbol) { 133 symbol.NameChanging += new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 134 symbol.NameChanged += new EventHandler(Symbol_NameChanged); 135 } 136 private void DeregisterSymbolEvents(ISymbol symbol) { 137 symbol.NameChanging -= new EventHandler<CancelEventArgs<string>>(Symbol_NameChanging); 138 symbol.NameChanged -= new EventHandler(Symbol_NameChanged); 139 } 140 141 private void Symbol_NameChanging(object sender, CancelEventArgs<string> e) { 142 if (symbols.ContainsKey(e.Value)) e.Cancel = true; 143 } 144 private void Symbol_NameChanged(object sender, EventArgs e) { 145 ISymbol symbol = (ISymbol)sender; 146 string oldName = symbols.Where(x => x.Value == symbol).First().Key; 147 string newName = symbol.Name; 148 149 symbols.Remove(oldName); 150 symbols.Add(newName, symbol); 151 152 var subtreeCount = symbolSubtreeCount[oldName]; 153 symbolSubtreeCount.Remove(oldName); 154 symbolSubtreeCount.Add(newName, subtreeCount); 155 156 List<string> allowedChilds; 157 if (allowedChildSymbols.TryGetValue(oldName, out allowedChilds)) { 158 allowedChildSymbols.Remove(oldName); 159 allowedChildSymbols.Add(newName, allowedChilds); 160 } 161 162 for (int i = 0; i < GetMaximumSubtreeCount(symbol); i++) { 163 if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(oldName, i), out allowedChilds)) { 164 allowedChildSymbolsPerIndex.Remove(Tuple.Create(oldName, i)); 165 allowedChildSymbolsPerIndex.Add(Tuple.Create(newName, i), allowedChilds); 166 } 167 } 168 169 foreach (var parent in Symbols) { 170 if (allowedChildSymbols.TryGetValue(parent.Name, out allowedChilds)) 171 if (allowedChilds.Remove(oldName)) 172 allowedChilds.Add(newName); 173 174 for (int i = 0; i < GetMaximumSubtreeCount(parent); i++) { 175 if (allowedChildSymbolsPerIndex.TryGetValue(Tuple.Create(parent.Name, i), out allowedChilds)) 176 if (allowedChilds.Remove(oldName)) allowedChilds.Add(newName); 177 } 178 } 179 118 180 ClearCaches(); 119 181 } … … 126 188 symbolSubtreeCount.Remove(symbol.Name); 127 189 128 129 190 foreach (var parent in Symbols) { 130 191 List<string> allowedChilds; … … 137 198 } 138 199 } 200 DeregisterSymbolEvents(symbol); 139 201 ClearCaches(); 140 202 } -
branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeGrammar.cs
r5809 r6340 36 36 } 37 37 public override IDeepCloneable Clone(Cloner cloner) { 38 foreach (ISymbol symbol in base.Symbols) 39 if (!cloner.ClonedObjectRegistered(symbol)) 40 cloner.RegisterClonedObject(symbol, symbol); 38 41 return new SymbolicExpressionTreeGrammar(this, cloner); 39 42 } -
branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeTerminalNode.cs
r5809 r6340 45 45 protected SymbolicExpressionTreeTerminalNode() : base() { } 46 46 47 protected SymbolicExpressionTreeTerminalNode( Symbol symbol)47 protected SymbolicExpressionTreeTerminalNode(ISymbol symbol) 48 48 : base() { 49 49 // symbols are reused -
branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/SymbolicExpressionTreeTopLevelNode.cs
r5809 r6340 42 42 } 43 43 public SymbolicExpressionTreeTopLevelNode() : base() { } 44 public SymbolicExpressionTreeTopLevelNode( Symbol symbol) : base(symbol) { }44 public SymbolicExpressionTreeTopLevelNode(ISymbol symbol) : base(symbol) { } 45 45 46 46 -
branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Argument.cs
r5809 r6340 38 38 } 39 39 40 public override bool CanChangeDescription {41 get { return false; }42 }43 44 40 [StorableConstructor] 45 41 private Argument(bool deserializing) : base(deserializing) { } -
branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/InvokeFunction.cs
r5809 r6340 20 20 #endregion 21 21 22 using System;23 22 using HeuristicLab.Common; 24 23 using HeuristicLab.Core; … … 38 37 public string FunctionName { 39 38 get { return functionName; } 40 set {41 if (value == null) throw new ArgumentNullException();42 functionName = value;43 }44 39 } 45 40 … … 53 48 public InvokeFunction(string functionName) 54 49 : base("Invoke: " + functionName, InvokeFunction.InvokeFunctionDescription) { 55 this. FunctionName = functionName;50 this.functionName = functionName; 56 51 } 57 52 -
branches/histogram/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Symbols/Symbol.cs
r5809 r6340 43 43 } 44 44 public override bool CanChangeName { 45 get { return false; }45 get { return !(this is IReadOnlySymbol); } 46 46 } 47 47 public override bool CanChangeDescription { … … 56 56 initialFrequency = original.initialFrequency; 57 57 } 58 protected Symbol()59 : base() {60 initialFrequency = 1.0;61 }62 58 63 59 protected Symbol(string name, string description) … … 65 61 initialFrequency = 1.0; 66 62 } 67 68 63 69 64 public virtual ISymbolicExpressionTreeNode CreateTreeNode() {
Note: See TracChangeset
for help on using the changeset viewer.