@@ -78,130 +78,176 @@ int FitStatus(vector<bool> Line) {
7878 return FitCounter;
7979}
8080
81- int main () {
82- // VERY IMPORTANT LINE, IT IS NECESSARY FOR THE RAND() FUNCTION TO WORK CORRECTLY.
83- srand (time (0 ));
81+ vector<bool > BestLineSearch (vector<bool > FirstPop, int LineSymb, vector<bool > BestLine1 = {}) {
82+ vector<bool > BestLine2;
8483
85- // GeneticalAlgorithm
86- int MutationProb = 5 ;
87- int cycles=0 ;
88- int LineSymb = 10 ;
89- vector<bool > FirstPop;
90- FirstPop = RandomMatrix (LineSymb, 4 );
84+ int BestLine2NotDefault = -1 ;
85+ int MaxFitStatus = max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 1 )), max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 2 )), max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 3 )), FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 4 )))));
86+ int SecondMaxFitStatus = -1 ;
9187
92- while (true ) {
93- cout << " FirstPop:\n " ;
94- PrintMatrix (FirstPop, LineSymb);
95- cout << " \n End of FirstPop.\n " ;
96- cycles++;
97- vector<bool > BestLine1;
98- vector<bool > BestLine2;
99- int BestLine2NotDefault = -1 ;
100- int MaxFitStatus = max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 1 )), max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 2 )), max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 3 )), FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 4 )))));
101- int SecondMaxFitStatus = -1 ;
88+ if (MaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 1 ))) {
89+ SecondMaxFitStatus = max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 2 )), max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 3 )), FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 4 ))));
90+ BestLine1 = GetLineFromMatrix (FirstPop, LineSymb, 1 );
91+ for (int i = 1 ; i <= 4 ; i++) {
92+ if (i == 1 ) {
93+ i++;
94+ }
95+ if (SecondMaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, i))) {
96+ BestLine2 = GetLineFromMatrix (FirstPop, LineSymb, i);
97+ }
98+ }
99+ }
102100
103- if (MaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 1 ))) {
104- SecondMaxFitStatus = max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 2 )), max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 3 )), FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 4 ))));
105- BestLine1 = GetLineFromMatrix (FirstPop, LineSymb, 1 );
106- for (int i = 1 ; i <= 4 ; i++) {
107- if (i == 1 ) {
108- i++;
109- }
101+ if (MaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 2 ))) {
102+ if (SecondMaxFitStatus != -1 ) {
103+ BestLine1 = rand () % 2 == 0 ? GetLineFromMatrix (FirstPop, LineSymb, 2 ) : BestLine1;
104+ }
105+ else {
106+ BestLine1 = GetLineFromMatrix (FirstPop, LineSymb, 2 );
107+ }
108+
109+ SecondMaxFitStatus = max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 3 )), max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 1 )), FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 4 ))));
110+
111+ for (int i = 1 ; i <= 4 ; i++) {
112+ if (i == 2 ) {
113+ i++;
114+ }
115+ if (BestLine2NotDefault == -1 ) {
110116 if (SecondMaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, i))) {
111117 BestLine2 = GetLineFromMatrix (FirstPop, LineSymb, i);
118+ BestLine2NotDefault = 0 ;
112119 }
113120 }
114- }
115-
116- if (MaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 2 ))) {
117- if (SecondMaxFitStatus != -1 ) {
118- BestLine1 = rand () % 2 == 0 ? GetLineFromMatrix (FirstPop, LineSymb, 2 ) : BestLine1;
119- }
120121 else {
121- BestLine1 = GetLineFromMatrix (FirstPop, LineSymb, 2 );
122+ if (SecondMaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, i))) {
123+ BestLine2 = rand () % 2 == 0 ? GetLineFromMatrix (FirstPop, LineSymb, i) : BestLine2;
124+ }
122125 }
126+ }
127+ }
123128
124- SecondMaxFitStatus = max ( FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 3 )), max ( FitStatus ( GetLineFromMatrix (FirstPop, LineSymb, 1 )), FitStatus ( GetLineFromMatrix (FirstPop, LineSymb, 4 ))));
129+ if (MaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 3 ))) {
125130
126- for (int i = 1 ; i <= 4 ; i++) {
127- if (i == 2 ) {
128- i++;
129- }
130- if (BestLine2NotDefault == -1 ) {
131- if (SecondMaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, i))) {
132- BestLine2 = GetLineFromMatrix (FirstPop, LineSymb, i);
133- BestLine2NotDefault = 0 ;
134- }
135- }
136- else {
137- if (SecondMaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, i))) {
138- BestLine2 = rand () % 2 == 0 ? GetLineFromMatrix (FirstPop, LineSymb, i) : BestLine2;
139- }
140- }
141- }
131+ if (SecondMaxFitStatus != -1 ) {
132+ BestLine1 = rand () % 2 == 0 ? GetLineFromMatrix (FirstPop, LineSymb, 3 ) : BestLine1;
133+ }
134+ else {
135+ BestLine1 = GetLineFromMatrix (FirstPop, LineSymb, 3 );
142136 }
143137
144- if (MaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 3 ))) {
138+ SecondMaxFitStatus = max ( FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 2 )), max ( FitStatus ( GetLineFromMatrix (FirstPop, LineSymb, 1 )), FitStatus ( GetLineFromMatrix (FirstPop, LineSymb, 4 ))));
145139
146- if (SecondMaxFitStatus != -1 ) {
147- BestLine1 = rand () % 2 == 0 ? GetLineFromMatrix (FirstPop, LineSymb, 3 ) : BestLine1;
140+ for (int i = 1 ; i <= 4 ; i++) {
141+ if (i == 3 ) {
142+ i++;
143+ }
144+ if (BestLine2NotDefault == -1 ) {
145+ if (SecondMaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, i))) {
146+ BestLine2 = GetLineFromMatrix (FirstPop, LineSymb, i);
147+ BestLine2NotDefault = 0 ;
148+ }
148149 }
149150 else {
150- BestLine1 = GetLineFromMatrix (FirstPop, LineSymb, 3 );
151+ if (SecondMaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, i))) {
152+ BestLine2 = rand () % 2 == 0 ? GetLineFromMatrix (FirstPop, LineSymb, i) : BestLine2;
153+ }
151154 }
155+ }
156+ }
157+
158+ if (MaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 4 ))) {
159+ if (SecondMaxFitStatus != -1 ) {
160+ BestLine1 = rand () % 2 == 0 ? GetLineFromMatrix (FirstPop, LineSymb, 4 ) : BestLine1;
161+ }
162+ else {
163+ BestLine1 = GetLineFromMatrix (FirstPop, LineSymb, 4 );
164+ }
152165
153- SecondMaxFitStatus = max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 2 )), max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 1 )), FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 4 ))));
166+ SecondMaxFitStatus = max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 2 )), max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 1 )), FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 3 ))));
154167
155- for (int i = 1 ; i <= 4 ; i++) {
156- if (i == 3 ) {
157- i++;
158- }
159- if (BestLine2NotDefault == -1 ) {
160- if (SecondMaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, i))) {
161- BestLine2 = GetLineFromMatrix (FirstPop, LineSymb, i);
162- BestLine2NotDefault = 0 ;
163- }
168+ for (int i = 1 ; i <= 4 ; i++) {
169+ if (i == 4 ) {
170+ break ;
171+ }
172+ if (BestLine2NotDefault == -1 ) {
173+ if (SecondMaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, i))) {
174+ BestLine2 = GetLineFromMatrix (FirstPop, LineSymb, i);
175+ BestLine2NotDefault = 0 ;
164176 }
165- else {
166- if (SecondMaxFitStatus == FitStatus ( GetLineFromMatrix (FirstPop, LineSymb, i))) {
167- BestLine2 = rand () % 2 == 0 ? GetLineFromMatrix (FirstPop, LineSymb, i) : BestLine2;
168- }
177+ }
178+ else {
179+ if (SecondMaxFitStatus == FitStatus ( GetLineFromMatrix (FirstPop, LineSymb, i))) {
180+ BestLine2 = rand () % 2 == 0 ? GetLineFromMatrix (FirstPop, LineSymb, i) : BestLine2;
169181 }
170182 }
171183 }
184+ }
185+ if (BestLine1.empty () == true ) {
186+ return BestLine1;
187+ }
188+ else {
189+ return BestLine2;
190+ }
191+ }
172192
173- if (MaxFitStatus == FitStatus ( GetLineFromMatrix (FirstPop, LineSymb, 4 )) ) {
174- if (SecondMaxFitStatus != - 1 ) {
175- BestLine1 = rand () % 2 == 0 ? GetLineFromMatrix (FirstPop, LineSymb, 4 ) : BestLine1;
176- }
177- else {
178- BestLine1 = GetLineFromMatrix (FirstPop, LineSymb, 4 ) ;
179- }
193+ vector< bool > IdealLine ( int LineSym ) {
194+ vector< bool > IdealLine1;
195+ for ( int i = 0 ; i < LineSym; i++) {
196+ IdealLine1. push_back ( true );
197+ }
198+ return IdealLine1 ;
199+ }
180200
181- SecondMaxFitStatus = max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 2 )), max (FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 1 )), FitStatus (GetLineFromMatrix (FirstPop, LineSymb, 3 ))));
201+ void RandomNumCheck (int cycles, int LineSymb) {
202+ vector<bool > test;
203+ for (int i = 0 ; i < cycles * LineSymb * 4 ; i++) {
204+ rand () % 2 == 0 ? test.push_back (true ) : test.push_back (false );
205+
206+ }
207+ int countertest = 0 ;
208+ if (GetLineFromMatrix (test, LineSymb, 1 ) == IdealLine (LineSymb))
209+ cout << " counter=1\n " ;
210+ int countertest2 = 2 ;
211+ for (int i = 0 ; i < cycles * 4 ; i++) {
212+ if (countertest == LineSymb - 1 ) {
213+ if (GetLineFromMatrix (test, LineSymb, i + 2 ) == IdealLine (LineSymb)) {
214+ cout << " counter: " << countertest2 << endl;
182215
183- for (int i = 1 ; i <= 4 ; i++) {
184- if (i == 4 ) {
185- break ;
186- }
187- if (BestLine2NotDefault == -1 ) {
188- if (SecondMaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, i))) {
189- BestLine2 = GetLineFromMatrix (FirstPop, LineSymb, i);
190- BestLine2NotDefault = 0 ;
191- }
192- }
193- else {
194- if (SecondMaxFitStatus == FitStatus (GetLineFromMatrix (FirstPop, LineSymb, i))) {
195- BestLine2 = rand () % 2 == 0 ? GetLineFromMatrix (FirstPop, LineSymb, i) : BestLine2;
196- }
197- }
198216 }
217+ countertest2++;
218+ countertest = 0 ;
199219 }
200- cout << " BestLine1:\n " ;
201- PrintMatrix (BestLine1, LineSymb);
202- cout << " \n BestLine2:\n " ;
203- PrintMatrix (BestLine2, LineSymb);
204- cout << " \n End of the BestLines\n " ;
220+ countertest++;
221+ }
222+ }
223+
224+ int main () {
225+ // VERY IMPORTANT LINE, IT IS NECESSARY FOR THE RAND() FUNCTION TO WORK CORRECTLY.
226+ srand (time (0 ));
227+
228+ // GeneticalAlgorithm
229+ start:
230+ int MutationProb = 5 ;
231+ int cycles=0 ;
232+ int LineSymb = 10 ;
233+ vector<bool > FirstPop;
234+ FirstPop = RandomMatrix (LineSymb, 4 );
235+
236+
237+
238+ while (true ) {
239+ // cout << "Here we have some func ->\n 1 to Generate New First Population \n 2 To display First Population \n 3 to Get BestLines From current population \n 4 to BestLine Mutation \n5 to include bestline in new First Population"
240+ // cout << "FirstPop:\n";
241+ // PrintMatrix(FirstPop, LineSymb);
242+ // cout << "\nEnd of FirstPop.\n";
243+ cycles++;
244+ vector<bool > BestLine1 = BestLineSearch (FirstPop, LineSymb);
245+ vector<bool > BestLine2 = BestLineSearch (FirstPop, LineSymb, BestLine1);
246+ // cout << "BestLine1:\n";
247+ // PrintMatrix(BestLine1, LineSymb);
248+ // cout << "\nBestLine2:\n";
249+ // PrintMatrix(BestLine2, LineSymb);
250+ // cout << "\nEnd of the BestLines\n";
205251
206252 vector<bool > temp1;
207253 for (int i = 0 ; i < (LineSymb/2 ); i++) {
@@ -215,34 +261,23 @@ int main() {
215261 }
216262 rand () % 2 == 0 ? BestLine1 = MutationProc (BestLine1, MutationProb) : BestLine2 = MutationProc (BestLine2, MutationProb);
217263
218- cout << " BestLine1 Mutated:\n " ;
219- PrintMatrix (BestLine1, LineSymb);
220- cout << " \n BestLine2 Mutated:\n " ;
221- PrintMatrix (BestLine2, LineSymb);
222- cout << " \n End of the BestLines Mutated\n " ;
264+ // cout << "BestLine1 Mutated:\n";
265+ // PrintMatrix(BestLine1, LineSymb);
266+ // cout << "\nBestLine2 Mutated:\n";
267+ // PrintMatrix(BestLine2, LineSymb);
268+ // cout << "\nEnd of the BestLines Mutated\n";
223269
224- int TrueCounter1 = 0 ;
225- int TrueCounter2 = 0 ;
226- for (int i = 0 ; i < LineSymb; i++) {
227- if (BestLine1[i] == true ) {
228- TrueCounter1++;
229- }
230- if (BestLine2[i] == true ) {
231- TrueCounter2++;
232- }
233- }
234- if (TrueCounter1 == LineSymb || TrueCounter2 == LineSymb) {
235- cout << " BestLine1 ClearEdition:\n " ;
236- PrintMatrix (BestLine1, LineSymb);
237- cout << " \n BestLine2 ClearEdition:\n " ;
238- PrintMatrix (BestLine2, LineSymb);
270+ if (BestLine1 == IdealLine (LineSymb) || BestLine2 == IdealLine (LineSymb)) {
271+ // cout << "BestLine1 ClearEdition:\n";
272+ // PrintMatrix(BestLine1, LineSymb);
273+ // cout << "\nBestLine2 ClearEdition:\n";
274+ // PrintMatrix(BestLine2, LineSymb);
239275 cout << " \n Cycles: " << cycles << endl;
240- cout << " \n End of the BestLines ClearEdition\n " ;
276+ RandomNumCheck (cycles, LineSymb);
277+ // cout << "\nEnd of the BestLines ClearEdition\n";
241278 system (" pause" );
242279 break ;
243280 }
244- TrueCounter1 = 0 ;
245- TrueCounter2 = 0 ;
246281 FirstPop.clear ();
247282 FirstPop = RandomMatrix (LineSymb, 2 );
248283 for (int i = 0 ; i < LineSymb; i++) {
@@ -254,5 +289,6 @@ int main() {
254289 BestLine1.clear ();
255290 BestLine2.clear ();
256291 }
292+ goto start;
257293 return 0 ;
258294}
0 commit comments