Добавил:
СПбГУТ * ИКСС * Программная инженерия Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Курсовой проект / ММвСС. Курсовой проект. Пример.docx
Скачиваний:
318
Добавлен:
15.01.2020
Размер:
358.46 Кб
Скачать

Приложение 13. Программа для расчета

Файл с входными данными «input_file.txt» для программы

; Комментарий начинается с ;

; Порядок и количество чисел во входном файле должны быть одними и теми же

; [Основные параметры]

20 ; n - количество узлов в сети связи (влияет на матрицу расстояний и вектор кол-ва абонентов)

0.1 ; y0 - интенсивность удельной абонентской нагрузки (Эрл)

85600 ; a0 - скорость потока для кодека (бит/с)

200 ; L - длина пакета (байт, не бит!)

0.98 ; (98%) q - доля вызовов, обслуженных с гарантированным качеством

0.1 ; T0 - начальное требование к величине задержки (сек)

1 ; оптимизация величины задержки T0: 1 - оптимизировать, 0 - не оптимизировать

10000 ; DC - шаг изменения пропускной способности (чем меньше, тем точнее, но дольше)

10000 ; ACCURACY – точность при оптимизации T0 (чем больше, тем точнее, но дольше)

; [Количество абонентов в каждом узле связи]

9780 1664 8037 6279 6162 8629 1068 5665 2821 9452 1450 7220 1303 6809 6214 4662 3021 3775 2207 4265

; [Матрица расстояний]

; Там, где нет прямого пути, должно быть большое число (например, 90000000000)

0 67.26413369 90000000000 38.00244927 63.82799745 66.16874337 96.63601518 90000000000 56.20794892 46.34776711 90000000000 86.64174676 90000000000 60.92620492 90000000000 75.8417666 90000000000 90000000000 75.44823289 90000000000

67.26413369 0 34.49365497 67.4957931 79.3202579 90000000000 55.25647998 90000000000 90000000000 28.34584117 90000000000 77.90572047 90000000000 95.87990642 90.41005969 90000000000 90000000000 1.133698225 90000000000 90000000000

90000000000 34.49365497 0 88.72202039 90000000000 59.64496732 90000000000 90000000000 90000000000 8.052140474 91.92288518 90000000000 90000000000 90000000000 90000000000 65.42547345 42.07881093 90000000000 39.60393071 35.57336926

38.00244927 67.4957931 88.72202039 0 51.52897239 90000000000 69.94108558 7.036393881 90000000000 90000000000 90000000000 45.34987807 90000000000 90000000000 90000000000 74.89871383 90000000000 90000000000 69.75092292 23.80790114

63.82799745 79.3202579 90000000000 51.52897239 0 28.97301316 90000000000 90000000000 90000000000 90000000000 54.61717248 90000000000 13.50856423 98.30322862 90000000000 0.709635019 90000000000 27.92683244 90000000000 90000000000

66.16874337 90000000000 59.64496732 90000000000 28.97301316 0 90000000000 90000000000 52.49039531 90000000000 67.41892695 6.861799955 21.85551524 90000000000 25.56578517 47.97374606 13.31536174 23.59258533 90000000000 90000000000

96.63601518 55.25647998 90000000000 69.94108558 90000000000 90000000000 0 49.69313741 49.96250272 19.88671422 41.54585004 23.5824883 90000000000 36.17108464 66.82069898 90000000000 90000000000 14.58924413 92.51058698 90000000000

90000000000 90000000000 90000000000 7.036393881 90000000000 90000000000 49.69313741 0 90000000000 41.12409949 88.88835311 90000000000 90000000000 90000000000 33.94011855 90000000000 46.73394561 53.60311866 90000000000 90000000000

56.20794892 90000000000 90000000000 90000000000 90000000000 52.49039531 49.96250272 90000000000 0 90000000000 90000000000 90000000000 64.60018754 91.84632897 90000000000 90000000000 90000000000 35.12349725 47.72883058 90000000000

46.34776711 28.34584117 8.052140474 90000000000 90000000000 90000000000 19.88671422 41.12409949 90000000000 0 90000000000 90000000000 90000000000 91.5438354 60.79627872 9.505814314 90000000000 92.36647487 90000000000 74.89144206

90000000000 90000000000 91.92288518 90000000000 54.61717248 67.41892695 41.54585004 88.88835311 90000000000 90000000000 0 90000000000 30.44350743 60.23252606 90000000000 41.61689878 40.18490911 69.58940625 69.72296834 57.04067349

