Implement faster comparison algorithm
This commit is contained in:
parent
a04e58c7ca
commit
1d0a816605
3 changed files with 75 additions and 24 deletions
32
compare.glsl
32
compare.glsl
|
|
@ -16,7 +16,7 @@ void main(void)
|
||||||
|
|
||||||
if (horizontal) {
|
if (horizontal) {
|
||||||
float posy = gl_FragCoord.y/size.y;
|
float posy = gl_FragCoord.y/size.y;
|
||||||
for (float x=0.0; x<size.x; x+=1.0) {
|
for (float x=0.0; x<=1.0; x+=(1.0/size.x)) {
|
||||||
vec4 color = texture2D(curr, vec2(x, posy));
|
vec4 color = texture2D(curr, vec2(x, posy));
|
||||||
vec4 basecolor = texture2D(base, vec2(x, 1.0-posy));
|
vec4 basecolor = texture2D(base, vec2(x, 1.0-posy));
|
||||||
color.rgb = abs(color.rgb - basecolor.rgb);
|
color.rgb = abs(color.rgb - basecolor.rgb);
|
||||||
|
|
@ -24,7 +24,7 @@ void main(void)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
float posx = gl_FragCoord.x/size.x;
|
float posx = gl_FragCoord.x/size.x;
|
||||||
for (float y=0.0; y<size.y; y+=1.0) {
|
for (float y=0.0; y<=1.0; y+=(1.0/size.y)) {
|
||||||
vec4 color = texture2D(curr, vec2(posx, y));
|
vec4 color = texture2D(curr, vec2(posx, y));
|
||||||
vec4 basecolor = texture2D(base, vec2(posx, 1.0-y));
|
vec4 basecolor = texture2D(base, vec2(posx, 1.0-y));
|
||||||
color.rgb = abs(color.rgb - basecolor.rgb);
|
color.rgb = abs(color.rgb - basecolor.rgb);
|
||||||
|
|
@ -33,13 +33,29 @@ void main(void)
|
||||||
}
|
}
|
||||||
|
|
||||||
// encode to rgba channels in current pixel
|
// encode to rgba channels in current pixel
|
||||||
vec4 color;
|
vec4 col = vec4(0.0, 0.0, 0.0, 1.0);
|
||||||
color.r = mod(total, 256.0/255.0); total = (total - color.r) / (256.0/255.0);
|
// total = total/200.0;
|
||||||
color.g = mod(total, 256.0/255.0); total = (total - color.g) / (256.0/255.0);
|
col.r = mod(total, 256.0/255.0);
|
||||||
color.b = mod(total, 256.0/255.0); total = (total - color.b) / (256.0/255.0);
|
col.g = mod(total/256.0, 256.0/255.0);
|
||||||
color.a = mod(total, 256.0/255.0);
|
col.b = mod(total/65536.0, 256.0/255.0);
|
||||||
|
// color.g = 0.0;
|
||||||
|
// color.b = 0.0;
|
||||||
|
// color.g = mod(total, 256.0/255.0);
|
||||||
|
// color.r = float(mod(total, 256))/255.0;
|
||||||
|
// total = total/256;
|
||||||
|
// color.r = 1.0/255.0;
|
||||||
|
// color.r = mod(total, 256.0/255.0);
|
||||||
|
// total = (total - color.r) / (256.0/255.0);
|
||||||
|
// color.g = mod(total, 256.0/255.0); total = (total - color.g) / (256.0/255.0);
|
||||||
|
// color.b = mod(total, 256.0/255.0);
|
||||||
|
// color.a = 1.0;
|
||||||
|
|
||||||
gl_FragColor = color;
|
// color.r = 0;
|
||||||
|
|
||||||
|
gl_FragColor = col;
|
||||||
|
// color.a = mod(total, 256.0/255.0);
|
||||||
|
|
||||||
|
// gl_FragColor = vec4(0.0, 1.0, 1.0, 1.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
#include <SFML/System.hpp>
|
#include <SFML/System.hpp>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
Fitness::Fitness(sf::Texture target, float scale) :
|
Fitness::Fitness(sf::Texture target, float scale) :
|
||||||
dummy(sf::TriangleFan, 4)
|
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.reset(sf::FloatRect(0, 0, targetSize.x, targetSize.y));
|
||||||
// this->view.setViewport(sf::FloatRect(0, 0, 1, 1));
|
// 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) {
|
if (this->horizontal) {
|
||||||
targetSize.x = 1;
|
targetSize.x = 1;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -40,7 +42,7 @@ bool Fitness::loadShader(std::string filename)
|
||||||
if (this->compshdr.isAvailable()) {
|
if (this->compshdr.isAvailable()) {
|
||||||
this->compshdr.setUniform("base", this->target);
|
this->compshdr.setUniform("base", this->target);
|
||||||
this->compshdr.setUniform("curr", this->tex.getTexture());
|
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);
|
this->compshdr.setUniform("horizontal", this->horizontal);
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -66,6 +68,7 @@ unsigned long long Fitness::of(Chromosome chr)
|
||||||
// then, download the result as an image and add the pixels
|
// then, download the result as an image and add the pixels
|
||||||
sf::Image image = this->comp.getTexture().copyToImage();
|
sf::Image image = this->comp.getTexture().copyToImage();
|
||||||
sf::Vector2u size = image.getSize();
|
sf::Vector2u size = image.getSize();
|
||||||
|
// std::cout << "Image size: " << size.x << " " << size.y << std::endl;
|
||||||
unsigned long long fitness = 0;
|
unsigned long long fitness = 0;
|
||||||
// for (unsigned int x=0; x<size.x; ++x) {
|
// for (unsigned int x=0; x<size.x; ++x) {
|
||||||
// for (unsigned int y=0; y<size.y; ++y) {
|
// for (unsigned int y=0; y<size.y; ++y) {
|
||||||
|
|
@ -77,10 +80,11 @@ unsigned long long Fitness::of(Chromosome chr)
|
||||||
if (this->horizontal) {
|
if (this->horizontal) {
|
||||||
for (unsigned int y=0; y<size.y; ++y) {
|
for (unsigned int y=0; y<size.y; ++y) {
|
||||||
sf::Color col = image.getPixel(0, y);
|
sf::Color col = image.getPixel(0, y);
|
||||||
fitness += col.r; // 2^(8*0)
|
// std::cout<<(int)col.r<<"|"<<(int)col.g<<"|"<<(int)col.b<<"|"<<(int)col.a<<std::endl;
|
||||||
fitness += col.g*256; // 2^(8*1)
|
fitness += ((unsigned int)col.r); // 2^(8*0)
|
||||||
fitness += col.b*65536; // 2^(8*2)
|
fitness += ((unsigned int)col.g)*256; // 2^(8*1)
|
||||||
fitness += col.a*16777216; // 2^(8*3)
|
fitness += ((unsigned int)col.b)*65536; // 2^(8*2)
|
||||||
|
// fitness += (unsigned int)col.a*16777216; // 2^(8*3)
|
||||||
// col = image.getPixel(1, y);
|
// col = image.getPixel(1, y);
|
||||||
// fitness += col.r*4294967296; // 2^(8*4)
|
// fitness += col.r*4294967296; // 2^(8*4)
|
||||||
// fitness += col.g*1099511627776; // 2^(8*5)
|
// fitness += col.g*1099511627776; // 2^(8*5)
|
||||||
|
|
@ -90,10 +94,11 @@ unsigned long long Fitness::of(Chromosome chr)
|
||||||
} else {
|
} else {
|
||||||
for (unsigned int x=0; x<size.x; ++x) {
|
for (unsigned int x=0; x<size.x; ++x) {
|
||||||
sf::Color col = image.getPixel(x, 0);
|
sf::Color col = image.getPixel(x, 0);
|
||||||
fitness += col.r; // 2^(8*0)
|
// std::cout<<(int)col.r<<"|"<<(int)col.g<<"|"<<(int)col.b<<"|"<<(int)col.a<<std::endl;
|
||||||
fitness += col.g*256; // 2^(8*1)
|
fitness += (unsigned long int)col.r; // 2^(8*0)
|
||||||
fitness += col.b*65536; // 2^(8*2)
|
fitness += ((unsigned long int)col.g)*256; // 2^(8*1)
|
||||||
fitness += col.a*16777216; // 2^(8*3)
|
fitness += ((unsigned long int)col.b)*65536; // 2^(8*2)
|
||||||
|
// fitness += ((unsigned long int)col.a)*16777216; // 2^(8*3)
|
||||||
// col = image.getPixel(x, 1);
|
// col = image.getPixel(x, 1);
|
||||||
// fitness += col.r*4294967296; // 2^(8*4)
|
// fitness += col.r*4294967296; // 2^(8*4)
|
||||||
// fitness += col.g*1099511627776; // 2^(8*5)
|
// fitness += col.g*1099511627776; // 2^(8*5)
|
||||||
|
|
|
||||||
36
src/main.cpp
36
src/main.cpp
|
|
@ -119,8 +119,18 @@ int main()
|
||||||
target.loadFromFile("tom-face.png");
|
target.loadFromFile("tom-face.png");
|
||||||
|
|
||||||
Chromosome::size = sf::Vector2f(target.getSize());
|
Chromosome::size = sf::Vector2f(target.getSize());
|
||||||
|
// Chromosome::size = sf::Vector2f(winW/2, winH/2);
|
||||||
Chromosome::re = &randomEngine;
|
Chromosome::re = &randomEngine;
|
||||||
|
|
||||||
|
// Chromosome targetc;
|
||||||
|
// for (int i=0; i<100; ++i) targetc.mutate();
|
||||||
|
// sf::RenderTexture targettex;
|
||||||
|
// targettex.create(winW/2, winH/2);
|
||||||
|
// targettex.clear();
|
||||||
|
// targettex.draw(targetc);
|
||||||
|
// targettex.display();
|
||||||
|
// sf::Texture target = targettex.getTexture();
|
||||||
|
|
||||||
Fitness fitn(target);
|
Fitness fitn(target);
|
||||||
fitn.loadShader("compare.glsl");
|
fitn.loadShader("compare.glsl");
|
||||||
|
|
||||||
|
|
@ -131,6 +141,9 @@ int main()
|
||||||
Generation genr;
|
Generation genr;
|
||||||
unsigned int gencnt = 0; // all white is generation 0
|
unsigned int gencnt = 0; // all white is generation 0
|
||||||
genr.updateFitness();
|
genr.updateFitness();
|
||||||
|
// for (auto ind : genr.individuals) {
|
||||||
|
// std::cout << ind.fitness << std::endl;
|
||||||
|
// }
|
||||||
// for (auto it=genr.individuals.begin(); it!=genr.individuals.end(); ++it) {
|
// for (auto it=genr.individuals.begin(); it!=genr.individuals.end(); ++it) {
|
||||||
// std::cout << "alive: " << it->alive << " fitnev: " << it->fitnessEvaluated << " fitn: " << it->fitness << std::endl;
|
// std::cout << "alive: " << it->alive << " fitnev: " << it->fitnessEvaluated << " fitn: " << it->fitness << std::endl;
|
||||||
// }
|
// }
|
||||||
|
|
@ -139,22 +152,37 @@ int main()
|
||||||
sf::Sprite starget(target);
|
sf::Sprite starget(target);
|
||||||
sf::View vbest(sf::FloatRect(0, 0, target.getSize().x, target.getSize().y));
|
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 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));
|
vbest.setViewport(sf::FloatRect(.5, 0, .5, .5));
|
||||||
vmed.setViewport(sf::FloatRect(0, .5, .5, .5));
|
vmed.setViewport(sf::FloatRect(0, .5, .5, .5));
|
||||||
vworst.setViewport(sf::FloatRect(.5, .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");
|
sf::RenderWindow window(sf::VideoMode(winW, winH), "gross");
|
||||||
window.setMouseCursorVisible(false); // hide the cursor
|
window.setMouseCursorVisible(false); // hide the cursor
|
||||||
|
|
||||||
while (window.isOpen()) {
|
while (window.isOpen()) {
|
||||||
sf::Event event;
|
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
|
// calculate next generation
|
||||||
genr.cull();
|
genr.cull();
|
||||||
genr.reproduce(0);
|
genr.reproduce(0);
|
||||||
genr.updateFitness();
|
genr.updateFitness();
|
||||||
|
|
||||||
|
// genr.cull();
|
||||||
|
// genr.reproduce(0);
|
||||||
|
// genr.updateFitness();
|
||||||
// genr.updateFitness();
|
// genr.updateFitness();
|
||||||
// for (auto it=genr.individuals.rbegin(); it!=genr.individuals.rend(); ++it) {
|
// for (auto it=genr.individuals.rbegin(); it!=genr.individuals.rend(); ++it) {
|
||||||
// if (it->fitnessEvaluated) {
|
// if (it->fitnessEvaluated) {
|
||||||
|
|
@ -167,9 +195,11 @@ int main()
|
||||||
window.draw(starget);
|
window.draw(starget);
|
||||||
window.setView(vbest ); window.draw(genr.individuals[0 ].chromosome);
|
window.setView(vbest ); window.draw(genr.individuals[0 ].chromosome);
|
||||||
window.setView(vmed ); window.draw(genr.individuals[genr.living/2].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.setView(window.getDefaultView());
|
||||||
window.display();
|
window.display();
|
||||||
|
|
||||||
std::cout << "Generation finished: " << ++gencnt << " (winner's length: " << genr.individuals[0].chromosome.length() << ")" << std::endl;
|
std::cout << "Generation finished: " << ++gencnt << " (winner's length: " << genr.individuals[0].chromosome.length() << ")" << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue