-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
103 lines (82 loc) · 2.64 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <fstream>
#include <sstream>
#include "CKlauzule.h"
#include "CGenerace.h"
#define SOUBOR "v100_c250_5.cnf"
using namespace std;
int main() {
ifstream inputFile;
string fileInst;
string line;
CKlauzule * klauzule;
int * vahy;
bool maVahu = 0;
string del;
int pocetPromennych = 0;
int pocetKlauzuli = 0;
int velikostPopulace = 100;
int pocetGeneraci = 100;
double pravdepodobnostKrizeni = 0.1;
double pravdepodobnostMutace = 0.3;
int elitismus = 1;
double timeSum = 0;
int count = 0;
fileInst = (string)"inst/" + SOUBOR;
inputFile.open( fileInst.c_str() );
while ( getline(inputFile, line) )
{
if ( inputFile.eof() && line.empty() ) break;
istringstream ss(line);
// komentar
if ( line[0] == 'c' ) {
//cout << line << endl;
}
// informace o instanci
if ( line[0] == 'p' ) {
ss >> del >> del >> pocetPromennych >> pocetKlauzuli;
klauzule = new CKlauzule[pocetKlauzuli];
}
// vahy
if ( line[0] == 'w' ) {
maVahu = 1;
vahy = new int [pocetPromennych];
ss >> del;
for (int i = 0; i < pocetPromennych; ++i) {
ss >> vahy[i];
}
}
if ( line[0] != 'c' && line[0] != 'p' && line[0] != 'w' ) {
ss >> klauzule[count].var1 >> klauzule[count].var2 >> klauzule[count].var3 >> del;
count++;
}
}
// if ( maVahu ) {
// for (int i = 0; i < pocetPromennych; ++i) {
// cout << vahy[i] << " ";
// }
// cout << endl;
// }
//
// for (int i = 0; i < pocetKlauzuli; ++i) {
// cout << klauzule[i].var1 << " " << klauzule[i].var2 << " " << klauzule[i].var3 << endl;
// }
clock_t timeStart = clock();
CGenerace * generace = new CGenerace(pocetPromennych, velikostPopulace, pocetKlauzuli, klauzule, vahy);
for (int i = 0; i < pocetGeneraci; ++i) {
generace->evoluce(pravdepodobnostKrizeni, pravdepodobnostMutace, elitismus);
//cout << generace->nejlepsiFitness()<< endl;
cout << i + 1 << ". " << generace->splneneKlauzule() << "/" << pocetKlauzuli << endl;
//cout << generace->splneneKlauzule() << endl;
}
cout << "X = {";
generace->vypsatNejlepsiReseni();
cout << "}" << endl;
cout << "S = " << generace->nejlepsiVaha() << endl;
double duration = ( clock() - timeStart ) / (double) CLOCKS_PER_SEC;
delete generace;
delete klauzule;
cout << "Čas zpracování instance: " << duration << endl;
inputFile.close();
return 0;
}