From 4e61535eb8813c02080ef408f10d67731cd8abc1 Mon Sep 17 00:00:00 2001 From: Hnatekmar Date: Sat, 24 Jun 2017 19:45:54 +0200 Subject: [PATCH] Drawing thread now terminates after GA finishes --- source/app.d | 18 +++++++++++++++--- source/genetic.d | 2 +- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/source/app.d b/source/app.d index 9b0020d..cb915eb 100644 --- a/source/app.d +++ b/source/app.d @@ -86,9 +86,10 @@ class ImageFitness(alias toShape, alias rasterizer, size_t genomSize) void gaCircle(in Options options, Tid someId) { - auto fitness = new ImageFitness!(toCircle, rasterizeCircles, circleGenomSize)(options.input, someId); - geneticAlgorithm!fitness(circleGenomSize * options.shapeCount, 0.0, 50, options.mutation, options.maxEpoch, - options.forever); + auto fitness = new ImageFitness!(toCircle, rasterizeCircles, circleGenomSize)(options.input, someId); + geneticAlgorithm!fitness(circleGenomSize * options.shapeCount, 0.0, 50, options.mutation, options.maxEpoch, + options.forever); + send(ownerTid, true); } void gaRectangle(in Options options, Tid someId) @@ -97,6 +98,7 @@ void gaRectangle(in Options options, Tid someId) someId); geneticAlgorithm!fitness(rectangleGenomSize * options.shapeCount, 0.0, 50, options.mutation, options.maxEpoch, options.forever); + send(ownerTid, true); } void drawingThread(in Options options) @@ -120,6 +122,7 @@ void drawingThread(in Options options) "Genetický algoritmus" ); + bool stop = false; while (window.isOpen()) { Event event; @@ -133,7 +136,16 @@ void drawingThread(in Options options) receiveTimeout(100.msecs, (shared SfmlImage img) { loadImageIntoSprite(cast(SfmlImage) img, sprite); + }, + (bool _) + { + stop = true; }); + if(stop) + { + window.close(); + break; + } window.clear(Color.White); if(sprite !is null) window.draw(sprite); window.display(); diff --git a/source/genetic.d b/source/genetic.d index fcbde75..10bd145 100644 --- a/source/genetic.d +++ b/source/genetic.d @@ -75,7 +75,7 @@ Individual!fitness geneticAlgorithm(alias fitness)(size_t genomSize, double requ } generationNumber += 1; if(generationNumber >= generationMax && !infinite) break; - receiveTimeout(1.msecs, (OwnerTerminated own) { running = false; }); + receiveTimeout(-1.msecs, (OwnerTerminated own) { running = false; }); } return current.getFittest(); }