55#include < random>
66#include < math.h>
77#include < execution>
8+ #include < iostream>
89
910bool cmp (const PopulationGroup& a, const PopulationGroup& b) {
1011 return a.fitness < b.fitness ;
@@ -19,25 +20,34 @@ Habitat::Habitat(const SrcImage* reconstructionImage, const std::vector<SrcImage
1920 mReconstructionImage = reconstructionImage;
2021 mSettings = settings;
2122 init_pop ();
23+
24+ /*
25+ std::vector<int> indexes;
26+ for(int i=0; i<mSettings.popSize; i++) {
27+ indexes.push_back(i);
28+ }
29+
30+ std::for_each(std::execution::par_unseq, indexes.begin(), indexes.end(), [&](int i) {
31+ drawComputeFit(mPopulation[i]);
32+ }); */
2233}
2334
2435void Habitat::step () {
2536 std::sort (mPopulation .begin (), mPopulation .end (), cmp);
2637
2738 std::vector<int > indexes;
2839 for (int i=0 ; i<mSettings .popSize ; i++) {
29- if (i>mSettings .popSize - ceil (mSettings .popSize * mSettings .reroll )) {
40+ if (i>( mSettings .popSize - ceil (mSettings .popSize * mSettings .reroll ) - 1 )) {
3041 indexes.push_back (i);
31- }
42+ }
3243 }
3344
3445 std::for_each (std::execution::par_unseq, indexes.begin (), indexes.end (), [&](int i) {
3546 if (rand ()%100 < mSettings .crossoverChance ) {
36- int ind1 = rand () % mSettings .popSize ;
37- int ind2 = rand () % mSettings .popSize ;
47+ int ind1 = rand () % ( int )( mSettings .popSize - ceil ( mSettings . popSize * mSettings . reroll ) - 1 ) ;
48+ int ind2 = rand () % ( int )( mSettings .popSize - ceil ( mSettings . popSize * mSettings . reroll ) - 1 ) ;
3849 crossover (mPopulation [ind1], mPopulation [ind2], mPopulation [i]);
39- }
40- else {
50+ } else {
4151 mutate (mPopulation [i]);
4252 }
4353
@@ -53,7 +63,7 @@ void Habitat::init_pop() {
5363 mPopulation .clear ();
5464 for (int i = 0 ; i < mSettings .popSize ; i++) {
5565 mPopulation .push_back (PopulationGroup{});
56- mPopulation [i].pastedData = new uint8_t [mReconstructionImage ->pitch *mReconstructionImage ->height ];
66+ mPopulation [i].pastedData = new uint8_t [mReconstructionImage ->width *mReconstructionImage ->height * 4 ];
5767 mPopulation [i].fitness = UINT32_MAX;
5868 for (int j = 0 ; j < mSettings .imgCount ; j++) {
5969 mPopulation [i].individuals .push_back (random_individual ());
@@ -62,11 +72,11 @@ void Habitat::init_pop() {
6272}
6373
6474void Habitat::drawComputeFit (PopulationGroup& grp) {
65- memset (grp.pastedData , 0xFFFFFF00 , mReconstructionImage ->width *mReconstructionImage ->height );
75+ memset (grp.pastedData , 0xFF , mReconstructionImage ->width *mReconstructionImage ->height * 4 );
6676
6777 RotatePixel_t *pDstBase = static_cast <RotatePixel_t*>((void *)grp.pastedData );
6878
69- for (int i = 0 ; i < mSettings . imgCount ; i++) {
79+ for (int i = 0 ; i < grp. individuals . size () ; i++) {
7080 const SrcImage* pImg = grp.individuals [i].img ;
7181 RotatePixel_t *pSrcBase = static_cast <RotatePixel_t*>((void *)pImg->data );
7282 RotateDrawClip (pDstBase, mReconstructionImage ->width , mReconstructionImage ->height , mReconstructionImage ->pitch ,
@@ -77,27 +87,56 @@ void Habitat::drawComputeFit(PopulationGroup& grp) {
7787 }
7888
7989 grp.fitness = compute_sad (grp.pastedData , mReconstructionImage ->data ,
80- mReconstructionImage ->pitch *mReconstructionImage ->height );
90+ mReconstructionImage ->width *mReconstructionImage ->height * 4 );
8191}
8292
8393void Habitat::crossover (const PopulationGroup& grpA, const PopulationGroup& grpB, PopulationGroup& grpC) {
84- for (int i = 0 ; i < mSettings .imgCount ; i++) {
94+ grpC.individuals .clear ();
95+ for (int i = 0 ; i < std::min (grpA.individuals .size (),
96+ grpB.individuals .size ()); i++) {
8597 if (rand ()%2 ) {
86- grpC.individuals [i] = grpA.individuals [i];
98+ grpC.individuals . push_back ( grpA.individuals [i]) ;
8799 } else {
88- grpC.individuals [i] = grpB.individuals [i];
100+ grpC.individuals . push_back ( grpB.individuals [i]) ;
89101 }
90102 }
91103}
92104
93105void Habitat::mutate (PopulationGroup& grp) {
94- for (int i = 0 ; i < mSettings .imgCount ; i++) {
95- grp.individuals [i].angle += rand ()%50 / 100.0 ;
106+ int roll = rand ()%100 ;
107+ if (roll < 60 ) {
108+ mutateAdjust (grp);
109+ } else if (roll < 85 ) {
110+ mutateAdd (grp);
111+ } else {
112+ mutateRemove (grp);
113+ }
114+ }
115+
116+ void Habitat::mutateAdd (PopulationGroup& grp) {
117+ grp.individuals .push_back (random_individual ());
118+ }
119+
120+ void Habitat::mutateRemove (PopulationGroup& grp) {
121+ if (grp.individuals .size () == 0 )
122+ return ;
123+
124+ int ind = rand ()%grp.individuals .size ();
125+ grp.individuals .erase (grp.individuals .begin () + ind);
126+ }
127+
128+ void Habitat::mutateAdjust (PopulationGroup& grp) {
129+ // for (int i = 0; i < grp.individuals.size(); i++) {
130+ {
131+ int i = rand ()%grp.individuals .size ();
132+ // ADD CLOSEST IMAGES
133+
134+ grp.individuals [i].angle += rand ()%50 / 100.0 * std::pow (-1 , rand ()%2 );
96135 grp.individuals [i].x += rand ()%(mReconstructionImage ->width ) * 0.1 * std::pow (-1 , rand ()%2 );
97136 grp.individuals [i].y += rand ()%(mReconstructionImage ->height ) * 0.1 * std::pow (-1 , rand ()%2 );
98- float randScale = (rand ()%((int )(100 *mSettings .maxScale - 100 *mSettings .minScale ))
99- + (100 *mSettings .minScale )) / 100 .0 ;
100- grp.individuals [i].scale += randScale * 0.3 * std::pow (-1 , rand ()%2 );
137+ float randScale = (rand ()%((int )(1000 *mSettings .maxScale - 1000 *mSettings .minScale ))
138+ + (1000 *mSettings .minScale )) / 1000 .0 ;
139+ grp.individuals [i].scale += randScale * 0.1 * std::pow (-1 , rand ()%2 );
101140 if (grp.individuals [i].scale < mSettings .minScale ) {
102141 grp.individuals [i].scale = mSettings .minScale ;
103142 } else if (grp.individuals [i].scale > mSettings .maxScale ) {
@@ -116,14 +155,15 @@ Individual Habitat::random_individual() {
116155 float angle;
117156 float scale;
118157
119- newIndiv.imgID = rand ()%mRefImages ->size ();
158+ newIndiv.imgID = rand ()%( mRefImages ->size () );
120159 newIndiv.img = &(*mRefImages )[newIndiv.imgID ];
121160
122- newIndiv.x = rand ()%(mReconstructionImage ->width - 10 ) + 5 ;
123- newIndiv.y = rand ()%(mReconstructionImage ->height - 10 ) + 5 ;
161+ newIndiv.x = rand ()%(mReconstructionImage ->width ) ;
162+ newIndiv.y = rand ()%(mReconstructionImage ->height ) ;
124163 newIndiv.angle = rand ()%(314 ) / 100.0 ;
125- newIndiv.scale = (rand ()%((int )(100 *mSettings .maxScale - 100 *mSettings .minScale ))
126- + (100 *mSettings .minScale )) / 100.0 ;
164+ newIndiv.scale = (rand ()%((int )(1000 *mSettings .maxScale - 1000 *mSettings .minScale ))
165+ + (1000 *mSettings .minScale )) / 1000.0 ;
166+ // newIndiv.scale = mSettings.minScale;
127167
128168 return newIndiv;
129169
0 commit comments