coco_utilities.c File Reference

Definitions of miscellaneous functions used throughout the COCO framework. More...

#include "coco_platform.h"
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include "coco.h"
#include "coco_internal.h"
#include "coco_string.c"

Functions

const char * coco_set_log_level (const char *log_level)
 Sets the COCO log level to the given value and returns the previous value of the log level. More...
 
Methods regarding file, directory and path manipulations
static void coco_join_path (char *path, const size_t path_max_length,...)
 Creates a platform-dependent path from the given strings. More...
 
static int coco_directory_exists (const char *path)
 Checks if the given directory exists. More...
 
static int coco_file_exists (const char *path)
 Checks if the given file exists. More...
 
static int coco_mkdir (const char *path)
 Calls the right mkdir() method (depending on the platform) with full privileges for the user. If the created directory has not existed before, returns 0, otherwise returns 1. If the directory has not been created, a coco_error is raised. More...
 
static int coco_create_directory (const char *path)
 Creates a directory (possibly having to create nested directories). If the last created directory has not existed before, returns 0, otherwise returns 1. More...
 
static void coco_create_unique_directory (char **path)
 Creates a directory that has not existed before. More...
 
int coco_remove_directory (const char *path)
 Removes the given directory and all its contents. More...
 
int coco_remove_file (const char *file_name)
 
Methods regarding memory allocations
double * coco_allocate_vector (const size_t number_of_elements)
 Safe memory allocation for a vector of doubles that either succeeds or triggers a coco_error. More...
 
static double * coco_allocate_vector_with_value (const size_t number_of_elements, double value)
 Allocates memory for a vector and sets all its elements to value. More...
 
static size_t * coco_allocate_vector_size_t (const size_t number_of_elements)
 Safe memory allocation for a vector with size_t elements that either succeeds or triggers a coco_error. More...
 
static char * coco_allocate_string (const size_t number_of_elements)
 Safe memory allocation for a vector of doubles that either succeeds or triggers a coco_error. More...
 
static double * coco_duplicate_vector (const double *src, const size_t number_of_elements)
 Safe memory allocation for a vector of doubles that either succeeds or triggers a coco_error. More...
 
Methods regarding string options
static coco_option_keys_tcoco_option_keys_allocate (const size_t count, const char **keys)
 Allocates an option keys structure holding the given number of option keys. More...
 
static void coco_option_keys_free (coco_option_keys_t *option_keys)
 Frees the given option keys structure. More...
 
static coco_option_keys_tcoco_option_keys_get_redundant (const coco_option_keys_t *known_option_keys, const coco_option_keys_t *given_option_keys)
 Returns redundant option keys (the ones present in given_option_keys but not in known_option_keys). More...
 
static void coco_option_keys_add (coco_option_keys_t **basic_option_keys, const coco_option_keys_t *additional_option_keys)
 Adds additional option keys to the given basic option keys (changes the basic keys). More...
 
static coco_option_keys_tcoco_option_keys (const char *option_string)
 Creates an instance of option keys from the given string of options containing keys and values separated by colons. More...
 
static char * coco_option_keys_get_output_string (const coco_option_keys_t *option_keys, const char *info_string)
 Creates and returns a string containing the info_string and all keys from option_keys. More...
 
static int coco_options_read (const char *options, const char *name, const char *format, void *pointer)
 Parses options in the form "name1: value1 name2: value2". More...
 
static int coco_options_read_int (const char *options, const char *name, int *pointer)
 Reads an integer from options using the form "name1: value1 name2: value2". More...
 
static int coco_options_read_size_t (const char *options, const char *name, size_t *pointer)
 Reads a size_t from options using the form "name1: value1 name2: value2". More...
 
static int coco_options_read_double (const char *options, const char *name, double *pointer)
 Reads a double value from options using the form "name1: value1 name2: value2". More...
 
static int coco_options_read_string (const char *options, const char *name, char *pointer)
 Reads a string from options using the form "name1: value1 name2: value2". More...
 
static int coco_options_read_values (const char *options, const char *name, char *pointer)
 Reads (possibly delimited) values from options using the form "name1: value1,value2,value3 name2: value4", i.e. reads all characters from the corresponding name up to the next alphabetic character or end of string, ignoring white-space characters. More...
 
Methods implementing functions on double values not contained in C89 standard
static double coco_double_round (const double number)
 Rounds the given double to the nearest integer. More...
 
static double coco_double_max (const double a, const double b)
 Returns the maximum of a and b. More...
 
