#include "simviewer.h"
SimViewer::SimViewer(QWidget *parent)
: QDialog(parent)
{
m_tabs = new QTabWidget;
m_averagesTab = new AveragesTab;
m_tabs->addTab(m_averagesTab, tr("&Averages"));
QPushButton *closeButton = new QPushButton(tr("&Close"));
closeButton->setDefault(true);
connect(closeButton, SIGNAL(clicked()), this, SLOT(accept()));
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch(1);
buttonLayout->addWidget(closeButton);
QVBoxLayout *topLayout = new QVBoxLayout;
topLayout->addWidget(m_tabs);
topLayout->addLayout(buttonLayout);
setLayout(topLayout);
setWindowTitle(tr("Simulation Results - Quackle"));
}
void SimViewer::setSimulator(const Quackle::Simulator &simulator)
{
m_averagesTab->setSimulator(simulator);
setWindowTitle(tr("%1 iterations of %2 - Quackle").arg(simulator.iterations()).arg(QuackleIO::Util::letterStringToQString(simulator.currentPosition().currentPlayer().rack().tiles())));
}
/////////////
AveragesTab::AveragesTab(QWidget *parent)
: QWidget(parent)
{
QVBoxLayout *topLayout = new QVBoxLayout(this);
m_textEdit = new QTextEdit;
m_textEdit->setReadOnly(true);
QPushButton *explainButton = new QPushButton(tr("&Explain me!"));
connect(explainButton, SIGNAL(clicked()), this, SLOT(explain()));
topLayout->addWidget(m_textEdit);
//topLayout->addWidget(explainButton);
}
void AveragesTab::setSimulator(const Quackle::Simulator &simulator)
{
QString html;
html += statisticTable(simulator);
html += "
";
for (const auto& it : simulator.simmedMoves())
{
if (!it.includeInSimulation())
continue;
QString levels;
for (const auto& levelIt : it.levels)
{
QString plays;
for (const auto& valueIt : levelIt.statistics)
{
//plays += QString("(%1) ").arg((*valueIt).score.averagedValue());
//plays += tr("(bingos %1) ").arg((*valueIt).bingos.averagedValue());
}
if (!plays.isEmpty())
levels += QString("%1").arg(plays);
}
html += QString("%1
%2
").arg(QuackleIO::Util::moveToDetailedString(it.move)).arg(levels);
html += "";
if (it.residual.hasValues())
html += tr("- Rack leftover value: %1
").arg(it.residual.averagedValue());
if (it.gameSpread.hasValues())
html += tr("- Spread: %1 (sd %2)
").arg(it.gameSpread.averagedValue()).arg(it.gameSpread.standardDeviation());
html += tr("- Valuation: %1
").arg(it.calculateEquity());
html += tr("- Bogowin %: %1%
").arg(it.calculateWinPercentage());
html += "
";
}
// TODO don't scroll to top when resetting
m_textEdit->setHtml(html);
}
QString AveragesTab::statisticTable(const Quackle::Simulator &simulator)
{
QString ret;
for (int levelIndex = 0; levelIndex < simulator.numLevels(); ++levelIndex)
{
for (int playerIndex = 0; playerIndex < simulator.numPlayersAtLevel(levelIndex); ++playerIndex)
{
if (levelIndex == 0 && playerIndex == 0)
continue;
const Quackle::SimmedMoveList::const_iterator end(simulator.simmedMoves().end());
// Little bit of fudgery so that the turn after our next turn is #2,
// and turn after oppo's next turn is also #2.
const int turnNumber = levelIndex + (playerIndex == 0? 0 : 1);
QString name = tr("%1 turn #%2").arg(playerIndex == 0? "Our" : "Oppo").arg(turnNumber);
if (playerIndex == 1 && levelIndex == 0)
{
name = tr("Oppo next turn");
}
else if (playerIndex == 0 && levelIndex == 1)
{
name = tr("Our next turn");
}
ret += QString("%1
").arg(name);
ret += "";
ret += tr("Candidate | Score | Std. Dev. | Bingo % |
");
for (Quackle::SimmedMoveList::const_iterator it = simulator.simmedMoves().begin(); it != end; ++it)
{
if (!(*it).includeInSimulation())
continue;
ret += "";
ret += tr("%1 | ").arg(QuackleIO::Util::moveToDetailedString((*it).move));
Quackle::AveragedValue value = (*it).getPositionStatistics(levelIndex, playerIndex).getStatistic(Quackle::PositionStatistics::StatisticScore);
Quackle::AveragedValue bingos = (*it).getPositionStatistics(levelIndex, playerIndex).getStatistic(Quackle::PositionStatistics::StatisticBingos);
ret += QString("%1 | %2 | ").arg(value.averagedValue()).arg(value.standardDeviation());
ret += QString("%1 | ").arg(bingos.averagedValue() * 100.0);
ret += "
";
}
ret += "
";
}
}
return ret;
}
void AveragesTab::explain()
{
QMessageBox::information(this, tr("Simulation Details Explanation - Quackle"), tr("A Quackle 2-ply simulation first puts our candidate play on the board, then has opponents make their best plays based on static evaluation, and has us make our best response based on static evaluation. So
- (28 [sd 0]) (39.1503 [sd 17.8229])
- (45.1284 [sd 21.0234])
means we're looking at a candidate play scoring 28. The average oppo response scored 39.2 with standard deviation 17.8. Our average riposte scored 45.1 with standard deviation 21.0.
- The residual value is the average leave value of our rack at the end of simulation minus the summed average leave value of oppo's racks.
- The spread is the average differential between our score and the leading player's score at the end of an iteration.
- The fake win percentage is how often we had a positive spread at the end of the iteration.
Note that odd-plied simulations are fun to try occasionally. For example, a 3-ply simulation has two of our plays (including the candidate) and two of each oppo's plays. Also try the Oppos pass option, which has oppos pass for all of their turns in the simulation.
"));
}
QSize SimViewer::sizeHint() const
{
return QSize(400, 400);
}