Free cookie consent management tool by TermsFeed Policy Generator

Changeset 3817


Ignore:
Timestamp:
05/16/10 23:31:08 (15 years ago)
Author:
swagner
Message:

Implemented reviewers' comments (#893)

Location:
trunk/sources
Files:
1 deleted
5 edited
1 moved

Legend:

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

    r3816 r3817  
    3737namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Analyzers {
    3838  /// <summary>
    39   /// An operator that tracks the min avgerage and max tree size.
     39  /// An operator that tracks the min average and max tree size.
    4040  /// </summary>
    41   [Item("MinAvgMaxSymbolicExpressionTreeSizeAnalyzer", "An operator that tracks the min avgerage and max tree size.")]
     41  [Item("MinAverageMaxSymbolicExpressionTreeSizeAnalyzer", "An operator that tracks the min avgerage and max tree size.")]
    4242  [StorableClass]
    43   public sealed class MinAvgMaxSymbolicExpressionTreeSizeAnalyzer : AlgorithmOperator, ISymbolicExpressionTreeAnalyzer {
     43  public sealed class MinAverageMaxSymbolicExpressionTreeSizeAnalyzer : AlgorithmOperator, ISymbolicExpressionTreeAnalyzer {
    4444    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
    4545    private const string SymbolicExpressionTreeSizeParameterName = "SymbolicExpressionTreeSize";
     
    7070
    7171    #endregion
    72     public MinAvgMaxSymbolicExpressionTreeSizeAnalyzer()
     72    public MinAverageMaxSymbolicExpressionTreeSizeAnalyzer()
    7373      : base() {
    7474      Parameters.Add(new ScopeTreeLookupParameter<SymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression tree whose size should be calculated."));
     
    104104
    105105    [StorableConstructor]
    106     private MinAvgMaxSymbolicExpressionTreeSizeAnalyzer(bool deserializing) : base() { }
     106    private MinAverageMaxSymbolicExpressionTreeSizeAnalyzer(bool deserializing) : base() { }
    107107
    108108    [StorableHook(HookType.AfterDeserialization)]
     
    113113
    114114    public override IDeepCloneable Clone(Cloner cloner) {
    115       MinAvgMaxSymbolicExpressionTreeSizeAnalyzer clone = (MinAvgMaxSymbolicExpressionTreeSizeAnalyzer)base.Clone(cloner);
     115      MinAverageMaxSymbolicExpressionTreeSizeAnalyzer clone = (MinAverageMaxSymbolicExpressionTreeSizeAnalyzer)base.Clone(cloner);
    116116      clone.Initialize();
    117117      return clone;
  • trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.csproj

    r3710 r3817  
    8383  </ItemGroup>
    8484  <ItemGroup>
    85     <Compile Include="Analyzers\MinAvgMaxSymbolicExpressionTreeSizeAnalyzer.cs" />
     85    <Compile Include="Analyzers\MinAverageMaxSymbolicExpressionTreeSizeAnalyzer.cs" />
    8686    <Compile Include="Analyzers\SymbolicExpressionTreeSizeCalculator.cs" />
    8787    <Compile Include="ArchitectureManipulators\ArgumentCreater.cs" />
  • trunk/sources/HeuristicLab.Operators/3.3/HeuristicLab.Operators-3.3.csproj

    r3702 r3817  
    9292    <Compile Include="Assigner.cs" />
    9393    <Compile Include="AlgorithmOperator.cs" />
    94     <Compile Include="StochasticMultiBranchOfT.cs" />
    9594    <Compile Include="MultiOperator.cs" />
    9695    <Compile Include="Operator.cs" />
  • trunk/sources/HeuristicLab.Operators/3.3/StochasticMultiBranch.cs

    r3597 r3817  
    3434  /// Selects one of its branches (if there are any) given a list of relative probabilities.
    3535  /// </summary>
     36  [Item("StochasticMultiBranch<T>", "Selects one of its branches (if there are any) given a list of relative probabilities.")]
     37  [StorableClass]
     38  public abstract class StochasticMultiBranch<T> : CheckedMultiOperator<T> where T : class, IOperator {
     39    /// <summary>
     40    /// Should return true if the StochasticMultiOperator should create a new child operation with the selected successor
     41    /// or if it should create a new operation. If you need to shield the parameters of the successor you should return true here.
     42    /// </summary>
     43    protected abstract bool CreateChildOperation { get; }
     44
     45    public ValueLookupParameter<DoubleArray> ProbabilitiesParameter {
     46      get { return (ValueLookupParameter<DoubleArray>)Parameters["Probabilities"]; }
     47    }
     48    public ILookupParameter<IRandom> RandomParameter {
     49      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
     50    }
     51
     52    public DoubleArray Probabilities {
     53      get { return ProbabilitiesParameter.Value; }
     54      set { ProbabilitiesParameter.Value = value; }
     55    }
     56
     57    [StorableConstructor]
     58    protected StochasticMultiBranch(bool deserializing) : base(deserializing) { }
     59    /// <summary>
     60    /// Initializes a new instance of <see cref="StochasticMultiOperator"/> with two parameters
     61    /// (<c>Probabilities</c> and <c>Random</c>).
     62    /// </summary>
     63    public StochasticMultiBranch()
     64      : base() {
     65      Parameters.Add(new ValueLookupParameter<DoubleArray>("Probabilities", "The array of relative probabilities for each operator.", new DoubleArray()));
     66      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
     67    }
     68
     69    protected override void Operators_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
     70      base.Operators_ItemsRemoved(sender, e);
     71      if (Probabilities != null && Probabilities.Length > Operators.Count) {
     72        List<double> probs = new List<double>(Probabilities.Cast<double>());
     73        var sorted = e.Items.OrderByDescending(x => x.Index);
     74        foreach (IndexedItem<T> item in sorted)
     75          if (probs.Count > item.Index) probs.RemoveAt(item.Index);
     76        Probabilities = new DoubleArray(probs.ToArray());
     77      }
     78    }
     79
     80    protected override void Operators_ItemsAdded(object sender, HeuristicLab.Collections.CollectionItemsChangedEventArgs<IndexedItem<T>> e) {
     81      base.Operators_ItemsAdded(sender, e);
     82      if (Probabilities != null && Probabilities.Length < Operators.Count) {
     83        double avg = (Probabilities.Where(x => x > 0).Count() > 0) ? (Probabilities.Where(x => x > 0).Average()) : (1);
     84        // add the average of all probabilities in the respective places (the new operators)
     85        var added = e.Items.OrderBy(x => x.Index).ToList();
     86        int insertCount = 0;
     87        DoubleArray probs = new DoubleArray(Operators.Count);
     88        for (int i = 0; i < Operators.Count; i++) {
     89          if (insertCount < added.Count && i == added[insertCount].Index) {
     90            probs[i] = avg;
     91            insertCount++;
     92          } else if (i - insertCount < Probabilities.Length) {
     93            probs[i] = Probabilities[i - insertCount];
     94          } else probs[i] = avg;
     95        }
     96        Probabilities = probs;
     97      }
     98    }
     99
     100    /// <summary>
     101    /// Applies an operator of the branches to the current scope with a
     102    /// specific probability.
     103    /// </summary>
     104    /// <exception cref="InvalidOperationException">Thrown when the list of probabilites does not
     105    /// match the number of operators, the list of selected operators is empty,
     106    /// or all selected operators have zero probabitlity.</exception>
     107    /// <returns>A new operation with the operator that was selected followed by the current operator's successor.</returns>
     108    public override IOperation Apply() {
     109      IRandom random = RandomParameter.ActualValue;
     110      DoubleArray probabilities = ProbabilitiesParameter.ActualValue;
     111      if (probabilities.Length != Operators.Count) {
     112        throw new InvalidOperationException(Name + ": The list of probabilities has to match the number of operators");
     113      }
     114      IOperator successor = null;
     115      var checkedOperators = Operators.CheckedItems;
     116      if (checkedOperators.Count() > 0) {
     117        // select a random operator from the checked operators
     118        double sum = (from indexedItem in checkedOperators select probabilities[indexedItem.Index]).Sum();
     119        if (sum == 0) throw new InvalidOperationException(Name + ": All selected operators have zero probability.");
     120        double r = random.NextDouble() * sum;
     121        sum = 0;
     122        foreach (var indexedItem in checkedOperators) {
     123          sum += probabilities[indexedItem.Index];
     124          if (sum > r) {
     125            successor = indexedItem.Value;
     126            break;
     127          }
     128        }
     129      }
     130      OperationCollection next = new OperationCollection(base.Apply());
     131      if (successor != null) {
     132        if (CreateChildOperation)
     133          next.Insert(0, ExecutionContext.CreateChildOperation(successor));
     134        else next.Insert(0, ExecutionContext.CreateOperation(successor));
     135      }
     136      return next;
     137    }
     138  }
     139
     140  /// <summary>
     141  /// Selects one of its branches (if there are any) given a list of relative probabilities.
     142  /// </summary>
    36143  [Item("StochasticMultiBranch", "Selects one of its branches (if there are any) given a list of relative probabilities.")]
    37144  [StorableClass]
  • trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntProblem.cs

    r3739 r3817  
    301301      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>().OfType<IOperator>());
    302302      operators.Add(new BestAntTrailAnalyzer());
    303       operators.Add(new MinAvgMaxSymbolicExpressionTreeSizeAnalyzer());
     303      operators.Add(new MinAverageMaxSymbolicExpressionTreeSizeAnalyzer());
    304304      ParameterizeAnalyzers();
    305305      ParameterizeOperators();
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionProblem.cs

    r3806 r3817  
    369369      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>().OfType<IOperator>());
    370370      operators.Add(new ValidationBestScaledSymbolicRegressionSolutionAnalyzer());
    371       operators.Add(new MinAvgMaxSymbolicExpressionTreeSizeAnalyzer());
     371      operators.Add(new MinAverageMaxSymbolicExpressionTreeSizeAnalyzer());
    372372      operators.Add(new SymbolicRegressionVariableFrequencyAnalyzer());
    373373      ParameterizeOperators();
Note: See TracChangeset for help on using the changeset viewer.