static double coco_double_min (const double a, const double b)
 Returns the minimum of a and b. More...
 
static size_t coco_double_to_size_t (const double number)
 Performs a "safer" double to size_t conversion. More...
 
static int coco_double_to_int (const double number)
 Rounds the given double to the nearest integer (returns the number in int type) More...
 
static int coco_double_almost_equal (const double a, const double b, const double precision)
 Returns 1 if |a - b| < precision and 0 otherwise. More...
 
Methods handling NAN and INFINITY
static int coco_is_nan (const double x)
 Returns 1 if x is NAN and 0 otherwise. More...
 
static int coco_vector_contains_nan (const double *x, const size_t dim)
 Returns 1 if the input vector of dimension dim contains any NAN values and 0 otherwise. More...
 
static void coco_vector_set_to_nan (double *y, const size_t dim)
 Sets all dim values of y to NAN. More...
 
static int coco_is_inf (const double x)
 Returns 1 if x is INFINITY and 0 otherwise. More...
 
static int coco_vector_isfinite (const double *x, const size_t dim)
 Returns 1 if the input vector of dimension dim contains no NaN of inf values, and 0 otherwise. More...
 
static int coco_is_feasible (coco_problem_t *problem, const double *x, double *constraint_values)
 Returns 1 if the point x is feasible, and 0 otherwise. More...
 
Miscellaneous methods
static char * coco_current_time_get_string (void)
 Returns the current time as a string. More...
 
static size_t coco_count_numbers (const size_t *numbers, const size_t max_count, const char *name)
 Returns the number of positive numbers pointed to by numbers (the count stops when the first 0 is encountered of max_count numbers have been read). More...
 
static double coco_vector_scale (double *x, size_t dimension, double nom, double denom)
 multiply each componenent by nom/denom or by nom if denom == 0. More...
 
static double coco_vector_norm (const double *x, size_t dimension)
 return norm of vector x. More...
 
static int coco_is_orthogonal (const double *M, const size_t nb_rows, const size_t nb_columns)
 Checks if a given matrix M is orthogonal by (partially) computing M * M^T. If M is a square matrix and M * M^T is close enough to the identity matrix (up to a chosen precision), the function returns 1. Otherwise, it returns 0. The matrix M must be represented as an array of doubles. More...
 
static int coco_vector_is_zero (const double *x, const size_t dim)
 Returns 1 if the input vector x is (close to) zero and 0 otherwise. More...
 

Variables

static const double chosen_precision = 1e-9
 Sets the constant chosen_precision to 1e-9. More...
 
static coco_log_level_type_e coco_log_level = COCO_INFO
 Initializes the logging level to COCO_INFO. More...
 

Detailed Description

Definitions of miscellaneous functions used throughout the COCO framework.

Function Documentation

static char* coco_allocate_string ( const size_t  number_of_elements)
static

Safe memory allocation for a vector of doubles that either succeeds or triggers a coco_error.

double* coco_allocate_vector ( const size_t  number_of_elements)

Safe memory allocation for a vector of doubles that either succeeds or triggers a coco_error.

static size_t* coco_allocate_vector_size_t ( const size_t  number_of_elements)
static

Safe memory allocation for a vector with size_t elements that either succeeds or triggers a coco_error.

static double* coco_allocate_vector_with_value ( const size_t  number_of_elements,
double  value 
)
static

Allocates memory for a vector and sets all its elements to value.

static size_t coco_count_numbers ( const size_t *  numbers,
const size_t  max_count,
const char *  name 
)
static

Returns the number of positive numbers pointed to by numbers (the count stops when the first 0 is encountered of max_count numbers have been read).

If there are more than max_count numbers, a coco_error is raised. The name argument is used only to provide more informative output in case of any problems.

static int coco_create_directory ( const char *  path)
static

Creates a directory (possibly having to create nested directories). If the last created directory has not existed before, returns 0, otherwise returns 1.

Parameters
pathThe directory path.
Returns
0 if the created directory has not existed before and 1 otherwise.
static void coco_create_unique_directory ( char **  path)
static

Creates a directory that has not existed before.

If the given path does not yet exit, it is left as is, otherwise it is changed(!) by appending a number to it. If path already exists, path-001 will be tried. If this one exists as well, path-002 will be tried, and so on. If path-999 exists as well, an error is raised.

static char* coco_current_time_get_string ( void  )
static

Returns the current time as a string.

The caller is responsible for freeing the allocated memory using coco_free_memory().

static int coco_directory_exists ( const char *  path)
static

