Repository: conan-io/conan-center-index

Issue:
[request] ensmallen/2.17.0
### Package Details
  * Package Name/Version: **ensmallen/2.17.0**
  * Website: **https://ensmallen.org/**
  * Source code: 
      * **https://ensmallen.org/files/ensmallen-2.17.0.tar.gz**
      * **https://github.com/mlpack/ensmallen**


### Description Of The Library / Tool
ensmallen provides a simple set of abstractions for writing an objective function to optimize. It also provides a large set of standard and cutting-edge optimizers that can be used for virtually any numerical optimization task. These include full-batch gradient descent techniques, small-batch techniques, gradient-free optimizers, and constrained optimization


Discussion:

Resolving patch:
diff --git a/recipes/ensmallen/all/conanfile.py b/recipes/ensmallen/all/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/ensmallen/all/conanfile.py
@@ -0,0 +1,69 @@
+from conan import ConanFile, conan_version
+from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout, CMakeDeps
+from conan.tools.files import get, replace_in_file, rmdir, copy
+from conan.tools.scm import Version
+
+import os
+
+required_conan_version = ">=1.55.0"
+
+class ensmallenRecipe(ConanFile):
+    name = "ensmallen"
+    description = "ensmallen is a high quality C++ library for non-linear numerical optimization."
+    license = "BSD-3-Clause"
+    url = "https://github.com/conan-io/conan-center-index"
+    homepage = "https://github.com/mlpack/ensmallen"
+    topics = ("optimization", "numerical", "header-only")
+
+    package_type = "header-library"
+    settings = "os", "compiler", "build_type", "arch"
+
+    def package_id(self):
+        self.info.clear()
+
+    def layout(self):
+        cmake_layout(self, src_folder="src")
+
+    def requirements(self):
+        self.requires("armadillo/12.2.0")
+
+    def source(self):
+        get(self, **self.conan_data["sources"][self.version], strip_root=True)
+
+    def generate(self):
+        deps = CMakeDeps(self)
+        deps.set_property("armadillo", "cmake_file_name", "Armadillo")
+        deps.set_property("armadillo", "cmake_target_name", "Armadillo::Armadillo")
+        deps.set_property("armadillo", "cmake_config_version_compat", "AnyNewerVersion")
+        deps.generate()
+        tc = CMakeToolchain(self)
+        tc.variables["USE_OPENMP"] = False
+        tc.generate()
+
+
+    def build(self):
+        # TODO: Remove when conan 1.x support is dropped. This is largely resolved by the above
+        # specification of AnyNewerVersion for the compatibility policy, but this feature isn't
+        # available below version 2.0.12.
+        if conan_version < Version("2.0.12"):
+            # Remove hard requirement on armadillo 9.800.0
+            # This is a minimum requirement, use latest
+            replace_in_file(
+                self,
+                os.path.join(self.source_folder, "CMakeLists.txt"),
+                "find_package(Armadillo 9.800.0 REQUIRED)",
+                "find_package(Armadillo REQUIRED)",
+            )
+        cmake = CMake(self)
+        cmake.configure()
+
+    def package(self):
+        cmake = CMake(self)
+        cmake.install()
+
+        copy(self, "LICENSE.txt", dst=os.path.join(self.package_folder, "licenses"), src=self.source_folder)
+        rmdir(self, os.path.join(self.package_folder, "lib"))
+
+    def package_info(self):
+        self.cpp_info.bindirs = []
+        self.cpp_info.libdirs = []


Test patch:
diff --git a/recipes/ensmallen/all/test_package/CMakeLists.txt b/recipes/ensmallen/all/test_package/CMakeLists.txt
new file mode 100644
--- /dev/null
+++ b/recipes/ensmallen/all/test_package/CMakeLists.txt
@@ -0,0 +1,8 @@
+cmake_minimum_required(VERSION 3.15)
+project(PackageTest CXX)
+
+set(CMAKE_CXX_STANDARD 11)
+find_package(ensmallen CONFIG REQUIRED)
+
+add_executable(example src/example.cpp)
+target_link_libraries(example ensmallen::ensmallen)
diff --git a/recipes/ensmallen/all/test_package/conanfile.py b/recipes/ensmallen/all/test_package/conanfile.py
new file mode 100644
--- /dev/null
+++ b/recipes/ensmallen/all/test_package/conanfile.py
@@ -0,0 +1,30 @@
+import os
+
+from conan import ConanFile
+from conan.tools.cmake import CMake, cmake_layout
+from conan.tools.build import can_run
+
+class ensmallenTestConan(ConanFile):
+    settings = "os", "compiler", "build_type", "arch"
+    generators = "CMakeDeps", "CMakeToolchain"
+
+    def requirements(self):
+        self.requires(self.tested_reference_str)
+        # Required for consumers of armadillo>=12.2.0 due to upstream changes
+        # and armadillo/*:use_hdf5=True by default.
+        # See https://github.com/conan-io/conan-center-index/pull/17320 for more
+        # information.
+        self.requires("hdf5/1.14.1")
+
+    def build(self):
+        cmake = CMake(self)
+        cmake.configure()
+        cmake.build()
+
+    def layout(self):
+        cmake_layout(self)
+
+    def test(self):
+        if can_run(self):
+            cmd = [os.path.join(self.cpp.build.bindir, "example"), "10", "20"]
+            self.run(" ".join(cmd), env="conanrun")
diff --git a/recipes/ensmallen/all/test_package/src/example.cpp b/recipes/ensmallen/all/test_package/src/example.cpp
new file mode 100644
--- /dev/null
+++ b/recipes/ensmallen/all/test_package/src/example.cpp
@@ -0,0 +1,70 @@
+// Example implementation of an objective function class for linear regression
+// and usage of the L-BFGS optimizer.
+// 
+// Compilation:
+// g++ example.cpp -o example -O3 -larmadillo
+
+
+#include <iostream>
+#include <armadillo>
+#include <ensmallen.hpp>
+
+
+class LinearRegressionFunction
+{
+  public:
+  
+  LinearRegressionFunction(arma::mat& X, arma::vec& y) : X(X), y(y) { }
+  
+  double EvaluateWithGradient(const arma::mat& theta, arma::mat& gradient)
+  {
+    const arma::vec tmp = X.t() * theta - y;
+    gradient = 2 * X * tmp;
+    return arma::dot(tmp,tmp);
+  }
+  
+  private:
+  
+  const arma::mat& X;
+  const arma::vec& y;
+};
+
+
+int main(int argc, char** argv)
+{
+  if (argc < 3)
+  {
+    std::cout << "usage: " << argv[0] << " n_dims n_points" << std::endl;
+    return -1;
+  }
+  
+  int n_dims   = atoi(argv[1]);
+  int n_points = atoi(argv[2]);
+  
+  // generate noisy dataset with a slight linear pattern
+  arma::mat X(n_dims, n_points, arma::fill::randu);
+  arma::vec y(        n_points, arma::fill::randu);
+  
+  for (size_t i = 0; i < n_points; ++i)
+  {
+    double a = arma::randu();
+    X(1, i) += a;
+    y(i)    += a;
+  }
+  
+  LinearRegressionFunction lrf(X, y);
+  
+  // create a Limited-memory BFGS optimizer object with default parameters
+  ens::L_BFGS opt;
+  opt.MaxIterations() = 10;
+  
+  // initial point (uniform random)
+  arma::vec theta(n_dims, arma::fill::randu);
+  
+  opt.Optimize(lrf, theta);
+  
+  // theta now contains the optimized parameters
+  theta.print("theta:");
+  
+  return 0;
+}