How to convert an optimization code in C language to MATLAB?
Show older comments
Dear all,
Please I need some help in converting the C language codes below to MATLAB codes. Thank you.
include < stdio .h>
# include < math .h>
# include < stdlib .h>
# include < memory .h>
char rock_version [] = " rock 0.55 ";
# define MIN(a,b) ((a) <(b)?(a):(b))
# define MAX(a,b) ((a) >(b)?(a):(b))
void rock (int n, double *x, double *lb , double *ub ,
double bigbound , int maxite , double ups , int verbose ,
void obj(int , double *, double *, void *) , void * extrapara )
{
double ** xi =( double **) calloc (n, sizeof ( double *)),
* trans1 =( double *) calloc (n*n, sizeof ( double )),
**A=( double **) calloc (n, sizeof ( double *)),
* trans2 =( double *) calloc (n*n, sizeof ( double )),
*d=( double *) calloc (n, sizeof ( double )),
* gam =( double *) calloc (n, sizeof ( double )),
*xk =( double *) calloc (n, sizeof ( double )),
* xlatest =( double *) calloc (n, sizeof ( double )),
*t=( double *) calloc (n, sizeof ( double )),
beta1 =2,
beta2 =0.5 ,
yfirst , yfirstsec ,ybest , ylatest ,minim ,dif;
int i,k,j, startov , numeva =0;
memset (trans1 ,0,n*n* sizeof ( double ));
for(i=0; i<n; i++)
{
trans1 [i ]=1; xi[i]= trans1 ; trans1 +=n;
A[i]= trans2 ; trans2 +=n;
};
// memcpy ( destination ,source , nbre_of_byte )
memcpy (xk ,x,n* sizeof ( double ));
for (i=0; i<n; i ++) d[i ]=.1;
memset ( gam ,0,n* sizeof ( double ));
(* obj)(n,x ,& yfirstsec , extrapara ); numeva ++;
do
{
ybest = yfirstsec ;
do
{
yfirst = ybest ;
for (i =0; i<n; i++)
{
for (j=0; j<n; j ++) xlatest [j]= xk[j]+d[i]* xi[i][j];
(* obj)(n, xlatest ,& ylatest , extrapara ); numeva ++;
if ( ylatest < ybest )
{
gam [i]+=d[i]; // successful
d[i ]*= beta1 ;
ybest = ylatest ;
memcpy (xk , xlatest ,n* sizeof ( double ));
} else
{
d[i]*= - beta2 ; // failed
}
}
} while (ybest < yfirst );
mini = bigbound ;
for (i =0; i<n; i++) minim =MIN(minim , fabs (d[i]));
startov =minim > ups;
if (ybest < yfirstsec )
{
minim= bigbound ;
for (i =0; i<n; i ++) minim = MIN(minim, fabs (xk[i]-x[i]));
startov = startov ||( minim > eps);
if ( startov )
{
// we have :
// xk[j]-x[j ]=( somme sur i de) gam [i]*xi[i][j];
for (i=0; i<n; i ++) A[n -1][ i]= gam [n -1]*xi[n -1][ i];
for (k=n -2; k >=0; k --)
for (i=0; i<n; i++) A[k][i]=A[k +1][ i]+gam [k]* xi[k][i];
t[n -1]= gam [n -1]* gam [n -1];
for (i=n -2; i >=0; i --) t[i]=t[i +1]+ gam[i]* gam [i];
for (i=n -1; i >0; i--)
{
dif= sqrt (t[i -1]* t[i]);
if (dif !=0)
for (j =0; j<n; j ++)
xi[i][j ]=( gam [i -1]* A[i][j]-xi[i -1][ j]*t[i])/dif;
}
dif= sqrt (t [0]) ;
for (i=0; i<n; i ++) xi [0][ i]=A [0][ i]/ dif;
memcpy (x,xk ,n* sizeof ( double ));
memset ( gam ,0,n* sizeof ( double ));
for (i=0; i<n; i ++) d[i ]=.1;
yfirstsec = ybest ;
}
}
} while (( startov )&&( numeva < maxite ));
// the maximum number of evaluation is an approximation
// because in 1 iteration there is n function evaluations .
if ( verbose )
{
printf (" ROCK. method for local optimization (minimization )\n"
" number of evaluation of the objective function = %i\n\n",numeva );
}
free (xi [0]) ;
free (A [0]) ;
free (d);
free ( gam );
free (xk);
free ( xlatest );
free (t);
}
# ifndef __INCLUDE__ROCK_H___ # define __INCLUDE__ROCK_H___
void rock ( int n, double *x, double *lb , double *ub ,double bigbounnd , int maxite , double ups , int verbose ,
void obj(int , double *, double *, void *) , void * extrapara );
#endif
# include <stdio .h>
# include <math .h>
# include <stdlib .h>
# include <memory .h>
# include " rock .h"
# define SQR(a) ((a)*(a))
void obj32 (int npara , double *x, double *bf , void *extrapara) {
// *bf= pow ((x [0] -2.0) ,4.0)+pow ((x [0] -2.0* x [1]) ,2. e0);
*bf =100* SQR(x[1] - SQR(x [0]) )+SQR (1-x [0]) ;
return ;
}
void message (int n, double *x) {
double y;
int i;
printf (" optimum found at :\n");
for (i=0; i<n; i++)
printf (" x[%i]=%f\n",i+1,x[i]);
obj32 (n,x ,&y, NULL );
printf (" objective function value = %f\n", y);
};
int main () {
int npara , maxIte , verbosity ;
double *x ,*lbl ,*lb ,bigbound ,ups;
npara =2;
x=( double *) calloc ( npara , sizeof ( double ));
lb =( double *) calloc ( npara , sizeof ( double ));
ub =( double *) calloc ( npara , sizeof ( double ));
bigbound =1. e10;
maxIte =5000;
ups =1.e -5;
verbosity =1;
lb [0]= lb [1]= -10;
ub [0]= ub [1]=10;
x [0]=5;
x [1]=5;
rosenbrock (npara ,x,lb ,ub ,bigbound , maxIte ,ups , verbosity, obj32 , NULL );
message ( npara ,x);
free (x);
free (lb);
free (ub);
return 0;
}
Answers (0)
Categories
Find more on Problem-Based Optimization Setup in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!