From 682376900c69ce208a68eab51d626ce8d98edd0a Mon Sep 17 00:00:00 2001 From: Honglin Yu Date: Thu, 11 Dec 2014 22:52:09 +1100 Subject: [PATCH] make into standard folder structure, not usable yet --- .gitignore | 75 +++++++++++++++++++++++++++ .travis.yml | 17 +++++++ LICENSE.md | 25 +++++++++ README.md | 1 + src/Lbfgsb.jl | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 255 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 LICENSE.md create mode 100644 src/Lbfgsb.jl diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..264ec61 --- /dev/null +++ b/.gitignore @@ -0,0 +1,75 @@ +# Created by http://www.gitignore.io + +### Emacs ### +# -*- mode: gitignore; -*- +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + + +### C++ ### +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + + +### CMake ### +CMakeCache.txt +CMakeFiles +Makefile +cmake_install.cmake +install_manifest.txt + +### build ### +/build/* +/bin/* +/emacs-build/* +/output/* +/viewer.py +# /python/* +/test/* +libpysegfit.so +segfit \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..7ca7f00 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,17 @@ +language: cpp +compiler: + - clang +notifications: + email: false +env: + matrix: + - JULIAVERSION="juliareleases" + - JULIAVERSION="julianightlies" +before_install: + - sudo add-apt-repository ppa:staticfloat/julia-deps -y + - sudo add-apt-repository ppa:staticfloat/${JULIAVERSION} -y + - sudo apt-get update -qq -y + - sudo apt-get install libpcre3-dev julia -y + - if [[ -a .git/shallow ]]; then git fetch --unshallow; fi +script: + - julia -e 'Pkg.init(); Pkg.clone(pwd()); Pkg.test("Lbfgsb")' diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..cf99400 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,25 @@ +The Lbfgsb.jl package is licensed under the Simplified "2-clause" BSD License: + +> Copyright (c) 2014: Honglin Yu. +> +> Redistribution and use in source and binary forms, with or without +> modification, are permitted provided that the following conditions are +> met: +> +> 1. Redistributions of source code must retain the above copyright +> notice, this list of conditions and the following disclaimer. +> 2. Redistributions in binary form must reproduce the above copyright +> notice, this list of conditions and the following disclaimer in the +> documentation and/or other materials provided with the distribution. +> +> THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +> "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +> LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +> A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +> OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +> SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +> LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +> DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +> THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +> (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +> OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md index c6b238e..c67176b 100644 --- a/README.md +++ b/README.md @@ -47,3 +47,4 @@ BSD-3 [lbfgsb fortran library]:http://users.iems.northwestern.edu/~nocedal/lbfgsb.html [this wrapper]:http://hannes.nickisch.org/code/glm-ie/pls/lbfgsb/README.html + diff --git a/src/Lbfgsb.jl b/src/Lbfgsb.jl new file mode 100644 index 0000000..1d0ffa1 --- /dev/null +++ b/src/Lbfgsb.jl @@ -0,0 +1,137 @@ +module Lbfgsb + +# package code goes here +macro callLBFGS(cmd) + quote + if length($cmd) != 0 + @simd for i = 1:length($cmd) + task[i] = ($cmd)[i]; + end + @simd for i = length($cmd)+1:60 + task[i] = ' '; + end + end + + ccall((:setulb_, "./liblbfgsb.so"), + Void, + (Ptr{Int32}, + Ptr{Int32}, + Ptr{Float64}, + Ptr{Float64}, + Ptr{Float64}, + Ptr{Int32}, + Ptr{Float64}, + Ptr{Float64}, + Ptr{Float64}, + Ptr{Float64}, + Ptr{Float64}, + Ptr{Int32}, + Ptr{Uint8}, + Ptr{Int32}, + Ptr{Uint8}, + Ptr{Bool}, + Ptr{Int32}, + Ptr{Float64} ), + n, + m, + x, + lb, + ub, + btype, + f, + g, + factr, + pgtol, + wa, + iwa, + task, + iprint, + csave, + lsave, + isave, + dsave ); + end +end + +function lbfgsb (ogFunc, + x; + lb = [], + ub = [], + btype = [], + m::Int64 = 5, + maxiter::Int64 = 100, + factr::Float64 = 1e7, + pgtol::Float64 = 1e-5, + iprint::Int64 = -1 # does not print + ) + m = [convert(Int32, m)] + factr = [convert(Float64, factr)]; + pgtol = [convert(Float64, pgtol)]; + iprint = [convert(Int32, iprint)]; + + n = [convert(Int32, length(x))]; # number of variables + f = [convert(Float64, 0.0)]; # The value of the objective. + g = [convert(Float64, 0.0) for i=1:(n[1])]; # The value of the gradient. + + if length(lb) == 0 + lb = [-Inf for i=1:(n[1])]; + else + lb = [convert(Float64, i) for i in lb] + end + + if length(ub) == 0 + ub = [Inf for i=1:(n[1])]; + else + ub = [convert(Float64, i) for i in ub] + end + + if length(btype) == 0 + btype = [convert(Int32, 2) for i=1:(n[1])]; + else + btype = [convert(Int32, i) for i in btype]; + end + + # structures used by the L-BFGS-B routine. + wa = [convert(Float64, 0.0) for i = 1:(2*m[1] + 5)*n[1] + 12*m[1]*(m[1] + 1)]; + iwa = [convert(Int32, 0) for i = 1:3*n[1]]; + task = [convert(Uint8, 0) for i =1:60]; + csave = [convert(Uint8, 0) for i =1:60]; + lsave = [convert(Bool, 0) for i=1:4]; + isave = [convert(Int32, 0) for i=1:44]; + dsave = [convert(Float64, 0.0) for i=1:29]; + + @callLBFGS "START" + + status = "success"; + + t = 0; + + while true + + if task[1] == 'F' + tf, g = ogFunc(x); + f[1] = convert(Float64, tf); + + elseif task[1] == 'N' + t += 1; + if t >= maxiter # exceed maximum number of iteration + @callLBFGS "STOP" + break; + end + elseif task[1] == 'C' # convergence + break; + elseif task[1] == 'A' + status = "abnormal"; + break; + elseif task[1] == 'E' + status = "error"; + break; + end + + @callLBFGS "" + end + + return (f, x, status, t); +end + +end # module