/// <summary> /// Parameterized Constructor /// </summary> /// <param name="chromosome"></param> public SimpleStockTraderChromosome(SimpleStockTraderChromosome chromosome) { _ranges = (Range[])chromosome.Ranges.Clone(); _length = _ranges.Length; _values = (double[])chromosome.Values.Clone(); InitializeGenerators(); }
/// <summary> /// Equal override method /// </summary> /// <param name="obj"></param> /// <returns></returns> public override bool Equals(object obj) { if (obj is SimpleStockTraderChromosome) { SimpleStockTraderChromosome chromosome = obj as SimpleStockTraderChromosome; if (chromosome.fitness != fitness) { return(false); } for (int i = 0; i < _length; i++) { if (_values[i] != chromosome.Values[i]) { return(false); } } return(true); } return(false); }
/// <summary> /// Perform two point crossover between seletected pair /// </summary> /// <param name="pair"></param> public override void Crossover(IChromosome pair) { //If length is less than 3 two point crossover cannot be performed. if (_length < 3) { int xoverpoint = (int)Math.Ceiling(_random.NextDouble() * (_length - 1)); SinglePointCrossover(pair, xoverpoint); return; } SimpleStockTraderChromosome chromosome = (SimpleStockTraderChromosome)pair; int left = _random.Next(_length - 1); int right = _random.Next(_length - 1); while (left == right) { right = _random.Next(_length - 1); } if (left > right) { double[] temp = new double[_length]; // copy part of first (this) chromosome to temp Array.Copy(_values, temp, _length); // copy part of second (pair) chromosome to the first Array.Copy(chromosome.Values, right, _values, right, left - right); // copy temp to the second Array.Copy(temp, right, chromosome.Values, right, left - right); } else { double[] temp = new double[_length]; // copy part of first (this) chromosome to temp Array.Copy(_values, temp, _length); // copy part of second (pair) chromosome to the first Array.Copy(chromosome.Values, left, _values, left, right - left); // copy temp to the second Array.Copy(temp, left, chromosome.Values, left, right - left); } }
/// <summary> /// Single point crossover /// </summary> public void SinglePointCrossover(IChromosome pair, int xoverpoint) { SimpleStockTraderChromosome chromosome = (SimpleStockTraderChromosome)pair; Array.Copy(chromosome.Values, xoverpoint, _values, xoverpoint, _length - xoverpoint); }