Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11976


Ignore:
Timestamp:
02/11/15 02:22:18 (10 years ago)
Author:
gkronber
Message:

#2283 worked on seq search for ant

Location:
branches/HeuristicLab.Problems.GrammaticalOptimization
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.Bandits/Policies/ThresholdAscentPolicy.cs

    r11806 r11976  
    3131      public double Value {
    3232        get {
    33           if (Tries == 0.0) return 0.0;
     33          if (Tries == 0.0) return double.PositiveInfinity;
    3434          return rewardHistogram[thresholdBin] / (double)Tries;
    3535        }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/SequentialDecisionPolicies/GenericFunctionApproximationGrammarPolicy.cs

    r11974 r11976  
    1414  public sealed class GenericFunctionApproximationGrammarPolicy : IGrammarPolicy {
    1515    private Dictionary<string, double> featureWeigths; // stores the necessary information for bandit policies for each state (=canonical phrase)
     16    private Dictionary<string, int> featureTries;
    1617    private HashSet<string> done;
    1718    private readonly bool useCanonicalPhrases;
    1819    private readonly IProblem problem;
     20
    1921
    2022
     
    2325      this.problem = problem;
    2426      this.featureWeigths = new Dictionary<string, double>();
     27      this.featureTries = new Dictionary<string, int>();
    2528      this.done = new HashSet<string>();
    2629    }
     
    5760        originalIdx++;
    5861      }
    59      
    60       const double beta = 20.0;
    61       var w = from q in activeAfterStates
    62               select Math.Exp(beta * q);
     62
     63
     64      /*
     65      const double beta = 1;
     66      var w = from idx in Enumerable.Range(0, maxIdx)
     67              let afterStateQ = activeAfterStates[idx]
     68              select Math.Exp(beta * afterStateQ);
    6369
    6470      var bestAction = Enumerable.Range(0, maxIdx).SampleProportional(random, w);
    6571      selectedStateIdx = actionIndexMap[bestAction];
    6672      Debug.Assert(selectedStateIdx >= 0);
    67      
    68       /*
     73      */
     74
     75
    6976      if (random.NextDouble() < 0.2) {
    7077        selectedStateIdx = actionIndexMap[random.Next(maxIdx)];
     
    8491        selectedStateIdx = actionIndexMap[bestIdxs[random.Next(bestIdxs.Count)]];
    8592      }
    86       */
     93
    8794
    8895
     
    114121
    115122    public int GetTries(string state) {
    116       return 1;
     123      return 0;
     124    }
     125
     126    public int GetFeatureTries(string featureId) {
     127      int t;
     128      if (featureTries.TryGetValue(featureId, out t)) {
     129        return t;
     130      } else return 0;
    117131    }
    118132
    119133    public double GetValue(string state) {
    120       return problem.GetFeatures(state).Sum(feature => GetWeight(feature));
     134      return problem.GetFeatures(state).Average(feature => GetWeight(feature));
    121135    }
    122136
     
    124138      double w;
    125139      if (featureWeigths.TryGetValue(feature.Id, out w)) return w * feature.Value;
    126       else return 0.0; // TODO: alternatives?
     140      else return 0.0;
    127141    }
    128142    private void UpdateWeights(string state, double reward) {
    129       const double alpha = 0.01;
    130143      double delta = reward - GetValue(state);
     144      delta /= problem.GetFeatures(state).Count();
     145      const double alpha = 0.001;
    131146      foreach (var feature in problem.GetFeatures(state)) {
     147        featureTries[feature.Id] = GetFeatureTries(feature.Id) + 1;
     148        Debug.Assert(GetFeatureTries(feature.Id) >= 1);
     149        //double alpha = 1.0 / GetFeatureTries(feature.Id);
     150        //alpha = Math.Max(alpha, 0.01);
     151
    132152        double w;
    133153        if (!featureWeigths.TryGetValue(feature.Id, out w)) {
    134           featureWeigths[feature.Id] = alpha * delta;
     154          featureWeigths[feature.Id] = alpha * delta * feature.Value;
    135155        } else {
    136           featureWeigths[feature.Id] += alpha * delta;
     156          featureWeigths[feature.Id] += alpha * delta * feature.Value;
    137157        }
    138158      }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GrammaticalOptimization/Solvers/SequentialSearch.cs

    r11850 r11976  
    166166    private void DistributeReward(double reward) {
    167167      behaviourPolicy.UpdateReward(stateChain, reward);
    168       greedyPolicy.UpdateReward(stateChain, reward);
     168      //greedyPolicy.UpdateReward(stateChain, reward);
    169169    }
    170170
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Distributions/GaussianModel.cs

    r11851 r11976  
    3838      this.meanPriorMu = meanPriorMu;
    3939      this.meanPriorVariance = meanPriorVariance;
    40 
     40     
    4141      this.knownVariance = false;
    4242      this.precisionPriorAlpha = precisionPriorAlpha;
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Feature.cs

    r11832 r11976  
    1515      this.Value = val;
    1616    }
     17
     18    public override string ToString() {
     19      return string.Format("{0} {1:N3}", Id, Value);
     20    }
    1721  }
    1822}
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/SantaFeAntProblem.cs

    r11974 r11976  
    5454    }
    5555
    56     private void Run(Ant ant, string sentence, ref int p) {
     56    private static void Run(Ant ant, string sentence, ref int p, bool stopAfterFirst = false) {
    5757      while (!ant.Done()) {
    58         if (p >= sentence.Length) p = 0; // restart
     58        if (p >= sentence.Length) {
     59          if (stopAfterFirst) return;
     60          p = 0; // restart
     61        }
    5962        switch (sentence[p]) {
    6063          case 'r': {
     
    9396              break;
    9497            }
     98          case '.': {
     99              // nop
     100              p++;
     101              ant.Nop();
     102              break;
     103            }
    95104          case ')': {
    96105              p++; // skip
     
    104113    }
    105114
    106     private void Skip(string sentence, ref int p) {
     115    private static void Skip(string sentence, ref int p) {
    107116      int openCount = 1;
    108117      while (openCount > 0) {
     
    114123
    115124    public string CanonicalRepresentation(string phrase) {
     125      phrase = phrase.Replace("A", ".");
    116126      var sb = new StringBuilder(phrase);
    117127      string canonicalPhrase = phrase;
     
    119129      do {
    120130        oldPhrase = canonicalPhrase;
    121         sb.Replace("ll", "rr").Replace("rl", "lr").Replace("lr", "").Replace("lll", "r").Replace("rrr", "l");
     131        sb.Replace("ll", "rr").Replace("rl", "").Replace("lr", "").Replace("lll", "r").Replace("rrr", "l");
     132        sb.Replace("?(m)(m)", "?()()m").Replace("?(l)(l)", "?()()l").Replace("?(r)(r)", "?()()r").Replace("?()()", "");
    122133        canonicalPhrase = sb.ToString();
    123134      } while (canonicalPhrase != oldPhrase);
     
    126137
    127138    public IEnumerable<Feature> GetFeatures(string phrase) {
    128       yield return new Feature(CanonicalRepresentation(phrase), 1.0);
     139      phrase = CanonicalRepresentation(phrase);
     140      var isTerminal = grammar.IsTerminal(phrase);
     141
     142
     143      //yield return new Feature("const", 0.0);
     144      //if (phrase.Length > 0) {
     145      //  var ant = new Ant(recordTrail: true);
     146      //  int pos = 0;
     147      //  Run(ant, phrase, ref pos, true);
     148      //  //yield return new Feature("food", ant.FoodEaten);
     149      //  yield return new Feature(ant.Trail, 1.0);
     150      //}
     151      //yield return new Feature(isTerminal + "const", 0.0);
     152      //yield return new Feature(isTerminal.ToString() + phrase.Length, 1.0);
     153      //int ntIdx;
     154      //for (ntIdx = 0; ntIdx < phrase.Length; ntIdx++) if (grammar.IsNonTerminal(phrase[ntIdx])) break;
     155      //for (int l = ntIdx-2; l >= 0; l--) {
     156      //  yield return new Feature(phrase.Substring(l, ntIdx-l-1), 1.0);
     157      //}
     158      //
     159      ////yield return new Feature("$" + phrase[0], 1.0);
     160      // if (!isTerminal) {
     161      //   for (int i = 4; i < phrase.Length; i++) {
     162      //     if (grammar.IsNonTerminal(phrase[i])) {
     163      //       yield return new Feature(phrase[i - 4].ToString() + phrase[i - 3].ToString() + phrase[i - 2] + phrase[i - 1], 0.1);
     164      //       break;
     165      //     }
     166      //   }
     167      // }
     168      // var d = 0;
     169      // var ls = 0;
     170      // var rs = 0;
     171      // var qs = 0;
     172      // foreach (var ch in phrase) if (ch == 'l') { d--; ls++; } else if (ch == 'r') { d++; rs++; } else if (ch == '?') qs++;
     173      // yield return new Feature(isTerminal + "D" + Math.Abs(d), 1.0);
     174      // yield return new Feature(isTerminal + "R" + rs, 1.0);
     175      // yield return new Feature(isTerminal + "L" + ls, 1.0);
     176      // yield return new Feature(isTerminal + "?" + qs, 1.0);
     177
     178      yield return new Feature(phrase, 1.0);
     179      //for (int i = 0; i < phrase.Length; i++)
     180      //  yield return new Feature(i.ToString() + phrase[i].ToString(), 1.0);
    129181      // yield return new Feature("Length", phrase.Length); //
    130182      // foreach (var pair in phrase.Zip(phrase.Skip(1), Tuple.Create)) {
     
    158210
    159211  public class Ant {
    160     private const int maxSteps = 600;
     212    private int maxSteps = 600;
     213    public int MaxSteps { get { return maxSteps; } set { maxSteps = value; } }
    161214    public enum HeadingEnum { North, East, South, West };
    162215    public int FoodEaten { get; private set; }
     
    166219    private int steps;
    167220    private HeadingEnum heading;
    168 
    169 
    170 
    171     public Ant() {
     221    public int PosX { get { return posX; } }
     222    public int PosY { get { return posY; } }
     223    public HeadingEnum Heading { get { return heading; } }
     224    private bool recordTrail = false;
     225    private StringBuilder trailBuilder;
     226
     227    public string Trail {
     228      get {
     229        if (!recordTrail) throw new NotSupportedException();
     230        else return trailBuilder.ToString() + heading; // add final heading as state
     231      }
     232    }
     233
     234    public Ant(bool recordTrail = false) {
    172235      world[00] = " ###                            ".ToCharArray();
    173236      world[01] = "   #                            ".ToCharArray();
     
    208271      FoodEaten = 0;
    209272      steps = 0;
     273
     274      this.recordTrail = recordTrail;
     275      if (this.recordTrail) trailBuilder = new StringBuilder();
    210276    }
    211277
     
    238304          world[posY][posX] = '.';
    239305        }
     306        if (recordTrail) trailBuilder.Append("m" + posX + "x" + posY); // record position change
     307      }
     308    }
     309
     310    public void Nop() {
     311      // wait one time step
     312      if (steps <= maxSteps) {
     313        steps++;
    240314      }
    241315    }
     
    262336      int nextPosY = posY;
    263337      MoveInternal(ref nextPosX, ref nextPosY);
     338
     339      if (recordTrail) trailBuilder.Append("?" + nextPosX + "x" + nextPosY); // record check
     340
    264341      return world[nextPosY][nextPosX] == '#';
    265342    }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/SymbolicRegressionPoly10Problem.cs

    r11857 r11976  
    153153        canonicalTerms.Add(CanonicalTerm(t));
    154154      }
    155       return canonicalTerms.Select(entry => new Feature(entry, 1.0)).Concat(new Feature[] { new Feature(CanonicalRepresentation(phrase), 1.0) });
     155      return canonicalTerms.Select(entry => new Feature(entry, 1.0))
     156        .Concat(new Feature[] { new Feature(CanonicalRepresentation(phrase), 1.0) });
    156157    }
    157158
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Main/Program.cs

    r11974 r11976  
    2828      //RunDemo();
    2929      //RunGpDemo();
    30       //RunGridTest();
     30      // RunGridTest();
    3131      //RunGpGridTest();
    3232     RunFunApproxTest();
     
    8383         //() => new BoltzmannExplorationPolicy(200),
    8484         //() => new BoltzmannExplorationPolicy(500),
    85          () => new ChernoffIntervalEstimationPolicy( 0.01),
    86          () => new ChernoffIntervalEstimationPolicy( 0.05),
    87          () => new ChernoffIntervalEstimationPolicy( 0.1),
    88          () => new ChernoffIntervalEstimationPolicy( 0.2),
     85         // () => new ChernoffIntervalEstimationPolicy( 0.01),
     86         // () => new ChernoffIntervalEstimationPolicy( 0.05),
     87         // () => new ChernoffIntervalEstimationPolicy( 0.1),
     88         // () => new ChernoffIntervalEstimationPolicy( 0.2),
    8989         //() => new ThresholdAscentPolicy(5, 0.01),
    9090         //() => new ThresholdAscentPolicy(5, 0.05),
     
    100100         //() => new ThresholdAscentPolicy(50, 0.2),
    101101         //() => new ThresholdAscentPolicy(100, 0.01),
    102          //() => new ThresholdAscentPolicy(100, 0.05),
     102         () => new ThresholdAscentPolicy(100, 0.05),
    103103         //() => new ThresholdAscentPolicy(100, 0.1),
    104104         //() => new ThresholdAscentPolicy(100, 0.2),
     
    113113      var instanceFactories = new Func<Random, Tuple<IProblem, int>>[]
    114114      {
    115         (rand) => Tuple.Create((IProblem)new SantaFeAntProblem(), 17),
     115        //(rand) => Tuple.Create((IProblem)new SantaFeAntProblem(), 17),
    116116        //(rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:0, correctReward:1, decoyReward:0, phrasesAsSets:false ), 15),
    117117        //(rand) => Tuple.Create((IProblem)new FindPhrasesProblem(rand, 10, numPhrases:5, phraseLen:3, numOptimalPhrases:5, numDecoyPhrases:0, correctReward:1, decoyReward:0, phrasesAsSets:true ), 15),
     
    122122
    123123      foreach (var instanceFactory in instanceFactories) {
    124         foreach (var useCanonical in new bool[] { true, false }) {
    125           foreach (var randomTries in new int[] { 0, 1, 10 /*, /* 5, 100 /*, 500, 1000 */}) {
     124        foreach (var useCanonical in new bool[] { true /*, false */ }) {
     125          foreach (var randomTries in new int[] { 1 /*, 1, 10 /*, /* 5, 100 /*, 500, 1000 */}) {
    126126            foreach (var policyFactory in policyFactories) {
    127127              var myRandomTries = randomTries;
     
    311311      const int seed = 31415;
    312312      //const int maxIters = 50000;
    313       var rand = new Random(seed);
     313      var rand = new Random();
    314314      var problemFactories = new Func<Tuple<int, int, ISymbolicExpressionTreeProblem>>[]
    315315      {
     
    349349              iterations++;
    350350              globalStatistics.AddSentence(sentence, quality);
    351 
    352               if (iterations % 1000 == 0) {
     351              //if (iterations % 100 == 0) {
     352              //  Console.Clear();
     353              //  Console.SetCursorPosition(0, 0);
     354              //  alg.PrintStats();
     355              //}
     356              //Console.WriteLine("{0:N5} {1}", quality, sentence);
     357              if (iterations % 200 == 0) {
    353358                Console.WriteLine("\"{0,25}\" {1} \"{2,25}\" {3}", algName, maxSize, probName, globalStatistics);
    354359              }
Note: See TracChangeset for help on using the changeset viewer.