Skip to content

Commit

Permalink
Initial NLopt 2.7.1 import
Browse files Browse the repository at this point in the history
  • Loading branch information
relf committed Oct 8, 2023
0 parents commit 052a636
Show file tree
Hide file tree
Showing 19 changed files with 6,946 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Set update schedule for GitHub Actions

version: 2
updates:

- package-ecosystem: "github-actions"
directory: "/"
schedule:
# Check for updates to GitHub Actions every week
interval: "weekly"
35 changes: 35 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: tests

on: [push, pull_request]

env:
CARGO_TERM_COLOR: always

jobs:
testing:
name: testing-${{ matrix.toolchain }}-${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
toolchain:
- stable
os:
- ubuntu-latest
- windows-latest

steps:
- name: Checkout sources
uses: actions/checkout@v4

- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.toolchain }}
override: true

- name: Run cargo test in release mode
uses: actions-rs/cargo@v1
with:
command: test
args: --all --release
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Generated by Cargo
# will have compiled files and executables
/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk

/build
21 changes: 21 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
authors = ["Remi Lafage <[email protected]>"]
name = "slsqp"
version = "0.0.1"
edition = "2021"
license-file = "LICENSE.md"
description = "SLSQP optimizer for Rust"
readme = "README.md"
repository = "https://github.com/relf/slsqp/"
keywords = ["optimizer", "optimization", "constrained", "derivative-free"]
categories = ["algorithms", "api-bindings", "science"]
documentation = "https://docs.rs/slsqp"

[dependencies]
libc = "0.2"
argmin = "0.8.0"
serde = "1"
instant = "0.1"

[dev-dependencies]
approx = "0.5"
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# slsqp

