Free cookie consent management tool by TermsFeed Policy Generator

Changeset 11972


Ignore:
Timestamp:
02/09/15 09:48:30 (10 years ago)
Author:
gkronber
Message:

#2283 new experiments

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

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Algorithms.GeneticProgramming/StandardGP.cs

    r11895 r11972  
    2929      // default parameter values
    3030      PopulationSize = 1000;
    31       TournamentGroupSize = 7;
     31      TournamentGroupSize = 4;
    3232      MutationRate = 0.15;
    3333      MaxSolutionSize = 100;
     
    6565
    6666        ga.Problem = hlProblem;
     67        //ga.Problem.SolutionCreatorParameter.ActualValue = new FullTreeCreator();
    6768        var mutator = (MultiSymbolicExpressionTreeManipulator)ga.MutatorParameter.ValidValues.Single(op => op.Name == "MultiSymbolicExpressionTreeManipulator");
    6869        foreach (var op in mutator.Operators) {
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Common/ExpressionExtender.cs

    r11966 r11972  
    4949    }
    5050
    51     // an expression is a list of terms
     51    // an expression is a sorted set of terms
    5252    // a term is an (ordered) list of factors
    53     // a factor is a symbol or an inverted expression
     53    // a factor is either a single symbol or an inverted expression
    5454    // CanonicalExpression reads multiple terms (expressions) and must merge the terms in the expression (ordering and duplicates)
    5555    // CanonicalTerm reads multiple factors and must expand factors whenever it reads a combined factor (expression) it produces an expression
     
    8383    // canonical term returns either a single term (product of factors) or a set of terms
    8484    private Expr CanonicalTerm() {
    85       var factors = new List<Expr>();
    86       var invFactors = new List<Expr>();
     85      var factors = new List<Factor>();
    8786      var f = CanonicalFact();
    8887      if (f != null) factors.Add(f);
     
    9796          f = CanonicalFact();
    9897          f.Invert();
    99           if (f != null) invFactors.Add(f);
     98          if (f != null) factors.Add(f);
    10099        }
    101100        curSy = CurSy();
    102101      }
    103 
    104       // cancellation
    105       foreach (var invF in invFactors.ToArray()) {
    106         invF.Invert();
    107         if (factors.Contains(invF)) {
    108           invFactors.Remove(invF);
    109           factors.Remove(invF);
    110           if (factors.Count == 0) factors.Add(new Expr(new Term('1')));
    111         } else invF.Invert();
    112       }
    113 
    114       return ExpandFactors(factors, invFactors);
    115     }
    116 
    117     private Expr ExpandFactors(List<Expr> factors, List<Expr> invFactors) {
     102      return ExpandFactors(factors);
     103    }
     104
     105    /*
     106    private List<Factor> CancelFactors(Term t) {
     107      var nonInvF = t.Factors.Where(f => !f.IsInverse).ToArray();
     108      var invF = t.Factors.Where(f => f.IsInverse).ToArray();
     109
     110      var result = new List<Factor>();
     111      foreach (var f in nonInvF) {
     112        if (!invF.Contains(f)) {
     113          result.Add(f);
     114        }
     115      }
     116      if (result.Count == 0) result.Add(new Factor('1'));
     117      return result;
     118    }
     119    */
     120    // canonical fact returns a factor (either a singe variable, or a set of terms)
     121    private Factor CanonicalFact() {
     122      var curSy = CurSy();
     123      if (curSy == '!') {
     124        throw new NotSupportedException();
     125      } else if (curSy == '(') {
     126        NewSy();
     127        Expr r = CanonicalExpr(); // this is already simplified
     128        if (CurSy() != ')') throw new ArgumentException();
     129        NewSy();
     130        return new Factor(r);
     131      } else if (curSy >= 'a' && curSy <= 'z') {
     132        NewSy();
     133        return new Factor(curSy);
     134        //       } else if (curSy >= '0' && curSy <= '9') {
     135      } else if (curSy >= 'A' && curSy <= 'Z') {
     136        // treat nonterminals in the same way as variables
     137        NewSy();
     138        return new Factor(curSy);
     139
     140      } else throw new ArgumentException("found symbol " + curSy);
     141    }
     142
     143    // a list of factors (symbols, or expressions, and possibly inverses are read
     144    // a lis to factors symbols or expressions and possibly inverses are produced
     145    // all non-inverse expression factors are expanded
     146    private Expr ExpandFactors(IEnumerable<Factor> factors) {
    118147      // if (invFactors.Count > 0) throw new NotImplementedException();
    119       Expr currentFact = factors.First(); // new Expr(simpleFactor));
    120       Debug.Assert(!currentFact.Inverse); // the first factor is never an inverted factor
     148      Debug.Assert(!factors.First().IsInverse); // the first factor is never an inverted factor
     149
     150      // each factor could be a list of terms (expression)
     151
     152      Expr currentFact = null;
     153      var firstFactor = factors.First();
     154      if (firstFactor.IsSimpleFactor) currentFact = new Expr(new Term(firstFactor));
     155      else currentFact = firstFactor.Expr;
     156
    121157      foreach (var fact in factors.Skip(1)) {
    122         currentFact = AllProducts(currentFact, fact);
    123       }
    124       foreach (var invF in invFactors) {
    125         currentFact = AllInvProducts(currentFact, invF);
     158        Expr curExpr = null;
     159        if (fact.IsSimpleFactor || fact.IsInverse) curExpr = new Expr(new Term(fact));
     160        else curExpr = fact.Expr;
     161        currentFact = AllProducts(currentFact, curExpr);
    126162      }
    127163      return currentFact;
     
    133169      var combs = from aT in aTerms
    134170                  from bT in bTerms
    135                   select new Term(aT.Symbols.Concat(bT.Symbols), aT.InvExpressions.Concat(bT.InvExpressions));
     171                  let factors = CancelFactors(aT.Factors.Concat(bT.Factors))
     172                  select new Term(factors);
    136173      return new Expr(combs);
    137174    }
    138175
    139     private Expr AllInvProducts(Expr a, Expr b) {
    140       var aTerms = a.Terms.ToArray();
    141       var combs = from aT in aTerms
    142                   select new Term(aT.Symbols, aT.InvExpressions.Concat(new Expr[] { b }));
    143       return new Expr(combs);
    144     }
    145 
    146     // canonical fact returns a factor (either a singe variable, or a set of terms)
    147     private Expr CanonicalFact() {
    148       var curSy = CurSy();
    149       if (curSy == '!') {
    150         throw new NotSupportedException();
    151       } else if (curSy == '(') {
    152         NewSy();
    153         Expr r = CanonicalExpr();
    154         if (CurSy() != ')') throw new ArgumentException();
    155         NewSy();
    156         return r;
    157       } else if (curSy >= 'a' && curSy <= 'z') {
    158         NewSy();
    159         return new Expr(new Term(curSy));
    160         //       } else if (curSy >= '0' && curSy <= '9') {
    161       } else if (curSy >= 'A' && curSy <= 'Z') {
    162         // treat nonterminals in the same way as variables
    163         NewSy();
    164         return new Expr(new Term(curSy));
    165 
    166       } else throw new ArgumentException("found symbol " + curSy);
     176    private IEnumerable<Factor> CancelFactors(IEnumerable<Factor> factors) {
     177      var factorsArr = factors.ToArray();
     178      var results = new List<Factor>(factors);
     179      foreach (var f in factorsArr) {
     180        if (f.ToString() == "1") results.Remove(f);
     181        if (f.IsInverse) {
     182          // find matching
     183          Factor match;
     184          if (f.IsSimpleFactor) match = factorsArr.FirstOrDefault(other => !other.IsInverse && f.Symbol.Equals(other.Symbol));
     185          else match = factorsArr.FirstOrDefault(other => !other.IsInverse && f.Expr.Equals(other.Expr));
     186          if (match != null) {
     187            results.Remove(match);
     188            results.Remove(f);
     189          }
     190        }
     191      }
     192      if (results.Count == 0) results.Add(new Factor('1'));
     193      return results;
    167194    }
    168195
     
    170197    // term can be merged (essentially an ordered list of factors)
    171198    internal class Term : IComparable<Term> {
    172       private readonly SortedList<char, int> factorSymbs; // factor symbol and the number of occurrences
    173       private readonly SortedList<Expr, int> invExpressions;
    174 
    175       public IEnumerable<char> Symbols {
     199      private readonly SortedList<Factor, int> factors; // factor symbol and the number of occurrences
     200
     201      public IEnumerable<Factor> Factors {
    176202        get {
    177           return factorSymbs.SelectMany(p => Enumerable.Repeat(p.Key, p.Value));
    178         }
    179       }
    180 
    181       public IEnumerable<Expr> InvExpressions {
    182         get {
    183           if (invExpressions == null) return Enumerable.Empty<Expr>();
    184           return invExpressions.Where(p => p.Value > 0).SelectMany(p => Enumerable.Repeat(p.Key, p.Value));
    185         }
    186       }
    187 
    188       public Term(char f) {
    189         factorSymbs = new SortedList<char, int>(new FactorComparer());
    190         factorSymbs.Add(f, 1);
    191       }
    192       public Term(IEnumerable<char> factors, IEnumerable<Expr> invFactors) {
    193         factorSymbs = new SortedList<char, int>(new FactorComparer());
    194         invExpressions = new SortedList<Expr, int>();
     203          return factors.SelectMany(p => Enumerable.Repeat(p.Key, p.Value));
     204        }
     205      }
     206
     207      public Term(Factor f) {
     208        factors = new SortedList<Factor, int>();
     209        factors.Add(f, 1);
     210      }
     211      public Term(IEnumerable<Factor> factors) {
     212        this.factors = new SortedList<Factor, int>();
    195213        foreach (var f in factors) {
    196           if (factorSymbs.ContainsKey(f)) factorSymbs[f] += 1;
    197           else factorSymbs.Add(f, 1);
    198         }
    199 
    200         foreach (var invF in invFactors) {
    201           if (invF.Terms.Count == 1) {
    202             var t = invF.Terms.First();
    203             foreach (var f in factors.Concat(new char[] { '1' })) {
    204               if (t.factorSymbs.ContainsKey(f)) {
    205                 t.factorSymbs[f] -= 1;
    206                 if (t.factorSymbs[f] == 0) t.factorSymbs.Remove(f);
    207               }
    208             }
    209           }
    210           if (invExpressions.ContainsKey(invF)) invExpressions[invF] += 1;
    211           else invExpressions.Add(invF, 1);
     214          if (this.factors.ContainsKey(f)) this.factors[f] += 1;
     215          else this.factors.Add(f, 1);
    212216        }
    213217      }
    214218
    215219      public int CompareTo(Term other) {
    216         if (ContainsNonTerminal(Symbols) && !ContainsNonTerminal(other.Symbols)) {
     220        if (ContainsNonTerminal(Factors) && !ContainsNonTerminal(other.Factors)) {
    217221          return 1;
    218         } else if (!ContainsNonTerminal(Symbols) && ContainsNonTerminal(other.Symbols)) {
     222        } else if (!ContainsNonTerminal(Factors) && ContainsNonTerminal(other.Factors)) {
    219223          return -1;
    220224        } else {
    221           var countComp = Symbols.Count().CompareTo(other.Symbols.Count());
     225          var countComp = Factors.Count().CompareTo(other.Factors.Count());
    222226          if (countComp != 0) return countComp;
    223           return string.Join("", Symbols).CompareTo(string.Join("", other.Symbols));
     227          return string.Join("", Factors).CompareTo(string.Join("", other.Factors));
    224228        }
    225229      }
    226230
    227231      public override string ToString() {
    228         if (InvExpressions.Any())
    229           return string.Join("*", Symbols) + "%" + string.Join("%", InvExpressions);
    230         else {
    231           return string.Join("*", Symbols);
    232         }
     232
     233        return string.Join("*", Factors);
    233234      }
    234235      public override bool Equals(object obj) {
    235236        var other = obj as Term;
    236237        if (other == null) return false;
    237         if (this.Symbols.Count() != other.Symbols.Count()) return false;
    238         if (this.InvExpressions.Count() != other.InvExpressions.Count()) return false;
    239         if (this.Symbols.Zip(other.Symbols, Tuple.Create).Any(t => t.Item1 != t.Item2)) return false;
    240         if (this.InvExpressions.Zip(other.InvExpressions, Tuple.Create).Any(t => t.Item1 != t.Item2)) return false;
     238        if (this.Factors.Count() != other.Factors.Count()) return false;
     239        if (this.Factors.Zip(other.Factors, Tuple.Create).Any(t => t.Item1 != t.Item2)) return false;
    241240        return true;
    242241      }
    243242      public override int GetHashCode() {
    244243        var h = 31415;
    245         foreach (var v in Symbols) {
     244        foreach (var v in Factors) {
    246245          h ^= v.GetHashCode();
    247246        }
    248         foreach (var v in InvExpressions) {
    249           h ^= v.GetHashCode();
    250         }
     247        return h;
     248      }
     249    }
     250    #endregion
     251
     252    #region factor
     253    // factors is either a single symbol or an inverted expression
     254    internal class Factor : IComparable<Factor> {
     255      public bool IsSimpleFactor { get { return Expr == null; } }
     256      public char Symbol { get { return symbol; } }
     257      public Expr Expr { get { return expr; } }
     258      public bool IsInverse { get { return inv; } }
     259      private readonly char symbol = '\0';
     260      private readonly Expr expr;
     261      private bool inv;
     262
     263      public Factor(char f) {
     264        this.symbol = f;
     265      }
     266      public Factor(Expr expr) {
     267        this.expr = expr;
     268      }
     269
     270      public void Invert() {
     271        this.inv = !inv;
     272      }
     273
     274      public int CompareTo(Factor other) {
     275        // 1) single symbol factors first
     276        // 2) expression factors by expression compare
     277        var crit1 = ContainsNonTerminal(this).CompareTo(ContainsNonTerminal(other));
     278        if (crit1 != 0) return crit1;
     279
     280        var crit2 = this.IsInverse.CompareTo(other.IsInverse);
     281        if (crit2 != 0) return crit2;
     282
     283        var crit3 = this.IsSimpleFactor.CompareTo(other.IsSimpleFactor);
     284        if (crit3 != 0) return crit3;
     285
     286        // both are simple or expressions
     287        if (IsSimpleFactor) return this.symbol.CompareTo(other.symbol);
     288        else return this.Expr.CompareTo(other.Expr);
     289      }
     290
     291      public override string ToString() {
     292        var s = Expr == null ? symbol.ToString() : "(" + expr.ToString() + ")";
     293        if (IsInverse) {
     294          return "1/" + s;
     295        } else return s;
     296      }
     297      public override bool Equals(object obj) {
     298        var other = obj as Factor;
     299        if (other == null) return false;
     300        if (IsInverse != other.IsInverse) return false;
     301        if (this.symbol != other.symbol) return false;
     302        if (this.Expr != other.Expr) return false;
     303        return true;
     304      }
     305      public override int GetHashCode() {
     306        var h = 31415;
     307        h ^= symbol.GetHashCode();
     308        if (Expr != null) h ^= Expr.GetHashCode();
    251309        return h;
    252310      }
     
    258316    internal class Expr : IComparable<Expr> {
    259317      public readonly SortedSet<Term> Terms; // only set for Kind == Expr
    260       public bool Inverse;
     318      //public bool Inverse;
    261319      public Expr(Term t) {
    262320        Terms = new SortedSet<Term>();
     
    286344
    287345      public override string ToString() {
    288         if (Inverse && Terms.Count > 1)
    289           return "(" + string.Join("+", Terms) + ")";
    290         else if (Inverse && Terms.Count == 1) {
    291           return Terms.First().ToString().Replace("%", "#").Replace("*", "%").Replace("#", "*");
    292         } else
    293           return string.Join("+", Terms);
     346        // if (Inverse && Terms.Count > 1)
     347        //   return "(" + string.Join("+", Terms) + ")";
     348        // else if (Inverse && Terms.Count == 1) {
     349        //   return Terms.First().ToString().Replace("%", "#").Replace("*", "%").Replace("#", "*");
     350        // } else
     351        return string.Join("+", Terms);
    294352      }
    295353      public override bool Equals(object obj) {
    296354        var other = obj as Expr;
    297355        if (other == null) return false;
    298         if (this.Inverse != other.Inverse) return false;
     356        //if (this.Inverse != other.Inverse) return false;
    299357        if (this.Terms.Count() != other.Terms.Count()) return false;
    300358        return this.Terms.Intersect(other.Terms).Count() == this.Terms.Count;
     
    308366        return h;
    309367      }
    310 
    311       public void Invert() {
    312         this.Inverse = !Inverse;
    313       }
    314368    }
    315369    #endregion
     
    317371      return symb >= 'A' && symb <= 'Z';
    318372    }
    319     internal static bool ContainsNonTerminal(IEnumerable<char> symbs) {
    320       return symbs.Any(IsNonTerminal);
    321     }
     373    internal static bool ContainsNonTerminal(IEnumerable<Factor> factors) {
     374      return factors.Any(ContainsNonTerminal);
     375    }
     376    internal static bool ContainsNonTerminal(Factor f) {
     377      if (f.Expr == null) return IsNonTerminal(f.Symbol);
     378      else return ContainsNonTerminal(f.Expr);
     379    }
     380
     381    private static bool ContainsNonTerminal(Expr expr) {
     382      return expr.Terms.Any(ContainsNonTerminal);
     383    }
     384
     385    private static bool ContainsNonTerminal(Term term) {
     386      return ContainsNonTerminal(term.Factors);
     387    }
     388
     389
     390    /*
    322391
    323392    internal class FactorComparer : IComparer<char> {
     
    333402        }
    334403      }
    335     }
     404    }*/
    336405  }
    337406}
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization.SymbReg/ExpressionCompiler.cs

    r11895 r11972  
    2626    // for boolean problems the symbol * representes the AND operator, the symbol + represents the OR operator, ! = NOT, ^ = XOR
    2727
    28     public void Compile(string sentence, out Term f, out Variable[] variables, out Variable[] constants) {
     28    public void Compile(string sentence, out Term f, Variable[] variables, out Variable[] constants) {
    2929      InitLex(sentence);
    30       var variablesList = new List<Variable>();
    3130      var constantsList = new List<Variable>();
    32       f = Expr(variablesList, constantsList);
    33       variables = variablesList.ToArray();
     31      f = Expr(variables, constantsList);
    3432      constants = constantsList.ToArray();
    3533    }
     
    132130        NewSy();
    133131      } else if (curSy >= 'a' && curSy <= 'z') {
    134         var newVar = new Variable();
    135         variables.Add(newVar);
    136         r = newVar;
     132        var varIdx = (byte)curSy - (byte)'a';
     133        if (varIdx >= variables.Count) throw new ArgumentException();
     134        r = variables[varIdx];
    137135        NewSy();
    138136      } else if (curSy >= '0' && curSy <= '9') {
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization.SymbReg/SymbolicRegressionProblem.cs

    r11902 r11972  
    7070
    7171    public SymbolicRegressionProblem(Random random, string partOfName, bool useConstantOpt = true) {
    72       var instanceProvider = new RegressionRealWorldInstanceProvider();
    73       var dds = instanceProvider.GetDataDescriptors().OfType<RegressionDataDescriptor>();
     72      var instanceProviders = new RegressionInstanceProvider[]
     73      {new RegressionRealWorldInstanceProvider(),
     74        new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(),
     75        new KeijzerInstanceProvider(),
     76        new VladislavlevaInstanceProvider(),
     77        new NguyenInstanceProvider(),
     78        new KornsInstanceProvider(),
     79    };
     80      var instanceProvider = instanceProviders.FirstOrDefault(prov => prov.GetDataDescriptors().Any(dd => dd.Name.Contains(partOfName)));
     81      if (instanceProvider == null) throw new ArgumentException("instance not found");
     82
    7483      this.useConstantOpt = useConstantOpt;
    7584
     85      var dds = instanceProvider.GetDataDescriptors();
    7686      var problemData = instanceProvider.LoadData(dds.Single(ds => ds.Name.Contains(partOfName)));
    7787
     
    133143
    134144    public string CanonicalRepresentation(string phrase) {
    135       throw new NotImplementedException();
     145      return phrase;
    136146    }
    137147
     
    141151      // for each expression split into terms
    142152      // for each term order factors to canonical // ../E = * 1/E
    143       var canonical = CanonicalRepresentation(phrase);
    144       throw new NotImplementedException();
     153      return new Feature[] { new Feature(phrase, 1.0) };
    145154    }
    146155
     
    150159      AutoDiff.Term func;
    151160      int pos = 0;
     161      int n = x.GetLength(0);
     162      int m = x.GetLength(1);
    152163
    153164      var compiler = new ExpressionCompiler();
    154       Variable[] variables;
     165      Variable[] variables = Enumerable.Range(0, m).Select(_ => new Variable()).ToArray();
    155166      Variable[] constants;
    156       compiler.Compile(sentence, out func, out variables, out constants);
     167      compiler.Compile(sentence, out func, variables, out constants);
    157168
    158169      // constants are manipulated
     
    168179      int info;
    169180
    170       int n = x.GetLength(0);
    171       int m = x.GetLength(1);
     181
    172182      int k = c.Length;
    173183
     
    222232      return sb.ToString();
    223233    }
     234
    224235
    225236    private void TreeToSentence(ISymbolicExpressionTreeNode treeNode, StringBuilder sb) {
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization.csproj

    r11865 r11972  
    7979    <Compile Include="Interfaces\IProblem.cs" />
    8080    <Compile Include="Interfaces\ISymbolicExpressionTreeProblem.cs" />
     81    <Compile Include="Problems\PermutationProblem.cs" />
    8182    <Compile Include="Problems\EvenParityProblem.cs" />
    8283    <Compile Include="Problems\FindPhrasesProblem.cs" />
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalTreeProblem.cs

    r11865 r11972  
    8787      //optimalQualityForLevel[1] = 4;
    8888
    89       for (int i = 1; i < maxSizeForLevel.Length; i++) {
     89      for (int i = 1; i < levels; i++) {
    9090        maxSizeForLevel[i] = i * maxSizeForLevel[i - 1] + 1; // e.g. lvl-2: baxax
    9191
     
    110110      // search for corresponding level
    111111      int level = 0;
    112       while (maxSizeForLevel[level + 1] < maxLen) level++;
     112      while (level+1 < maxSizeForLevel.Length && maxSizeForLevel[level + 1] < maxLen) level++;
    113113      // ASSERT: maxSizeForLevel[level + 1] >= maxLen
    114114      return optimalQualityForLevel[level];
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Main/Program.cs

    r11966 r11972  
    2727
    2828      //RunDemo();
    29       // RunGpDemo();
    30       RunGridTest();
    31       //RunGpGridTest();
     29      //RunGpDemo();
     30      //RunGridTest();
     31      RunGpGridTest();
    3232    }
    3333
     
    229229      // - ant: UCB1Tuned with canonical states also works very well for the artificial ant! constistent solutions in less than 10k iters     
    230230
    231       var problem = new SymbolicRegressionPoly10Problem();
     231      //var problem = new SymbolicRegressionPoly10Problem();
    232232      //var problem = new SantaFeAntProblem();
    233       //var problem = new SymbolicRegressionProblem(random, "Tower");
     233      var problem = new SymbolicRegressionProblem(random, "Breiman");
    234234      //var problem = new PalindromeProblem();
    235235      //var problem = new HardPalindromeProblem();
     
    244244      //var alg = new SequentialSearch(problem, 23, random, 0,
    245245      //  new HeuristicLab.Algorithms.Bandits.GrammarPolicies.GenericContextualGrammarPolicy(problem, new GenericThompsonSamplingPolicy(new GaussianModel(0.5, 10, 1, 1)), true));
    246       var alg = new SequentialSearch(problem, 23, random, 0,
     246      var alg = new SequentialSearch(problem, 30, random, 0,
    247247        new HeuristicLab.Algorithms.Bandits.GrammarPolicies.GenericFunctionApproximationGrammarPolicy(problem, true));
    248248      //var alg = new MctsQLearningSampler(problem, sentenceLen, random, 0, null);
     
    265265        globalStatistics.AddSentence(sentence, quality);
    266266
    267         if (iterations % 1000 == 0) {
    268           if (iterations % 10000 == 0) Console.Clear();
    269           Console.SetCursorPosition(0, 0);
    270           alg.PrintStats();
     267        //if (iterations % 100 == 0) {
     268        //  if (iterations % 10000 == 0) Console.Clear();
     269        //  Console.SetCursorPosition(0, 0);
     270        //  alg.PrintStats();
     271        //}
     272
     273        //Console.WriteLine(sentence);
     274
     275        if (iterations % 100 == 0) {
     276          Console.WriteLine("{0}", globalStatistics);
    271277        }
    272 
    273         //Console.WriteLine(sentence);
    274 
    275         //if (iterations % 10000 == 0) {
    276         //  Console.WriteLine("{0}", globalStatistics);
    277         //}
    278278      };
    279279
     
    309309      const int nReps = 20;
    310310      const int seed = 31415;
    311       const int maxIters = 200000;
     311      //const int maxIters = 50000;
    312312      var rand = new Random(seed);
    313       var problemFactories = new Func<ISymbolicExpressionTreeProblem>[]
     313      var problemFactories = new Func<Tuple<int, int, ISymbolicExpressionTreeProblem>>[]
    314314      {
    315         () => new SymbolicRegressionPoly10Problem(),
    316         () => new SantaFeAntProblem(),
    317       };
    318       foreach (var popSize in new int[] { 50, 100, 250, 500, 1000, 2500, 5000, 10000 }) {
     315        () => Tuple.Create(50000, 32, (ISymbolicExpressionTreeProblem)new PermutationProblem()),
     316        () => Tuple.Create(50000, 32, (ISymbolicExpressionTreeProblem)new RoyalPairProblem()),
     317        () => Tuple.Create(50000, 32,(ISymbolicExpressionTreeProblem)new RoyalSymbolProblem()),
     318        () => Tuple.Create(50000, 64, (ISymbolicExpressionTreeProblem)new RoyalPairProblem()),
     319        () => Tuple.Create(50000, 64,(ISymbolicExpressionTreeProblem)new RoyalSymbolProblem()),
     320        () => Tuple.Create(50000, 128, (ISymbolicExpressionTreeProblem)new RoyalPairProblem()),
     321        () => Tuple.Create(50000, 128,(ISymbolicExpressionTreeProblem)new RoyalSymbolProblem()),
     322        () => Tuple.Create(50000, 256, (ISymbolicExpressionTreeProblem)new RoyalPairProblem()),
     323        () => Tuple.Create(50000, 256,(ISymbolicExpressionTreeProblem)new RoyalSymbolProblem()),
     324        //() => new RoyalPairProblem(),
     325        //() => new FindPhrasesProblem(rand, 20, 5, 3, 5, 0, 1, 0, true),
     326        //() => new FindPhrasesProblem(rand, 20, 5, 3, 5, 0, 1, 0, false),
     327        //() => new FindPhrasesProblem(rand, 20, 5, 3, 5, 50, 1, 0.8, false),
     328      };
     329
     330      foreach (var popSize in new int[] { 100 /*, 250, 500, 1000, 2500, 5000, 10000 */ }) {
    319331        foreach (var mutationRate in new double[] { /* 0.05, /* 0.10, */ 0.15, /* 0.25, 0.3 */}) {
    320           foreach (var maxSize in new int[] { 30, 50, 100, 150, 250 }) {
    321             // skip experiments that are already done
    322             if (popSize == 10000 || maxSize == 150 || maxSize == 250) {
    323               foreach (var problemFactory in problemFactories)
    324                 for (int i = 0; i < nReps; i++) {
    325                   var solverSeed = rand.Next();
    326                   {
    327                     var prob = problemFactory();
    328                     var sgp = new StandardGP(prob, new Random(solverSeed));
    329                     RunGP(sgp, prob, maxIters, popSize, mutationRate, maxSize);
    330                   }
    331                   // {
    332                   //   var prob = problemFactory();
    333                   //   var osgp = new OffspringSelectionGP(prob, new Random(solverSeed));
    334                   //   RunGP(osgp, prob, maxIters, popSize, mutationRate, maxSize);
    335                   // }
    336                 }
     332          // skip experiments that are already done
     333          foreach (var problemFactory in problemFactories) {
     334            for (int i = 0; i < nReps; i++) {
     335              {
     336                var solverSeed = rand.Next();
     337                var tupel = problemFactory();
     338                var maxIters = tupel.Item1;
     339                var maxSize = tupel.Item2;
     340                var prob = tupel.Item3;
     341                var sgp = new StandardGP(prob, new Random(solverSeed));
     342                RunGP(sgp, prob, maxIters, popSize, mutationRate, maxSize);
     343              }
     344              //{
     345              //  var prob = problemFactory();
     346              //  var osgp = new OffspringSelectionGP(prob, new Random(solverSeed));
     347              //  RunGP(osgp, prob, maxIters, popSize, mutationRate, maxSize);
     348              //}
    337349            }
    338350          }
    339351        }
     352
    340353      }
    341354    }
     
    350363        globalStatistics.AddSentence(sentence, quality);
    351364
    352         if (iterations % 1000 == 0) {
     365        if (iterations % 100 == 0) {
    353366          Console.WriteLine("\"{0,25}\" {1} {2:N2} {3} \"{4,25}\" {5}", gpName, popSize, mutationRate, maxSize, probName, globalStatistics);
    354367        }
  • branches/HeuristicLab.Problems.GrammaticalOptimization/Test/TestCanonicalExpressions.cs

    r11966 r11972  
    6868    public void TestDivisionExpansion() {
    6969      var extender = new ExpressionExtender();
    70       Assert.AreEqual("a%c+b%c", extender.CanonicalRepresentation("(b+a)%c"));
    71       Assert.AreEqual("a%c%d+b%c%d", extender.CanonicalRepresentation("(b+a)%(d*c)"));
    72       Assert.AreEqual("a%(c+d)+b%(c+d)", extender.CanonicalRepresentation("(b-a)%(d-c)"));
    73       Assert.AreEqual("a*b%(c+d)", extender.CanonicalRepresentation("(b*a)%(d-c)"));
     70      Assert.AreEqual("a*1/c+b*1/c", extender.CanonicalRepresentation("(b+a)%c"));
     71      Assert.AreEqual("a*1/c*1/d+b*1/c*1/d", extender.CanonicalRepresentation("(b+a)%(d*c)"));
     72      Assert.AreEqual("a*1/(c+d)+b*1/(c+d)", extender.CanonicalRepresentation("(b-a)%(d-c)"));
     73      Assert.AreEqual("a*b*1/(c+d)", extender.CanonicalRepresentation("(b*a)%(d-c)"));
    7474
    75       Assert.AreEqual("a*b%(a+b)%(c+d)", extender.CanonicalRepresentation("(b*a)%(d-c)%(a+b)"));
    76       Assert.AreEqual("a*b%(a%e+b%e)%(c+d)", extender.CanonicalRepresentation("((b*a)%(d-c))%((a+b)%e)"));
     75      Assert.AreEqual("a*b*1/(a+b)*1/(c+d)", extender.CanonicalRepresentation("(b*a)%(d-c)%(a+b)"));
     76      Assert.AreEqual("a*b*1/(a*1/e+b*1/e)*1/(c+d)", extender.CanonicalRepresentation("((b*a)%(d-c))%((a+b)%e)"));
    7777      // a*b*e%(c+d)%(a+b)
    7878    }
     
    8282      Assert.AreEqual("1", extender.CanonicalRepresentation("a%a"));
    8383      Assert.AreEqual("a", extender.CanonicalRepresentation("a*a%a"));
    84       Assert.AreEqual("1%a", extender.CanonicalRepresentation("(a%a)%a"));
    85       Assert.AreEqual("1%a", extender.CanonicalRepresentation("a%a%a"));
     84      Assert.AreEqual("1/a", extender.CanonicalRepresentation("(a%a)%a"));
     85      Assert.AreEqual("1/a", extender.CanonicalRepresentation("a%a%a"));
    8686      Assert.AreEqual("a", extender.CanonicalRepresentation("a%(a%a)"));
    8787      Assert.AreEqual("1", extender.CanonicalRepresentation("(a+b)%(b+a)"));
    88       Assert.AreEqual("1%a+1%b", extender.CanonicalRepresentation("(a+b)%(a*b)"));
     88      Assert.AreEqual("1/a+1/b", extender.CanonicalRepresentation("(a+b)%(a*b)"));
     89      Assert.AreEqual("a*1/(a*c*1/b+e*1/d*1/f)+b*1/(a*c*1/b+e*1/d*1/f)", extender.CanonicalRepresentation("(a+b)%(a%b*c+e%f%d)"));
    8990    }
    9091  }
Note: See TracChangeset for help on using the changeset viewer.