Skip to content
This repository was archived by the owner on Jun 17, 2024. It is now read-only.

Commit f6774e6

Browse files
authored
Add files via upload
Fixed a bug in the algorithm for calculating the final vector.
1 parent f24fb58 commit f6774e6

8 files changed

Lines changed: 64 additions & 43 deletions

File tree

2 KB
Binary file not shown.
-125 KB
Binary file not shown.
-40 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

GeneticalAlgorythm/GeneticalAlgorythm/GeneticalAlgorythm.cpp

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -84,145 +84,166 @@ int main() {
8484
//End of TestCode
8585

8686
//GeneticalAlgorithm
87+
int cycles=0;
88+
int LineSymb = 10;
8789
vector<bool> FirstPop;
88-
FirstPop = RandomMatrix(6, 4);
90+
FirstPop = RandomMatrix(LineSymb, 4);
91+
cout << "FirstPop:\n";
92+
PrintMatrix(FirstPop, LineSymb);
93+
cout << "\nEnd of FirstPop.\n";
8994

9095
while (true) {
96+
cycles++;
9197
vector<bool> BestLine1;
9298
vector<bool> BestLine2;
9399
int BestLine2NotDefault = -1;
94-
int MaxFitStatus = max(FitStatus(GetLineFromMatrix(FirstPop, 6, 1)), max(FitStatus(GetLineFromMatrix(FirstPop, 6, 2)), max(FitStatus(GetLineFromMatrix(FirstPop, 6, 3)), FitStatus(GetLineFromMatrix(FirstPop, 6, 4)))));
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)))));
95101
int SecondMaxFitStatus = -1;
96102

97-
if (MaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, 6, 1))) {
98-
SecondMaxFitStatus = max(FitStatus(GetLineFromMatrix(FirstPop, 6, 2)), max(FitStatus(GetLineFromMatrix(FirstPop, 6, 3)), FitStatus(GetLineFromMatrix(FirstPop, 6, 4))));
99-
BestLine1 = GetLineFromMatrix(FirstPop, 6, 1);
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);
100106
for (int i = 1; i <= 4; i++) {
101107
if (i == 1) {
102108
i++;
103109
}
104-
if (SecondMaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, 6, i))) {
105-
BestLine2 = GetLineFromMatrix(FirstPop, 6, i);
110+
if (SecondMaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, LineSymb, i))) {
111+
BestLine2 = GetLineFromMatrix(FirstPop, LineSymb, i);
106112
}
107113
}
108114
}
109115

110-
if (MaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, 6, 2))) {
116+
if (MaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, LineSymb, 2))) {
111117
if (SecondMaxFitStatus != -1) {
112-
BestLine1 = rand() % 2 == 0 ? GetLineFromMatrix(FirstPop, 6, 2) : BestLine1;
118+
BestLine1 = rand() % 2 == 0 ? GetLineFromMatrix(FirstPop, LineSymb, 2) : BestLine1;
113119
}
114120
else {
115-
BestLine1 = GetLineFromMatrix(FirstPop, 6, 2);
121+
BestLine1 = GetLineFromMatrix(FirstPop, LineSymb, 2);
116122
}
117123

118-
SecondMaxFitStatus = max(FitStatus(GetLineFromMatrix(FirstPop, 6, 3)), max(FitStatus(GetLineFromMatrix(FirstPop, 6, 1)), FitStatus(GetLineFromMatrix(FirstPop, 6, 4))));
124+
SecondMaxFitStatus = max(FitStatus(GetLineFromMatrix(FirstPop, LineSymb, 3)), max(FitStatus(GetLineFromMatrix(FirstPop, LineSymb, 1)), FitStatus(GetLineFromMatrix(FirstPop, LineSymb, 4))));
119125

120126
for (int i = 1; i <= 4; i++) {
121127
if (i == 2) {
122128
i++;
123129
}
124130
if (BestLine2NotDefault == -1) {
125-
if (SecondMaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, 6, i))) {
126-
BestLine2 = GetLineFromMatrix(FirstPop, 6, i);
131+
if (SecondMaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, LineSymb, i))) {
132+
BestLine2 = GetLineFromMatrix(FirstPop, LineSymb, i);
127133
BestLine2NotDefault = 0;
128134
}
129135
}
130136
else {
131-
if (SecondMaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, 6, i))) {
132-
BestLine2 = rand() % 2 == 0 ? GetLineFromMatrix(FirstPop, 6, i) : BestLine2;
137+
if (SecondMaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, LineSymb, i))) {
138+
BestLine2 = rand() % 2 == 0 ? GetLineFromMatrix(FirstPop, LineSymb, i) : BestLine2;
133139
}
134140
}
135141
}
136142
}
137143

