File: moveGeneratorDialog.cc

package info (click to toggle)
gpsshogi 0.7.0-3.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 111,280 kB
  • sloc: cpp: 80,962; perl: 12,610; ruby: 3,929; javascript: 1,631; makefile: 1,202; sh: 473; tcl: 166; ansic: 67
file content (266 lines) | stat: -rw-r--r-- 7,286 bytes parent folder | download | duplicates (3)
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
#include "moveGeneratorDialog.h"
#include "osl/search/analyzer/categoryMoveVector.h"
#include "osl/search/moveGenerator.h"
#include "osl/search/searchState2.h"
#include "osl/effect_util/effectUtil.h"
#include "osl/eval/progressEval.h"
#include "osl/container/moveStack.h"
#include "osl/rating/featureSet.h"
#include "osl/rating/ratingEnv.h"
#include "gpsshogi/gui/util.h"

#include <QAbstractTableModel>
#include <QTableView>
#include <QVector>
#include <QSortFilterProxyModel>
#include <qlayout.h>
#include <qpushbutton.h>
#include <qspinbox.h>

struct MoveData
{
  MoveData() {}
  MoveData(const std::string &c,
	   osl::MoveLogProb m,
	   const std::string &a) : category(c.c_str()), move(m),
				   annotate(a.c_str()) {}
  QString category;
  osl::MoveLogProb move;
  QString annotate;
};

class MoveOrderModel : public QAbstractTableModel
{
public:
  MoveOrderModel(const osl::search::analyzer::CategoryMoveVector &moves,
		 const osl::NumEffectState &s,
		 const osl::Move m,
		 QObject *parent = 0);
  int rowCount(const QModelIndex &parent = QModelIndex()) const;
  int columnCount(const QModelIndex &parent = QModelIndex()) const;
  QVariant data(const QModelIndex &index, int role) const;
  QVariant headerData(int section, Qt::Orientation orientation,
		      int role = Qt::DisplayRole) const;
private:
  QStringList headers;
  QVector<MoveData> move_data;
  osl::NumEffectState state;
  osl::Move correct_move;
};

MoveOrderModel::MoveOrderModel(
  const osl::search::analyzer::CategoryMoveVector &moves,
  const osl::NumEffectState &s,
  const osl::Move m,
  QObject *parent) : QAbstractTableModel(parent), state(s),
		     correct_move(m)
{
  int size = 0;
  for (osl::search::analyzer::CategoryMoveVector::const_iterator p =
	 moves.begin();
       p != moves.end(); p++)
  {
    size += p->moves.size();
  }
  move_data.reserve(size);
  for (osl::search::analyzer::CategoryMoveVector::const_iterator p =
	 moves.begin();
       p != moves.end(); p++)
  {
    for (size_t i = 0; i < p->moves.size(); ++i)
    {
      std::string annotate = "";
      {
	static osl::rating::StandardFeatureSet fs;
	osl::RatingEnv env;
	env.make(state);
	annotate =
	  fs.annotate(state, env, p->moves[i].move());
      }
      move_data.push_back(MoveData(p->category, p->moves[i], annotate));
    }
  }
  headers << "Category" << "Move" << "Prob.";
  headers << "Annotate";
}

int MoveOrderModel::rowCount(const QModelIndex &) const
{
  return move_data.size();
}

int MoveOrderModel::columnCount(const QModelIndex &) const
{
  return headers.size();
}

QVariant MoveOrderModel::data(const QModelIndex &index, int role) const
{
  if (!index.isValid() || index.row() > rowCount() ||
      index.column() >  columnCount())
    return QVariant();

  if (role == Qt::DisplayRole)
  {
    switch (index.column())
    {
    case 0:
      return move_data[index.row()].category;
    case 1:
      return gpsshogi::gui::Util::moveToString(
	move_data[index.row()].move.move());
    case 2:
      return QString("%1").arg(move_data[index.row()].move.logProb());
    case 3:
    {
      return move_data[index.row()].annotate;
    }
    default:
      return QVariant();
    }
  }
  else if (role == Qt::ForegroundRole && index.column() == 1 &&
	   move_data[index.row()].move.move() == correct_move)
  {
    return QBrush(QColor("blue"));
  }
  else if (role == Qt::UserRole)
  {
    switch (index.column())
    {
    case 0:
      return move_data[index.row()].category;
    case 1:
      return move_data[index.row()].move.move().intValue();
    case 2:
      return static_cast<uint>(move_data[index.row()].move.logProb());
    case 3:
      return move_data[index.row()].annotate;
    default:
      return QVariant();
    }
  }
  else
  {
    return QVariant();
  }
}