Checks if the given directory exists.

Note
Should work cross-platform.
Parameters
pathThe given path.
Returns
1 if the path exists and corresponds to a directory and 0 otherwise.
static int coco_double_almost_equal ( const double  a,
const double  b,
const double  precision 
)
static

Returns 1 if |a - b| < precision and 0 otherwise.

static double coco_double_max ( const double  a,
const double  b 
)
static

Returns the maximum of a and b.

static double coco_double_min ( const double  a,
const double  b 
)
static

Returns the minimum of a and b.

static double coco_double_round ( const double  number)
static

Rounds the given double to the nearest integer.

static int coco_double_to_int ( const double  number)
static

Rounds the given double to the nearest integer (returns the number in int type)

static size_t coco_double_to_size_t ( const double  number)
static

Performs a "safer" double to size_t conversion.

TODO: This method could (should?) check for overflow when casting (similarly as is done in coco_double_to_int()).

static double* coco_duplicate_vector ( const double *  src,
const size_t  number_of_elements 
)
static

Safe memory allocation for a vector of doubles that either succeeds or triggers a coco_error.

static int coco_file_exists ( const char *  path)
static

Checks if the given file exists.

Note
Should work cross-platform.
Parameters
pathThe given path.
Returns
1 if the path exists and corresponds to a file and 0 otherwise.
static int coco_is_feasible ( coco_problem_t problem,
const double *  x,
double *  constraint_values 
)
static

Returns 1 if the point x is feasible, and 0 otherwise.

Allows constraint_values == NULL, otherwise constraint_values must be a valid double* pointer and contains the g-values of x on "return".

Any point x containing NaN or inf values is considered infeasible.

This function is (and should be) used internally only, and does not increase the counter of constraint function evaluations.

Parameters
problemThe given COCO problem.
xDecision vector.
constraint_valuesVector of contraints values resulting from evaluation.
static int coco_is_inf ( const double  x)
static

Returns 1 if x is INFINITY and 0 otherwise.

static int coco_is_nan ( const double  x)
static

Returns 1 if x is NAN and 0 otherwise.

static int coco_is_orthogonal ( const double *  M,
const size_t  nb_rows,
const size_t  nb_columns 
)
static

Checks if a given matrix M is orthogonal by (partially) computing M * M^T. If M is a square matrix and M * M^T is close enough to the identity matrix (up to a chosen precision), the function returns 1. Otherwise, it returns 0. The matrix M must be represented as an array of doubles.

static void coco_join_path ( char *  path,
const size_t  path_max_length,
  ... 
)
static

Creates a platform-dependent path from the given strings.

Note
The last argument must be NULL.
The first parameter must be able to accommodate path_max_length characters and the length of the joined path must not exceed path_max_length characters.
Should work cross-platform.

Usage examples:

  • coco_join_path(base_path, 100, folder1, folder2, folder3, NULL) creates base_path/folder1/folder2/folder3
  • coco_join_path(base_path, 100, folder1, file_name, NULL) creates base_path/folder1/file_name
    Parameters
    pathThe base path; it's also where the joined path is stored to.
    path_max_lengthThe maximum length of the path.
    ...Additional strings, must end with NULL
static int coco_mkdir ( const char *  path)
static

Calls the right mkdir() method (depending on the platform) with full privileges for the user. If the created directory has not existed before, returns 0, otherwise returns 1. If the directory has not been created, a coco_error is raised.

Parameters
pathThe directory path.
Returns
0 if the created directory has not existed before and 1 otherwise.
static coco_option_keys_t* coco_option_keys ( const char *  option_string)
static

Creates an instance of option keys from the given string of options containing keys and values separated by colons.

Note
Relies heavily on the "key: value" format and might fail if the number of colons doesn't match the number of keys.
static void coco_option_keys_add ( coco_option_keys_t **  basic_option_keys,
const coco_option_keys_t additional_option_keys 
)
static

Adds additional option keys to the given basic option keys (changes the basic keys).

static coco_option_keys_t* coco_option_keys_allocate ( const size_t  count,
const char **  keys 
)
static

Allocates an option keys structure holding the given number of option keys.

static void coco_option_keys_free ( coco_option_keys_t option_keys)
static

Frees the given option keys structure.

static char* coco_option_keys_get_output_string ( const coco_option_keys_t option_keys,
const char *  info_string 
)
static

Creates and returns a string containing the info_string and all keys from option_keys.

Can be used to output information about the given option_keys.

static coco_option_keys_t* coco_option_keys_get_redundant ( const coco_option_keys_t known_option_keys,
const coco_option_keys_t given_option_keys 
)
static

Returns redundant option keys (the ones present in given_option_keys but not in known_option_keys).

static int coco_options_read ( const char *  options,
const char *  name,
const char *  format,
void *  pointer 
)
static

Parses options in the form "name1: value1 name2: value2".

Formatting requirements:

  • name and value need to be separated by a colon (spaces are optional)
  • value needs to be a single string (no spaces allowed)
Returns
The number of successful assignments.
static int coco_options_read_double ( const char *  options,
const char *  name,
double *  pointer 
)
static

Reads a double value from options using the form "name1: value1 name2: value2".

Formatting requirements:

  • name and value need to be separated by a colon (spaces are optional)
  • the value corresponding to the given name needs to be a double
Returns
The number of successful assignments.
static int coco_options_read_int ( const char *  options,
const char *  name,
int *  pointer 
)
static

Reads an integer from options using the form "name1: value1 name2: value2".

Formatting requirements:

  • name and value need to be separated by a colon (spaces are optional)
  • the value corresponding to the given name needs to be an integer
Returns
The number of successful assignments.
static int coco_options_read_size_t ( const char *  options,
const char *  name,
size_t *  pointer 
)
static

Reads a size_t from options using the form "name1: value1 name2: value2".

Formatting requirements:

  • name and value need to be separated by a colon (spaces are optional)
  • the value corresponding to the given name needs to be a size_t
Returns
The number of successful assignments.
static int coco_options_read_string ( const char *  options,
const char *  name,
char *  pointer 
)
static

Reads a string from options using the form "name1: value1 name2: value2".

Formatting requirements:

  • name and value need to be separated by a colon (spaces are optional)
  • the value corresponding to the given name needs to be a string - either a single word or multiple words in double quotes
Returns
The number of successful assignments.
static int coco_options_read_values ( const char *  options,
const char *  name,
char *  pointer 
)
static

Reads (possibly delimited) values from options using the form "name1: value1,value2,value3 name2: value4", i.e. reads all characters from the corresponding name up to the next alphabetic character or end of string, ignoring white-space characters.

Formatting requirements:

  • names have to start with alphabetic characters
  • values cannot include alphabetic characters
  • name and value need to be separated by a colon (spaces are optional)
Returns
The number of successful assignments.
int coco_remove_directory ( const char *  path)

Removes the given directory and all its contents.

The method should work across different platforms/compilers.

The path to the directory

Returns
0 on successful completion, and -1 on error.
int coco_remove_file ( const char *  file_name)

The method should work across different platforms/compilers.

The path to the file

Returns
0 on successful completion, and -1 on error.
const char* coco_set_log_level ( const char *  log_level)

Sets the COCO log level to the given value and returns the previous value of the log level.

Parameters
log_levelDenotes the level of information given to the user through the standard output and error streams. Can take on the values:
  • "error" (only error messages are output),
  • "warning" (only error and warning messages are output),
  • "info" (only error, warning and info messages are output) and
  • "debug" (all messages are output).
  • "" does not set a new value The default value is info.
Returns
The previous coco_log_level value as an immutable string.
static int coco_vector_contains_nan ( const double *  x,
const size_t  dim 
)
static

Returns 1 if the input vector of dimension dim contains any NAN values and 0 otherwise.

static int coco_vector_is_zero ( const double *  x,
const size_t  dim 
)
static

Returns 1 if the input vector x is (close to) zero and 0 otherwise.

static int coco_vector_isfinite ( const double *  x,
const size_t  dim 
)
static

Returns 1 if the input vector of dimension dim contains no NaN of inf values, and 0 otherwise.

static double coco_vector_norm ( const double *  x,
size_t  dimension 
)
static

return norm of vector x.

static double coco_vector_scale ( double *  x,
size_t  dimension,
double  nom,
double  denom 
)
static

multiply each componenent by nom/denom or by nom if denom == 0.

return used scaling factor, usually nom/denom.

Example: coco_vector_scale(x, dimension, 1, coco_vector_norm(x, dimension));

static void coco_vector_set_to_nan ( double *  y,
const size_t  dim 
)
static

Sets all dim values of y to NAN.

Variable Documentation

const double chosen_precision = 1e-9
static

Sets the constant chosen_precision to 1e-9.

coco_log_level_type_e coco_log_level = COCO_INFO
static

Initializes the logging level to COCO_INFO.