1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Configuration;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Security.Policy;
|
---|
6 | using System.Text;
|
---|
7 | using HeuristicLab.Common;
|
---|
8 | using HeuristicLab.Problems.GrammaticalOptimization;
|
---|
9 |
|
---|
10 | namespace 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 | }
|
---|