[12291] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
[12298] | 4 | using System.Security.Policy;
|
---|
[12291] | 5 | using HeuristicLab.Common;
|
---|
| 6 |
|
---|
| 7 | namespace HeuristicLab.Problems.GrammaticalOptimization {
|
---|
| 8 | public class PartialExpressionInterpreter {
|
---|
| 9 | private static readonly double[] emptyErc = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
---|
| 10 | private string sentence;
|
---|
| 11 | private int syIdx;
|
---|
[12298] | 12 | private HashSet<double> intermediateValues = new HashSet<double>();
|
---|
[12291] | 13 | private Stack<double> stack = new Stack<double>();
|
---|
| 14 | // interprets sentences from L(G(Expr)):
|
---|
| 15 | // Expr -> Term { ('+' | '-' | '^' ) Term }
|
---|
| 16 | // Term -> Fact { ('*' | '%') Fact }
|
---|
| 17 | // Fact -> '!' Expr | '(' Expr ')' | Var | const
|
---|
| 18 | // Var -> 'a'..'z'
|
---|
| 19 | // const -> '0' .. '9'
|
---|
| 20 |
|
---|
| 21 | // uses protected division symbol %
|
---|
| 22 | // constants are Koza-style ephemeral random constants (ERC). for now we only allow up to 10 constants.
|
---|
| 23 | // The constant symbols '0' .. '9' are treated as ERC indices
|
---|
| 24 |
|
---|
[12298] | 25 | public IEnumerable<double> Interpret(string sentence, double[] vars) {
|
---|
[12291] | 26 | return Interpret(sentence, vars, emptyErc);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
[12298] | 29 | public IEnumerable<double> Interpret(string sentence, double[] vars, double[] erc) {
|
---|
[12291] | 30 | InitLex(sentence);
|
---|
[12298] | 31 | intermediateValues.Clear();
|
---|
| 32 | stack.Clear();
|
---|
[12291] | 33 | Expr(vars, erc);
|
---|
[12298] | 34 | return intermediateValues;
|
---|
[12291] | 35 | }
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | private void InitLex(string sentence) {
|
---|
| 39 | this.sentence = sentence;
|
---|
| 40 | this.syIdx = 0;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | private char CurSy() {
|
---|
| 44 | if (syIdx >= sentence.Length) return '\0';
|
---|
| 45 | return sentence[syIdx];
|
---|
| 46 | }
|
---|
| 47 | private void NewSy() {
|
---|
| 48 | if (syIdx < sentence.Length) syIdx++;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | // helper for xor
|
---|
| 52 | private double Not(double x) {
|
---|
| 53 | return DoubleExtensions.IsAlmost(x, 0) ? 1.0 : 0.0;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | private bool Expr(double[] d, double[] erc) {
|
---|
| 57 | if (!Term(d, erc)) return false;
|
---|
| 58 | var curSy = CurSy();
|
---|
| 59 | while (curSy == '+' || curSy == '-' || curSy == '^') {
|
---|
| 60 | if (curSy == '+') {
|
---|
| 61 | NewSy();
|
---|
[12298] | 62 | if (!Term(d, erc)) { return false; }
|
---|
[12291] | 63 | stack.Push(stack.Pop() + stack.Pop());
|
---|
[12298] | 64 | intermediateValues.Add(stack.Peek());
|
---|
[12291] | 65 | } else if (curSy == '-') {
|
---|
| 66 | NewSy();
|
---|
[12298] | 67 | if (!Term(d, erc)) { return false; }
|
---|
[12291] | 68 | stack.Push(-stack.Pop() + stack.Pop());
|
---|
[12298] | 69 | intermediateValues.Add(stack.Peek());
|
---|
[12291] | 70 | } else {
|
---|
| 71 | NewSy();
|
---|
[12298] | 72 | if (!Term(d, erc)) { return false; }
|
---|
[12291] | 73 | var e = stack.Pop();
|
---|
| 74 | var r = stack.Pop();
|
---|
| 75 | stack.Push(Not(r) * e + r * Not(e)); // xor = (!x AND y) OR (x AND !y)
|
---|
[12298] | 76 | intermediateValues.Add(stack.Peek());
|
---|
[12291] | 77 | }
|
---|
| 78 | curSy = CurSy();
|
---|
| 79 | }
|
---|
| 80 | return true;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | private bool Term(double[] d, double[] erc) {
|
---|
| 84 | if (!Fact(d, erc)) { return false; }
|
---|
| 85 | var curSy = CurSy();
|
---|
| 86 | while (curSy == '*' || curSy == '%') {
|
---|
| 87 | if (curSy == '*') {
|
---|
| 88 | NewSy();
|
---|
[12298] | 89 | if (!Fact(d, erc)) { return false; }
|
---|
[12291] | 90 | stack.Push(stack.Pop() * stack.Pop());
|
---|
[12298] | 91 | intermediateValues.Add(stack.Peek());
|
---|
[12291] | 92 | } else {
|
---|
| 93 | NewSy();
|
---|
[12298] | 94 | if (!Fact(d, erc)) { return false; }
|
---|
[12291] | 95 | var nom = stack.Pop();
|
---|
| 96 | var r = stack.Pop();
|
---|
| 97 | if (HeuristicLab.Common.Extensions.IsAlmost(nom, 0.0)) nom = 1.0;
|
---|
| 98 | stack.Push(r / nom);
|
---|
[12298] | 99 | intermediateValues.Add(stack.Peek());
|
---|
[12291] | 100 | }
|
---|
| 101 | curSy = CurSy();
|
---|
| 102 | }
|
---|
| 103 | return true;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | private bool Fact(double[] d, double[] erc) {
|
---|
| 107 | var curSy = CurSy();
|
---|
| 108 | if (curSy == '!') {
|
---|
[12298] | 109 | //NewSy();
|
---|
| 110 | //if (!Expr(d, erc)) { stack.Push(-7.0); return false; }
|
---|
| 111 | //stack.Push(Not(stack.Pop()));
|
---|
[12291] | 112 | } else if (curSy == '(') {
|
---|
[12298] | 113 | //NewSy();
|
---|
| 114 | //if (!Expr(d, erc)) { stack.Push(-8.0); return false; }
|
---|
| 115 | //if (CurSy() != ')') throw new ArgumentException();
|
---|
| 116 | //NewSy();
|
---|
[12291] | 117 | } else if (curSy >= 'a' && curSy <= 'z') {
|
---|
| 118 | int o = (byte)curSy - (byte)'a';
|
---|
| 119 | //int o = Convert.ToByte(CurSy()) - Convert.ToByte('a');
|
---|
| 120 | if (o < 0 || o >= d.Length) throw new ArgumentException();
|
---|
| 121 | stack.Push(d[o]);
|
---|
[12298] | 122 | intermediateValues.Add(stack.Peek());
|
---|
[12291] | 123 | NewSy();
|
---|
| 124 | } else if (curSy == '/') {
|
---|
| 125 | // /-symbol is used in the expressionextender to represent inverse (1/x).
|
---|
| 126 | // this is necessary because we also use symbols 0..9 as indices for ERCs
|
---|
[12298] | 127 | //NewSy();
|
---|
| 128 | //if (!Fact(d, erc)) { stack.Push(-9.0); return false; }
|
---|
| 129 | //stack.Push(1.0 / stack.Pop());
|
---|
[12291] | 130 | } else if (curSy >= '0' && curSy <= '9') {
|
---|
| 131 | int o = (byte)curSy - (byte)'0';
|
---|
| 132 | //int o = Convert.ToByte(CurSy()) - Convert.ToByte('a');
|
---|
| 133 | if (o < 0 || o >= 10) throw new ArgumentException();
|
---|
| 134 | stack.Push(erc[o]);
|
---|
[12298] | 135 | intermediateValues.Add(stack.Peek());
|
---|
[12291] | 136 | NewSy();
|
---|
| 137 | } else {
|
---|
| 138 | return false;
|
---|
| 139 | }
|
---|
| 140 | return true;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | }
|
---|
| 144 | }
|
---|