-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
189 lines (144 loc) · 5.31 KB
/
main.cpp
File metadata and controls
189 lines (144 loc) · 5.31 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <cstdlib>
#include <iostream>
#include <chrono>
#include <iomanip>
#include "InputOutput.h"
#include "Algorithms.h"
#include "Generator.h"
#include "cxxopts.hpp"
void firstMode() {
std::vector<char> buffer;
try {
buffer = InputOutput::getInput();
Algorithms alg;
std::vector<int> ret;
ret.push_back(alg.dynamicProgramming(buffer));
ret.push_back(alg.alternativeAlgorithm(buffer));
ret.push_back(alg.brutalAlgorithm(buffer));
std::cout << "Dynamic programming algorithm = " << ret[0] << std::endl;
std::cout << "Alternative algorithm = " << ret[0] << std::endl;
std::cout << "Brutal algorithm = " << ret[0] << std::endl;
}
catch (std::string e) {
std::cout << e << std::endl;
return;
}
}
void secondMode(int n, int d) {
std::vector<char> buffer;
std::string generatedData = Generator::generate(n, d);
buffer = InputOutput::getInput(generatedData);
Algorithms alg;
std::vector<int> ret;
ret.push_back(alg.dynamicProgramming(buffer));
ret.push_back(alg.alternativeAlgorithm(buffer));
ret.push_back(alg.brutalAlgorithm(buffer));
std::cout << "Dynamic programming algorithm = " << ret[0] << std::endl;
std::cout << "Alternative algorithm = " << ret[0] << std::endl;
std::cout << "Brutal algorithm = " << ret[0] << std::endl;
}
void thirdMode(int n, int s, int k, int r) {
Algorithms alg;
double avgTime = 0, c = 330 * 4.0 / 100000, tempTime = 0;
std::vector<double> results;
std::vector<char> buffer;
std::cout << " ________________________________" << std::endl;
std::cout << "| ";
std::cout << std::setw(8) << "n" << " | ";
std::cout << std::setw(8) << "t(n)[ms]" << " | ";
std::cout << std::setw(8) << "q(n)" << " |" << std::endl;
for (int i = 0; i < k; ++i, n += s) {
for (int j = 0; j < r; j++) {
double d = i + j;
std::string generatedData = Generator::generate(n, d);
buffer = InputOutput::getInput(generatedData);
auto start = std::chrono::system_clock::now();
alg.dynamicProgramming(buffer);
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now() - start);
tempTime = milliseconds.count();
avgTime += tempTime;
}
avgTime /= r;
results.push_back(avgTime);
std::cout << "| ";
std::cout << std::setw(8) << n << " | ";
std::cout << std::fixed << std::setprecision(3) << std::setw(8) << avgTime << " | ";
std::cout << std::fixed << std::setprecision(6) << std::setw(8) << avgTime / (n * c) << " |" << std::endl;
}
}
int main(int argc, char **argv) {
int m, n, k, s, r, d;
d = m = n = k = s = r = 0;
d = time(NULL);
try {
cxxopts::Options options(argv[0], " - command line options");
options
.positional_help("[optional args]")
.show_positional_help();
options.add_options("Program")
("m1", "Mode with reading from I/O")
("m2", "Mode with generation of data, -n option is obligatory")
("m3", "Full test mode, -n, -k, -s, -r options are obligatory")
("n, number", "Get number of tasks", cxxopts::value<int>())
("d, seed", "Get seed for generator", cxxopts::value<int>())
("s, step", "Get step of tasks in next iterations", cxxopts::value<int>())
("k, kproblem", "Get number of problems", cxxopts::value<int>())
("r, rinstance", "Get number of problem instantions", cxxopts::value<int>())
("h, help", "Print help");
auto result = options.parse(argc, argv);
if (result.count("help")) {
std::cout << options.help({"", "Program"}) << std::endl;
exit(0);
}
if (result.count("m1")) {
m = 1;
std::cout << "mode 1 runned" << std::endl;
}
if (result.count("m2")) {
m = 2;
std::cout << "mode 2 runned" << std::endl;
}
if (result.count("m3")) {
m = 3;
std::cout << "mode 3 runned" << std::endl;
}
if (result.count("n")) {
n = result["n"].as<int>();
std::cout << "n -> " << n << std::endl;
}
if (result.count("s")) {
s = result["s"].as<int>();
std::cout << "step -> " << s << std::endl;
}
if (result.count("k")) {
k = result["k"].as<int>();
std::cout << "k -> " << k << std::endl;
}
if (result.count("r")) {
r = result["r"].as<int>();
std::cout << "r -> " << r << std::endl;
}
if (result.count("d")) {
d = result["d"].as<int>();
std::cout << "d -> " << d << std::endl;
}
} catch (const cxxopts::OptionException &e) {
std::cout << "error parsing options: " << e.what() << std::endl;
exit(1);
}
switch (m) {
case 1:
firstMode();
break;
case 2:
secondMode(n, d);
break;
case 3:
thirdMode(n, s, k, r);
break;
default:
break;
}
return 0;
}