Skip to content

Commit

Permalink
240201.005300.HKT C: change the indentation from 2 spaces to 4 spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
zaikunzhang committed Jan 31, 2024
1 parent 716393e commit c159aed
Show file tree
Hide file tree
Showing 7 changed files with 521 additions and 520 deletions.
82 changes: 41 additions & 41 deletions c/examples/bobyqa/bobyqa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,62 @@
// Objective function
static void fun(const double x[], double *f, const void *data)
{
const double x1 = x[0];
const double x2 = x[1];
*f = pow(x1-5, 2) + pow(x2-4, 2);
(void)data;
const double x1 = x[0];
const double x2 = x[1];
*f = pow(x1-5, 2) + pow(x2-4, 2);
(void)data;
}

// Callback function
static void callback(int n, const double x[], double f, int nf, int tr, double cstrv, const int m_nlcon, const double nlconstr[], bool *terminate)
{
(void)n;
(void)cstrv;
(void)m_nlcon;
(void)nlconstr;
printf("Best point so far: x = {%g, %g}, f = %g, nf = %d, tr = %d\n", x[0], x[1], f, nf, tr);
*terminate = 0;
(void)n;
(void)cstrv;
(void)m_nlcon;
(void)nlconstr;
printf("Best point so far: x = {%g, %g}, f = %g, nf = %d, tr = %d\n", x[0], x[1], f, nf, tr);
*terminate = 0;
}

// Main function
int main(int argc, char * argv[])
{
(void)argc;
(void)argv;
const int n = 2;
double x0[2] = {0.0, 0.0};
(void)argc;
(void)argv;
const int n = 2;
double x0[2] = {0.0, 0.0};

// Set up the problem
prima_problem_t problem;
prima_init_problem(&problem, n);
problem.x0 = x0;
problem.calfun = &fun;
// We define an upper bound that will be active in order to demonstrate the usage.
double xl[] = {-1.0, -1.0};
double xu[] = {4.5, 4.5};
problem.xl = xl;
problem.xu = xu;
// Set up the problem
prima_problem_t problem;
prima_init_problem(&problem, n);
problem.x0 = x0;
problem.calfun = &fun;
// We define an upper bound that will be active in order to demonstrate the usage.
double xl[] = {-1.0, -1.0};
double xu[] = {4.5, 4.5};
problem.xl = xl;
problem.xu = xu;

// Set up the options
prima_options_t options;
prima_init_options(&options);
options.iprint = PRIMA_MSG_EXIT;
options.rhoend= 1e-3;
options.maxfun = 200*n;
options.callback = &callback;
// Set up the options
prima_options_t options;
prima_init_options(&options);
options.iprint = PRIMA_MSG_EXIT;
options.rhoend= 1e-3;
options.maxfun = 200*n;
options.callback = &callback;

// Call the solver
prima_result_t result;
const int rc = prima_minimize(PRIMA_BOBYQA, &problem, &options, &result);
// Call the solver
prima_result_t result;
const int rc = prima_minimize(PRIMA_BOBYQA, &problem, &options, &result);

// Print the result
printf("x* = {%g, %g}, rc = %d, msg = '%s', evals = %d\n", result.x[0], result.x[1], rc, result.message, result.nf);
// Print the result
printf("x* = {%g, %g}, rc = %d, msg = '%s', evals = %d\n", result.x[0], result.x[1], rc, result.message, result.nf);

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

// Free the result
prima_free_result(&result);
// Free the result
prima_free_result(&result);

return success;
return success;
}
82 changes: 41 additions & 41 deletions c/examples/cobyla/cobyla_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,64 +13,64 @@
// Objective & constraint function
static void fun(const double x[], double *f, double constr[], const void *data)
{
const double x1 = x[0];
const double x2 = x[1];
*f = pow(x1-5, 2) + pow(x2-4, 2);
constr[0] = pow(x1, 2) - 9; // Constraint: x1^2 - 9 <= 0
(void)data;
const double x1 = x[0];
const double x2 = x[1];
*f = pow(x1-5, 2) + pow(x2-4, 2);
constr[0] = pow(x1, 2) - 9; // Constraint: x1^2 - 9 <= 0
(void)data;
}

// Callback function
static void callback(const int n, const double x[], const double f, const int nf, const int tr, const double cstrv, const int m_nlcon, const double nlconstr[], bool *terminate)
{
(void)n;
(void)m_nlcon;
printf("Best point so far: x = {%g, %g}, f = %g, cstrv = %g, nlconstr = {%g}, nf = %d, tr = %d\n", x[0], x[1], f, cstrv, nlconstr[0], nf, tr);
*terminate = 0;
(void)n;
(void)m_nlcon;
printf("Best point so far: x = {%g, %g}, f = %g, cstrv = %g, nlconstr = {%g}, nf = %d, tr = %d\n", x[0], x[1], f, cstrv, nlconstr[0], nf, tr);
*terminate = 0;
}

