@@ -77,66 +77,85 @@ namespace evolution {
77
77
return (a.score < b.score );
78
78
}
79
79
80
- float generation::score_individual (representation::representation *individual , config::parse *config_parse) {
80
+ void generation::score_individual (unsigned index , config::parse *config_parse) {
81
81
82
- std::vector<simulation::io_t > xor_outputs (individual->chromosome ->wire_out .size ());
83
- std::vector<simulation::io_t > test_circuic (individual->chromosome ->size ());
84
- std::vector<simulation::io_t > reference_circuic (this ->reference ->chromosome ->size ());
85
- std::vector<unsigned > variant_counter (individual->chromosome ->last_input + 2 );
82
+ this ->individuals [index].mae = 0 ;
83
+ this ->individuals [index].wce = 0 ;
84
+ unsigned variants_count = 1 << TO_REAL_INPUT (this ->individuals [index].repres ->chromosome ->last_input + 1 );
86
85
87
- for (unsigned i = 0 ; i <= individual->chromosome ->last_input ; i++) {
88
- SET_VARIANTS_BITS (test_circuic[i].vec , i);
89
- SET_VARIANTS_BITS (reference_circuic[i].vec , i);
86
+ std::vector<simulation::io_t > xor_outputs (this ->individuals [index].repres ->chromosome ->wire_out .size ());
87
+ std::vector<simulation::io_t > test_circuic (this ->individuals [index].repres ->chromosome ->size ());
88
+ std::vector<simulation::io_t > reference_circuic (this ->reference ->chromosome ->size ());
89
+ std::vector<unsigned > variant_counter (this ->individuals [index].repres ->chromosome ->last_input + 2 );
90
+
91
+ // set const inputs
92
+ SET_VECTOR_TO (test_circuic[0 ].vec , VARIANTS_BITS_ALL0);
93
+ SET_VECTOR_TO (test_circuic[1 ].vec , VARIANTS_BITS_ALL1);
94
+ SET_VECTOR_TO (reference_circuic[0 ].vec , VARIANTS_BITS_ALL0);
95
+ SET_VECTOR_TO (reference_circuic[1 ].vec , VARIANTS_BITS_ALL1);
96
+
97
+ // set varaible inputs
98
+ for (unsigned i = TO_GENOME_INPUT (0 ); i <= this ->individuals [index].repres ->chromosome ->last_input ; i++) {
99
+ SET_VARIANTS_BITS (test_circuic[i].vec , TO_REAL_INPUT (i));
100
+ SET_VARIANTS_BITS (reference_circuic[i].vec , TO_REAL_INPUT (i));
90
101
}
91
102
92
103
unsigned total_error = 0 ;
93
104
bool done = false ;
94
105
95
- if (ONE_SIM_VARIANTS > individual-> chromosome ->last_input ) {
106
+ if (ONE_SIM_VARIANTS > TO_REAL_INPUT ( this -> individuals [index]. repres -> chromosome ->last_input + 1 ) ) {
96
107
done = true ;
97
108
}
98
109
99
110
do {
100
- individual ->simulate (test_circuic);
111
+ this -> individuals [index]. repres ->simulate (test_circuic);
101
112
this ->reference ->simulate (reference_circuic);
102
113
114
+ // Calc error using xor
103
115
unsigned i = 0 ;
104
- for (auto output: individual ->chromosome ->wire_out ) {
116
+ for (auto output: this -> individuals [index]. repres ->chromosome ->wire_out ) {
105
117
xor_outputs[i].vec = test_circuic[output.first ].vec ^ reference_circuic[this ->reference_inverse_wire_out [output.second ]].vec ;
106
- total_error += simulation::bits_count (xor_outputs[i]) * config_parse->port_weight (output.second );
118
+ total_error += simulation::bits_count (xor_outputs[i], variants_count ) * config_parse->port_weight (output.second );
107
119
i++;
108
120
}
109
121
110
- if (simulation::one_max_loss (xor_outputs, individual->chromosome ->wire_out , config_parse) > this ->max_one_loss ) {
111
- return INFINITY;
122
+ // WCE
123
+ auto current_wce = simulation::one_max_loss (xor_outputs, this ->individuals [index].repres ->chromosome ->wire_out , config_parse);
124
+ if (this ->individuals [index].wce < current_wce) {
125
+ this ->individuals [index].wce = current_wce;
126
+
127
+ if (current_wce > this ->max_one_loss ) {
128
+ this ->individuals [index].score = INFINITY;
129
+ return ;
130
+ }
112
131
}
113
132
114
- for (unsigned i = ONE_SIM_VARIANTS; i <= individual->chromosome ->last_input ; i++) {
133
+ // Update inputs for next simulation
134
+ for (unsigned i = ONE_SIM_VARIANTS; i <= TO_REAL_INPUT (this ->individuals [index].repres ->chromosome ->last_input ); i++) {
115
135
variant_counter[i] = (variant_counter[i] + 1 ) % (1 << (i - ONE_SIM_VARIANTS));
116
136
117
137
if (variant_counter[i] == 0 ) {
118
- test_circuic[i ].vec = ~test_circuic[i ].vec ;
119
- reference_circuic[i ].vec = ~reference_circuic[i ].vec ;
138
+ test_circuic[TO_GENOME_INPUT (i) ].vec = ~test_circuic[TO_GENOME_INPUT (i) ].vec ;
139
+ reference_circuic[TO_GENOME_INPUT (i) ].vec = ~reference_circuic[TO_GENOME_INPUT (i) ].vec ;
120
140
121
- if (i == individual ->chromosome ->last_input ) {
122
- if (variant_counter[individual-> chromosome ->last_input + 1 ]) {
141
+ if (TO_GENOME_INPUT (i) == this -> individuals [index]. repres ->chromosome ->last_input ) {
142
+ if (variant_counter[TO_REAL_INPUT ( this -> individuals [index]. repres -> chromosome ->last_input + 1 ) ]) {
123
143
done = true ;
124
144
}
125
145
126
- variant_counter[individual-> chromosome ->last_input + 1 ]++;
146
+ variant_counter[TO_REAL_INPUT ( this -> individuals [index]. repres -> chromosome ->last_input + 1 ) ]++;
127
147
}
128
148
}
129
149
}
130
150
} while (!done);
131
151
132
- unsigned variants_count = 1 << (individual->chromosome ->last_input + 1 );
133
- float abs_error = (float ) total_error / variants_count;
152
+ this ->individuals [index].mae = (float ) total_error / variants_count;
134
153
135
- if (abs_error > this ->max_abs_loss ) {
136
- return INFINITY;
154
+ if (this ->individuals [index].mae > this ->max_abs_loss ) {
155
+ this ->individuals [index].score = INFINITY;
156
+ } else {
157
+ this ->individuals [index].score = (1 - this ->power_accuracy_ratio ) * this ->individuals [index].mae + this ->power_accuracy_ratio * this ->individuals [index].repres ->power_loss ();
137
158
}
138
-
139
- return (1 - this ->power_accuracy_ratio ) * abs_error + this ->power_accuracy_ratio * individual->power_loss ();
140
159
}
141
160
142
161
void generation::selection (unsigned count, config::parse *config_parse) {
@@ -148,7 +167,7 @@ namespace evolution {
148
167
continue ;
149
168
}
150
169
151
- this ->individuals [i]. score = this -> score_individual (this -> individuals [i]. repres , config_parse);
170
+ this ->score_individual (i , config_parse);
152
171
}
153
172
154
173
std::sort (this ->individuals .begin (), this ->individuals .end (), generation::sort_individual_score_asc);
@@ -161,7 +180,7 @@ namespace evolution {
161
180
}
162
181
163
182
unsigned generation::add_individual (representation::representation *individual) {
164
- this ->individuals .push_back ({individual, 0 });
183
+ this ->individuals .push_back ({individual, 0 , 0 , 0 });
165
184
166
185
return this ->individuals .size () - 1 ;
167
186
}
0 commit comments