138-
if (MaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, 6, 3))) {
144+
if (MaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, LineSymb, 3))) {
139145

140146
if (SecondMaxFitStatus != -1) {
141-
BestLine1 = rand() % 2 == 0 ? GetLineFromMatrix(FirstPop, 6, 3) : BestLine1;
147+
BestLine1 = rand() % 2 == 0 ? GetLineFromMatrix(FirstPop, LineSymb, 3) : BestLine1;
142148
}
143149
else {
144-
BestLine1 = GetLineFromMatrix(FirstPop, 6, 3);
150+
BestLine1 = GetLineFromMatrix(FirstPop, LineSymb, 3);
145151
}
146152

147-
SecondMaxFitStatus = max(FitStatus(GetLineFromMatrix(FirstPop, 6, 2)), max(FitStatus(GetLineFromMatrix(FirstPop, 6, 1)), FitStatus(GetLineFromMatrix(FirstPop, 6, 4))));
153+
SecondMaxFitStatus = max(FitStatus(GetLineFromMatrix(FirstPop, LineSymb, 2)), max(FitStatus(GetLineFromMatrix(FirstPop, LineSymb, 1)), FitStatus(GetLineFromMatrix(FirstPop, LineSymb, 4))));
148154

149155
for (int i = 1; i <= 4; i++) {
150156
if (i == 3) {
151157
i++;
152158
}
153159
if (BestLine2NotDefault == -1) {
154-
if (SecondMaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, 6, i))) {
155-
BestLine2 = GetLineFromMatrix(FirstPop, 6, i);
160+
if (SecondMaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, LineSymb, i))) {
161+
BestLine2 = GetLineFromMatrix(FirstPop, LineSymb, i);
156162
BestLine2NotDefault = 0;
157163
}
158164
}
159165
else {
160-
if (SecondMaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, 6, i))) {
161-
BestLine2 = rand() % 2 == 0 ? GetLineFromMatrix(FirstPop, 6, i) : BestLine2;
166+
if (SecondMaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, LineSymb, i))) {
167+
BestLine2 = rand() % 2 == 0 ? GetLineFromMatrix(FirstPop, LineSymb, i) : BestLine2;
162168
}
163169
}
164170
}
165171
}
166172

