Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/v-sekai-godot/vsk-lbfgs-4.4' in…
Browse files Browse the repository at this point in the history
…to groups-staging-4.4
  • Loading branch information
fire committed Jan 28, 2025
2 parents 0325029 + 8ee3664 commit ab75417
Show file tree
Hide file tree
Showing 340 changed files with 125,384 additions and 0 deletions.
12 changes: 12 additions & 0 deletions modules/lbfgs/.gitrepo
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
; DO NOT EDIT (unless you know what you are doing)
;
; This subdirectory is a git "subrepo", and this file is maintained by the
; git-subrepo command. See https://github.com/ingydotnet/git-subrepo#readme
;
[subrepo]
remote = https://github.com/V-Sekai/godot-lbfgs.git
branch = main
commit = d180824044da5cc6cc831edfcbbee3f88ac709c7
parent = 66a8c486f9e525ad027248b1b170da7af7057df9
method = merge
cmdver = 0.4.9
21 changes: 21 additions & 0 deletions modules/lbfgs/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023-present K. S. Ernest (iFire) Lee

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
21 changes: 21 additions & 0 deletions modules/lbfgs/SCsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env python
from misc.utility.scons_hints import *

Import("env")
Import("env_modules")

env_thirdparty = env.Clone()
env_thirdparty.Append(CPPDEFINES=[("EIGEN_HAS_STD_RESULT_OF", 0)])
env_thirdparty.Prepend(CPPPATH=["thirdparty/eigen"])
env_thirdparty.Prepend(CPPPATH=["#modules/lbfgs"])
env_thirdparty.Prepend(CPPPATH=["#modules/lbfgs/src"])
env_thirdparty.Prepend(CPPPATH=["thirdparty/LBFGSpp/include"])

env_thirdparty.disable_warnings()
env_lbfgs = env_thirdparty.Clone()

env_lbfgs.Append(CPPDEFINES=["TASKING_INTERNAL"])
env_lbfgs.Append(CPPDEFINES=["_USE_MATH_DEFINES"])
env_lbfgs.add_source_files(env.modules_sources, "thirdparty/*.cpp")

env_thirdparty.add_source_files(env.modules_sources, "*.cpp")
14 changes: 14 additions & 0 deletions modules/lbfgs/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def can_build(env, platform):
return not env["disable_3d"]


def configure(env):
pass


def get_doc_classes():
return ["LBFGSBSolver"]


