- Timestamp:
- 01/22/14 18:09:44 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs
r10338 r10384 33 33 34 34 // generic interface for communication from problem interpretation to solver 35 public interface ISolverState {36 void Reset();37 int PeekNextAlternative(); // in alternative nodes returns the index of the alternative that should be followed38 void Follow(int idx); // next: derive the NT symbol with index=idx;39 void Unwind(); // finished with deriving the NT symbol40 }35 // public interface ISolverState { 36 // void Reset(); 37 // int PeekNextAlternative(); // in alternative nodes returns the index of the alternative that should be followed 38 // void Follow(int idx); // next: derive the NT symbol with index=idx; 39 // void Unwind(); // finished with deriving the NT symbol 40 // } 41 41 42 42 public sealed class ?IDENT?Problem { … … 53 53 } 54 54 55 private ISolverState _state;56 public double Evaluate( ISolverState _state) {57 this._ state = _state;55 private Tree _t; 56 public double Evaluate(Tree _t) { 57 this._t = _t; 58 58 #region objective function (MINIMIZE / MAXIMIZE section) 59 59 ?FITNESSFUNCTION? … … 216 216 sb.AppendFormat("public class {0}Tree : Tree {{", terminal.Ident).BeginBlock(); 217 217 foreach (var att in terminal.FieldDefinitions) { 218 sb.AppendFormat(" {0} {1};", att.Type, att.Identifier).AppendLine();218 sb.AppendFormat("public {0} {1};", att.Type, att.Identifier).AppendLine(); 219 219 } 220 220 sb.AppendFormat(" public {0}Tree(Random random, ?IDENT?Problem problem) : base() {{", terminal.Ident).BeginBlock(); … … 222 222 if (constr.Type == ConstraintNodeType.Set) { 223 223 sb.AppendLine("{").BeginBlock(); 224 sb.AppendFormat(" 224 sb.AppendFormat("var elements = problem.GetAllowed{0}_{1}().ToArray();", terminal.Ident, constr.Ident).AppendLine(); 225 225 sb.AppendFormat("{0} = elements[random.Next(elements.Length)]; ", constr.Ident).AppendLine(); 226 226 sb.AppendLine("}").EndBlock(); … … 229 229 sb.AppendFormat(" var max = problem.GetMax{0}_{1}();", terminal.Ident, constr.Ident).AppendLine(); 230 230 sb.AppendFormat(" var min = problem.GetMin{0}_{1}();", terminal.Ident, constr.Ident).AppendLine(); 231 sb.AppendFormat("{0} = random.NextDouble() * (max - min) + min 231 sb.AppendFormat("{0} = random.NextDouble() * (max - min) + min;", constr.Ident).AppendLine(); 232 232 sb.AppendLine("}").EndBlock(); 233 233 } … … 268 268 else 269 269 actualParameter = string.Empty; 270 sb.AppendLine("_state.Reset();"); 271 sb.AppendFormat("{0}(_state, {1});", s.Name, actualParameter).AppendLine(); 270 sb.AppendFormat("{0}(_t, {1});", s.Name, actualParameter).AppendLine(); 272 271 sb.AppendLine("}").EndBlock(); 273 272 } … … 275 274 private void GenerateInterpreterMethod(AttributedGrammar g, ISymbol s, SourceBuilder sb) { 276 275 if (!s.Attributes.Any()) 277 sb.AppendFormat("private void {0}( ISolverState _state) {{", s.Name).BeginBlock();276 sb.AppendFormat("private void {0}(Tree _t) {{", s.Name).BeginBlock(); 278 277 else 279 sb.AppendFormat("private void {0}( ISolverState _state, {1}) {{", s.Name, s.GetAttributeString()).BeginBlock();278 sb.AppendFormat("private void {0}(Tree _t, {1}) {{", s.Name, s.GetAttributeString()).BeginBlock(); 280 279 281 280 // generate local definitions … … 297 296 298 297 private void GenerateSwitchStatement(IEnumerable<Sequence> alts, SourceBuilder sb) { 299 sb.Append("switch(_ state.PeekNextAlternative()) {").BeginBlock();298 sb.Append("switch(_t.altIdx) {").BeginBlock(); 300 299 // generate a case for each alternative 301 300 int i = 0; … … 321 320 sb.Append(action.Code + ";"); 322 321 else if (!s.Attributes.Any()) 323 sb.AppendFormat(" _state.Follow({0}); {1}(_state); _state.Unwind();", idx, s.Name);324 else sb.AppendFormat(" _state.Follow({0}); {1}(_state, {2}); _state.Unwind();", idx, s.Name, s.GetAttributeString());322 sb.AppendFormat("{1}(_t.subtrees[{0}]);", idx, s.Name); 323 else sb.AppendFormat("{1}(_t.subtrees[{0}], {2}); ", idx, s.Name, s.GetAttributeString()); 325 324 sb.AppendLine(); 326 325 } … … 329 328 // if the terminal symbol has attributes then we must samples values for these attributes 330 329 if (!s.Attributes.Any()) 331 sb.AppendFormat("private void {0}( ISolverState _state) {{", s.Name).BeginBlock();330 sb.AppendFormat("private void {0}(Tree _t) {{", s.Name).BeginBlock(); 332 331 else 333 sb.AppendFormat("private void {0}( ISolverState _state, {1}) {{", s.Name, s.GetAttributeString()).BeginBlock();332 sb.AppendFormat("private void {0}(Tree _t, {1}) {{", s.Name, s.GetAttributeString()).BeginBlock(); 334 333 335 334 // each field must match a formal parameter, assign a value for each parameter 336 335 int i = 0; 337 336 foreach (var element in s.Attributes) { 338 sb.AppendFormat("_state.Follow({0});", i++).AppendLine(); 339 sb.AppendFormat("{0} = Get{1}_{0}(_state);", element.Name, s.Name).AppendLine(); 340 sb.AppendFormat("_state.Unwind();").AppendLine(); 337 sb.AppendFormat("{0} = (_t as {1}Tree).{0};", element.Name, s.Name).AppendLine(); 341 338 } 342 339 sb.Append("}").EndBlock();
Note: See TracChangeset
for help on using the changeset viewer.