npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

battleship-search

v1.0.1

Published

maximize an n-dimensional landscape using the battleship search algorithm

Downloads

48

Readme

battleship-search

maximize an n-dimensional landscape using the battleship search algorithm

build status

testling badge

example

2-dimensional

var search = require('battleship-search');
var q = search([ [ 0, 5 ], [ 0, 5 ] ], function (pt) {
    var x = pt[0], y = pt[1];
    return Math.sin(5 * x) - Math.cos(x)
        + Math.sin(3 * y) - 1 / 5 * Math.cos(x * y + 3 * y)
        + 1/4 * Math.sin(x - 1) - 2 * Math.cos(x)
    ;
});

var count = 0;
q.on('test', function (pt, x) {
    console.log('TEST', pt, x);
});

q.on('max', function (pt, x) {
    console.log('MAX', pt, x);
});

for (var i = 0; i < 20; i++) q.next();

output:

TEST [ 2.5, 2.5 ] 3.4489693584958943
MAX [ 2.5, 2.5 ] 3.4489693584958943
TEST [ 0, 0 ] -3.410367746201974
TEST [ 0, 2.5 ] -2.34169483299424
TEST [ 0, 5 ] -2.408142323473093
TEST [ 2.5, 5 ] 3.379747929853664
TEST [ 5, 5 ] -0.38886347782686465
TEST [ 5, 2.5 ] -0.31615536590237336
TEST [ 5, 0 ] -1.372538930314434
TEST [ 2.5, 0 ] 2.386482695940614
TEST [ 3.750000000000001, 3.75 ] 1.2931164053743218
TEST [ 3.75, 1.25 ] 1.996333158495778
TEST [ 5, 1.25 ] -1.576285943241487
TEST [ 3.75, 2.500000000000001 ] 3.4742774295274836
MAX [ 3.75, 2.500000000000001 ] 3.4742774295274836
TEST [ 3.75, 2.500000000000002 ] 3.4742774295274836
TEST [ 4.999999999999999, 3.7500000000000004 ] -2.171197217803212
TEST [ 4.375000000000003, 3.125000000000003 ] 1.1994139125493546
TEST [ 3.1249999999999973, 3.125000000000003 ] 3.1532049938105677
TEST [ 4.375000000000002, 4.374999999999999 ] 1.4491198782015409
TEST [ 4.375000000000002, 3.1250000000000018 ] 1.1994139125493692
TEST [ 1.25, 1.25 ] -1.601803452333597

4-dimensional example

var search = require('battleship-search');
var domain = [ [ -5, 5 ], [ -5, 5 ], [ -5, 5 ], [ -5, 5 ] ];

var q = search(domain, function (pt) {
    var x = pt[0], y = pt[1], z = pt[2], w = pt[3];
    return Math.sin(x + y * Math.cos(z) / 10)
        + Math.sin(3 * (y - 3)) - 7 * Math.cos((z-1) + x) * 1/2
        + 2 * Math.sin(5 * (w - 5)) * Math.sin(3 * x + 4 * w) * 1/8
    ;
});

q.on('max', function (pt, x) {
    console.log('MAX', pt, x);
});

while (true) q.next();

output:

MAX [ 0, 0, 0, 0 ] -2.303176555780246
MAX [ -5, -5, -5, -5 ] 1.827372685649148
MAX [ -3.3333333333333335,
  3.3333333333333335,
  4.440892098500626e-16,
  3.3333333333333335 ] 2.0377750437796696
MAX [ 3.3333333333333335,
  3.3333333333333335,
  -4.440892098500626e-16,
  3.3333333333333335 ] 2.973899545084325
MAX [ 5, -5, 5, -5 ] 3.168040771270319
^C

methods

var search = require('battleship-search')

var q = search(bounds, testFn)

Create a new search from an array of 2-element arrays bounds with [min,max] bounds for each dimension.

testFn(pt, cb) fires for each point pt to test. testFn() should call cb() with its result.

var res = q.next()

Compute the next point in the search, returning res, an object with the point tested, res.point, and the value at that point res.value.

events

q.on('test', function (pt, value) {})

Each time the test function produces a result, the 'test' event fires with the coordinate tested pt and the resulting value.

q.on('max', function (pt, value) {})

Each time a test value is greater than the maximum value seen so far, the 'max' event fires with the point pt and the value.

install

With npm do:

npm install battleship-search

license

MIT