Changeset 15189 for branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter
- Timestamp:
- 07/10/17 21:36:03 (7 years ago)
- Location:
- branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/Extensions.cs
r15017 r15189 25 25 26 26 public static Type GetStackEntryType(this StackTypes stackType) { 27 if (StackProperties.ContainsKey(stackType) && 28 StackProperties[stackType].PropertyType.IsGenericType) { 29 var genericTypeDef = StackProperties[stackType].PropertyType.GetGenericTypeDefinition(); 30 if (genericTypeDef == typeof(IPushStack<>)) 31 return genericTypeDef.GetGenericArguments()[0]; 32 } 33 27 34 return StackProperties.ContainsKey(stackType) 28 ? StackProperties[stackType].PropertyType.GetGenericArguments().Single() 35 ? StackProperties[stackType] 36 .PropertyType 37 .GetInterfaces() 38 .First(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPushStack<>)) 39 .GetGenericArguments() 40 .First() 29 41 : null; 30 42 } 31 43 32 44 public static void PrintStacks(this IPushInterpreter interpreter) { 33 var stackTypesType = typeof(StackTypes); 34 35 foreach (StackTypes type in Enum.GetValues(stackTypesType)) { 36 if (!interpreter.Stacks.ContainsKey(type)) continue; 37 38 var stackName = Enum.GetName(stackTypesType, type); 39 var stack = interpreter.Stacks[type]; 45 foreach (var pair in interpreter.Stacks) { 46 var stackName = pair.Key.ToString(); 47 var stack = interpreter.Stacks[pair.Key]; 40 48 41 49 if (stack.IsEmpty || !stack.IsEnabled) continue; 42 50 43 var stackString = string.Join(" ", interpreter.StringifyStack( type).Reverse());51 var stackString = string.Join(" ", interpreter.StringifyStack(pair.Key).Reverse()); 44 52 Console.WriteLine("--------- {0} ---------\n{1}\n", stackName, stackString); 45 53 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/IInternalPushInterpreter.cs
r14834 r15189 1 1 namespace HeuristicLab.Problems.ProgramSynthesis.Push.Interpreter { 2 using System.Collections.Generic; 3 4 using HeuristicLab.Problems.ProgramSynthesis.Push.Expressions; 5 2 6 public interface IInternalPushInterpreter : IPushInterpreter { 3 7 InterpreterPoolContainer PoolContainer { get; } 4 8 bool IsNameQuoteFlagSet { get; set; } 9 IReadOnlyList<Expression> InputExpressions { get; } 5 10 } 6 11 } -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/IPushInterpreter.cs
r15017 r15189 24 24 IPushStack<IReadOnlyList<bool>> BooleanVectorStack { get; } 25 25 IPushStack<IReadOnlyList<string>> StringVectorStack { get; } 26 IP ushStack<string>PrintStack { get; }26 IPrintStack PrintStack { get; } 27 27 IDictionary<string, Expression> CustomExpressions { get; } 28 28 IReadOnlyDictionary<StackTypes, IPushStack> Stacks { get; } … … 30 30 void Clear(); 31 31 void Reset(); 32 33 void SetInput( 34 IReadOnlyList<long> integers = null, 35 IReadOnlyList<double> floats = null, 36 IReadOnlyList<bool> booleans = null, 37 IReadOnlyList<char> chars = null, 38 IReadOnlyList<string> strings = null, 39 IReadOnlyList<IReadOnlyList<long>> integerVectors = null, 40 IReadOnlyList<IReadOnlyList<double>> floatVectors = null, 41 IReadOnlyList<IReadOnlyList<string>> stringVectors = null); 32 42 33 43 void Run(bool stepwise); -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PushInterpreter.cs
r15032 r15189 2 2 using System; 3 3 using System.Collections.Generic; 4 using System.Linq; 4 5 using System.Runtime.CompilerServices; 5 6 using System.Threading; … … 35 36 BooleanVectorStack = new PushStack<IReadOnlyList<bool>>(); 36 37 StringVectorStack = new PushStack<IReadOnlyList<string>>(); 37 PrintStack = new P ushStack<string>();38 PrintStack = new PrintStack(); 38 39 39 40 Stacks = new Dictionary<StackTypes, IPushStack> { … … 53 54 }; 54 55 56 supportedStackTypes = Stacks.Keys.ToArray(); 57 55 58 CustomExpressions = new Dictionary<string, Expression>(); 56 59 PoolContainer = poolContainer ?? new InterpreterPoolContainer(); … … 63 66 } 64 67 68 private readonly StackTypes[] supportedStackTypes; 65 69 public IReadOnlyDictionary<StackTypes, IPushStack> Stacks { get; private set; } 66 70 … … 128 132 public IPushStack<IReadOnlyList<string>> StringVectorStack { get; private set; } 129 133 [PushStack(StackTypes.Print)] 130 public IP ushStack<string>PrintStack { get; private set; }134 public IPrintStack PrintStack { get; private set; } 131 135 132 136 public IDictionary<string, Expression> CustomExpressions { get; private set; } 133 137 134 138 public bool IsNameQuoteFlagSet { get; set; } 139 140 private readonly List<Expression> inputExpressions = new List<Expression>(); 141 IReadOnlyList<Expression> IInternalPushInterpreter.InputExpressions { get { return inputExpressions; } } 142 143 public void SetInput( 144 IReadOnlyList<long> integers = null, 145 IReadOnlyList<double> floats = null, 146 IReadOnlyList<bool> booleans = null, 147 IReadOnlyList<char> chars = null, 148 IReadOnlyList<string> strings = null, 149 IReadOnlyList<IReadOnlyList<long>> integerVectors = null, 150 IReadOnlyList<IReadOnlyList<double>> floatVectors = null, 151 IReadOnlyList<IReadOnlyList<string>> stringVectors = null) { 152 153 // Integer 154 if (integers != null && integers.Count > 0) { 155 var integerPushExpressionPool = PoolContainer.GetStatefulExpressionPool<IntegerPushExpression>(); 156 157 for (var i = 0; i < integers.Count; i++) { 158 var expression = IntegerPushExpression.Create(integerPushExpressionPool, integers[i]); 159 inputExpressions.Add(expression); 160 } 161 } 162 163 // Float 164 if (floats != null && floats.Count > 0) { 165 var floatPushExpressionPool = PoolContainer.GetStatefulExpressionPool<FloatPushExpression>(); 166 167 for (var i = 0; i < floats.Count; i++) { 168 var expression = FloatPushExpression.Create(floatPushExpressionPool, floats[i]); 169 inputExpressions.Add(expression); 170 } 171 } 172 173 // Booleans 174 if (booleans != null && booleans.Count > 0) { 175 var booleanPushExpressionPool = PoolContainer.GetStatefulExpressionPool<BooleanPushExpression>(); 176 177 for (var i = 0; i < booleans.Count; i++) { 178 var expression = BooleanPushExpression.Create(booleanPushExpressionPool, booleans[i]); 179 inputExpressions.Add(expression); 180 } 181 } 182 183 // Char 184 if (chars != null && chars.Count > 0) { 185 var charPushExpressionPool = PoolContainer.GetStatefulExpressionPool<CharPushExpression>(); 186 187 for (var i = 0; i < chars.Count; i++) { 188 var expression = CharPushExpression.Create(charPushExpressionPool, chars[i]); 189 inputExpressions.Add(expression); 190 } 191 } 192 193 // String 194 if (strings != null && strings.Count > 0) { 195 var stringPushExpressionPool = PoolContainer.GetStatefulExpressionPool<StringPushExpression>(); 196 197 for (var i = 0; i < strings.Count; i++) { 198 var expression = StringPushExpression.Create(stringPushExpressionPool, strings[i]); 199 inputExpressions.Add(expression); 200 } 201 } 202 203 // IntegerVector 204 if (integerVectors != null && integerVectors.Count > 0) { 205 var integerVectorPushExpressionPool = PoolContainer.GetStatefulExpressionPool<IntegerVectorPushExpression>(); 206 207 for (var i = 0; i < integerVectors.Count; i++) { 208 var expression = IntegerVectorPushExpression.Create(integerVectorPushExpressionPool, integerVectors[i]); 209 inputExpressions.Add(expression); 210 } 211 } 212 213 // FloatVector 214 if (floatVectors != null && floatVectors.Count > 0) { 215 var floatVectorPushExpressionPool = PoolContainer.GetStatefulExpressionPool<FloatVectorPushExpression>(); 216 217 for (var i = 0; i < floatVectors.Count; i++) { 218 var expression = FloatVectorPushExpression.Create(floatVectorPushExpressionPool, floatVectors[i]); 219 inputExpressions.Add(expression); 220 } 221 } 222 223 // StringVector 224 if (stringVectors != null && stringVectors.Count > 0) { 225 var stringVectorPushExpressionPool = PoolContainer.GetStatefulExpressionPool<StringVectorPushExpression>(); 226 227 for (var i = 0; i < stringVectors.Count; i++) { 228 var expression = StringVectorPushExpression.Create(stringVectorPushExpressionPool, stringVectors[i]); 229 inputExpressions.Add(expression); 230 } 231 } 232 } 233 135 234 136 235 public Task RunAsync(string code, CancellationToken token = default(CancellationToken)) { … … 240 339 currentProgram = null; 241 340 341 inputExpressions.Clear(); 242 342 Clear(); 243 343 ConfigureStacks(); … … 245 345 246 346 private void ConfigureStacks() { 247 foreach (var pair in this.Stacks) { 248 pair.Value.IsEnabled = Configuration.IsStackEnabled(pair.Key); 347 for (var i = 0u; i < supportedStackTypes.Length; i++) { 348 var key = supportedStackTypes[i]; 349 Stacks[key].IsEnabled = Configuration.IsStackEnabled(key); 249 350 } 250 351 … … 257 358 /// </summary> 258 359 public void Clear() { 259 foreach (var stack in Stacks) 260 stack.Value.Clear(); 360 for (var i = 0u; i < supportedStackTypes.Length; i++) { 361 var key = supportedStackTypes[i]; 362 Stacks[key].Clear(); 363 } 261 364 262 365 CustomExpressions.Clear(); -
branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Interpreter/PushInterpreterPool.cs
r15032 r15189 47 47 PushProgramPoolProvider.Clear(); 48 48 ExpressionListPoolProvider.Clear(); 49 pool.Clear(); 49 50 } 50 51 }
Note: See TracChangeset
for help on using the changeset viewer.