提交 f6722958 编写于 作者: J Julien Schueller

Add algo/result<->string converters

上级 35240398
......@@ -72,6 +72,13 @@ You can get a descriptive (null-terminated) string corresponding to a particular
const char *nlopt_algorithm_name(nlopt_algorithm algorithm);
```
You can match a particular algorithm to its string identifier by calling:
```
const char *nlopt_algorithm_to_string(nlopt_algorithm algorithm);
nlopt_algorithm nlopt_algorithm_from_string(const char *name);
```
Objective function
------------------
......@@ -97,6 +104,7 @@ In addition, if the argument `grad` is not `NULL`, then `grad` points to an arra
The `f_data` argument is the same as the one passed to `nlopt_set_min_objective` or `nlopt_set_max_objective`, and may be used to pass any additional data through to the function. (That is, it may be a pointer to some caller-defined data structure/type containing information your function needs, which you convert from `void*` by a typecast.)
Bound constraints
-----------------
......@@ -399,6 +407,15 @@ NLOPT_FORCED_STOP` `=` `-5
Halted because of a [forced termination](#Forced_termination.md): the user called `nlopt_force_stop(opt)` on the optimization’s `nlopt_opt` object `opt` from the user’s objective function or constraints.
You can match a particular result code to its string identifier by calling:
```
const char *nlopt_result_to_string(nlopt_result result);
nlopt_result nlopt_result_from_string(const char *name);
```
Local/subsidiary optimization algorithm
---------------------------------------
......
......@@ -71,7 +71,7 @@ if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp)
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp "${ENUM_LINE}\n")
if (NLOPT_H_LINE MATCHES "NLOPT_NUM_ALGORITHMS")
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp " };\n enum result {\n")
elseif (NLOPT_H_LINE MATCHES "NLOPT_MAXTIME_REACHED")
elseif (NLOPT_H_LINE MATCHES "NLOPT_NUM_RESULTS")
file (APPEND ${CMAKE_CURRENT_BINARY_DIR}/nlopt.hpp " };\n")
endif ()
endforeach ()
......
......@@ -21,6 +21,7 @@
*/
#include "nlopt-internal.h"
#include <string.h>
/*************************************************************************/
......@@ -96,6 +97,114 @@ const char *NLOPT_STDCALL nlopt_algorithm_name(nlopt_algorithm a)
return nlopt_algorithm_names[a];
}
/*************************************************************************/
const char *nlopt_algorithm_to_string(nlopt_algorithm algorithm)
{
switch(algorithm)
{
case NLOPT_GN_DIRECT: return "GN_DIRECT";
case NLOPT_GN_DIRECT_L: return "GN_DIRECT_L";
case NLOPT_GN_DIRECT_L_RAND: return "GN_DIRECT_L_RAND";
case NLOPT_GN_DIRECT_NOSCAL: return "GN_DIRECT_NOSCAL";
case NLOPT_GN_DIRECT_L_NOSCAL: return "GN_DIRECT_L_NOSCAL";
case NLOPT_GN_DIRECT_L_RAND_NOSCAL: return "GN_DIRECT_L_RAND_NOSCAL";
case NLOPT_GN_ORIG_DIRECT: return "GN_ORIG_DIRECT";
case NLOPT_GN_ORIG_DIRECT_L: return "GN_ORIG_DIRECT_L";
case NLOPT_GD_STOGO: return "GD_STOGO";
case NLOPT_GD_STOGO_RAND: return "GD_STOGO_RAND";
case NLOPT_LD_LBFGS_NOCEDAL: return "LD_LBFGS_NOCEDAL";
case NLOPT_LD_LBFGS: return "LD_LBFGS";
case NLOPT_LN_PRAXIS: return "LN_PRAXIS";
case NLOPT_LD_VAR1: return "LD_VAR1";
case NLOPT_LD_VAR2: return "LD_VAR2";
case NLOPT_LD_TNEWTON: return "LD_TNEWTON";
case NLOPT_LD_TNEWTON_RESTART: return "LD_TNEWTON_RESTART";
case NLOPT_LD_TNEWTON_PRECOND: return "LD_TNEWTON_PRECOND";
case NLOPT_LD_TNEWTON_PRECOND_RESTART: return "LD_TNEWTON_PRECOND_RESTART";
case NLOPT_GN_CRS2_LM: return "GN_CRS2_LM";
case NLOPT_GN_MLSL: return "GN_MLSL";
case NLOPT_GD_MLSL: return "GD_MLSL";
case NLOPT_GN_MLSL_LDS: return "GN_MLSL_LDS";
case NLOPT_GD_MLSL_LDS: return "GD_MLSL_LDS";
case NLOPT_LD_MMA: return "LD_MMA";
case NLOPT_LN_COBYLA: return "LN_COBYLA";
case NLOPT_LN_NEWUOA: return "LN_NEWUOA";
case NLOPT_LN_NEWUOA_BOUND: return "LN_NEWUOA_BOUND";
case NLOPT_LN_NELDERMEAD: return "LN_NELDERMEAD";
case NLOPT_LN_SBPLX: return "LN_SBPLX";
case NLOPT_LN_AUGLAG: return "LN_AUGLAG";
case NLOPT_LD_AUGLAG: return "LD_AUGLAG";
case NLOPT_LN_AUGLAG_EQ: return "LN_AUGLAG_EQ";
case NLOPT_LD_AUGLAG_EQ: return "LD_AUGLAG_EQ";
case NLOPT_LN_BOBYQA: return "LN_BOBYQA";
case NLOPT_GN_ISRES: return "GN_ISRES";
case NLOPT_AUGLAG: return "AUGLAG";
case NLOPT_AUGLAG_EQ: return "AUGLAG_EQ";
case NLOPT_G_MLSL: return "G_MLSL";
case NLOPT_G_MLSL_LDS: return "G_MLSL_LDS";
case NLOPT_LD_SLSQP: return "LD_SLSQP";
case NLOPT_LD_CCSAQ: return "LD_CCSAQ";
case NLOPT_GN_ESCH: return "GN_ESCH";
case NLOPT_GN_AGS: return "GN_AGS";
case NLOPT_NUM_ALGORITHMS: return NULL;
}
return NULL;
}
nlopt_algorithm nlopt_algorithm_from_string(const char * name)
{
int i;
if (name == NULL)
return -1;
for (i = 0; i < NLOPT_NUM_ALGORITHMS; ++i)
{
if (strcmp (name, nlopt_algorithm_to_string(i)) == 0)
return i;
}
return -1;
}
/*************************************************************************/
const char *nlopt_result_to_string(nlopt_result result)
{
switch(result)
{
case NLOPT_FAILURE: return "FAILURE";
case NLOPT_INVALID_ARGS: return "INVALID_ARGS";
case NLOPT_OUT_OF_MEMORY: return "OUT_OF_MEMORY";
case NLOPT_ROUNDOFF_LIMITED: return "ROUNDOFF_LIMITED";
case NLOPT_FORCED_STOP: return "FORCED_STOP";
case NLOPT_SUCCESS: return "SUCCESS";
case NLOPT_STOPVAL_REACHED: return "STOPVAL_REACHED";
case NLOPT_FTOL_REACHED: return "FTOL_REACHED";
case NLOPT_XTOL_REACHED: return "XTOL_REACHED";
case NLOPT_MAXEVAL_REACHED: return "MAXEVAL_REACHED";
case NLOPT_MAXTIME_REACHED: return "MAXTIME_REACHED";
case NLOPT_NUM_RESULTS: return NULL;
}
return NULL;
}
nlopt_result nlopt_result_from_string(const char * name)
{
int i;
if (name == NULL)
return -1;
for (i = 0; i < NLOPT_NUM_RESULTS; ++i)
{
if (strcmp(name, nlopt_result_to_string(i)) == 0)
return i;
}
return -1;
}
/*************************************************************************/
/* get thread id, if possible, for use in nlopt_srand_time to ensure that
different threads have a different default seed even if they are called
......
......@@ -218,6 +218,14 @@ namespace nlopt {
if (!o) throw std::bad_alloc();
nlopt_set_munge(o, free_myfunc_data, dup_myfunc_data);
}
opt(const char * algo_str, unsigned n) :
o(nlopt_create(nlopt_algorithm_from_string(algo_str), n)),
xtmp(0), gradtmp(0), gradtmp0(0),
last_result(nlopt::FAILURE), last_optf(HUGE_VAL),
forced_stop_reason(NLOPT_FORCED_STOP) {
if (!o) throw std::bad_alloc();
nlopt_set_munge(o, free_myfunc_data, dup_myfunc_data);
}
opt(const opt& f) : o(nlopt_copy(f.o)),
xtmp(f.xtmp), gradtmp(f.gradtmp), gradtmp0(0),
last_result(f.last_result), last_optf(f.last_optf),
......
......@@ -155,6 +155,10 @@ typedef enum {
NLOPT_EXTERN(const char *) nlopt_algorithm_name(nlopt_algorithm a);
/* nlopt_algorithm enum <-> string conversion */
NLOPT_EXTERN(const char *) nlopt_algorithm_to_string(nlopt_algorithm algorithm);
NLOPT_EXTERN(nlopt_algorithm) nlopt_algorithm_from_string(const char *name);
typedef enum {
NLOPT_FAILURE = -1, /* generic failure code */
NLOPT_INVALID_ARGS = -2,
......@@ -166,9 +170,14 @@ typedef enum {
NLOPT_FTOL_REACHED = 3,
NLOPT_XTOL_REACHED = 4,
NLOPT_MAXEVAL_REACHED = 5,
NLOPT_MAXTIME_REACHED = 6
NLOPT_MAXTIME_REACHED = 6,
NLOPT_NUM_RESULTS /* not a result, just the number of them */
} nlopt_result;
/* nlopt_result enum <-> string conversion */
NLOPT_EXTERN(const char *) nlopt_result_to_string(nlopt_result algorithm);
NLOPT_EXTERN(nlopt_result) nlopt_result_from_string(const char *name);
#define NLOPT_MINF_MAX_REACHED NLOPT_STOPVAL_REACHED
NLOPT_EXTERN(void) nlopt_srand(unsigned long seed);
......@@ -207,6 +216,7 @@ NLOPT_EXTERN(unsigned) nlopt_get_dimension(const nlopt_opt opt);
NLOPT_EXTERN(const char *) nlopt_get_errmsg(nlopt_opt opt);
/* constraints: */
NLOPT_EXTERN(nlopt_result) nlopt_set_lower_bounds(nlopt_opt opt, const double *lb);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册