Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/10/15 16:12:08 (9 years ago)
Author:
gkronber
Message:

#2283: experiments with q-learning

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization-gkr/Main/Program.cs

    r12295 r12298  
    11using System;
     2using System.Collections.Generic;
    23using System.Diagnostics;
    34using System.Globalization;
     5using System.Linq;
     6using System.Text.RegularExpressions;
    47using HeuristicLab.Algorithms.Bandits.BanditPolicies;
    58using HeuristicLab.Algorithms.Bandits.GrammarPolicies;
     
    3841
    3942        var globalStatistics = new SentenceSetStatistics();
     43        ResetAlleleStatistics();
    4044        var random = new Random();
    4145
     
    6670          iterations++;
    6771          globalStatistics.AddSentence(sentence, quality);
    68 
     72          UpdateAlleleStatistics(sentence);
    6973          // comment this if you don't want to see solver statistics
    7074          if (iterations % 100 == 0) {
    71             if (iterations % 1000 == 0) Console.Clear();
     75            if (iterations % 1000 == 0) {
     76              Console.Clear();
     77            }
    7278            Console.SetCursorPosition(0, 0);
    73              alg.PrintStats();
    74             //policy.PrintStats();
     79            Console.WriteLine(iterations);
     80            WriteAlleleStatistics();
     81            Console.WriteLine(globalStatistics.BestSentenceQuality);
     82            Console.WriteLine(globalStatistics.BestSentence);
     83            Console.WriteLine(globalStatistics);
     84            //alg.PrintStats();
     85            policy.PrintStats();
     86            //ResetAlleleStatistics();
    7587          }
    76 
     88         
    7789          // uncomment this if you want to collect statistics of the generated sentences
    7890          //if (iterations % 100 == 0) {
     
    94106      }
    95107    }
     108
     109    private static void UpdateAlleleStatistics(string sentence) {
     110      for (int i = 0; i < sentence.Length; i++) {
     111        var allele = sentence.Substring(i, 1);
     112        if (alleleStatistics.ContainsKey(allele)) alleleStatistics[allele]++;
     113      }
     114      for (int i = 0; i < sentence.Length - 2; i+=2) {
     115        var allele = sentence.Substring(i, 3);
     116        if (alleleStatistics.ContainsKey(allele)) alleleStatistics[allele]++;
     117      }
     118      for (int i = 0; i < sentence.Length - 4; i+=2) {
     119        var allele = sentence.Substring(i, 5);
     120        if (alleleStatistics.ContainsKey(allele)) alleleStatistics[allele]++;
     121      }
     122    }
     123
     124
     125    private static Dictionary<string, int> alleleStatistics;
     126
     127    private static void ResetAlleleStatistics() {
     128      alleleStatistics = new Dictionary<string, int>()
     129      {
     130        {"a", 0},
     131        {"b", 0},
     132        {"c", 0},
     133        {"d", 0},
     134        {"e", 0},
     135        {"f", 0},
     136        {"g", 0},
     137        {"h", 0},
     138        {"i", 0},
     139        {"j", 0},
     140        {"a*b", 0},
     141        {"b*a", 0},
     142        {"c*d", 0},
     143        {"d*c", 0},
     144        {"e*f", 0},
     145        {"f*e", 0},
     146        {"a*g*i", 0},
     147        {"a*i*g", 0},
     148        {"g*a*i", 0},
     149        {"g*i*a", 0},
     150        {"i*g*a", 0},
     151        {"i*a*g", 0},
     152        {"j*c*f", 0},
     153        {"j*f*c", 0},
     154        {"c*j*f", 0},
     155        {"c*f*j", 0},
     156        {"f*c*j", 0},
     157        {"f*j*c", 0}
     158      };
     159    }
     160
     161
     162    private static void WriteAlleleStatistics() {
     163      double count = alleleStatistics.Sum(e => e.Value);
     164      foreach (var entry in alleleStatistics.OrderByDescending(e=>e.Value)) {
     165        Console.WriteLine("{0,-10} {1,-10}", entry.Key, entry.Value);
     166      }
     167    }
    96168  }
    97169}
Note: See TracChangeset for help on using the changeset viewer.