numo-optimize 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +3 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +27 -0
- data/README.md +77 -0
- data/Rakefile +35 -0
- data/ext/numo/optimize/extconf.rb +45 -0
- data/ext/numo/optimize/optimize.c +228 -0
- data/ext/numo/optimize/optimize.h +13 -0
- data/ext/numo/optimize/src/License.txt +71 -0
- data/ext/numo/optimize/src/blas.c +253 -0
- data/ext/numo/optimize/src/blas.h +11 -0
- data/ext/numo/optimize/src/common.h +16 -0
- data/ext/numo/optimize/src/lbfgsb.c +3995 -0
- data/ext/numo/optimize/src/lbfgsb.h +83 -0
- data/ext/numo/optimize/src/linpack.c +237 -0
- data/ext/numo/optimize/src/linpack.h +9 -0
- data/lib/numo/optimize/version.rb +10 -0
- data/lib/numo/optimize.rb +110 -0
- metadata +78 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 8f81cd59ec9e2ae660daef989ae28a5e5a075063897598e39a1457e5d5107037
|
4
|
+
data.tar.gz: 304c618243188c812c8a37a9d7060bf40526c8bce3f306f21f3a75cdfb917532
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 64f1624de1f310cbb58fc390f0108e94dfbfcfd714c6f1c1a012af626bb96bf7da713470bfe076b1b0bf64a7016e5925801e6eb2e5ea014bb669ae16ab5b952d
|
7
|
+
data.tar.gz: 373769fc7093dc7b429f296a5f4b2361489cede752618ec6c280a612bf13c64bca2fb9d4dd027c0b147a9691f032d41ccb7c498782e850986ec8c485de1233ed
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
10
|
+
identity and orientation.
|
11
|
+
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
13
|
+
diverse, inclusive, and healthy community.
|
14
|
+
|
15
|
+
## Our Standards
|
16
|
+
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
18
|
+
community include:
|
19
|
+
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
24
|
+
and learning from the experience
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
26
|
+
community
|
27
|
+
|
28
|
+
Examples of unacceptable behavior include:
|
29
|
+
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
31
|
+
any kind
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
33
|
+
* Public or private harassment
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
35
|
+
without their explicit permission
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
37
|
+
professional setting
|
38
|
+
|
39
|
+
## Enforcement Responsibilities
|
40
|
+
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
44
|
+
or harmful.
|
45
|
+
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
49
|
+
decisions when appropriate.
|
50
|
+
|
51
|
+
## Scope
|
52
|
+
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
54
|
+
an individual is officially representing the community in public spaces.
|
55
|
+
Examples of representing our community include using an official email address,
|
56
|
+
posting via an official social media account, or acting as an appointed
|
57
|
+
representative at an online or offline event.
|
58
|
+
|
59
|
+
## Enforcement
|
60
|
+
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
62
|
+
reported to the community leaders responsible for enforcement at
|
63
|
+
[INSERT CONTACT METHOD].
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
65
|
+
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
67
|
+
reporter of any incident.
|
68
|
+
|
69
|
+
## Enforcement Guidelines
|
70
|
+
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
73
|
+
|
74
|
+
### 1. Correction
|
75
|
+
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
77
|
+
unprofessional or unwelcome in the community.
|
78
|
+
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
82
|
+
|
83
|
+
### 2. Warning
|
84
|
+
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
86
|
+
actions.
|
87
|
+
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
93
|
+
ban.
|
94
|
+
|
95
|
+
### 3. Temporary Ban
|
96
|
+
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
98
|
+
sustained inappropriate behavior.
|
99
|
+
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
101
|
+
communication with the community for a specified period of time. No public or
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
104
|
+
Violating these terms may lead to a permanent ban.
|
105
|
+
|
106
|
+
### 4. Permanent Ban
|
107
|
+
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
111
|
+
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
113
|
+
community.
|
114
|
+
|
115
|
+
## Attribution
|
116
|
+
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
118
|
+
version 2.1, available at
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
120
|
+
|
121
|
+
Community Impact Guidelines were inspired by
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
123
|
+
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
127
|
+
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Copyright (c) 2025 Atsushi Tatsuma
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
* Redistributions of source code must retain the above copyright notice, this
|
8
|
+
list of conditions and the following disclaimer.
|
9
|
+
|
10
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
12
|
+
and/or other materials provided with the distribution.
|
13
|
+
|
14
|
+
* Neither the name of the copyright holder nor the names of its
|
15
|
+
contributors may be used to endorse or promote products derived from
|
16
|
+
this software without specific prior written permission.
|
17
|
+
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
19
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
20
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
21
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
22
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
23
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
24
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
25
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
26
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
27
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# Numo::Optimize
|
2
|
+
|
3
|
+
[](https://github.com/yoshoku/numo-optimize/actions/workflows/main.yml)
|
4
|
+
[](https://github.com/yoshoku/numo-optimize/blob/main/LICENSE.txt)
|
5
|
+
|
6
|
+
Numo::Optimize (numo-optimize) provides functions for minimizing objective functions.
|
7
|
+
This gem is based on [Lbfgsb.rb](https://github.com/yoshoku/lbfgsb.rb) and
|
8
|
+
[mopti](https://github.com/yoshoku/mopti) by the same author.
|
9
|
+
As for optimization algorithms, only L-BFGS-B is currently supported.
|
10
|
+
|
11
|
+
Please note that numo-optimize depends on [numo-narray-alt](https://github.com/yoshoku/numo-narray-alt), not Numo::NArray.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Install the gem and add to the application's Gemfile by executing:
|
16
|
+
|
17
|
+
```bash
|
18
|
+
bundle add numo-optimize
|
19
|
+
```
|
20
|
+
|
21
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
22
|
+
|
23
|
+
```bash
|
24
|
+
gem install numo-optimize
|
25
|
+
```
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
example.rb:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
require 'numo/narray'
|
33
|
+
require 'numo/optimize'
|
34
|
+
|
35
|
+
# Define the objective function.
|
36
|
+
obj_func = proc do |x|
|
37
|
+
(x - 3)**2 + 2
|
38
|
+
end
|
39
|
+
|
40
|
+
# Define the gradient (Jacobian) of the objective function.
|
41
|
+
gradient_func = proc do |x|
|
42
|
+
2 * (x - 3)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Initial guess
|
46
|
+
x_init = Numo::DFloat[0.0]
|
47
|
+
|
48
|
+
# Perform optimization
|
49
|
+
result = Numo::Optimize.minimize(x_init: x_init, fnc: obj_func, jcb: gradient_func)
|
50
|
+
|
51
|
+
# Output the result
|
52
|
+
puts "Optimized parameters: #{result[:x][0]}"
|
53
|
+
```
|
54
|
+
|
55
|
+
```sh
|
56
|
+
$ ruby example.rb
|
57
|
+
Optimized parameters: 3.0
|
58
|
+
```
|
59
|
+
|
60
|
+
## Contributing
|
61
|
+
|
62
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/yoshoku/numo-optimize.
|
63
|
+
This project is intended to be a safe, welcoming space for collaboration,
|
64
|
+
and contributors are expected to adhere to the [code of conduct](https://github.com/yoshoku/numo-optimize/blob/main/CODE_OF_CONDUCT.md).
|
65
|
+
|
66
|
+
## License
|
67
|
+
|
68
|
+
The gem is available as open source under the terms of the [BSD-3-Clause License](https://opensource.org/licenses/BSD-3-Clause).
|
69
|
+
In addition, L-BFGS-B carries the following condition for use:
|
70
|
+
|
71
|
+
This software is freely available, but we expect that all publications describing work using this software ,
|
72
|
+
or all commercial products using it, quote at least one of the references given below.
|
73
|
+
This software is released under the "New BSD License" (aka "Modified BSD License" or "3-clause license").
|
74
|
+
|
75
|
+
## Code of Conduct
|
76
|
+
|
77
|
+
Everyone interacting in the Numo::Optimize project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/yoshoku/numo-optimize/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler/gem_tasks'
|
4
|
+
|
5
|
+
if ENV['BUNDLE_WITH'] == 'memcheck'
|
6
|
+
require 'ruby_memcheck'
|
7
|
+
require 'rake/testtask'
|
8
|
+
test_config = lambda do |t|
|
9
|
+
t.libs << 'test'
|
10
|
+
t.test_files = FileList['test/**/test_*.rb']
|
11
|
+
end
|
12
|
+
Rake::TestTask.new(test: :compile, &test_config)
|
13
|
+
namespace :test do
|
14
|
+
RubyMemcheck::TestTask.new(valgrind: :compile, &test_config)
|
15
|
+
end
|
16
|
+
else
|
17
|
+
require 'minitest/test_task'
|
18
|
+
Minitest::TestTask.create
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'rubocop/rake_task'
|
22
|
+
|
23
|
+
RuboCop::RakeTask.new
|
24
|
+
|
25
|
+
require 'rake/extensiontask'
|
26
|
+
|
27
|
+
task build: :compile # rubocop:disable Rake/Desc
|
28
|
+
|
29
|
+
GEMSPEC = Gem::Specification.load('numo-optimize.gemspec')
|
30
|
+
|
31
|
+
Rake::ExtensionTask.new('numo/optimize', GEMSPEC) do |ext|
|
32
|
+
ext.lib_dir = 'lib/numo/optimize'
|
33
|
+
end
|
34
|
+
|
35
|
+
task default: %i[clobber compile test]
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'mkmf'
|
4
|
+
require 'numo/narray'
|
5
|
+
|
6
|
+
$LOAD_PATH.each do |lp|
|
7
|
+
if File.exist?(File.join(lp, 'numo/numo/narray.h'))
|
8
|
+
$INCFLAGS = "-I#{lp}/numo #{$INCFLAGS}"
|
9
|
+
break
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
abort 'numo/narray.h not found.' unless have_header('numo/narray.h')
|
14
|
+
|
15
|
+
if RUBY_PLATFORM.match?(/mswin|cygwin|mingw/)
|
16
|
+
$LOAD_PATH.each do |lp|
|
17
|
+
if File.exist?(File.join(lp, 'numo/libnarray.a'))
|
18
|
+
$LDFLAGS = "-L#{lp}/numo #{$LDFLAGS}"
|
19
|
+
break
|
20
|
+
end
|
21
|
+
end
|
22
|
+
abort 'libnarray.a not found.' unless have_library('narray', 'nary_new')
|
23
|
+
end
|
24
|
+
|
25
|
+
$defs << '-DUSE_INT64' if with_config('use-int64', false)
|
26
|
+
|
27
|
+
$srcs = Dir.glob("#{$srcdir}/**/*.c").map { |path| File.basename(path) }
|
28
|
+
|
29
|
+
blas_dir = with_config('blas-dir')
|
30
|
+
$LDFLAGS = "-L#{blas_dir} #{$LDFLAGS}" unless blas_dir.nil?
|
31
|
+
|
32
|
+
blas_lib = with_config('blas-lib')
|
33
|
+
unless blas_lib.nil?
|
34
|
+
abort "#{blas_lib} not found." unless have_library(blas_lib)
|
35
|
+
$srcs.delete('blas.c')
|
36
|
+
end
|
37
|
+
|
38
|
+
$VPATH << '$(srcdir)/src'
|
39
|
+
|
40
|
+
# Makes all symbols private by default to avoid unintended conflict
|
41
|
+
# with other gems. To explicitly export symbols you can use RUBY_FUNC_EXPORTED
|
42
|
+
# selectively, or entirely remove this flag.
|
43
|
+
append_cflags('-fvisibility=hidden')
|
44
|
+
|
45
|
+
create_makefile('numo/optimize/optimize')
|
@@ -0,0 +1,228 @@
|
|
1
|
+
#include "optimize.h"
|
2
|
+
|
3
|
+
VALUE rb_mOptimize;
|
4
|
+
VALUE rb_mLbfgsb;
|
5
|
+
|
6
|
+
static VALUE lbfgsb_fmin(VALUE self, VALUE fnc, VALUE x_val, VALUE jcb, VALUE args, VALUE l_val, VALUE u_val,
|
7
|
+
VALUE nbd_val, VALUE maxcor, VALUE ftol, VALUE gtol, VALUE maxiter, VALUE disp) {
|
8
|
+
F77_int n_iter;
|
9
|
+
F77_int n_fev;
|
10
|
+
F77_int n_jev;
|
11
|
+
#ifdef USE_INT64
|
12
|
+
F77_int max_iter = NUM2LONG(maxiter);
|
13
|
+
#else
|
14
|
+
F77_int max_iter = NUM2INT(maxiter);
|
15
|
+
#endif
|
16
|
+
narray_t* x_nary;
|
17
|
+
narray_t* l_nary;
|
18
|
+
narray_t* u_nary;
|
19
|
+
narray_t* nbd_nary;
|
20
|
+
F77_int n;
|
21
|
+
#ifdef USE_INT64
|
22
|
+
F77_int m = NUM2LONG(maxcor);
|
23
|
+
#else
|
24
|
+
F77_int m = NUM2INT(maxcor);
|
25
|
+
#endif
|
26
|
+
double* x_ptr;
|
27
|
+
double* l_ptr;
|
28
|
+
double* u_ptr;
|
29
|
+
F77_int* nbd_ptr;
|
30
|
+
double f;
|
31
|
+
double* g;
|
32
|
+
double factr = NUM2DBL(ftol);
|
33
|
+
double pgtol = NUM2DBL(gtol);
|
34
|
+
double* wa;
|
35
|
+
F77_int* iwa;
|
36
|
+
char task[60];
|
37
|
+
#ifdef USE_INT64
|
38
|
+
F77_int iprint = NIL_P(disp) ? -1 : NUM2LONG(disp);
|
39
|
+
#else
|
40
|
+
F77_int iprint = NIL_P(disp) ? -1 : NUM2INT(disp);
|
41
|
+
#endif
|
42
|
+
char csave[60];
|
43
|
+
F77_int lsave[4];
|
44
|
+
F77_int isave[44];
|
45
|
+
double dsave[29];
|
46
|
+
VALUE g_val;
|
47
|
+
VALUE fg_arr;
|
48
|
+
VALUE ret;
|
49
|
+
|
50
|
+
GetNArray(x_val, x_nary);
|
51
|
+
if (NA_NDIM(x_nary) != 1) {
|
52
|
+
rb_raise(rb_eArgError, "x must be a 1-D array.");
|
53
|
+
return Qnil;
|
54
|
+
}
|
55
|
+
n = (F77_int)NA_SIZE(x_nary);
|
56
|
+
if (CLASS_OF(x_val) != numo_cDFloat) {
|
57
|
+
x_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, x_val);
|
58
|
+
}
|
59
|
+
if (!RTEST(nary_check_contiguous(x_val))) {
|
60
|
+
x_val = nary_dup(x_val);
|
61
|
+
}
|
62
|
+
|
63
|
+
GetNArray(l_val, l_nary);
|
64
|
+
if (NA_NDIM(l_nary) != 1) {
|
65
|
+
rb_raise(rb_eArgError, "l must be a 1-D array.");
|
66
|
+
return Qnil;
|
67
|
+
}
|
68
|
+
if ((F77_int)NA_SIZE(l_nary) != n) {
|
69
|
+
rb_raise(rb_eArgError, "The size of l must be equal to that of x.");
|
70
|
+
return Qnil;
|
71
|
+
}
|
72
|
+
if (CLASS_OF(l_val) != numo_cDFloat) {
|
73
|
+
l_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, l_val);
|
74
|
+
}
|
75
|
+
if (!RTEST(nary_check_contiguous(l_val))) {
|
76
|
+
l_val = nary_dup(l_val);
|
77
|
+
}
|
78
|
+
|
79
|
+
GetNArray(u_val, u_nary);
|
80
|
+
if (NA_NDIM(u_nary) != 1) {
|
81
|
+
rb_raise(rb_eArgError, "u must be a 1-D array.");
|
82
|
+
return Qnil;
|
83
|
+
}
|
84
|
+
if ((F77_int)NA_SIZE(u_nary) != n) {
|
85
|
+
rb_raise(rb_eArgError, "The size of u must be equal to that of x.");
|
86
|
+
return Qnil;
|
87
|
+
}
|
88
|
+
if (CLASS_OF(u_val) != numo_cDFloat) {
|
89
|
+
u_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, u_val);
|
90
|
+
}
|
91
|
+
if (!RTEST(nary_check_contiguous(u_val))) {
|
92
|
+
u_val = nary_dup(u_val);
|
93
|
+
}
|
94
|
+
|
95
|
+
GetNArray(nbd_val, nbd_nary);
|
96
|
+
if (NA_NDIM(nbd_nary) != 1) {
|
97
|
+
rb_raise(rb_eArgError, "nbd must be a 1-D array.");
|
98
|
+
return Qnil;
|
99
|
+
}
|
100
|
+
if ((F77_int)NA_SIZE(nbd_nary) != n) {
|
101
|
+
rb_raise(rb_eArgError, "The size of nbd must be equal to that of x.");
|
102
|
+
return Qnil;
|
103
|
+
}
|
104
|
+
#ifdef USE_INT64
|
105
|
+
if (CLASS_OF(nbd_val) != numo_cInt64) {
|
106
|
+
nbd_val = rb_funcall(numo_cInt64, rb_intern("cast"), 1, nbd_val);
|
107
|
+
}
|
108
|
+
#else
|
109
|
+
if (CLASS_OF(nbd_val) != numo_cInt32) {
|
110
|
+
nbd_val = rb_funcall(numo_cInt32, rb_intern("cast"), 1, nbd_val);
|
111
|
+
}
|
112
|
+
#endif
|
113
|
+
if (!RTEST(nary_check_contiguous(nbd_val))) {
|
114
|
+
nbd_val = nary_dup(nbd_val);
|
115
|
+
}
|
116
|
+
|
117
|
+
x_ptr = (double*)na_get_pointer_for_read_write(x_val);
|
118
|
+
l_ptr = (double*)na_get_pointer_for_read(l_val);
|
119
|
+
u_ptr = (double*)na_get_pointer_for_read(u_val);
|
120
|
+
nbd_ptr = (F77_int*)na_get_pointer_for_read(nbd_val);
|
121
|
+
g = ALLOC_N(double, n);
|
122
|
+
wa = ALLOC_N(double, (2 * m + 5) * n + 12 * m * m + 12 * m);
|
123
|
+
iwa = ALLOC_N(F77_int, 3 * n);
|
124
|
+
|
125
|
+
g_val = Qnil;
|
126
|
+
f = 0.0;
|
127
|
+
memset(g, 0, n * sizeof(*g));
|
128
|
+
strcpy(task, "START");
|
129
|
+
n_fev = 0;
|
130
|
+
n_jev = 0;
|
131
|
+
|
132
|
+
for (n_iter = 0; n_iter < max_iter;) {
|
133
|
+
setulb_(&n, &m, x_ptr, l_ptr, u_ptr, nbd_ptr, &f, g, &factr, &pgtol, wa, iwa, task, &iprint, csave, lsave, isave, dsave);
|
134
|
+
if (strncmp(task, "FG", 2) == 0) {
|
135
|
+
if (RB_TYPE_P(jcb, T_TRUE)) {
|
136
|
+
fg_arr = rb_funcall(self, rb_intern("fnc"), 3, fnc, x_val, args);
|
137
|
+
f = NUM2DBL(rb_ary_entry(fg_arr, 0));
|
138
|
+
g_val = rb_ary_entry(fg_arr, 1);
|
139
|
+
} else {
|
140
|
+
f = NUM2DBL(rb_funcall(self, rb_intern("fnc"), 3, fnc, x_val, args));
|
141
|
+
g_val = rb_funcall(self, rb_intern("jcb"), 3, jcb, x_val, args);
|
142
|
+
}
|
143
|
+
n_fev++;
|
144
|
+
n_jev++;
|
145
|
+
if (CLASS_OF(g_val) != numo_cDFloat)
|
146
|
+
g_val = rb_funcall(numo_cDFloat, rb_intern("cast"), 1, g_val);
|
147
|
+
if (!RTEST(nary_check_contiguous(g_val)))
|
148
|
+
g_val = nary_dup(g_val);
|
149
|
+
memcpy(g, na_get_pointer_for_read(g_val), n * sizeof(*g));
|
150
|
+
RB_GC_GUARD(g_val);
|
151
|
+
} else if (strncmp(task, "NEW_X", 5) == 0) {
|
152
|
+
n_iter++;
|
153
|
+
} else {
|
154
|
+
break;
|
155
|
+
}
|
156
|
+
}
|
157
|
+
|
158
|
+
xfree(g);
|
159
|
+
xfree(wa);
|
160
|
+
xfree(iwa);
|
161
|
+
|
162
|
+
ret = rb_hash_new();
|
163
|
+
rb_hash_aset(ret, ID2SYM(rb_intern("task")), rb_str_new_cstr(task));
|
164
|
+
rb_hash_aset(ret, ID2SYM(rb_intern("x")), x_val);
|
165
|
+
rb_hash_aset(ret, ID2SYM(rb_intern("fnc")), DBL2NUM(f));
|
166
|
+
rb_hash_aset(ret, ID2SYM(rb_intern("jcb")), g_val);
|
167
|
+
#ifdef USE_INT64
|
168
|
+
rb_hash_aset(ret, ID2SYM(rb_intern("n_iter")), LONG2NUM(n_iter));
|
169
|
+
rb_hash_aset(ret, ID2SYM(rb_intern("n_fev")), LONG2NUM(n_fev));
|
170
|
+
rb_hash_aset(ret, ID2SYM(rb_intern("n_jev")), LONG2NUM(n_jev));
|
171
|
+
#else
|
172
|
+
rb_hash_aset(ret, ID2SYM(rb_intern("n_iter")), INT2NUM(n_iter));
|
173
|
+
rb_hash_aset(ret, ID2SYM(rb_intern("n_fev")), INT2NUM(n_fev));
|
174
|
+
rb_hash_aset(ret, ID2SYM(rb_intern("n_jev")), INT2NUM(n_jev));
|
175
|
+
#endif
|
176
|
+
rb_hash_aset(ret, ID2SYM(rb_intern("success")), strncmp(task, "CONV", 4) == 0 ? Qtrue : Qfalse);
|
177
|
+
|
178
|
+
RB_GC_GUARD(x_val);
|
179
|
+
RB_GC_GUARD(l_val);
|
180
|
+
RB_GC_GUARD(u_val);
|
181
|
+
RB_GC_GUARD(nbd_val);
|
182
|
+
|
183
|
+
return ret;
|
184
|
+
}
|
185
|
+
|
186
|
+
RUBY_FUNC_EXPORTED void
|
187
|
+
Init_optimize(void) {
|
188
|
+
rb_require("numo/narray");
|
189
|
+
|
190
|
+
/**
|
191
|
+
* Document-module: Numo::Optimize
|
192
|
+
*/
|
193
|
+
rb_mOptimize = rb_define_module_under(rb_mNumo, "Optimize");
|
194
|
+
/**
|
195
|
+
* Document-module: Numo::Optimize::Lbfgsb
|
196
|
+
*/
|
197
|
+
rb_mLbfgsb = rb_define_module_under(rb_mOptimize, "Lbfgsb");
|
198
|
+
|
199
|
+
#ifdef USE_INT64
|
200
|
+
/* The bit size of fortran integer. */
|
201
|
+
rb_define_const(rb_mLbfgsb, "SZ_F77_INTEGER", INT2NUM(64));
|
202
|
+
#else
|
203
|
+
/* The bit size of fortran integer. */
|
204
|
+
rb_define_const(rb_mLbfgsb, "SZ_F77_INTEGER", INT2NUM(32));
|
205
|
+
#endif
|
206
|
+
/* The value of double epsilon used in the native extension. */
|
207
|
+
rb_define_const(rb_mLbfgsb, "DBL_EPSILON", DBL2NUM(DBL_EPSILON));
|
208
|
+
/**
|
209
|
+
* Minimize a function using the L-BFGS-B algorithm.
|
210
|
+
* This module function is for internal use. It is recommended to use `Numo::Optimize.minimize`.
|
211
|
+
*
|
212
|
+
* @overload fmin(fnc, x, jcb, args, l, u, nbd, maxcor, ftol, gtol, maxiter, disp)
|
213
|
+
* @param fnc [Method/Proc]
|
214
|
+
* @param x [Numo::DFloat]
|
215
|
+
* @param jcb [Method/Proc/boolean]
|
216
|
+
* @param args [Object]
|
217
|
+
* @param l [Numo::DFloat]
|
218
|
+
* @param u [Numo::DFloat]
|
219
|
+
* @param nbd [Numo::Int32/Numo::Int64]
|
220
|
+
* @param maxcor [Integer]
|
221
|
+
* @param ftol [Float]
|
222
|
+
* @param gtol [Float]
|
223
|
+
* @param maxiter [Integer]
|
224
|
+
* @param disp [Integer/nil]
|
225
|
+
* @return [Hash{Symbol => Object}]
|
226
|
+
*/
|
227
|
+
rb_define_module_function(rb_mLbfgsb, "fmin", lbfgsb_fmin, 12);
|
228
|
+
}
|
@@ -0,0 +1,71 @@
|
|
1
|
+
3-clause license ("New BSD License" or "Modified BSD License")
|
2
|
+
New BSD License
|
3
|
+
Author Regents of the University of California
|
4
|
+
Publisher Public Domain
|
5
|
+
Published July 22, 1999[8]
|
6
|
+
DFSG compatible Yes[7]
|
7
|
+
FSF approved Yes[1]
|
8
|
+
OSI approved Yes[3]
|
9
|
+
GPL compatible Yes[1]
|
10
|
+
Copyleft No[1]
|
11
|
+
Copyfree Yes
|
12
|
+
Linking from code with a different license Yes
|
13
|
+
|
14
|
+
The advertising clause was removed from the license text in the official BSD on July 22, 1999 by William Hoskins, Director of
|
15
|
+
the Office of Technology Licensing for UC Berkeley.[8] Other BSD distributions removed the clause, but many similar clauses
|
16
|
+
remain in BSD-derived code from other sources, and unrelated code using a derived license.
|
17
|
+
|
18
|
+
While the original license is sometimes referred to as "BSD-old", the resulting 3-clause version is sometimes referred to by
|
19
|
+
"BSD-new." Other names include "New BSD", "revised BSD", "BSD-3", or "3-clause BSD". This version has been vetted as
|
20
|
+
an Open source license by the OSI as the "The BSD License".[3] The Free Software Foundation, which refers to the license
|
21
|
+
as the "Modified BSD License", states that it is compatible with the GNU GPL. The FSF encourages users to be specific
|
22
|
+
when referring to the license by name (i.e. not simply referring to it as "a BSD license" or "BSD-style") to avoid confusion with
|
23
|
+
the original BSD license.[1]
|
24
|
+
|
25
|
+
This version allows unlimited redistribution for any purpose as long as its copyright notices and the license's disclaimers of
|
26
|
+
warranty are maintained. The license also contains a clause restricting use of the names of contributors for endorsement of a
|
27
|
+
derived work without specific permission.
|
28
|
+
|
29
|
+
Copyright (c) <year>, <copyright holder>
|
30
|
+
All rights reserved.
|
31
|
+
|
32
|
+
Redistribution and use in source and binary forms, with or without
|
33
|
+
modification, are permitted provided that the following conditions are met:
|
34
|
+
* Redistributions of source code must retain the above copyright
|
35
|
+
notice, this list of conditions and the following disclaimer.
|
36
|
+
* Redistributions in binary form must reproduce the above copyright
|
37
|
+
notice, this list of conditions and the following disclaimer in the
|
38
|
+
documentation and/or other materials provided with the distribution.
|
39
|
+
* Neither the name of the <organization> nor the
|
40
|
+
names of its contributors may be used to endorse or promote products
|
41
|
+
derived from this software without specific prior written permission.
|
42
|
+
|
43
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
44
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
45
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
46
|
+
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
47
|
+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
48
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
49
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
50
|
+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
51
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
52
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
53
|
+
|
54
|
+
|
55
|
+
References
|
56
|
+
|
57
|
+
1. ^ a b c d e "Various Licenses and Comments about Them - GNU Project - Free Software Foundation (FSF): Modified BSD license".
|
58
|
+
Free Software Foundation. Retrieved 02 October 2010.
|
59
|
+
2. ^ a b c d e "Various Licenses and Comments about Them - GNU Project - Free Software Foundation (FSF): FreeBSD license".
|
60
|
+
Free Software Foundation. Retrieved 02 October 2010.
|
61
|
+
3. ^ a b c d e f "Open Source Initiative OSI - The BSD License:Licensing". Open Source Initiative. Retrieved 06 December 2009.
|
62
|
+
4. ^ a b c d "Various Licenses and Comments about Them - GNU Project - Free Software Foundation (FSF): Original BSD license".
|
63
|
+
Free Software Foundation. Retrieved 02 October 2010.
|
64
|
+
5. ^ The year given is the year 4.3BSD-Tahoe was released. Whether this is the first use of the license is not known.
|
65
|
+
6. ^ The year given is the year 4.3BSD-Reno was released. Whether this is the first use of the license is not known.
|
66
|
+
7. ^ a b "Debian -- License information". Debian. Retrieved 18 February 2010.
|
67
|
+
8. ^ a b c "To All Licensees, Distributors of Any Version of BSD". University of California, Berkeley. 1999-07-22. Retrieved
|
68
|
+
2006-11-15.
|
69
|
+
9. ^ Richard Stallman. "The BSD License Problem". Free Software Foundation. Retrieved 2006-11-15.
|
70
|
+
10. ^ "The FreeBSD Copyright". The FreeBSD Project. Retrieved 6 December 2009.
|
71
|
+
11. ^ "NetBSD Licensing and Redistribution". The NetBSD Foundation. Retrieved 06 December 2009.
|