Skip to content

Instantly share code, notes, and snippets.

@kazh98
Created August 19, 2013 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazh98/6265643 to your computer and use it in GitHub Desktop.
Save kazh98/6265643 to your computer and use it in GitHub Desktop.
私立・プログラミングキャンプ成果物(最急降下法による3次元制約つき最適化ソルバ)
#!/usr/bin/env gosh
(use srfi-27)
(use gauche.uvector)
(use gauche.collection)
(define v f64vector)
(define <v> <f64vector>)
(define v-ref f64vector-ref)
(define v-map (pa$ map-to <v>))
(define (projection a b c d x)
(let* ((det (- (+ (* a (v-ref x 0)) (* b (v-ref x 1)) (* c (v-ref x 2))) d))
(dif (/ det (+ (expt a 2) (expt b 2) (expt c 2)))))
(if (<= det 0)
x
(v-map - x (v-map (pa$ * dif) (v a b c))))))
(define (p0 x)
(v (max 0 (v-ref x 0))
(max 0 (v-ref x 1))
(max 0 (v-ref x 2))))
(define p1 (pa$ projection 3 5 2 500))
(define p2 (pa$ projection 2 7 4 400))
(define project (compose p0 p1 p2))
(define (gradient x)
(v 4 5 3))
(define (evaluate x)
(+ (* 4 (v-ref x 0)) (* 5 (v-ref x 1)) (* 3 (v-ref x 2))))
(let lp ((n 0) (x (v 0 0 0)))
(format #t "~D ~D ~D ~D ~D~%" n (evaluate x) (v-ref x 0) (v-ref x 1) (v-ref x 2))
(when (< n 100000)
(lp (+ n 1) (project (v-map + x (v-map (cut / <> (expt n 0.45)) (gradient x)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment