Skip to content

Commit

Permalink
240430.174247.HKT try prima_internal.h
Browse files Browse the repository at this point in the history
  • Loading branch information
zaikunzhang committed Apr 30, 2024
1 parent daed3f3 commit a1b7f19
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 40 deletions.
38 changes: 38 additions & 0 deletions c/include/prima/prima_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#ifndef PRIMA_INTERNAL_H
#define PRIMA_INTERNAL_H

#include "prima/prima.h"

unsigned int get_random_seed(void);

// Function to check whether the problem matches the algorithm
prima_rc_t prima_check_problem(const prima_problem_t problem, const prima_algorithm_t algorithm);

// Function to initialize the result
prima_rc_t prima_init_result(prima_result_t *const result, const prima_problem_t problem);

// Functions implemented in Fortran (*_c.f90)
int cobyla_c(const int m_nlcon, const prima_objcon_t calcfc, const void *data, const int n, double x[], double *const f, double *const cstrv, double nlconstr[],
const int m_ineq, const double Aineq[], const double bineq[],
const int m_eq, const double Aeq[], const double beq[],
const double xl[], const double xu[],
const double f0, const double nlconstr0[],
int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int iprint, const double ctol,
const prima_callback_t callback, int *const info);

int bobyqa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f, const double xl[], const double xu[],
int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int npt, const int iprint, const prima_callback_t callback, int *const info);

int newuoa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f,
int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int npt, const int iprint, const prima_callback_t callback, int *const info);

int uobyqa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f,
int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int iprint, const prima_callback_t callback, int *const info);

int lincoa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f,
double *const cstrv, const int m_ineq, const double Aineq[], const double bineq[],
const int m_eq, const double Aeq[], const double beq[], const double xl[], const double xu[],
int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int npt, const int iprint, const double ctol,
const prima_callback_t callback, int *const info);

#endif
56 changes: 20 additions & 36 deletions c/prima.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Dedicated to the late Professor M. J. D. Powell FRS (1936--2015).


#include "prima/prima.h"
#include <float.h>
#include "prima/prima_internal.h"
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -65,6 +65,16 @@ prima_rc_t prima_init_options(prima_options_t *const options)
// Function to check whether the problem matches the algorithm
prima_rc_t prima_check_problem(const prima_problem_t problem, const prima_algorithm_t algorithm)
{
if (algorithm != PRIMA_COBYLA && (problem.calcfc || problem.nlconstr0 || problem.m_nlcon > 0))
return PRIMA_PROBLEM_SOLVER_MISMATCH_NONLINEAR_CONSTRAINTS;

if ((algorithm != PRIMA_COBYLA && algorithm != PRIMA_LINCOA) &&
(problem.m_ineq > 0 || problem.m_eq > 0 || problem.Aineq || problem.bineq || problem.Aeq || problem.beq))
return PRIMA_PROBLEM_SOLVER_MISMATCH_LINEAR_CONSTRAINTS;

if ((algorithm != PRIMA_COBYLA && algorithm != PRIMA_LINCOA && algorithm != PRIMA_BOBYQA) && (problem.xl || problem.xu))
return PRIMA_PROBLEM_SOLVER_MISMATCH_BOUNDS;

if (!problem.x0)
return PRIMA_NULL_X0;

Expand All @@ -90,10 +100,10 @@ prima_rc_t prima_init_result(prima_result_t *const result, const prima_problem_t
result->cstrv = NAN;

// nf: number of function evaluations
result->nf = 0;
result->nf = INT_MIN;

// status: return code
result->status = PRIMA_RESULT_INITIALIZED;
result->status = INT_MIN;

// message: exit message
result->message = NULL;
Expand Down Expand Up @@ -182,37 +192,17 @@ const char *prima_get_rc_string(const prima_rc_t rc)
return "NULL result";
case PRIMA_NULL_FUNCTION:
return "NULL function";
case PRIMA_PROBLEM_SOLVER_MISMATCH_NONLINEAR_CONSTRAINTS:
return "Nonlinear constraints were provided for an algorithm that cannot handle them";
case PRIMA_PROBLEM_SOLVER_MISMATCH_LINEAR_CONSTRAINTS:
return "Linear constraints were provided for an algorithm that cannot handle them";
case PRIMA_PROBLEM_SOLVER_MISMATCH_BOUNDS:
return "Bounds were provided for an algorithm that cannot handle them";
default:
return "Invalid return code";
}
}


