-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (46 loc) · 1.47 KB
/
index.js
File metadata and controls
53 lines (46 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/** Default settings */
const defaults = {
algorithm: 'genetic'
}
/**
* Module definition of this kreatives-feld package
*/
module.exports = {
/**
* This function creates a group combination from given input
* and returns combination with its fitness score
* @param {{String: Array<Number>}} input
* @param {Number} groups Number of groups
* @param {String: any} settings Object with option properties
* @returns {Promise<{combination: Array<Number>, score: Number}>}
*/
diverse: (data, groups, settings = {}) => {
// Validate input arguments
validate(data, groups)
// Merge given settings with defaults
settings = Object.assign(defaults, settings)
// Wrap data- and groups-input into one input object
var input = { data, groups }
// Decide which algorithm to use
var algorithm = (settings.algorithm === 'monte-carlo')
? require('./src/monte-carlo')()
: require('./src/genetic-algorithm')()
// Run algorithm and return a promise
return algorithm.run(input, settings)
}
}
/**
* Validates given input arguments
* @param {*} data
* @param {*} groups
*/
var validate = (data, groups) => {
//
if (groups < 2) {
throw new Error('Error: Number of groups must be at least 2. Given ' + groups)
}
// We need at least two elements for each group
if (Object.keys(data).length < (2 * groups)) {
throw new Error('Error: We need at least 2 x groups input elements. Given ' + Object.keys(data).length)
}
}