Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
05/31/10 15:51:50 (15 years ago)
Author:
gkronber
Message:

Fixed bug: ChangeNodeTypeManipulation created tree nodes with disabled symbols (InitialFrequency == 0). #1016

File:
1 edited

Legend:

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

    r3512 r3875  
    2626using HeuristicLab.Data;
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     28using System;
     29using System.Collections.Generic;
     30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Symbols;
    2831
    2932namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators {
     
    6568        return;
    6669      }
    67       var newSymbol = constrainedSymbols.SelectRandom(random);
     70      var newSymbol = SelectRandomSymbol(random, constrainedSymbols);
    6871
    6972      // replace the old node with the new node
     
    7780      success = true;
    7881    }
     82
     83    private static Symbol SelectRandomSymbol(IRandom random, IEnumerable<Symbol> symbols) {
     84      var symbolList = symbols.ToList();
     85      var ticketsSum = symbolList.Select(x => x.InitialFrequency).Sum();
     86      if (ticketsSum == 0.0) throw new ArgumentException("The initial frequency of all allowed symbols is zero.");
     87      var r = random.NextDouble() * ticketsSum;
     88      double aggregatedTickets = 0;
     89      for (int i = 0; i < symbolList.Count; i++) {
     90        aggregatedTickets += symbolList[i].InitialFrequency;
     91        if (aggregatedTickets > r) {
     92          return symbolList[i];
     93        }
     94      }
     95      // this should never happen
     96      throw new ArgumentException("There is a problem with the initial frequency setting of allowed symbols.");
     97    }
    7998  }
    8099}
Note: See TracChangeset for help on using the changeset viewer.