Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.Bandits/GrammarPolicies/RandomNoResamplingPolicy.cs @ 11770

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

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

File size: 1.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Configuration;
4using System.Linq;
5using System.Security.Policy;
6using System.Text;
7using HeuristicLab.Common;
8using HeuristicLab.Problems.GrammaticalOptimization;
9
10namespace HeuristicLab.Algorithms.Bandits.GrammarPolicies {
11  public class RandomNoResamplingPolicy : GrammarPolicy {
12
13    private readonly HashSet<string> done;
14
15    public RandomNoResamplingPolicy(IProblem problem, bool useCanonicalRepresentation)
16      : base(problem, useCanonicalRepresentation) {
17      this.done = new HashSet<string>();
18    }
19
20    public override bool TrySelect(Random random, ReadonlySequence curState, IEnumerable<ReadonlySequence> afterStates, out ReadonlySequence selectedState) {
21      // only select states that are not yet done
22      afterStates = afterStates.Where(a => !done.Contains(a.ToString())).ToArray();
23      if (!afterStates.Any()) {
24        // fail because all follow states have already been visited => also disable the current state
25        done.Add(CanonicalState(curState.ToString()));
26        selectedState = null;
27        return false;
28      }
29
30      selectedState = afterStates.SelectRandom(random);
31      return true;
32    }
33
34    public override void UpdateReward(IEnumerable<ReadonlySequence> stateTrajectory, double reward) {
35      base.UpdateReward(stateTrajectory, reward);
36      // ignore rewards but update the set of visited terminal states
37
38      // the last state could be terminal
39      var lastState = stateTrajectory.Last();
40      if (lastState.IsTerminal) done.Add(CanonicalState(lastState.ToString()));
41    }
42
43    public override void Reset() {
44      base.Reset();
45      done.Clear();
46    }
47  }
48}
Note: See TracBrowser for help on using the repository browser.