[Rcpp-devel] Rm: R Embedded in a class function

CESAR PERNALETE pernaletec at pdvsa.com
Wed Mar 23 15:15:39 CET 2011


Hi Davor. 

I already had done what you are suggesting. In fact the example already 
works fine. The mistake was related to something with the NOMAD 
specification. 

Thanks a lot for your help!

Now the example code looks like: 

#include "nomad.hpp"
#include <RInside.h>
#include <Rcpp.h>
using namespace std;

/*----------------------------------------*/
/*               The problem              */
/*----------------------------------------*/

class My_Evaluator : public NOMAD::Evaluator {
public:
    My_Evaluator ( const NOMAD::Parameters & p, RInside & R) :
            NOMAD::Evaluator ( p) ,m_R(R)  {}

  ~My_Evaluator ( void ) {}

  RInside & m_R;                // reference to the R instance passed to 
constructor

  bool eval_x ( NOMAD::Eval_Point   & x          ,
                const NOMAD::Double & h_max      ,
                bool                & count_eval   ) const {

m_R.parseEvalQ("rFunction <- function (a,b,c){result <- (a + b + c) * 20; 
return(result)}");

   double x0;
   x0= x[0].value();
   double x1;
   x1= x[1].value();
   double x2;
   x2= x[2].value();
 
   m_R.assign( x0,"x0" );
   m_R.assign( x1,"x1" );
   m_R.assign( x2,"x2" );
 
   m_R.parseEvalQ ("eval.data[1,1]<-x0");
   m_R.parseEvalQ ("eval.data[1,2]<-x1");
   m_R.parseEvalQ ("eval.data[1,3]<-x2");

 
m_R.parseEvalQ("predict<-rFunction(eval.data[1,1],eval.data[1,2],eval.data[1,3])");

   double predict_ = m_R["predict"];

   NOMAD::Double predict = predict_;

   NOMAD::Double c1;

   c1= x0*predict + x1+x2-72;                       //Only one constraint

   x.set_bb_output  ( 0 , x0*46.74 + x1*43.47 + x2*55.72-80); // objective 
value
   x.set_bb_output  ( 1 , c1);     // constraint 1
 
   count_eval = true; // count a black-box evaluation

    return true;       // the evaluation succeeded
  }
};

/*------------------------------------------*/
/*            NOMAD main function           */
/*------------------------------------------*/
int main ( int argc , char ** argv ) {

  // display:
  NOMAD::Display out ( std::cout );
  out.precision ( NOMAD::DISPLAY_PRECISION_STD );

  try {

      RInside R(argc,argv);  // create an embedded R instance

    // NOMAD initializations:
    NOMAD::begin ( argc , argv );

    // parameters creation:
    NOMAD::Parameters p ( out );

    //p.set_DIMENSION (8);             // number of variables
    p.set_DIMENSION (3);             // number of variables

    vector<NOMAD::bb_output_type> bbot (2); // definition of
    bbot[0] = NOMAD::OBJ;                   // output types
    bbot[1] = NOMAD::PB;
    p.set_BB_OUTPUT_TYPE ( bbot );

    p.set_DISPLAY_STATS ( "bbe ( sol ) obj" );

    // p.set_DISPLAY_DEGREE ( FULL_DISPLAY );

    NOMAD::Point sp(3);
    sp[0]=0.15;
    sp[1]=0.10;
    sp[2]=0.3;
 
    p.set_X0 ( sp );  // starting point

    NOMAD::Point lb ( 3 );                  // Límite inferior
    lb[0] = 0;                              //
    lb[1] = 0;                              //
    lb[2] = 0;                              //
 
    p.set_LOWER_BOUND ( lb ); // all var. >= 0

    NOMAD::Point ub ( 3 );                    // Límite superior
    ub[0] = 0.22;                               //
    ub[1] = 0.18;                               //
    ub[2] = 1.00;                              //
 
    p.set_UPPER_BOUND ( ub );

    p.set_MAX_BB_EVAL (50);     // the algorithm terminates after
                                 // 50 black-box evaluations

    // parameters validation:
    p.check();

    // custom evaluator creation:
    My_Evaluator ev   ( p,R );

    // algorithm creation and execution:
    NOMAD::Mads mads ( p , &ev );

    mads.run();

  }
  catch ( exception & e ) {
    cerr << "\nNOMAD has been interrupted (" << e.what() << ")\n\n";
  }

  NOMAD::Slave::stop_slaves ( out );
  NOMAD::end();

  return EXIT_SUCCESS;
}

