chiark / gitweb /
Use trusty
[nlopt.git] / api / options.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 <stdio.h>
24 #include <stdlib.h>
25 #include <math.h>
26 #include <string.h>
27 #include <float.h>
28 #include <stdarg.h>
29
30 #include "nlopt-internal.h"
31
32 #define ERR(err, opt, msg) (nlopt_set_errmsg(opt, msg) ? err : err)
33
34 /*************************************************************************/
35
36 void NLOPT_STDCALL nlopt_destroy(nlopt_opt opt)
37 {
38      if (opt) {
39           unsigned i;
40           if (opt->munge_on_destroy) {
41                nlopt_munge munge = opt->munge_on_destroy;
42                munge(opt->f_data);
43                for (i = 0; i < opt->m; ++i)
44                     munge(opt->fc[i].f_data);
45                for (i = 0; i < opt->p; ++i)
46                     munge(opt->h[i].f_data);
47           }
48           for (i = 0; i < opt->m; ++i)
49                free(opt->fc[i].tol);
50           for (i = 0; i < opt->p; ++i)
51                free(opt->h[i].tol);
52           free(opt->lb); free(opt->ub);
53           free(opt->xtol_abs);
54           free(opt->fc);
55           free(opt->h);
56           nlopt_destroy(opt->local_opt);
57           free(opt->dx);
58           free(opt->work);
59           free(opt->errmsg);
60           free(opt);
61      }
62 }
63
64 nlopt_opt NLOPT_STDCALL nlopt_create(nlopt_algorithm algorithm, unsigned n)
65 {
66      nlopt_opt opt;
67
68      if (((int) algorithm) < 0 || algorithm >= NLOPT_NUM_ALGORITHMS)
69           return NULL;
70
71      opt = (nlopt_opt) malloc(sizeof(struct nlopt_opt_s));
72      if (opt) {
73           opt->algorithm = algorithm;
74           opt->n = n;
75           opt->f = NULL; opt->f_data = NULL; opt->pre = NULL;
76           opt->maximize = 0;
77           opt->munge_on_destroy = opt->munge_on_copy = NULL;
78
79           opt->lb = opt->ub = NULL;
80           opt->m = opt->m_alloc = 0;
81           opt->fc = NULL;
82           opt->p = opt->p_alloc = 0;
83           opt->h = NULL;
84
85           opt->stopval = -HUGE_VAL;
86           opt->ftol_rel = opt->ftol_abs = 0;
87           opt->xtol_rel = 0; opt->xtol_abs = NULL;
88           opt->maxeval = 0;
89           opt->maxtime = 0;
90           opt->force_stop = 0;
91           opt->force_stop_child = NULL;
92
93           opt->local_opt = NULL;
94           opt->stochastic_population = 0;
95           opt->vector_storage = 0;
96           opt->dx = NULL;
97           opt->work = NULL;
98           opt->errmsg = NULL;
99
100           if (n > 0) {
101                opt->lb = (double *) calloc(n, sizeof(double));
102                if (!opt->lb) goto oom;
103                opt->ub = (double *) calloc(n, sizeof(double));
104                if (!opt->ub) goto oom;
105                opt->xtol_abs = (double *) calloc(n, sizeof(double));
106                if (!opt->xtol_abs) goto oom;
107                nlopt_set_lower_bounds1(opt, -HUGE_VAL);
108                nlopt_set_upper_bounds1(opt, +HUGE_VAL);
109                nlopt_set_xtol_abs1(opt, 0.0);
110           }
111      }
112
113      return opt;
114
115 oom:
116      nlopt_destroy(opt);
117      return NULL;
118 }
119
120 nlopt_opt NLOPT_STDCALL nlopt_copy(const nlopt_opt opt)
121 {
122      nlopt_opt nopt = NULL;
123      unsigned i;
124      if (opt) {
125           nlopt_munge munge;
126           nopt = (nlopt_opt) malloc(sizeof(struct nlopt_opt_s));
127           *nopt = *opt;
128           nopt->lb = nopt->ub = nopt->xtol_abs = NULL;
129           nopt->fc = nopt->h = NULL;
130           nopt->m_alloc = nopt->p_alloc = 0;
131           nopt->local_opt = NULL;
132           nopt->dx = NULL;
133           nopt->work = NULL;
134           nopt->errmsg = NULL;
135           nopt->force_stop_child = NULL;
136
137           munge = nopt->munge_on_copy;
138           if (munge && nopt->f_data)
139                if (!(nopt->f_data = munge(nopt->f_data))) goto oom;
140
141           if (opt->n > 0) {
142                nopt->lb = (double *) malloc(sizeof(double) * (opt->n));
143                if (!opt->lb) goto oom;
144                nopt->ub = (double *) malloc(sizeof(double) * (opt->n));
145                if (!opt->ub) goto oom;
146                nopt->xtol_abs = (double *) malloc(sizeof(double) * (opt->n));
147                if (!opt->xtol_abs) goto oom;
148                
149                memcpy(nopt->lb, opt->lb, sizeof(double) * (opt->n));
150                memcpy(nopt->ub, opt->ub, sizeof(double) * (opt->n));
151                memcpy(nopt->xtol_abs, opt->xtol_abs, sizeof(double) * (opt->n));
152           }
153
154           if (opt->m) {
155                nopt->m_alloc = opt->m;
156                nopt->fc = (nlopt_constraint *) malloc(sizeof(nlopt_constraint)
157                                                       * (opt->m));
158                if (!nopt->fc) goto oom;
159                memcpy(nopt->fc, opt->fc, sizeof(nlopt_constraint) * (opt->m));
160                for (i = 0; i < opt->m; ++i) nopt->fc[i].tol = NULL;
161                if (munge)
162                     for (i = 0; i < opt->m; ++i)
163                          if (nopt->fc[i].f_data &&
164                              !(nopt->fc[i].f_data
165                                = munge(nopt->fc[i].f_data)))
166                               goto oom;
167                for (i = 0; i < opt->m; ++i)
168                     if (opt->fc[i].tol) {
169                          nopt->fc[i].tol = (double *) malloc(sizeof(double)
170                                                              * nopt->fc[i].m);
171                          if (!nopt->fc[i].tol) goto oom;
172                          memcpy(nopt->fc[i].tol, opt->fc[i].tol,
173                                 sizeof(double) * nopt->fc[i].m);
174                     }
175           }
176
177           if (opt->p) {
178                nopt->p_alloc = opt->p;
179                nopt->h = (nlopt_constraint *) malloc(sizeof(nlopt_constraint)
180                                                      * (opt->p));
181                if (!nopt->h) goto oom;
182                memcpy(nopt->h, opt->h, sizeof(nlopt_constraint) * (opt->p));
183                for (i = 0; i < opt->p; ++i) nopt->h[i].tol = NULL;
184                if (munge)
185                     for (i = 0; i < opt->p; ++i)
186                          if (nopt->h[i].f_data &&
187                              !(nopt->h[i].f_data
188                                = munge(nopt->h[i].f_data)))
189                               goto oom;
190                for (i = 0; i < opt->p; ++i)
191                     if (opt->h[i].tol) {
192                          nopt->h[i].tol = (double *) malloc(sizeof(double)
193                                                              * nopt->h[i].m);
194                          if (!nopt->h[i].tol) goto oom;
195                          memcpy(nopt->h[i].tol, opt->h[i].tol,
196                                 sizeof(double) * nopt->h[i].m);
197                     }
198           }
199
200           if (opt->local_opt) {
201                nopt->local_opt = nlopt_copy(opt->local_opt);
202                if (!nopt->local_opt) goto oom;
203           }
204
205           if (opt->dx) {
206                nopt->dx = (double *) malloc(sizeof(double) * (opt->n));
207                if (!nopt->dx) goto oom;
208                memcpy(nopt->dx, opt->dx, sizeof(double) * (opt->n));
209           }
210      }
211      return nopt;
212
213 oom:
214      nopt->munge_on_destroy = NULL; /* better to leak mem than crash */
215      nlopt_destroy(nopt);
216      return NULL;
217 }
218
219 /*************************************************************************/
220
221 nlopt_result NLOPT_STDCALL nlopt_set_precond_min_objective(nlopt_opt opt,
222                                                            nlopt_func f, 
223                                                            nlopt_precond pre,
224                                                            void *f_data)
225 {
226      if (opt) {
227           nlopt_unset_errmsg(opt);
228           if (opt->munge_on_destroy) opt->munge_on_destroy(opt->f_data);
229           opt->f = f; opt->f_data = f_data; opt->pre = pre;
230           opt->maximize = 0;
231           if (nlopt_isinf(opt->stopval) && opt->stopval > 0)
232                opt->stopval = -HUGE_VAL; /* switch default from max to min */
233           return NLOPT_SUCCESS;
234      }
235      return NLOPT_INVALID_ARGS;
236 }
237
238 nlopt_result NLOPT_STDCALL nlopt_set_min_objective(nlopt_opt opt,
239                                                    nlopt_func f, void *f_data)
240 {
241      return nlopt_set_precond_min_objective(opt, f, NULL, f_data);
242 }
243
244 nlopt_result NLOPT_STDCALL nlopt_set_precond_max_objective(nlopt_opt opt, 
245                                                            nlopt_func f, 
246                                                            nlopt_precond pre,
247                                                            void *f_data)
248 {
249      if (opt) {
250           nlopt_unset_errmsg(opt);
251           if (opt->munge_on_destroy) opt->munge_on_destroy(opt->f_data);
252           opt->f = f; opt->f_data = f_data; opt->pre = pre;
253           opt->maximize = 1;
254           if (nlopt_isinf(opt->stopval) && opt->stopval < 0)
255                opt->stopval = +HUGE_VAL; /* switch default from min to max */
256           return NLOPT_SUCCESS;
257      }
258      return NLOPT_INVALID_ARGS;
259 }
260
261 nlopt_result NLOPT_STDCALL nlopt_set_max_objective(nlopt_opt opt,
262                                                    nlopt_func f, void *f_data)
263 {
264      return nlopt_set_precond_max_objective(opt, f, NULL, f_data);
265 }
266
267 /*************************************************************************/
268
269 nlopt_result
270 NLOPT_STDCALL nlopt_set_lower_bounds(nlopt_opt opt, const double *lb)
271 {
272      nlopt_unset_errmsg(opt);
273      if (opt && (opt->n == 0 || lb)) {
274           unsigned int i;
275           memcpy(opt->lb, lb, sizeof(double) * (opt->n));
276           for (i = 0; i < opt->n; ++i)
277               if (opt->lb[i] < opt->ub[i] && nlopt_istiny(opt->ub[i] - opt->lb[i]))
278                   opt->lb[i] = opt->ub[i];
279           return NLOPT_SUCCESS;
280      }
281      return NLOPT_INVALID_ARGS;
282 }
283
284 nlopt_result
285 NLOPT_STDCALL nlopt_set_lower_bounds1(nlopt_opt opt, double lb)
286 {
287      nlopt_unset_errmsg(opt);
288      if (opt) {
289           unsigned i;
290           for (i = 0; i < opt->n; ++i) {
291               opt->lb[i] = lb;
292               if (opt->lb[i] < opt->ub[i] && nlopt_istiny(opt->ub[i] - opt->lb[i]))
293                   opt->lb[i] = opt->ub[i];
294           }
295           return NLOPT_SUCCESS;
296      }
297      return NLOPT_INVALID_ARGS;
298 }
299
300 nlopt_result
301 NLOPT_STDCALL nlopt_get_lower_bounds(const nlopt_opt opt, double *lb)
302 {
303      nlopt_unset_errmsg(opt);
304      if (opt && (opt->n == 0 || lb)) {
305           memcpy(lb, opt->lb, sizeof(double) * (opt->n));
306           return NLOPT_SUCCESS;
307      }
308      return NLOPT_INVALID_ARGS;
309 }
310
311 nlopt_result
312 NLOPT_STDCALL nlopt_set_upper_bounds(nlopt_opt opt, const double *ub)
313 {
314      nlopt_unset_errmsg(opt);
315      if (opt && (opt->n == 0 || ub)) {
316           unsigned int i;
317           memcpy(opt->ub, ub, sizeof(double) * (opt->n));
318           for (i = 0; i < opt->n; ++i)
319               if (opt->lb[i] < opt->ub[i] && nlopt_istiny(opt->ub[i] - opt->lb[i]))
320                   opt->ub[i] = opt->lb[i];
321           return NLOPT_SUCCESS;
322      }
323      return NLOPT_INVALID_ARGS;
324 }
325
326 nlopt_result
327 NLOPT_STDCALL nlopt_set_upper_bounds1(nlopt_opt opt, double ub)
328 {
329      nlopt_unset_errmsg(opt);
330      if (opt) {
331           unsigned i;
332           for (i = 0; i < opt->n; ++i) {
333               opt->ub[i] = ub;
334               if (opt->lb[i] < opt->ub[i] && nlopt_istiny(opt->ub[i] - opt->lb[i]))
335                   opt->ub[i] = opt->lb[i];
336           }
337           return NLOPT_SUCCESS;
338      }
339      return NLOPT_INVALID_ARGS;
340 }
341
342 nlopt_result
343 NLOPT_STDCALL nlopt_get_upper_bounds(const nlopt_opt opt, double *ub)
344 {
345      nlopt_unset_errmsg(opt);
346      if (opt && (opt->n == 0 || ub)) {
347           memcpy(ub, opt->ub, sizeof(double) * (opt->n));
348           return NLOPT_SUCCESS;
349      }
350      return NLOPT_INVALID_ARGS;
351 }
352
353 /*************************************************************************/
354
355 #define AUGLAG_ALG(a) ((a) == NLOPT_AUGLAG ||           \
356                        (a) == NLOPT_AUGLAG_EQ ||        \
357                        (a) == NLOPT_LN_AUGLAG ||        \
358                        (a) == NLOPT_LN_AUGLAG_EQ ||     \
359                        (a) == NLOPT_LD_AUGLAG ||        \
360                        (a) == NLOPT_LD_AUGLAG_EQ)
361
362 nlopt_result
363 NLOPT_STDCALL nlopt_remove_inequality_constraints(nlopt_opt opt)
364 {
365      unsigned i;
366      nlopt_unset_errmsg(opt);
367      if (!opt) return NLOPT_INVALID_ARGS;
368      if (opt->munge_on_destroy) {
369           nlopt_munge munge = opt->munge_on_destroy;
370           for (i = 0; i < opt->m; ++i)
371                munge(opt->fc[i].f_data);
372      }
373      for (i = 0; i < opt->m; ++i)
374           free(opt->fc[i].tol);
375      free(opt->fc);
376      opt->fc = NULL;
377      opt->m = opt->m_alloc = 0;
378      return NLOPT_SUCCESS;
379 }
380
381 static nlopt_result add_constraint(nlopt_opt opt,
382                                    unsigned *m, unsigned *m_alloc,
383                                    nlopt_constraint **c,
384                                    unsigned fm, nlopt_func fc, nlopt_mfunc mfc,
385                                    nlopt_precond pre,
386                                    void *fc_data,
387                                    const double *tol)
388 {
389      double *tolcopy;
390      unsigned i;
391
392      if ((fc && mfc) || (fc && fm != 1) || (!fc && !mfc))
393           return NLOPT_INVALID_ARGS;
394      if (tol) 
395          for (i = 0; i < fm; ++i) if (tol[i] < 0)
396              return ERR(NLOPT_INVALID_ARGS, opt, "negative constraint tolerance");
397
398      tolcopy = (double *) malloc(sizeof(double) * fm);
399      if (fm && !tolcopy) return NLOPT_OUT_OF_MEMORY;
400      if (tol)
401           memcpy(tolcopy, tol, sizeof(double) * fm);
402      else
403           for (i = 0; i < fm; ++i) tolcopy[i] = 0;
404
405      *m += 1;
406      if (*m > *m_alloc) {
407           /* allocate by repeated doubling so that 
408              we end up with O(log m) mallocs rather than O(m). */
409           *m_alloc = 2 * (*m);
410           *c = (nlopt_constraint *) realloc(*c,
411                                             sizeof(nlopt_constraint)
412                                             * (*m_alloc));
413           if (!*c) {
414                *m_alloc = *m = 0;
415                free(tolcopy);
416                return NLOPT_OUT_OF_MEMORY;
417           }
418      }
419      
420      (*c)[*m - 1].m = fm;
421      (*c)[*m - 1].f = fc;
422      (*c)[*m - 1].pre = pre;
423      (*c)[*m - 1].mf = mfc;
424      (*c)[*m - 1].f_data = fc_data;
425      (*c)[*m - 1].tol = tolcopy;
426      return NLOPT_SUCCESS;
427 }
428
429 static int inequality_ok(nlopt_algorithm algorithm) {
430      /* nonlinear constraints are only supported with some algorithms */
431      return (algorithm == NLOPT_LD_MMA || algorithm == NLOPT_LD_CCSAQ 
432              || algorithm == NLOPT_LD_SLSQP
433              || algorithm == NLOPT_LN_COBYLA
434              || AUGLAG_ALG(algorithm) 
435              || algorithm == NLOPT_GN_ISRES
436              || algorithm == NLOPT_GN_ORIG_DIRECT
437              || algorithm == NLOPT_GN_ORIG_DIRECT_L);
438 }
439
440 nlopt_result
441 NLOPT_STDCALL nlopt_add_inequality_mconstraint(nlopt_opt opt, unsigned m,
442                                                nlopt_mfunc fc, void *fc_data,
443                                                const double *tol)
444 {
445      nlopt_result ret;
446      nlopt_unset_errmsg(opt);
447      if (!m) { /* empty constraints are always ok */
448           if (opt && opt->munge_on_destroy) opt->munge_on_destroy(fc_data);
449           return NLOPT_SUCCESS;
450      }
451      if (!opt) ret = NLOPT_INVALID_ARGS;
452      else if (!inequality_ok(opt->algorithm))
453          ret = ERR(NLOPT_INVALID_ARGS, opt, "invalid algorithm for constraints");
454      else ret = add_constraint(opt, &opt->m, &opt->m_alloc, &opt->fc,
455                                m, NULL, fc, NULL, fc_data, tol);
456      if (ret < 0 && opt && opt->munge_on_destroy)
457           opt->munge_on_destroy(fc_data);
458      return ret;
459 }
460
461 nlopt_result
462 NLOPT_STDCALL nlopt_add_precond_inequality_constraint(nlopt_opt opt,
463                                                       nlopt_func fc, 
464                                                       nlopt_precond pre,
465                                                       void *fc_data,
466                                                       double tol)
467 {
468      nlopt_result ret;
469      nlopt_unset_errmsg(opt);
470      if (!opt) ret = NLOPT_INVALID_ARGS;
471      else if (!inequality_ok(opt->algorithm))
472          ret = ERR(NLOPT_INVALID_ARGS, opt, "invalid algorithm for constraints");
473      else ret = add_constraint(opt, &opt->m, &opt->m_alloc, &opt->fc,
474                                1, fc, NULL, pre, fc_data, &tol);
475      if (ret < 0 && opt && opt->munge_on_destroy)
476           opt->munge_on_destroy(fc_data);
477      return ret;
478 }
479
480 nlopt_result
481 NLOPT_STDCALL nlopt_add_inequality_constraint(nlopt_opt opt,
482                                               nlopt_func fc, void *fc_data,
483                                               double tol)
484 {
485      return nlopt_add_precond_inequality_constraint(opt, fc, NULL, fc_data,
486                                                     tol);
487 }
488
489 nlopt_result
490 NLOPT_STDCALL nlopt_remove_equality_constraints(nlopt_opt opt)
491 {
492      unsigned i;
493      nlopt_unset_errmsg(opt);
494      if (!opt) return NLOPT_INVALID_ARGS;
495      if (opt->munge_on_destroy) {
496           nlopt_munge munge = opt->munge_on_destroy;
497           for (i = 0; i < opt->p; ++i)
498                munge(opt->h[i].f_data);
499      }
500      for (i = 0; i < opt->p; ++i)
501           free(opt->h[i].tol);
502      free(opt->h);
503      opt->h = NULL;
504      opt->p = opt->p_alloc = 0;
505      return NLOPT_SUCCESS;
506 }
507
508 static int equality_ok(nlopt_algorithm algorithm) {
509      /* equality constraints (h(x) = 0) only via some algorithms */
510      return (AUGLAG_ALG(algorithm) 
511              || algorithm == NLOPT_LD_SLSQP
512              || algorithm == NLOPT_GN_ISRES
513              || algorithm == NLOPT_LN_COBYLA);
514 }
515
516 nlopt_result
517 NLOPT_STDCALL nlopt_add_equality_mconstraint(nlopt_opt opt, unsigned m,
518                                              nlopt_mfunc fc, void *fc_data,
519                                              const double *tol)
520 {
521      nlopt_result ret;
522      nlopt_unset_errmsg(opt);
523      if (!m) { /* empty constraints are always ok */
524           if (opt && opt->munge_on_destroy) opt->munge_on_destroy(fc_data);
525           return NLOPT_SUCCESS;
526      }
527      if (!opt) ret = NLOPT_INVALID_ARGS;
528      else if (!equality_ok(opt->algorithm))
529          ret = ERR(NLOPT_INVALID_ARGS, opt, "invalid algorithm for constraints");
530      else if (nlopt_count_constraints(opt->p, opt->h) + m > opt->n) 
531          ret = ERR(NLOPT_INVALID_ARGS, opt, "too many equality constraints");
532      else ret = add_constraint(opt, &opt->p, &opt->p_alloc, &opt->h,
533                                m, NULL, fc, NULL, fc_data, tol);
534      if (ret < 0 && opt && opt->munge_on_destroy)
535           opt->munge_on_destroy(fc_data);
536      return ret;
537 }
538
539 nlopt_result
540 NLOPT_STDCALL nlopt_add_precond_equality_constraint(nlopt_opt opt,
541                                                     nlopt_func fc,
542                                                     nlopt_precond pre,
543                                                     void *fc_data,
544                                                     double tol)
545 {
546      nlopt_result ret;
547      nlopt_unset_errmsg(opt);
548      if (!opt) ret = NLOPT_INVALID_ARGS;
549      else if (!equality_ok(opt->algorithm))
550          ret = ERR(NLOPT_INVALID_ARGS, opt, "invalid algorithm for constraints");
551      else if (nlopt_count_constraints(opt->p, opt->h) + 1 > opt->n)
552          ret = ERR(NLOPT_INVALID_ARGS, opt, "too many equality constraints");
553      else ret = add_constraint(opt, &opt->p, &opt->p_alloc, &opt->h,
554                                1, fc, NULL, pre, fc_data, &tol);
555      if (ret < 0 && opt && opt->munge_on_destroy)
556           opt->munge_on_destroy(fc_data);
557      return ret;
558 }
559
560 nlopt_result
561 NLOPT_STDCALL nlopt_add_equality_constraint(nlopt_opt opt,
562                                             nlopt_func fc, void *fc_data,
563                                             double tol)
564 {
565      return nlopt_add_precond_equality_constraint(opt, fc, NULL, fc_data, tol);
566 }
567
568 /*************************************************************************/
569
570 #define SET(param, T, arg)                                              \
571    nlopt_result NLOPT_STDCALL nlopt_set_##param(nlopt_opt opt, T arg)   \
572    {                                                                    \
573         if (opt) {                                                      \
574              nlopt_unset_errmsg(opt);                                   \
575              opt->arg = arg;                                            \
576              return NLOPT_SUCCESS;                                      \
577         }                                                               \
578         return NLOPT_INVALID_ARGS;                                      \
579    }
580
581
582 #define GET(param, T, arg) T NLOPT_STDCALL      \
583    nlopt_get_##param(const nlopt_opt opt) {     \
584         return opt->arg;                        \
585    }
586
587 #define GETSET(param, T, arg) GET(param, T, arg) SET(param, T, arg)
588
589 GETSET(stopval, double, stopval)
590
591 GETSET(ftol_rel, double, ftol_rel)
592 GETSET(ftol_abs, double, ftol_abs)
593 GETSET(xtol_rel, double, xtol_rel)
594
595 nlopt_result
596 NLOPT_STDCALL nlopt_set_xtol_abs(nlopt_opt opt, const double *xtol_abs)
597 {
598      if (opt) {
599           nlopt_unset_errmsg(opt);
600           memcpy(opt->xtol_abs, xtol_abs, opt->n * sizeof(double));
601           return NLOPT_SUCCESS;
602      }
603      return NLOPT_INVALID_ARGS;
604 }
605
606 nlopt_result
607 NLOPT_STDCALL nlopt_set_xtol_abs1(nlopt_opt opt, double xtol_abs)
608 {
609      if (opt) {
610           unsigned i;
611           nlopt_unset_errmsg(opt);
612           for (i = 0; i < opt->n; ++i)
613                opt->xtol_abs[i] = xtol_abs;
614           return NLOPT_SUCCESS;
615      }
616      return NLOPT_INVALID_ARGS;
617 }
618
619 nlopt_result
620 NLOPT_STDCALL nlopt_get_xtol_abs(const nlopt_opt opt, double *xtol_abs)
621 {
622      memcpy(xtol_abs, opt->xtol_abs, opt->n * sizeof(double));
623      return NLOPT_SUCCESS;
624 }
625
626 GETSET(maxeval, int, maxeval)
627
628 GETSET(maxtime, double, maxtime)
629
630 /*************************************************************************/
631
632 nlopt_result
633 NLOPT_STDCALL nlopt_set_force_stop(nlopt_opt opt, int force_stop)
634 {
635      if (opt) {
636           nlopt_unset_errmsg(opt);
637           opt->force_stop = force_stop;
638           if (opt->force_stop_child)
639                return nlopt_set_force_stop(opt->force_stop_child, force_stop);
640           return NLOPT_SUCCESS;
641      }
642      return NLOPT_INVALID_ARGS;
643 }
644
645 GET(force_stop, int, force_stop)
646 nlopt_result NLOPT_STDCALL nlopt_force_stop(nlopt_opt opt) { 
647      return nlopt_set_force_stop(opt, 1); 
648 }
649
650 /*************************************************************************/
651
652 GET(algorithm, nlopt_algorithm, algorithm)
653 GET(dimension, unsigned, n)
654
655 /*************************************************************************/
656
657 nlopt_result
658 NLOPT_STDCALL nlopt_set_local_optimizer(nlopt_opt opt,
659                                         const nlopt_opt local_opt)
660 {
661      if (opt) {
662           nlopt_unset_errmsg(opt);
663           if (local_opt && local_opt->n != opt->n)
664               return ERR(NLOPT_INVALID_ARGS, opt, "dimension mismatch in local optimizer");
665           nlopt_destroy(opt->local_opt);
666           opt->local_opt = nlopt_copy(local_opt);
667           if (local_opt) {
668                if (!opt->local_opt) return NLOPT_OUT_OF_MEMORY;
669                nlopt_set_lower_bounds(opt->local_opt, opt->lb);
670                nlopt_set_upper_bounds(opt->local_opt, opt->ub);
671                nlopt_remove_inequality_constraints(opt->local_opt);
672                nlopt_remove_equality_constraints(opt->local_opt);
673                nlopt_set_min_objective(opt->local_opt, NULL, NULL);
674                nlopt_set_munge(opt->local_opt, NULL, NULL);
675                opt->local_opt->force_stop = 0;
676           }
677           return NLOPT_SUCCESS;
678      }
679      return NLOPT_INVALID_ARGS;
680 }
681
682 /*************************************************************************/
683
684 GETSET(population, unsigned, stochastic_population)
685 GETSET(vector_storage, unsigned, vector_storage)
686
687 /*************************************************************************/
688
689 nlopt_result NLOPT_STDCALL nlopt_set_initial_step1(nlopt_opt opt, double dx)
690 {
691      unsigned i;
692      if (!opt) return NLOPT_INVALID_ARGS;
693      nlopt_unset_errmsg(opt);
694      if (dx == 0) return ERR(NLOPT_INVALID_ARGS, opt, "zero step size");
695      if (!opt->dx && opt->n > 0) {
696           opt->dx = (double *) malloc(sizeof(double) * (opt->n));
697           if (!opt->dx) return NLOPT_OUT_OF_MEMORY;
698      }
699      for (i = 0; i < opt->n; ++i) opt->dx[i] = dx;
700      return NLOPT_SUCCESS;
701 }
702
703 nlopt_result
704 NLOPT_STDCALL nlopt_set_initial_step(nlopt_opt opt, const double *dx)
705 {
706      unsigned i;
707      if (!opt) return NLOPT_INVALID_ARGS;
708      nlopt_unset_errmsg(opt);
709      if (!dx) {
710           free(opt->dx); opt->dx = NULL;
711           return NLOPT_SUCCESS;
712      }
713      for (i = 0; i < opt->n; ++i)
714          if (dx[i] == 0) return ERR(NLOPT_INVALID_ARGS, opt, "zero step size");
715      if (!opt->dx && nlopt_set_initial_step1(opt, 1) == NLOPT_OUT_OF_MEMORY)
716           return NLOPT_OUT_OF_MEMORY;
717      memcpy(opt->dx, dx, sizeof(double) * (opt->n));
718      return NLOPT_SUCCESS;
719 }
720
721 nlopt_result
722 NLOPT_STDCALL nlopt_get_initial_step(const nlopt_opt opt, const double *x, 
723                                      double *dx)
724 {
725      if (!opt) return NLOPT_INVALID_ARGS;
726      nlopt_unset_errmsg(opt);
727      if (!opt->n) return NLOPT_SUCCESS;
728      if (!opt->dx) {
729           nlopt_opt o = (nlopt_opt) opt; /* discard const temporarily */
730           nlopt_result ret = nlopt_set_default_initial_step(o, x);
731           if (ret != NLOPT_SUCCESS) return ret;
732           memcpy(dx, o->dx, sizeof(double) * (opt->n));
733           free(o->dx); o->dx = NULL; /* don't save, since x-dependent */
734      }
735      else
736           memcpy(dx, opt->dx, sizeof(double) * (opt->n));
737      return NLOPT_SUCCESS;
738 }
739
740 nlopt_result
741 NLOPT_STDCALL nlopt_set_default_initial_step(nlopt_opt opt, const double *x)
742 {
743      const double *lb, *ub;
744      unsigned i;
745
746      nlopt_unset_errmsg(opt);
747      if (!opt || !x) return NLOPT_INVALID_ARGS;
748      lb = opt->lb; ub = opt->ub;
749
750      if (!opt->dx && nlopt_set_initial_step1(opt, 1) == NLOPT_OUT_OF_MEMORY)
751           return NLOPT_OUT_OF_MEMORY;
752
753      /* crude heuristics for initial step size of nonderivative algorithms */
754      for (i = 0; i < opt->n; ++i) {
755           double step = HUGE_VAL;
756
757           if (!nlopt_isinf(ub[i]) && !nlopt_isinf(lb[i])
758               && (ub[i] - lb[i]) * 0.25 < step && ub[i] > lb[i])
759                step = (ub[i] - lb[i]) * 0.25;
760           if (!nlopt_isinf(ub[i]) 
761               && ub[i] - x[i] < step && ub[i] > x[i])
762                step = (ub[i] - x[i]) * 0.75;
763           if (!nlopt_isinf(lb[i]) 
764               && x[i] - lb[i] < step && x[i] > lb[i])
765                step = (x[i] - lb[i]) * 0.75;
766
767           if (nlopt_isinf(step)) {
768                if (!nlopt_isinf(ub[i]) 
769                    && fabs(ub[i] - x[i]) < fabs(step))
770                     step = (ub[i] - x[i]) * 1.1;
771                if (!nlopt_isinf(lb[i]) 
772                    && fabs(x[i] - lb[i]) < fabs(step))
773                     step = (x[i] - lb[i]) * 1.1;
774           }
775           if (nlopt_isinf(step) || nlopt_istiny(step)) {
776                step = x[i];
777           }
778           if (nlopt_isinf(step) || step == 0.0)
779                step = 1;
780           
781           opt->dx[i] = step;
782      }
783      return NLOPT_SUCCESS;
784 }
785
786 /*************************************************************************/
787
788 void NLOPT_STDCALL nlopt_set_munge(nlopt_opt opt,
789                                    nlopt_munge munge_on_destroy,
790                                    nlopt_munge munge_on_copy) {
791      if (opt) {
792           opt->munge_on_destroy = munge_on_destroy;
793           opt->munge_on_copy = munge_on_copy;
794      }
795 }
796
797 void NLOPT_STDCALL nlopt_munge_data(nlopt_opt opt,
798                                     nlopt_munge2 munge, void *data) {
799      if (opt && munge) {
800           unsigned i;
801           opt->f_data = munge(opt->f_data, data);
802           for (i = 0; i < opt->m; ++i)
803                opt->fc[i].f_data = munge(opt->fc[i].f_data, data);
804           for (i = 0; i < opt->p; ++i)
805                opt->h[i].f_data = munge(opt->h[i].f_data, data);
806      }
807 }
808
809 /*************************************************************************/
810
811 const char *nlopt_set_errmsg(nlopt_opt opt, const char *format, ...)
812 {
813     va_list ap;
814     va_start(ap, format);
815     opt->errmsg = nlopt_vsprintf(opt->errmsg, format, ap);
816     va_end(ap);
817     return opt->errmsg;
818 }
819
820 void nlopt_unset_errmsg(nlopt_opt opt)
821 {
822     if (opt) {
823         free(opt->errmsg);
824         opt->errmsg = NULL;
825     }
826 }
827
828 const char *nlopt_get_errmsg(nlopt_opt opt)
829 {
830     return opt->errmsg;
831 }
832
833 /*************************************************************************/