public static uint[] ToFullTwoCycleArray(this IRando rando, uint order) { var aRet = uint.MaxValue.Repeat(order).ToArray(); var rem = order; if (order % 2 == 1) { var cd = rando.NextUint(rem); aRet[cd] = cd; rem--; } var curDex = 0u; while (rem > 0) { if (aRet[curDex] == uint.MaxValue) { var steps = rando.NextUint(rem - 1) + 1; var wr = WalkAndTag(aRet, curDex, steps); aRet[curDex] = wr; rem -= 2; } curDex++; } return(aRet); }
public static IEnumerable <ulong> ToUlongEnumerator(this IRando rando) { while (true) { var retVal = (ulong)rando.NextUint(); yield return((retVal << 32) + rando.NextUint()); } }
public static StageBits2 ToStageBits2(this IRando randy, uint order) { return(0u.CountUp(order * (order + 1) / 2) .Select(i => randy.NextUint()) .ToStageBits2(order: order, masks: 0u.CountUp(order) .Select(i => randy.NextUint()) )); }
public static Tuple <GenomeSorterStageDimer, GenomeSorterStageDimer> RecombineFine(this IRando randy, GenomeSorterStageDimer gdA, GenomeSorterStageDimer gdB) { var al = gdA.GenomeStageDimers.SelectMany(sd => sd.ToPermutations()).ToList(); var aList = gdA.GenomeStageDimers.ToList(); var bList = gdB.GenomeStageDimers.ToList(); // var combies = aList.Recombo(bList, randy.NextUint((uint)bList.Count())); var combies = aList.RecomboL2(bList, randy.NextUint((uint)bList.Count()), StageDimerExt.RecomboP(randy.NextUint(4u))); return(new Tuple <GenomeSorterStageDimer, GenomeSorterStageDimer>( combies.Item1.ToGenomeDimer(Guid.NewGuid()), combies.Item1.ToGenomeDimer(Guid.NewGuid()))); }
public static ISorter Mutate(this ISorter sorter, IRando rando, StageReplacementMode stageReplacementMode) { var mutantIndex = rando.NextUint(sorter.StageCount); var stageToReplace = sorter[(int)mutantIndex]; ISorterStage mutantStage = null; switch (stageReplacementMode) { case StageReplacementMode.RandomReplace: mutantStage = rando.ToFullSorterStage(order: sorter.Order, stageNumber: mutantIndex); break; case StageReplacementMode.RandomRewire: mutantStage = rando.RewireSorterStage(stageToReplace); break; case StageReplacementMode.RandomConjugate: mutantStage = stageToReplace.ConjugateByRandomPermutation(rando).ToSorterStage(mutantIndex); break; case StageReplacementMode.RCTC: mutantStage = stageToReplace.ConjugateByRandomSingleTwoCycle(rando).ToSorterStage(mutantIndex); break; default: throw new Exception($"{stageReplacementMode.ToString()}"); } return(sorter.SorterStages.ReplaceAtIndex(mutantIndex, mutantStage) .ToSorter(id: Guid.NewGuid(), genomeId: Guid.Empty)); }
public static StageDimer Mutate(this StageDimer genomeStageDimer, IRando randy) { var newStage1 = genomeStageDimer.Stage1; var newStage2 = genomeStageDimer.Stage2; var newModifier = genomeStageDimer.Modifier; var spot = randy.NextUint(3); switch (spot) { case 0: newStage1 = newStage1.ConjugateBy(randy.ToSingleTwoCyclePermutation(newStage1.Order)); break; case 1: newStage2 = newStage2.ConjugateBy(randy.ToSingleTwoCyclePermutation(newStage2.Order)); break; case 2: newModifier = newModifier.ConjugateBy(randy.ToSingleTwoCyclePermutation(newModifier.Order)); break; default: throw new Exception($"spot {spot} not handled in StageDimerExt.Mutate"); } return(new StageDimer( stage1: newStage1, stage2: newStage2, modifier: newModifier )); }
public static IEnumerable <uint> ToUintEnumerator(this IRando rando) { while (true) { yield return(rando.NextUint()); } }
public static GenomeSorterDual Mutate(this GenomeSorterDual genomeDualSorter, IRando randy) { var mutantIndex = randy.NextUint(genomeDualSorter.StageCount); var newChromA = genomeDualSorter.ChromosomeA; var newChromB = genomeDualSorter.ChromosomeB; var newChoices = genomeDualSorter.Choices; if (randy.NextBool(0.5)) { var mutantStage = randy.RewireSorterStage(genomeDualSorter.ChromoA(mutantIndex)); newChromA = newChromA.ReplaceAtIndex(mutantIndex, mutantStage); } else { var mutantStage = randy.RewireSorterStage(genomeDualSorter.ChromoB(mutantIndex)); newChromB = newChromB.ReplaceAtIndex(mutantIndex, mutantStage); } return(new GenomeSorterDual( id: Guid.NewGuid(), chromA: newChromA, chromB: newChromB, choices: newChoices)); }
public static Tuple <ISorter, ISorter> Recombine(this IRando randy, ISorter sorterA, ISorter sorterB) { var aList = sorterA.SorterStages.ToList(); var bList = sorterB.SorterStages.ToList(); var combies = aList.Recombo(bList, crossPt: randy.NextUint(sorterA.StageCount)); return(new Tuple <ISorter, ISorter>( combies.Item1.ToSorter(Guid.NewGuid(), Guid.Empty), combies.Item1.ToSorter(Guid.NewGuid(), Guid.Empty))); }
public static Tuple <GenomeSorterStageDimer, GenomeSorterStageDimer> RecombineCoarse(this IRando randy, GenomeSorterStageDimer gdA, GenomeSorterStageDimer gdB) { var aList = gdA.GenomeStageDimers.ToList(); var bList = gdB.GenomeStageDimers.ToList(); //var combies = randy.Recombo(aList, bList); var combies = aList.Recombo(bList, randy.NextUint((uint)bList.Count())); return(new Tuple <GenomeSorterStageDimer, GenomeSorterStageDimer>( combies.Item1.ToGenomeDimer(Guid.NewGuid()), combies.Item2.ToGenomeDimer(Guid.NewGuid()))); }
public static IEnumerable <uint> ToUintEnumerator(this IRando rando, uint maxVal) { var mask = Numbers.UintMask(maxVal.HiBit()); while (true) { var retVal = rando.NextUint() & mask; if (retVal < maxVal) { yield return(retVal); } } }
public static uint NextUint(this IRando rando, uint maxVal) { var mask = Numbers.UintMask(maxVal.HiBit()); while (true) { var retVal = rando.NextUint() & mask; if (retVal < maxVal) { return(retVal); } } }
public static Tuple <GenomeSorterDual, GenomeSorterDual> Recombine( this GenomeSorterDual genomeDualSorterA, GenomeSorterDual genomeDualSorterB, IRando randy) { var cA = genomeDualSorterA.ChromosomeA.ToList(); var cB = genomeDualSorterA.ChromosomeB.ToList(); var recombin1 = cA.Recombo(cB, randy.NextUint((uint)cA.Count())); var recombin2 = cB.Recombo(cA, randy.NextUint((uint)cA.Count())); if (randy.NextBool(0.5)) { return(new Tuple <GenomeSorterDual, GenomeSorterDual>( item1: new GenomeSorterDual( id: Guid.NewGuid(), chromA: recombin1.Item1, chromB: recombin1.Item2, choices: genomeDualSorterA.Choices), item2: new GenomeSorterDual( id: Guid.NewGuid(), chromA: recombin2.Item1, chromB: recombin2.Item2, choices: genomeDualSorterA.Choices) )); } return(new Tuple <GenomeSorterDual, GenomeSorterDual>( item1: new GenomeSorterDual( id: Guid.NewGuid(), chromA: recombin1.Item2, chromB: recombin1.Item1, choices: genomeDualSorterA.Choices), item2: new GenomeSorterDual( id: Guid.NewGuid(), chromA: recombin2.Item2, chromB: recombin2.Item1, choices: genomeDualSorterA.Choices) )); }
public static GenomeSorterDual RecombineI(this GenomeSorterDual genomeDualSorter, IRando randy) { var kk = randy.NextUint(genomeDualSorter.StageCount); var recombines = genomeDualSorter.ChromosomeA.ToList().Recombo( genomeDualSorter.ChromosomeB.ToList(), kk); return(new GenomeSorterDual( id: Guid.NewGuid(), chromA: recombines.Item1.ToList(), chromB: recombines.Item2.ToList(), choices: genomeDualSorter.Choices)); }
public IGene Mutate(IRando rando) { return(GeneUintModN.Make(rando.NextUint(MaxVal), MaxVal)); }
public static ulong NextUlong(this IRando rando) { var mask = (ulong)rando.NextUint(); return((mask << 32) + rando.NextUint()); }
public override IGeneUintModN NewBlock(IRando rando) { return(GeneUintModN.Make(rando.NextUint(MaxVal), MaxVal)); }