Beispiel #1
0
RcppExport SEXP geneticLHS_cpp(SEXP /*int*/ n, SEXP /*int*/ k, SEXP /*int*/ pop,
        SEXP /*int*/ gen, SEXP /*double*/ pMut, SEXP criterium,
        SEXP /*bool*/ bVerbose)
{
    try
    {
        Rcpp::RNGScope tempRNG;

        int m_n = Rcpp::as<int>(n);
        int m_k = Rcpp::as<int>(k);
        int m_pop = Rcpp::as<int>(pop);
        int m_gen = Rcpp::as<int>(gen);
        double m_pMut = Rcpp::as<double>(pMut);
        std::string m_criterium = Rcpp::as<std::string>(criterium);
        bool m_bVerbose = Rcpp::as<bool>(bVerbose);

        lhs_r::checkArguments(m_n, m_k);
        if (m_n == 1)
        {
            if (m_bVerbose)
            {
                Rprintf("Design is already optimal");
            }
            return lhs_r::degenerateCase(m_k);
        }
        
        lhs_r::RStandardUniform oRStandardUniform = lhs_r::RStandardUniform();
        bclib::matrix<double> mat = bclib::matrix<double>(m_n, m_k);
        lhslib::geneticLHS(m_n, m_k, m_pop, m_gen, m_pMut, m_criterium, m_bVerbose, mat, oRStandardUniform);

        Rcpp::NumericMatrix rresult(m_n, m_k);
        for (int irow = 0; irow < m_n; irow++)
        {
            for (int jcol = 0; jcol < m_k; jcol++)
            {
                rresult(irow, jcol) = mat(irow, jcol);
            }
        }

        return rresult;
    }
    catch (Rcpp::not_compatible & nc)
    {
        std::stringstream message;
        message << nc.what() << "\n" << "Error is likely caused by an input vector where a single value is required.";
        ::Rf_error(message.str().c_str());
    }
    catch (std::exception & e)
    {
        ::Rf_error(e.what());
    }
    catch (...)
    {
        ::Rf_error("Unrecognized exception");
    }
    return ::R_NilValue;
}
Beispiel #2
0
RcppExport SEXP randomLHS_cpp(SEXP n, SEXP k, SEXP preserveDraw)
{
    if (TYPEOF(n) != INTSXP || TYPEOF(k) != INTSXP ||
            TYPEOF(preserveDraw) != LGLSXP)
    {
        ::Rf_error("n and k should be integers, preserveDraw should be a logical");
    }
    
    try
    {
        Rcpp::RNGScope tempRNG;
        
        int m_n = Rcpp::as<int>(n);
        int m_k = Rcpp::as<int>(k);
        bool bPreserveDraw = Rcpp::as<bool>(preserveDraw);

        lhs_r::checkArguments(m_n, m_k);
        if (m_n == 1)
        {
            return lhs_r::degenerateCase(m_k);
        }
        
        lhs_r::RStandardUniform oRStandardUniform = lhs_r::RStandardUniform();
        bclib::matrix<double> result = bclib::matrix<double>(m_n, m_k);
        lhslib::randomLHS(m_n, m_k, bPreserveDraw, result, oRStandardUniform);

        Rcpp::NumericMatrix rresult(m_n, m_k);
        for (int irow = 0; irow < m_n; irow++)
        {
            for (int jcol = 0; jcol < m_k; jcol++)
            {
                rresult(irow, jcol) = result(irow, jcol);
            }
        }

        return rresult;
    }
    catch (Rcpp::not_compatible & nc)
    {
        std::stringstream message;
        message << nc.what() << "\n" << "Error is likely caused by an input vector where a single value is required.";
        ::Rf_error(message.str().c_str());
    }
    catch (std::exception & e)
    {
        ::Rf_error(e.what());
    }
    catch (...)
    {
        ::Rf_error("Unrecognized exception");
    }
    return ::R_NilValue;
}
Beispiel #3
0
void SerialPortLister::ListPorts(std::vector<std::string> &availablePorts)
{
#ifdef WIN32

   availablePorts.clear();
   std::auto_ptr<B100000> allDeviceNames (new B100000());

   // on Windows the serial ports are devices that begin with "COM"
   int ret = QueryDosDevice( 0, allDeviceNames->buffer, MaxBuf);
   if( 0!= ret)
   {
      for( int ii = 0; ii < ret; ++ii)
      {
         if ( 0 == allDeviceNames->buffer[ii])
            allDeviceNames->buffer[ii] = ' ';
      }
      std::string all(allDeviceNames->buffer, ret);
      std::vector<std::string> tokens;
      CDeviceUtils::Tokenize(all, tokens, " ");
      for( std::vector<std::string>::iterator jj = tokens.begin(); jj != tokens.end(); ++jj)
      {
         if( 0 == jj->substr(0,3).compare("COM"))
            availablePorts.push_back(*jj);
      }
      
   }
#endif // WIN32

#ifdef linux 
   // Look for /dev files with correct signature 
   DIR* pdir = opendir("/dev");
   struct dirent *pent;
   if (pdir) {
      while (pent = readdir(pdir)) {
         if ( (strstr(pent->d_name, "ttyS") != 0) || 
               (strstr(pent->d_name, "ttyUSB") != 0)  || 
               (strstr(pent->d_name, "ttyACM") != 0))  
         {
            std::string p = ("/dev/");
            p.append(pent->d_name);
            if (portAccessible(p.c_str()))
               availablePorts.push_back(p.c_str());
         }
      }
   }
#endif // linux 

#ifdef __APPLE__
   // port discovery code for Darwin/Mac OS X
   // Derived from Apple's examples at: http://developer.apple.com/samplecode/SerialPortSample/SerialPortSample.html
   io_iterator_t   serialPortIterator;
   char            bsdPath[256];
   kern_return_t       kernResult;
   CFMutableDictionaryRef classesToMatch; 
                                                                                 
   // Serial devices are instances of class IOSerialBSDClient          
   classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue); 
   if (classesToMatch == NULL) {                                 
       printf("IOServiceMatching returned a NULL dictionary.\n");  
   } else {                                                   
       CFDictionarySetValue(classesToMatch,                         
                           CFSTR(kIOSerialBSDTypeKey),        
                           CFSTR(kIOSerialBSDAllTypes));   
   }
   kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, classesToMatch, &serialPortIterator);
   if (KERN_SUCCESS != kernResult) {
      printf("IOServiceGetMatchingServices returned %d\n", kernResult);
   }                           
                                                                        
   // Given an iterator across a set of modems, return the BSD path to the first one.
   // If no modems are found the path name is set to an empty string.            
   io_object_t      modemService;
   
   // Initialize the returned path                                              
   *bsdPath = '\0';           
   // Iterate across all modems found.                                          
   while ( (modemService = IOIteratorNext(serialPortIterator)) ) {
       CFTypeRef bsdPathAsCFString;
       // Get the device's path (/dev/tty.xxxxx).
       bsdPathAsCFString = IORegistryEntryCreateCFProperty(modemService,
                               CFSTR(kIODialinDeviceKey),          
                               kCFAllocatorDefault,  
                               0);                   
      if (bsdPathAsCFString) {
         Boolean result;                                                        
         // Convert the path from a CFString to a C (NUL-terminated) string for use           
         // with the POSIX open() call. 
         result = CFStringGetCString( (const __CFString*) bsdPathAsCFString, 
                                         bsdPath,   
                                         sizeof(bsdPath), 
                                         kCFStringEncodingUTF8); 

         CFRelease(bsdPathAsCFString);

         // add the name to our vector<string> only when this is not a dialup port
         std::string rresult (bsdPath);
         std::string::size_type loc = rresult.find("DialupNetwork", 0);
         if (result && (loc == std::string::npos)) {
             bool blackListed = false;
             std::vector<std::string>::iterator it = g_BlackListedPorts.begin();
             while (it < g_BlackListedPorts.end()) {
                if( bsdPath == (*it))
                   blackListed = true;
            }
            if (portAccessible(bsdPath) && ! blackListed)  {
                 availablePorts.push_back(bsdPath);
            }                 
            kernResult = KERN_SUCCESS;
         }
      }
   } 

    // Release the io_service_t now that we are done with it.
    (void) IOObjectRelease(modemService);

#endif // __APPLE
}