Best regards!!

César 




Davor Cubranic <cubranic at stat.ubc.ca> 
21/03/2011 10:33 a.m.

Para
CESAR PERNALETE <pernaletec at pdvsa.com>
cc
rcpp-devel at r-forge.wu-wien.ac.at
Asunto
Re: [Rcpp-devel] R Embedded in a class function






Hi Cesar,

I don't know why your embed does not work, but I would suggest to rework 
your code a bit and follow the pattern used by Dirk in his Qt example:

1. create an RInside instance "R" in the "main" function
2. pass that RInside instance by reference to the constructor for 
MyEvaluator and save it to a private member variable
3. MyEvaluator::eval_x can work the same as now, except that it will begin 
at the line "R.parseEvalQ" since you already have the RInside instance

Hope this helps,

Davor

----- Remitido por CESAR PERNALETE/PERNALETEC/PDV/PDVSA con fecha 
23/03/2011 09:33 a.m. -----

CESAR PERNALETE/PERNALETEC/PDV/PDVSA
21/03/2011 11:23 a.m.

Para
Davor Cubranic <cubranic at stat.ubc.ca>, Dirk Eddelbuettel <edd at debian.org>
cc
rcpp-devel at r-forge.wu-wien.ac.at, rcpp-devel-bounces at r-forge.wu-wien.ac.at
Asunto
Re: [Rcpp-devel] R Embedded in a class function





Hi Davor, Dirk.

Thanks a lot for your help. I tried the way Dirk are suggesting and it 
appears to work fine. The application at all is not working yet, i don't 
know exactly 
what is happening, so i did a consultation to the NOMAD developer. But i 
believe that the problem of using the R object into a method is solved. 

Please, find attached a simplified example of the code. 

If you see something weird, please don't hesitate in writing to the list. 

Best regards. 

César 






Davor Cubranic <cubranic at stat.ubc.ca> 
Enviado por: rcpp-devel-bounces at r-forge.wu-wien.ac.at
17/03/2011 07:51 p.m.

Para
Dirk Eddelbuettel <edd at debian.org>
cc
rcpp-devel at r-forge.wu-wien.ac.at
Asunto
Re: [Rcpp-devel] R Embedded in a class function






On 2011-03-17, at 3:31 PM, Dirk Eddelbuettel wrote:

> | And it so happens that I just wrote an example of instantiating 
RInside and
> | passing to Qt so that you'd get 'R inside Qt'.  That is now in SVN for
> | RInside but I haven't even gotten around to writing a short blog post 
about
> | it.  Maybe on the weekend...
> 
> I forgot to give the URL:
> 
> 
https://r-forge.r-project.org/scm/viewvc.php/pkg/inst/examples/qt/?root=rinside

> 

But your code still creates R instance in "main", which it then passes to 
the QtDensity instance (a subclass of QtMainWindow). OTOH, once the R 
instance is stored as a member variable of QtDensity, it *is* used in 
QtDensity's methods. 

But I'm quibbling -- it's a good pattern to follow, so Cesar could perhaps 
just adapt it to his code.

Davor
_______________________________________________
Rcpp-devel mailing list
Rcpp-devel at lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110323/93c32782/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: embedRinAmethodv2.cpp
Type: application/octet-stream
Size: 3843 bytes
Desc: not available
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110323/93c32782/attachment-0001.obj>


More information about the Rcpp-devel mailing list