/// <summary> /// Choose the next node for an ant to visit. This is based on probability. /// </summary> /// <param name="currentIndex">The step we are at in the path.</param> /// <param name="ant">The ant being evaluated.</param> /// <returns>The node we will move into.</returns> private int PickNextNode(int currentIndex, DiscreteAnt ant) { if (currentIndex == 0 || Random.NextDouble() < PR) { int index; do { index = Random.NextInt(0, _graph.Count); } while (ant.WasVisited(index)); return(index); } double[] prob = CalculateProbability(currentIndex, ant); double r = Random.NextDouble(); double sum = 0; for (int i = 0; i < _graph.Count; i++) { sum += prob[i]; if (sum >= r) { return(i); } } // should not happen! return(-1); }
void Crossover(IGenerateRandom rnd, UniverseRunner crossoverParent1, UniverseRunner crossoverParent2) { double[] parent1 = crossoverParent1.PhysicsRules.Data; double[] parent2 = crossoverParent2.PhysicsRules.Data; double[] child = PhysicsRules.Data; int len = parent1.Length; var p1 = (int)(rnd.NextDouble() * len); var p2 = (int)(rnd.NextDouble() * len); for (int i = 0; i < PhysicsRules.Data.Length; i++) { if (i < p1) { child[i] = parent1[i]; } else if (i >= p1 && i <= p2) { child[i] = parent2[i]; } else if (i > p2) { child[i] = parent1[i]; } } }
public void Process() { long tries = 0; int success = 0; int lastUpdate = 0; for (int i = 0; i < 1000000000; i++) { // pick a point at random. double x = _rnd.NextDouble(); double y = _rnd.NextDouble(); tries++; // was the point inside of a circle? if (x * x + y * y <= 1) { success++; } lastUpdate++; if (lastUpdate >= 1000000) { double pi = 4 * (double)success / tries; Console.WriteLine("Tries=" + tries + ", pi=" + pi); lastUpdate = 0; } } }
/// <summary> /// Perform a mutate on a parent and generate a new child. The parent is not changed. /// </summary> /// <param name="rnd">Random number generator.</param> /// <param name="sourcePhysics">The parent object.</param> /// <param name="probChange">The probability of changing an individual element.</param> /// <param name="perturb">The amount that an object is changed by.</param> public void Mutate(IGenerateRandom rnd, IPhysics sourcePhysics, double probChange, double perturb) { PhysicsRules.CopyData(sourcePhysics.Data); for (int i = 0; i < sourcePhysics.Data.Length; i++) { if (rnd.NextDouble() < probChange) { PhysicsRules.Data[i] += perturb * rnd.NextDouble(-1, 1); } } }
/// <summary> /// Randomize the long term memory, with the specified random number generator. /// </summary> /// <param name="rnd">A random number generator.</param> public void Reset(IGenerateRandom rnd) { for (int i = 0; i < _longTermMemory.Length; i++) { _longTermMemory[i] = rnd.NextDouble(-1, 1); } }
/// <inheritdoc /> public override void TrainingBatch(IGenerateRandom rnd) { for (var i = 0; i < _active.Length; i++) { _active[i] = rnd.NextDouble() > DropoutProbability; } }
public void BackgroundThread() { if (_uniformMode) { while (!_requestStop) { ReportNumber(_rnd.NextDouble()); } } else { while (!_requestStop) { ReportNumber(_rnd.NextGaussian()); } } Action a = () => { ButtonStart.IsEnabled = true; ButtonStop.IsEnabled = false; ComboDistribution.IsEnabled = true; ComboGenerator.IsEnabled = true; }; Dispatcher.BeginInvoke(a); }
/// <summary> /// Pick a operator based on the number of parents available. /// </summary> /// <param name="rnd">A random number generator.</param> /// <param name="maxParents">The maximum number of parents available.</param> /// <returns>The operator that was selected.</returns> public IEvolutionaryOperator PickMaxParents(IGenerateRandom rnd, int maxParents) { // determine the total probability of eligible operators double total = 0; foreach (ObjectHolder <IEvolutionaryOperator> holder in ObjList) { if (holder.Obj.ParentsNeeded <= maxParents) { total += holder.Probability; } } // choose an operator double r = rnd.NextDouble() * total; double current = 0; foreach (ObjectHolder <IEvolutionaryOperator> holder in ObjList) { if (holder.Obj.ParentsNeeded <= maxParents) { current += holder.Probability; if (r < current) { return(holder.Obj); } } } return(null); }
/// <summary> /// The constructor. /// </summary> /// <param name="theAlgorithm">The algorithm to fit.</param> /// <param name="theScore">The score function.</param> /// <param name="thePopulationSize">The population size.</param> public ContinuousACO(IMLMethod theAlgorithm, IScoreFunction theScore, int thePopulationSize) { Epsilon = .75; _algorithm = theAlgorithm; _populationSize = thePopulationSize; _score = theScore; Random = new MersenneTwisterGenerateRandom(); _paramCount = theAlgorithm.LongTermMemory.Length; _population = new ContinuousAnt[thePopulationSize * 2]; _weighting = new double[thePopulationSize]; for (int i = 0; i < _population.Length; i++) { _population[i] = new ContinuousAnt(_paramCount, _score.ShouldMinimize); for (int j = 0; j < _paramCount; j++) { _population[i].Params[j] = Random.NextDouble(-1, 1); } } UpdateScore(); Array.Sort(_population); ComputeWeighting(); SampleSolutions(); Array.Sort(_population); }
/// <summary> /// v = k * U(0,1) * v /// /// The components of the vector are multiplied /// by k and a random number. /// A new random number is generated for each /// component. /// </summary> /// <param name="rnd">Random number generator.</param> /// <param name="v">Array of doubles.</param> /// <param name="k">A scaler.</param> public static void MulRand(IGenerateRandom rnd, double[] v, double k) { for (int i = 0; i < v.Length; i++) { v[i] *= k * rnd.NextDouble(); } }
/// <summary> /// v = U(-1, 1) * maxValue /// <p/> /// Randomise each component of a vector to /// [-maxValue, maxValue]. /// </summary> /// <param name="rnd">Random number generator.</param> /// <param name="v">An array of doubles.</param> /// <param name="maxValue">Maximum value +/-.</param> public static void Randomise(IGenerateRandom rnd, double[] v, double maxValue) { for (int i = 0; i < v.Length; i++) { v[i] = (2 * rnd.NextDouble() - 1) * maxValue; } }
/// <summary> /// Randomize the properties between (-1,1). /// </summary> /// <param name="rnd">A random number generator.</param> public void Randomize(IGenerateRandom rnd) { for (int i = 0; i < _prop.Length; i++) { _prop[i] = rnd.NextDouble(-1, 1); } }
/// <summary> /// Randomly move to a new location. To specify a new randomization function, override this method. /// </summary> /// <param name="memory">The long term memory.</param> public void PerformRandomize(double[] memory) { for (int i = 0; i < memory.Length; i++) { memory[i] = _rnd.NextDouble(_lowRange, _highRange); } }
/// <inheritdoc /> public override void TrainingBatch(IGenerateRandom rnd) { for (var i = 0; i < _active.Length; i++) { _active[i] = rnd.NextDouble() > DropoutProbability; } }
public void Reset(IGenerateRandom rnd) { for (int i = 0; i < _weights.RowCount; i++) { for (int j = 0; j < _weights.ColumnCount; j++) { _weights[i, j] = rnd.NextDouble(-1, 1); } } }
public SOMColors() { InitializeComponent(); network = CreateNetwork(); gaussian = new NeighborhoodRBF(RBFEnum.Gaussian, WIDTH, HEIGHT); train = new BasicTrainSOM(network, 0.01, null, gaussian); train.ForceWinner = false; samples = AIFH.Alloc2D <double>(15, 3); for (int i = 0; i < 15; i++) { samples[i][0] = rnd.NextDouble(-1, 1); samples[i][1] = rnd.NextDouble(-1, 1); samples[i][2] = rnd.NextDouble(-1, 1); } train.SetAutoDecay(100, 0.8, 0.003, 30, 5); }
/// <summary> /// The constructor. /// </summary> /// <param name="rnd">A random number generator.</param> /// <param name="numConst">The number of constants.</param> /// <param name="numVar">The number of variables.</param> /// <param name="minConstValue">The minimum amount for a constant.</param> /// <param name="maxConstValue">The maximum amount for a constant.</param> public EvaluateExpression(IGenerateRandom rnd, int numConst, int numVar, double minConstValue, double maxConstValue) { constValues = new double[numConst]; varCount = numVar; for (int i = 0; i < constValues.Length; i++) { constValues[i] = rnd.NextDouble(minConstValue, maxConstValue); } }
/// <inheritdoc /> public void Iteration() { var len = _algorithm.LongTermMemory.Length; _k++; _currentTemperature = CoolingSchedule(); for (var cycle = 0; cycle < _cycles; cycle++) { // backup current state var oldState = new double[len]; Array.Copy(_algorithm.LongTermMemory, 0, oldState, 0, len); // randomize the method PerformRandomize(_algorithm.LongTermMemory); // did we improve it? Only keep the new method if it improved (greedy). var trialError = _score.CalculateScore(_algorithm); // was this iteration an improvement? If so, always keep. var keep = false; if (trialError < _currentError) { keep = true; } else { _lastProbability = CalcProbability(_currentError, trialError, _currentTemperature); if (_lastProbability > _rnd.NextDouble()) { keep = true; } } if (keep) { _currentError = trialError; // better than global error if (trialError < _globalBestError) { _globalBestError = trialError; Array.Copy(_algorithm.LongTermMemory, 0, oldState, 0, len); Array.Copy(_algorithm.LongTermMemory, 0, _globalBest, 0, len); } } else { Array.Copy(oldState, 0, _algorithm.LongTermMemory, 0, len); } } }
/// <inheritdoc /> public void PerformOperation(IGenerateRandom rnd, IGenome[] parents, int parentIndex, IGenome[] offspring, int offspringIndex) { var parent = (IArrayGenome)parents[parentIndex]; offspring[offspringIndex] = owner.Population.GenomeFactory.Factor(); var child = (IArrayGenome)offspring[offspringIndex]; child.Copy(parent); int length = parent.Count; var iswap1 = (int)(rnd.NextDouble() * length); var iswap2 = (int)(rnd.NextDouble() * length); // can't be equal if (iswap1 == iswap2) { // move to the next, but // don't go out of bounds if (iswap1 > 0) { iswap1--; } else { iswap1++; } } // make sure they are in the right order if (iswap1 > iswap2) { int temp = iswap1; iswap1 = iswap2; iswap2 = temp; } child.Swap(iswap1, iswap2); }
/// <summary> /// Run until state stabilizes. /// </summary> public void EstablishEquilibrium() { var count = NeuronCount; if (_on == null) { _on = new int[count]; _off = new int[count]; } for (var i = 0; i < count; i++) { _on[i] = 0; _off[i] = 0; } for (var n = 0; n < _runCycles * count; n++) { Run((int)_random.NextDouble(0, count - 1)); } for (var n = 0; n < _annealCycles * count; n++) { var i = (int)_random.NextDouble(0, count - 1); Run(i); if (CurrentState[i] > 0) { _on[i]++; } else { _off[i]++; } } for (var i = 0; i < count; i++) { CurrentState[i] = _on[i] > _off[i] ? 1 : 0; } }
/// <inheritdoc /> public void PerformOperation(IGenerateRandom rnd, IGenome[] parents, int parentIndex, IGenome[] offspring, int offspringIndex) { var parent = (IArrayGenome) parents[parentIndex]; offspring[offspringIndex] = owner.Population.GenomeFactory.Factor(); var child = (IArrayGenome) offspring[offspringIndex]; child.Copy(parent); int length = parent.Count; var iswap1 = (int) (rnd.NextDouble()*length); var iswap2 = (int) (rnd.NextDouble()*length); // can't be equal if (iswap1 == iswap2) { // move to the next, but // don't go out of bounds if (iswap1 > 0) { iswap1--; } else { iswap1++; } } // make sure they are in the right order if (iswap1 > iswap2) { int temp = iswap1; iswap1 = iswap2; iswap2 = temp; } child.Swap(iswap1, iswap2); }
/// <inheritdoc /> public void PerformOperation(IGenerateRandom rnd, IGenome[] parents, int parentIndex, IGenome[] offspring, int offspringIndex) { var parent = (DoubleArrayGenome) parents[parentIndex]; offspring[offspringIndex] = parent.Population.GenomeFactory.Factor(); var child = (DoubleArrayGenome) offspring[offspringIndex]; for (int i = 0; i < parent.Count; i++) { double value = parent.Data[i]; value += (perturbAmount - (rnd.NextDouble()*perturbAmount*2)); child.Data[i] = value; } }
/// <inheritdoc /> public void PerformOperation(IGenerateRandom rnd, IGenome[] parents, int parentIndex, IGenome[] offspring, int offspringIndex) { var parent = (DoubleArrayGenome)parents[parentIndex]; offspring[offspringIndex] = parent.Population.GenomeFactory.Factor(); var child = (DoubleArrayGenome)offspring[offspringIndex]; for (int i = 0; i < parent.Count; i++) { double value = parent.Data[i]; value += (perturbAmount - (rnd.NextDouble() * perturbAmount * 2)); child.Data[i] = value; } }
/// <summary> /// Randomize the weights of the neural network. /// </summary> public void Reset() { for (int i = 0; i < _rbm.Length; i++) { HiddenLayer layer = _layers[i]; double a = 1.0 / layer.InputCount; for (int j = 0; j < layer.OutputCount; j++) { for (int k = 0; k < layer.InputCount; k++) { layer.Weights[j][k] = Random.NextDouble(-a, a); } } } }
/// <summary> /// Select a probability distribution function (PDF). /// </summary> /// <returns>The PDF index.</returns> private int SelectPDF() { int l = 0; double temp = 0; double r = Random.NextDouble(); for (int i = 0; i < _populationSize; i++) { temp += _weighting[i] / _sumWeighting; if (r < temp) { l = i; break; } } return(l); }
/// <summary> /// Generate a random choice, but skip one of the choices. /// </summary> /// <param name="theGenerator">Random number generator.</param> /// <param name="skip">The choice to skip.</param> /// <returns>The random choice.</returns> public int Generate(IGenerateRandom theGenerator, int skip) { double totalProb = 1.0 - _probabilities[skip]; double throwValue = theGenerator.NextDouble() * totalProb; double accumulator = 0.0; for (int i = 0; i < skip; i++) { accumulator += _probabilities[i]; if (accumulator > throwValue) { return(i); } } for (int i = skip + 1; i < _probabilities.Length; i++) { accumulator += _probabilities[i]; if (accumulator > throwValue) { return(i); } } for (int i = 0; i < skip; i++) { if (_probabilities[i] != 0.0) { return(i); } } for (int i = skip + 1; i < _probabilities.Length; i++) { if (_probabilities[i] != 0.0) { return(i); } } return(-1); }
/// <summary> /// Generate a random choice, based on the probabilities provided to the constructor. /// </summary> /// <param name="theGenerator"></param> /// <returns>The random choice.</returns> public int Generate(IGenerateRandom theGenerator) { double r = theGenerator.NextDouble(); double sum = 0.0; for (int i = 0; i < _probabilities.Length; i++) { sum += _probabilities[i]; if (r < sum) { return(i); } } for (int i = 0; i < _probabilities.Length; i++) { if (_probabilities[i] != 0.0) { return(i); } } throw new AIFHError("Invalid probabilities."); }
/// <summary> /// Perform one training iteration. This will execute the specified number of cycles at the current /// temperature. /// </summary> public void Iteration() { // Is this the first time through, if so, then setup. if (_k == 0) { _currentScore = Evaluate(); FoundNewBest(); _globalBestScore = _currentScore; } // incrament the current iteration counter _k++; // obtain the correct temperature _currentTemperature = CoolingSchedule(); // perform the specified number of cycles for (int cycle = 0; cycle < _cycles; cycle++) { // backup current state BackupState(); // randomize the method MoveToNeighbor(); // did we improve it? Only keep the new method if it improved (greedy). double trialScore = Evaluate(); // was this iteration an improvement? If so, always keep. bool keep = false; if (trialScore < _currentScore) { // it was better, so always keep it keep = true; } else { // it was worse, so we might keep it _lastProbability = CalcProbability(_currentScore, trialScore, _currentTemperature); if (_lastProbability > _rnd.NextDouble()) { keep = true; } } // should we keep this position? if (keep) { _currentScore = trialScore; // better than global error if (trialScore < _globalBestScore) { _globalBestScore = trialScore; FoundNewBest(); } } else { // do not keep this position RestoreState(); } } }
/// <summary> /// Randomize the properties between (-1,1). /// </summary> /// <param name="rnd">A random number generator.</param> public void Randomize(IGenerateRandom rnd) { for (int i = 0; i < _prop.Length; i++) { _prop[i] = rnd.NextDouble(-1, 1); } }
/// <summary> /// The constructor. /// </summary> /// <param name="rnd">A random number generator.</param> /// <param name="numConst">The number of constants.</param> /// <param name="numVar">The number of variables.</param> /// <param name="minConstValue">The minimum amount for a constant.</param> /// <param name="maxConstValue">The maximum amount for a constant.</param> public EvaluateExpression(IGenerateRandom rnd, int numConst, int numVar, double minConstValue, double maxConstValue) { constValues = new double[numConst]; varCount = numVar; for (int i = 0; i < constValues.Length; i++) { constValues[i] = rnd.NextDouble(minConstValue, maxConstValue); } }
/// <summary> /// Generate a random choice, but skip one of the choices. /// </summary> /// <param name="theGenerator">Random number generator.</param> /// <param name="skip">The choice to skip.</param> /// <returns>The random choice.</returns> public int Generate(IGenerateRandom theGenerator, int skip) { var totalProb = 1.0 - _probabilities[skip]; var throwValue = theGenerator.NextDouble()*totalProb; var accumulator = 0.0; for (var i = 0; i < skip; i++) { accumulator += _probabilities[i]; if (accumulator > throwValue) { return i; } } for (var i = skip + 1; i < _probabilities.Length; i++) { accumulator += _probabilities[i]; if (accumulator > throwValue) { return i; } } for (var i = 0; i < skip; i++) { if (_probabilities[i] != 0.0) { return i; } } for (var i = skip + 1; i < _probabilities.Length; i++) { if (_probabilities[i] != 0.0) { return i; } } return -1; }
public void Reset(IGenerateRandom rnd) { for (var i = 0; i < _weights.RowCount; i++) { for (var j = 0; j < _weights.ColumnCount; j++) { _weights[i, j] = rnd.NextDouble(-1, 1); } } }
/// <summary> /// Randomize the long term memory, with the specified random number generator. /// </summary> /// <param name="rnd">A random number generator.</param> public void Reset(IGenerateRandom rnd) { for (int i = 0; i < _longTermMemory.Length; i++) { _longTermMemory[i] = rnd.NextDouble(-1, 1); } }
/// <summary> /// v = U(-1, 1) * maxValue /// <p /> /// Randomise each component of a vector to /// [-maxValue, maxValue]. /// </summary> /// <param name="rnd">Random number generator.</param> /// <param name="v">An array of doubles.</param> /// <param name="maxValue">Maximum value +/-.</param> public static void Randomise(IGenerateRandom rnd, double[] v, double maxValue) { for (var i = 0; i < v.Length; i++) { v[i] = (2*rnd.NextDouble() - 1)*maxValue; } }
/// <summary> /// The constructor. /// </summary> /// <param name="theAlgorithm">The algorithm to fit.</param> /// <param name="theScore">The score function.</param> /// <param name="thePopulationSize">The population size.</param> public ContinuousACO(IMLMethod theAlgorithm, IScoreFunction theScore, int thePopulationSize) { Epsilon = .75; _algorithm = theAlgorithm; _populationSize = thePopulationSize; _score = theScore; Random = new MersenneTwisterGenerateRandom(); _paramCount = theAlgorithm.LongTermMemory.Length; _population = new ContinuousAnt[thePopulationSize * 2]; _weighting = new double[thePopulationSize]; for (int i = 0; i < _population.Length; i++) { _population[i] = new ContinuousAnt(_paramCount, _score.ShouldMinimize); for (int j = 0; j < _paramCount; j++) { _population[i].Params[j] = Random.NextDouble(-1, 1); } } UpdateScore(); Array.Sort(_population); ComputeWeighting(); SampleSolutions(); Array.Sort(_population); }
/// <summary> /// v = k * U(0,1) * v /// The components of the vector are multiplied /// by k and a random number. /// A new random number is generated for each /// component. /// </summary> /// <param name="rnd">Random number generator.</param> /// <param name="v">Array of doubles.</param> /// <param name="k">A scaler.</param> public static void MulRand(IGenerateRandom rnd, double[] v, double k) { for (var i = 0; i < v.Length; i++) { v[i] *= k*rnd.NextDouble(); } }
void Crossover(IGenerateRandom rnd, UniverseRunner crossoverParent1, UniverseRunner crossoverParent2) { double[] parent1 = crossoverParent1.PhysicsRules.Data; double[] parent2 = crossoverParent2.PhysicsRules.Data; double[] child = PhysicsRules.Data; int len = parent1.Length; var p1 = (int) (rnd.NextDouble()*len); var p2 = (int) (rnd.NextDouble()*len); for (int i = 0; i < PhysicsRules.Data.Length; i++) { if (i < p1) { child[i] = parent1[i]; } else if (i >= p1 && i <= p2) { child[i] = parent2[i]; } else if (i > p2) { child[i] = parent1[i]; } } }
/// <summary> /// Generate a random choice, based on the probabilities provided to the constructor. /// </summary> /// <param name="theGenerator"></param> /// <returns>The random choice.</returns> public int Generate(IGenerateRandom theGenerator) { var r = theGenerator.NextDouble(); var sum = 0.0; for (var i = 0; i < _probabilities.Length; i++) { sum += _probabilities[i]; if (r < sum) { return i; } } for (var i = 0; i < _probabilities.Length; i++) { if (_probabilities[i] != 0.0) { return i; } } throw new AIFHError("Invalid probabilities."); }
/// <summary> /// Perform a mutate on a parent and generate a new child. The parent is not changed. /// </summary> /// <param name="rnd">Random number generator.</param> /// <param name="sourcePhysics">The parent object.</param> /// <param name="probChange">The probability of changing an individual element.</param> /// <param name="perturb">The amount that an object is changed by.</param> public void Mutate(IGenerateRandom rnd, IPhysics sourcePhysics, double probChange, double perturb) { PhysicsRules.CopyData(sourcePhysics.Data); for (int i = 0; i < sourcePhysics.Data.Length; i++) { if (rnd.NextDouble() < probChange) { PhysicsRules.Data[i] += perturb*rnd.NextDouble(-1, 1); } } }
/// <inheritdoc /> public object Sample(IGenerateRandom rnd) { return rnd.NextDouble(_start, _stop); }
/// <inheritdoc /> public object Sample(IGenerateRandom rnd) { return(rnd.NextDouble(_start, _stop)); }