void GeodesicProvider::write(DataWriter &writer) const {
	writer.write(this->m_source_vertices);
	writer.write(this->m_target_vertices);
	writer.write(this->m_algorithm_option);
	writer.write(this->m_subdivision_level);
	writer.write(this->m_geodesics);
}
void SamplingProvider::write(DataWriter &writer) const {
    writer.write(this->m_sampling_radius);
    writer.write(this->m_curvatures);
    writer.write(this->m_barycentric_areas);
    writer.write(this->m_geodesics);
    writer.write(this->m_samples);
}
Exemplo n.º 3
0
void Observer::saveMap(DataWriter& writer, const std::map<const PlayerId*, CardSet>& map){
    writer.write(static_cast<unsigned int>(map.size()));
    for (std::map<const decore::PlayerId*, decore::CardSet>::const_iterator it = map.begin(); it != map.end(); ++it) {
        writer.write(mPlayers.index(it->first));
        writer.write(it->second.begin(), it->second.end());
    }
}
Exemplo n.º 4
0
void Observer::saveRoundData(DataWriter& writer, const RoundData& roundData){
    writer.write(static_cast<unsigned int>(roundData.mPlayers.size()));
    for(PlayerIds::const_iterator it = roundData.mPlayers.begin(); it != roundData.mPlayers.end(); ++it) {
        writer.write(mPlayers.index(*it));
    }
    saveMap(writer, roundData.mDroppedCards);
    saveMap(writer, roundData.mPickedUpCards);
}
Exemplo n.º 5
0
bool ClientSocket::connect(const char* host,const char* nick)
{
	hostent* serv = gethostbyname(host);
	if (!serv) {
		cout<<"gethostbyname failed\n";
		exit(16);
	}
	sockaddr_in addr;
	#ifdef WIN32
	int sockfd = socket(PF_INET, SOCK_STREAM, 0);
	#else
	int sockfd = socket(AF_INET, SOCK_STREAM, 0);
	#endif
		#ifdef WIN32
	if (sockfd==INVALID_SOCKET) {
		cout<<"binding socket failed "<<WSAGetLastError()<<endl;
		exit(15);
	}
		#endif
	memset(&addr,0,sizeof(addr));
	addr.sin_family = AF_INET;
#ifdef WIN32
	addr.sin_addr.s_addr = ((struct in_addr *)(serv->h_addr))->s_addr;
//    memcpy(serv->h_addr, &addr.sin_addr.s_addr, serv->h_length);
#else
	bcopy(serv->h_addr, &addr.sin_addr.s_addr, serv->h_length);
#endif
	addr.sin_port = htons(SERVER_PORT);
	if (::connect(sockfd, (sockaddr*)&addr, sizeof(addr))<0) {
		perror("Connecting failed");
		#ifdef WIN32
		cout<<"connect error: "<<WSAGetLastError()<<'\n';
		#endif
		return 0;
	}
	#ifndef WIN32
	fcntl(sockfd, F_SETFL, O_NONBLOCK);
	#else
    unsigned long x=1;
    ioctlsocket(sockfd, FIONBIO, &x);
    char flag=1;
    if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(flag))<0) {
    	perror("setsockopt TCP_NODELAY");
    	exit(16);
    }
	#endif
	conn.fd = sockfd;

    DataWriter w;
    w.writeByte(CLI_NAME);
    char buf[33]={};
    strncpy(buf,nick,31);
    w.write(buf,32);
    conn.write(w);
	return 1;
}
void PartawareProvider::write(DataWriter &writer) const {
	writer.write(this->m_cmc_rays_count);
	writer.write(this->m_cmc_rays_theta);
	writer.write(this->m_vsi_rays_count);
	writer.write(this->m_geodestic_weight);
	writer.write(this->m_angular_weight);
	writer.write(this->m_vsi_weight);
	writer.write(this->m_graph);
}
Exemplo n.º 7
0
void Observer::save(DataWriter& writer)
{
    writer.write(mGameCards.begin(), mGameCards.end());
    writer.write(mGameCardsCount);
    writer.write(mTrumpSuit);
    writer.write(static_cast<unsigned int>(mPlayers.size()));
    for (PlayerIds::const_iterator it = mPlayers.begin(); it != mPlayers.end(); ++it) {
        unsigned int playerIndex = mPlayers.index(*it);
        writer.write(playerIndex);
        writer.write(mPlayersCards.at(*it));
    }
    writer.write(static_cast<unsigned int>(mRoundsData.size()));
    for (std::vector<const RoundData*>::const_iterator it = mRoundsData.begin(); it != mRoundsData.end(); ++it) {
        saveRoundData(writer, **it);
    }
    writer.write(mCurrentRoundIndex);
    // save if mCurrentRoundData exist
    writer.write(static_cast<bool>(mCurrentRoundData));
    if (mCurrentRoundData) {
        saveRoundData(writer, *mCurrentRoundData);
    }
}
Exemplo n.º 8
0
void Engine::save(DataWriter& writer) const
{
    if (!mDeck || !mCurrentPlayer) {
        // save called too early - nothing to save actually because the game has not been even started
        return;
    }

    lock();

    // save players count
    writer.write(mGeneratedIds.size());
    // save each player cards
    for (std::vector<const PlayerId*>::const_iterator it = mGeneratedIds.begin(); it != mGeneratedIds.end(); ++it) {
        const CardSet& playerCards = mPlayersCards.at(*it);
        writer.write(playerCards.begin(), playerCards.end());
    }
    // save deck
    writer.write(mDeck->begin(), mDeck->end());
    // save trump suit
    writer.write(mDeck->trumpSuit());
    // save current player index
    writer.write(mGeneratedIds.index(mCurrentPlayer));
    // save current round index
    writer.write(mRoundIndex);

    // save bool if round is running
    writer.write(static_cast<bool>(mCurrentRoundIndex));
    // save current round related data
    if (mCurrentRoundIndex) {
        // save attackers
        writer.write(mAttackers.size());
        for (std::vector<const PlayerId*>::const_iterator it = mAttackers.begin(); it != mAttackers.end(); ++it) {
            writer.write(mGeneratedIds.index(*it));
        }
        assert(mDefender);
        writer.write(mGeneratedIds.index(mDefender));
        writer.write(mPassedCounter);
        assert(mCurrentRoundAttackerId);
        writer.write(mGeneratedIds.index(mCurrentRoundAttackerId));
        writer.write(mTableCards.attackCards().begin(), mTableCards.attackCards().end());
        writer.write(mTableCards.defendCards().begin(), mTableCards.defendCards().end());
        writer.write(mMaxAttackCards);
        writer.write(mDefendFailed);
    }

    // save observers data
    unsigned int observersCount = mGameObservers.size();
    writer.write(observersCount);
    for (std::vector<GameObserver*>::const_iterator it = mGameObservers.begin(); it != mGameObservers.end(); ++it) {
        unsigned int observerDataStart = writer.position();
        (*it)->save(writer);
        writer.write(static_cast<unsigned int>(writer.position() - observerDataStart));
    }
    unlock();
}
Exemplo n.º 9
0
PyObject* UserKnn_testrec( PyUserKnn* self, PyObject* args, PyObject* kwdict )
{
   const char* input_file = NULL;
   const char* output_file = NULL;
   char dlmchar = ',';
   int header = 0;
   int usercol = 0;
   int itemcol = 1;
   int ratingcol = -1;
   int topn = 10;

   static char* kwlist[] = { const_cast<char*>( "input_file" ),
                             const_cast<char*>( "output_file" ),
                             const_cast<char*>( "dlmchar" ),
                             const_cast<char*>( "header" ),
                             const_cast<char*>( "usercol" ),
                             const_cast<char*>( "itemcol" ),
                             const_cast<char*>( "ratingcol" ),
                             const_cast<char*>( "topn" ),
                             NULL };

   if( !PyArg_ParseTupleAndKeywords( args, kwdict, "s|sciiiii", kwlist, &input_file,
                                     &output_file, &dlmchar, &header, &usercol, &itemcol, &ratingcol, &topn ) )
   {
      return NULL;
   }

   if( NULL == input_file )
   {
      return NULL;
   }

   DataWriter dataWriter;
   if( NULL != output_file )
   {
      string strfilename = output_file;
      dataWriter.open( strfilename );
   }

   DataReader testReader( input_file, dlmchar, header );
   DataFrame testData( testReader, usercol, itemcol, ratingcol );

   PyObject* pyDict = PyDict_New();
   if( NULL == pyDict )
   {
      return NULL;
   }

   map<string, int> userFilter;
   DataFrame::iterator ind;
   DataFrame::iterator end = testData.end();
   for( ind = testData.begin() ; ind != end ; ++ind )
   {
      // Recommned item list once per user
      std::string userId = ind->first.first;
      if( userFilter.find( userId ) != userFilter.end() )
      {
         continue;
      }
      userFilter[userId] = 0;

      vector<string> ranking;
      if( !self->m_recAlgorithm->recommend( userId, topn, ranking ) )
      {
         continue;
      }

      PyObject* pyList = PyList_New( 0 );
      vector<string>::iterator rankind;
      vector<string>::iterator rankend = ranking.end();
      for( rankind = ranking.begin() ; rankind != rankend ; ++rankind )
      {
#if PY_MAJOR_VERSION >= 3
         if( -1 == PyList_Append( pyList, PyBytes_FromString( rankind->c_str() ) ) )
#else
         if( -1 == PyList_Append( pyList, PyString_FromString( rankind->c_str() ) ) )
#endif
         {
            return NULL;
         }
      }

      if( PyDict_SetItemString( pyDict, userId.c_str(), pyList ) < 0 )
      {
         Py_DECREF( pyList );
         return NULL;
      }

      if( itemcol >= 0 && ratingcol >= 0 )
      {
         // Calculate p@r, nDCG
         ;
      }

      if( dataWriter.isOpen() )
      {
         dataWriter.write( userId, ranking );
      }
   }

   PyObject* pyTupleResult = PyTuple_New( 1 );
   PyTuple_SET_ITEM( pyTupleResult, 0, pyDict );
   //PyTuple_SET_ITEM( pyTupleResult, 1, PyFloat_FromDouble( self->m_mae.eval() ) );
   //PyTuple_SET_ITEM( pyTupleResult, 2, PyFloat_FromDouble( self->m_rmse.eval() ) );

   return pyTupleResult;
}
Exemplo n.º 10
0
PyObject* UserKnn_test( PyUserKnn* self, PyObject* args, PyObject* kwdict )
{
   const char* input_file = NULL;
   const char* output_file = NULL;
   char dlmchar = ',';
   int header = 0;
   int usercol = 0;
   int itemcol = 1;
   int ratingcol = -1;

   static char* kwlist[] = { const_cast<char*>( "input_file" ),
                             const_cast<char*>( "output_file" ),
                             const_cast<char*>( "dlmchar" ),
                             const_cast<char*>( "header" ),
                             const_cast<char*>( "usercol" ),
                             const_cast<char*>( "itemcol" ),
                             const_cast<char*>( "ratingcol" ),
                             NULL };

   if( !PyArg_ParseTupleAndKeywords( args, kwdict, "s|sciiii", kwlist, &input_file,
                                     &output_file, &dlmchar, &header, &usercol, &itemcol, &ratingcol ) )
   {
      return NULL;
   }

   if( NULL == input_file )
   {
      return NULL;
   }

   DataWriter dataWriter;
   if( NULL != output_file )
   {
      char dlm = '\t';
      string strfilename = output_file;
      if( strfilename.substr( strfilename.find_last_of( "." ) + 1 ) == "csv" )
      {
         dlm = ',';
      }
      dataWriter.open( strfilename, dlm );
   }

   DataReader testReader( input_file, dlmchar, header );
   DataFrame testData( testReader, usercol, itemcol, ratingcol );

   PyObject* pyList = PyList_New( 0 );
   if( NULL == pyList )
   {
      return NULL;
   }

   DataFrame::iterator ind;
   DataFrame::iterator end = testData.end();
   for( ind = testData.begin() ; ind != end ; ++ind )
   {
      std::string userId = ind->first.first;
      std::string itemId = ind->first.second;
      double prediction = self->m_recAlgorithm->predict( userId, itemId );

      PyObject* pyTuple = PyTuple_New( 3 );
      if( NULL == pyTuple )
      {
         return NULL;
      }

#if PY_MAJOR_VERSION >= 3
      PyTuple_SET_ITEM( pyTuple, 0, PyBytes_FromString( userId.c_str() ) );
      PyTuple_SET_ITEM( pyTuple, 1, PyBytes_FromString( itemId.c_str() ) );
#else
      PyTuple_SET_ITEM( pyTuple, 0, PyString_FromString( userId.c_str() ) );
      PyTuple_SET_ITEM( pyTuple, 1, PyString_FromString( itemId.c_str() ) );
#endif
      PyTuple_SET_ITEM( pyTuple, 2, PyFloat_FromDouble( prediction ) );
      if( -1 == PyList_Append( pyList, pyTuple ) )
      {
         return NULL;
      }

      if( ratingcol >= 0 )
      {
         double rating = ind->second;
         self->m_mae.append( rating, prediction );
         self->m_rmse.append( rating, prediction );
      }

      if( dataWriter.isOpen() )
      {
         vector<string> vline;
         vline.push_back( userId );
         vline.push_back( itemId );
         std::ostringstream ss;
         ss << prediction;
         vline.push_back( ss.str() );
         dataWriter.write( vline );
      }
   }

   PyObject* pyTupleResult = PyTuple_New( 3 );
   PyTuple_SET_ITEM( pyTupleResult, 0, pyList );
   PyTuple_SET_ITEM( pyTupleResult, 1, PyFloat_FromDouble( self->m_mae.eval() ) );
   PyTuple_SET_ITEM( pyTupleResult, 2, PyFloat_FromDouble( self->m_rmse.eval() ) );

   return pyTupleResult;
}