Upcoming SlideShare
×

# Statistics.cpp

460 views

Published on

dpsm computer diffusion model

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
460
On SlideShare
0
From Embeds
0
Number of Embeds
315
Actions
Shares
0
1
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Statistics.cpp

1. 1. statistics.cpp #include <iostream> #include <fstream> #include <cmath> #include <vector> #include <algorithm> #include <string> #include "statistics.hpp" #include "tools.hpp" using std::endl; Histogram::Histogram(std::vector<double> &v, double t_bin_size) { bin_size = t_bin_size; //Groesstes und kleinstes Element finden min = *min_element(v.begin(), v.end()); double max = *max_element(v.begin(), v.end()); int num_bins = int((max - min) / bin_size + 1.0); data.resize(num_bins, 0); int position; for (unsigned int i = 0; i < v.size(); i++) { position = int((v[i] - min) / bin_size); data[position] += 1; } } Histogram::~Histogram() {} /*-----------------------------------------------*/ void Statistics::Init(int* t_grid_size, int* t_bin_size, int* t_num_iterations, int* t_num_customers, int* t_num_salesman) { grid_size = t_grid_size; log_field.resize((*grid_size) * (*grid_size), 0); bin_size = t_bin_size; num_iterations = t_num_iterations; num_customers = t_num_customers; num_salesman = t_num_salesman; log_p.clear(); log_p.resize(*num_salesman); log_earn.clear(); log_earn.resize(*num_salesman); last_earn.clear(); last_earn.resize(*num_salesman, 0); log_sales.clear(); log_sales.resize(*num_salesman); last_sales.clear(); last_sales.resize(*num_salesman, 0); log_visits.clear(); log_visits.resize(*num_salesman); Seite 1
2. 2. statistics.cpp last_visits.clear(); last_visits.resize(*num_salesman, 0); path = "./output/"; prefix = "out"; } void Statistics::LogField(int x, int y) { log_field[x + y * (*grid_size)]++; } void Statistics::LogSalesman(int salesm_id, double earnings, double p, int sales, int visits) { log_p[salesm_id].push_back(p); log_earn[salesm_id].push_back(earnings - last_earn[salesm_id]); log_sales[salesm_id].push_back(sales - last_sales[salesm_id]); log_visits[salesm_id].push_back(visits - last_visits[salesm_id]); last_earn[salesm_id] = earnings; last_sales[salesm_id] = sales; last_visits[salesm_id] = visits; } void Statistics::WriteLogSalesman(std::string name) { if (name == "") name = prefix; Write2dVector(log_earn, path + name + "_earn.dat"); Write2dVector(log_p, path + name + "_p.dat"); Write2dVector(log_sales, path + name + "_sales.dat"); Write2dVector(log_visits, path + name + "_visits.dat"); } void Statistics::Write2dVector(std::vector<std::vector<double> > &v, std::string filename) { std::ofstream write(filename.c_str()); for (unsigned int i = 0; i < v[0].size(); i++) { write << (i + 1) * (*bin_size) - 1; for (unsigned int j = 0; j < v.size(); j++) { write << "t" << v[j][i]; } write << endl; } } void Statistics::DivideData(std::vector<double> &v, double divisor) { for (unsigned int i = 0; i < v.size(); i++) { v[i] = v[i] / divisor; } } Seite 2
3. 3. statistics.cpp void Statistics::Smoothing(std::vector<double> &v) { int N = v.size(); std::vector<double> tmp(N, 0); for (int i = 0; i < N; i++) { tmp[i] += 0.4 * v[i]; if (i > 0) tmp[i] += 0.3 * v[i-1]; else tmp[i] += 0.3 * v[i]; if (i < N-1) tmp[i] += 0.3 * v[i+1]; else tmp[i] += 0.3 * v[i]; } v = tmp; } void Statistics::MSmoothing(std::vector<double> &v, int n) { for (int i = 0; i < n; i++) { Smoothing(v); } } double Statistics::Midpoint(std::vector<double> &v) { double sum = 0; unsigned int size = v.size(); for (unsigned int i = 0; i < size; i++) { sum = sum + v[i]; } return sum/size; } double Statistics::Variance(std::vector<double> &v) { double midpoint = Midpoint(v); double sum = 0; unsigned int size = v.size(); for (unsigned int i = 0; i < size; i++) { sum = sum + (v[i] - midpoint) * (v[i] - midpoint); } return sum/(size - 1); } void Statistics::WriteHistogram(std::vector<double> &v, double bin_size, std::string filename) { filename = path + prefix + "_" + filename; std::ofstream write(filename.c_str()); Histogram histo(v, bin_size); Seite 3
4. 4. statistics.cpp for (unsigned int i = 0; i < histo.data.size(); i++) { write << (i*bin_size + histo.min) << "t" << histo.data[i] << endl; } } void Statistics::WriteFields(std::string filename) { filename = path + prefix + "_" + filename; std::ofstream write(filename.c_str()); int fx, fy; for (unsigned int i = 0; i < log_field.size(); i++) { fx = i % (*grid_size); fy = (i - fx) / (*grid_size); write << fx << "t" << fy << "t" << log_field[i] << endl; } } void Statistics::WriteFieldsDistance(std::string filename, int x, int y) { filename = path + prefix + "_" + filename; std::ofstream write(filename.c_str()); int fx, fy; for (unsigned int i = 0; i < log_field.size(); i++) { fx = i % (*grid_size); fy = (i - fx) / (*grid_size); write << Distance(x, y, fx, fy, *grid_size) << "t" << log_field[i] << endl; } } Seite 4