86.64174676 77.90572047 90000000000 45.34987807 90000000000 6.861799955 23.5824883 90000000000 90000000000 90000000000 90000000000 0 0.197738409 90000000000 67.49290824 90000000000 90000000000 72.92098403 30.08007407 62.52145171

90000000000 90000000000 90000000000 90000000000 13.50856423 21.85551524 90000000000 90000000000 64.60018754 90000000000 30.44350743 0.197738409 0 53.51839662 61.88941598 41.35571122 70.09111047 27.83194184 90000000000 90000000000

60.92620492 95.87990642 90000000000 90000000000 98.30322862 90000000000 36.17108464 90000000000 91.84632897 91.5438354 60.23252606 90000000000 53.51839662 0 90000000000 90000000000 90000000000 96.37138247 91.29673839 65.33738971

90000000000 90.41005969 90000000000 90000000000 90000000000 25.56578517 66.82069898 33.94011855 90000000000 60.79627872 90000000000 67.49290824 61.88941598 90000000000 0 90000000000 90000000000 32.29373097 90000000000 90000000000

75.8417666 90000000000 65.42547345 74.89871383 0.709635019 47.97374606 90000000000 90000000000 90000000000 9.505814314 41.61689878 90000000000 41.35571122 90000000000 90000000000 0 90000000000 90000000000 90000000000 90000000000

90000000000 90000000000 42.07881093 90000000000 90000000000 13.31536174 90000000000 46.73394561 90000000000 90000000000 40.18490911 90000000000 70.09111047 90000000000 90000000000 90000000000 0 25.82755685 46.6268599 89.35151696

90000000000 1.133698225 90000000000 90000000000 27.92683244 23.59258533 14.58924413 53.60311866 35.12349725 92.36647487 69.58940625 72.92098403 27.83194184 96.37138247 32.29373097 90000000000 25.82755685 0 90000000000 47.2155273

75.44823289 90000000000 39.60393071 69.75092292 90000000000 90000000000 92.51058698 90000000000 47.72883058 90000000000 69.72296834 30.08007407 90000000000 91.29673839 90000000000 90000000000 46.6268599 90000000000 0 64.40275311

90000000000 90000000000 35.57336926 23.80790114 90000000000 90000000000 90000000000 90000000000 90000000000 74.89144206 57.04067349 62.52145171 90000000000 65.33738971 90000000000 90000000000 89.35151696 47.2155273 64.40275311 0

Код программы на C++ для расчета всех необходимых данных1

#include <iostream>

#include <fstream>

#include <iomanip>

#include <vector>

#include <algorithm>

#include <ctime>

int DC, ACCURACY;

using namespace std;

using float_type = long double;

void original_Floyd_Warshall(vector<vector<float_type>> &matrixR1, vector<vector<int>> &matrixR2) {

int n = matrixR1.size(); // алгоритм работает правильно при условии matrixR1 = distance_matrix

float_type x, y;

for (int i = 0; i < n; ++i) {

iota(matrixR2[i].begin(), matrixR2[i].end(), 1);

}

for (int k = 0; k < n; ++k) {

for (int i = 0; i < n; ++i) {

for (int j = 0; j < n; ++j) {

if (i != j && i != k && j != k) {

x = matrixR1[i][k] + matrixR1[k][j];

y = matrixR1[i][j];

if (x < y) {

matrixR1[i][j] = x;

matrixR2[i][j] = matrixR2[i][k];

}

}

}

}

}

}

vector<vector<float_type>> load_intensity(const vector<vector<float_type>> &matrixY, vector<vector<int>> &matrixR2) {

int n = matrixY.size();

vector<vector<float_type>> matrixY_hatch(n, vector<float_type>(n, 0));

for (int i = 0; i < n; ++i) {

for (int j = 0; j < n; ++j) {

matrixY_hatch[i][matrixR2[i][j] - 1] += matrixY[i][j];

for (int k = matrixR2[i][j] - 1; k != j; k = matrixR2[k][j] - 1) {

matrixY_hatch[k][matrixR2[k][j] - 1] += matrixY[i][j];

}

}

}

return matrixY_hatch;

}

vector<vector<int>> gen_streams_matrix(vector<vector<float_type>> &matrixY_hatch, float_type quality) {

int n = matrixY_hatch.size();

float_type p_max = 1. - quality;

vector<vector<int>> matrixV(n, vector<int>(n, 0));

for (int i = 0; i < n; ++i) {

for (int j = 0; j < n; ++j) {

int v = 0;

if (matrixY_hatch[i][j] != 0) {

v = 1;

float_type p = 1, y = matrixY_hatch[i][j], numerator = y, sum = y;

while (p > p_max) {

++v;

numerator = ( numerator / v ) * y;

sum += numerator;

p = numerator / sum;

}

}

matrixV[i][j] = v;

}

}

return matrixV;

}