[![tests](https://github.com/relf/slsqp/workflows/tests/badge.svg)](https://github.com/relf/slsqp/actions?query=workflow%3Atests)
[![crates.io](https://img.shields.io/crates/v/slsqp)](https://crates.io/crates/slsqp)
[![docs](https://docs.rs/slsqp/badge.svg)](https://docs.rs/slsqp)

## slsqp 0.1.x

Rust wrapper for SLSQP optimizer (SLSQP stands for ).
slsqp Rust code was generated from NLopt 2.7.1 slsqp C code thanks to c2rust trabspiler then
manually edited to make it work.

44 changes: 44 additions & 0 deletions slsqp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
cmake_minimum_required (VERSION 3.0.0)
project (slsqp)

include (CheckIncludeFiles)
include (CheckFunctionExists)
include (CheckTypeSize)
include (CheckCCompilerFlag)
include (CheckCXXSymbolExists)
include (CheckCSourceCompiles)
include (CheckCXXCompilerFlag)
include (CheckLibraryExists)

check_include_file (getopt.h HAVE_GETOPT_H)
check_include_file (unistd.h HAVE_UNISTD_H)
check_include_file (stdint.h HAVE_STDINT_H)
check_include_file (time.h HAVE_TIME_H)
check_include_file (sys/time.h HAVE_SYS_TIME_H)
if (HAVE_TIME_H AND HAVE_SYS_TIME_H)
set (TIME_WITH_SYS_TIME TRUE)
endif ()
check_function_exists (getpid HAVE_GETPID)
check_function_exists (syscall HAVE_GETTID_SYSCALL)
check_function_exists (isinf HAVE_ISINF)
check_function_exists (isnan HAVE_ISNAN)
check_function_exists (gettimeofday HAVE_GETTIMEOFDAY)
check_function_exists (qsort_r HAVE_QSORT_R)
check_function_exists (time HAVE_TIME)
check_function_exists (copysign HAVE_COPYSIGN)
check_function_exists (getopt HAVE_GETOPT)
check_type_size ("uint32_t" SIZEOF_UINT32_T)
set (HAVE_UINT32_T ${SIZEOF_UINT32_T})
check_type_size ("unsigned int" SIZEOF_UNSIGNED_INT)
check_type_size ("unsigned long" SIZEOF_UNSIGNED_LONG)

check_library_exists ("m" sqrt "" HAVE_LIBM)
if (HAVE_LIBM)
set (M_LIBRARY m)
endif()

configure_file (${CMAKE_CURRENT_SOURCE_DIR}/nlopt_config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/nlopt_config.h IMMEDIATE)

add_library (slsqp slsqp.c)

target_include_directories (slsqp PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
11 changes: 11 additions & 0 deletions slsqp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
To generate compile commands json file required by c2rust in top directory

> mkdir build
> cd build
> cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 ../slsqp
> cp compile_commands.json ../slsqp/compile_commands.json
Then rust generation from C

> cd ../slsqp
> c2rust transpile compile_commands.json
7 changes: 7 additions & 0 deletions slsqp/compile_commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"directory": "/home/rlafage/workspace/slsqp/build",
"command": "/usr/bin/cc -I/home/rlafage/workspace/slsqp/slsqp -o CMakeFiles/slsqp.dir/slsqp.c.o -c /home/rlafage/workspace/slsqp/slsqp/slsqp.c",
"file": "/home/rlafage/workspace/slsqp/slsqp/slsqp.c"
}
]
140 changes: 140 additions & 0 deletions slsqp/nlopt-util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/* Copyright (c) 2007-2014 Massachusetts Institute of Technology
*
* 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 NLOPT_UTIL_H
#define NLOPT_UTIL_H

#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
#include "nlopt_config.h"

#include "nlopt.h"

/* workaround for Solaris + gcc 3.4.x bug (see configure.ac) */
#if defined(__GNUC__) && defined(REPLACEMENT_HUGE_VAL)
# undef HUGE_VAL
# define HUGE_VAL REPLACEMENT_HUGE_VAL
#endif

#ifndef HAVE_COPYSIGN
/* not quite right for y == -0, but good enough for us */
# define copysign(x, y) ((y) < 0 ? -fabs(x) : fabs(x))
#elif !defined(__cplusplus) && __STDC_VERSION__ < 199901
extern double copysign(double x, double y); /* may not be declared in C89 */
#endif

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

int nlopt_isinf(double x);
int nlopt_isfinite(double x);
int nlopt_istiny(double x);
int nlopt_isnan(double x);

/* re-entrant qsort, uses the BSD convention */
extern void nlopt_qsort_r(void *base_, size_t nmemb, size_t size, void *thunk, int (*compar) (void *, const void *, const void *));

/* seconds timer */
extern double nlopt_seconds(void);
extern unsigned long nlopt_time_seed(void);

/* pseudorandom number generation by Mersenne twister algorithm */
extern void nlopt_init_genrand(unsigned long s);
extern double nlopt_urand(double a, double b);
extern int nlopt_iurand(int n);
extern double nlopt_nrand(double mean, double stddev);

/* Sobol' low-discrepancy-sequence generation */
typedef struct nlopt_soboldata_s *nlopt_sobol;
extern nlopt_sobol nlopt_sobol_create(unsigned sdim);
extern void nlopt_sobol_destroy(nlopt_sobol s);
extern void nlopt_sobol_next01(nlopt_sobol s, double *x);
extern void nlopt_sobol_next(nlopt_sobol s, double *x, const double *lb, const double *ub);
extern void nlopt_sobol_skip(nlopt_sobol s, unsigned n, double *x);

/* stopping criteria */
typedef struct {
unsigned n;
double minf_max;
double ftol_rel;
double ftol_abs;
double xtol_rel;
const double *xtol_abs;
const double *x_weights;
int *nevals_p, maxeval;
double maxtime, start;
int *force_stop;
char **stop_msg; /* pointer to msg string to update */
} nlopt_stopping;
extern int nlopt_stop_f(const nlopt_stopping * stop, double f, double oldf);
extern int nlopt_stop_ftol(const nlopt_stopping * stop, double f, double oldf);
extern int nlopt_stop_x(const nlopt_stopping * stop, const double *x, const double *oldx);
extern int nlopt_stop_dx(const nlopt_stopping * stop, const double *x, const double *dx);
extern int nlopt_stop_xs(const nlopt_stopping * stop, const double *xs, const double *oldxs, const double *scale_min, const double *scale_max);
extern int nlopt_stop_evals(const nlopt_stopping * stop);
extern int nlopt_stop_time_(double start, double maxtime);
extern int nlopt_stop_time(const nlopt_stopping * stop);
extern int nlopt_stop_evalstime(const nlopt_stopping * stop);
extern int nlopt_stop_forced(const nlopt_stopping * stop);

/* like vsprintf, but reallocs p to whatever size is needed */
extern char *nlopt_vsprintf(char *p, const char *format, va_list ap);
extern void nlopt_stop_msg(const nlopt_stopping * s, const char *format, ...)
#ifdef __GNUC__
__attribute__ ((format(printf, 2, 3)))
#endif
;

/* for local optimizations, temporarily setting eval/time limits */
extern nlopt_result nlopt_optimize_limited(nlopt_opt opt, double *x, double *minf, int maxevals, double maxtime);

/* data structure for nonlinear inequality or equality constraint
(f <= 0 or f = 0, respectively). tol (>= 0) is a tolerance
that is used for stopping criteria -- the point is considered
"feasible" for purposes of stopping if the constraint is violated
by at most tol. */
typedef struct {
unsigned m; /* dimensional of constraint: mf maps R^n -> R^m */
nlopt_func f; /* one-dimensional constraint, requires m == 1 */
nlopt_mfunc mf;
nlopt_precond pre; /* preconditioner for f (NULL if none or if mf) */
void *f_data;
double *tol;
} nlopt_constraint;

extern unsigned nlopt_count_constraints(unsigned p, const nlopt_constraint * c);
extern unsigned nlopt_max_constraint_dim(unsigned p, const nlopt_constraint * c);
extern void nlopt_eval_constraint(double *result, double *grad, const nlopt_constraint * c, unsigned n, const double *x);

/* rescale.c: */
double *nlopt_compute_rescaling(unsigned n, const double *dx);
double *nlopt_new_rescaled(unsigned n, const double *s, const double *x);
void nlopt_rescale(unsigned n, const double *s, const double *x, double *xs);
void nlopt_unscale(unsigned n, const double *s, const double *x, double *xs);
void nlopt_reorder_bounds(unsigned n, double *lb, double *ub);

#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */
#endif
Loading

0 comments on commit 052a636

Please sign in to comment.