/// <summary> /// Randomizes the chromosome. /// </summary> public void Randomize() { TradeSystemChromosome newChrom = new TradeSystemChromosome(this.ChromosomeValue, this.indicatorHelper, size); int i = 1; Random rand = new Random(); string indicatorbuy1 = null; string indicatorbuy2 = null; IndicatorCompareType mixbuy = new IndicatorCompareType(); this.indicatorHelper.GetRandomIndicators(out indicatorbuy1, out mixbuy, out indicatorbuy2); this.ChromosomeValue.BuyCondition.TradeRules.Add(TradeRuleFactory.GenerateTradeRule(indicatorbuy1, mixbuy, indicatorbuy2)); while (i < size && rand.Next(100) > 40) { string indicator1 = null; string indicator2 = null; IndicatorCompareType mix = new IndicatorCompareType(); this.ChromosomeValue.BuyCondition.RuleJoinTypes.Add(indicatorHelper.GetRandomRuleJoinType()); indicatorHelper.GetRandomIndicators(out indicator1, out mix, out indicator2); this.ChromosomeValue.BuyCondition.TradeRules.Add(TradeRuleFactory.GenerateTradeRule(indicator1, mix, indicator2)); i++; } this.Clone(); this.newID(); }
/// <summary> /// Returns a new population based on the breeding algorithm. /// </summary> /// <param name="oldChromosomes">The chromosomes of a population.</param> /// <returns>A new population.</returns> public TradeSystemChromosome[] Breed(TradeSystemChromosome[] oldChromosomes) { TradeSystemChromosome[] newChromosomes = new TradeSystemChromosome[oldChromosomes.Length]; /// ********************************************* /// GDBCup - Code goes here /// ********************************************* return(newChromosomes); }
/// <summary> /// Cross the buy trade conditions. /// </summary> /// <param name="chromosome1">First chromosome to cross.</param> /// <param name="chromosome2">Second chromosome to cross.</param> private void CrossBuyTradeConditions(TradeSystemChromosome chromosome1, TradeSystemChromosome chromosome2) { int c1 = GetRandomPosition(chromosome1.ChromosomeValue.BuyCondition.TradeRules.Count); int c2 = GetRandomPosition(chromosome2.ChromosomeValue.BuyCondition.TradeRules.Count); TradeRule c1tr = chromosome1.ChromosomeValue.BuyCondition.TradeRules[c1]; TradeRule c2tr = chromosome2.ChromosomeValue.BuyCondition.TradeRules[c2]; chromosome1.ChromosomeValue.BuyCondition.TradeRules[c1] = c2tr; chromosome2.ChromosomeValue.BuyCondition.TradeRules[c2] = c1tr; }
/// <summary> /// Initializes a new instance of the GeneticAlgorithm class. /// </summary> /// <param name="chromosomes">The number of chromosomes in the population.</param> /// <param name="generations">The number of generations to run.</param> /// <param name="chromosomeSize">The size of the chromosomes.</param> /// <param name="tradeSystemTemplate">A template to make TradeSystemChromosomes with</param> /// <param name="indicatorHelper">The indicator helper for generating new chromosomes.</param> /// <param name="breedingAlgorithm">The algorithm to breed with.</param> /// <param name="mutatingAlgorithm">The algorithm to mutate with.</param> /// <param name="fitnessAlgorithm">The algorithm to calculate fitness.</param> public GeneticAlgorithm(int chromosomes, int generations, int chromosomeSize, TradeSystem tradeSystemTemplate, IndicatorHelper indicatorHelper, IGeneticBreeding breedingAlgorithm, IGeneticMutating mutatingAlgorithm, IGeneticFitness fitnessAlgorithm) { this.chromosomes = new TradeSystemChromosome[chromosomes]; for (int i = 0; i < chromosomes; i++) { TradeSystemChromosome newchromosome = new TradeSystemChromosome(tradeSystemTemplate, indicatorHelper, chromosomeSize); this.chromosomes[i] = newchromosome; } this.generations = generations; this.breedingAlgorithm = breedingAlgorithm; this.fitnessAlgorithm = fitnessAlgorithm; this.mutatingAlgorithm = mutatingAlgorithm; GeneratePopulation(); }
/// <summary> /// Run the genetic algorithm. /// </summary> /// <param name="worker">The background worker object.</param> /// <param name="e">Do work event args.</param> public void Run(BackgroundWorker worker, DoWorkEventArgs e) { fitnessAlgorithm.SetFitness(chromosomes); Dictionary <string, double> fitnessResult = fitnessAlgorithm.getFitness(); IEnumerable <KeyValuePair <string, double> > temp = fitnessResult.OrderByDescending(d => d.Value); chromosomes = chromosomes.OrderByDescending(d => d.Capital).ToArray(); bestChromosome = DeepCopy.getDeepCopy(chromosomes[0]); chromosomes = breedingAlgorithm.Breed(chromosomes); mutatingAlgorithm.Mutate(ref chromosomes); for (int j = 0; j < chromosomes.Count(); j++) { chromosomes[j].newID(); } }
/// <summary> /// Creates a new instance of the chromosome with the same values. /// </summary> /// <returns>A new instance of the chromosome with the same values.</returns> internal TradeSystemChromosome Clone() { TradeCondition nBuy = new TradeCondition(); nBuy = this.ChromosomeValue.BuyCondition.Clone(); TradeSystemChromosome tc = new TradeSystemChromosome( new TradeSystem( nBuy), this.indicatorHelper, this.size); tc.Capital = this.Capital; tc.FitnessLosers = this.FitnessLosers; tc.FitnessWinners = this.FitnessWinners; tc.Fitness = this.Fitness; tc.UniqueID = this.UniqueID; tc.TradeTable = this.TradeTable; return(tc); }
/// <summary> /// Cross the sell trade conditions. /// </summary> /// <param name="chromosome1">First chromosome to cross.</param> /// <param name="chromosome2">Second chromosome to cross.</param> /// <param name="whereToSplit">The place to split the chromosomes.</param> private void CrossSellTradeConditions(TradeSystemChromosome chromosome1, TradeSystemChromosome chromosome2, int whereToSplit) { /// ********************************************* /// GDBCup - Code goes here /// ********************************************* }
/// <summary> /// Mutate the chrmosome. /// type identifies what will be mutated /// x=1 is an indicator /// x=2 is a comparetype /// x=3 is a join rule /// buysell identifies which traderule will be mutated /// x = 1 buyrule /// x = 2 sellrule /// </summary> /// <param name="tradeSystemChromosome">The chromosome to mutate.</param> private void Mutate(TradeSystemChromosome tradeSystemChromosome) { int type = rand.Next(3); int buysell = rand.Next(2); switch (buysell) { case 1: switch (type) { case 1: if (tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules.Count == 0) { if (rand.Next(2) == 1) { tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[0].IndicatorName1 = indicatorHelper.mutateIndicator(tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[0].IndicatorName2, tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[0].CompareType); } else { tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[0].IndicatorName2 = indicatorHelper.mutateIndicator(tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[0].IndicatorName1, tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[0].CompareType); } } else { int location = rand.Next(tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules.Count); if (rand.Next(2) == 1) { tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[location].IndicatorName1 = indicatorHelper.mutateIndicator(tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[location].IndicatorName2, tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[location].CompareType); } else { tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[location].IndicatorName2 = indicatorHelper.mutateIndicator(tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[location].IndicatorName1, tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[location].CompareType); } } break; case 2: if (tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules.Count == 0) { tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[0].CompareType = indicatorHelper.mutateCompareType(); } else { int location = rand.Next(tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules.Count); tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[location].CompareType = indicatorHelper.mutateCompareType(); } break; case 3: if (tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules.Count == 0) { tradeSystemChromosome.ChromosomeValue.BuyCondition.RuleJoinTypes[0] = indicatorHelper.mutateRuleJoinType(); } else { int location = rand.Next(tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules.Count); tradeSystemChromosome.ChromosomeValue.BuyCondition.RuleJoinTypes[location] = indicatorHelper.mutateRuleJoinType(); } break; default: if (tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules.Count == 0) { if (rand.Next(2) == 1) { tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[0].IndicatorName1 = indicatorHelper.mutateIndicator(tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[0].IndicatorName2, tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[0].CompareType); } else { tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[0].IndicatorName2 = indicatorHelper.mutateIndicator(tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[0].IndicatorName1, tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[0].CompareType); } } else { int location = rand.Next(tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules.Count); if (rand.Next(2) == 1) { tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[location].IndicatorName1 = indicatorHelper.mutateIndicator(tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[location].IndicatorName2, tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[location].CompareType); } else { tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[location].IndicatorName2 = indicatorHelper.mutateIndicator(tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[location].IndicatorName1, tradeSystemChromosome.ChromosomeValue.BuyCondition.TradeRules[location].CompareType); } } break; } break; default: break; } }