diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 793e6052e..6cfd58202 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,6 +13,7 @@ repos: rev: v6.0.0 hooks: - id: trailing-whitespace + exclude: '^c/pcg64/LICENSE\.md$' - id: check-yaml - id: check-symlinks - id: check-merge-conflict diff --git a/COPYRIGHTS b/COPYRIGHTS index 51d08c970..0c50f0a8b 100644 --- a/COPYRIGHTS +++ b/COPYRIGHTS @@ -88,48 +88,14 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ==== -mt19937 +pcg64/pcg64.h -Copyright (c) 2003-2005, Jean-Sebastien Roy (js@jeannot.org) +PCG Random Number Generation for C. -The rk_random and rk_seed functions algorithms and the original design of -the Mersenne Twister RNG: +Copyright 2014 Melissa O'Neill +Copyright 2015 Robert Kern - Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - 3. The names of its contributors may not be used to endorse or promote - products derived from this software without specific prior written - permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER -OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -Original algorithm for the implementation of rk_interval function from -Richard J. Wagner's implementation of the Mersenne Twister RNG, optimised by -Magnus Jonsson. - -Constants used in the rk_double implementation by Isaku Wada. +The MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/c/Makefile b/c/Makefile index 81922b8d6..0bbd93b56 100644 --- a/c/Makefile +++ b/c/Makefile @@ -75,7 +75,6 @@ SRCS = avl.c \ main-hv.c \ main-hvapprox.c \ main-nondominated.c \ - mt19937/mt19937.c \ ndsort.c \ nondominated.c \ pareto.c \ @@ -105,9 +104,9 @@ HDRS = avl.h \ io.h \ io_priv.h \ libmoocore-config.h \ - mt19937/mt19937.h \ nondominated.h \ nondominated_kung.h \ + pcg64/pcg64.h \ pow_int.h \ rng.h \ sort.h \ @@ -117,7 +116,7 @@ HDRS = avl.h \ ziggurat_constants.h # Relative to root folder. -DIST_OTHER_FILES = git_version *.mk +DIST_OTHER_FILES = git_version *.mk pcg64/LICENSE.md pcg64/README.md DIST_ROOT_FILES = Makefile README.md LICENSE OBJS = $(SRCS:.c=.o) OBJS := $(OBJS:.cpp=.o) @@ -196,7 +195,7 @@ $(BINDIR)/dominatedsets$(EXE): dominatedsets.o nondominated.o $(BINDIR)/eaf$(EXE): eaf.o eaf_main.o eaf3d.o avl.o $(BINDIR)/epsilon$(EXE): epsilon.o nondominated.o $(BINDIR)/hv$(EXE): main-hv.o timer.o $(LIBHV_OBJS) -$(BINDIR)/hvapprox$(EXE): main-hvapprox.o timer.o hvapprox.o rng.o mt19937/mt19937.o nondominated.o +$(BINDIR)/hvapprox$(EXE): main-hvapprox.o timer.o hvapprox.o rng.o nondominated.o $(BINDIR)/igd$(EXE): igd.o nondominated.o $(BINDIR)/ndsort$(EXE): ndsort.o pareto.o nondominated.o $(LIBHV_OBJS) $(BINDIR)/nondominated$(EXE): main-nondominated.o timer.o nondominated.o @@ -217,7 +216,7 @@ hv.o: hv.h hv_priv.h hvc4d_priv.h sort.h libmoocore-config.h hv3dplus.o: hv_priv.h sort.h hv3d_priv.h avl_tiny.h hv4d.o: hv4d_priv.h hv_priv.h sort.h hv_contrib.o: hv.h libmoocore-config.h nondominated.h -hvapprox.o: hvapprox.h pow_int.h rng.h mt19937/mt19937.h +hvapprox.o: hvapprox.h pow_int.h rng.h pcg64/pcg64.h hvc3d.o: hv_priv.h sort.h hv3d_priv.h avl_tiny.h igd.o: cmdline.h io.h nondominated.h igd.h pow_int.h io.o: io.h io_priv.h @@ -228,7 +227,7 @@ ndsort.o: cmdline.h io.h nondominated.h hv.h nondominated.h: avl_tiny.h nondominated_kung.h nondominated.o: nondominated.h nondominated_kung.h pareto.o: nondominated.h -rng.o: rng.h mt19937/mt19937.h ziggurat_constants.h +rng.o: rng.h pcg64/pcg64.h ziggurat_constants.h timer.o: timer.h $(OBJS): common.h gcc_attribs.h config.h maxminclamp.h @@ -247,7 +246,7 @@ clean-obj: @$(RM) $(OBJS) $(HV_OBJS) $(HV_LIB) clean: clean-obj - @-$(RM) *.s *.i *.gcda mt19937/*.gcda + @-$(RM) *.s *.i *.gcda @-$(RM) config.status config.log @-$(RM) $(SHLIB) $(CXXSHLIB) @for exe in $(EXE_FILES); do \ @@ -332,7 +331,7 @@ dist: DEBUG=0 dist: CDEBUG= dist: all $(call MKDIR,../$(DIST_SRC) - @(rsync -rlpC --exclude=.svn $(DIST_SRC_FILES) ../$(DIST_SRC)/ \ + @(rsync -RrlpC --exclude=.svn $(DIST_SRC_FILES) ../$(DIST_SRC)/ \ && cd .. \ && tar cf - $(DIST_SRC) | gzip -f9 > $(DIST_SRC).tar.gz \ && rm -rf ./$(DIST_SRC)/* && rmdir ./$(DIST_SRC)/ \ diff --git a/c/NEWS.md b/c/NEWS.md index 317b10c1d..7dbca935c 100644 --- a/c/NEWS.md +++ b/c/NEWS.md @@ -2,6 +2,7 @@ ## 0.20 + * Use PCG64 for stochastic hypervolume calculations. Seeded results change. * radixsort.h, insort.h: New. * main-nondominated.c: Renamed from nondominated.c. * hv and hvapprox executables now use -U instead of -u for --union to be diff --git a/c/README.md b/c/README.md index 4dd10db26..6dba50c18 100644 --- a/c/README.md +++ b/c/README.md @@ -67,6 +67,10 @@ Most functions are implemented in headers such as `nondominated.h`. For other fu Functions exported by the library are marked with `MOOCORE_API`. +The stochastic hypervolume functions use NumPy's PCG64 implementation. A +given seed is reproducible, but produces different results from earlier +moocore versions and from NumPy's seed initialization. + Testsuite ========= diff --git a/c/mt19937/LICENSE.md b/c/mt19937/LICENSE.md deleted file mode 100644 index 377c4e36d..000000000 --- a/c/mt19937/LICENSE.md +++ /dev/null @@ -1,61 +0,0 @@ -# MT19937 - -Copyright (c) 2003-2005, Jean-Sebastien Roy (js@jeannot.org) - -The rk_random and rk_seed functions algorithms and the original design of -the Mersenne Twister RNG: - - Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura, - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - 3. The names of its contributors may not be used to endorse or promote - products derived from this software without specific prior written - permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER -OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, -EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR -PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING -NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -Original algorithm for the implementation of rk_interval function from -Richard J. Wagner's implementation of the Mersenne Twister RNG, optimised by -Magnus Jonsson. - -Constants used in the rk_double implementation by Isaku Wada. - -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. diff --git a/c/mt19937/mt19937.c b/c/mt19937/mt19937.c deleted file mode 100644 index 52134f153..000000000 --- a/c/mt19937/mt19937.c +++ /dev/null @@ -1,61 +0,0 @@ -/* - From https://github.com/numpy/numpy/blob/5b00a4d86134b9c190dccd519ba500af776f2ab0/numpy/random/src/mt19937/mt19937.c -*/ -#include "mt19937.h" - -void mt19937_seed(mt19937_state *state, uint32_t seed) { - int pos; - seed &= 0xffffffffUL; - - /* Knuth's PRNG as used in the Mersenne Twister reference implementation */ - for (pos = 0; pos < RK_STATE_LEN; pos++) { - state->key[pos] = seed; - seed = (1812433253UL * (seed ^ (seed >> 30)) + pos + 1) & 0xffffffffUL; - } - state->pos = RK_STATE_LEN; -} - -void mt19937_gen(mt19937_state *state) { - uint32_t y; - int i; - -/* Not sure how to fix these warnings:: - -mt19937/mt19937.c:90:53: warning: higher order bits are zeroes after implicit conversion [-Wimplicit-int-conversion] - 90 | state->key[i] = state->key[i + M] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A); - | ^~~~~~~~ ~ -mt19937/mt19937.c:94:59: warning: higher order bits are zeroes after implicit conversion [-Wimplicit-int-conversion] - 94 | state->key[i] = state->key[i + (M - N)] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A); - | ^~~~~~~~ ~ -mt19937/mt19937.c:97:55: warning: higher order bits are zeroes after implicit conversion [-Wimplicit-int-conversion] - 97 | state->key[N - 1] = state->key[M - 1] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A); - | ^~~~~~~~ ~ -*/ -#if defined(__clang__) -# pragma clang diagnostic push -# pragma clang diagnostic ignored "-Wimplicit-int-conversion" -#endif - - for (i = 0; i < _MT19937_N - _MT19937_M; i++) { - y = (state->key[i] & UPPER_MASK) | (state->key[i + 1] & LOWER_MASK); - state->key[i] = state->key[i + _MT19937_M] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A); - } - for (; i < _MT19937_N - 1; i++) { - y = (state->key[i] & UPPER_MASK) | (state->key[i + 1] & LOWER_MASK); - state->key[i] = state->key[i + (_MT19937_M - _MT19937_N)] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A); - } - y = (state->key[_MT19937_N - 1] & UPPER_MASK) | (state->key[0] & LOWER_MASK); - state->key[_MT19937_N - 1] = state->key[_MT19937_M - 1] ^ (y >> 1) ^ (-(y & 1) & MATRIX_A); - -#if defined(__clang__) -# pragma clang diagnostic pop -#endif - - state->pos = 0; -} - -extern inline uint64_t mt19937_next64(mt19937_state *state); - -extern inline uint32_t mt19937_next32(mt19937_state *state); - -extern inline double mt19937_next_double(mt19937_state *state); diff --git a/c/mt19937/mt19937.h b/c/mt19937/mt19937.h deleted file mode 100644 index 5e7f17b00..000000000 --- a/c/mt19937/mt19937.h +++ /dev/null @@ -1,58 +0,0 @@ -// From https://github.com/numpy/numpy/blob/5b00a4d86134b9c190dccd519ba500af776f2ab0/numpy/random/src/mt19937/mt19937.h -#pragma once -#include - -#if defined(_WIN32) && !defined (__MINGW32__) -#define inline __forceinline -#endif - -#define RK_STATE_LEN 624 - -#define _MT19937_N 624 -#define _MT19937_M 397 -#define MATRIX_A 0x9908b0dfUL -#define UPPER_MASK 0x80000000UL -#define LOWER_MASK 0x7fffffffUL - -typedef struct s_mt19937_state { - uint32_t key[RK_STATE_LEN]; - int pos; -} mt19937_state; - -extern void mt19937_seed(mt19937_state *state, uint32_t seed); - -extern void mt19937_gen(mt19937_state *state); - -/* Slightly optimized reference implementation of the Mersenne Twister */ -static inline uint32_t mt19937_next(mt19937_state *state) { - uint32_t y; - - if (state->pos == RK_STATE_LEN) { - // Move to function to help inlining - mt19937_gen(state); - } - y = state->key[state->pos++]; - - /* Tempering */ - y ^= (y >> 11); - y ^= (y << 7) & 0x9d2c5680UL; - y ^= (y << 15) & 0xefc60000UL; - y ^= (y >> 18); - - return y; -} - -static inline uint64_t mt19937_next64(mt19937_state *state) { - return (uint64_t)mt19937_next(state) << 32 | mt19937_next(state); -} - -static inline uint32_t mt19937_next32(mt19937_state *state) { - return mt19937_next(state); -} - -static inline double mt19937_next_double(mt19937_state *state) { - int32_t a = mt19937_next(state) >> 5, b = mt19937_next(state) >> 6; - return (a * 67108864.0 + b) / 9007199254740992.0; -} - -void mt19937_jump(mt19937_state *state); diff --git a/c/pcg64/LICENSE.md b/c/pcg64/LICENSE.md new file mode 100644 index 000000000..7aac7a51c --- /dev/null +++ b/c/pcg64/LICENSE.md @@ -0,0 +1,22 @@ +# PCG64 + +## The MIT License + +PCG Random Number Generation for C. + +Copyright 2014 Melissa O'Neill + +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. diff --git a/c/pcg64/README.md b/c/pcg64/README.md new file mode 100644 index 000000000..99defbadc --- /dev/null +++ b/c/pcg64/README.md @@ -0,0 +1,10 @@ +# PCG64 + +`pcg64.h` and `LICENSE.md` come from +[NumPy's PCG64 implementation](https://github.com/numpy/numpy/tree/98acfbde9876839be145d022beb76e7ff835e8e1/numpy/random/src/pcg64) +at revision `98acfbde9876839be145d022beb76e7ff835e8e1`. +The header adds explicit casts to silence conversion warnings. +moocore uses the inline functions in `pcg64.h`. + +moocore initializes PCG64 with its 32-bit seed as the initial state and a +fixed stream identifier of 54; it does not use NumPy's `SeedSequence`. diff --git a/c/pcg64/pcg64.h b/c/pcg64/pcg64.h new file mode 100644 index 000000000..7f18b77a9 --- /dev/null +++ b/c/pcg64/pcg64.h @@ -0,0 +1,425 @@ +/* + * PCG64 Random Number Generation for C. + * + * Copyright 2014 Melissa O'Neill + * Copyright 2015 Robert Kern + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * For additional information about the PCG random number generation scheme, + * including its license and other licensing options, visit + * + * https://www.pcg-random.org + * + * Relicensed MIT in May 2019 + * + * The MIT License + * + * PCG Random Number Generation for C. + * + * Copyright 2014 Melissa O'Neill + * + * 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 PCG64_H_INCLUDED +#define PCG64_H_INCLUDED 1 + +#include + +#ifdef _WIN32 +#include +#endif + +#if defined(_WIN32) && !defined (__MINGW32__) +#define inline __forceinline +#endif + +#if defined(__GNUC_GNU_INLINE__) && !defined(__cplusplus) +#error Nonstandard GNU inlining semantics. Compile with -std=c99 or better. +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(__SIZEOF_INT128__) && !defined(PCG_FORCE_EMULATED_128BIT_MATH) +typedef __uint128_t pcg128_t; +#define PCG_128BIT_CONSTANT(high, low) (((pcg128_t)(high) << 64) + low) +#else +typedef struct { + uint64_t high; + uint64_t low; +} pcg128_t; + +static inline pcg128_t PCG_128BIT_CONSTANT(uint64_t high, uint64_t low) { + pcg128_t result; + result.high = high; + result.low = low; + return result; +} + +#define PCG_EMULATED_128BIT_MATH 1 +#endif + +typedef struct { pcg128_t state; } pcg_state_128; + +typedef struct { + pcg128_t state; + pcg128_t inc; +} pcg_state_setseq_128; + +#define PCG_DEFAULT_MULTIPLIER_HIGH 2549297995355413924ULL +#define PCG_DEFAULT_MULTIPLIER_LOW 4865540595714422341ULL + +#define PCG_DEFAULT_MULTIPLIER_128 \ + PCG_128BIT_CONSTANT(PCG_DEFAULT_MULTIPLIER_HIGH, PCG_DEFAULT_MULTIPLIER_LOW) +#define PCG_DEFAULT_INCREMENT_128 \ + PCG_128BIT_CONSTANT(6364136223846793005ULL, 1442695040888963407ULL) +#define PCG_STATE_SETSEQ_128_INITIALIZER \ + { \ + PCG_128BIT_CONSTANT(0x979c9a98d8462005ULL, 0x7d3e9cb6cfe0549bULL) \ + , PCG_128BIT_CONSTANT(0x0000000000000001ULL, 0xda3e39cb94b95bdbULL) \ + } + +#define PCG_CHEAP_MULTIPLIER_128 (0xda942042e4dd58b5ULL) + + +static inline uint64_t pcg_rotr_64(uint64_t value, unsigned int rot) { +#ifdef _WIN32 + return _rotr64(value, rot); +#else + return (value >> rot) | (value << ((-rot) & 63)); +#endif +} + +#ifdef PCG_EMULATED_128BIT_MATH + +static inline pcg128_t pcg128_add(pcg128_t a, pcg128_t b) { + pcg128_t result; + + result.low = a.low + b.low; + result.high = a.high + b.high + (result.low < b.low); + return result; +} + +static inline void _pcg_mult64(uint64_t x, uint64_t y, uint64_t *z1, + uint64_t *z0) { + +#if defined _WIN32 && _M_AMD64 + z0[0] = _umul128(x, y, z1); +#else + uint64_t x0, x1, y0, y1; + uint64_t w0, w1, w2, t; + /* Lower 64 bits are straightforward clock-arithmetic. */ + *z0 = x * y; + + x0 = x & 0xFFFFFFFFULL; + x1 = x >> 32; + y0 = y & 0xFFFFFFFFULL; + y1 = y >> 32; + w0 = x0 * y0; + t = x1 * y0 + (w0 >> 32); + w1 = t & 0xFFFFFFFFULL; + w2 = t >> 32; + w1 += x0 * y1; + *z1 = x1 * y1 + w2 + (w1 >> 32); +#endif +} + +static inline pcg128_t pcg128_mult(pcg128_t a, pcg128_t b) { + uint64_t h1; + pcg128_t result; + + h1 = a.high * b.low + a.low * b.high; + _pcg_mult64(a.low, b.low, &(result.high), &(result.low)); + result.high += h1; + return result; +} + +static inline void pcg_setseq_128_step_r(pcg_state_setseq_128 *rng) { + rng->state = pcg128_add(pcg128_mult(rng->state, PCG_DEFAULT_MULTIPLIER_128), + rng->inc); +} + +static inline uint64_t pcg_output_xsl_rr_128_64(pcg128_t state) { + return pcg_rotr_64(state.high ^ state.low, (unsigned int)(state.high >> 58u)); +} + +static inline void pcg_setseq_128_srandom_r(pcg_state_setseq_128 *rng, + pcg128_t initstate, + pcg128_t initseq) { + rng->state = PCG_128BIT_CONSTANT(0ULL, 0ULL); + rng->inc.high = initseq.high << 1u; + rng->inc.high |= initseq.low >> 63u; + rng->inc.low = (initseq.low << 1u) | 1u; + pcg_setseq_128_step_r(rng); + rng->state = pcg128_add(rng->state, initstate); + pcg_setseq_128_step_r(rng); +} + +static inline uint64_t +pcg_setseq_128_xsl_rr_64_random_r(pcg_state_setseq_128 *rng) { +#if defined _WIN32 && _M_AMD64 + uint64_t h1; + pcg128_t product; + + /* Manually inline the multiplication and addition using intrinsics */ + h1 = rng->state.high * PCG_DEFAULT_MULTIPLIER_LOW + + rng->state.low * PCG_DEFAULT_MULTIPLIER_HIGH; + product.low = + _umul128(rng->state.low, PCG_DEFAULT_MULTIPLIER_LOW, &(product.high)); + product.high += h1; + _addcarry_u64(_addcarry_u64(0, product.low, rng->inc.low, &(rng->state.low)), + product.high, rng->inc.high, &(rng->state.high)); + return _rotr64(rng->state.high ^ rng->state.low, (int)(rng->state.high >> 58u)); +#else + pcg_setseq_128_step_r(rng); + return pcg_output_xsl_rr_128_64(rng->state); +#endif +} + +static inline pcg128_t pcg128_mult_64(pcg128_t a, uint64_t b) { + uint64_t h1; + pcg128_t result; + + h1 = a.high * b; + _pcg_mult64(a.low, b, &(result.high), &(result.low)); + result.high += h1; + return result; +} + +static inline void pcg_cm_step_r(pcg_state_setseq_128 *rng) { +#if defined _WIN32 && _M_AMD64 + uint64_t h1; + pcg128_t product; + + /* Manually inline the multiplication and addition using intrinsics */ + h1 = rng->state.high * PCG_CHEAP_MULTIPLIER_128; + product.low = + _umul128(rng->state.low, PCG_CHEAP_MULTIPLIER_128, &(product.high)); + product.high += h1; + _addcarry_u64(_addcarry_u64(0, product.low, rng->inc.low, &(rng->state.low)), + product.high, rng->inc.high, &(rng->state.high)); +#else + rng->state = pcg128_add(pcg128_mult_64(rng->state, PCG_CHEAP_MULTIPLIER_128), + rng->inc); +#endif +} + + +static inline void pcg_cm_srandom_r(pcg_state_setseq_128 *rng, pcg128_t initstate, pcg128_t initseq) { + rng->state = PCG_128BIT_CONSTANT(0ULL, 0ULL); + rng->inc.high = initseq.high << 1u; + rng->inc.high |= initseq.low >> 63u; + rng->inc.low = (initseq.low << 1u) | 1u; + pcg_cm_step_r(rng); + rng->state = pcg128_add(rng->state, initstate); + pcg_cm_step_r(rng); +} + +static inline uint64_t pcg_cm_random_r(pcg_state_setseq_128* rng) +{ + /* Lots of manual inlining to help out certain compilers to generate + * performant code. */ + uint64_t hi = rng->state.high; + uint64_t lo = rng->state.low; + + /* Run the DXSM output function on the pre-iterated state. */ + lo |= 1; + hi ^= hi >> 32; + hi *= 0xda942042e4dd58b5ULL; + hi ^= hi >> 48; + hi *= lo; + + /* Run the CM step. */ +#if defined _WIN32 && _M_AMD64 + uint64_t h1; + pcg128_t product; + + /* Manually inline the multiplication and addition using intrinsics */ + h1 = rng->state.high * PCG_CHEAP_MULTIPLIER_128; + product.low = + _umul128(rng->state.low, PCG_CHEAP_MULTIPLIER_128, &(product.high)); + product.high += h1; + _addcarry_u64(_addcarry_u64(0, product.low, rng->inc.low, &(rng->state.low)), + product.high, rng->inc.high, &(rng->state.high)); +#else + rng->state = pcg128_add(pcg128_mult_64(rng->state, PCG_CHEAP_MULTIPLIER_128), + rng->inc); +#endif + return hi; +} +#else /* PCG_EMULATED_128BIT_MATH */ + +static inline void pcg_setseq_128_step_r(pcg_state_setseq_128 *rng) { + rng->state = rng->state * PCG_DEFAULT_MULTIPLIER_128 + rng->inc; +} + +static inline uint64_t pcg_output_xsl_rr_128_64(pcg128_t state) { + return pcg_rotr_64(((uint64_t)(state >> 64u)) ^ (uint64_t)state, + (unsigned int)(state >> 122u)); +} + +static inline void pcg_cm_step_r(pcg_state_setseq_128 *rng) { + rng-> state = rng->state * PCG_CHEAP_MULTIPLIER_128 + rng->inc; +} + +static inline uint64_t pcg_output_cm_128_64(pcg128_t state) { + uint64_t hi = (uint64_t)(state >> 64); + uint64_t lo = (uint64_t)state; + + lo |= 1; + hi ^= hi >> 32; + hi *= 0xda942042e4dd58b5ULL; + hi ^= hi >> 48; + hi *= lo; + return hi; +} + +static inline void pcg_cm_srandom_r(pcg_state_setseq_128 *rng, pcg128_t initstate, pcg128_t initseq) { + rng->state = 0U; + rng->inc = (initseq << 1u) | 1u; + pcg_cm_step_r(rng); + rng->state += initstate; + pcg_cm_step_r(rng); +} + +static inline uint64_t pcg_cm_random_r(pcg_state_setseq_128* rng) +{ + uint64_t ret = pcg_output_cm_128_64(rng->state); + pcg_cm_step_r(rng); + return ret; +} + +static inline uint64_t +pcg_setseq_128_xsl_rr_64_random_r(pcg_state_setseq_128* rng) +{ + pcg_setseq_128_step_r(rng); + return pcg_output_xsl_rr_128_64(rng->state); +} + +static inline void pcg_setseq_128_srandom_r(pcg_state_setseq_128 *rng, + pcg128_t initstate, + pcg128_t initseq) { + rng->state = 0U; + rng->inc = (initseq << 1u) | 1u; + pcg_setseq_128_step_r(rng); + rng->state += initstate; + pcg_setseq_128_step_r(rng); +} + +#endif /* PCG_EMULATED_128BIT_MATH */ + +static inline uint64_t +pcg_setseq_128_xsl_rr_64_boundedrand_r(pcg_state_setseq_128 *rng, + uint64_t bound) { + uint64_t threshold = -bound % bound; + for (;;) { + uint64_t r = pcg_setseq_128_xsl_rr_64_random_r(rng); + if (r >= threshold) + return r % bound; + } +} + +extern pcg128_t pcg_advance_lcg_128(pcg128_t state, pcg128_t delta, + pcg128_t cur_mult, pcg128_t cur_plus); + +static inline void pcg_setseq_128_advance_r(pcg_state_setseq_128 *rng, + pcg128_t delta) { + rng->state = pcg_advance_lcg_128(rng->state, delta, + PCG_DEFAULT_MULTIPLIER_128, rng->inc); +} + +static inline void pcg_cm_advance_r(pcg_state_setseq_128 *rng, pcg128_t delta) { + rng->state = pcg_advance_lcg_128(rng->state, delta, + PCG_128BIT_CONSTANT(0, PCG_CHEAP_MULTIPLIER_128), + rng->inc); +} + +typedef pcg_state_setseq_128 pcg64_random_t; +#define pcg64_random_r pcg_setseq_128_xsl_rr_64_random_r +#define pcg64_boundedrand_r pcg_setseq_128_xsl_rr_64_boundedrand_r +#define pcg64_srandom_r pcg_setseq_128_srandom_r +#define pcg64_advance_r pcg_setseq_128_advance_r +#define PCG64_INITIALIZER PCG_STATE_SETSEQ_128_INITIALIZER + +#ifdef __cplusplus +} +#endif + +typedef struct s_pcg64_state { + pcg64_random_t *pcg_state; + int has_uint32; + uint32_t uinteger; +} pcg64_state; + +static inline uint64_t pcg64_next64(pcg64_state *state) { + return pcg64_random_r(state->pcg_state); +} + +static inline uint32_t pcg64_next32(pcg64_state *state) { + uint64_t next; + if (state->has_uint32) { + state->has_uint32 = 0; + return state->uinteger; + } + next = pcg64_random_r(state->pcg_state); + state->has_uint32 = 1; + state->uinteger = (uint32_t)(next >> 32); + return (uint32_t)(next & 0xffffffff); +} + +static inline uint64_t pcg64_cm_next64(pcg64_state *state) { + return pcg_cm_random_r(state->pcg_state); +} + +static inline uint32_t pcg64_cm_next32(pcg64_state *state) { + uint64_t next; + if (state->has_uint32) { + state->has_uint32 = 0; + return state->uinteger; + } + next = pcg_cm_random_r(state->pcg_state); + state->has_uint32 = 1; + state->uinteger = (uint32_t)(next >> 32); + return (uint32_t)(next & 0xffffffff); +} + +void pcg64_advance(pcg64_state *state, uint64_t *step); +void pcg64_cm_advance(pcg64_state *state, uint64_t *step); + +void pcg64_set_seed(pcg64_state *state, uint64_t *seed, uint64_t *inc); + +void pcg64_get_state(pcg64_state *state, uint64_t *state_arr, int *has_uint32, + uint32_t *uinteger); + +void pcg64_set_state(pcg64_state *state, uint64_t *state_arr, int has_uint32, + uint32_t uinteger); + +#endif /* PCG64_H_INCLUDED */ diff --git a/c/rng.c b/c/rng.c index dd925848f..d1a1187b0 100644 --- a/c/rng.c +++ b/c/rng.c @@ -73,7 +73,7 @@ rng_standard_normal(rng_state *rng) { for (;;) { /* r = e3n52sb8 */ - uint64_t r = mt19937_next64(rng); + uint64_t r = pcg64_random_r(rng); int idx = r & 0xff; r >>= 8; int sign = r & 0x1; @@ -86,13 +86,13 @@ rng_standard_normal(rng_state *rng) if (idx == 0) { for (;;) { /* Switch to 1.0 - U to avoid log(0.0), see GH 13361 */ - double xx = -ziggurat_nor_inv_r * log1p(-mt19937_next_double(rng)); - double yy = -log1p(-mt19937_next_double(rng)); + double xx = -ziggurat_nor_inv_r * log1p(-rng_random(rng)); + double yy = -log1p(-rng_random(rng)); if (yy + yy > xx * xx) return ((rabs >> 8) & 0x1) ? -(ziggurat_nor_r + xx) : ziggurat_nor_r + xx; } - } else if (((fi_double[idx - 1] - fi_double[idx]) * mt19937_next_double(rng) + + } else if (((fi_double[idx - 1] - fi_double[idx]) * rng_random(rng) + fi_double[idx]) < exp(-0.5 * x * x)) return x; } diff --git a/c/rng.h b/c/rng.h index 3b5a65cc7..de82e64f1 100644 --- a/c/rng.h +++ b/c/rng.h @@ -1,17 +1,22 @@ #ifndef RNG_H_ #define RNG_H_ -#include "mt19937/mt19937.h" +#ifdef _WIN64 +#include +#endif -typedef mt19937_state rng_state; +#include "pcg64/pcg64.h" + +typedef pcg64_random_t rng_state; #include "common.h" static inline rng_state * rng_new(uint32_t seed) { - rng_state * rng = malloc(sizeof(rng_state)); - mt19937_seed(rng, seed); + rng_state * rng = MOOCORE_MALLOC(1, rng_state); + pcg64_srandom_r(rng, PCG_128BIT_CONSTANT(0, seed), + PCG_128BIT_CONSTANT(0, 54)); return rng; } @@ -25,7 +30,7 @@ rng_free(rng_state * rng) static inline double rng_random(rng_state * rng) { - return mt19937_next_double(rng); + return (double)(pcg64_random_r(rng) >> 11) * (1.0 / 9007199254740992.0); } static inline double diff --git a/python/doc/source/whatsnew/index.rst b/python/doc/source/whatsnew/index.rst index f9e61e005..dc37f11d4 100644 --- a/python/doc/source/whatsnew/index.rst +++ b/python/doc/source/whatsnew/index.rst @@ -8,6 +8,8 @@ Version 0.4.0 ------------- - Requires ``numpy>=2.1``. +- :func:`~moocore.hv_approx` and :func:`~moocore.whv_hype` now use PCG64 for + random sampling, so seeded results differ from earlier versions. - :func:`~moocore.vorob_t` returns a :class:`~typing.NamedTuple` instead of a dictionary. - :func:`~moocore.is_nondominated` is up to 10x faster in some inputs thanks to a customized radixsort implementation. - New shapes ``"concave-cliff"`` and ``"convex-cliff"`` added to :func:`~moocore.generate_ndset`. diff --git a/python/pyproject.toml b/python/pyproject.toml index 30cac0f0f..c31cf7cd8 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -95,6 +95,7 @@ zip-safe = false [tool.setuptools.package-data] "*" = [ "*.c", "*.h" ] "moocore.data" = [ "*.10", "*.csv", "*.dat", "*.txt", "*.xz" ] +moocore = [ "libmoocore/pcg64/LICENSE.md", "libmoocore/pcg64/README.md" ] [tool.distutils.bdist_wheel] py_limited_api = "cp310" diff --git a/python/src/moocore/_ffi_build.py b/python/src/moocore/_ffi_build.py index 60732dce3..67e2803c3 100644 --- a/python/src/moocore/_ffi_build.py +++ b/python/src/moocore/_ffi_build.py @@ -45,10 +45,9 @@ "hv_contrib.c", "io.c", "libutil.c", # For fatal_error() - "mt19937/mt19937.c", "nondominated.c", "pareto.c", - "rng.c", + "rng.c", # Uses the inline PCG64 routines in pcg64.h. "whv.c", "whv_hype.c", ] diff --git a/python/src/moocore/_moocore.py b/python/src/moocore/_moocore.py index e291eac1d..4f6c3975e 100644 --- a/python/src/moocore/_moocore.py +++ b/python/src/moocore/_moocore.py @@ -1031,6 +1031,9 @@ def hv_approx( is not monotonic as shown in the example :ref:`sphx_glr_auto_examples_plot_hv_approx.py`. + Monte-Carlo sampling uses PCG64. Seeded results differ from earlier + moocore versions and from NumPy's seed initialization. + .. seealso:: For details of the calculation, see the Notes section below. See :ref:`Benchmarks: Approximation of the hypervolume `. @@ -1117,7 +1120,7 @@ def hv_approx( >>> moocore.hv_approx(x, ref=[10, 10], method="DZ2019-HW") 37.99996 >>> moocore.hv_approx(x, ref=[10, 10], seed=42, method="DZ2019-MC") - 38.00081 + 38.00155 Merge all the sets of a dataset by removing the set number column: @@ -2622,6 +2625,9 @@ def whv_hype( points following the procedure described by :footcite:t:`AugBadBroZit2009gecco`. A weight distribution describing user preferences may be specified. + Sampling uses PCG64. Seeded results differ from earlier moocore versions + and from NumPy's seed initialization. + .. warning:: The current implementation only supports 2 objectives. @@ -2674,15 +2680,15 @@ def whv_hype( >>> moocore.hypervolume([[2, 2]], ref=4) 4.0 >>> moocore.whv_hype([[2, 2]], ref=4, ideal=1, seed=42) - 3.99807 + 3.9861 >>> moocore.hypervolume([[3, 1]], ref=4) 3.0 >>> moocore.whv_hype([[3, 1]], ref=4, ideal=1, seed=42) - 3.00555 + 2.9979 >>> moocore.whv_hype([[2, 2]], ref=4, ideal=1, dist="exponential", mu=0.2, seed=42) - 1.14624 + 1.13184 >>> moocore.whv_hype([[3, 1]], ref=4, ideal=1, dist="exponential", mu=0.2, seed=42) - 1.66815 + 1.66437 >>> moocore.whv_hype( ... [[2, 2]], ... ref=4, @@ -2691,7 +2697,7 @@ def whv_hype( ... mu=[2.9, 0.9], ... seed=42, ... ) - 0.64485 + 0.64386 >>> moocore.whv_hype( ... [[3, 1]], ... ref=4, @@ -2700,7 +2706,7 @@ def whv_hype( ... mu=[2.9, 0.9], ... seed=42, ... ) - 4.03632 + 4.0365 """ # Convert to numpy.array in case the user provides a list. We use diff --git a/python/tests/test_moocore.py b/python/tests/test_moocore.py index 3699e6166..103e1dbda 100644 --- a/python/tests/test_moocore.py +++ b/python/tests/test_moocore.py @@ -536,7 +536,9 @@ def test_hv_approx(dim): x = np.full((1, dim), 0.5) ref = np.full(dim, 1.0) true_hv = moocore.hypervolume(x, ref=ref) - appr_hv = moocore.hv_approx(x, ref=ref, method="DZ2019-MC", seed=42) + appr_hv = moocore.hv_approx( + x, ref=ref, method="DZ2019-MC", seed=42, nsamples=1_048_576 + ) # print(f"{dim}: {(true_hv - appr_hv)/true_hv}") ## Precision goes down significantly with higher dimensions. signif = 3 if dim < 7 else 2 @@ -568,6 +570,14 @@ def test_hv_approx_default_seed(): np.testing.assert_approx_equal(true_hv, appr_hv, significant=signif) +def test_hv_approx_seed_reproducibility(): + kwargs = {"ref": 1, "method": "DZ2019-MC", "nsamples": 4096} + points = [[0.3, 0.4, 0.5]] + result = moocore.hv_approx(points, seed=42, **kwargs) + assert result == moocore.hv_approx(points, seed=42, **kwargs) + assert result != moocore.hv_approx(points, seed=43, **kwargs) + + def test_hv_approx_errors(): with pytest.raises( ValueError, match=r".*must be a positive integer value.*" @@ -763,6 +773,17 @@ def test_r2_from_doc(self): assert_allclose(r2, 0.33360768789505657) +@pytest.mark.parametrize( + ("dist", "mu"), + [("uniform", None), ("exponential", 0.2), ("point", [2.9, 0.9])], +) +def test_whv_hype_seed_reproducibility(dist, mu): + kwargs = {"ref": 4, "ideal": 1, "dist": dist, "mu": mu} + result = moocore.whv_hype([[2, 2]], seed=42, **kwargs) + assert result == moocore.whv_hype([[2, 2]], seed=42, **kwargs) + assert result != moocore.whv_hype([[2, 2]], seed=43, **kwargs) + + def test_whv_hype_errors(): """Check error handling of whv_hype().""" with pytest.raises(TypeError, match=r"real number"): diff --git a/r/DESCRIPTION b/r/DESCRIPTION index 8a4ddf093..b46ebce8d 100644 --- a/r/DESCRIPTION +++ b/r/DESCRIPTION @@ -12,9 +12,8 @@ Authors@R: c(person("Manuel", "López-Ibáñez", role = c("aut", "cre"), person(c("Michael", "H."), "Buselli", role = "cph", comment = "AVL-tree library"), person("Wessel", "Dankers", role = "cph", comment = "AVL-tree library"), person("NumPy Developers", role = "cph", comment = "RNG and ziggurat constants"), - person("Jean-Sebastien", "Roy", role = "cph", comment = "mt19937 library"), - person("Makoto", "Matsumoto", role = "cph", comment = "mt19937 library"), - person("Takuji", "Nishimura", role = "cph", comment = "mt19937 library")) + person("Melissa", "O'Neill", role = "cph", comment = "PCG64 library"), + person("Robert", "Kern", role = "cph", comment = "PCG64 library")) Description: Fast implementations of mathematical operations and performance metrics for multi-objective optimization, including filtering and ranking of dominated vectors according to Pareto optimality, hypervolume metric, C.M. Fonseca, L. Paquete, M. López-Ibáñez (2006) , epsilon indicator, inverted generational distance, computation of the empirical attainment function, V.G. da Fonseca, C.M. Fonseca, A.O. Hall (2001) , and Vorob'ev threshold, expectation and deviation, M. Binois, D. Ginsbourger, O. Roustant (2015) , among others. Depends: R (>= 4.1) Imports: diff --git a/r/NEWS.md b/r/NEWS.md index d71900730..a4f5e6998 100644 --- a/r/NEWS.md +++ b/r/NEWS.md @@ -1,5 +1,6 @@ # moocore 0.3.2.900 + * `hv_approx()` and `whv_hype()` now use PCG64, so seeded results change. * `is_nondominated` is up to 10x faster in some inputs thanks to a customized radixsort implementation. * New shapes `"concave-cliff"` and `"convex-cliff"` added to `generate_ndset()`. diff --git a/r/R/hv_approx.R b/r/R/hv_approx.R index 820cb4755..57652cc31 100644 --- a/r/R/hv_approx.R +++ b/r/R/hv_approx.R @@ -72,7 +72,7 @@ #' hv_approx(x, ref=10, method="Rphi-FWE+") #' @expect equal(37.999958) #' hv_approx(x, ref=10, method="DZ2019-HW") -#' @expect equal(38.000806) +#' @expect equal(38.001554) #' hv_approx(x, ref=10, seed=42, method="DZ2019-MC") #' #' @export diff --git a/r/R/whv.R b/r/R/whv.R index 897d4702d..18a258df5 100644 --- a/r/R/whv.R +++ b/r/R/whv.R @@ -116,7 +116,8 @@ total_whv_rect <- function(x, rectangles, reference, maximise = FALSE, ideal = N #' sampling. Higher values typically produce more accurate approximations of #' the true hypervolume, but require more time. #' -#' @param seed `integer(1)`\cr Random seed. +#' @param seed `integer(1)`\cr Random seed for PCG64. Seeded results differ +#' from earlier moocore versions and from NumPy seed initialization. #' #' @param dist `character(1)`\cr Weight distribution type. See Details. #' @@ -138,20 +139,20 @@ total_whv_rect <- function(x, rectangles, reference, maximise = FALSE, ideal = N #' @seealso [read_datasets()], [eafdiff()], [whv_rect()] #' #' @doctest -#' @expect equal(3.99807) +#' @expect equal(3.9861) #' whv_hype(matrix(2, ncol=2), reference = 4, ideal = 1, seed = 42) -#' @expect equal(3.00555) +#' @expect equal(2.9979) #' whv_hype(matrix(c(3,1), ncol=2), reference = 4, ideal = 1, seed = 42) -#' @expect equal(1.14624) +#' @expect equal(1.13184) #' whv_hype(matrix(2, ncol=2), reference = 4, ideal = 1, seed = 42, #' dist = "exponential", mu=0.2) -#' @expect equal(1.66815) +#' @expect equal(1.66437) #' whv_hype(matrix(c(3,1), ncol=2), reference = 4, ideal = 1, seed = 42, #' dist = "exponential", mu=0.2) -#' @expect equal(0.64485) +#' @expect equal(0.64386) #' whv_hype(matrix(2, ncol=2), reference = 4, ideal = 1, seed = 42, #' dist = "point", mu=c(2.9,0.9)) -#' @expect equal(4.03632) +#' @expect equal(4.0365) #' whv_hype(matrix(c(3,1), ncol=2), reference = 4, ideal = 1, seed = 42, #' dist = "point", mu=c(2.9,0.9)) #' @concept metrics diff --git a/r/inst/WORDLIST b/r/inst/WORDLIST index aef9fd6b7..17f95bd72 100644 --- a/r/inst/WORDLIST +++ b/r/inst/WORDLIST @@ -25,15 +25,19 @@ IGDX IRIDIA Ibanez Ibáñez +Kern Lennart López MacOS Makespan +Melissa Metaheuristics Mickael Mickaël +Neill NumPy ORCID +PCG64 Paquete Preuss Quemy @@ -43,7 +47,6 @@ SPEA SPEA2 Schapermeier Schäpermeier -Sebastien Stützle TPLS TR @@ -71,7 +74,6 @@ hypervolumes libre matrixStats metaheuristic -mt19937 nondominated orthant orthotopes diff --git a/r/man/hv_approx.Rd b/r/man/hv_approx.Rd index 78bacd777..d35b98f06 100644 --- a/r/man/hv_approx.Rd +++ b/r/man/hv_approx.Rd @@ -28,7 +28,8 @@ objectives or a vector of logical values, with one value per objective.} sampling. Higher values typically produce more accurate approximations of the true hypervolume, but require more time.} -\item{seed}{\code{integer(1)}\cr Random seed.} +\item{seed}{\code{integer(1)}\cr Random seed for PCG64. Seeded results differ +from earlier moocore versions and from NumPy seed initialization.} \item{method}{\code{character(1)}\cr Method to generate the sampling weights. See `Details'.} } diff --git a/r/man/moocore-package.Rd b/r/man/moocore-package.Rd index 16ec4d7b8..425bf8374 100644 --- a/r/man/moocore-package.Rd +++ b/r/man/moocore-package.Rd @@ -34,9 +34,8 @@ Other contributors: \item Michael H. Buselli (AVL-tree library) [copyright holder] \item Wessel Dankers (AVL-tree library) [copyright holder] \item NumPy Developers (RNG and ziggurat constants) [copyright holder] - \item Jean-Sebastien Roy (mt19937 library) [copyright holder] - \item Makoto Matsumoto (mt19937 library) [copyright holder] - \item Takuji Nishimura (mt19937 library) [copyright holder] + \item Melissa O'Neill (PCG64 library) [copyright holder] + \item Robert Kern (PCG64 library) [copyright holder] } } diff --git a/r/man/whv_hype.Rd b/r/man/whv_hype.Rd index 22e5e356b..fbf1cca54 100644 --- a/r/man/whv_hype.Rd +++ b/r/man/whv_hype.Rd @@ -32,7 +32,8 @@ objectives or a vector of logical values, with one value per objective.} sampling. Higher values typically produce more accurate approximations of the true hypervolume, but require more time.} -\item{seed}{\code{integer(1)}\cr Random seed.} +\item{seed}{\code{integer(1)}\cr Random seed for PCG64. Seeded results differ +from earlier moocore versions and from NumPy seed initialization.} \item{dist}{\code{character(1)}\cr Weight distribution type. See Details.} diff --git a/r/src/Makevars b/r/src/Makevars index 8164b3bdd..5a240885d 100644 --- a/r/src/Makevars +++ b/r/src/Makevars @@ -19,7 +19,6 @@ MOOCORE_SRC_FILES = \ hvapprox.c \ hvc3d.c \ io.c \ - mt19937/mt19937.c \ nondominated.c \ pareto.c \ r2_exact.c \ diff --git a/r/tests/testthat/test-doctest-hv_approx.R b/r/tests/testthat/test-doctest-hv_approx.R index bb1a3f101..5f11c262b 100644 --- a/r/tests/testthat/test-doctest-hv_approx.R +++ b/r/tests/testthat/test-doctest-hv_approx.R @@ -8,6 +8,6 @@ test_that("Doctest: hv_approx", { expect_equal(hypervolume(x, ref = 10), 38) expect_equal(hv_approx(x, ref = 10, method = "Rphi-FWE+"), 37.999979) expect_equal(hv_approx(x, ref = 10, method = "DZ2019-HW"), 37.999958) - expect_equal(hv_approx(x, ref = 10, seed = 42, method = "DZ2019-MC"), 38.000806) + expect_equal(hv_approx(x, ref = 10, seed = 42, method = "DZ2019-MC"), 38.001554) }) diff --git a/r/tests/testthat/test-doctest-whv_hype.R b/r/tests/testthat/test-doctest-whv_hype.R index 277e2eb80..26853a0b4 100644 --- a/r/tests/testthat/test-doctest-whv_hype.R +++ b/r/tests/testthat/test-doctest-whv_hype.R @@ -5,16 +5,16 @@ test_that("Doctest: whv_hype", { # Created from @doctest for `whv_hype` # Source file: R/whv.R expect_equal(whv_hype(matrix(2, ncol = 2), reference = 4, ideal = 1, seed = 42), - 3.99807) + 3.9861) expect_equal(whv_hype(matrix(c(3, 1), ncol = 2), reference = 4, ideal = 1, - seed = 42), 3.00555) + seed = 42), 2.9979) expect_equal(whv_hype(matrix(2, ncol = 2), reference = 4, ideal = 1, seed = 42, - dist = "exponential", mu = 0.2), 1.14624) + dist = "exponential", mu = 0.2), 1.13184) expect_equal(whv_hype(matrix(c(3, 1), ncol = 2), reference = 4, ideal = 1, - seed = 42, dist = "exponential", mu = 0.2), 1.66815) + seed = 42, dist = "exponential", mu = 0.2), 1.66437) expect_equal(whv_hype(matrix(2, ncol = 2), reference = 4, ideal = 1, seed = 42, - dist = "point", mu = c(2.9, 0.9)), 0.64485) + dist = "point", mu = c(2.9, 0.9)), 0.64386) expect_equal(whv_hype(matrix(c(3, 1), ncol = 2), reference = 4, ideal = 1, - seed = 42, dist = "point", mu = c(2.9, 0.9)), 4.03632) + seed = 42, dist = "point", mu = c(2.9, 0.9)), 4.0365) }) diff --git a/r/tests/testthat/test-hv_approx.R b/r/tests/testthat/test-hv_approx.R index ed21e4bc3..193fd6a50 100644 --- a/r/tests/testthat/test-hv_approx.R +++ b/r/tests/testthat/test-hv_approx.R @@ -13,18 +13,21 @@ for (dim in seq(3L, 10L)) { ## Precision goes down significantly with higher dimensions. signif <- if (dim < 4) 3 else 2 - expect_equal(hv_approx(x, ref=ref, method="DZ2019-MC", seed=42), true_hv, + appr_hv <- hv_approx(x, ref=ref, method="DZ2019-MC", seed=42, nsamples=1048576) + expect_equal(appr_hv, true_hv, tolerance = 10**-signif, info = paste0("dim=", dim, ", signif=", signif, " error=", log10(abs(true_hv - appr_hv) / true_hv))) signif <- if (dim < 8) 4 else if (dim < 10) 3 else 2 - expect_equal(hv_approx(x, ref=ref, method="DZ2019-HW"), true_hv, + appr_hv <- hv_approx(x, ref=ref, method="DZ2019-HW") + expect_equal(appr_hv, true_hv, tolerance = 10**-signif, info = paste0("dim=", dim, ", signif=", signif, " error=", log10(abs(true_hv - appr_hv) / true_hv))) # method="Rphi-FWE+" is the default signif <- if (dim < 6) 4 else if (dim < 8) 3 else 2 - expect_equal(hv_approx(x, ref=ref), true_hv, + appr_hv <- hv_approx(x, ref=ref) + expect_equal(appr_hv, true_hv, tolerance = 10**-signif, info = paste0("dim=", dim, ", signif=", signif, " error=", log10(abs(true_hv - appr_hv) / true_hv))) }) diff --git a/r/tests/testthat/test-whv_hype.R b/r/tests/testthat/test-whv_hype.R index d64dd0c63..824c1e0ff 100644 --- a/r/tests/testthat/test-whv_hype.R +++ b/r/tests/testthat/test-whv_hype.R @@ -30,25 +30,24 @@ test_that("whv_hype", { x_list <- split.data.frame(x[,1:2], x[,3]) y_list <- split.data.frame(y[,1:2], y[,3]) - set.seed(12345) - whv_x <- whv_hype(x_list[[1]], reference = ref, ideal = ideal) - expect_equal(whv_x, 2480979524388, tolerance=10) + whv_x <- whv_hype(x_list[[1]], reference = ref, ideal = ideal, seed = 42) + expect_equal(whv_x, 2480047738211, tolerance=1e-5) whv_x <- whv_hype(x_list[[1]], reference = ref, ideal = ideal, - dist = "point", mu = goal) - expect_equal(whv_x, 1496335657875, tolerance=10) + dist = "point", mu = goal, seed = 42) + expect_equal(whv_x, 1496335657875, tolerance=1e-5) whv_x <- whv_hype(x_list[[1]], reference = ref, ideal = ideal, - dist = "exponential", mu=0.2) - expect_equal(whv_x, 1903385037871, tolerance=10) + dist = "exponential", mu=0.2, seed = 42) + expect_equal(whv_x, 1902086184412, tolerance=1e-5) }) test_that("whv_hype mixed min/max", { expect_equal(whv_hype(matrix(-2, ncol = 2), reference = -4, ideal = -1, seed = 42, maximise=TRUE), - 3.99807) + 3.9861) expect_equal(whv_hype(matrix(c(-2, 2), ncol = 2), reference = c(-4, 4), ideal = c(-1, 1), seed = 42, maximise=c(TRUE,FALSE)), - 3.99807) + 3.9861) })