Changeset 11832 for branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems
- Timestamp:
- 01/27/15 16:34:34 (9 years ago)
- Location:
- branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/EvenParityProblem.cs
r11803 r11832 50 50 } 51 51 52 public string CanonicalRepresentation(string terminalPhrase) {52 public string CanonicalRepresentation(string phrase) { 53 53 throw new NotImplementedException(); 54 return terminalPhrase; 54 return phrase; 55 } 56 57 public IEnumerable<Feature> GetFeatures(string phrase) 58 { 59 throw new NotImplementedException(); 55 60 } 56 61 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/ExpressionInterpreter.cs
r11803 r11832 3 3 using System.Diagnostics.Eventing.Reader; 4 4 using System.Linq; 5 using System.Runtime.CompilerServices; 5 6 using System.Runtime.Remoting.Messaging; 6 7 using System.Text; … … 10 11 namespace HeuristicLab.Problems.GrammaticalOptimization { 11 12 public class ExpressionInterpreter { 13 private static readonly double[] emptyErc = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 14 12 15 private string sentence; 13 16 private int syIdx; … … 15 18 // Expr -> Term { ('+' | '-' | '^' ) Term } 16 19 // Term -> Fact { ('*' | '/') Fact } 17 // Fact -> '!' Expr | '(' Expr ')' | Var 20 // Fact -> '!' Expr | '(' Expr ')' | Var | const 18 21 // Var -> 'a'..'z' 22 // const -> '0' .. '9' 23 24 // constants are Koza-style ephemeral random constants (ERC). for now we only allow up to 10 constants. 25 // The constant symbols '0' .. '9' are treated as ERC indices 19 26 20 27 // for boolean problems the symbol * representes the AND operator, the symbol + represents the OR operator, ! = NOT, ^ = XOR 21 28 22 29 public bool Interpret(string sentence, bool[] vars) { 30 return Interpret(sentence, vars, emptyErc); 31 } 32 33 public bool Interpret(string sentence, bool[] vars, double[] erc) { 23 34 InitLex(sentence); 24 35 var d = new double[vars.Length]; 25 36 for (int i = 0; i < vars.Length; i++) d[i] = vars[i] ? 1.0 : 0.0; 26 return !(Expr(d ).IsAlmost(0));37 return !(Expr(d, erc).IsAlmost(0)); 27 38 } 28 39 29 40 public double Interpret(string sentence, double[] vars) { 41 return Interpret(sentence, vars, emptyErc); 42 } 43 44 public double Interpret(string sentence, double[] vars, double[] erc) { 30 45 InitLex(sentence); 31 return Expr(vars );46 return Expr(vars, erc); 32 47 } 33 48 … … 51 66 } 52 67 53 private double Expr(double[] d ) {68 private double Expr(double[] d, double[] erc) { 54 69 var r = 0.0; 55 r = Term(d );70 r = Term(d, erc); 56 71 var curSy = CurSy(); 57 72 while (curSy == '+' || curSy == '-' || curSy == '^') { 58 73 if (curSy == '+') { 59 74 NewSy(); 60 r += Expr(d );75 r += Expr(d, erc); 61 76 } else if (curSy == '-') { 62 77 NewSy(); 63 r -= Expr(d );78 r -= Expr(d, erc); 64 79 } else { 65 80 NewSy(); 66 var e = Expr(d );81 var e = Expr(d, erc); 67 82 r = Not(r) * e + r * Not(e); // xor = (!x AND y) OR (x AND !y) 68 83 } … … 72 87 } 73 88 74 private double Term(double[] d ) {89 private double Term(double[] d, double[] erc) { 75 90 var r = 0.0; 76 r = Fact(d );91 r = Fact(d, erc); 77 92 var curSy = CurSy(); 78 93 while (curSy == '*' || curSy == '/') { 79 94 if (curSy == '*') { 80 95 NewSy(); 81 r *= Term(d );96 r *= Term(d, erc); 82 97 } else { 83 98 NewSy(); 84 r /= Term(d );99 r /= Term(d, erc); 85 100 } 86 101 curSy = CurSy(); … … 89 104 } 90 105 91 private double Fact(double[] d ) {106 private double Fact(double[] d, double[] erc) { 92 107 double r = 0.0; 93 108 var curSy = CurSy(); 94 109 if (curSy == '!') { 95 110 NewSy(); 96 r = Not(Expr(d ));111 r = Not(Expr(d, erc)); 97 112 } else if (curSy == '(') { 98 113 NewSy(); 99 r = Expr(d );114 r = Expr(d, erc); 100 115 if (CurSy() != ')') throw new ArgumentException(); 101 116 NewSy(); 102 } else /* if (curSy >= 'a' && curSy <= 'z') */{117 } else if (curSy >= 'a' && curSy <= 'z') { 103 118 int o = (byte)curSy - (byte)'a'; 104 119 //int o = Convert.ToByte(CurSy()) - Convert.ToByte('a'); 105 120 if (o < 0 || o >= d.Length) throw new ArgumentException(); 106 121 r = d[o]; 122 NewSy(); 123 } else /* if (curSy >= '0' && curSy <= '9') */ { 124 int o = (byte)curSy - (byte)'0'; 125 //int o = Convert.ToByte(CurSy()) - Convert.ToByte('a'); 126 if (o < 0 || o >= 10) throw new ArgumentException(); 127 r = erc[o]; 107 128 NewSy(); 108 129 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/FindPhrasesProblem.cs
r11803 r11832 129 129 } 130 130 131 public string CanonicalRepresentation(string terminalPhrase) {131 public string CanonicalRepresentation(string phrase) { 132 132 // as the ordering of phrases does not matter we can reorder the phrases 133 133 // and remove duplicates 134 var numPhrases = terminalPhrase.Length / phraseLen;134 var numPhrases = phrase.Length / phraseLen; 135 135 var phrases = new SortedSet<string>(); 136 136 for (int phraseIdx = 0; phraseIdx < numPhrases; phraseIdx++) { 137 137 var sentenceIdx = phraseIdx * phraseLen; 138 var phrase = terminalPhrase.Substring(sentenceIdx, phraseLen);139 phrase = CanonicalPhrase(phrase);140 if (!phrases.Contains( phrase)) phrases.Add(phrase);138 var subphrase = phrase.Substring(sentenceIdx, phraseLen); 139 subphrase = CanonicalPhrase(subphrase); 140 if (!phrases.Contains(subphrase)) phrases.Add(subphrase); 141 141 } 142 var remainder = terminalPhrase.Substring(numPhrases * phraseLen, terminalPhrase.Length - (numPhrases * phraseLen));142 var remainder = phrase.Substring(numPhrases * phraseLen, phrase.Length - (numPhrases * phraseLen)); 143 143 remainder = CanonicalPhrase(remainder); 144 144 if (!phrases.Contains(remainder)) phrases.Add(remainder); … … 146 146 return string.Join("", phrases); 147 147 } 148 149 public IEnumerable<Feature> GetFeatures(string phrase) 150 { 151 throw new NotImplementedException(); 152 } 153 154 public override string ToString() { 155 return string.Format("\"FindPhrasesProblem {0} {1} {2} {3:F2} {4} {5:F2} {6}\"", numPhrases, phraseLen, 156 optimalPhrases.Count, correctReward, decoyPhrases.Count, decoyReward, phrasesAsSets); 157 } 148 158 } 149 159 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/HardPalindromeProblem.cs
r11803 r11832 39 39 } 40 40 41 public string CanonicalRepresentation(string terminalPhrase) { 42 return terminalPhrase; 41 public string CanonicalRepresentation(string phrase) { 42 return phrase; 43 } 44 45 public IEnumerable<Feature> GetFeatures(string phrase) 46 { 47 throw new NotImplementedException(); 43 48 } 44 49 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/PalindromeProblem.cs
r11803 r11832 80 80 } 81 81 82 public string CanonicalRepresentation(string terminalPhrase) { 83 return terminalPhrase; 82 public string CanonicalRepresentation(string phrase) { 83 return phrase; 84 } 85 86 public IEnumerable<Feature> GetFeatures(string phrase) 87 { 88 throw new NotImplementedException(); 84 89 } 85 90 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalPairProblem.cs
r11803 r11832 35 35 } 36 36 37 public string CanonicalRepresentation(string terminalPhrase) { 37 public string CanonicalRepresentation(string phrase) { 38 //throw new NotImplementedException(); 39 return phrase; 40 } 41 42 public IEnumerable<Feature> GetFeatures(string phrase) 43 { 38 44 throw new NotImplementedException(); 39 return terminalPhrase;40 45 } 41 46 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalPhraseSequenceProblem.cs
r11806 r11832 113 113 } 114 114 115 public string CanonicalRepresentation(string terminalPhrase) {115 public string CanonicalRepresentation(string phrase) { 116 116 if (phrasesAsSets) { 117 117 var sb = new StringBuilder(); 118 var numPhrases = terminalPhrase.Length / phraseLen;118 var numPhrases = phrase.Length / phraseLen; 119 119 for (int phraseIdx = 0; phraseIdx < numPhrases; phraseIdx++) { 120 120 var sentenceIdx = phraseIdx * phraseLen; 121 var phrase = terminalPhrase.Substring(sentenceIdx, phraseLen);122 phrase = CanonicalPhrase(phrase);123 sb.Append( phrase);121 var subphrase = phrase.Substring(sentenceIdx, phraseLen); 122 subphrase = CanonicalPhrase(subphrase); 123 sb.Append(subphrase); 124 124 } 125 125 126 var remainder = terminalPhrase.Substring(numPhrases * phraseLen, terminalPhrase.Length - (numPhrases * phraseLen));126 var remainder = phrase.Substring(numPhrases * phraseLen, phrase.Length - (numPhrases * phraseLen)); 127 127 remainder = CanonicalPhrase(remainder); 128 128 sb.Append(remainder); … … 130 130 return sb.ToString(); 131 131 } else 132 return terminalPhrase; 132 return phrase; 133 } 134 135 public IEnumerable<Feature> GetFeatures(string phrase) 136 { 137 throw new NotImplementedException(); 133 138 } 134 139 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalRoadProblem.cs
r11803 r11832 29 29 throw new NotImplementedException(); 30 30 } 31 public string CanonicalRepresentation(string terminalPhrase) {32 return terminalPhrase;31 public string CanonicalRepresentation(string phrase) { 32 return phrase; 33 33 } 34 34 35 public IEnumerable<Feature> GetFeatures(string phrase) 36 { 37 throw new NotImplementedException(); 38 } 35 39 } 36 40 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalSequenceProblem.cs
r11803 r11832 77 77 78 78 // in each position there could be multiple correct and incorrect symbols 79 public string CanonicalRepresentation(string terminalPhrase) {79 public string CanonicalRepresentation(string phrase) { 80 80 var sb = new StringBuilder(); 81 for (int i = 0; i < terminalPhrase.Length; i++) {82 if (optimalSymbolsForPos[i].Contains( terminalPhrase[i])) {81 for (int i = 0; i < phrase.Length; i++) { 82 if (optimalSymbolsForPos[i].Contains(phrase[i])) { 83 83 sb.Append(optimalSymbolsForPos[i].First()); // all symbols in the set are equivalent 84 84 } else { 85 sb.Append( terminalPhrase[i]);85 sb.Append(phrase[i]); 86 86 } 87 87 } 88 88 return sb.ToString(); 89 89 } 90 91 public IEnumerable<Feature> GetFeatures(string phrase) 92 { 93 throw new NotImplementedException(); 94 } 90 95 } 91 96 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalSymbolProblem.cs
r11806 r11832 34 34 return regex.Matches(sentence).Count; 35 35 } 36 public string CanonicalRepresentation(string terminalPhrase) {36 public string CanonicalRepresentation(string phrase) { 37 37 throw new NotImplementedException(); 38 return terminalPhrase;38 return phrase; 39 39 } 40 40 41 public IEnumerable<Feature> GetFeatures(string phrase) 42 { 43 throw new NotImplementedException(); 44 } 41 45 } 42 46 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/RoyalTreeProblem.cs
r11803 r11832 29 29 throw new NotImplementedException(); 30 30 } 31 public string CanonicalRepresentation(string terminalPhrase) {31 public string CanonicalRepresentation(string phrase) { 32 32 throw new NotImplementedException(); 33 return terminalPhrase;33 return phrase; 34 34 } 35 35 36 public IEnumerable<Feature> GetFeatures(string phrase) 37 { 38 throw new NotImplementedException(); 39 } 36 40 } 37 41 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/SantaFeAntProblem.cs
r11806 r11832 102 102 } 103 103 104 public string CanonicalRepresentation(string terminalPhrase) {105 var sb = new StringBuilder( terminalPhrase);106 string canonicalPhrase = terminalPhrase;104 public string CanonicalRepresentation(string phrase) { 105 var sb = new StringBuilder(phrase); 106 string canonicalPhrase = phrase; 107 107 string oldPhrase; 108 108 do { … … 112 112 } while (canonicalPhrase != oldPhrase); 113 113 return sb.ToString(); 114 } 115 116 public IEnumerable<Feature> GetFeatures(string phrase) { 117 return Enumerable.Repeat(new Feature(CanonicalRepresentation(phrase), 1.0), 1); 114 118 } 115 119 } -
branches/HeuristicLab.Problems.GrammaticalOptimization/HeuristicLab.Problems.GrammaticalOptimization/Problems/SymbolicRegressionPoly10Problem.cs
r11806 r11832 120 120 return canonicalTerm; 121 121 } 122 123 // splits the phrase into terms and creates (sparse) term-occurrance features 124 public IEnumerable<Feature> GetFeatures(string phrase) { 125 var canonicalTerms = new HashSet<string>(); 126 foreach (string t in phrase.Split('+')) { 127 canonicalTerms.Add(CanonicalTerm(t)); 128 } 129 return canonicalTerms.Select(entry => new Feature(entry, 1.0)).Concat(new Feature[] { new Feature(CanonicalRepresentation(phrase), 1.0) }); 130 } 131 122 132 } 123 133 }
Note: See TracChangeset
for help on using the changeset viewer.