Free cookie consent management tool by TermsFeed Policy Generator

Changeset 3990 for trunk


Ignore:
Timestamp:
06/30/10 15:28:07 (14 years ago)
Author:
gkronber
Message:

Improved GlobalSymbolicExpressionGrammar regarding memory efficiency and fixed a bug in cloning. #938

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/GlobalSymbolicExpressionGrammar.cs

    r3885 r3990  
    4040      set {
    4141        minFunctionDefinitions = value;
    42         Initialize();
     42        UpdateAdfConstraints();
    4343      }
    4444    }
     
    4949      set {
    5050        maxFunctionDefinitions = value;
    51         Initialize();
     51        UpdateAdfConstraints();
    5252      }
    5353    }
     
    5858      set {
    5959        minFunctionArguments = value;
    60         Initialize();
    6160      }
    6261    }
     
    6766      set {
    6867        maxFunctionArguments = value;
    69         Initialize();
    7068      }
    7169    }
     70
    7271    [Storable]
    73     private ISymbolicExpressionGrammar mainBranchGrammar;
     72    private Defun defunSymbol;
    7473
    7574    public GlobalSymbolicExpressionGrammar() : base() { } // empty constructor for cloning
     
    7978      maxFunctionArguments = 3;
    8079      maxFunctionDefinitions = 3;
    81       this.mainBranchGrammar = mainBranchGrammar;
    82       Initialize();
     80      Initialize(mainBranchGrammar);
    8381    }
    8482
    85     private void Initialize() {
     83    private void Initialize(ISymbolicExpressionGrammar mainBranchGrammar) {
    8684      base.Clear();
    8785
     
    9088
    9189      StartSymbol = new ProgramRootSymbol();
    92       var defunSymbol = new Defun();
     90      defunSymbol = new Defun();
    9391      AddSymbol(StartSymbol);
    9492      AddSymbol(defunSymbol);
     
    9896      SetMinSubtreeCount(defunSymbol, 1);
    9997      SetMaxSubtreeCount(defunSymbol, 1);
     98
     99      // ADF branches maxFunctionDefinitions
     100      for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
     101        SetAllowedChild(StartSymbol, defunSymbol, argumentIndex);
     102      }
    100103
    101104      if (mainBranchGrammar != null) {
     
    109112        // the start symbol of the mainBranchGrammar is allowed as the result producing branch
    110113        SetAllowedChild(StartSymbol, mainBranchGrammar.StartSymbol, 0);
    111 
    112         // ADF branches maxFunctionDefinitions
    113         for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
    114           SetAllowedChild(StartSymbol, defunSymbol, argumentIndex);
    115         }
    116114
    117115        // copy syntax constraints from mainBranchGrammar
     
    133131      }
    134132    }
     133    private void UpdateAdfConstraints() {
     134      SetMinSubtreeCount(StartSymbol, minFunctionDefinitions + 1);
     135      SetMaxSubtreeCount(StartSymbol, maxFunctionDefinitions + 1);
     136     
     137      // ADF branches maxFunctionDefinitions
     138      for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
     139        SetAllowedChild(StartSymbol, defunSymbol, argumentIndex);
     140      }
     141    }
     142
    135143    public override IDeepCloneable Clone(Cloner cloner) {
    136144      GlobalSymbolicExpressionGrammar clone = (GlobalSymbolicExpressionGrammar)base.Clone(cloner);
     145      clone.defunSymbol = defunSymbol;
    137146      clone.maxFunctionArguments = maxFunctionArguments;
    138147      clone.maxFunctionDefinitions = maxFunctionDefinitions;
    139148      clone.minFunctionArguments = minFunctionArguments;
    140149      clone.minFunctionDefinitions = minFunctionDefinitions;
    141       clone.mainBranchGrammar = (ISymbolicExpressionGrammar)cloner.Clone(mainBranchGrammar);
    142150      return clone;
    143151    }
Note: See TracChangeset for help on using the changeset viewer.