private void WorkerThreadStart() { Initialize(); while (isRunning) { //assign current work data to a tmp var, so that we use the same data through the entire itteration //even if data is changed by the main thread, nothing will break or go wrong. WorkerData tmpData = data; if (tmpData.hasNewParent) { Tools.InitRandom(tmpData.randomSeed); tmpData.hasNewParent = false; } DnaDrawing newDrawing = GetMutatedSeedSyncedDrawing(); double newErrorLevel = FitnessCalculator.GetDrawingFitness(newDrawing, info.SourceImage, partitionY, partitionHeight); var result = new DnaPartitionResult { Drawing = newDrawing, ErrorLevel = newErrorLevel, }; tmpData.workerTail.Enqueue(result); } }
public DnaPartitionResult GetNextResult() { WorkerData tmp = data; while (tmp.workerTail.Count == 0) { Thread.Sleep(2); //only happens at startup and on rare occasions tmp = data; } DnaPartitionResult result = tmp.workerTail.Dequeue(); tmp.workerUsedTail.Add(result); return(result); }
public void AcceptGoodDrawing(int tailIndex, int newSeed) { DnaPartitionResult result = data.workerUsedTail[tailIndex]; parentDrawing = result.Drawing; //.Clone(); var newData = new WorkerData { randomSeed = newSeed, hasNewParent = true, workerUsedTail = new List <DnaPartitionResult>(), workerTail = new Queue <DnaPartitionResult>(), }; data = newData; // assign new data to worker, worker will get it next loop itteration }
public double GetNextErrorLevel() { int tailIndex = 0; while (true) { var results = new List <DnaPartitionResult>(); foreach (ClusteredWorker worker in workers) { DnaPartitionResult partitionResult = worker.GetNextResult(); results.Add(partitionResult); } double newErrorLevel = 0; foreach (DnaPartitionResult partitionResult in results) { newErrorLevel += partitionResult.ErrorLevel; } if (newErrorLevel <= currentErrorLevel) { currentErrorLevel = newErrorLevel; int newSeed = Tools.GetRandomNumber(int.MinValue, int.MaxValue); foreach (ClusteredWorker worker in workers) { worker.AcceptGoodDrawing(tailIndex, newSeed); } break; } tailIndex++; // Thread.Sleep(10); } return(currentErrorLevel); }