/// <summary> /// Performs a single throw onto a roulette wheel where the wheel's space is unevenly divided between outcomes. /// The probabilty that a segment will be selected is given by that segment's value in the 'probabilities' /// array within the specified RouletteWheelLayout. The probabilities within a RouletteWheelLayout have already /// been normalised so that their total is always equal to 1.0. /// </summary> /// <param name="layout">The roulette wheel layout.</param> /// <param name="rng">Random number generator.</param> public static int SingleThrow(RouletteWheelLayout layout, FastRandom rng) { // Throw the ball and return an integer indicating the outcome. double throwValue = layout.ProbabilitiesTotal * rng.NextDouble(); double accumulator = 0.0; for(int i=0; i<layout.Probabilities.Length; i++) { accumulator += layout.Probabilities[i]; if(throwValue < accumulator) { return layout.Labels[i]; } } // We might get here through floating point arithmetic rounding issues. // e.g. accumulator == throwValue. // Find a nearby non-zero probability to select. // Wrap around to start of array. for(int i=0; i<layout.Probabilities.Length; i++) { if(layout.Probabilities[i] != 0.0) { return layout.Labels[i]; } } // If we get here then we have an array of zero probabilities. throw new SharpNeatException("Invalid operation. No non-zero probabilities to select."); }
/// <summary> /// Genetic mutation for auxiliary argument data. /// </summary> public void MutateAuxArgs(double[] auxArgs, FastRandom rng, ZigguratGaussianSampler gaussianSampler, double connectionWeightRange) { throw new SharpNeatException("MutateAuxArgs() called on activation function that does not use auxiliary arguments."); }
/// <summary> /// For activation functions that accept auxiliary arguments; generates random initial values for aux arguments for newly /// added nodes (from an 'add neuron' mutation). /// </summary> public double[] GetRandomAuxArgs(FastRandom rng, double connectionWeightRange) { throw new SharpNeatException("GetRandomAuxArgs() called on activation function that does not use auxiliary arguments."); }
/// <summary> /// Gets the single NEAT activation function provided at construction time. /// The provided random number generator is not used in this implementation. /// </summary> /// <param name="rng"></param> /// <returns></returns> public ActivationFunctionInfo GetRandomFunction(FastRandom rng) { return _activationFnInfo; }
/// <summary> /// Performs a single throw for a given number of outcomes with equal probabilities. /// </summary> /// <param name="numberOfOutcomes">The number of possible outcomes.</param> /// <param name="rng">Random number generator.</param> /// <returns>An integer between 0..numberOfOutcomes-1. In effect this routine selects one of the possible outcomes.</returns> public static int SingleThrowEven(int numberOfOutcomes, FastRandom rng) { return (int)(rng.NextDouble() * numberOfOutcomes); }
/// <summary> /// A simple single throw routine. /// </summary> /// <param name="probability">A probability between 0..1 that the throw will result in a true result.</param> /// <param name="rng">Random number generator.</param> public static bool SingleThrow(double probability, FastRandom rng) { return rng.NextDouble() < probability; }
/// <summary> /// Default constructor. /// </summary> public TestCaseField() { _rng = new FastRandom(); }
/// <summary> /// Randomly select a function based on each function's selection probability. /// </summary> public ActivationFunctionInfo GetRandomFunction(FastRandom rng) { return _functionList[RouletteWheel.SingleThrow(_rwl, rng)]; }