void calc_delays(const vector<vector<int>> &Rij,

const vector<vector<int>> &Aij,

const vector<vector<float_type>> &Bij,

vector<vector<float_type>> &Tij,

float_type L) {

int n = Tij.size();

for (int i = 0; i < n; ++i) {

for (int j = 0; j < n; ++j) {

if (Rij[i][j] == j + 1 && i != j) {

Tij[i][j] = ( 8. * L ) / ( Bij[i][j] - Aij[i][j] ); // задержка M/M/1

} else { Tij[i][j] = 0; }

}

}

for (int i = 0; i < n; ++i) {

for (int j = 0; j < n; ++j) {

if (Rij[i][j] != j + 1) {

Tij[i][j] += Tij[i][Rij[i][j] - 1];

for (int k = Rij[i][j] - 1; k != j; k = Rij[k][j] - 1) {

Tij[i][j] += Tij[k][Rij[k][j] - 1];

}

}

}

}

}

float_type calc_O(const vector<vector<float_type>> &Tij, float_type T0) {

double O = 0;

for (auto &str : Tij) {

for (auto &t : str) {

O += ( t - T0 / 2 ) * ( t - T0 / 2 );

}

}

return O;

}

vector<vector<float_type>> optimize_BMatrix_sub(const vector<vector<int>> &Rij,

const vector<vector<int>> &Aij,

vector<vector<float_type>> &Bij,

float_type T0,

float_type L) {

int bestI = 0, bestJ = 0, n = Bij.size();

float_type q = 1e+10, bestO = q;

vector<vector<float_type>> Tij(n, vector<float_type>(n, 0));

while (true) {

for (int i = 0; i < n; ++i) {

for (int j = 0; j < n; ++j) {

if (Bij[i][j] != 0) {

Bij[i][j] += DC;

calc_delays(Rij, Aij, Bij, Tij, L);

Bij[i][j] -= DC;

float_type O = calc_O(Tij, T0);

if (O < bestO) {

bestO = O, bestI = i, bestJ = j;

}

}

}

}

if (bestO < q) {

Bij[bestI][bestJ] += DC;

q = bestO;

} else { break; }

}

return Tij;

}

bool check_optimize(const vector<vector<float_type>> &Tij, float_type T0) {

return find_if(Tij.begin(), Tij.end(), [&T0](auto v) {

return any_of(v.begin(), v.end(), [&T0](auto x) {

return x > T0 / 2;

}) != 0;

}) == Tij.end();

}

pair<float_type, vector<vector<float_type>>> optimize_BMatrix(const vector<vector<int>> &Rij,

const vector<vector<int>> &Aij,

vector<vector<float_type>> &Bij,

float_type T0,

float_type L,

bool is_optimizeT0) {

auto old_T0 = T0, dT = T0 / ACCURACY;

auto Tij = optimize_BMatrix_sub(Rij, Aij, Bij, T0, L);

while (is_optimizeT0 && !check_optimize(Tij, old_T0) && ( T0 -= dT ) > 1e-20) {

Tij = optimize_BMatrix_sub(Rij, Aij, Bij, T0, L);

}

return make_pair(T0, Tij);

}

template <class T>

void print_vector(std::ostream &os, const vector<T> &matrix) {

for (auto &s : matrix)

os << s << ' ';

os << endl << endl;

}

template <class T>

void print_matrix(std::ostream &os, const vector<vector<T>> &matrix) {

for (auto &s : matrix) {

for (auto &t : s)

os << t << ' ';

os << endl;

}

os << endl;

}