// Functions implemented in Fortran (*_c.f90)
int cobyla_c(const int m_nlcon, const prima_objcon_t calcfc, const void *data, const int n, double x[], double *const f, double *const cstrv, double nlconstr[],
const int m_ineq, const double Aineq[], const double bineq[],
const int m_eq, const double Aeq[], const double beq[],
const double xl[], const double xu[],
const double f0, const double nlconstr0[],
int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int iprint, const double ctol,
const prima_callback_t callback, int *const info);

int bobyqa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f, const double xl[], const double xu[],
int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int npt, const int iprint, const prima_callback_t callback, int *const info);

int newuoa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f,
int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int npt, const int iprint, const prima_callback_t callback, int *const info);

int uobyqa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f,
int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int iprint, const prima_callback_t callback, int *const info);

int lincoa_c(prima_obj_t calfun, const void *data, const int n, double x[], double *const f,
double *const cstrv, const int m_ineq, const double Aineq[], const double bineq[],
const int m_eq, const double Aeq[], const double beq[], const double xl[], const double xu[],
int *const nf, const double rhobeg, const double rhoend, const double ftarget, const int maxfun, const int npt, const int iprint, const double ctol,
const prima_callback_t callback, int *const info);


// The function that does the minimization using a PRIMA solver
prima_rc_t prima_minimize(const prima_algorithm_t algorithm, const prima_problem_t problem, const prima_options_t options, prima_result_t *const result)
{
Expand Down Expand Up @@ -267,9 +257,3 @@ prima_rc_t prima_minimize(const prima_algorithm_t algorithm, const prima_problem

return info;
}

bool prima_is_success(const prima_result_t result)
{
return (result.status == PRIMA_SMALL_TR_RADIUS ||
result.status == PRIMA_FTARGET_ACHIEVED) && (result.cstrv <= sqrt(DBL_EPSILON));
}
10 changes: 6 additions & 4 deletions c/tests/stress.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ static void fun_con(const double x[], double *const f, double constr[], const vo
}

// A function generating a seed that alters weekly
unsigned int get_random_seed(void) {
unsigned int get_random_seed(void)
{
// Set the random seed to year/week
char buf[10] = {0};
time_t t = time(NULL);
struct tm *tmp = localtime(&t);
int rc = strftime(buf, 10, "%y%W", tmp);
struct tm timeinfo;
localtime_s(&timeinfo, &t);
int rc = strftime(buf, 10, "%y%W", &timeinfo);
if (!rc)
return 42;
else
Expand All @@ -87,7 +89,7 @@ int main(int argc, char * argv[])
printf("Debug = %d\n", debug);

unsigned int seed = get_random_seed();
printf("Random seed = %d\n", seed);
printf("Random seed = %u\n", seed);
srand(seed);

// Set up the options
Expand Down

1 comment on commit a1b7f19

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@check-spelling-bot Report

🔴 Please review

See the 📜action log or 📝 job summary for details.

Unrecognized words (1)

timeinfo

Some files were automatically ignored 🙈

These sample patterns would exclude them:

^\Qpython/profiles/bobyqa.txt\E$
^\Qpython/profiles/cobyla.txt\E$
^\Qpython/profiles/lincoa.txt\E$
^\Qpython/profiles/uobyqa_newuoa.txt\E$

You should consider adding them to:

.github/actions/spelling/excludes.txt

File matching is via Perl regular expressions.

To check these files, more of their words need to be in the dictionary than not. You can use patterns.txt to exclude portions, add items to the dictionary (e.g. by adding them to allow.txt), or fix typos.

To accept these unrecognized words as correct and update file exclusions, you could run the following commands

... in a clone of the [email protected]:libprima/prima.git repository
on the c_headers_internal branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/main/apply.pl' |
perl - 'https://github.com/libprima/prima/actions/runs/8892709672/attempts/1'
Warnings (1)

See the 📜action log or 📝 job summary for details.

ℹ️ Warnings Count
ℹ️ noisy-file 4

See ℹ️ Event descriptions for more information.

If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

Please sign in to comment.