Changeset 12893 for branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Algorithms.GrammaticalOptimization/Solvers
- Timestamp:
- 08/24/15 13:56:27 (9 years ago)
- Location:
- branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Algorithms.GrammaticalOptimization/Solvers
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Algorithms.GrammaticalOptimization/Solvers/ExhaustiveRandomFirstSearch.cs
r11850 r12893 13 13 private readonly System.Collections.Generic.SortedList<double, Sequence> sortedList = new SortedList<double, Sequence>(); 14 14 private readonly IProblem problem; 15 private readonly Random random;15 private readonly System.Random random; 16 16 17 public ExhaustiveRandomFirstSearch(IProblem problem, Random random, int maxLen) {17 public ExhaustiveRandomFirstSearch(IProblem problem, System.Random random, int maxLen) { 18 18 this.maxLen = maxLen; 19 19 this.problem = problem; 20 this.random = new Random();20 this.random = new System.Random(); 21 21 } 22 22 -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Algorithms.GrammaticalOptimization/Solvers/RandomSearch.cs
r11850 r12893 5 5 public class RandomSearch : SolverBase { 6 6 private readonly int maxLen; 7 private readonly Random random;7 private readonly System.Random random; 8 8 private readonly IProblem problem; 9 9 10 public RandomSearch(IProblem problem, Random random, int maxLen) {10 public RandomSearch(IProblem problem, System.Random random, int maxLen) { 11 11 this.maxLen = maxLen; 12 12 this.random = random; -
branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/HeuristicLab.Algorithms.GrammaticalOptimization/Solvers/SequentialSearch.cs
r12876 r12893 6 6 using System.Runtime.InteropServices; 7 7 using System.Text; 8 using System.Windows.Markup; 8 9 using HeuristicLab.Algorithms.Bandits; 9 10 using HeuristicLab.Algorithms.Bandits.BanditPolicies; … … 40 41 private readonly int maxLen; 41 42 private readonly IProblem problem; 42 private readonly Random random;43 private readonly System.Random random; 43 44 private readonly int randomTries; 44 45 private readonly IGrammarPolicy behaviourPolicy; … … 51 52 private readonly List<string> stateChain; 52 53 53 public SequentialSearch(IProblem problem, int maxLen, Random random, int randomTries, IGrammarPolicy behaviourPolicy) {54 public SequentialSearch(IProblem problem, int maxLen, System.Random random, int randomTries, IGrammarPolicy behaviourPolicy) { 54 55 this.maxLen = maxLen; 55 56 this.problem = problem; … … 117 118 GenerateFollowStates(n); // creates child nodes for node n 118 119 120 119 121 int selectedChildIdx; 120 122 if (!behaviourPolicy.TrySelect(random, n.phrase, n.children.Select(ch => ch.phrase), out selectedChildIdx)) { 121 123 return false; 122 124 } 125 123 126 phrase.ReplaceAt(phrase.FirstNonTerminalIndex, 1, n.children[selectedChildIdx].alternative); 124 127 … … 167 170 } 168 171 172 173 169 174 private void DistributeReward(double reward) { 170 175 behaviourPolicy.UpdateReward(stateChain, reward); 171 176 } 177 172 178 173 179 … … 178 184 bestQuality = 0.0; 179 185 tries = 0; 186 //rootNode = new TreeNode("a*b+c*d+e*f+E", new ReadonlySequence("$")); 180 187 rootNode = new TreeNode(problem.Grammar.SentenceSymbol.ToString(), new ReadonlySequence("$")); 181 188 } … … 194 201 195 202 var n = rootNode; 196 203 int lvl = 0; 197 204 while (n != null) { 198 205 var phrase = n.phrase; 199 206 Console.ForegroundColor = ConsoleColor.White; 207 208 if (lvl++ > 10) return; 209 200 210 Console.WriteLine("{0,-30}", phrase); 201 211 var children = n.children; 202 212 if (children == null || !children.Any()) break; 203 var triesEnumerable = children.Select(ch => policy.GetTries(ch.phrase));204 double max Tries = triesEnumerable.Where(v => !double.IsInfinity(v)).DefaultIfEmpty(1).Max();205 max Tries = Math.Max(maxTries, 1.0);213 var valuesEnumerable = children.Select(ch => policy.GetValue(ch.phrase)); 214 double maxValue = valuesEnumerable.Where(v => !double.IsInfinity(v)).DefaultIfEmpty(0).Max(); 215 maxValue = Math.Max(maxValue, 1.0); 206 216 // write phrases 207 217 foreach (var ch in children) { 208 SetColorForValue(policy.GetTries(ch.phrase) / maxTries);218 //SetColorForValue(policy.GetValue(ch.phrase) / maxValue); 209 219 Console.Write(" {0,-4}", ch.phrase.Substring(Math.Max(0, ch.phrase.Length - 3), Math.Min(3, ch.phrase.Length))); 210 220 } … … 213 223 // write values 214 224 foreach (var ch in children) { 215 SetColorForValue(policy.GetTries(ch.phrase) / maxTries);225 //SetColorForValue(policy.GetValue(ch.phrase) / maxValue); 216 226 if (!double.IsInfinity(policy.GetValue(ch.phrase))) 217 227 Console.Write(" {0:F2}", policy.GetValue(ch.phrase) * 10.0); … … 223 233 // write tries 224 234 foreach (var ch in children) { 225 SetColorForValue(policy.GetTries(ch.phrase) / maxTries);235 //SetColorForValue(policy.GetValue(ch.phrase) / maxValue); 226 236 Console.Write(" {0,4}", policy.GetTries(ch.phrase)); 227 237 } 228 238 Console.WriteLine(); 229 int selectedChildIdx; 230 if (!policy.TrySelect(random, phrase, children.Select(ch => ch.phrase), out selectedChildIdx)) { 231 break; 232 } 239 var triesArr = valuesEnumerable.ToArray(); 240 //var selectedChildIdx = Array.IndexOf(triesArr, triesArr.Max()); 241 var valuesArr = children.Select(ch => policy.GetValue(ch.phrase)).ToArray(); 242 int selectedChildIdx = Enumerable.Range(0, children.Length).OrderByDescending(i => valuesArr[i]).ThenByDescending(i => triesArr[i]).First(); 243 244 //int selectedChildIdx; 245 //if (!policy.TrySelect(random, phrase, children.Select(ch => ch.phrase), out selectedChildIdx)) { 246 // break; 247 //} 233 248 n = n.children[selectedChildIdx]; 234 249 }
Note: See TracChangeset
for help on using the changeset viewer.