Changeset 15032 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration
- Timestamp:
- 06/12/17 14:11:43 (7 years ago)
- Location:
- branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration/IEnabledExpressionsConfiguration.cs
r14905 r15032 20 20 event EventHandler<EnabledExpressionsChangedEventArgs> EnabledExpressionsChanged; 21 21 22 I List<string> EnabledExpressions { get; }22 IReadOnlyList<string> EnabledExpressions { get; } 23 23 24 void EnableStack(StackTypes types, bool enable Expressions = true, bool enableDependencies = true);25 void DisableStack(StackTypes type, bool disableExpressions = true, bool enableDepenencies = true);26 void SetStack(StackTypes type, bool state, bool set Expressions = true, bool setDependencies = true);24 void EnableStack(StackTypes types, bool enableDependencies = false); 25 void DisableStack(StackTypes type, bool enableDepenencies = false); 26 void SetStack(StackTypes type, bool state, bool setDependencies = false); 27 27 void EnableExpressionOfStack(StackTypes types); 28 28 void DisableExpressionsOfStack(StackTypes types); 29 void EnableExpression(string name , bool enableStackIfDisabled = false);30 void DisableExpression(string name , bool disableStackIfEnabled = false);31 void SetExpression(string name, bool state , bool cascadeForStack = false);32 void SetExpression<T>(bool state , bool cascadeForStack = false) where T : Expression;33 void EnableExpression<T>( bool enableStackIfDisabled = false) where T : Expression;34 void DisableExpression<T>( bool disableStackIfEnabled = false) where T : Expression;29 void EnableExpression(string name); 30 void DisableExpression(string name); 31 void SetExpression(string name, bool state); 32 void SetExpression<T>(bool state) where T : Expression; 33 void EnableExpression<T>() where T : Expression; 34 void DisableExpression<T>() where T : Expression; 35 35 } 36 36 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration/IReadonlyPushConfiguration.cs
r15017 r15032 3 3 using Base.Erc; 4 4 using Common; 5 6 using HeuristicLab.Core; 7 5 8 using Stack; 6 9 7 public interface IReadOnlyPushConfiguration : IDeepCloneable { 8 IReadOnlyDictionary<StackTypes, bool> EnabledStacks { get; } 10 public interface IReadOnlyPushConfiguration : IItem { 9 11 int EvalPushLimit { get; } 10 12 int MaxDepth { get; } … … 20 22 IReadOnlyList<string> EnabledExpressions { get; } 21 23 string FloatStringFormat { get; } 24 25 bool IsStackEnabled(StackTypes stackType); 22 26 } 23 27 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration/PushConfiguration.cs
r15017 r15032 1 1 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Configuration { 2 using System;3 2 using System.Collections.Generic; 4 using System.Linq;5 3 using Base.Erc; 6 4 using Common; 7 using Core;8 using Expressions;9 5 using Persistence.Default.CompositeSerializers.Storable; 10 using Stack;11 6 12 7 [StorableClass] 13 public class PushConfiguration : P arameterizedNamedItem, IReadOnlyPushConfiguration, IEnabledExpressionsConfiguration {8 public class PushConfiguration : PushConfigurationBase, IReadOnlyPushConfiguration { 14 9 public PushConfiguration() { 15 10 Name = "Push Configuration"; 16 17 enabledExpressions = ExpressionTable.ExpressionNames.ToList();18 enabledStacks = new Dictionary<StackTypes, bool>();19 11 20 12 ErcOptions = new ErcOptions(); … … 31 23 MaxParenthesesClose = 4; 32 24 ParenthesesCloseBiasLevel = 1; 33 34 InitEnabledStacks();35 25 } 36 26 37 private void InitEnabledStacks(bool state = true) {38 foreach (StackTypes type in Enum.GetValues(typeof(StackTypes))) {39 if (!enabledStacks.ContainsKey(type) && type != StackTypes.None)40 enabledStacks.Add(type, state);41 }42 }43 27 44 28 [StorableConstructor] … … 48 32 49 33 public PushConfiguration(PushConfiguration origin, Cloner cloner) : base(origin, cloner) { 50 enabledExpressions = origin.EnabledExpressions.ToList();51 enabledStacks = origin.EnabledStacks.ToDictionary(x => x.Key, x => x.Value);52 34 53 35 ErcOptions = cloner.Clone(origin.ErcOptions); … … 66 48 } 67 49 68 [StorableHook(HookType.AfterDeserialization)]69 // ReSharper disable once UnusedMember.Local70 private void AfterDeserialization() {71 // Ensures that all types added after last serialization are available in enabledStacks72 InitEnabledStacks(false);73 }74 75 50 [Storable] 76 51 public string FloatStringFormat { get; set; } 77 52 78 [Storable]79 private readonly Dictionary<StackTypes, bool> enabledStacks;80 53 81 public IReadOnlyDictionary<StackTypes, bool> EnabledStacks { get { return enabledStacks; } }82 83 public event EventHandler<EnabledExpressionsChangedEventArgs> EnabledExpressionsChanged;84 85 [Storable]86 private readonly List<string> enabledExpressions;87 88 public IList<string> EnabledExpressions89 {90 get { return enabledExpressions; }91 set92 {93 var removedExpressions = enabledExpressions.ToArray();94 enabledExpressions.Clear();95 enabledExpressions.AddRange(value);96 97 if (EnabledExpressionsChanged != null) {98 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(value, removedExpressions));99 }100 }101 }102 54 IReadOnlyList<string> IReadOnlyPushConfiguration.EnabledExpressions { get { return enabledExpressions; } } 103 55 … … 174 126 public int MaxVectorLength { get; set; } 175 127 176 177 public void SetEnabledStacks(StackTypes types) {178 // Disable all179 EnabledExpressions.Clear();180 181 foreach (StackTypes type in Enum.GetValues(types.GetType())) {182 if (type == StackTypes.None) continue;183 enabledStacks[type] = false;184 }185 186 foreach (var pair in ExpressionTable.StackDependencyToNamesTable) {187 if (types.HasFlag(pair.Key)) {188 foreach (var name in pair.Value) {189 EnableExpression(name, true);190 }191 }192 }193 }194 195 public void EnableExpressionOfStack(StackTypes types) {196 EnableExpressions(ExpressionTable.StackTypeToNamesTable[types]);197 }198 199 public void EnableExpressionDependentOnStack(StackTypes types) {200 var names = ExpressionTable.StackDependencyToNamesTable[types].Where(201 name => {202 var type = ExpressionTable.NameToTypeTable[name];203 var attribute = ExpressionTable.TypeToAttributeTable[type];204 205 return (attribute.StackType | attribute.AdditionalStackDependencies)206 .ToEnumerable()207 .All(st => enabledStacks.ContainsKey(st) && enabledStacks[st]);208 });209 210 EnableExpressions(names);211 }212 213 private void EnableExpressions(IEnumerable<string> names) {214 foreach (var name in names.Except(EnabledExpressions))215 EnabledExpressions.Add(name);216 217 if (EnabledExpressionsChanged != null) {218 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(names, new string[0]));219 }220 }221 222 public void DisableExpressionsOfStack(StackTypes types) {223 DisableExpressions(ExpressionTable.StackTypeToNamesTable[types]);224 }225 226 public void DisableExpressionsDependentOnStack(StackTypes types) {227 var names = ExpressionTable.StackDependencyToNamesTable228 .Where(pair => pair.Key.HasFlag(types))229 .SelectMany(pair => pair.Value);230 231 DisableExpressions(names);232 }233 234 private void DisableExpressions(IEnumerable<string> names) {235 foreach (var name in names.Intersect(EnabledExpressions))236 EnabledExpressions.Remove(name);237 238 if (EnabledExpressionsChanged != null) {239 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(new string[0], names));240 }241 }242 243 public void EnableExpression(string name, bool enableStackIfDisabled = false) {244 if (EnabledExpressions.Contains(name)) return;245 246 EnabledExpressions.Add(name);247 248 if (enableStackIfDisabled) {249 var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == name).Key;250 var attribute = ExpressionTable.TypeToAttributeTable[type];251 enabledStacks[attribute.StackType] = true;252 }253 254 if (EnabledExpressionsChanged != null) {255 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(256 new[] { name },257 new string[0]));258 }259 }260 261 public void DisableExpression(string name, bool disableStackIfEnabled = false) {262 if (!EnabledExpressions.Contains(name)) return;263 264 EnabledExpressions.Remove(name);265 266 if (disableStackIfEnabled) {267 var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == name).Key;268 var attribute = ExpressionTable.TypeToAttributeTable[type];269 enabledStacks[attribute.StackType] = false;270 }271 272 if (EnabledExpressionsChanged != null) {273 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(274 new string[0],275 new[] { name }));276 }277 }278 279 public void EnableExpression<T>(bool enableStackIfDisabled = false) where T : Expression {280 var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)];281 EnableExpression(attribute.ExpressionName, enableStackIfDisabled);282 }283 284 public void DisableExpression<T>(bool disableStackIfEnabled = false) where T : Expression {285 var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)];286 DisableExpression(attribute.ExpressionName, disableStackIfEnabled);287 }288 289 public void SetExpression(string name, bool state, bool cascadeForStack = false) {290 if (state) EnableExpression(name, cascadeForStack);291 else DisableExpression(name, cascadeForStack);292 }293 294 public void SetExpression<T>(bool state, bool cascadeForStack = false) where T : Expression {295 if (state) EnableExpression<T>(cascadeForStack);296 else DisableExpression<T>(cascadeForStack);297 }298 299 public void EnableStack(StackTypes type, bool enableExpressions = true, bool enableDependencies = true) {300 enabledStacks[type] = true;301 302 if (enableExpressions) EnableExpressionOfStack(type);303 if (enableDependencies) EnableExpressionDependentOnStack(type);304 }305 306 public void DisableStack(StackTypes type, bool disableExpressions = true, bool disableDependencies = true) {307 enabledStacks[type] = false;308 309 if (disableExpressions) DisableExpressionsOfStack(type);310 if (disableDependencies) DisableExpressionsDependentOnStack(type);311 }312 313 public void SetStack(StackTypes type, bool state, bool setExpressions = true, bool setDependencies = true) {314 if (state) EnableStack(type, setExpressions, setDependencies);315 else DisableStack(type, setExpressions, setDependencies);316 }317 318 128 public override IDeepCloneable Clone(Cloner cloner) { 319 129 return new PushConfiguration(this, cloner); -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration/PushConfigurationBase.cs
r15017 r15032 8 8 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 9 9 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 10 using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions; 10 11 using HeuristicLab.Problems.ProgramSynthesis.Push.Stack; 11 12 … … 17 18 18 19 enabledExpressions = ExpressionTable.ExpressionNames.ToList(); 19 enabledStacks = new Dictionary<StackTypes, bool>(); 20 21 InitEnabledStacks(); 20 expressionsPerStackCount = new Dictionary<StackTypes, int>(); 21 InitExpressionsPerStackCount(); 22 22 } 23 23 … … 27 27 protected PushConfigurationBase(PushConfigurationBase origin, Cloner cloner) : base(origin, cloner) { 28 28 enabledExpressions = origin.EnabledExpressions.ToList(); 29 e nabledStacks = origin.EnabledStacks.ToDictionary(x => x.Key, x => x.Value);29 expressionsPerStackCount = origin.expressionsPerStackCount.ToDictionary(x => x.Key, x => x.Value); 30 30 } 31 31 32 32 [Storable] 33 private readonly Dictionary<StackTypes, bool> enabledStacks; 34 35 public IReadOnlyDictionary<StackTypes, bool> EnabledStacks { get { return enabledStacks; } } 33 private readonly Dictionary<StackTypes, int> expressionsPerStackCount; 36 34 37 35 public event EventHandler<EnabledExpressionsChangedEventArgs> EnabledExpressionsChanged; … … 40 38 protected readonly List<string> enabledExpressions; 41 39 42 public IList<string> EnabledExpressions 40 public bool IsStackEnabled(StackTypes type) { 41 int count; 42 return expressionsPerStackCount.TryGetValue(type, out count) && count > 0; 43 } 44 45 public IReadOnlyList<string> EnabledExpressions 43 46 { 44 47 get { return enabledExpressions; } … … 55 58 } 56 59 57 private void InitE nabledStacks(bool state = true) {60 private void InitExpressionsPerStackCount() { 58 61 foreach (StackTypes type in Enum.GetValues(typeof(StackTypes))) { 59 if (!enabledStacks.ContainsKey(type) && type != StackTypes.None) 60 enabledStacks.Add(type, state); 62 if (type == StackTypes.None) continue; 63 expressionsPerStackCount.Add(type, 0); 64 } 65 66 foreach (var expressionName in ExpressionTable.ExpressionNames) { 67 var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == expressionName).Key; 68 var attribute = ExpressionTable.TypeToAttributeTable[type]; 69 70 expressionsPerStackCount[attribute.StackType]++; 71 72 foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues()) 73 expressionsPerStackCount[additionalStackType]++; 61 74 } 62 75 } … … 64 77 public void SetEnabledStacks(StackTypes types) { 65 78 // Disable all 66 EnabledExpressions.Clear();67 68 foreach (StackTypes type in Enum.GetValues(type s.GetType())) {79 enabledExpressions.Clear(); 80 81 foreach (StackTypes type in Enum.GetValues(typeof(StackTypes))) { 69 82 if (type == StackTypes.None) continue; 70 enabledStacks[type] = false; 71 } 72 73 foreach (var pair in ExpressionTable.StackDependencyToNamesTable) { 74 if (types.HasFlag(pair.Key)) { 75 foreach (var name in pair.Value) { 76 EnableExpression(name, true); 77 } 78 } 79 } 83 expressionsPerStackCount[type] = 0; 84 } 85 86 var expressionNames = ExpressionTable.StackDependencyToNamesTable 87 .Where(pair => types.HasFlag(pair.Key)) 88 .SelectMany(pair => pair.Value); 89 90 EnableExpressions(expressionNames); 80 91 } 81 92 … … 92 103 return (attribute.StackType | attribute.AdditionalStackDependencies) 93 104 .ToEnumerable() 94 .All( st => enabledStacks.ContainsKey(st) && enabledStacks[st]);105 .All(IsStackEnabled); 95 106 }); 96 107 … … 98 109 } 99 110 100 private void EnableExpressions(IEnumerable<string> names) {101 foreach (var name in names.Except(EnabledExpressions))102 Enable dExpressions.Add(name);111 private void EnableExpressions(IEnumerable<string> expressionNames) { 112 foreach (var expressionName in expressionNames.Except(EnabledExpressions)) 113 EnableExpression(expressionName, false); 103 114 104 115 if (EnabledExpressionsChanged != null) { 105 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs( names, new string[0]));116 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(expressionNames, new string[0])); 106 117 } 107 118 } … … 119 130 } 120 131 121 private void DisableExpressions(IEnumerable<string> names) {122 foreach (var name in names.Intersect(EnabledExpressions))123 EnabledExpressions.Remove(name);132 private void DisableExpressions(IEnumerable<string> expressionNames) { 133 foreach (var expressionName in expressionNames.Intersect(EnabledExpressions)) 134 DisableExpression(expressionName, false); 124 135 125 136 if (EnabledExpressionsChanged != null) { 126 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(new string[0], names)); 127 } 128 } 129 130 public void EnableExpression(string name, bool enableStackIfDisabled = false) { 131 if (EnabledExpressions.Contains(name)) return; 132 133 EnabledExpressions.Add(name); 134 135 if (enableStackIfDisabled) { 136 var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == name).Key; 137 var attribute = ExpressionTable.TypeToAttributeTable[type]; 138 enabledStacks[attribute.StackType] = true; 139 } 140 141 if (EnabledExpressionsChanged != null) { 137 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs(new string[0], expressionNames)); 138 } 139 } 140 141 public void EnableExpression(string expressionName) { 142 EnableExpression(expressionName, true); 143 } 144 145 private void EnableExpression(string expressionName, bool triggerExpressionChanged) { 146 if (enabledExpressions.Contains(expressionName)) return; 147 148 var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == expressionName).Key; 149 var attribute = ExpressionTable.TypeToAttributeTable[type]; 150 151 enabledExpressions.Add(expressionName); 152 expressionsPerStackCount[attribute.StackType]++; 153 foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues()) 154 expressionsPerStackCount[additionalStackType]++; 155 156 if (EnabledExpressionsChanged != null && triggerExpressionChanged) { 142 157 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs( 143 new[] { name },158 new[] { expressionName }, 144 159 new string[0])); 145 160 } 146 161 } 147 162 148 public void DisableExpression(string name, bool disableStackIfEnabled = false) { 149 if (!EnabledExpressions.Contains(name)) return; 150 151 EnabledExpressions.Remove(name); 152 153 if (disableStackIfEnabled) { 154 var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == name).Key; 155 var attribute = ExpressionTable.TypeToAttributeTable[type]; 156 enabledStacks[attribute.StackType] = false; 157 } 158 159 if (EnabledExpressionsChanged != null) { 163 public void DisableExpression(string expressionName) { 164 DisableExpression(expressionName, true); 165 } 166 167 public void DisableExpression(string expressionName, bool triggerExpressionChanged) { 168 if (!EnabledExpressions.Contains(expressionName)) return; 169 170 var type = ExpressionTable.TypeToNameTable.Single(x => x.Value == expressionName).Key; 171 var attribute = ExpressionTable.TypeToAttributeTable[type]; 172 173 enabledExpressions.Remove(expressionName); 174 expressionsPerStackCount[attribute.StackType]--; 175 foreach (var additionalStackType in attribute.AdditionalStackDependencies.ToValues()) 176 expressionsPerStackCount[additionalStackType]--; 177 178 if (EnabledExpressionsChanged != null && triggerExpressionChanged) { 160 179 EnabledExpressionsChanged(this, new EnabledExpressionsChangedEventArgs( 161 180 new string[0], 162 new[] { name }));163 } 164 } 165 166 public void EnableExpression<T>( bool enableStackIfDisabled = false) where T : Expression {181 new[] { expressionName })); 182 } 183 } 184 185 public void EnableExpression<T>() where T : Expression { 167 186 var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)]; 168 EnableExpression(attribute. ExpressionName, enableStackIfDisabled);169 } 170 171 public void DisableExpression<T>( bool disableStackIfEnabled = false) where T : Expression {187 EnableExpression(attribute.Name); 188 } 189 190 public void DisableExpression<T>() where T : Expression { 172 191 var attribute = ExpressionTable.TypeToAttributeTable[typeof(T)]; 173 DisableExpression(attribute. ExpressionName, disableStackIfEnabled);174 } 175 176 public void SetExpression(string name, bool state , bool cascadeForStack = false) {177 if (state) EnableExpression(name , cascadeForStack);178 else DisableExpression(name , cascadeForStack);179 } 180 181 public void SetExpression<T>(bool state , bool cascadeForStack = false) where T : Expression {182 if (state) EnableExpression<T>( cascadeForStack);183 else DisableExpression<T>( cascadeForStack);184 } 185 186 public void EnableStack(StackTypes type, bool enable Expressions = true, bool enableDependencies = true) {187 enabledStacks[type] = true;188 189 if (enable Expressions) EnableExpressionOfStack(type);190 if (enableDependencies)EnableExpressionDependentOnStack(type);191 } 192 193 public void DisableStack(StackTypes type, bool disable Expressions = true, bool disableDependencies = true) {194 enabledStacks[type] = false;195 196 if (disable Expressions) DisableExpressionsOfStack(type);197 if (disableDependencies)DisableExpressionsDependentOnStack(type);198 } 199 200 public void SetStack(StackTypes type, bool state, bool set Expressions = true, bool setDependencies = true) {201 if (state) EnableStack(type, set Expressions, setDependencies);202 else DisableStack(type, set Expressions, setDependencies);192 DisableExpression(attribute.Name); 193 } 194 195 public void SetExpression(string name, bool state) { 196 if (state) EnableExpression(name); 197 else DisableExpression(name); 198 } 199 200 public void SetExpression<T>(bool state) where T : Expression { 201 if (state) EnableExpression<T>(); 202 else DisableExpression<T>(); 203 } 204 205 public void EnableStack(StackTypes type, bool enableDependencies = false) { 206 EnableExpressionOfStack(type); 207 208 if (enableDependencies) 209 EnableExpressionDependentOnStack(type); 210 } 211 212 public void DisableStack(StackTypes type, bool disableDependencies = false) { 213 DisableExpressionsOfStack(type); 214 215 if (disableDependencies) 216 DisableExpressionsDependentOnStack(type); 217 } 218 219 public void SetStack(StackTypes type, bool state, bool setDependencies = false) { 220 if (state) EnableStack(type, setDependencies); 221 else DisableStack(type, setDependencies); 203 222 } 204 223 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Configuration/PushConfigurationParameterCollection.cs
r15017 r15032 192 192 193 193 /// <summary> 194 /// This is the maximum of depth a push program can have. Expressions, which lead to exceed this limit are interpreted as NOOP. 194 /// Determines the likelyhood of smaller or bigger values. 195 /// x greater than 1 means that result is biased towards min. 196 /// x smaller than 1 means that result is biased towards max. 195 197 /// </summary> 196 198 public IValueParameter<DoubleValue> ParenthesesCloseBiasLevelParameter … … 206 208 207 209 /// <summary> 208 /// This is the maximum of depth a push program can have. Expressions, which lead to exceed this limit are interpreted as NOOP.210 /// Determines the maximum of blocks which will be closed. 209 211 /// </summary> 210 212 public IValueParameter<IntValue> MaxParenthesesCloseParameter
Note: See TracChangeset
for help on using the changeset viewer.