void report_multiplication_error(const SparseKalmanMatrix *T,
                                 const SparseVector &Z,
                                 bool new_time,
                                 double fraction_in_initial_period,
                                 const VEC &v) {
    ostringstream err;
    int state_dim = T->nrow();
    err << "incompatible sizes in AccumulatorTransitionMatrix multiplication"
        << endl
        << "T.nrow() = " << state_dim << endl
        << "Z.size() = " << Z.size() << endl
        << "v.size() = " << v.size() << endl
        << "The first two should match.  The last should be two more "
        << "than the others" << endl;
    report_error(err.str());
}
Vec RQR_Multiply(const VECTOR &v,
                 const SparseKalmanMatrix &RQR,
                 const SparseVector &Z,
                 double H) {
    int state_dim = Z.size();
    if(v.size() != state_dim + 2) {
        report_error("wrong sizes in RQR_Multiply");
    }
    // Partition v = [eta, epsilon, 0]
    ConstVectorView eta(v, 0, state_dim);
    double epsilon = v[state_dim];

    // Partition this
    Vec RQRZ = RQR * Z.dense();
    double ZRQRZ_plus_H = Z.dot(RQRZ) + H;

    Vec ans(v.size());
    VectorView(ans, 0, state_dim) = (RQR * eta).axpy(RQRZ, epsilon);
    ans[state_dim] = RQRZ.dot(eta) + ZRQRZ_plus_H * epsilon;
    return ans;
}
예제 #3
0
{
	TEST_EQUAL(sv2.size(), 8)
}
END_SECTION

START_SECTION((void resize(size_type newsize)))
{
	sv2.resize(10);
	TEST_EQUAL(sv2.size(),10)
}
END_SECTION

START_SECTION((SparseVector& operator=(const SparseVector &source)))
{
	sv2=sv;
	TEST_EQUAL(sv2.size(), 8)
}
END_SECTION


START_SECTION((bool operator==(const SparseVector &rhs) const ))
{
	SparseVector<double> sv3(sv);
	TEST_EQUAL((sv3==sv), true)
}
END_SECTION