def get_doc_path():
return "doc_classes"
31 changes: 31 additions & 0 deletions modules/lbfgs/doc_classes/LBFGSBSolver.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="LBFGSBSolver" inherits="RefCounted" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../doc/class.xsd">
<brief_description>
The LBFGSBSolver class provides an implementation of the L-BFGSB optimization algorithm.
</brief_description>
<description>
L-BFGSB (Limited-memory Broyden-Fletcher-Goldfarb-Shanno with Bounds) is a variant of the L-BFGS optimization algorithm that supports bound constraints. It is not as robust as L-BFGS and has a time complexity of O(N*M), where N is the number of variables and M is the number of iterations.
</description>
<tutorials>
</tutorials>
<methods>
<method name="_call_operator" qualifiers="virtual">
<return type="float" />
<param index="0" name="x" type="float[]" />
<param index="1" name="gradient" type="float[]" />
<description>
This method is used to calculate the function value and its gradient at a given point x.
</description>
</method>
<method name="minimize">
<return type="Array" />
<param index="0" name="x" type="float[]" />
<param index="1" name="fx" type="float" />
<param index="2" name="lower_bound" type="float[]" />
<param index="3" name="upper_bound" type="float[]" />
<description>
This method is used to find the minimum of the function starting from the initial guess x, subject to the given lower and upper bounds.
</description>
</method>
</methods>
</class>
45 changes: 45 additions & 0 deletions modules/lbfgs/register_types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**************************************************************************/
/* register_types.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "register_types.h"

#include "thirdparty/lbfgsbpp.h"

void initialize_lbfgs_module(ModuleInitializationLevel p_level) {
if (p_level == MODULE_INITIALIZATION_LEVEL_SERVERS) {
GDREGISTER_CLASS(LBFGSBSolver);
}
}

void uninitialize_lbfgs_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
}
39 changes: 39 additions & 0 deletions modules/lbfgs/register_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**************************************************************************/
/* register_types.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef LBFGS_REGISTER_TYPES_H
#define LBFGS_REGISTER_TYPES_H

#include "modules/register_module_types.h"

void initialize_lbfgs_module(ModuleInitializationLevel p_level);
void uninitialize_lbfgs_module(ModuleInitializationLevel p_level);

#endif // LBFGS_REGISTER_TYPES_H
67 changes: 67 additions & 0 deletions modules/lbfgs/tests/llfgsbpp.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
@tool
extends EditorScript

class Rosenbrock:
extends LBFGSBSolver
var n : int

func _init(n_: int):
self.n = n_

func _call_operator(x: Array, grad: Array) -> float:
var fx : float = pow((x[0] - 1.0), 2)
grad[0] = 2 * (x[0] - 1) + 16 * (pow(x[0], 2) - x[1]) * x[0]

for i in range(1, self.n):
fx += 4 * pow((x[i] - pow(x[i - 1], 2)), 2)

if(i == self.n - 1):
grad[i] = 8 * (x[i] - pow(x[i - 1], 2))
else:
grad[i] = 8 * (x[i] - pow(x[i - 1], 2)) + 16 * (pow(x[i], 2) - x[i + 1]) * x[i]

return fx


func _run():
var n : int = 25
var rosenbrock = Rosenbrock.new(n)

var lb : Array[float] = []
var ub : Array[float] = []
var x : Array[float] = []

for i in range(n):
lb.append(2.0)
ub.append(4.0)
x.append(3.0)

lb[2] = -INF
ub[2] = INF

x[0] = 2.0
x[1] = 2.0
x[5] = 4.0
x[7] = 4.0

var res: Array = rosenbrock.minimize(x, 0, lb, ub)

print(str(n) + " iterations")
print("x = " + str(res[0]))
print("f(x) = " + str(res[1]))
print("fx = " + str(res[2]))

# Output

# 25 iterations
# x = 13
# f(x) = [2, 2, 1.64742666556789, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2.10909336539027, 4]
# fx = 360.283585551151

# Example

# 13 iterations
# x = 2 2 1.64743 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2.10909 4
# f(x) = 360.284
# grad = 66 59.2823 8.76499e-07 58.2879 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 48 44.509 2.23559e-06 -3.5862
# projected grad norm = 2.23559e-06
9 changes: 9 additions & 0 deletions modules/lbfgs/thirdparty/LBFGSpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.o
*.out
include/Eigen/*
archive/*
.settings/*
.project
.cproject
/Debug/
/Release/
12 changes: 12 additions & 0 deletions modules/lbfgs/thirdparty/LBFGSpp/AUTHORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
The LBFGS++ library was adapted from the libLBFGS library
(https://github.com/chokkan/liblbfgs), written by
Naoaki Okazaki <<okazaki@c.titech.ac.jp>>.

The files

- `include/LBFGS/LineSearchBracketing.h`
- `include/LBFGS/LineSearchNocedalWright.h`

were contributed by Dirk Toewe <<DirkToewe@GoogleMail.com>>.

Other part of LBFGS++ was written by Yixuan Qiu <<yixuan.qiu@cos.name>>.
49 changes: 49 additions & 0 deletions modules/lbfgs/thirdparty/LBFGSpp/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## Unreleased

### Added

- Added functions `final_grad()` and `final_grad_norm()` to `LBFGSSolver`
and `LBFGSBSolver` to retrieve the final gradient information
([#12](https://github.com/yixuan/LBFGSpp/issues/12))

### Changed

- `LBFGS++` now requires C++11
- The line search classes now have a unified API for both `LBFGSSolver` and `LBFGSBSolver`
- The Moré-Thuente line search algorithm `LineSearchMoreThuente` now can also be used
in the L-BFGS solver `LBFGSSolver`
- Improved the numerical stability of `LineSearchNocedalWright`
([#27](https://github.com/yixuan/LBFGSpp/issues/27))
- Removed the unused variable `dg_hi` in `LineSearchNocedalWright`
([#35](https://github.com/yixuan/LBFGSpp/issues/35))



## [0.2.0] - 2022-05-20

### Added

- Added a CMake script for installation ([#24](https://github.com/yixuan/LBFGSpp/pull/24)),
contributed by [@steinmig](https://github.com/steinmig)

### Changed

- The default line search method for `LBFGSSolver` has been changed from `LineSearchBacktracking`
to `LineSearchNocedalWright`, per the suggestion of [@mpayrits](https://github.com/mpayrits)
([#25](https://github.com/yixuan/LBFGSpp/pull/25))
- Fixed a few critical issues ([#9](https://github.com/yixuan/LBFGSpp/issues/9),
[#15](https://github.com/yixuan/LBFGSpp/issues/15),
[#21](https://github.com/yixuan/LBFGSpp/issues/21)), with big thanks to
[@mpayrits](https://github.com/mpayrits) ([#25](https://github.com/yixuan/LBFGSpp/pull/25))
- Fixed one inconsistency with Moré and Thuente (1994) in the `LineSearchMoreThuente`
line search algorithm, pointed out by [@mpayrits](https://github.com/mpayrits)
([#23](https://github.com/yixuan/LBFGSpp/issues/23))
- The source code is now formatted using [Clang-Format](https://clang.llvm.org/docs/ClangFormat.html)



## [0.1.0] - 2021-08-19

### Added

- Initial Github release
Loading

0 comments on commit ab75417

Please sign in to comment.