Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/17/15 16:03:49 (10 years ago)
Author:
gkronber
Message:

#2283: changed handling of inverse expressions in transformation of expressions to canonical form

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Common/ExpressionExtender.cs

    r12014 r12024  
    1111namespace HeuristicLab.Algorithms.Bandits {
    1212  // helper to create canonical forms of expressions
    13   // NOTE: Not implemented yet (see unit test for divisions)
    1413  // TODO: change symbolicregressionpoly10problem to use this class
    1514  // this does not support expressions with constants (in transformations we assume constant opt is used)
     
    1918    // supports the grammar
    2019    // G(E):
    21     //     E -> V | V+E | V-E | V*E | V%E | V/E | (E)
     20    //     E -> V | V+E | V-E | V*E | V%E | (E)
    2221    //     V -> <variables>
    2322    //     ";
    2423
    25     // might produce expressions of the form |/x
     24    // might produce expressions of the form /x (= 1/x)
    2625    // the pipe symbol | is used for the constant one (in comparison to other constants)
    2726    private string sentence;
     
    3130      InitLex(phrase);
    3231      var e = CanonicalExpr();
    33       return e.ToString(); 
     32      return e.ToString();
    3433    }
    3534
     
    9796          if (!f.IsSimpleFactor && f.Expr.Terms.Count == 1) {
    9897            foreach (var invF in f.Expr.Terms.First().Factors) {
    99               if (invF.ToString() == "|") continue;
     98              if (invF.ToString() == "1") continue;
    10099              invF.Invert();
    101100              factors.Add(invF);
     
    173172      var results = new List<Factor>(factors);
    174173      foreach (var f in factorsArr) {
    175         if (f.ToString() == "|") results.Remove(f);
    176         if (f.ToString() == "|/(|)") results.Remove(f);
     174        if (f.ToString() == "1") results.Remove(f);
     175        if (f.ToString() == "1/(1)") results.Remove(f);
    177176        if (f.IsInverse) {
    178177          // find matching
     
    185184            results.Remove(match);
    186185            if (!results.Any())
    187               results.Insert(idx, new Factor('|')); // when the factor is the last one then insert a one
     186              results.Insert(idx, new Factor('1')); // when the factor is the last one then insert a one
    188187
    189188            // also mark as cancelled in the factorsArr
    190189            idx = Array.IndexOf(factorsArr, match);
    191             factorsArr[idx] = new Factor('|');
     190            factorsArr[idx] = new Factor('1');
    192191          }
    193192        }
    194193      }
    195       // remove all unnecessary "1* factors"
    196 
    197       if (results.Count == 0) results.Add(new Factor('|'));
     194      if (results.Count == 0) results.Add(new Factor('1'));
    198195      return results;
    199196    }
     
    230227          var countComp = Factors.Count().CompareTo(other.Factors.Count());
    231228          if (countComp != 0) return countComp;
    232           return string.Join("", Factors).CompareTo(string.Join("", other.Factors));
     229          foreach (var pair in Factors.Zip(other.Factors, Tuple.Create)) {
     230            var fComp = pair.Item1.CompareTo(pair.Item2);
     231            if (fComp != 0) return fComp;
     232          }
     233          return 0;
    233234        }
    234235      }
     
    304305        var s = Expr == null ? symbol.ToString() : "(" + expr.ToString() + ")";
    305306        if (IsInverse) {
    306           return "|/" + s;
     307          return "/" + s;
    307308        } else return s;
    308309      }
     
    356357
    357358      public override string ToString() {
    358         // if (Inverse && Terms.Count > 1)
    359         //   return "(" + string.Join("+", Terms) + ")";
    360         // else if (Inverse && Terms.Count == 1) {
    361         //   return Terms.First().ToString().Replace("%", "#").Replace("*", "%").Replace("#", "*");
    362         // } else
    363359        return string.Join("+", Terms);
    364360      }
Note: See TracChangeset for help on using the changeset viewer.