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