protected override void SolveInstance(IGH_DataAccess DA) { int populationSize = 50; int chromosomeLength = 10; IChromosome ancestor = new BinaryChromosome(chromosomeLength); //population = new Population(populationSize, ancestor, ) }
private FunctionResult GetFunctionResultForChromosomes(BinaryChromosome chromosome1, BinaryChromosome chromosome2, Range range) { var x1Best = BinaryUtils.BinaryToDecimalRepresentation(chromosome1.BinaryRepresentation, range, NumberOfBitsInChromosome); var x2Best = BinaryUtils.BinaryToDecimalRepresentation(chromosome2.BinaryRepresentation, range, NumberOfBitsInChromosome); return(new FunctionResult(Function.Compute(x1Best, x2Best), x1Best, x2Best)); }
public double Evaluate(IChromosome chromosome) { BinaryChromosome knapsackConfiguration = (BinaryChromosome)chromosome; int length = knapsackConfiguration.Length; ulong value = knapsackConfiguration.Value; for (int i = 0; i < length; i++) { if ((value & TestBitsMask[i]) == 0) { _bag.RemoveItem(_items[i]); } else { _bag.InsertItem(_items[i]); } } if (_bag.AcceptableWeight()) { return(_bag.ItemsCost()); } return(0); }
public void PointMutationTest() { IChromosome<bool> a = new BinaryChromosome().GenerateFromArray(new bool[] { false, true, false, true, false }); PointMutation mutation = new PointMutation(1); IChromosome<bool> res = mutation.Mutate(a); IChromosome<bool> exp = new BinaryChromosome().GenerateFromArray(new bool[] { true, false, true, false, true }); Assert.AreEqual(exp, res); }
public void SwapMutationTest() { IChromosome<bool> a = new BinaryChromosome().GenerateFromArray(new bool[] { false, true, false, true }); SwapMutation<bool> mutation = new SwapMutation<bool>(0, 3); IChromosome<bool> res = mutation.Mutate(a); IChromosome<bool> exp = new BinaryChromosome().GenerateFromArray(new bool[] { true, true, false, false }); Assert.AreEqual(exp, res); }
public void EasyMutation() { IChromosome<bool> a = new BinaryChromosome().GenerateFromArray(new bool[] { false, true, false, true }); EasyMutation mutation = new EasyMutation(2); IChromosome<bool> res = mutation.Mutate(a); IChromosome<bool> exp = new BinaryChromosome().GenerateFromArray(new bool[] { false, true, true, true }); Assert.AreEqual(exp, res); }
private BinaryChromosome Mutate(BinaryChromosome binaryChromosome) { var binaryRepresentation = binaryChromosome.BinaryRepresentation; StringBuilder newBinaryRepresentation = new StringBuilder(binaryRepresentation); newBinaryRepresentation.Remove(binaryRepresentation.Length - 1, 1); newBinaryRepresentation.Append(binaryRepresentation[binaryRepresentation.Length - 1] == '0' ? '1' : '0'); return(new BinaryChromosome(newBinaryRepresentation.ToString())); }
private BinaryChromosome Mutate(BinaryChromosome binaryChromosome) { var binaryRepresentation = binaryChromosome.BinaryRepresentation; StringBuilder newBinaryRepresentation = new StringBuilder(binaryRepresentation); int index = new Random().Next(0, newBinaryRepresentation.Length); newBinaryRepresentation[index] = newBinaryRepresentation[index] == '0' ? '1' : '0'; return(new BinaryChromosome(newBinaryRepresentation.ToString())); }
private void VerifyChangeOccured(BinaryChromosome x, BinaryChromosome y) { var diff = 0; for (var i = 0; i < GENE_COUNT; i++) { if (x.Bits[i] != y.Bits[i]) { diff++; } } Assert.IsTrue(diff > 0, "Mutation failed to change chromosome data."); }
private BinaryChromosome Mutate(BinaryChromosome binaryChromosome) { var binaryRepresentation = binaryChromosome.BinaryRepresentation; StringBuilder newBinaryRepresentation = new StringBuilder(binaryRepresentation); Random rand = new Random(); int index1 = rand.Next(0, newBinaryRepresentation.Length); int index2 = index1; while (index1 == index2) { index2 = rand.Next(0, newBinaryRepresentation.Length); } newBinaryRepresentation[index1] = newBinaryRepresentation[index1] == '0' ? '1' : '0'; newBinaryRepresentation[index2] = newBinaryRepresentation[index2] == '0' ? '1' : '0'; return(new BinaryChromosome(newBinaryRepresentation.ToString())); }
//评价染色体,计算它的适应度,输出迭代的进度 public double Evaluate(IChromosome chromosome) { Common.services.Clear(); BinaryChromosome realDude = ((BinaryChromosome)chromosome); string genome;//基因组 genome = realDude.Value.ToString(); Phenotype p = new Phenotype(); p.init(genome); double fit = p.getFitness(); /* * PROGRESS += 1 / GPROGRESS; * evolutionProgress = PROGRESS * 100; */ return(1 / fit); }
private BinaryChromosome PerformInversion(BinaryChromosome binaryChromosome) { var binaryRepresentation = binaryChromosome.BinaryRepresentation; var length = binaryRepresentation.Length; Random random = new Random(); var startRange = random.Next(0, length - 1); var endRange = random.Next(startRange + 1, length + 1); var subBinaryRepresentation = new StringBuilder(binaryRepresentation.Substring(startRange, endRange - startRange)); for (int i = 0; i < subBinaryRepresentation.Length; i++) { subBinaryRepresentation[i] = subBinaryRepresentation[i] == '0' ? subBinaryRepresentation[i] = '1' : subBinaryRepresentation[i] = '0'; } var newChromosome = new BinaryChromosome(new StringBuilder() .Append(binaryRepresentation.Substring(0, startRange)) .Append(subBinaryRepresentation.ToString()) .Append(binaryRepresentation.Substring(endRange)) .ToString()); return(newChromosome); }
private Tuple <int, int> GetDifferenceRange(BinaryChromosome chromosome, BinaryChromosome mutation) { var start = 0; var end = 0; for (var i = 0; i < GENE_COUNT; i++) { if (mutation.Bits[i] != chromosome.Bits[i]) { start = i; while (i < GENE_COUNT) { if (mutation.Bits[i] != chromosome.Bits[i]) { end = i; } i++; } } } return(new Tuple <int, int>(start, end)); }
private void Test2_Click(object sender, RoutedEventArgs e) { BinaryChromosome.MutationOperator = new PathDirectionMutation(_viewModel.CurrentBuilding, 1); BinaryChromosome.Transformer = new ThreeSegmentLoopOptimizer(_viewModel.CurrentBuilding); BinaryChromosome c = new BinaryChromosome(_viewModel.CurrentBuilding.GetFenotype().ToGenotype()); c.Transform(); _viewModel.CurrentBuilding.SetFenotype(c.Genotype.ToFenotype()); _viewModel.CurrentBuilding.DrawSolution(); }
private BinaryChromosome GetMutation(BinaryChromosome chromosome, BinaryMutation mutation) { var rator = GeneticFactory.ConstructBinaryMutationOperators(mutation).First(); return(rator.Invoke(chromosome) as BinaryChromosome); }
private BinaryChromosome GetCrossover(BinaryChromosome x, BinaryChromosome y, BinaryCrossover crossover) { var rator = GeneticFactory.ConstructBinaryCrossoverOperators(crossover).First(); return(rator.Invoke(x, y) as BinaryChromosome); }
protected MemberBinaryChromosome(BinaryChromosome source) : base(source) { }