Skip to content

Commit

Permalink
[workspace] Add libcmaes as a testonly dependency (#16441)
Browse files Browse the repository at this point in the history
In the future, we might use this in production, but for getting started
we only need it in the sample test.
  • Loading branch information
jwnimmer-tri authored Jan 27, 2022
1 parent d8a1180 commit ed5fdb0
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tools/workspace/default.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ load("@drake//tools/workspace/json:repository.bzl", "json_repository")
load("@drake//tools/workspace/lapack:repository.bzl", "lapack_repository")
load("@drake//tools/workspace/lcm:repository.bzl", "lcm_repository")
load("@drake//tools/workspace/libblas:repository.bzl", "libblas_repository")
load("@drake//tools/workspace/libcmaes:repository.bzl", "libcmaes_repository")
load("@drake//tools/workspace/libcurl:repository.bzl", "libcurl_repository")
load("@drake//tools/workspace/libjpeg:repository.bzl", "libjpeg_repository")
load("@drake//tools/workspace/liblapack:repository.bzl", "liblapack_repository") # noqa
Expand Down Expand Up @@ -191,6 +192,8 @@ def add_default_repositories(excludes = [], mirrors = DEFAULT_MIRRORS):
lcm_repository(name = "lcm", mirrors = mirrors)
if "libblas" not in excludes:
libblas_repository(name = "libblas")
if "libcmaes" not in excludes:
libcmaes_repository(name = "libcmaes", mirrors = mirrors)
if "libcurl" not in excludes:
libcurl_repository(name = "libcurl")
if "libjpeg" not in excludes:
Expand Down
5 changes: 5 additions & 0 deletions tools/workspace/libcmaes/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- python -*-

load("//tools/lint:lint.bzl", "add_lint_tests")

add_lint_tests()
12 changes: 12 additions & 0 deletions tools/workspace/libcmaes/dev/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# -*- python -*-

load("//tools/lint:lint.bzl", "add_lint_tests")

cc_test(
name = "sample_code",
srcs = ["sample_code.cc"],
tags = ["nolint"],
deps = ["@libcmaes"],
)

add_lint_tests()
53 changes: 53 additions & 0 deletions tools/workspace/libcmaes/dev/sample_code.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// From https://github.com/CMA-ES/libcmaes/blob/master/examples/sample-code.cc
// as of 89ebe138eae646f79f326e49944c4cff33106e4f, with slight modifications.
// This program serves to test the CMA-ES build system. Once we have any other
// uses of CMA-ES in Drake, we should remove this file.

/**
* CMA-ES, Covariance Matrix Adaptation Evolution Strategy
* Copyright (c) 2014 Inria
* Author: Emmanuel Benazera <emmanuel.benazera@lri.fr>
*
* This file is part of libcmaes.
*
* libcmaes is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libcmaes is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libcmaes. If not, see <http://www.gnu.org/licenses/>.
*/

#include <libcmaes/cmaes.h>
#include <iostream>

using namespace libcmaes;

FitFunc fsphere = [](const double *x, const int N)
{
double val = 0.0;
for (int i=0;i<N;i++)
val += x[i]*x[i];
return val;
};

int main()
{
int dim = 10; // problem dimensions.
std::vector<double> x0(dim,10.0);
double sigma = 0.1;
//int lambda = 100; // offsprings at each generation.
CMAParameters<> cmaparams(x0,sigma);
//cmaparams._algo = BIPOP_CMAES;
CMASolutions cmasols = cmaes<>(fsphere,cmaparams);
std::cout << "best solution: " << cmasols << std::endl;
std::cout << "optimization took " << cmasols.elapsed_time() / 1000.0 << " seconds\n";
std::cout << "status " << cmasols.run_status() << "\n";
return 0;
}
70 changes: 70 additions & 0 deletions tools/workspace/libcmaes/package.BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# -*- python -*-

load(
"@drake//tools/workspace:cmake_configure_file.bzl",
"cmake_configure_file",
)
load(
"@drake//tools/workspace:generate_export_header.bzl",
"generate_export_header",
)

licenses(["notice"]) # Apache-2.0

package(
# TODO(jwnimmer-tri) At the time when libdrake depends on libcmaes, we
# will need to change this to public, add the rules to install the
# license file(s), and add it to the drake/tools/wheel build.
default_visibility = ["@//tools/workspace/libcmaes/dev:__pkg__"],
)

cmake_configure_file(
name = "config",
src = "include/libcmaes/libcmaes_config.h.cmake.in",
out = "include/libcmaes/libcmaes_config.h",
defines = [
"HAVE_CXX11",
"HAVE_INTTYPES_H",
"HAVE_MEMORY_H",
"HAVE_STDINT_H",
"HAVE_STDLIB_H",
"HAVE_STRINGS_H",
"HAVE_STRING_H",
"HAVE_UNISTD_H",
"LIBCMAES_VERSION_STRING=0.0.0",
],
visibility = ["//visibility:private"],
)

generate_export_header(
out = "include/libcmaes/cmaes_export.h",
lib = "cmaes",
static_define = "LIBCMAES_STATIC_DEFINE",
visibility = ["//visibility:private"],
)

cc_library(
name = "libcmaes",
srcs = glob([
"src/*.cc",
], exclude = [
"src/surrogatestrategy.cc",
]),
hdrs = [
":include/libcmaes/libcmaes_config.h",
":include/libcmaes/cmaes_export.h",
] + glob([
"include/libcmaes/*.h",
], exclude = [
"surrcmaes.h",
"surrogatestrategy.h",
]),
copts = [
# Ignore OpenMP-related warnings.
"-Wno-unknown-pragmas",
],
defines = ["LIBCMAES_STATIC_DEFINE"],
deps = ["@eigen"],
includes = ["include"],
linkstatic = True,
)
19 changes: 19 additions & 0 deletions tools/workspace/libcmaes/repository.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- python -*-

load("@drake//tools/workspace:github.bzl", "github_archive")

def libcmaes_repository(
name,
mirrors = None):
github_archive(
name = name,
repository = "CMA-ES/libcmaes",
# TODO(jwnimmer-tri) We use an untagged commit, in order to use the
# Apache-2.0 license. Any time we upgrade this to a newer commit, we
# should check if there is an official version number yet that we
# could use (i.e., newer than v0.10).
commit = "1c39d7f931b267117d365370c6da5334a6948942",
sha256 = "0abb557242da5a4b785e11aec1c00e57d583f23f04579921da9110206d0a6767", # noqa
build_file = "@drake//tools/workspace/libcmaes:package.BUILD.bazel",
mirrors = mirrors,
)

0 comments on commit ed5fdb0

Please sign in to comment.