[11902] | 1 | using System;
|
---|
[11966] | 2 | using System.CodeDom;
|
---|
[11902] | 3 | using System.Collections.Generic;
|
---|
| 4 | using System.Diagnostics;
|
---|
| 5 | using System.Linq;
|
---|
[11966] | 6 | using System.Security.Cryptography.X509Certificates;
|
---|
[11902] | 7 | using System.Text;
|
---|
| 8 | using System.Threading.Tasks;
|
---|
| 9 | using HeuristicLab.Common;
|
---|
| 10 |
|
---|
| 11 | namespace HeuristicLab.Algorithms.Bandits {
|
---|
| 12 | // helper to create canonical forms of expressions
|
---|
[11981] | 13 | // NOTE: Not implemented yet (see unit test for divisions)
|
---|
[11902] | 14 | // TODO: change symbolicregressionpoly10problem to use this class
|
---|
| 15 | // this does not support expressions with constants (in transformations we assume constant opt is used)
|
---|
| 16 | // (e.g. we transform all negative signs to + because it is assumed a negative constant can be produced for the term)
|
---|
| 17 | public class ExpressionExtender {
|
---|
| 18 |
|
---|
| 19 | // supports the grammar
|
---|
| 20 | // G(E):
|
---|
| 21 | // E -> V | V+E | V-E | V*E | V%E | (E)
|
---|
| 22 | // V -> <variables>
|
---|
| 23 | // ";
|
---|
| 24 |
|
---|
| 25 | // transformations:
|
---|
| 26 | // - transform c*b*a to a*b*c (lexicographic ordering for factors, cached)
|
---|
| 27 | // - transfrom - to + (assumed optimized constants for terms)
|
---|
| 28 |
|
---|
[11966] | 29 | private string sentence;
|
---|
| 30 | private int syIdx;
|
---|
[11902] | 31 |
|
---|
| 32 | public string CanonicalRepresentation(string phrase) {
|
---|
[11981] | 33 | throw new NotImplementedException();
|
---|
[11966] | 34 | InitLex(phrase);
|
---|
| 35 | var e = CanonicalExpr();
|
---|
| 36 | return e.ToString();
|
---|
[11902] | 37 | }
|
---|
| 38 |
|
---|
[11966] | 39 |
|
---|
| 40 | private void InitLex(string sentence) {
|
---|
| 41 | this.sentence = sentence;
|
---|
| 42 | this.syIdx = 0;
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | private char CurSy() {
|
---|
| 46 | if (syIdx >= sentence.Length) return '\0';
|
---|
| 47 | return sentence[syIdx];
|
---|
| 48 | }
|
---|
| 49 | private void NewSy() {
|
---|
| 50 | if (syIdx < sentence.Length) syIdx++;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[11972] | 53 | // an expression is a sorted set of terms
|
---|
[11966] | 54 | // a term is an (ordered) list of factors
|
---|
[11972] | 55 | // a factor is either a single symbol or an inverted expression
|
---|
[11966] | 56 | // CanonicalExpression reads multiple terms (expressions) and must merge the terms in the expression (ordering and duplicates)
|
---|
| 57 | // CanonicalTerm reads multiple factors and must expand factors whenever it reads a combined factor (expression) it produces an expression
|
---|
| 58 | // CanonicalFactor produces an expression
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | // canonical expression returns either a single term or a set of terms
|
---|
| 62 | private Expr CanonicalExpr() {
|
---|
| 63 | var terms = new List<Expr>();
|
---|
| 64 | terms.Add(CanonicalTerm());
|
---|
| 65 | var curSy = CurSy();
|
---|
[11902] | 66 | while (curSy == '+' || curSy == '-' || curSy == '^') {
|
---|
| 67 | if (curSy == '+') {
|
---|
[11966] | 68 | NewSy();
|
---|
| 69 | terms.Add(CanonicalTerm());
|
---|
[11902] | 70 | } else if (curSy == '-') {
|
---|
[11966] | 71 | NewSy();
|
---|
| 72 | terms.Add(CanonicalTerm()); // minus is the same as plus assuming constant opt
|
---|
[11902] | 73 | } else {
|
---|
[11966] | 74 | NewSy();
|
---|
[11902] | 75 | throw new NotImplementedException();
|
---|
| 76 | // var e = Expr(variables, constants);
|
---|
| 77 | // r = Not(r) * e + r * Not(e); // xor = (!x AND y) OR (x AND !y)
|
---|
| 78 | }
|
---|
[11966] | 79 | curSy = CurSy();
|
---|
[11902] | 80 | }
|
---|
| 81 |
|
---|
[11966] | 82 | return new Expr(terms.SelectMany(t => t.Terms));
|
---|
[11902] | 83 | }
|
---|
| 84 |
|
---|
[11966] | 85 | // canonical term returns either a single term (product of factors) or a set of terms
|
---|
| 86 | private Expr CanonicalTerm() {
|
---|
[11972] | 87 | var factors = new List<Factor>();
|
---|
[11966] | 88 | var f = CanonicalFact();
|
---|
[11902] | 89 | if (f != null) factors.Add(f);
|
---|
[11966] | 90 | var curSy = CurSy();
|
---|
[11902] | 91 | while (curSy == '*' || curSy == '%') {
|
---|
| 92 | if (curSy == '*') {
|
---|
[11966] | 93 | NewSy();
|
---|
| 94 | f = CanonicalFact();
|
---|
[11902] | 95 | if (f != null) factors.Add(f);
|
---|
| 96 | } else {
|
---|
[11966] | 97 | NewSy();
|
---|
| 98 | f = CanonicalFact();
|
---|
| 99 | f.Invert();
|
---|
[11972] | 100 | if (f != null) factors.Add(f);
|
---|
[11902] | 101 | }
|
---|
[11966] | 102 | curSy = CurSy();
|
---|
[11902] | 103 | }
|
---|
[11972] | 104 | return ExpandFactors(factors);
|
---|
| 105 | }
|
---|
[11902] | 106 |
|
---|
[11972] | 107 | /*
|
---|
| 108 | private List<Factor> CancelFactors(Term t) {
|
---|
| 109 | var nonInvF = t.Factors.Where(f => !f.IsInverse).ToArray();
|
---|
| 110 | var invF = t.Factors.Where(f => f.IsInverse).ToArray();
|
---|
[11902] | 111 |
|
---|
[11972] | 112 | var result = new List<Factor>();
|
---|
| 113 | foreach (var f in nonInvF) {
|
---|
| 114 | if (!invF.Contains(f)) {
|
---|
| 115 | result.Add(f);
|
---|
| 116 | }
|
---|
[11966] | 117 | }
|
---|
[11972] | 118 | if (result.Count == 0) result.Add(new Factor('1'));
|
---|
| 119 | return result;
|
---|
[11902] | 120 | }
|
---|
[11972] | 121 | */
|
---|
[11966] | 122 | // canonical fact returns a factor (either a singe variable, or a set of terms)
|
---|
[11972] | 123 | private Factor CanonicalFact() {
|
---|
[11966] | 124 | var curSy = CurSy();
|
---|
[11902] | 125 | if (curSy == '!') {
|
---|
| 126 | throw new NotSupportedException();
|
---|
| 127 | } else if (curSy == '(') {
|
---|
[11966] | 128 | NewSy();
|
---|
[11972] | 129 | Expr r = CanonicalExpr(); // this is already simplified
|
---|
[11966] | 130 | if (CurSy() != ')') throw new ArgumentException();
|
---|
| 131 | NewSy();
|
---|
[11972] | 132 | return new Factor(r);
|
---|
[11902] | 133 | } else if (curSy >= 'a' && curSy <= 'z') {
|
---|
[11966] | 134 | NewSy();
|
---|
[11972] | 135 | return new Factor(curSy);
|
---|
[11902] | 136 | // } else if (curSy >= '0' && curSy <= '9') {
|
---|
[11966] | 137 | } else if (curSy >= 'A' && curSy <= 'Z') {
|
---|
| 138 | // treat nonterminals in the same way as variables
|
---|
| 139 | NewSy();
|
---|
[11972] | 140 | return new Factor(curSy);
|
---|
[11966] | 141 |
|
---|
[11902] | 142 | } else throw new ArgumentException("found symbol " + curSy);
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[11972] | 145 | // a list of factors (symbols, or expressions, and possibly inverses are read
|
---|
| 146 | // a lis to factors symbols or expressions and possibly inverses are produced
|
---|
| 147 | // all non-inverse expression factors are expanded
|
---|
| 148 | private Expr ExpandFactors(IEnumerable<Factor> factors) {
|
---|
| 149 | // if (invFactors.Count > 0) throw new NotImplementedException();
|
---|
| 150 | Debug.Assert(!factors.First().IsInverse); // the first factor is never an inverted factor
|
---|
| 151 |
|
---|
| 152 | // each factor could be a list of terms (expression)
|
---|
| 153 |
|
---|
| 154 | Expr currentFact = null;
|
---|
| 155 | var firstFactor = factors.First();
|
---|
| 156 | if (firstFactor.IsSimpleFactor) currentFact = new Expr(new Term(firstFactor));
|
---|
| 157 | else currentFact = firstFactor.Expr;
|
---|
| 158 |
|
---|
| 159 | foreach (var fact in factors.Skip(1)) {
|
---|
| 160 | Expr curExpr = null;
|
---|
| 161 | if (fact.IsSimpleFactor || fact.IsInverse) curExpr = new Expr(new Term(fact));
|
---|
| 162 | else curExpr = fact.Expr;
|
---|
| 163 | currentFact = AllProducts(currentFact, curExpr);
|
---|
| 164 | }
|
---|
| 165 | return currentFact;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | private Expr AllProducts(Expr a, Expr b) {
|
---|
| 169 | var aTerms = a.Terms.ToArray();
|
---|
| 170 | var bTerms = b.Terms.ToArray();
|
---|
| 171 | var combs = from aT in aTerms
|
---|
| 172 | from bT in bTerms
|
---|
| 173 | let factors = CancelFactors(aT.Factors.Concat(bT.Factors))
|
---|
| 174 | select new Term(factors);
|
---|
| 175 | return new Expr(combs);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | private IEnumerable<Factor> CancelFactors(IEnumerable<Factor> factors) {
|
---|
| 179 | var factorsArr = factors.ToArray();
|
---|
| 180 | var results = new List<Factor>(factors);
|
---|
| 181 | foreach (var f in factorsArr) {
|
---|
| 182 | if (f.ToString() == "1") results.Remove(f);
|
---|
| 183 | if (f.IsInverse) {
|
---|
| 184 | // find matching
|
---|
| 185 | Factor match;
|
---|
| 186 | if (f.IsSimpleFactor) match = factorsArr.FirstOrDefault(other => !other.IsInverse && f.Symbol.Equals(other.Symbol));
|
---|
| 187 | else match = factorsArr.FirstOrDefault(other => !other.IsInverse && f.Expr.Equals(other.Expr));
|
---|
| 188 | if (match != null) {
|
---|
| 189 | results.Remove(match);
|
---|
| 190 | results.Remove(f);
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
| 194 | if (results.Count == 0) results.Add(new Factor('1'));
|
---|
| 195 | return results;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
[11966] | 198 | #region term
|
---|
| 199 | // term can be merged (essentially an ordered list of factors)
|
---|
| 200 | internal class Term : IComparable<Term> {
|
---|
[11972] | 201 | private readonly SortedList<Factor, int> factors; // factor symbol and the number of occurrences
|
---|
[11902] | 202 |
|
---|
[11972] | 203 | public IEnumerable<Factor> Factors {
|
---|
[11966] | 204 | get {
|
---|
[11972] | 205 | return factors.SelectMany(p => Enumerable.Repeat(p.Key, p.Value));
|
---|
[11966] | 206 | }
|
---|
[11902] | 207 | }
|
---|
[11966] | 208 |
|
---|
[11972] | 209 | public Term(Factor f) {
|
---|
| 210 | factors = new SortedList<Factor, int>();
|
---|
| 211 | factors.Add(f, 1);
|
---|
[11902] | 212 | }
|
---|
[11972] | 213 | public Term(IEnumerable<Factor> factors) {
|
---|
| 214 | this.factors = new SortedList<Factor, int>();
|
---|
[11966] | 215 | foreach (var f in factors) {
|
---|
[11972] | 216 | if (this.factors.ContainsKey(f)) this.factors[f] += 1;
|
---|
| 217 | else this.factors.Add(f, 1);
|
---|
[11902] | 218 | }
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[11966] | 221 | public int CompareTo(Term other) {
|
---|
[11972] | 222 | if (ContainsNonTerminal(Factors) && !ContainsNonTerminal(other.Factors)) {
|
---|
[11966] | 223 | return 1;
|
---|
[11972] | 224 | } else if (!ContainsNonTerminal(Factors) && ContainsNonTerminal(other.Factors)) {
|
---|
[11966] | 225 | return -1;
|
---|
| 226 | } else {
|
---|
[11972] | 227 | var countComp = Factors.Count().CompareTo(other.Factors.Count());
|
---|
[11966] | 228 | if (countComp != 0) return countComp;
|
---|
[11972] | 229 | return string.Join("", Factors).CompareTo(string.Join("", other.Factors));
|
---|
[11966] | 230 | }
|
---|
[11902] | 231 | }
|
---|
| 232 |
|
---|
| 233 | public override string ToString() {
|
---|
[11972] | 234 |
|
---|
| 235 | return string.Join("*", Factors);
|
---|
[11902] | 236 | }
|
---|
| 237 | public override bool Equals(object obj) {
|
---|
| 238 | var other = obj as Term;
|
---|
| 239 | if (other == null) return false;
|
---|
[11972] | 240 | if (this.Factors.Count() != other.Factors.Count()) return false;
|
---|
| 241 | if (this.Factors.Zip(other.Factors, Tuple.Create).Any(t => t.Item1 != t.Item2)) return false;
|
---|
[11966] | 242 | return true;
|
---|
[11902] | 243 | }
|
---|
| 244 | public override int GetHashCode() {
|
---|
| 245 | var h = 31415;
|
---|
[11972] | 246 | foreach (var v in Factors) {
|
---|
[11966] | 247 | h ^= v.GetHashCode();
|
---|
[11902] | 248 | }
|
---|
| 249 | return h;
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 | #endregion
|
---|
[11966] | 253 |
|
---|
[11972] | 254 | #region factor
|
---|
| 255 | // factors is either a single symbol or an inverted expression
|
---|
| 256 | internal class Factor : IComparable<Factor> {
|
---|
| 257 | public bool IsSimpleFactor { get { return Expr == null; } }
|
---|
| 258 | public char Symbol { get { return symbol; } }
|
---|
| 259 | public Expr Expr { get { return expr; } }
|
---|
| 260 | public bool IsInverse { get { return inv; } }
|
---|
| 261 | private readonly char symbol = '\0';
|
---|
| 262 | private readonly Expr expr;
|
---|
| 263 | private bool inv;
|
---|
| 264 |
|
---|
| 265 | public Factor(char f) {
|
---|
| 266 | this.symbol = f;
|
---|
| 267 | }
|
---|
| 268 | public Factor(Expr expr) {
|
---|
| 269 | this.expr = expr;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
| 272 | public void Invert() {
|
---|
| 273 | this.inv = !inv;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | public int CompareTo(Factor other) {
|
---|
| 277 | // 1) single symbol factors first
|
---|
| 278 | // 2) expression factors by expression compare
|
---|
| 279 | var crit1 = ContainsNonTerminal(this).CompareTo(ContainsNonTerminal(other));
|
---|
| 280 | if (crit1 != 0) return crit1;
|
---|
| 281 |
|
---|
| 282 | var crit2 = this.IsInverse.CompareTo(other.IsInverse);
|
---|
| 283 | if (crit2 != 0) return crit2;
|
---|
| 284 |
|
---|
| 285 | var crit3 = this.IsSimpleFactor.CompareTo(other.IsSimpleFactor);
|
---|
| 286 | if (crit3 != 0) return crit3;
|
---|
| 287 |
|
---|
| 288 | // both are simple or expressions
|
---|
| 289 | if (IsSimpleFactor) return this.symbol.CompareTo(other.symbol);
|
---|
| 290 | else return this.Expr.CompareTo(other.Expr);
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | public override string ToString() {
|
---|
| 294 | var s = Expr == null ? symbol.ToString() : "(" + expr.ToString() + ")";
|
---|
| 295 | if (IsInverse) {
|
---|
| 296 | return "1/" + s;
|
---|
| 297 | } else return s;
|
---|
| 298 | }
|
---|
| 299 | public override bool Equals(object obj) {
|
---|
| 300 | var other = obj as Factor;
|
---|
| 301 | if (other == null) return false;
|
---|
| 302 | if (IsInverse != other.IsInverse) return false;
|
---|
| 303 | if (this.symbol != other.symbol) return false;
|
---|
| 304 | if (this.Expr != other.Expr) return false;
|
---|
| 305 | return true;
|
---|
| 306 | }
|
---|
| 307 | public override int GetHashCode() {
|
---|
| 308 | var h = 31415;
|
---|
| 309 | h ^= symbol.GetHashCode();
|
---|
| 310 | if (Expr != null) h ^= Expr.GetHashCode();
|
---|
| 311 | return h;
|
---|
| 312 | }
|
---|
| 313 | }
|
---|
| 314 | #endregion
|
---|
| 315 |
|
---|
[11902] | 316 | #region expr
|
---|
| 317 |
|
---|
[11966] | 318 | internal class Expr : IComparable<Expr> {
|
---|
| 319 | public readonly SortedSet<Term> Terms; // only set for Kind == Expr
|
---|
[11972] | 320 | //public bool Inverse;
|
---|
[11966] | 321 | public Expr(Term t) {
|
---|
| 322 | Terms = new SortedSet<Term>();
|
---|
| 323 | Terms.Add(t);
|
---|
[11902] | 324 | }
|
---|
[11966] | 325 | public Expr(IEnumerable<Term> exprTerms) {
|
---|
| 326 | Terms = new SortedSet<Term>();
|
---|
| 327 | foreach (var t in exprTerms) {
|
---|
| 328 | Terms.Add(t);
|
---|
| 329 | }
|
---|
[11902] | 330 | }
|
---|
| 331 |
|
---|
[11966] | 332 | public void Merge(Expr other) {
|
---|
| 333 | this.Terms.UnionWith(other.Terms);
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | public int CompareTo(Expr other) {
|
---|
| 337 | var sizeComp = this.Terms.Count.CompareTo(other.Terms.Count);
|
---|
| 338 | if (sizeComp != 0) return sizeComp;
|
---|
| 339 | // same size => compare terms
|
---|
| 340 | foreach (var pair in Terms.Zip(other.Terms, Tuple.Create)) {
|
---|
| 341 | var termComp = pair.Item1.CompareTo(pair.Item2);
|
---|
| 342 | if (termComp != 0) return termComp;
|
---|
| 343 | }
|
---|
| 344 | return 0;
|
---|
| 345 | }
|
---|
| 346 |
|
---|
[11902] | 347 | public override string ToString() {
|
---|
[11972] | 348 | // if (Inverse && Terms.Count > 1)
|
---|
| 349 | // return "(" + string.Join("+", Terms) + ")";
|
---|
| 350 | // else if (Inverse && Terms.Count == 1) {
|
---|
| 351 | // return Terms.First().ToString().Replace("%", "#").Replace("*", "%").Replace("#", "*");
|
---|
| 352 | // } else
|
---|
| 353 | return string.Join("+", Terms);
|
---|
[11902] | 354 | }
|
---|
| 355 | public override bool Equals(object obj) {
|
---|
| 356 | var other = obj as Expr;
|
---|
| 357 | if (other == null) return false;
|
---|
[11972] | 358 | //if (this.Inverse != other.Inverse) return false;
|
---|
[11902] | 359 | if (this.Terms.Count() != other.Terms.Count()) return false;
|
---|
[11966] | 360 | return this.Terms.Intersect(other.Terms).Count() == this.Terms.Count;
|
---|
[11902] | 361 | }
|
---|
| 362 | public override int GetHashCode() {
|
---|
| 363 | var h = 31415;
|
---|
[11966] | 364 | if (Terms != null)
|
---|
| 365 | foreach (var t in Terms) {
|
---|
| 366 | h ^= t.GetHashCode();
|
---|
| 367 | }
|
---|
[11902] | 368 | return h;
|
---|
| 369 | }
|
---|
| 370 | }
|
---|
| 371 | #endregion
|
---|
[11966] | 372 | internal static bool IsNonTerminal(char symb) {
|
---|
| 373 | return symb >= 'A' && symb <= 'Z';
|
---|
| 374 | }
|
---|
[11972] | 375 | internal static bool ContainsNonTerminal(IEnumerable<Factor> factors) {
|
---|
| 376 | return factors.Any(ContainsNonTerminal);
|
---|
[11966] | 377 | }
|
---|
[11972] | 378 | internal static bool ContainsNonTerminal(Factor f) {
|
---|
| 379 | if (f.Expr == null) return IsNonTerminal(f.Symbol);
|
---|
| 380 | else return ContainsNonTerminal(f.Expr);
|
---|
| 381 | }
|
---|
[11902] | 382 |
|
---|
[11972] | 383 | private static bool ContainsNonTerminal(Expr expr) {
|
---|
| 384 | return expr.Terms.Any(ContainsNonTerminal);
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | private static bool ContainsNonTerminal(Term term) {
|
---|
| 388 | return ContainsNonTerminal(term.Factors);
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 |
|
---|
| 392 | /*
|
---|
| 393 |
|
---|
[11966] | 394 | internal class FactorComparer : IComparer<char> {
|
---|
| 395 | public int Compare(char x, char y) {
|
---|
| 396 | if (IsNonTerminal(x) && !IsNonTerminal(y)) {
|
---|
| 397 | return 1;
|
---|
| 398 | } else if (!IsNonTerminal(x) && IsNonTerminal(y)) {
|
---|
| 399 | return -1;
|
---|
| 400 | } else if (IsNonTerminal(x) && IsNonTerminal(y)) {
|
---|
| 401 | return x.CompareTo(y);
|
---|
| 402 | } else {
|
---|
| 403 | return x.CompareTo(y);
|
---|
[11902] | 404 | }
|
---|
| 405 | }
|
---|
[11972] | 406 | }*/
|
---|
[11902] | 407 | }
|
---|
| 408 | }
|
---|