summaryrefslogtreecommitdiff
path: root/src/phenotypebinary.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/phenotypebinary.cpp')
-rw-r--r--src/phenotypebinary.cpp56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/phenotypebinary.cpp b/src/phenotypebinary.cpp
index 0d32c9f..9d9e26c 100644
--- a/src/phenotypebinary.cpp
+++ b/src/phenotypebinary.cpp
@@ -18,6 +18,8 @@ Genetic::PhenotypeBinary::PhenotypeBinary( int iSize, bool bRandom ) :
18 aGenes = new uint_fast32_t[iWords]; 18 aGenes = new uint_fast32_t[iWords];
19 if( bRandom ) 19 if( bRandom )
20 randomize(); 20 randomize();
21 else
22 memset( aGenes, 0, sizeof(uint_fast32_t)*iWords );
21} 23}
22 24
23Genetic::PhenotypeBinary::~PhenotypeBinary() 25Genetic::PhenotypeBinary::~PhenotypeBinary()
@@ -57,12 +59,16 @@ Genetic::Phenotype *Genetic::PhenotypeBinary::makeEmptyOffspring(
57Genetic::Phenotype &Genetic::PhenotypeBinary::copyFrom( const Phenotype &rSrc, 59Genetic::Phenotype &Genetic::PhenotypeBinary::copyFrom( const Phenotype &rSrc,
58 int iStart, int iCount, int iDest ) 60 int iStart, int iCount, int iDest )
59{ 61{
62 if( iCount <= 0 )
63 return *this;
64
60 const PhenotypeBinary &rbSrc = 65 const PhenotypeBinary &rbSrc =
61 dynamic_cast<const Genetic::PhenotypeBinary &>(rSrc); 66 dynamic_cast<const Genetic::PhenotypeBinary &>(rSrc);
62 if( iDest < 0 ) 67 if( iDest < 0 )
63 iDest = iStart; 68 iDest = iStart;
64 69
65 // Fist draft, very sloppy: bit by bit copy, this is stupid, but easy 70 // Fist draft, very sloppy: bit by bit copy, this is stupid, but easy
71 /*
66 for( int j = 0; j < iCount; j++ ) 72 for( int j = 0; j < iCount; j++ )
67 { 73 {
68 int wd = wordWithBit(j+iDest); 74 int wd = wordWithBit(j+iDest);
@@ -76,6 +82,56 @@ Genetic::Phenotype &Genetic::PhenotypeBinary::copyFrom( const Phenotype &rSrc,
76 aGenes[wd] |= (1<<((j+iDest)%BPW)); 82 aGenes[wd] |= (1<<((j+iDest)%BPW));
77 } 83 }
78 } 84 }
85 */
86
87 {
88 if( iDest == iStart )
89 {
90 if( wordWithBit(iStart) == wordWithBit(iStart+iCount-1) )
91 {
92 uint_fast32_t uMask = -1;
93 uMask = (uMask << (iStart%BPW)) &
94 (uMask >> (BPW-(((iStart+iCount))%BPW)));
95// sio << Fmt().radix(2).width(32).fill('0') << uMask << sio.nl;
96 aGenes[wordWithBit( iDest )] =
97 (aGenes[wordWithBit( iDest )]&(~uMask)) |
98 (rbSrc.aGenes[wordWithBit( iStart )]&uMask);
99 }
100 else
101 {
102 int iStartWord = wordWithBit( iStart );
103 int iEndWord = wordWithBit( iStart+iCount );
104
105// sio << wordWithBit( iStart+iCount ) << "/" << iWords << sio.nl;
106// sio << iStart << " + " << iCount << " = " << iStart+iCount << " / " << iSize << sio.nl;
107
108
109 uint_fast32_t uMask = -1;
110 uMask = uMask << (iStart%BPW);
111// sio << Fmt().radix(2).width(32).fill('0') << uMask << sio.nl;
112 aGenes[wordWithBit( iDest )] =
113 (aGenes[wordWithBit( iDest )]&(~uMask)) |
114 (rbSrc.aGenes[wordWithBit( iStart )]&uMask);
115
116 for( int j = iStartWord+1; j < iEndWord; j++ )
117 aGenes[j] = rbSrc.aGenes[j];
118
119 uMask = ((uint_fast32_t)-1) >> (BPW-(((iStart+iCount))%BPW));
120// sio << Fmt().radix(2).width(32).fill('0') << uMask << sio.nl;
121 aGenes[wordWithBit( iDest+iCount-1 )] =
122 (aGenes[wordWithBit( iDest+iCount-1 )]&(~uMask)) |
123 (rbSrc.aGenes[wordWithBit( iStart+iCount-1 )]&uMask);
124 }
125 }
126 else if( iDest < iStart )
127 {
128 sio << "iDest < iStart not finished." << sio.nl;
129 }
130 else
131 {
132 sio << "iDest > iStart not finished." << sio.nl;
133 }
134 }
79 135
80 /* 136 /*
81 iStart%BPW 137 iStart%BPW