// Main function
int main(int argc, char * argv[])
{
(void)argc;
(void)argv;
const int n = 2;
double x0[2] = {0.0, 0.0};
(void)argc;
(void)argv;
const int n = 2;
double x0[2] = {0.0, 0.0};

// Set up the problem
prima_problem_t problem;
prima_init_problem(&problem, n);
problem.x0 = x0;
problem.m_nlcon = M_NLCON;
problem.calcfc = &fun;
// Provide the initial values of the objective function and the nonlinear constraints.
// This is OPTIONAL, and end users should NOT do it in general. Here, we do it for testing.
// Set up the problem
prima_problem_t problem;
prima_init_problem(&problem, n);
problem.x0 = x0;
problem.m_nlcon = M_NLCON;
problem.calcfc = &fun;
// Provide the initial values of the objective function and the nonlinear constraints.
// This is OPTIONAL, and end users should NOT do it in general. Here, we do it for testing.
#if PROVIDE_INITIAL_F_AND_NLCONSTR
double nlconstr0[M_NLCON] = {0};
fun(x0, &(problem.f0), nlconstr0, NULL);
problem.nlconstr0 = nlconstr0;
double nlconstr0[M_NLCON] = {0};
fun(x0, &(problem.f0), nlconstr0, NULL);
problem.nlconstr0 = nlconstr0;
#endif

// Set up the options
prima_options_t options;
prima_init_options(&options);
options.iprint = PRIMA_MSG_EXIT;
options.rhoend= 1e-3;
options.maxfun = 200*n;
options.callback = &callback;
// Set up the options
prima_options_t options;
prima_init_options(&options);
options.iprint = PRIMA_MSG_EXIT;
options.rhoend= 1e-3;
options.maxfun = 200*n;
options.callback = &callback;

// Call the solver
prima_result_t result;
const int rc = prima_minimize(PRIMA_COBYLA, &problem, &options, &result);
// Call the solver
prima_result_t result;
const int rc = prima_minimize(PRIMA_COBYLA, &problem, &options, &result);

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

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

// Free the result
prima_free_result(&result);
// Free the result
prima_free_result(&result);

return success;
return success;
}
84 changes: 42 additions & 42 deletions c/examples/lincoa/lincoa_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,63 +9,63 @@
// Objective function
static void fun(const double x[], double *f, const void *data)
{
const double x1 = x[0];
const double x2 = x[1];
*f = pow(x1-5, 2) + pow(x2-4, 2);
(void)data;
const double x1 = x[0];
const double x2 = x[1];
*f = pow(x1-5, 2) + pow(x2-4, 2);
(void)data;
}

// Callback function
static void callback(int n, const double x[], double f, int nf, int tr, double cstrv, int m_nlcon, const double nlconstr[], bool *terminate)
{
(void)n;
(void)m_nlcon;
(void)nlconstr;
printf("Best point so far: x = {%g, %g}, f = %g, cstrv = %g, nf = %d, tr = %d\n", x[0], x[1], f, cstrv, nf, tr);
*terminate = 0;
(void)n;
(void)m_nlcon;
(void)nlconstr;
printf("Best point so far: x = {%g, %g}, f = %g, cstrv = %g, nf = %d, tr = %d\n", x[0], x[1], f, cstrv, nf, tr);
*terminate = 0;
}

// Main function
int main(int argc, char * argv[])
{
(void)argc;
(void)argv;
const int n = 2;
double x0[2] = {0.0, 0.0};
(void)argc;
(void)argv;
const int n = 2;
double x0[2] = {0.0, 0.0};

// Set up the problem
prima_problem_t problem;
prima_init_problem(&problem, n);
problem.calfun = &fun;
problem.x0 = x0;
// We define linear constraints that will be active in order to demonstrate the usage.
// The constraint is x1 + x2 <= 8.
problem.m_ineq = 1;
double Aineq[1*2] = {1.0, 1.0};
double bineq[1] = {8.0};
problem.Aineq = Aineq;
problem.bineq = bineq;
// Set up the problem
prima_problem_t problem;
prima_init_problem(&problem, n);
problem.calfun = &fun;
problem.x0 = x0;
// We define linear constraints that will be active in order to demonstrate the usage.
// The constraint is x1 + x2 <= 8.
problem.m_ineq = 1;
double Aineq[1*2] = {1.0, 1.0};
double bineq[1] = {8.0};
problem.Aineq = Aineq;
problem.bineq = bineq;

// Set up the options
prima_options_t options;
prima_init_options(&options);
options.iprint = PRIMA_MSG_EXIT;
options.rhoend= 1e-3;
options.maxfun = 200*n;
options.callback = &callback;
// Set up the options
prima_options_t options;
prima_init_options(&options);
options.iprint = PRIMA_MSG_EXIT;
options.rhoend= 1e-3;
options.maxfun = 200*n;
options.callback = &callback;

// Call the solver
prima_result_t result;
const int rc = prima_minimize(PRIMA_LINCOA, &problem, &options, &result);
// Call the solver
prima_result_t result;
const int rc = prima_minimize(PRIMA_LINCOA, &problem, &options, &result);

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

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

// Free the result
prima_free_result(&result);
// Free the result
prima_free_result(&result);

return success;
return success;
}
Loading

0 comments on commit c159aed

Please sign in to comment.