QVariant MoveOrderModel::headerData(int section, Qt::Orientation orientation,
				    int role) const
{
  if (role != Qt::DisplayRole || section > headers.size())
    return QVariant();

  if (orientation == Qt::Horizontal)
    return headers[section];
  else
    return QString("%1").arg(section);
}

class SortFilterProxyModel : public QSortFilterProxyModel
{
public:
  SortFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent) { }
  bool lessThan(const QModelIndex &left, const QModelIndex &right) const
  {
    QVariant left_data = sourceModel()->data(left, sortRole());
    QVariant right_data = sourceModel()->data(right, sortRole());
    if (left_data.type() == QVariant::Int)
    {
      osl::Move m1 = osl::Move::makeDirect(left_data.toInt());
      osl::Move m2 = osl::Move::makeDirect(right_data.toInt());
      return gpsshogi::gui::Util::compare(m1, m2) < 0;
    }
    return QSortFilterProxyModel::lessThan(left, right);
  }
};

MoveGeneratorDialog::MoveGeneratorDialog(const osl::SimpleState &state,
					 const std::vector<osl::Move> &moves,
					 int limit, osl::Move next,
					 QWidget *parent)
  : QDialog(parent), limit(limit), state(state), moves(moves),
    next_move(next)
{
  osl::search::analyzer::CategoryMoveVector category_moves;
  QAbstractItemModel *model =
    new MoveOrderModel(category_moves, osl::NumEffectState(state),
		       osl::Move());
  view = new QTableView(this);
  view->setSortingEnabled(true);
  view->setModel(model);

  QVBoxLayout *layout = new QVBoxLayout(this);
  spinBox = new QSpinBox(this);
  spinBox->setRange(0, 2000);
  spinBox->setSingleStep(100);
  layout->addWidget(spinBox);
  layout->addWidget(view);
  QPushButton *button = new QPushButton(this);
  button->setText("&OK");
  layout->addWidget(button);
  connect(spinBox, SIGNAL(valueChanged(int)), SLOT(setLimit(int)));
  connect(button, SIGNAL(clicked()), this, SLOT(accept()));

  updateMoves();

  resize(layout->sizeHint());
}

void MoveGeneratorDialog::updateMoves()
{
  spinBox->setValue(limit);
  osl::MoveStack history;
  osl::NumEffectState new_state(state);
  osl::eval::ProgressEval eval(new_state);
  osl::search::SimpleHashRecord record;

  for (size_t i = 0; i < moves.size(); i++)
  {
    new_state.makeMove(moves[i]);
    history.push(moves[i]);
  }
  osl::search::analyzer::CategoryMoveVector category_moves;
  {
    osl::search::MoveGenerator generator;
    generator.initOnce();
    
    record.setInCheck(new_state.inCheck());
    osl::eval::ProgressEval eval(new_state);
    generator.init(limit, &record, eval, new_state, true, osl::Move(), limit == 0);
    osl::search::SearchState2::checkmate_t checkmate;
    osl::search::SearchState2 sstate(new_state, checkmate);
    sstate.setHistory(history);
    generator.generateAll(new_state.turn(), sstate, category_moves);
  }
  
  QAbstractItemModel *model =
    new MoveOrderModel(category_moves, new_state, next_move);
  SortFilterProxyModel *proxy_model = new SortFilterProxyModel(this);
  proxy_model->setSourceModel(model);
  proxy_model->setSortRole(Qt::UserRole);
  view->setModel(proxy_model);
  view->resizeColumnsToContents();
}

void MoveGeneratorDialog::setLimit(int new_limit)
{
  limit = new_limit;
  updateMoves();
}

void MoveGeneratorDialog::setStatus(const osl::SimpleState &s,
				    const std::vector<osl::Move> &m,
				    int l, osl::Move next)
{
  state = s;
  moves = m;
  limit = l;
  next_move = next;
  updateMoves();
}
// ;;; Local Variables:
// ;;; mode:c++
// ;;; c-basic-offset:2
// ;;; End: