Tu minimalnie ulepszona wersja:
g++ -std=c++11 -O3 main.cpp -o genetic
root@SSDB:/home/m/Dokumenty/c/eduGetLineZero# time ./genetic
seed = 1573694515
Podaj rozmiar populacji: 100000
Podaj ilosc rodzicow: 50000
Podaj udzwig plecaka: 2000
Podaj raz na ile razy mayc mutacja: 10000
Podaj elementy (waga,wartość) [ctrl+d kończy]
60.9
54.4
41.76
3.58
7.94
84.25
91.7
91.4
38.85
0.4
3.3
44.46
77.09
29.61
47.22
12.12
18.29
61.71
15.59
63.67
42.84
92.92
3.2
43.81
74.44
19.67
41.4
62.09
23.98
33.96
53.34
2.97
39.53
80.08
5.63
15.37
82.18
36.22
59.57
5.5
0.18
10.68
65
81.51
32.85
73.79
24.06
73.95
22.41
31.68
5.75
89.74
59.1
29.8
56.13
5.79
76.07
39.92
32.46
78.4
76.42
11
97.11
21.37
54.33
75.41
60.64
47.62
25.41
81.42
14.78
88.96
56.37
81.9
35.82
16.04
42.35
42.34
25.76
88.48
63.27
44.81
71.77
7.1
49.56
26.26
31.66
36.36
43.84
12.53
47.11
68.74
72.03
85.93
34.7
64.92
5.41
74.03
8.91
0.89
71.15
51.43
14.01
34.94
22.98
27.1
32.04
80.62
77.59
56.45
8.03
35.25
71.63
76.49
11.01
72.88
51.73
91.01
31.81
69.8
60.08
94.75
78.24
25.55
82.13
74.49
3.25
34.26
63.25
86.11
49.75
60.4
69.89
50.5
13.68
15.27
44.46
13.92
71.94
7.73
79.45
74.9
87.07
98.1
58.66
85.22
79.18
6.18
57.06
72.96
86.13
18.04
31.8
99.71
75.22
54.04
29.45
46.16
12.97
18.59
85.77
11.72
94.42
47.67
25.86
37.58
63.79
73.79
69.33
56.02
75.05
62.51
3.83
50.59
62.59
60.91
1.8
90.83
56.44
67.85
37.61
69.83
29.99
53.49
38.73
72.91
2.26
97.25
61.66
10.23
54.49
67.64
79.98
84.34
31.55
49.29
14.61
98.07
31.48
62.85
98.04
96.28
36.33
50.87
33.2
53.48
37.25
30.89
15.77
32.62
87.92
14.59
31.63
99.69
95.22
40.94
60.15
71.15
16.97
78.5
34.89
16.87
96.6
19.88
13.21
77.02
46.41
85.2
12.17
90.9
42.47
76.12
79.29
22.93
99.8
74.72
32.14
20.76
56.95
43.87
53.93
28.49
8.38
63.98
53.23
61.35
76.22
53.47
83.55
38.18
98.16
56.91
37.82
74.35
4.08
5.08
78.97
3.59
77.45
84.84
20.67
72.27
74.01
41.63
90.66
88.72
33.58
81.62
15.44
48.44
20.82
25.51
18.55
93.33
60.87
87.75
62.13
66.82
54.73
54
60.73
98.24
59.63
42.04
95.26
50.86
42.98
94.9
88.63
75.04
26
24.08
42.3
89.12
12.23
83.99
2.8
90.22
97.07
96.08
Wczyano 150 elementow
best[0]: 3381.19, 1999.7 [101011001001011010000001010000000111110100000011111011010110000001001000110011010110101110110100001011010000110101100110000001011100011101010001010100]
31s; best[2]: 3453.94, 1999.85 [101001101011010011011111010111001011000010000101011100010110001110010001100000110000101111011101111001000010100100111100110101111110000100110111111011]
33s; best[3]: 3582.6, 1999.66 [111001011111001010001111101000001111111100000011100110010101100011010011000011110111001000101101011000100110011100101111001001100100111001011010001110]
35s; best[4]: 3641.05, 1994.5 [011011001001000001001111111001000011101100100011010111011101100000010001100011110000001110011001011110001010010001110101000011111010110010101110001101]
37s; best[5]: 3743.07, 1995.58 [011001101111110011000001011001001011100100100010110110010101101110001000001010100011101110100111011010001010111111100000001001110010000101110010111110]
39s; best[6]: 3877.6, 1996.61 [011001001100001010000111011001000011100110000000110001111110100010100000100010010001011010001101011111101110000010110000000100110111001100111010001110]
44s; best[8]: 3930.57, 1995.82 [000001001101001000000001111001001011110100010010101101010101100010000001101010100000011011110101111111110010100011100101010110101100110100010010000110]
46s; best[9]: 4034.31, 1998.93 [001001011101010011000111110101001011110110001010100100010111100100000000001010100010001011001101011000100010111111110110110101101000100011000101110110]
50s; best[11]: 4185.72, 1999.07 [101001010101011000001011010001001011110100000111100101010111100111110010100010110000001011011100001101100010010011100010111100111010110110000000101010]
52s; best[12]: 4186.25, 1992.5 [101001001110010011000101110001000111100100000101100101010111101110010010100010110000100010101100001110101010011001100011010100101010101010110010010110]
55s; best[13]: 4295.49, 1981.17 [001101001101000010001111110001000011000100010100100111010111100110010001100010000000000010011100011011001010110011111000000100010010011011000010011111]
57s; best[14]: 4306.62, 1980.44 [001001001111000010001111010001000011101110000011100111011111000011000000100010100001001110110101101101001010110010110011111010000011001011011101110010]
61s; best[16]: 4423.07, 1994.3 [001001000111000011001111110001001111110100100001100111011111000101000000001010100001001011101100001101011010111010100010010000100010001111111000101110]
64s; best[17]: 4430.05, 1981.32 [001001001111011011001011110001001011000100010110100101011111100100110001100011000000001011101100101000110010010011110100010000000000010011001110101111]
66s; best[18]: 4580.48, 1992.36 [001001001111001010001011110001001011100100010111100101011111101110010000000011000010001010101100011101101010010011100010011100100110011110100011110111]
74s; best[21]: 4682.99, 1992.25 [001001001101010011001011010000001011101100000101100001011111100110010000101010110010001011111101011101101010010011110000011000100010111111110101001111]
80s; best[24]: 4683.64, 1975.94 [001101001111010011001011010001000011100100010101110101011111100101000000100010000000001010111101011101101010110011110001011000100010011011111011100111]
83s; best[25]: 4752.79, 1997.07 [001001001111010011000111010001001011000100000111100101010111100101010000101010100010001010111101011001000010010011110000011000110110010011011000001111]
85s; best[26]: 4779.02, 1996.49 [001001001111001011001011110001001011100100010111100101011111100110000000101010110000001010111101001100111010010011110000011000010110011011000001001111]
89s; best[28]: 4806.84, 1996.32 [001001001111010011000011110001001011000100000101100101010111100110010000101010110001001011111101011100111010010011110000010000100100011110001001011110]
94s; best[30]: 4845.39, 1989.39 [001001001111011011001011110001001011000100010101100111010111100110010000101010010010001011111101111101001010010011110000010000100010011111001001101111]
98s; best[32]: 4846.74, 1998.12 [001001001111011011001111110001000011100100000101110101010111100101000000101010110010001010101101011100101010110011110000011000100010011011101001111110]
100s; best[33]: 4871.76, 1992.47 [001001001111010011001011110001001011100100000101100101011111100111000000100010100011001010111100011101101010110011110000010000110010001011001001001111]
102s; best[34]: 4889.05, 1990.76 [001001001111010011001011110001001011100100000001100101010111100111000000001010110010001011111100011101101010010011110000010000110110011011101001101110]
104s; best[35]: 4920.32, 1990.7 [001001001111011011001011110001001011100100000001100111010111100110010000101010110010001011111100011101101010010011110000011000100010011111001001101110]
113s; best[39]: 4926.67, 1990.77 [001001001111010011001111110001001011100100000101100101010111100111010000101010100000001010111101011101101010010011110000010000100010011011001001101111]
115s; best[40]: 4929.2, 1998.61 [001001001111011011001111010001001011100100000101100111010111100110000000100010110010001010111100011101101010010011110000011000110010011011101001101110]
117s; best[41]: 4937.08, 1997.92 [001001001111011011001111110001000011100100000101100101010111100110000000101010110010001011111100011101101010110011110000010000100010011011001001001111]
119s; best[42]: 4947.33, 1999.83 [001001001111011011001011110001001011100100000101100111010111100111000000101010110010001011111100011101101010010011110000010000100010011011001001001111]
124s; best[44]: 4948.05, 1998 [001001001111011011001111110001001011100100000101100101010111100110000000101010100010001010111101011101101010010011110000010000100010011111001001101111]
131s; best[47]: 4949.16, 1999.09 [001001001111011011001111110001001011100100000101100101010111100111010000101010110010001010111100011101101010010011110000010000100010011011001001101111]
135s; best[49]: 4949.72, 1999.89 [001001001111011011001111110001001011100100000101100101010111100110000000101010110010001010111100011101101010110011110000010000110010011011001001101111]
143s; best[53]: 4950.61, 1999.8 [001001001111011011001011110001001011100100000101100101010111100111000000101010110010001010111101011101101010010011110000010000110010011111001001101111]
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
typedef double ftyp;
typedef const ftyp cftyp;
typedef std::ranlux48 Trnd;
typedef std::uniform_int_distribution<int> Tdist;
class MyException : public std::exception {
private:
const std::string swhat;
public:
MyException( const char *const swhat ) : swhat(swhat) {
}
virtual const char* what() const throw(){
return swhat.c_str();
}
};
struct Spec {
std::vector<bool> genotype;
ftyp eval;
ftyp weight;
};
static bool cmpSpec( const Spec &a , const Spec &b ) {
return a.eval > b.eval;
}
static std::ostream& operator<<( std::ostream& dest, const Spec &spec ) {
std::cout << spec.eval << ", " << spec.weight << " [";
for( size_t i=0 ; i<spec.genotype.size() ; i++ ) {
std::cout << (spec.genotype[i] ? "1" : "0");
}
std::cout << "]";
return dest;
}
struct BpItem {
ftyp weight;
ftyp value;
};
static int getSize() {
std::cout << "Podaj rozmiar populacji: ";
int n;
std::cin >> n;
if( std::cin.fail() || n < 1) {
throw MyException("Nieprawidlowy rozmiar populacji");
}
return n;
}
static int getBests( const int size) {
std::cout << "Podaj ilosc rodzicow: ";
int n;
std::cin >> n;
if( std::cin.fail() || n >= size || n < 1) {
throw MyException("Nieprawidlowa ilosc rodzicow");
}
return n;
}
static ftyp getBackpak() {
std::cout << "Podaj udzwig plecaka: ";
ftyp n;
std::cin >> n;
if( std::cin.fail() || n < 0 ) {
throw MyException("Nieprawidlowy udzwig plecaka");
}
return n;
}
static ftyp getMutation() {
std::cout << "Podaj raz na ile razy mayc mutacja: ";
ftyp n;
std::cin >> n;
if( std::cin.fail() || n < 0 ) {
throw MyException("Nieprawidlowa mutacja");
}
return n;
}
static void evalSpec(Spec &spec, cftyp backpack , const std::vector<BpItem> &items ) {
spec.eval = 0;
spec.weight = 0;
for( size_t i=0 ; i<spec.genotype.size() ; i++ ) {
if( ! spec.genotype[i] ) {
continue;
}
if( spec.weight + items[i].weight > backpack ) {
continue;
}
spec.weight += items[i].weight;
spec.eval += items[i].value;
}
}
static void randSpec(
Spec &spec ,
Trnd &rnd,
Tdist &dist,
const std::vector<BpItem> &items
) {
for( size_t i=0 ; i<items.size() ; i++ ) {
spec.genotype.push_back( dist(rnd) == 1 );
}
}
static void mutationSpec(
Spec &spec,
Trnd &rnd
) {
const size_t gen = rnd() % spec.genotype.size();
spec.genotype[ gen ] = !spec.genotype[ gen ];
}
static void cross(
const Spec &a,
const Spec &b,
Spec &out,
Trnd &rnd,
Tdist &dist
) {
for( size_t i=0 ; i<a.genotype.size() ; i++ ) {
if( dist(rnd) ) {
out.genotype[i] = a.genotype[i];
} else {
out.genotype[i] = b.genotype[i];
}
}
}
int main(int argc, char *argv[]) {
(void)argc;
(void)argv;
try {
const int start = (int)time(NULL);
int lastTime = start;
const int seed = start;
std::cout << "seed = " << seed << std::endl;
Trnd rnd( seed );
Tdist bDist(0,1); //binary distribution
const int size = getSize();
const int bests = getBests( size ); //number of parents (parents are bests)
Tdist pDist(0,bests-1); //parnets distribution
cftyp backpack = getBackpak();
const int mutation = getMutation();
std::vector<BpItem> items;
std::cout << "Podaj elementy (waga,wartość) [ctrl+d kończy]" << std::endl;
while( true ) {
BpItem bpItem;
// std::cout << "weight: ";
std::cin >> bpItem.weight;
if( std::cin.eof() ) {
break;
}
if( std::cin.fail() || bpItem.weight < 0 ) {
throw MyException("Nieprawidlowa waga");
}
// std::cout << "value: ";
std::cin >> bpItem.value;
if( std::cin.eof() ) {
break;
}
if( std::cin.fail() || bpItem.value < 0 ) {
throw MyException("Nieprawidlowa wartosc");
}
items.push_back( bpItem );
}
std::cout << "Wczyano " << items.size() << " elementow" << std::endl;
std::vector<Spec> specs(size);
for( int i=0 ; i<size ; i++ ) {
randSpec( specs[i] , rnd, bDist , items );
evalSpec( specs[i] , backpack, items);
}
std::sort(specs.begin(), specs.end(), cmpSpec);
std::cout << "best[0]: " << specs[0] << std::endl;
ftyp bestEval = specs[0].eval;
for( long long loop=1 ; true ; loop++ ) {
for( size_t i=bests ; i<specs.size() ; i++ ) {
cross( specs[ pDist(rnd) ] , specs[ pDist(rnd) ] , specs[ i ] , rnd , bDist );
evalSpec( specs[i] , backpack , items );
}
for( size_t i=1 ; i<specs.size() ; i++ ) {
if( rnd() % mutation == 0 ) {
mutationSpec( specs[i] , rnd );
evalSpec( specs[i] , backpack , items );
}
}
std::sort(specs.begin(), specs.end(), cmpSpec);
if( bestEval < specs[0].eval || time(NULL) - lastTime > 180 ) {
bestEval = specs[0].eval;
std::cout << (time(NULL)-start) << "s; best[" << loop << "]: " << specs[0] << std::endl;
lastTime = time(NULL);
}
}
}
catch( std::exception &e ) {
std::cout << "Wystapił błąd: " << e.what() << std::endl;
}
return 0;
}