forked from Project-LemonLime/Project_LemonLime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatisticsbrowser.cpp
413 lines (344 loc) · 14 KB
/
statisticsbrowser.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/***************************************************************************
This file is part of Project LemonLime
Copyright (C) 2019 iotang
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***************************************************************************/
#include "statisticsbrowser.h"
#include "ui_statisticsbrowser.h"
#include "globaltype.h"
#include "contestant.h"
#include "settings.h"
#include "contest.h"
#include "task.h"
#include <QMap>
#include <QMessageBox>
#include <QCheckBox>
#include <QGridLayout>
#include <QHeaderView>
#include <QMenu>
#include <QApplication>
#include <QFileDialog>
StatisticsBrowser::StatisticsBrowser(QWidget *parent) :
QWidget(parent),
ui(new Ui::StatisticsBrowser)
{
ui->setupUi(this);
curContest = nullptr;
}
StatisticsBrowser::~StatisticsBrowser()
{
delete ui;
}
void StatisticsBrowser::setContest(Contest *contest)
{
curContest = contest;
}
auto StatisticsBrowser::getScoreNormalChart(const QMap<int, int> &scoreCount, int listSize, int totalScore) -> QString
{
QString buffer = "";
long long overallScoreSum = 0;
double scoreDiscrim = 0;
double scoreStandardDevia = 0;
int scoreTierPrefix = 0;
int lastScoreTier = -1;
int lastScoreTierNum = -1;
for (auto i = scoreCount.constEnd(); i != scoreCount.constBegin();)
{
i--;
int curScoreTier = i.key();
int curScoreTierNum = i.value();
if (curScoreTier < 0) continue;
overallScoreSum += 1LL * curScoreTier * curScoreTierNum;
if (lastScoreTier >= 0)scoreDiscrim += qLn(1 + 10.00 * (lastScoreTier - curScoreTier) / totalScore) * (1.00 - 1.00 * lastScoreTierNum * curScoreTierNum / listSize / listSize);
lastScoreTier = curScoreTier;
lastScoreTierNum = curScoreTierNum;
}
double scoreAverage = 1.00 * overallScoreSum / listSize;
buffer += "<table border=\"-1\">";
buffer += QString(R"(<tr><th>%1</th><th>%2</th><th>%3</th><th colspan="2">%4</th><th colspan="2">%5</th></tr>)").arg(tr("Score")).arg(tr("Count")).arg(tr("Ratio")).arg(tr("Prefix")).arg(tr("Suffix"));
for (auto i = scoreCount.constEnd(); i != scoreCount.constBegin();)
{
i--;
int curScoreTier = i.key();
int curScoreTierNum = i.value();
scoreStandardDevia += qPow(curScoreTier - scoreAverage, 2) * curScoreTierNum;
buffer += "<tr>";
buffer += QString("<td align=\"right\"><nobr>%1 Pt</nobr></td>").arg(curScoreTier < 0 ? QString("N/A") : QString::number(curScoreTier));
buffer += QString("<td align=\"right\"><nobr>%1</nobr></td>").arg(curScoreTierNum);
buffer += QString("<td align=\"right\"><nobr>%1%</nobr></td>").arg(QString::number(100.00 * curScoreTierNum / listSize, 'f', 3));
buffer += QString("<td align=\"right\"><nobr>%1</nobr></td>").arg(listSize - scoreTierPrefix);
buffer += QString("<td align=\"right\"><nobr>%1%</nobr></td>").arg(QString::number(100.00 - 100.00 * scoreTierPrefix / listSize, 'f', 3));
scoreTierPrefix += curScoreTierNum;
buffer += QString("<td align=\"right\"><nobr>%1</nobr></td>").arg(scoreTierPrefix);
buffer += QString("<td align=\"right\"><nobr>%1%</nobr></td>").arg(QString::number(100.00 * scoreTierPrefix / listSize, 'f', 3));
buffer += "</tr>";
}
buffer += "</table>";
scoreStandardDevia = qSqrt(scoreStandardDevia / listSize);
scoreDiscrim = scoreDiscrim * scoreDiscrim;
buffer += "<p>" + tr("Average") + " : " + QString::number(scoreAverage) + " / " + QString::number(totalScore) + "</p>";
buffer += "<p>" + tr("Standard Deviation") + " : " + QString::number(scoreStandardDevia) + "<p>";
buffer += "<p>" + tr("Score Discrimination Power") + " : " + QString::number(scoreDiscrim) + "<p>";
return buffer;
}
auto StatisticsBrowser::getTestcaseScoreChart(QList<TestCase *> testCaseList, QList<QList<QList<int>>> scoreList, QList<QList<QList<ResultState>>> resultList) -> QString
{
QString buffer = "";
buffer += "<table border=\"-1\">";
buffer += QString(R"(<tr><th>%1</th><th>%2</th><th>%3</th><th colspan="2">%4</th><th colspan="2">%5</th><th colspan="2">%6</th><th>%7</th></tr>)").arg(tr("No.")).arg(tr("Input")).arg(tr("Output")).arg(tr("Pure")).arg(tr("Far")).arg(tr("Lost")).arg(tr("Average"));
for (int i = 0; i < testCaseList.length(); i++)
{
QStringList inFileList = testCaseList[i]->getInputFiles();
QStringList outFileList = testCaseList[i]->getOutputFiles();
int mxScore = testCaseList[i]->getFullScore();
QList<int> miScoreRecord;
QList<int> miStatRecord;
for (int j = 0; j < scoreList.length(); j++)
{
miScoreRecord.append(mxScore);
miStatRecord.append(2);
}
for (int j = 0; j < inFileList.length(); j++)
{
int cntFail = 0;
int cntPati = 0;
int cntSucc = 0;
long long sumscore = 0;
for (int k = 0; k < scoreList.length(); k++)
{
int score = 0;
int statVal = 2;
ResultState stat = WrongAnswer;
if (scoreList[k].length() > i && scoreList[k][i].length() > j)
{
score = scoreList[k][i][j];
stat = resultList[k][i][j];
}
if (stat == CorrectAnswer)cntSucc++, statVal = 2;
else if (stat == PartlyCorrect)cntPati++, statVal = 1;
else cntFail++, statVal = 0;
sumscore += score;
miScoreRecord[k] = qMin(miScoreRecord[k], score);
miStatRecord[k] = qMin(miStatRecord[k], statVal);
}
buffer += "<tr>";
buffer += "<td align=\"left\">" + QString("%1.%2").arg(i + 1).arg(j + 1) + "</td>";
buffer += "<td align=\"left\">" + QString("%1").arg(inFileList[j]) + "</td>";
buffer += "<td align=\"left\">" + QString("%1").arg(outFileList[j]) + "</td>";
buffer += "<td align=\"right\"><nobr>" + QString("%1").arg(cntSucc) + "</nobr></td>";
buffer += "<td align=\"right\"><nobr>" + QString("%1%").arg(QString::number(100.00 * cntSucc / scoreList.length(), 'f', 3)) + "</nobr></td>";
buffer += "<td align=\"right\"><nobr>" + QString("%1").arg(cntPati) + "</nobr></td>";
buffer += "<td align=\"right\"><nobr>" + QString("%1%").arg(QString::number(100.00 * cntPati / scoreList.length(), 'f', 3)) + "</nobr></td>";
buffer += "<td align=\"right\"><nobr>" + QString("%1").arg(cntFail) + "</nobr></td>";
buffer += "<td align=\"right\"><nobr>" + QString("%1%").arg(QString::number(100.00 * cntFail / scoreList.length(), 'f', 3)) + "</nobr></td>";
buffer += "<td align=\"right\"><nobr>" + QString("%1 / %2").arg(QString::number(1.00 * sumscore / scoreList.length(), 'f', 3)).arg(mxScore) + "</nobr></td>";
buffer += "</tr>";
}
if (inFileList.length() > 1)
{
int sumCntFail = 0;
int sumCntPati = 0;
int sumCntSucc = 0;
long long sumSumScore = 0;
for (int j = 0; j < scoreList.length(); j++)
{
sumSumScore += miScoreRecord[j];
if (miStatRecord[j] >= 2)sumCntSucc++;
else if (miStatRecord[j] == 1)sumCntPati++;
else sumCntFail++;
}
buffer += "<tr>";
buffer += "<td align=\"left\">" + QString("%1 %2").arg(i + 1).arg("Overall") + "</td>";
buffer += "<td align=\"left\">" + QString("%1 %2").arg(inFileList.length()).arg(tr("Files")) + "</td>";
buffer += "<td align=\"left\">" + QString("%1 %2").arg(outFileList.length()).arg(tr("Files")) + "</td>";
buffer += "<td align=\"right\"><nobr>" + QString("%1").arg(sumCntSucc) + "</nobr></td>";
buffer += "<td align=\"right\"><nobr>" + QString("%1%").arg(QString::number(100.00 * sumCntSucc / scoreList.length(), 'f', 3)) + "</nobr></td>";
buffer += "<td align=\"right\"><nobr>" + QString("%1").arg(sumCntPati) + "</nobr></td>";
buffer += "<td align=\"right\"><nobr>" + QString("%1%").arg(QString::number(100.00 * sumCntPati / scoreList.length(), 'f', 3)) + "</nobr></td>";
buffer += "<td align=\"right\"><nobr>" + QString("%1").arg(sumCntPati) + "</nobr></td>";
buffer += "<td align=\"right\"><nobr>" + QString("%1%").arg(QString::number(100.00 * sumCntPati / scoreList.length(), 'f', 3)) + "</nobr></td>";
buffer += "<td align=\"right\"><nobr>" + QString("%1 / %2").arg(QString::number(1.00 * sumSumScore / scoreList.length(), 'f', 3)).arg(mxScore) + "</nobr></td>";
buffer += "</tr>";
}
}
buffer += "</table>";
return buffer;
}
auto StatisticsBrowser::checkValid(QList<Task *> taskList, const QList<Contestant *> &contestantList) -> int
{
for (auto i : taskList)
{
for (auto j : i->getTestCaseList())
{
if (j->getInputFiles().length() != j->getOutputFiles().length())return 0;
}
}
for (auto i : contestantList)
{
for (int j = 0; j < taskList.length(); j++)
{
QList<QList<int>> scoreList;
QList<QList<ResultState>> resultList;
QList<TestCase *> testCaseList;
int isJudged;
try
{
scoreList = i->getSocre(j);
resultList = i->getResult(j);
testCaseList = taskList[j]->getTestCaseList();
isJudged = i->getCheckJudged(j);
}
catch (...)
{
return 0;
}
if (!isJudged)return 0;
if (scoreList.length() != resultList.length())return 0;
if (scoreList.length() > 0 && resultList.length() > 0 && testCaseList.length() > 0)
{
if (scoreList.length() != testCaseList.length())return 0;
if (resultList.length() != testCaseList.length())return 0;
for (int k = 0; k < testCaseList.length(); k++)
{
if (scoreList[k].length() - (!testCaseList[k]->getDependenceSubtask().empty()) != testCaseList[k]->getInputFiles().length())return 0;
if (resultList[k].length() != testCaseList[k]->getInputFiles().length())return 0;
}
}
}
}
return 1;
}
void StatisticsBrowser::refresh()
{
QString buffer;
if (! curContest)
{
buffer = tr("No contest yet");
ui->textBrowser->setHtml(buffer);
return;
}
QList<Task *> taskList = curContest->getTaskList();
QList<Contestant *> contestantList = curContest->getContestantList();
if (taskList.empty())
{
buffer = tr("No task yet");
ui->textBrowser->setHtml(buffer);
return;
}
if (contestantList.empty())
{
buffer = tr("No contestant yet");
ui->textBrowser->setHtml(buffer);
return;
}
if (!checkValid(curContest->getTaskList(), curContest->getContestantList()))
{
buffer = tr("Some unhandled situation happened. May not all contestants are well judged, or not rejudged after changing testcases. Please refresh and rejudge.");
ui->textBrowser->setHtml(buffer);
return;
}
int totalScore = curContest->getTotalScore();
buffer += "<html><head>";
buffer += "<style type=\"text/css\">th, td {padding-left: 1em; padding-right: 1em;}</style>";
buffer += "</head><body>";
buffer += "<h1>" + QString("%1 %2").arg(tr("Contest")).arg(curContest->getContestTitle()) + "</h1>";
buffer += "<h2>" + tr("Overall") + "</h2>";
int haveError = 0;
QMap<int, int> scoreCount;
for (auto &i : contestantList)
{
int contestantTotalScore = 0;
int loss = 0;
for (int j = 0; j < taskList.size(); j++)
{
contestantTotalScore += i->getTaskScore(j);
if (i->getTaskScore(j) < 0) haveError = 1, loss = 1;
}
if (!loss) scoreCount[contestantTotalScore]++;
else scoreCount[-1]++;
}
if (haveError)
{
buffer += "<p style=\"font-size: large; color: red;\">" + tr("Warning: Judgement is not finished.") + "</p><br>";
}
buffer += getScoreNormalChart(scoreCount, contestantList.size(), totalScore);
buffer += "<br>";
buffer += "<br>";
buffer += "<h2>" + tr("Problems") + "</h2>";
for (int i = 0; i < taskList.size(); i++)
{
buffer += "<h3>";
buffer += QString("%1 %2: %3").arg(tr("Task")).arg(i + 1).arg(taskList[i]->getProblemTile());
buffer += "</h3>";
int numberSubmitted = 0;
QMap<int, int> cnts;
QList<QList<QList<int>>> TestcaseScoreList;
QList<QList<QList<ResultState>>> resultList;
for (auto &j : contestantList)
{
cnts[j->getTaskScore(i)]++;
if (j->getCompileState(i) != NoValidSourceFile)
numberSubmitted ++;
TestcaseScoreList.append(j->getSocre(i));
resultList.append(j->getResult(i));
}
buffer += getScoreNormalChart(cnts, contestantList.size(), taskList[i]->getTotalScore());
buffer += "<p>" + tr("Number of answer submitted") + " : " + QString::number(numberSubmitted) + " / " + QString::number(contestantList.size()) + " (" + QString::number(100.00 * numberSubmitted / contestantList.size()) + "%)</p>";
buffer += getTestcaseScoreChart(taskList[i]->getTestCaseList(), TestcaseScoreList, resultList);
buffer += "<br>";
buffer += "<br>";
}
buffer += "</body></html>";
ui->textBrowser->setHtml(buffer);
nowBrowserText = buffer;
}
void StatisticsBrowser::exportStatsticsHtml(QWidget *widget, const QString &fileName)
{
QFile file(fileName);
if (! file.open(QFile::WriteOnly))
{
QMessageBox::warning(widget, tr("LemonLime"), tr("Cannot open file %1").arg(QFileInfo(file).fileName()),
QMessageBox::Ok);
return;
}
QApplication::setOverrideCursor(Qt::WaitCursor);
QTextStream out(&file);
out.setCodec("UTF-8");
out << nowBrowserText;
QApplication::restoreOverrideCursor();
QMessageBox::information(widget, tr("LemonLime"), tr("Export is done"), QMessageBox::Ok);
}
void StatisticsBrowser::exportStatstics(QWidget *widget, Contest *curContest)
{
if (! curContest)
{
QMessageBox::warning(widget, tr("LemonLime"), tr("No contest yet"), QMessageBox::Ok);
return;
}
QList<Task *> taskList = curContest->getTaskList();
QList<Contestant *> contestantList = curContest->getContestantList();
if (taskList.empty())
{
QMessageBox::warning(widget, tr("LemonLime"), tr("No task yet"), QMessageBox::Ok);
return;
}
if (contestantList.empty())
{
QMessageBox::warning(widget, tr("LemonLime"), tr("No contestant yet"), QMessageBox::Ok);
return;
}
QString filter = tr("HTML Document (*.html)");
QString fileName = QFileDialog::getSaveFileName(widget, tr("Export Statstics"),
QDir::currentPath() + QDir::separator() + "statstics.html", filter);
if (fileName.isEmpty()) return;
if (QFileInfo(fileName).suffix() == "html") exportStatsticsHtml(widget, fileName);
}