[prev in list] [next in list] [prev in thread] [next in thread] 

List:       scilab-dev
Subject:    SciFunction
From:       COLLETTE Yann <yann.collette () renault ! com>
Date:       2008-02-06 13:50:30
Message-ID: 47A9BB26.70501 () renault ! com
[Download RAW message or body]

Hello,

I'm trying to interface an optimisation method under scilab (the 
optimization method is in fortran and I'm writing the interface in C).
My question is: how to retrieve the output parameters of a call to 
SciFunction ?
The following program is called several times during the run of the 
optimization:

/////////////////////////////////////
//  First I create some parameters. The first one will contain the "x" 
vector and the second one the gradient of the output (I do this to give 
the correct size to the stack. If I don't do this, scilab says it 
doesn't know the ibegin + 1 parameter).

  // Create the first variable: x
  n_x1_tmp = n_x;
  m_x1_tmp = m_x;
  CreateVar(ibegin,"d",&n_x1_tmp,&m_x1_tmp,&l_x1_tmp);
  for(i=0;i<ndv; i++) *dstk(l_x1_tmp+i) = *dstk(l_x+i);

  // Create a fake 2nd variable on the stack. This will allow to call 
GetRhsVar to retrieve the 2nd output argument
  n_x2_tmp = 1;
  m_x2_tmp = 1;
  CreateVar(ibegin+1,"d",&n_x2_tmp,&m_x2_tmp,&l_x2_tmp);

///////////////////////////////////
// I call the scilab function via SciFunction


  // Call to the scilab function
  SciFunction(&ibegin,&l_fobj,&fobj_lhs,&fobj_rhs);
  if (Err>0)
    {
      sciprint("conmin: error when calling objective function\n");
      return 0;
    } /* End If */

  n1 = ndv + 2;

////////////////////////////////////
// Now I get the parameters via GetRhsVar. I hope that the size of the 
ibegin and ibegin+1 parameters are set correctly by SciFunction (first 
free the memory and then mallocated correctly to get the output parameters).
  // Get fobj
  GetRhsVar(ibegin,  "d",&n_fobj_tmp, &m_fobj_tmp, &l_fobj_tmp);
  // Get gradient of fobj
  GetRhsVar(ibegin+1,"d",&n_dfobj_tmp,&m_dfobj_tmp,&l_dfobj_tmp);

/////////////////////////////////////
// Finally, I store all the output parameters in temporary arrays 
(because just after the objective function, I called the constraints 
also via SciFunction).

  // Fill C2F(cnmn1).obj: objective function value
  C2F(cnmn1).obj = *dstk(l_fobj_tmp);

  // DF(N1) Analytic gradient of the objective function for the current 
decision variables, X(I).  DF(I) contains the partial derivative
  //        of OBJ with respect to X(I).  Calculate DF(I), I = 1, NDV if 
INFO = 3 or INFO = 4 and if NFDG = 0 or NFDG = 2.
  double * DF = (double *)malloc(n1*sizeof(double));
  // Fill DF(N1): gradient of objective function value
  for(j=0;j<ndv;j++) DF[j] = *dstk(l_dfobj_tmp+j);

///////////////////////////////////////
// Is my strategy good or do you recommend another method ?

Your sincerely,

Yann COLLETTE


-- Disclaimer ------------------------------------
Ce message ainsi que les eventuelles pieces jointes constituent une correspondance \
privee et confidentielle a l'attention exclusive du destinataire designe ci-dessus. \
Si vous n'etes pas le destinataire du present message ou une personne susceptible de \
pouvoir le lui delivrer, il vous est signifie que toute divulgation, distribution ou \
copie de cette transmission est strictement interdite. Si vous avez recu ce message \
par erreur, nous vous remercions d'en informer l'expediteur par telephone ou de lui \
retourner le present message, puis d'effacer immediatement ce message de votre \
                systeme.
***
This e-mail and any attachments is a confidential correspondence intended only for \
use of the individual or entity named above. If you are not the intended recipient or \
the agent responsible for delivering the message to the intended recipient, you are \
hereby notified that any disclosure, distribution or copying of this communication is \
strictly prohibited. If you have received this communication in error, please notify \
the sender by phone or by replying this message, and then delete this message from \
your system.


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic