Skip to content

Commit

Permalink
240129.114725.HKT improve data.c
Browse files Browse the repository at this point in the history
  • Loading branch information
zaikunzhang committed Jan 29, 2024
1 parent 3ff4086 commit bfd1855
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 48 deletions.
116 changes: 69 additions & 47 deletions c/tests/data.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Test for data callback argument.
// Test for the `data` argument in the objective/constraint callback function.

#include "prima/prima.h"
#include <stdio.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "prima/prima.h"

#define M_NLCON 1

const int n = 2;
int debug = 0;
static int int_data = 0xff;
Expand All @@ -18,17 +20,17 @@ static void fun(const double x[], double *f, const void *data)
const double x2 = x[1];
*f = 5*(x1-3)*(x1-3)+7*(x2-2)*(x2-2)+0.1*(x1+x2)-10;

static int count = 0;
static int nf = 0;
if (debug)
{
++ count;
printf("count=%d\n", count);
++ nf;
printf("Number of function evaluations = %d\n", nf);
}

// check data is ok
// Check whether data is OK
if (data != data_ref)
{
printf("invalid data\n");
printf("Invalid data!\n");
*f = NAN;
}
}
Expand All @@ -37,97 +39,117 @@ static void fun_con(const double x[], double *f, double constr[], const void *da
{
const double x1 = x[0];
const double x2 = x[1];
*f = 5*(x1-3)*(x1-3)+7*(x2-2)*(x2-2)+0.1*(x1+x2)-10;
constr[0] = x1*x1 + x2*x2 - 13;// ||x||^2<=13
*f = 5*(x1-3)*(x1-3)+7*(x2-2)*(x2-2) + 0.1*(x1+x2) - 10;
constr[0] = x1*x1 + x2*x2 - 13; // ||x||^2<=13

static int count = 0;
static int nf = 0;
if (debug)
{
++ count;
printf("count=%d\n", count);
++ nf;
printf("Number of function evaluations = %d\n", nf);
}

// check data is ok
// Check whether data is OK
if (data != data_ref)
{
printf("invalid data\n");
printf("Invalid data!\n");
*f = NAN;
}
}

int main(int argc, char * argv[])
{
char *algo = "bobyqa";
char *algo = "uobyqa";
prima_algorithm_t algorithm = PRIMA_UOBYQA;
if (argc > 1)
algo = argv[1];
printf("algo=%s\n", algo);
printf("Algorithm = %s\n", algo);

if (argc > 2)
debug = (strcmp(argv[2], "debug") == 0);
printf("debug=%d\n", debug);
printf("debug = %d\n", debug);

double x0[] = {0.0,
0.0};
double xl[] = {-6.0,
-6.0};
double xu[] = {6.0,
6.0};
double Aineq[3*2] = {1.0, 0.0,
0.0, 1.0,
1.0, 1.0};
double bineq[3] = {4.0,
3.0,
10.0};

double x0[] = {0, 0};
double xl[] = {-6.0, -6.0};
double xu[] = {6.0, 6.0};
prima_problem_t problem;
prima_init_problem(&problem, n);
problem.calfun = &fun;
problem.x0 = x0;

prima_options_t options;
prima_init_options(&options);
options.iprint = PRIMA_MSG_RHO;
options.maxfun = 500*n;
options.data = data_ref;
double Aineq[3*2] = {1.0, 0.0,
0.0, 1.0,
1.0, 1.0};
double bineq[3] = {4.0,
3.0,
10.0};

prima_result_t result;
prima_algorithm_t algorithm = 0;
if(strcmp(algo, "bobyqa") == 0)

// Define the algorithm and the problem according to `algo`
if(strcmp(algo, "uobyqa") == 0)
{
algorithm = PRIMA_UOBYQA;
problem.calfun = &fun;
}
else if(strcmp(algo, "newuoa") == 0)
{
algorithm = PRIMA_NEWUOA;
problem.calfun = &fun;
}
else if(strcmp(algo, "bobyqa") == 0)
{
algorithm = PRIMA_BOBYQA;
problem.calfun = &fun;
problem.xl = xl;
problem.xu = xu;
}
else if(strcmp(algo, "cobyla") == 0)
else if(strcmp(algo, "lincoa") == 0)
{
algorithm = PRIMA_COBYLA;
algorithm = PRIMA_LINCOA;
problem.calfun = &fun;
problem.xl = xl;
problem.xu = xu;
problem.m_ineq = 3;
problem.Aineq = Aineq;
problem.bineq = bineq;
problem.m_nlcon = M_NLCON;
problem.calcfc = &fun_con;
}
else if(strcmp(algo, "lincoa") == 0)
else if(strcmp(algo, "cobyla") == 0)
{
algorithm = PRIMA_LINCOA;
algorithm = PRIMA_COBYLA;
problem.m_nlcon = M_NLCON;
problem.calcfc = &fun_con;
problem.xl = xl;
problem.xu = xu;
problem.m_ineq = 3;
problem.Aineq = Aineq;
problem.bineq = bineq;
}
else if(strcmp(algo, "newuoa") == 0)
{
algorithm = PRIMA_NEWUOA;
}
else if(strcmp(algo, "uobyqa") == 0)
{
algorithm = PRIMA_UOBYQA;
}
else
{
printf("incorrect algo\n");
printf("Invalid algorithm!\n");
return 1;
}

// Call the solver
int rc = prima_minimize(algorithm, &problem, &options, &result);
printf("f*=%g cstrv=%g nlconstr=%g rc=%d msg='%s' evals=%d\n", result.f, result.cstrv, result.nlconstr ? result.nlconstr[0] : 0.0, rc, result.message, result.nf);
int success = (fabs(result.x[0]-3)>2e-2 || fabs(result.x[1]-2)>2e-2);

// Print the result
printf("f* = %g, cstrv = %g, nlconstr = {%g}, rc = %d, msg = '%s', evals = %d\n", result.f, result.cstrv, result.nlconstr ? result.nlconstr[0] : 0.0, rc, result.message, result.nf);

// Check the result
int success = (fabs(result.x[0] - 3) > 2e-2 || fabs(result.x[1] - 2) > 2e-2);

// Free the result
prima_free_result(&result);

return success;
}
2 changes: 1 addition & 1 deletion c/tests/stress.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ int main(int argc, char * argv[])
return 1;
}
rc = prima_minimize(algorithm, &problem, &options, &result);
printf("f*=%g cstrv=%g nlconstr=%g rc=%d msg='%s' evals=%d\n", result.f, result.cstrv, result.nlconstr ? result.nlconstr[0] : 0.0, rc, result.message, result.nf);
printf("f*=%g cstrv=%g nlconstr={%g} rc=%d msg='%s' evals=%d\n", result.f, result.cstrv, result.nlconstr ? result.nlconstr[0] : 0.0, rc, result.message, result.nf);
prima_free_result(&result);
return 0;
}

0 comments on commit bfd1855

Please sign in to comment.