From 1d0a816605f0cd540dc4dca4b23705a39b740787 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 29 Apr 2017 12:30:30 +0000 Subject: [PATCH] Implement faster comparison algorithm --- compare.glsl | 36 ++++++++++++++++++++++++++---------- src/Fitness.cpp | 27 ++++++++++++++++----------- src/main.cpp | 36 +++++++++++++++++++++++++++++++++--- 3 files changed, 75 insertions(+), 24 deletions(-) diff --git a/compare.glsl b/compare.glsl index e367810..431fa72 100644 --- a/compare.glsl +++ b/compare.glsl @@ -16,16 +16,16 @@ void main(void) if (horizontal) { float posy = gl_FragCoord.y/size.y; - for (float x=0.0; x - +#include Fitness::Fitness(sf::Texture target, float scale) : dummy(sf::TriangleFan, 4) @@ -16,7 +16,9 @@ Fitness::Fitness(sf::Texture target, float scale) : // this->view.reset(sf::FloatRect(0, 0, targetSize.x, targetSize.y)); // this->view.setViewport(sf::FloatRect(0, 0, 1, 1)); - this->horizontal = targetSize.x > targetSize.y; + this->horizontal = targetSize.x >= targetSize.y; +// this->horizontal = true; + std::cout << "Horizontal mode: " << horizontal << " " << targetSize.x << "." << targetSize.y << std::endl; if (this->horizontal) { targetSize.x = 1; } else { @@ -40,7 +42,7 @@ bool Fitness::loadShader(std::string filename) if (this->compshdr.isAvailable()) { this->compshdr.setUniform("base", this->target); this->compshdr.setUniform("curr", this->tex.getTexture()); - this->compshdr.setUniform("size", sf::Vector2f(this->comp.getSize())); + this->compshdr.setUniform("size", sf::Vector2f(this->target.getSize())); this->compshdr.setUniform("horizontal", this->horizontal); return true; } else { @@ -66,6 +68,7 @@ unsigned long long Fitness::of(Chromosome chr) // then, download the result as an image and add the pixels sf::Image image = this->comp.getTexture().copyToImage(); sf::Vector2u size = image.getSize(); +// std::cout << "Image size: " << size.x << " " << size.y << std::endl; unsigned long long fitness = 0; // for (unsigned int x=0; xhorizontal) { for (unsigned int y=0; yalive << " fitnev: " << it->fitnessEvaluated << " fitn: " << it->fitness << std::endl; // } @@ -139,22 +152,37 @@ int main() sf::Sprite starget(target); sf::View vbest(sf::FloatRect(0, 0, target.getSize().x, target.getSize().y)); sf::View vmed(sf::FloatRect(0, 0, target.getSize().x, target.getSize().y)); - sf::View vworst(sf::FloatRect(0, 0, target.getSize().x, target.getSize().y)); + sf::View vworst(sf::FloatRect(0, 0, 1/*target.getSize().x*/, target.getSize().y)); vbest.setViewport(sf::FloatRect(.5, 0, .5, .5)); vmed.setViewport(sf::FloatRect(0, .5, .5, .5)); vworst.setViewport(sf::FloatRect(.5, .5, .5, .5)); + sf::Sprite sprite(fitn.comp.getTexture()); + sf::RenderWindow window(sf::VideoMode(winW, winH), "gross"); window.setMouseCursorVisible(false); // hide the cursor while (window.isOpen()) { sf::Event event; - while (window.pollEvent(event)); + while (window.pollEvent(event)) { + if (event.type == sf::Event::KeyPressed) { +// for (auto ind : genr.individuals) { +// std::cout << ind.fitness << std::endl; +// } + + std::cout << genr.individuals[0].fitness << std::endl; + + } + } // calculate next generation genr.cull(); genr.reproduce(0); genr.updateFitness(); + +// genr.cull(); +// genr.reproduce(0); +// genr.updateFitness(); // genr.updateFitness(); // for (auto it=genr.individuals.rbegin(); it!=genr.individuals.rend(); ++it) { // if (it->fitnessEvaluated) { @@ -167,9 +195,11 @@ int main() window.draw(starget); window.setView(vbest ); window.draw(genr.individuals[0 ].chromosome); window.setView(vmed ); window.draw(genr.individuals[genr.living/2].chromosome); - window.setView(vworst); window.draw(genr.individuals[genr.living-1].chromosome); + window.setView(vworst); //window.draw(genr.individuals[genr.living-1].chromosome); + window.draw(sprite); window.setView(window.getDefaultView()); window.display(); + std::cout << "Generation finished: " << ++gencnt << " (winner's length: " << genr.individuals[0].chromosome.length() << ")" << std::endl; } }