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
|
Rev | Line | |
---|
[11770] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Diagnostics.Eventing.Reader;
|
---|
| 4 | using System.Linq;
|
---|
| 5 | using System.Runtime.InteropServices;
|
---|
| 6 | using System.Text;
|
---|
| 7 | using System.Threading.Tasks;
|
---|
| 8 | using HeuristicLab.Common;
|
---|
| 9 | using HeuristicLab.Problems.GrammaticalOptimization;
|
---|
| 10 |
|
---|
| 11 | namespace 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.