int main() {

/* Считывание данных с файла */

auto start = clock();

int n, a0, optimizeT0;

float_type y0, L, quality, T0;

ifstream file_in("input_file.txt");

if (!file_in.is_open()) {

cout << "Error opening file 'input_file.txt'" << endl;

return 1;

}

stringstream s;

while (!file_in.eof()) {

string param;

getline(file_in, param);

s << param.substr(0, param.find(';')) << ' ';

}

file_in.close();

s >> n >> y0 >> a0 >> L >> quality >> T0 >> optimizeT0 >> DC >> ACCURACY;

vector<float_type> subscribers(n, 0);

for (auto &sub : subscribers)

s >> sub;

vector<vector<float_type>> distance_matrix(n, vector<float_type>(n, 0));

for (auto &str : distance_matrix)

for (auto &d : str)

s >> d;

/* Расчет и запись результатов в выходной файл */

cout << "Please, wait!.." << endl;

ofstream file_out("output_file.txt");

if (!file_out.is_open()) {

cout << "Error opening file 'output_file.txt'" << endl;

return 2;

}

file_out << setprecision(std::numeric_limits<float_type>::digits10);

/* 1. Интенсивности производимого в узлах сети трафика */

file_out << "1. Интенсивности производимого в узлах сети трафика" << endl;

vector<float_type> matrixY(subscribers);

transform(matrixY.begin(), matrixY.end(), matrixY.begin(), [&y0](auto n) { return n * y0; });

print_vector(file_out, matrixY);

/* 2. Коэффициенты распределения трафика по направлениям связи */

file_out << "2. Коэффициенты распределения трафика по направлениям связи" << endl;

auto sum = accumulate(matrixY.begin(), matrixY.end(), 0.L);

vector<float_type> matrixK(matrixY);

transform(matrixK.begin(), matrixK.end(), matrixK.begin(), [&sum](auto n) { return n / sum; });

print_vector(file_out, matrixK);

/* 3. Матрица интенсивностей трафика в направлениях связи */

file_out << "3. Матрица интенсивностей трафика в направлениях связи" << endl;

vector<vector<float_type>> matrixYij(n, vector<float_type>(n, 0));

for (int i = 0; i < n; ++i)

for (int j = 0; j < n; ++j)

matrixYij[i][j] = matrixY[i] * matrixK[j];

print_matrix(file_out, matrixYij);

/* 4. Матрицы кратчайших расстояний и кратчайших маршрутов между вершинами графа */

vector<vector<float_type>> matrixR1(distance_matrix);

vector<vector<int>> matrixR2(n, vector<int>(n, 0));

original_Floyd_Warshall(matrixR1, matrixR2);

file_out << "4.1. Матрица кратчайших расстояний между вершинами графа" << endl;

print_matrix(file_out, matrixR1);

file_out << "4.2. Матрица кратчайших маршрутов между вершинами графа" << endl;

print_matrix(file_out, matrixR2);

/* 5. Матрица интенсивностей нагрузок на линиях связи */

file_out << "5. Матрица интенсивностей нагрузок на линиях связи" << endl;

auto matrixY_hatch = load_intensity(matrixYij, matrixR2);

print_matrix(file_out, matrixY_hatch);

/* 6. Матрица потоков */

file_out << "6. Матрица потоков" << endl;

auto streams_matrix = gen_streams_matrix(matrixY_hatch, quality);

print_matrix(file_out, streams_matrix);

/* 7. Матрица интенсивности трафика ПД для линий связи */

file_out << "7. Матрица интенсивности трафика ПД для линий связи" << endl;

vector<vector<int>> Aij(streams_matrix);

for (auto &str : Aij) {

transform(str.begin(), str.end(), str.begin(), [&a0](auto v) { return v * a0; });

}

print_matrix(file_out, Aij);

/* 8. Матрица пропускных способностей */

file_out << "8. Матрица пропускных способностей (бит/с)" << endl;

vector<vector<float_type>> Bij(n, vector<float_type>(n, 0));

for (int i = 0; i < n; ++i) {

for (int j = 0; j < n; ++j) {

Bij[i][j] = ( Aij[i][j] ) ? ( Aij[i][j] + ( 8. * L ) / T0 ) : 0;

}

}

print_matrix(file_out, Bij);

cout << "1-8 Time: " << ( clock() - start ) / 1000.L << " s." << endl;

start = clock();

/* 9. и 10. Матрицы задержек и оптимизированных пропускных способностей */

auto pair = optimize_BMatrix(matrixR2, Aij, Bij, T0, L, static_cast<bool>(optimizeT0));

file_out << "9. Матрица задержек" << endl;

print_matrix(file_out, pair.second);

file_out << "10. Матрица оптимизированных пропускных способностей (бит/с)" << endl;

print_matrix(file_out, Bij);

file_out << "Оптимизированное T: " << pair.first << endl;

file_out.close();

cout << "Optimization Time: " << ( clock() - start ) / 1000.L << " s." << endl;

cout << "Done. Data was written to file 'output_file.txt'!" << endl;

}

1 260 строк с реализацией всех этапов курсовой работы: от первого до последнего пунктов — по входному файлу.