START_SECTION((bool operator<(const SparseVector &rhs) const ))
{
	SparseVector<double> sv3(sv);
	sv3[0]=-1.23;
  void SoftmaxPolicyPlayout::updateProbabilitiesBeforeAction(const Go::Board *board, Color player) {
    // check the last move
    size_t historySize = board->getHistoryCount();
    const Board::MoveChangeEntry *lastMove = board->getHistory(historySize-1);       // enemy's move
    const Board::MoveChangeEntry *secondLastMove = board->getHistory(historySize-2); // last my move
    const Board::MoveChangeEntry *thirdLastMove = board->getHistory(historySize-3);

    double *table = NULL;
    SparseVector *featureTable = NULL;
    if (player == BLACK) {
      table = m_probTableBlack;
      featureTable = m_featureTableBlack;
    } else {
      table = m_probTableWhite;
      featureTable = m_featureTableWhite;
    }

    if (lastMove && lastMove->m_putPos != PASS) table[lastMove->m_putPos] = 0;
    if (secondLastMove && secondLastMove->m_putPos != PASS) table[secondLastMove->m_putPos] = 0;

    PointSet updatedMoves;

    // reset static features which are set in the last time of player
    PointSet &previousSetMoves = player == BLACK ? m_toResetStaticFeaturesMovesBlack : m_toResetStaticFeaturesMovesWhite;
    for (size_t i=0; i<previousSetMoves.size(); i++) {
      //cerr << previousSetMoves[i] << ":" << featureTable[previousSetMoves[i]].toString() << endl;
      Point p = previousSetMoves[i];
      StandardFeatureExtractor::clearStaticFeatures(featureTable[p]);
      if (board->isColor(p, FREE) && 
          (board->getNeighborEmptyCount(p) >= 1 || board->checkLegalHand(p, player, Board::flipColor(player)) == Board::PUT_LEGAL)) {
        updatedMoves.insert(p,p);
      }
    }
    previousSetMoves.clear();

    // enumerate pattern update moves
    PointSet patternUpdateMoves;
    enumerateMovesOfPatternChanged(patternUpdateMoves, board, player, lastMove, secondLastMove, false);

    // enumerate moves that their status will change
    PointSet toBeLegal, toBeIllegal;
    PointSet legalMovesSet;
    toBeLegal.clear(); toBeIllegal.clear();
    enumerateToBeLegalAndIllegalMoves(board, table, player, toBeLegal, toBeIllegal, legalMovesSet, false);

    PointSet &staticFeatureUpdateMoves = previousSetMoves;

    if (lastMove == NULL) return; // no further old moves

    // update static features
    m_featureExtractor.updateStaticFeaturesForAllMovesWithoutClearOldFeatures(board, player, legalMovesSet, featureTable, staticFeatureUpdateMoves);

    // update patterns
    PointSet::ListConstIterator it, end = patternUpdateMoves.end();
    for (it = patternUpdateMoves.begin(); it!=end; it++) {
      SparseVector &targetFeatures = featureTable[*it];
      assert (board->isColor(*it, FREE));
      m_featureExtractor.updatePatternFeature(targetFeatures, board, *it, player);
      //if (board->checkLegalHand(*it, player, Board::flipColor(player)) == Board::PUT_LEGAL) {
      if ((!toBeIllegal.contains(*it) && table[*it] != 0) || toBeLegal.contains(*it)) {
        updatedMoves.insert(*it,*it);
      }
    }

    end = toBeLegal.end();
    for (it = toBeLegal.begin(); it!=end; it++) {
      updatedMoves.insert(*it,*it);
      // check capturing, atari features
    }

    end = staticFeatureUpdateMoves.end();
    for (it=staticFeatureUpdateMoves.begin(); it!=end; it++) {
      //table[*it] = m_expFeatureWeights.multiplyAll(featureTable[*it]);
      updatedMoves.insert(*it,*it);
    }

    end = updatedMoves.end();
    for (it=updatedMoves.begin(); it!=end; it++) {
      //table[*it] = m_expFeatureWeights.multiplyAll(featureTable[*it]);
      table[*it] = fmath::expd(m_featureWeights.dot(featureTable[*it]));
      //table[*it] = exp(m_featureWeights.dot(featureTable[*it]));
    }

    end = toBeIllegal.end();
    for (it = toBeIllegal.begin(); it!=end; it++) {
      table[*it] = 0;
      StandardFeatureExtractor::clearStaticFeatures(featureTable[*it]);
    }

#ifdef STRICT_CHECK
    for (int y=0; y<board->getSize(); y++) {
      for (int x=0; x<board->getSize(); x++) {
        Point p = board->xyToPoint(x,y);
        Board::PutType err = board->checkLegalHand(p, player, Board::flipColor(player));
        assert (!(err == Board::PUT_LEGAL && table[p] == 0) &&
                !(err != Board::PUT_LEGAL && table[p] != 0));
        if (err == Board::PUT_LEGAL) {
          double v = m_expFeatureWeights.multiplyAll(featureTable[p]);
          if (fabs(v-table[p]) >= 0.001) {
            cerr << "v = " << v << endl;
            cerr << "table[p] = " << table[p] << endl;
          }
          assert (fabs(v-table[p]) < 0.001);

          SparseVector vec;
          m_featureExtractor.extractFromStateAndAction(vec, board, p, player);
          // for (int i=0; i<vec.size(); i++) {
          //   if (vec[i] > 0 && vec[i] < StandardFeatureExtractor::STATIC_FEATURE_SIZE) {
          //     vec.erase(i);i--;
          //   }
          // }
          for (int i=0; i<featureTable[p].size(); i++) {
            if (featureTable[p][i] > 0 && featureTable[p][i] < 9) {
              featureTable[p].erase(i);i--;
            }
          }
          if (vec.size() != featureTable[p].size()) {
            // cerr << "updated moves:" << endl;
            // for (int i=0; i<staticFeatureUpdateMoves.size(); i++) {
            //   cerr << staticFeatureUpdateMoves[i] << ":" << featureTable[staticFeatureUpdateMoves[i]].toString() << endl;
            // }
            cerr << vec.toString() << endl;
            cerr << featureTable[p].toString() << endl;
            cerr << "point " << p << endl;
            cerr << "turn = " << player << endl;
            cerr << "last move = " << board->getLastMove() << endl;
            cerr << "second last move = " << secondLastMove->m_putPos << endl;
            board->printToErr();
            printProbabilityTableToErr(board);
          }
          assert (vec.size() == featureTable[p].size());
        }
      }
    }
#endif
  }