added second part of evaluation in cpp

This commit is contained in:
2018-12-09 20:55:21 +01:00
parent 6c01b76b9c
commit 0a6b6b7d72
168 changed files with 5717 additions and 16 deletions

View File

@@ -1,29 +1,39 @@
#include "result.h"
#include "gnuplot-iostream.h"
#include "ui_result.h"
#include <QTableWidgetItem>
#include <algorithm>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
#include <cmath>
#include <iomanip> // setprecision
#include <iostream>
#include <map>
#include <math.h>
#include <qcolor.h>
#include <string>
#include <vector>
#define SHELL 1
#define DEA 2
using std::cout;
using std::endl;
using std::string;
using std::vector;
Result::Result(DataBase * db, QWidget * parent)
Result::Result(DataBase * db, int rennid, QWidget * parent)
: QWidget(parent), ui(new Ui::Result) {
ui->setupUi(this);
this->db = db;
// prepare dummy data
vector<std::string> header;
vector<string> header;
// dummy for now
this->rennid = 39;
// this->rennid = 39;
this->rennid = rennid;
this->drawPlots(rennid);
// test data
header.push_back("Spalte 1");
@@ -137,9 +147,9 @@ Result::Result(DataBase * db, QWidget * parent)
for (int fahrer : this->getFahrerIds(this->rennid)) {
for (int car : this->getAutoIds(this->rennid)) {
int shellmin = this->getMinimum(fahrer, 1, this->rennid, car,
int shellmin = this->getMinimum(fahrer, SHELL, this->rennid, car,
this->minimumTime);
int deamin = this->getMinimum(fahrer, 2, this->rennid, car,
int deamin = this->getMinimum(fahrer, DEA, this->rennid, car,
this->minimumTime);
subZeiten.push_back(static_cast<float>(shellmin) / 1000);
@@ -287,6 +297,86 @@ Result::Result(DataBase * db, QWidget * parent)
this->ui->tWResult->horizontalHeader()->setStretchLastSection(true);
}
void Result::drawPlots(int rennid) {
// get drivers
vector<int> driverIds = this->getFahrerIds(rennid);
vector<string> drivers;
for_each(driverIds.begin(), driverIds.end(), [&drivers, this](int id) {
drivers.push_back(this->getFahrerName(id));
});
vector<int> carIds = this->getAutoIds(rennid);
// get minimal time from race
std::stringstream statement;
statement << "select rennen.minimumRoundtime from Rennen where id like "
<< boost::lexical_cast<string>(rennid);
int minTimeRace = 2950;
try {
minTimeRace =
this->db->getData2(statement.str(), 1).at(0).at(0).toInt();
} catch (std::exception & e) {
Q_UNUSED(e);
cout << "minimal race time not readable from database" << endl;
cout << "min time set to: " << minTimeRace;
}
vector<struct Times> times;
struct Times tempTimes;
int sumShell = 0;
int sumDea = 0;
for (int driverid : driverIds) {
sumShell = 0;
sumDea = 0;
for (int car : carIds) {
// shell
sumShell +=
this->getMinimum(driverid, SHELL, rennid, car, minTimeRace);
// dea
sumDea += this->getMinimum(driverid, DEA, rennid, car, minTimeRace);
}
tempTimes.shell = sumShell / static_cast<int>(carIds.size());
tempTimes.dea = sumDea / static_cast<int>(carIds.size());
times.push_back(tempTimes);
}
this->plotDrivers(drivers, times);
// clean up
times.clear();
// calc cars
for (int carId : carIds) {
sumShell = 0;
sumDea = 0;
for (int driverId : driverIds) {
// shell
sumShell +=
this->getMinimum(driverId, SHELL, rennid, carId, minTimeRace);
// dea
sumDea +=
this->getMinimum(driverId, DEA, rennid, carId, minTimeRace);
}
tempTimes.shell = sumShell / static_cast<int>(driverIds.size());
tempTimes.dea = sumDea / static_cast<int>(driverIds.size());
times.push_back(tempTimes);
}
// calc car names
vector<string> carNames;
for_each(carIds.begin(), carIds.end(), [&carNames, this](int id) {
carNames.push_back(this->getAutoName(id));
});
plotCars(carNames, times);
}
Result::~Result() {
cout << "result closed" << endl;
}
@@ -308,7 +398,7 @@ template <typename T> T Result::myMin(vector<T> vec) {
}
template <typename T> double Result::meanVal(vector<T> vec) {
T sum = std::accumulate(vec.begin(), vec.end(), 0.0);
T sum = std::accumulate(vec.begin(), vec.end(), static_cast<float>(0.0));
double mean = static_cast<double>(sum) / vec.size();
return mean;
}
@@ -365,6 +455,7 @@ vector<int> Result::getAutoIds(int rennid) {
return vecToRet;
}
// returns valid minimal time in milliseconds
int Result::getMinimum(int fahrerId, int bahnId, int rennId, int autoid,
int minTime) {
std::stringstream statement;
@@ -375,6 +466,7 @@ int Result::getMinimum(int fahrerId, int bahnId, int rennId, int autoid,
<< boost::lexical_cast<string>(bahnId);
try {
cout << statement.str() << endl;
vector<vector<QString>> res = this->db->getData2(statement.str(), 1);
// filter impossible values
vector<int> validTimes;
@@ -390,6 +482,7 @@ int Result::getMinimum(int fahrerId, int bahnId, int rennId, int autoid,
return *result;
}
else {
cout << "no valid time found" << endl;
return 9999;
}
@@ -419,6 +512,8 @@ float Result::getMean(int fahrerid, int bahnid, int rennid) {
}
return 9999;
}
// returns name of driver with id
string Result::getFahrerName(int id) {
std::stringstream statement;
statement << "select name from fahrer where id like "
@@ -469,3 +564,138 @@ string Result::getAutoName(int id) {
return "name";
}
double Result::getMinRangeTime(vector<struct Times> times) {
double min = 9999;
for (struct Times time : times) {
if (time.shell < min) {
min = time.shell;
}
if (time.dea < min) {
min = time.dea;
}
}
min -= 100;
cout << min << endl;
return min / 1000;
}
double Result::getMaxRangeTime(vector<struct Times> times) {
double max = 0;
for (struct Times time : times) {
if (time.shell > max) {
max = time.shell;
}
if (time.dea > max) {
max = time.dea;
}
}
max += 200;
return max / 1000;
}
void Result::plotCars(vector<string> carNames, vector<struct Times> times) {
// structure of times:
Gnuplot gp;
std::stringstream xlabels;
xlabels << "set xtics(";
for (size_t i = 0; i < carNames.size(); i++) {
xlabels << "'" << carNames[i] << "' "
<< boost::lexical_cast<string>(i + 0.15);
if (i + 1 != carNames.size()) {
xlabels << ", ";
}
else {
xlabels << ")\n";
}
}
cout << xlabels.str();
gp << xlabels.str();
std::vector<std::pair<double, double>> xy_pts;
for (size_t i = 0; i < carNames.size(); i++) {
xy_pts.push_back(std::make_pair(i, (times.at(i).shell) / 1000.0));
// xy_pts.push_back(std::make_pair(i + 0.3, times.at(i).dea));
}
double minRange = getMinRangeTime(times);
double maxRange = getMaxRangeTime(times);
string strMinRange = boost::str(boost::format("%.2f") % minRange);
string strMaxRange = boost::str(boost::format("%.2f") % maxRange);
gp << "set yrange [" << strMinRange << ":" << strMaxRange << "]\n";
gp << "set boxwidth 0.3\nset style fill solid\n";
gp << "set title 'Vergleich der Autos'\n";
gp << "set ylabel 'Zeit in Sekunden'\n";
gp << "set xlabel 'Autos'\n";
gp << "show grid\nset grid ytics lc rgb '#bbbbbb' lw 1 lt 0\n";
gp << "plot" << gp.file1d(xy_pts)
<< "with boxes title 'Shell' lt rgb '#FF0000', '' u 1:($2 + "
"0.05):($2) with labels notitle"
<< std::endl;
xy_pts.clear();
for (size_t i = 0; i < carNames.size(); i++) {
// xy_pts.push_back(std::make_pair(i, times.at(i).shell));
xy_pts.push_back(std::make_pair(i + 0.3, (times.at(i).dea) / 1000.0));
}
gp << "replot" << gp.file1d(xy_pts)
<< "with boxes title 'Dea' lt rgb '#00FF00', ''u 1:($2 + 0.05):($2) "
"with labels notitle"
<< std::endl;
}
void Result::plotDrivers(vector<string> driverNames,
vector<struct Times> times) {
// structure of times:
Gnuplot gp;
std::stringstream xlabels;
xlabels << "set xtics(";
for (size_t i = 0; i < driverNames.size(); i++) {
xlabels << "'" << driverNames[i] << "' "
<< boost::lexical_cast<string>(i + 0.15);
if (i + 1 != driverNames.size()) {
xlabels << ", ";
}
else {
xlabels << ")\n";
}
}
gp << xlabels.str();
std::vector<std::pair<double, double>> xy_pts;
for (size_t i = 0; i < driverNames.size(); i++) {
xy_pts.push_back(std::make_pair(i, (times.at(i).shell) / 1000.0));
// xy_pts.push_back(std::make_pair(i + 0.3, times.at(i).dea));
}
double minRange = getMinRangeTime(times);
double maxRange = getMaxRangeTime(times);
string strMinRange = boost::str(boost::format("%.2f") % minRange);
string strMaxRange = boost::str(boost::format("%.2f") % maxRange);
gp << "set yrange [" << strMinRange << ":" << strMaxRange << "]\n";
gp << "set boxwidth 0.3\nset style fill solid\n";
gp << "set title 'Vergleich der Fahrer'\n";
gp << "set ylabel 'Zeit in Sekunden'\n";
gp << "set xlabel 'Fahrer'\n";
gp << "show grid\nset grid ytics lc rgb '#bbbbbb' lw 1 lt 0\n";
gp << "plot" << gp.file1d(xy_pts)
<< "with boxes title 'Shell' lt rgb '#FF0000', '' u 1:($2 + "
"0.05):($2) with labels notitle"
<< std::endl;
xy_pts.clear();
for (size_t i = 0; i < driverNames.size(); i++) {
// xy_pts.push_back(std::make_pair(i, times.at(i).shell));
xy_pts.push_back(std::make_pair(i + 0.3, (times.at(i).dea) / 1000.0));
}
gp << "replot" << gp.file1d(xy_pts)
<< "with boxes title 'Dea' lt rgb '#00FF00', ''u 1:($2 + 0.05):($2) "
"with labels notitle"
<< std::endl;
}