|
|
||
| array samplex; | ||
| array sampley; | ||
| array samplez; |
|
|
||
| #include <iostream> | ||
|
|
||
| #define OFFSET n/100 |
| array samplez; | ||
|
|
||
| int n = 256; | ||
| int nsamples = 16; |
| win(0,1).setAxesTitles("idx", "idy", "Search Space"); | ||
| win(0,0).surface(searchSpace_x_display, searchSpace_y_display, searchSpace); | ||
| win(0,1).scatter(samplex.as(f32), sampley.as(f32), samplez.as(f32)); | ||
| win.show(); |
| do { | ||
| reproduce(); | ||
| float maximum = max<float>(samplez); | ||
| array where_m = where(samplez == maximum); |
| printf("Current max at (%d,%d): %f (truemax %f)\n", | ||
| samplex(where_m).scalar<unsigned int>(), | ||
| sampley(where_m).scalar<unsigned int>(), maximum, truemax); | ||
| } while (max<float>(samplez) < truemax * 0.99); |
|
|
||
| do{ | ||
| float maximum = max<float>(samplez); | ||
| array where_m = where(samplez == maximum); |
|
|
||
| void init() { | ||
| //initialize space | ||
| searchSpace = tile(array(seq(n / 2)), 1, n / 2) + tile(moddims(array(seq(n / 2)), 1, n / 2), n / 2); |
| #include <stdio.h> | ||
| #include <string.h> | ||
| #include <math.h> | ||
| #include <limits.h> |
| #include <string.h> | ||
| #include <math.h> | ||
| #include <limits.h> | ||
|
|
| //Flip a random bit to vary the gene pool | ||
| int bits = (int)(log((double)n) / log(2.0)); | ||
| mutantx = mutantx ^ (pow2((randu(nsamples / 2, u32) % bits).as(f32))).as(u32); | ||
| mutanty = mutanty ^ (pow2((randu(nsamples / 2, u32) % bits).as(f32))).as(u32); |
|
|
||
| array update(const array& searchSpace, const array& samplex, const array& sampley, const int nsamples, const int n) | ||
| { | ||
| array result(nsamples, 1, f32); |
| indices = indices.rows(indices.elements() - nsamples / 2, indices.elements() - 1); | ||
| } | ||
|
|
||
| return indices; |
|
|
||
| //Get crossover points (at which bit to crossover) and construct bit masks from them | ||
| array crossover = randu(nsamples / 4, u32) % (log((double)n) / log(2.0)); | ||
| array lowermask = pow2(crossover.as(f32)).as(u32) - 1; |
| array parentsy2 = parentsy.rows(parentsy.elements() / 2, parentsy.elements() - 1); | ||
|
|
||
| //Get crossover points (at which bit to crossover) and construct bit masks from them | ||
| array crossover = randu(nsamples / 4, u32) % (log((double)n) / log(2.0)); |
| if (!console) { | ||
| af::Window win(1600, 800, "arrayfire genetic algorithm search demo"); | ||
| win.grid(1, 2); | ||
|
|
| win(0,1).setAxesTitles("idx", "idy", "search space"); | ||
| win(0,0).surface(searchspace_x_display, searchspace_y_display, searchspace); | ||
| win(0,1).scatter(samplex.as(f32), sampley.as(f32), samplez.as(f32)); | ||
| win.show(); |
| printf("max found at (%d,%d): %f (truemax %f)\n", | ||
| samplex(where_m).scalar<unsigned int>(), | ||
| sampley(where_m).scalar<unsigned int>(), maximum, truemax); | ||
| } |
| } | ||
|
|
||
| try { | ||
| printf("** ArrayFire Genetic Algorithm Search Demo **\n\n"); |
| } | ||
|
|
||
| array selectFittest(const array& samplez, const int nsamples) { | ||
| //pick top 50% fittest |
Kumar Aatish and others added 4 commits
April 11, 2017 17:15