Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.Bandits/GrammarPolicies/GreedyPolicy.cs @ 11792

Last change on this file since 11792 was 11770, checked in by gkronber, 10 years ago

#2283: worked on generic sequential search alg with bandit policy as parameter

File size: 1.5 KB
RevLine 
[11770]1using System;
2using System.Collections.Generic;
3using System.Diagnostics.Eventing.Reader;
4using System.Linq;
5using System.Runtime.InteropServices;
6using System.Text;
7using System.Threading.Tasks;
8using HeuristicLab.Common;
9using HeuristicLab.Problems.GrammaticalOptimization;
10
11namespace HeuristicLab.Algorithms.Bandits.GrammarPolicies {
12  public class GreedyPolicy : GrammarPolicy {
13    private readonly IProblem problem;
14
15    public GreedyPolicy(IProblem problem, bool useCanonicalRepresentation)
16      : base(problem, useCanonicalRepresentation) {
17    }
18
19    public override bool TrySelect(Random random, ReadonlySequence curState, IEnumerable<ReadonlySequence> afterStates, out ReadonlySequence selectedState) {
20      // always returns the afterstate with the highest value
21      double maxV = V(afterStates.First());
22      var bestStates = new List<ReadonlySequence>();
23      bestStates.Add(afterStates.First());
24      foreach (var state in afterStates.Skip(1)) {
25        if (maxV < V(state)) {
26          maxV = V(state);
27          bestStates.Clear();
28          bestStates.Add(state);
29        } else if (maxV == V(state)) {
30          bestStates.Add(state);
31        }
32      }
33      // break ties randomly
34      // selectedState = bestStates.SelectRandom(random);
35      // break ties using first
36      selectedState = bestStates.First();
37      return true;
38    }
39
40    private double V(ReadonlySequence state) {
41      return base.AvgReward(state);
42    }
43  }
44}
Note: See TracBrowser for help on using the repository browser.