167-
if (MaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, 6, 4))) {
173+
if (MaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, LineSymb, 4))) {
168174
if (SecondMaxFitStatus != -1) {
169-
BestLine1 = rand() % 2 == 0 ? GetLineFromMatrix(FirstPop, 6, 4) : BestLine1;
175+
BestLine1 = rand() % 2 == 0 ? GetLineFromMatrix(FirstPop, LineSymb, 4) : BestLine1;
170176
}
171177
else {
172-
BestLine1 = GetLineFromMatrix(FirstPop, 6, 4);
178+
BestLine1 = GetLineFromMatrix(FirstPop, LineSymb, 4);
173179
}
174180

175-
SecondMaxFitStatus = max(FitStatus(GetLineFromMatrix(FirstPop, 6, 2)), max(FitStatus(GetLineFromMatrix(FirstPop, 6, 1)), FitStatus(GetLineFromMatrix(FirstPop, 6, 3))));
181+
SecondMaxFitStatus = max(FitStatus(GetLineFromMatrix(FirstPop, LineSymb, 2)), max(FitStatus(GetLineFromMatrix(FirstPop, LineSymb, 1)), FitStatus(GetLineFromMatrix(FirstPop, LineSymb, 3))));
176182

177183
for (int i = 1; i <= 4; i++) {
178184
if (i == 4) {
179185
break;
180186
}
181187
if (BestLine2NotDefault == -1) {
182-
if (SecondMaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, 6, i))) {
183-
BestLine2 = GetLineFromMatrix(FirstPop, 6, i);
188+
if (SecondMaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, LineSymb, i))) {
189+
BestLine2 = GetLineFromMatrix(FirstPop, LineSymb, i);
184190
BestLine2NotDefault = 0;
185191
}
186192
}
187193
else {
188-
if (SecondMaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, 6, i))) {
189-
BestLine2 = rand() % 2 == 0 ? GetLineFromMatrix(FirstPop, 6, i) : BestLine2;
194+
if (SecondMaxFitStatus == FitStatus(GetLineFromMatrix(FirstPop, LineSymb, i))) {
195+
BestLine2 = rand() % 2 == 0 ? GetLineFromMatrix(FirstPop, LineSymb, i) : BestLine2;
190196
}
191197
}
192198
}
193199
}
200+
cout << "BestLine1:\n";
201+
PrintMatrix(BestLine1, LineSymb);
202+
cout << "\nBestLine2:\n";
203+
PrintMatrix(BestLine2, LineSymb);
204+
cout << "\nEnd of the BestLines\n";
205+
194206
vector<bool> temp1;
195-
for (int i = 0; i < 3; i++) {
207+
for (int i = 0; i < (LineSymb/2); i++) {
196208
temp1.push_back(BestLine1[i]);
197209
}
198-
for (int i = 0; i < 3; i++) {
210+
for (int i = 0; i < (LineSymb / 2); i++) {
199211
BestLine1[i] = BestLine2[i];
200212
}
201-
for (int i = 0; i < 3; i++) {
213+
for (int i = 0; i < (LineSymb / 2); i++) {
202214
BestLine2[i] = temp1[i];
203215
}
204216
rand() % 2 == 0 ? BestLine1 = MutationProc(BestLine1) : BestLine2 = MutationProc(BestLine2);
217+
218+
cout << "BestLine1 Mutated:\n";
219+
PrintMatrix(BestLine1, LineSymb);
220+
cout << "\nBestLine2 Mutated:\n";
221+
PrintMatrix(BestLine2, LineSymb);
222+
cout << "\nEnd of the BestLines Mutated\n";
223+
205224
int TrueCounter1 = 0;
206225
int TrueCounter2 = 0;
207-
for (int i = 0; i < 6; i++) {
208-
if (BestLine1[i] = true) {
226+
for (int i = 0; i < LineSymb; i++) {
227+
if (BestLine1[i] == true) {
209228
TrueCounter1++;
210229
}
211-
if (BestLine2[i] = true) {
230+
if (BestLine2[i] == true) {
212231
TrueCounter2++;
213232
}
214233
}
215-
if (TrueCounter1 == 6 || TrueCounter2 == 6) {
216-
PrintMatrix(BestLine1, 6);
217-
cout << "\n";
218-
PrintMatrix(BestLine2, 6);
219-
cout << "\n";
234+
if (TrueCounter1 == LineSymb || TrueCounter2 == LineSymb) {
235+
cout << "BestLine1 ClearEdition:\n";
236+
PrintMatrix(BestLine1, LineSymb);
237+
cout << "\nBestLine2 ClearEdition:\n";
238+
PrintMatrix(BestLine2, LineSymb);
239+
cout << "\nCycles: " << cycles << endl;
240+
cout << "\nEnd of the BestLines ClearEdition\n";
220241
system("pause");
221242
}
222243
TrueCounter1 = 0;
223244
TrueCounter2 = 0;
224245
FirstPop.clear();
225-
FirstPop = RandomMatrix(6, 2);
246+
FirstPop = RandomMatrix(LineSymb, 2);
226247
for (int i = 0; i < 6; i++) {
227248
FirstPop.push_back(BestLine1[i]);
228249
}

0 commit comments

Comments
 (0)