public void TestRandomTupleListSorting() { const int len = 0x1 << 10; SystemRandomSource random = new SystemRandomSource(); List <int> keys = new List <int>(len); List <int> items = new List <int>(len); int[] keysCopy = new int[len]; for (int i = 0; i < len; i++) { int value = random.Next(); keys.Add(value); keysCopy[i] = value; items.Add(-value); } Sorting.Sort(keys, items); for (int i = 1; i < len; i++) { Assert.That(keys[i] >= keys[i - 1], "Sort Order - " + i.ToString()); Assert.That(items[i], Is.EqualTo(-keys[i]), "Items Permutation - " + i.ToString()); } for (int i = 0; i < keysCopy.Length; i++) { Assert.That(keys.IndexOf(keysCopy[i]) >= 0, "All keys still there - " + i.ToString()); } }
public void TestRandomTupleArraySorting() { const int len = 0x1 << 10; SystemRandomSource random = new SystemRandomSource(); int[] keys = new int[len]; int[] items = new int[len]; int[] keysCopy = new int[len]; for (int i = 0; i < keys.Length; i++) { keys[i] = random.Next(); keysCopy[i] = keys[i]; items[i] = -keys[i]; } Sorting.Sort(keys, items); for (int i = 1; i < keys.Length; i++) { Assert.That(keys[i] >= keys[i - 1], "Sort Order - " + i.ToString()); Assert.That(items[i], Is.EqualTo(-keys[i]), "Items Permutation - " + i.ToString()); } for (int i = 0; i < keysCopy.Length; i++) { Assert.That(Array.IndexOf(keys, keysCopy[i]) >= 0, "All keys still there - " + i.ToString()); } }
public void TestRandomTripleArraySorting() { const int len = 0x1 << 10; SystemRandomSource random = new SystemRandomSource(); int[] keys = new int[len]; int[] items1 = new int[len]; int[] items2 = new int[len]; int[] keysCopy = new int[len]; for (int i = 0; i < keys.Length; i++) { keys[i] = random.Next(); keysCopy[i] = keys[i]; items1[i] = -keys[i]; items2[i] = keys[i] >> 2; } Sorting.Sort(keys, items1, items2); for (int i = 1; i < keys.Length; i++) { Assert.IsTrue(keys[i] >= keys[i - 1], "Sort Order - " + i.ToString()); Assert.AreEqual(-keys[i], items1[i], "Items1 Permutation - " + i.ToString()); Assert.AreEqual(keys[i] >> 2, items2[i], "Items2 Permutation - " + i.ToString()); } for (int i = 0; i < keysCopy.Length; i++) { Assert.IsTrue(Array.IndexOf(keys, keysCopy[i]) >= 0, "All keys still there - " + i.ToString()); } }
/// <summary> /// Gets a normal-like distribution from the 2D array /// </summary> /// <param name="rand">Random source component</param> /// <returns>Array of floats that sum <= 1</returns> public static float[] GetNormalDistribution(SystemRandomSource rand) { float[][] ret_val = { new float[] { 75.0f, 17.0f, 5.0f, 2.0f, 1.0f }, new float[] { 68.2f, 27.2f, 4.2f, 0.2f, 0.2f }, new float[] { 52.6875f, 25.0f, 12.75f, 6.375f, 3.1875f }, new float[] { 39.4f, 30.0f, 18.4f, 8.8f, 3.4f } }; return(ret_val[rand.Next(ret_val.Length)]); }
/// <summary> /// Shuffles elements in a list /// <!-- https://stackoverflow.com/a/1262619/5492446 --> /// </summary> /// <typeparam name="T">Template type</typeparam> /// <param name="list">The list this is called on</param> /// <param name="rand">Random source component</param> public static void Shuffle <T>(this IList <T> list, SystemRandomSource rand) { int n = list.Count; while (n > 1) { // Decrements n before evaluating rand.Next int k = rand.Next(--n + 1); T value = list[k]; list[k] = list[n]; list[n] = value; } }
/** * \brief Two-point crossover * \details Two-point crossover calls for two points to be selected on the parent organism. Everything between the two points is swapped between the parent organisms, rendering two child organisms * ![Genetic Algorithm Crossover Two-point](./media/genetic/crossover.png)<!-- .element height="50%" width="50%" --> * \param[in] idFirst Index of the first parent * \param[in] idSecond Index of the second parent * \param[in] sliceIndexA Index of the first swap point * \param[in] sliceIndexB Index of the second swap point */ public void CrossoverSliceTwoIndividuals(int idFirst, int idSecond, int sliceIndexA = 0, int sliceIndexB = 0) { if (sliceIndexA == 0 && sliceIndexB == 0) { sliceIndexA = rndGenerator.Next(1, individualSize - 1); sliceIndexB = rndGenerator.Next(1, individualSize - 1); } if (sliceIndexA > sliceIndexB) { int tmp = sliceIndexA; sliceIndexA = sliceIndexB; sliceIndexB = tmp; } sliceA = population.Column(idFirst, 0, sliceIndexA); sliceB = population.Column(idFirst, sliceIndexB, individualSize - sliceIndexB); population.SetColumn(idFirst, 0, sliceIndexA, population.Column(idSecond, 0, sliceIndexA)); population.SetColumn(idFirst, sliceIndexB, individualSize - sliceIndexB, population.Column(idSecond, sliceIndexB, individualSize - sliceIndexB)); population.SetColumn(idSecond, 0, sliceIndexA, sliceA); population.SetColumn(idSecond, sliceIndexB, individualSize - sliceIndexB, sliceB); }
public void TestAppliedSetSorting() { const int len = 0x1 << 10; SystemRandomSource random = new SystemRandomSource(); Set <int> set = new Set <int>(); for (int i = 0; i < len; i++) { set.Add(random.Next()); } // default sorting (Ascending) set.Sort(); // just check that the order is as expected, not that the items are correct for (int i = 1; i < set.Count; i++) { Assert.That(set[i] >= set[i - 1], "Sort Order - " + i.ToString()); } }
void Loop() { channel = alice.Connect(Thread.CurrentThread.ManagedThreadId); var package = new Package(); package.Type = Package.PackageType.RequestKetEncArray; channel.ClientSend(package); for (;;) { package = channel.ClientReceive(); if (package == null) { continue; } switch (package.Type) { case Package.PackageType.ResponseKetEncArray: { package.Type = Package.PackageType.RequestCorrectBroadArray; arrayLength = package.ArrayLength; package.MeasureRawArray = new byte[arrayLength]; resultMeasureArray = new byte[arrayLength]; var RandomGen = new SystemRandomSource(true); for (var i = 0; i < arrayLength; i++) { package.MeasureRawArray[i] = (byte)RandomGen.Next(2); if (package.MeasureRawArray[i] == 0) { if (package.KetEncArray[i].MeasuHResultIndex(zeroOneMeasure) != 0) { resultMeasureArray[i] = 1; } } else { if (package.KetEncArray[i].MeasuHResultIndex(plusminusMeasure) != 0) { resultMeasureArray[i] = 1; } } } channel.ClientSend(package); } break; case Package.PackageType.ResponseCorrectBroadArray: { var finalBobKeyList = new List <byte>(); for (var i = 0; i < arrayLength; i++) { if (package.CorrectBroadArray[i] != 0) { finalBobKeyList.Add(resultMeasureArray[i]); } } var finalBobKey = finalBobKeyList.ToArray(); lock (alice) { Console.Write($"FinalBobKey for {Thread.CurrentThread.ManagedThreadId}\t"); foreach (var b in finalBobKey) { Console.Write(b); } Console.WriteLine(); } } alice.Disconnect(Thread.CurrentThread.ManagedThreadId); return; } Thread.Sleep(100); } }
void Loop() { for (;;) { foreach (var pair in clients) { var package = pair.Value.Channel.ServerReceive(); if (package == null) { continue; } switch (package.Type) { case Package.PackageType.RequestKetEncArray: { package.Type = Package.PackageType.ResponseKetEncArray; package.ArrayLength = arrayLength; package.KetEncArray = new Ket[arrayLength]; pair.Value.RawKeyArray = new byte[arrayLength]; pair.Value.BasisRawArray = new byte[arrayLength]; package.KetEncArray = new Ket[arrayLength]; var complexArray = new Complex[2]; var RandomGen = new SystemRandomSource(true); for (var i = 0; i < arrayLength; i++) { pair.Value.RawKeyArray[i] = (byte)RandomGen.Next(2); pair.Value.BasisRawArray[i] = (byte)RandomGen.Next(2); if (pair.Value.BasisRawArray[i] == 0 && pair.Value.RawKeyArray[i] == 0) { complexArray[0] = new Complex(1, 0); complexArray[1] = new Complex(0, 0); package.KetEncArray[i] = new Ket(complexArray); } else if (pair.Value.BasisRawArray[i] == 0 && pair.Value.RawKeyArray[i] == 1) { complexArray[0] = new Complex(0, 0); complexArray[1] = new Complex(1, 0); package.KetEncArray[i] = new Ket(complexArray); } else if (pair.Value.BasisRawArray[i] == 1 && pair.Value.RawKeyArray[i] == 0) { complexArray[0] = new Complex(1 / Sqrt(2), 0); complexArray[1] = new Complex(1 / Sqrt(2), 0); package.KetEncArray[i] = new Ket(complexArray); } else if (pair.Value.BasisRawArray[i] == 1 && pair.Value.RawKeyArray[i] == 1) { complexArray[0] = new Complex(1 / Sqrt(2), 0); complexArray[1] = new Complex(-1 / Sqrt(2), 0); package.KetEncArray[i] = new Ket(complexArray); } } pair.Value.Channel.ServerSend(package); } break; case Package.PackageType.RequestCorrectBroadArray: { package.Type = Package.PackageType.ResponseCorrectBroadArray; package.CorrectBroadArray = new byte[arrayLength]; for (var i = 0; i < arrayLength; i++) { if (package.MeasureRawArray[i] == pair.Value.BasisRawArray[i]) { package.CorrectBroadArray[i] = 1; } } pair.Value.Channel.ServerSend(package); var finalAliceKeyList = new List <byte>(); for (var i = 0; i < arrayLength; i++) { if (package.CorrectBroadArray[i] != 0) { finalAliceKeyList.Add(pair.Value.RawKeyArray[i]); } } var finalAliceKey = finalAliceKeyList.ToArray(); lock (this) { Console.Write($"FinalAliceKey for {pair.Key}\t"); foreach (var b in finalAliceKey) { Console.Write(b); } Console.WriteLine(); } } break; } } if (exitFlag) { return; } Thread.Sleep(100); } }
public int NextInt() { return(random.Next()); }
public void Run() { IPriorityQueue <Event> events = new IntervalHeap <Event>(); SystemRandomSource random = new SystemRandomSource(); Exponential nextStoryStartedDistribution = new Exponential(_newStoryRate, random); events.Add(new Event(0, 0, new EventValue(EventType.NewStory))); events.Add(new Event(0, 0, new EventValue(EventType.Sample))); SimulationTime = 0; int?stopOnCompletedStories = null; double simulationWarmupDays = 30; while (!events.IsEmpty) { if (stopOnCompletedStories == null && SimulationTime > simulationWarmupDays) { stopOnCompletedStories = _simulatedStoryCycleTimes.Count + _expectedCompletedStories; } if (_simulatedStoryCycleTimes.Count >= stopOnCompletedStories) { break; } var evnt = events.DeleteMin(); SimulationTime = evnt.Time; var newEvents = ProcessEvent(evnt); foreach (var newEvent in newEvents) { double timeOffset; switch (newEvent.Distribution) { case Distribution.Immediate: timeOffset = 0; break; case Distribution.Unit: timeOffset = 1; break; case Distribution.NextStory: timeOffset = nextStoryStartedDistribution.Sample(); break; case Distribution.StoryCycleTime: timeOffset = _storyCycleTimes[random.Next(0, _storyCycleTimes.Length)]; break; default: throw new ArgumentOutOfRangeException(); } events.Add(new Event(SimulationTime + timeOffset, SimulationTime, newEvent.Value)); } } SimulationTime -= simulationWarmupDays; AverageWorkInProgress = _storiesInProgressSample.Average(); }