-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstants.lisp
More file actions
105 lines (98 loc) · 6.26 KB
/
constants.lisp
File metadata and controls
105 lines (98 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
(in-package #:cffi-nlopt)
(defgeneric get-symbol (type no))
;;; fixmee: this is bad! I obviously didn't know what I was doing when
;;; writing this
(defmacro make-constants (type-name
(success-name success-no &optional success-doc)
&rest constants)
(let+ ((constants (append
(list (list success-name success-no success-doc))
constants))
(b (iter (for (c no doc) in constants) (collect `(cons ,no ,doc))))
(s (iter (for (c no doc) in constants) (collect `(cons ,no ',c)))))
(alexandria:with-gensyms (docstring-bag symbols-bag no doc-string)
`(let ((,docstring-bag (list ,@b))
(,symbols-bag (list ,@s)))
(cffi:defctype ,type-name :int)
(defconstant ,success-name ,success-no)
(defun ,(alexandria:symbolicate 'check/ type-name) (,no)
(if (not (eql ,no ,success-no))
(let ((,doc-string (assoc ,no ,docstring-bag)))
(if ,doc-string
(error "Error: ~a" (cdr ,doc-string))
(error "Unknown error: ~a" (assoc ,no ,symbols-bag))))))
(defmethod docstring ((type (eql ',type-name)) no-or-symbol)
(alexandria:if-let
(val (assoc no-or-symbol
(cond
((numberp no-or-symbol) ,docstring-bag)
((symbolp no-or-symbol) ,symbols-bag)
(t (error "DOCSTRING method needs a number or symbols as input.")))))
(cdr val)
(error "Unknown error: ~a" no-or-symbol)))
(defmethod get-symbol ((type (eql ',type-name)) no)
(alexandria:if-let (val (assoc no ,symbols-bag))
(cdr val)
(error "Unknown number ~a for type: ~a" no ',type-name)))
,@(iter
(for (c no doc) in constants)
(collect `(defconstant ,c ,no ,doc))
(collect `(export ',c)))))))
(eval-when (:compile-toplevel :load-toplevel)
(make-constants nlopt-result
(+NLOPT_SUCCESS+ 1 "Success.")
(+NLOPT_FAILURE+ -1 "Failure.")
(+NLOPT_INVALID_ARGS+ -2 "Invalid arguments given.")
(+NLOPT_OUT_OF_MEMORY+ -3 "Out of memory.")
(+NLOPT_ROUNDOFF_LIMITED+ -4 "Roundoff limited.")
(+NLOPT_FORCED_STOP+ -5 "Forced stop.")
(+NLOPT_STOPVAL_REACHED+ 2 "Stopval reached.")
(+NLOPT_FTOL_REACHED+ 3 "Tolerance in F reached.")
(+NLOPT_XTOL_REACHED+ 4 "Tolerance i X reached.")
(+NLOPT_MAXEVAL_REACHED+ 5 "Max. number evaluations reached.")
(+NLOPT_MAXTIME_REACHED+ 6 "Max. time reached."))
(make-constants nlopt-algorithm
(+nlopt_gn_direct+ 0 "DIRECT (global, no-derivative)")
(+nlopt_gn_direct_l+ 1 "DIRECT-L (global, no-derivative)")
(+nlopt_gn_direct_l_rand+ 2 "Randomized DIRECT-L (global, no-derivative)")
(+nlopt_gn_direct_noscal+ 3 "Unscaled DIRECT (global, no-derivative)")
(+nlopt_gn_direct_l_noscal+ 4 "Unscaled DIRECT-L (global, no-derivative)")
(+nlopt_gn_direct_l_rand_noscal+ 5 "Unscaled Randomized DIRECT-L (global, no-derivative)")
(+nlopt_gn_orig_direct+ 6 "Original DIRECT version (global, no-derivative)")
(+nlopt_gn_orig_direct_l+ 7 "Original DIRECT-L version (global, no-derivative)")
(+nlopt_gd_stogo+ 8 "StoGO (NOT COMPILED)")
(+nlopt_gd_stogo_rand+ 9 "StoGO randomized (NOT COMPILED)")
(+nlopt_ld_lbfgs_nocedal+ 10 "original L-BFGS code by Nocedal et al. (NOT COMPILED)")
(+nlopt_ld_lbfgs+ 11 "Limited-memory BFGS (L-BFGS) (local, derivative-based)")
(+nlopt_ln_praxis+ 12 "Principal-axis, praxis (local, no-derivative)")
(+nlopt_ld_var1+ 13 "Limited-memory variable-metric, rank 1 (local, derivative-based)")
(+nlopt_ld_var2+ 14 "Limited-memory variable-metric, rank 2 (local, derivative-based)")
(+nlopt_ld_tnewton+ 15 "Truncated Newton (local, derivative-based)")
(+nlopt_ld_tnewton_restart+ 16 "Truncated Newton with restarting (local, derivative-based)")
(+nlopt_ld_tnewton_precond+ 17 "Preconditioned truncated Newton (local, derivative-based)")
(+nlopt_ld_tnewton_precond_restart+ 18 "Preconditioned truncated Newton with restarting (local, derivative-based)")
(+nlopt_gn_crs2_lm+ 19 "Controlled random search (CRS2) with local mutation (global, no-derivative)")
(+nlopt_gn_mlsl+ 20 "Multi-level single-linkage (MLSL), random (global, no-derivative)")
(+nlopt_gd_mlsl+ 21 "Multi-level single-linkage (MLSL), random (global, derivative)")
(+nlopt_gn_mlsl_lds+ 22 "Multi-level single-linkage (MLSL), quasi-random (global, no-derivative)")
(+nlopt_gd_mlsl_lds+ 23 "Multi-level single-linkage (MLSL), quasi-random (global, derivative)")
(+nlopt_ld_mma+ 24 "Method of Moving Asymptotes (MMA) (local, derivative)")
(+nlopt_ln_cobyla+ 25 "COBYLA (Constrained Optimization BY Linear Approximations) (local, no-derivative)")
(+nlopt_ln_newuoa+ 26 "NEWUOA unconstrained optimization via quadratic models (local, no-derivative)")
(+nlopt_ln_newuoa_bound+ 27 "Bound-constrained optimization via NEWUOA-based quadratic models (local, no-derivative)")
(+nlopt_ln_neldermead+ 28 "Nelder-Mead simplex algorithm (local, no-derivative)")
(+nlopt_ln_sbplx+ 29 "Sbplx variant of Nelder-Mead (re-implementation of Rowan's Subplex) (local, no-derivative)")
(+nlopt_ln_auglag+ 30 "Augmented Lagrangian method (local, no-derivative)")
(+nlopt_ld_auglag+ 31 "Augmented Lagrangian method (local, derivative)")
(+nlopt_ln_auglag_eq+ 32 "Augmented Lagrangian method for equality constraints (local, no-derivative)")
(+nlopt_ld_auglag_eq+ 33 "Augmented Lagrangian method for equality constraints (local, derivative)")
(+nlopt_ln_bobyqa+ 34 "BOBYQA bound-constrained optimization via quadratic models (local, no-derivative)")
(+nlopt_gn_isres+ 35 "ISRES evolutionary constrained optimization (global, no-derivative)")
(+nlopt_auglag+ 36 "Augmented Lagrangian method (needs sub-algorithm)")
(+nlopt_auglag_eq+ 37 "Augmented Lagrangian method for equality constraints (needs sub-algorithm)")
(+nlopt_g_mlsl+ 38 "Multi-level single-linkage (MLSL), random (global, needs sub-algorithm)")
(+nlopt_g_mlsl_lds+ 39 "Multi-level single-linkage (MLSL), quasi-random (global, needs sub-algorithm)")
(+nlopt_ld_slsqp+ 40 "Sequential Quadratic Programming (SQP) (local, derivative)")
(+nlopt_ld_ccsaq+ 41 "CCSA (Conservative Convex Separable Approximations) with simple quadratic approximations (local, derivative)")
(+nlopt_gn_esch+ 42 "ESCH evolutionary strategy")
(+nlopt_num_algorithm+ 43 "UNKNOWN")))