Example #1
0
int CProducer::GenerateData()
{
    int msec = m_random();
    boost::this_thread::sleep(boost::posix_time::milliseconds(msec));
    int res = m_random();
    std::cout << boost::format("Producer generate data: %1%. In buffer: %2%") % res % m_buffer->GetActualSize() << std::endl;
    return res;
}
Example #2
0
void RunnerTilemap::generateUntil(int x)
{
	std::size_t i = m_chunks.size();
	while (i <= x)
	{
		chunk& last = m_chunks.back();
		bool ceiled = last.ceil >= 4;
		chunk  curr(last);

		if (m_seed == 0)
		{
			m_chunks.push_back(curr);
			i++;
			continue;
		}

		while (curr.height == last.height)
		{
			short slope = (2 + m_random() % 4) * (m_random() & 1 ? 1 : -1);
			if (m_ceils)
			{
				if (ceiled || slope < 0)
				{
					curr.ceil = 0;
					if (ceiled) slope = -1 * abs(slope);
				}
				else
				{
					if (m_random() % 4 == 0) curr.ceil = 4 + m_random() % 4;
					else curr.ceil = 0;
				}
			}
			else
			{
				curr.ceil = 0;
			}

			curr.height = last.height + slope;
			if (curr.height <           0)
			{
				curr.height = last.height;
				continue;
			}
			if (curr.height > m_maxHeight)
			{
				curr.height = last.height;
				continue;
			}
		}

		int wide  =  4 + m_random()%4;
		while(wide--)
		{
			m_chunks.push_back(curr);
			i++;
		}
	}
}
Example #3
0
/*===========================================================================*/
void KMeans::run()
{
    if ( m_input_table.empty() )
    {
        kvsMessageError("Input table data is not assigned.");
        return;
    }

    const size_t ncolumns = m_input_table.columnSize();
    const size_t nrows = m_input_table.column(0).size();
    for ( size_t i = 1; i < m_input_table.columnSize(); i++ )
    {
        if ( nrows != m_input_table.column(i).size() )
        {
            kvsMessageError("The number of rows is different between each column.");
            return;
        }
    }

    // Allocate memory for the cluster center.
    m_cluster_centers = new kvs::ValueArray<kvs::Real32> [ m_nclusters ];
    for ( size_t i = 0; i < m_nclusters; i++ ) { m_cluster_centers[i].allocate( ncolumns ); }

    // Assign initial cluster IDs to each row of the input table randomly.
    kvs::ValueArray<kvs::UInt32> IDs( nrows );
    for ( size_t i = 0; i < nrows; i++ ) IDs[i] = kvs::UInt32( m_nclusters * m_random() );

    // Calculate the center of cluster.
    switch ( m_seeding_method )
    {
    case RandomSeeding:
        ::InitializeCentersWithRandomSeeding( m_input_table, m_nclusters, IDs, m_cluster_centers );
        break;
    case SmartSeeding:
        ::InitializeCentersWithSmartSeeding( m_input_table, m_nclusters, IDs, m_cluster_centers );
        break;
    default:
        ::InitializeCentersWithRandomSeeding( m_input_table, m_nclusters, IDs, m_cluster_centers );
        break;
    }

    // Cluster center used for convergence test.
    kvs::ValueArray<kvs::Real32> center_new( ncolumns );

    // Clustering.
    bool converged = false;
    size_t counter = 0;
    while ( !converged )
    {
        // Calculate euclidean distance between the center of cluster and the point, and update the IDs.
        for ( size_t i = 0; i < nrows; i++ )
        {
            size_t id = 0;
            kvs::Real32 distance = kvs::Value<kvs::Real32>::Max();
            for ( size_t j = 0; j < m_nclusters; j++ )
            {
                const kvs::Real32 d = ::GetEuclideanDistance( m_input_table, i, m_cluster_centers[j] );
                if ( d < distance ) { distance = d; id = j; }
            }
            IDs[i] = id;
        }

        // Convergence test.
        converged = true;
        for ( size_t i = 0; i < m_nclusters; i++ )
        {
            ::CalculateCenter( m_input_table, i, IDs, &center_new );

            const kvs::Real32 distance = ::GetEuclideanDistance( m_input_table, m_cluster_centers[i], center_new );
            if ( !( distance < m_tolerance ) )
            {
                converged = false;
                break;
            }
        }

        if ( counter++ > m_max_iterations ) break;

        // Calculate the center of cluster.
        if ( !converged )
        {
            for ( size_t i = 0; i < m_nclusters; i++ )
            {
                ::CalculateCenter( m_input_table, i, IDs, &(m_cluster_centers[i]) );
            }
        }

    } // end of while

    m_cluster_ids = IDs;
}
Example #4
0
long long WordsGenerator::rand(long long int a, long long int b) {
    return (m_random()/(double)m_random.max())*(b-a+1)+a;
}