Coder Social home page Coder Social logo

battleship-search's Introduction

battleship-search

maximize an n-dimensional landscape using the battleship search algorithm

build status

testling badge

example

1-dimensional

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

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

q.start();

output:

MAX [ 2.5 ] 2.586482695940614
MAX [ 3.125 ] 3.2950351120302708
MAX [ 2.8125 ] 4.078954538271598
MAX [ 2.890625 ] 4.093839145038978
MAX [ 2.8515625 ] 4.107647760472744
MAX [ 2.861328125 ] 4.108143493735451
MAX [ 2.8564453125 ] 4.108227683718651
MAX [ 2.85888671875 ] 4.108268474284235
MAX [ 2.857666015625 ] 4.108268817327394
MAX [ 2.8582763671875 ] 4.108273828324046
MAX [ 2.858257293701172 ] 4.1082738286448475
MAX [ 2.858266830444336 ] 4.108273829749719
MAX [ 2.8582656383514404 ] 4.10827382975
^C

4-dimensional example

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

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

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

q.start();

output:

MAX [ -5, 5, -5, 5 ] 2.0750809293428523
MAX [ 1.25, -3.75, -3.75, 1.25 ] 2.365703195960478
MAX [ 1.25, -3.75, -3.75, 3.75 ] 2.369529012327853
MAX [ 1.25, -3.75, -3.75, -3.75 ] 2.3803181691252
MAX [ 2.03125, 0.46875, 1.40625, 0.15625 ] 2.3943124663892474
MAX [ 2.03125, 0.46875, 1.40625, 0.46875 ] 2.4460672123039338
^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.

q.start()

Start the search. The search goes on forever until stopped.

q.stop()

Stop the search.

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

battleship-search's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.