chiark / gitweb /
cmake
[nlopt.git] / src / api / general.c
1 /* Copyright (c) 2007-2014 Massachusetts Institute of Technology
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be
12  * included in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22
23 #include "nlopt-internal.h"
24
25 /*************************************************************************/
26
27 void NLOPT_STDCALL nlopt_version(int *major, int *minor, int *bugfix)
28 {
29     *major = MAJOR_VERSION;
30     *minor = MINOR_VERSION;
31     *bugfix = BUGFIX_VERSION;
32 }
33
34 /*************************************************************************/
35
36 static const char nlopt_algorithm_names[NLOPT_NUM_ALGORITHMS][256] = {
37     "DIRECT (global, no-derivative)",
38     "DIRECT-L (global, no-derivative)",
39     "Randomized DIRECT-L (global, no-derivative)",
40     "Unscaled DIRECT (global, no-derivative)",
41     "Unscaled DIRECT-L (global, no-derivative)",
42     "Unscaled Randomized DIRECT-L (global, no-derivative)",
43     "Original DIRECT version (global, no-derivative)",
44     "Original DIRECT-L version (global, no-derivative)",
45 #ifdef NLOPT_CXX
46     "StoGO (global, derivative-based)",
47     "StoGO with randomized search (global, derivative-based)",
48 #else
49     "StoGO (NOT COMPILED)",
50     "StoGO randomized (NOT COMPILED)",
51 #endif
52     "original L-BFGS code by Nocedal et al. (NOT COMPILED)",
53     "Limited-memory BFGS (L-BFGS) (local, derivative-based)",
54     "Principal-axis, praxis (local, no-derivative)",
55     "Limited-memory variable-metric, rank 1 (local, derivative-based)",
56     "Limited-memory variable-metric, rank 2 (local, derivative-based)",
57     "Truncated Newton (local, derivative-based)",
58     "Truncated Newton with restarting (local, derivative-based)",
59     "Preconditioned truncated Newton (local, derivative-based)",
60     "Preconditioned truncated Newton with restarting (local, derivative-based)",
61     "Controlled random search (CRS2) with local mutation (global, no-derivative)",
62     "Multi-level single-linkage (MLSL), random (global, no-derivative)",
63     "Multi-level single-linkage (MLSL), random (global, derivative)",
64     "Multi-level single-linkage (MLSL), quasi-random (global, no-derivative)",
65     "Multi-level single-linkage (MLSL), quasi-random (global, derivative)",
66     "Method of Moving Asymptotes (MMA) (local, derivative)",
67     "COBYLA (Constrained Optimization BY Linear Approximations) (local, no-derivative)",
68     "NEWUOA unconstrained optimization via quadratic models (local, no-derivative)",
69     "Bound-constrained optimization via NEWUOA-based quadratic models (local, no-derivative)",
70     "Nelder-Mead simplex algorithm (local, no-derivative)",
71     "Sbplx variant of Nelder-Mead (re-implementation of Rowan's Subplex) (local, no-derivative)",
72     "Augmented Lagrangian method (local, no-derivative)",
73     "Augmented Lagrangian method (local, derivative)",
74     "Augmented Lagrangian method for equality constraints (local, no-derivative)",
75     "Augmented Lagrangian method for equality constraints (local, derivative)",
76     "BOBYQA bound-constrained optimization via quadratic models (local, no-derivative)",
77     "ISRES evolutionary constrained optimization (global, no-derivative)",
78     "Augmented Lagrangian method (needs sub-algorithm)",
79     "Augmented Lagrangian method for equality constraints (needs sub-algorithm)",
80     "Multi-level single-linkage (MLSL), random (global, needs sub-algorithm)",
81     "Multi-level single-linkage (MLSL), quasi-random (global, needs sub-algorithm)",
82     "Sequential Quadratic Programming (SQP) (local, derivative)",
83     "CCSA (Conservative Convex Separable Approximations) with simple quadratic approximations (local, derivative)",
84     "ESCH evolutionary strategy",
85 #ifdef NLOPT_CXX11
86     "AGS (global, no-derivative)"
87 #else
88     "AGS (NOT COMPILED)"
89 #endif
90 };
91
92 const char *NLOPT_STDCALL nlopt_algorithm_name(nlopt_algorithm a)
93 {
94     if (((int) a) < 0 || a >= NLOPT_NUM_ALGORITHMS)
95         return "UNKNOWN";
96     return nlopt_algorithm_names[a];
97 }
98
99 /*************************************************************************/
100 /* get thread id, if possible, for use in nlopt_srand_time to ensure that
101    different threads have a different default seed even if they are called
102    simultaneously */
103
104 #if defined(_WIN32) || defined(__WIN32__)
105 #  include <windows.h>
106 #  define my_gettid GetCurrentThreadId
107 #elif defined(HAVE_GETTID_SYSCALL)
108 #  include <unistd.h>
109 #  include <sys/syscall.h>
110 #  define my_gettid() syscall(SYS_gettid)
111 #elif defined(HAVE_GETPID)
112 #  include <sys/types.h>
113 #  include <unistd.h>
114 #  define my_gettid getpid
115 #else
116 #  define my_gettid() (0)
117 #endif
118
119 /*************************************************************************/
120
121 static THREADLOCAL int nlopt_srand_called = 0;
122 void NLOPT_STDCALL nlopt_srand(unsigned long seed)
123 {
124     nlopt_srand_called = 1;
125     nlopt_init_genrand(seed);
126 }
127
128 void NLOPT_STDCALL nlopt_srand_time(void)
129 {
130     nlopt_srand(nlopt_time_seed() + (unsigned long) my_gettid() * 314159);
131 }
132
133 void nlopt_srand_time_default(void)
134 {
135     if (!nlopt_srand_called)
136         nlopt_srand_time();
137 }
138
139 /*************************************************************************/