-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tester.cpp
45 lines (43 loc) · 1.8 KB
/
Tester.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
// Реализация класса, тестирующего решения задачи коммивояжера
#include <iostream>
#include <cmath>
#include "PointsGenerator.hpp"
#include "MSTravellingSalesmanProblem.hpp"
#include "TravellingSalesmanProblem.hpp"
#include "Tester.hpp"
Tester::Tester(int from, int to, int num_of_repetitions) {
this->from = from;
this->to = to;
for(int i = from; i <= to; ++i) {
test_with_points(i, num_of_repetitions);
}
}
void Tester::draw_statistics() const {
for(int i = from; i <= to; ++i) {
std::cout.fixed;
std::cout.precision(4);
std::cout << i << ": " << "average factor = " << average_factors[i - from] << " standart deviation = " << standart_factor_deviations[i - from] << std::endl;
}
}
void Tester::test_with_points(int num_of_points, int num_of_repetitions) {
PointsGenerator gen(num_of_points);
std::vector<double> factors(num_of_repetitions);
double sum_factors = 0;
for(int i = 0; i < num_of_repetitions; ++i) {
auto points = gen.get_points();
gen.generate_points(num_of_points);
MSTTravellingSalesmanProblem MST_solution(points);
TravellingSalesmanProblem brute_solution(points);
factors[i] = MST_solution.get_distance() / brute_solution.get_distance();
sum_factors += factors[i];
}
average_factors.push_back(sum_factors / num_of_repetitions);
// Рассчитаем стандартное отклонение
double average_difference = sum_factors / num_of_repetitions;
double standart_deviation = 0;
for(auto value: factors) {
standart_deviation += pow(value - average_difference, 2);
}
standart_deviation = sqrt(standart_deviation / num_of_repetitions);
standart_factor_deviations.push_back(standart_deviation);
}