Example #1
0
// Create a randomly generated point
// scatter time execution
void testScalability(unsigned numpts)
{
    using namespace boost;
    using namespace std;

    typedef adjacency_matrix<undirectedS, no_property,
        property <edge_weight_t, double,
        property<edge_index_t, int> > > Graph;
    typedef graph_traits<Graph>::vertex_descriptor Vertex;
    typedef property_map<Graph, edge_weight_t>::type WeightMap;
    typedef set<simple_point<double>, cmpPnt<double> > PointSet;
    typedef vector< Vertex > Container;

    boost::mt19937 rng(time(0));
    uniform_real<> range(0.01, (numpts * 2));
    variate_generator<boost::mt19937&, uniform_real<> >
        pnt_gen(rng, range);

    PointSet points;
    simple_point<double> pnt;

    while (points.size() < numpts)
    {
        pnt.x = pnt_gen();
        pnt.y = pnt_gen();
        points.insert(pnt);
    }

    Graph g(numpts);
    WeightMap weight_map(get(edge_weight, g));
    vector<simple_point<double> > point_vec(points.begin(), points.end());

    connectAllEuclidean(g, point_vec, weight_map, get(vertex_index, g), numpts);

    Container c;
    timer t;
    double len = 0.0;

    // Run the TSP approx, creating the visitor on the fly.
    metric_tsp_approx(g, make_tsp_tour_len_visitor(g, back_inserter(c), len, weight_map));

    cout << "Number of points: " << num_vertices(g) << endl;
    cout << "Number of edges: " << num_edges(g) << endl;
    cout << "Length of tour: " << len << endl;
    cout << "Elapsed: " << t.elapsed() << endl;
}
Example #2
0
void
computeCovarianceMatrixFromPointSet( const PointSet &aPoints, Vector3f &aCenter, Eigen::Matrix3f &aCovarianceMatrix )
{
	float scale = 1.0f/aPoints.size();
	aCenter = scale * std::accumulate( aPoints.begin(), aPoints.end(), Vector3f() );

/*	//compute variances
	Vector3f variances;
	for ( size_t k = 0; k < aPoints.size(); ++k ) {
		variances += VectorCoordinateProduct( aPoints[k]-aCenter, aPoints[k]-aCenter );
	}
	variances *= scale;*/

	for ( size_t i = 0; i < 3; ++i ) {
		for ( size_t j = 0; j < 3; ++j ) {
			float covariance = 0.0f;
			for ( size_t k = 0; k < aPoints.size(); ++k ) {
				covariance += (aPoints[k][i]-aCenter[i]) * (aPoints[k][j]-aCenter[j]);
			}
			aCovarianceMatrix(i,j) = scale * covariance;
		}
	}
}
PointSet::PointSet(const PointSet &ps) : PointSet{ps.begin(), ps.end()} {}
  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
  }