chiark / gitweb /
strip
[nlopt.git] / test / t_guile.scm
1 (use-modules (nlopt))
2
3 (define (myfunc x grad)
4   (if grad
5       (begin
6         (vector-set! grad 0 0.0)
7         (vector-set! grad 1 (/ 0.5 (sqrt (vector-ref x 1))))))
8   (sqrt (vector-ref x 1)))
9
10 (define (myconstraint x grad a b)
11   (let ((x0 (vector-ref x 0)) (x1 (vector-ref x 1)))
12     (if grad
13         (begin
14           (vector-set! grad 0 (* 3 a (expt (+ (* a x0) b) 2)))
15           (vector-set! grad 1 -1.0)))
16     (- (expt (+ (* a x0) b) 3) x1)))
17
18 (define opt (new-nlopt-opt NLOPT-LD-MMA 2))
19 (nlopt-opt-set-lower-bounds opt (vector (- (inf)) 0))
20 (nlopt-opt-set-min-objective opt myfunc)
21 (nlopt-opt-add-inequality-constraint opt (lambda (x grad)
22                                            (myconstraint x grad 2 0))
23                                      1e-8)
24 (nlopt-opt-add-inequality-constraint opt (lambda (x grad)
25                                            (myconstraint x grad -1 1))
26                                      1e-8)
27 (nlopt-opt-set-xtol-rel opt 1e-4)
28 (define x (nlopt-opt-optimize opt (vector 1.234 5.678)))
29 (define minf (nlopt-opt-last-optimum-value opt))
30 (define result (nlopt-opt-last-optimize-result opt))
31 (display "x=")
32 (display x)
33 (newline)
34 (display "minf=")
35 (display minf)
36 (newline)