new w-optimization()
- Source:
Methods
(static) arrBinarySearch(arr, x, optopt) → {Object}
- Description:
陣列二分搜尋法
Unit Test: Github
- Source:
Example
let arr = [1, 2, 4, 6, 1, 100, 0, 10000, 3]
console.log(arrBinarySearch(arr, 2))
// => { ind: 1, indSorted: 3, value: 2 }
console.log(arrBinarySearch(arr, 5))
// => null
Parameters:
| Name | Type | Attributes | Default | Description | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
arr |
Array | 輸入數據陣列,元素為數字或數字字串。種類:資料陣列,必給,元素為廣義數字:數字與數字字串(轉換後使用)參與搜尋,非數字字串(如'-'、'NP'、'')為標記字串,原樣保留於原位置、不參與搜尋、不算錯,null、布林、NaN、Infinity、BigInt、物件等其餘元素為無效,訊息帶元素索引;空陣列或無數字元素時回傳null |
||||||||||||
x |
Number | String | 輸入尋找數字或數字字串。種類:有效數字,必給,數字字串轉換後使用,'-'、'NP'、''等非數字字串與null、布林、NaN、Infinity皆為無效 |
||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{}。種類:一般物件,undefined或null視為未給,其餘非一般物件為無效;未宣告之鍵忽略;各鍵給了而不符種類即為無效,不改用預設 Properties
|
Returns:
回傳求解後結果物件,含鍵值ind(於原陣列之索引),indSorted(於排序後數字陣列之索引),value(值),找不到時回傳null;輸入無效時同步throw Error,訊息格式為invalid <名稱>: must be <種類描述>,不含輸入值
- Type
- Object
(static) arrBinarySearchClosest(arr, x, optopt) → {Object}
- Description:
陣列二分搜尋法,求最接近值
Unit Test: Github
- Source:
Example
let arr = [1, 2, 4, 6, 1, 100, 0, 10000, 3]
console.log(arrBinarySearchClosest(arr, 2))
// => { ind: 1, indSorted: 3, value: 2, diff: 0 }
console.log(arrBinarySearchClosest(arr, 4.5))
// => { ind: 2, indSorted: 5, value: 4, diff: 0.5 }
console.log(arrBinarySearchClosest(arr, 5))
// => { ind: 3, indSorted: 6, value: 6, diff: 1 }
Parameters:
| Name | Type | Attributes | Default | Description | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
arr |
Array | 輸入數據陣列,元素為數字或數字字串。種類:資料陣列,必給,元素為廣義數字:數字與數字字串(轉換後使用)參與搜尋,非數字字串(如'-'、'NP'、'')為標記字串,原樣保留於原位置、不參與搜尋、不算錯,null、布林、NaN、Infinity、BigInt、物件等其餘元素為無效,訊息帶元素索引;空陣列或無數字元素時回傳null |
||||||||||||
x |
Number | String | 輸入尋找數字或數字字串。種類:有效數字,必給,數字字串轉換後使用,'-'、'NP'、''等非數字字串與null、布林、NaN、Infinity皆為無效 |
||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{}。種類:一般物件,undefined或null視為未給,其餘非一般物件為無效;未宣告之鍵忽略;各鍵給了而不符種類即為無效,不改用預設 Properties
|
Returns:
回傳求解後最接近值物件,含鍵值ind(於原陣列之索引),indSorted(於排序後數字陣列之索引),value(最接近值),diff(與x之差值絕對值);無數字可搜尋時回傳null;輸入無效時同步throw Error,訊息格式為invalid <名稱>: must be <種類描述>,不含輸入值
- Type
- Object
(static) binarySearch(fun, xMin, xMax, optopt) → {Object}
- Description:
二分搜尋法
Unit Test: Github
- Source:
Example
function fun(param) {
let x = param / 180 * Math.PI
return Math.sin(x)
}
console.log(binarySearch(fun, 90, 300))
// => { y: -0.8660254037844386, x: 300 }
console.log(binarySearch(fun, 90, 267))
// => { y: -0.9986295347545739, x: 267 }
console.log(binarySearch(fun, 90, 270))
// => { y: -1, x: 270 }
Parameters:
| Name | Type | Attributes | Default | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
fun |
function | 輸入適應函數,將傳入變數x,需回傳適應函數值,以求解最小值為目標。種類:函數,必給 |
|||||||||||||||||
xMin |
Number | String | 輸入初始搜尋範圍最小值數字或數字字串。種類:有效數字,必給,數字字串轉換後使用,'-'、'NP'、''等非數字字串與null、布林、NaN、Infinity皆為無效 |
|||||||||||||||||
xMax |
Number | String | 輸入初始搜尋範圍最大值數字或數字字串。種類:有效數字,必給,處置同xMin |
|||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{}。種類:一般物件,undefined或null視為未給,其餘非一般物件為無效;未宣告之鍵忽略;各鍵給了而不符種類即為無效,不改用預設 Properties
|
Returns:
回傳求解後結果物件,含鍵值x,y,x為求解後變數組,y為最優適應函數值;輸入無效時同步throw Error,訊息格式為invalid <名稱>: must be <種類描述>,不含輸入值
- Type
- Object
(async, static) cobyla(fun, params, optopt) → {Promise}
- Description:
Michael J. D. Powell之Cobyla求解器
Fork: Cobyla.js
Rela: 张在坤/无导数优化方法的研究
Unit Test: Github
- Source:
Example
async function test() {
async function fun(params) {
let x = params[0] / 180 * Math.PI
return Math.sin(x)
}
console.log(await cobyla(fun, [0]))
// => { count: 78, y: -1, x: [ -90.0000000000001 ] }
console.log(await cobyla(fun, [87]))
// => { count: 58, y: -1, x: [ -90.00000057220495 ] }
console.log(await cobyla(fun, [90]))
// => { count: 58, y: -1, x: [ 270 ] }
}
test()
.catch((err) => {
console.log(err)
})
Parameters:
| Name | Type | Attributes | Default | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
fun |
function | 輸入適應函數,將傳入變數組params,需回傳適應函數值,以求解最小值為目標。種類:函數,必給 |
||||||||||||||||||||||
params |
Array | 輸入初始變數組,若適應函數fun需輸入3參數,則就需輸入[a,b,c]陣列作為初始值。種類:資料陣列,必給,非空陣列,元素為有效數字:數字或數字字串(轉換後使用),'-'、'NP'、''等非數字字串與null、布林、NaN、Infinity皆為無效,訊息帶元素索引;不改寫呼叫端之陣列 |
||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{}。種類:一般物件,undefined或null視為未給,其餘非一般物件為無效;未宣告之鍵忽略;各鍵給了而不符種類即為無效,不改用預設 Properties
|
Returns:
回傳Promise,resolve為求解後結果物件,含鍵值x,y,x為求解後變數組,y為最優適應函數值;輸入無效時reject字串,格式為invalid <名稱>: must be <種類描述>,不含輸入值
- Type
- Promise
(async, static) goldenSection(fun, xL, xU, optopt) → {Promise}
- Description:
黃金比例搜尋法(1維),求解最小值所在
Fork: golden-section-minimize.js
Unit Test: Github
- Source:
Example
async function test() {
function fun(param) {
let x = param / 180 * Math.PI
return Math.sin(x)
}
console.log(await goldenSection(fun, -360, 360))
// => { count: 56, y: -1, x: -89.99999939558693 }
}
test()
.catch((err) => {
console.log(err)
})
Parameters:
| Name | Type | Attributes | Default | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
fun |
function | 輸入適應函數,將傳入變數x,需回傳適應函數值,以求解最小值所在起訖範圍為目標。種類:函數,必給 |
|||||||||||||||||
xL |
Number | String | 輸入初始最小值數字或數字字串。種類:有效數字,必給,數字字串轉換後使用,'-'、'NP'、''等非數字字串與null、布林、NaN、Infinity皆為無效 |
|||||||||||||||||
xU |
Number | String | 輸入初始最大值數字或數字字串。種類:有效數字,必給,處置同xL |
|||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{}。種類:一般物件,undefined或null視為未給,其餘非一般物件為無效;未宣告之鍵忽略;各鍵給了而不符種類即為無效,不改用預設 Properties
|
Returns:
回傳Promise,resolve為求解後結果物件,含鍵值x,y,x為求解後變數組,y為最優適應函數值;輸入無效時reject字串,格式為invalid <名稱>: must be <種類描述>,不含輸入值
- Type
- Promise
(async, static) goldenSectionLiberate(fun, x, optopt) → {Promise}
- Description:
黃金比例搜尋法(1維),求解最小值所在
Fork: minimize-golden-section-1d.js
Unit Test: Github
- Source:
Example
async function test() {
function fun(param) {
let x = param / 180 * Math.PI
return Math.sin(x)
}
console.log(await goldenSectionLiberate(fun, 0))
// => { count: 67, y: -1, x: -89.99999939787351 }
}
test()
.catch((err) => {
console.log(err)
})
Parameters:
| Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
fun |
function | 輸入適應函數,將傳入變數x,需回傳適應函數值,以求解最小值所在起訖範圍為目標。種類:函數,必給 |
|||||||||||||||||||||||||||||||||||||
x |
Number | String | 輸入初始值數字或數字字串。種類:有效數字,必給,數字字串轉換後使用,'-'、'NP'、''等非數字字串與null、布林、NaN、Infinity皆為無效 |
|||||||||||||||||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{}。種類:一般物件,undefined或null視為未給,其餘非一般物件為無效;未宣告之鍵忽略;各鍵給了而不符種類即為無效,不改用預設 Properties
|
Returns:
回傳Promise,resolve為求解後結果物件,含鍵值x,y,x為求解後變數組,y為最優適應函數值;輸入無效時reject字串,格式為invalid <名稱>: must be <種類描述>,不含輸入值
- Type
- Promise
(async, static) limitBFGS(fun, params, optopt) → {Promise}
- Description:
L-BFGS法
Doc: 理解L-BFGS算法 Fork: BFGSAlgorithm.js
Unit Test: Github
- Source:
Example
async function test() {
async function fun(params) {
let x = params[0] / 180 * Math.PI
return Math.sin(x)
}
console.log(await limitBFGS(fun, [0]))
// => { count: 85, y: -1, x: [ -90.00000038876749 ] }
console.log(await limitBFGS(fun, [87]))
// => { count: 98, y: -1, x: [ -90.00000021058614 ] }
console.log(await limitBFGS(fun, [90]))
// => { count: 2, y: 100000000000000000000, x: null }
}
test()
.catch((err) => {
console.log(err)
})
Parameters:
| Name | Type | Attributes | Default | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
fun |
function | 輸入適應函數,將傳入變數組params,需回傳適應函數值,以求解最小值為目標。種類:函數,必給 |
||||||||||||||||||||||
params |
Array | 輸入初始變數組,若適應函數fun需輸入3參數,則就需輸入[a,b,c]陣列作為初始值。種類:資料陣列,必給,非空陣列,元素為有效數字:數字或數字字串(轉換後使用),'-'、'NP'、''等非數字字串與null、布林、NaN、Infinity皆為無效,訊息帶元素索引;不改寫呼叫端之陣列 |
||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{}。種類:一般物件,undefined或null視為未給,其餘非一般物件為無效;未宣告之鍵忽略;各鍵給了而不符種類即為無效,不改用預設 Properties
|
Returns:
回傳Promise,resolve為求解後結果物件,含鍵值x,y,x為求解後變數組,y為最優適應函數值;輸入無效時reject字串,格式為invalid <名稱>: must be <種類描述>,不含輸入值
- Type
- Promise
(async, static) nelderMead(fun, params, optopt) → {Promise}
- Description:
下山單純形法
Fork: nelderMead.js
Rela: 张在坤/无导数优化方法的研究
Unit Test: Github
- Source:
Example
async function test1() {
async function fun(params) {
let x = params[0] / 180 * Math.PI
return Math.sin(x)
}
console.log(await nelderMead(fun, [0]))
// => { count: 78, y: -1, x: [ -90.0000000000001 ] }
console.log(await nelderMead(fun, [87]))
// => { count: 58, y: -1, x: [ -90.00000057220495 ] }
console.log(await nelderMead(fun, [90]))
// => { count: 58, y: -1, x: [ 270 ] }
}
test1()
.catch((err) => {
console.log(err)
})
async function test2() {
async function fun(params) {
let x = params[0]
let y = params[1]
return Math.sin(y) * x + Math.sin(x) * y + x * x + y * y
}
console.log(await nelderMead(fun, [-3.5, 3.5]))
// => {
// count: 130,
// y: 5.786322126017525e-19,
// x: [ 0.000007191110664735547, -0.00000719035057196422 ]
// }
}
test2()
.catch((err) => {
console.log(err)
})
async function test3() {
let t = `
0
3
4
5 157
6 164
7 186
8 177
9 178
10 188
11 189
12 180
13 180
14 197
15 198
16 200
17 214
18 221
19 219
20 248
21 252
22 244
23 289
24 282
25 276
26 306
27 299
28 269
29 282
30 287
31 298
32 275
33 273
34 269
35 281
36 288
37 310
38 308
39 297
40 301
41 282
42 304
43 291
44 275
45 279
46 300
47 309
48 306
49 367
50 308
51 315
52 282
53 302
54 271
55 290
56 290
57 299
58 290
59 304
60 308
61 306
62 309
63 351
64 309
65 306
66 297
67 334
68 304
69 323
70 318
71 328
72 295
73 301
74 295
75 297
76 308
77 295
78 345
79 361
80 370
81 381
82 334
83 374
84 385
85 354
86 311
87 345
88 331
89 364
90 339
91 311
92 323
93 295
94 318
95 310
96 315
97 315
98 358
99 342
100 351
`
let ps = w.sep(t, '\n')
let _ps = []
_.each(ps, (v) => {
let s = w.sep(v, ' ')
let Depth = _.get(s, 0, '')
let Vs = _.get(s, 1, '')
if (w.isnum(Depth) && w.isnum(Vs)) {
_ps.push({
Depth: w.cdbl(Depth),
Vs: w.cdbl(Vs),
})
}
ps = _ps
})
// console.log(ps)
async function fun(params) {
//Vs=166.92*ln(x+35)-455.84
//a=166.02, b=35, c=-455.84
let a = params[0]
let b = params[1]
let c = params[2]
let fitness = 0
_.each(ps, (v) => {
let d = Math.max(v.Depth + b, 0.001) //深度+b需>0
let Vs = a * Math.log(d) + c
Vs = Math.max(Vs, 0) //不能給予0.001, 否則適應函數為分連續可微
let dy = Vs - v.Vs
// fitness += dy * dy
fitness += Math.abs(dy)
})
// fitness = Math.sqrt(fitness)
// console.log('x', x, 'fitness', fitness)
return fitness
}
console.log(await nelderMead(fun, [0, 0, 0])) //初始值影響很大, 用0,0,0比較是曲線, 否則很容易找到高係數而近直線之回歸線
// => {
// count: 326,
// y: 1782.0083185373996,
// x: [ 79.27689137899918, 4.16685541895392, -19.853651133415656 ]
// }
}
test3()
.catch((err) => {
console.log(err)
})
Parameters:
| Name | Type | Attributes | Default | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
fun |
function | 輸入適應函數,將傳入變數組params,需回傳適應函數值,以求解最小值為目標。種類:函數,必給 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
params |
Array | 輸入初始變數組,若適應函數fun需輸入3參數,則就需輸入[a,b,c]陣列作為初始值。種類:資料陣列,必給,非空陣列,元素為有效數字:數字或數字字串(轉換後使用),'-'、'NP'、''等非數字字串與null、布林、NaN、Infinity皆為無效,訊息帶元素索引;不改寫呼叫端之陣列 |
||||||||||||||||||||||||||||||||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{}。種類:一般物件,undefined或null視為未給,其餘非一般物件為無效;未宣告之鍵忽略;各鍵給了而不符種類即為無效,不改用預設 Properties
|
Returns:
回傳Promise,resolve為求解後結果物件,含鍵值x,y,x為求解後變數組,y為最優適應函數值;輸入無效時reject字串,格式為invalid <名稱>: must be <種類描述>,不含輸入值
- Type
- Promise
(async, static) omlACO(dps, funFit, optopt) → {Promise}
- Description:
蟻群最佳化(Ant Colony Optimization)
Unit Test: Github
- Source:
Parameters:
| Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dps |
Array | 輸入設計變數陣列,各元素為{ values: [可用值], n: 可用值數量 }。種類:非空陣列,必給;元素為一般物件,values為非空陣列(元素為可用值,任意值,由funFit自行解讀,不檢),n為有效數字,正整數(數字字串轉換後使用)且須等於values長度;不改寫呼叫端之陣列 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
funFit |
function | 輸入適應函數,將傳入各設計變數所取之值陣列,需回傳適應函數值,以求解最小值為目標。種類:函數,必給 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{}。種類:一般物件,undefined或null視為未給,其餘非一般物件為無效;未宣告之鍵忽略;各鍵給了而不符種類即為無效,不改用預設 Properties
|
Returns:
回傳Promise,resolve為結果物件,含鍵值bestSolution,hists,stopMode,stopNl,stopExecutions;輸入無效時reject字串,格式為invalid <名稱>: must be <種類描述>,不含輸入值
- Type
- Promise
(async, static) omlDE(dps, funFit, optopt) → {Promise}
Parameters:
| Name | Type | Attributes | Default | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dps |
Array | 輸入設計變數陣列,各元素為{ values: [可用值], n: 可用值數量 }。種類:非空陣列,必給;元素為一般物件,values為非空陣列(元素為可用值,任意值,由funFit自行解讀,不檢),n為有效數字,正整數(數字字串轉換後使用)且須等於values長度;不改寫呼叫端之陣列 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
funFit |
function | 輸入適應函數,將傳入各設計變數所取之值陣列,需回傳適應函數值,以求解最小值為目標。種類:函數,必給 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{}。種類:一般物件,undefined或null視為未給,其餘非一般物件為無效;未宣告之鍵忽略;各鍵給了而不符種類即為無效,不改用預設 Properties
|
Returns:
回傳Promise,resolve為結果物件,含鍵值bestSolution,hists,stopMode,stopNg,stopExecutions;輸入無效時reject字串,格式為invalid <名稱>: must be <種類描述>,不含輸入值
- Type
- Promise
(async, static) omlHS(dps, funFit, optopt) → {Promise}
Parameters:
| Name | Type | Attributes | Default | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dps |
Array | 輸入設計變數陣列,各元素為{ values: [可用值], n: 可用值數量 }。種類:非空陣列,必給;元素為一般物件,values為非空陣列(元素為可用值,任意值,由funFit自行解讀,不檢),n為有效數字,正整數(數字字串轉換後使用)且須等於values長度;不改寫呼叫端之陣列 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
funFit |
function | 輸入適應函數,將傳入各設計變數所取之值陣列,需回傳適應函數值,以求解最小值為目標。種類:函數,必給 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{}。種類:一般物件,undefined或null視為未給,其餘非一般物件為無效;未宣告之鍵忽略;各鍵給了而不符種類即為無效,不改用預設 Properties
|
Returns:
回傳Promise,resolve為結果物件,含鍵值bestSolution,hists,stopMode,stopNi,stopExecutions;輸入無效時reject字串,格式為invalid <名稱>: must be <種類描述>,不含輸入值
- Type
- Promise
(async, static) omlPSO(dps, funFit, optopt) → {Promise}
- Description:
粒子群最佳化(Particle Swarm Optimization)
Unit Test: Github
- Source:
Parameters:
| Name | Type | Attributes | Default | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dps |
Array | 輸入設計變數陣列,各元素為{ values: [可用值], n: 可用值數量 }。種類:非空陣列,必給;元素為一般物件,values為非空陣列(元素為可用值,任意值,由funFit自行解讀,不檢),n為有效數字,正整數(數字字串轉換後使用)且須等於values長度;不改寫呼叫端之陣列 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
funFit |
function | 輸入適應函數,將傳入各設計變數所取之值陣列,需回傳適應函數值,以求解最小值為目標。種類:函數,必給 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{}。種類:一般物件,undefined或null視為未給,其餘非一般物件為無效;未宣告之鍵忽略;各鍵給了而不符種類即為無效,不改用預設 Properties
|
Returns:
回傳Promise,resolve為結果物件,含鍵值bestSolution,hists,stopMode,stopNl,stopExecutions;輸入無效時reject字串,格式為invalid <名稱>: must be <種類描述>,不含輸入值
- Type
- Promise
(async, static) omlRGA(dps, funFit, optopt) → {Promise}
- Description:
實數編碼基因演算法(Real-coded Genetic Algorithm)
Unit Test: Github
- Source:
Parameters:
| Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dps |
Array | 輸入設計變數陣列,各元素為{ values: [可用值], n: 可用值數量 }。種類:非空陣列,必給;元素為一般物件,values為非空陣列(元素為可用值,任意值,由funFit自行解讀,不檢),n為有效數字,正整數(數字字串轉換後使用)且須等於values長度;不改寫呼叫端之陣列 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
funFit |
function | 輸入適應函數,將傳入各設計變數所取之值陣列,需回傳適應函數值,以求解最小值為目標。種類:函數,必給 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{}。種類:一般物件,undefined或null視為未給,其餘非一般物件為無效;未宣告之鍵忽略;各鍵給了而不符種類即為無效,不改用預設 Properties
|
Returns:
回傳Promise,resolve為結果物件,含鍵值bestSolution,hists,stopMode,stopNg,stopExecutions;輸入無效時reject字串,格式為invalid <名稱>: must be <種類描述>,不含輸入值
- Type
- Promise
(async, static) omlSA(dps, funFit, optopt) → {Promise}
- Description:
模擬退火(Simulated Annealing)
此版本同時支援「主演算法」與「LocalSearch helper」雙用途: 主演算法不傳opt.initSolution,內部隨機初始化;LocalSearch傳opt.initSolution與opt.calcFitness(共享caller之iExecute counter)
Unit Test: Github
- Source:
Parameters:
| Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dps |
Array | 輸入設計變數陣列,各元素為{ values: [可用值], n: 可用值數量 }。種類:非空陣列,必給;元素為一般物件,values為非空陣列(元素為可用值,任意值,由funFit自行解讀,不檢),n為有效數字,正整數(數字字串轉換後使用)且須等於values長度;不改寫呼叫端之陣列 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
funFit |
function | 輸入適應函數,將傳入各設計變數所取之值陣列,需回傳適應函數值,以求解最小值為目標。種類:函數,必給 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{}。種類:一般物件,undefined或null視為未給,其餘非一般物件為無效;未宣告之鍵忽略;各鍵給了而不符種類即為無效,不改用預設 Properties
|
Returns:
回傳Promise,resolve為結果物件,含鍵值bestSolution,hists,stopMode,stopNl,stopExecutions;輸入無效時reject字串,格式為invalid <名稱>: must be <種類描述>,不含輸入值
- Type
- Promise
(async, static) omlTA(dps, funFit, optopt) → {Promise}
- Description:
門檻接受法(Threshold Accepting,SA之確定性變體)
此版本同時支援「主演算法」與「LocalSearch helper」雙用途: 主演算法不傳opt.initSolution,內部隨機初始化;LocalSearch傳opt.initSolution與opt.calcFitness(共享caller之iExecute counter)
Unit Test: Github
- Source:
Parameters:
| Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dps |
Array | 輸入設計變數陣列,各元素為{ values: [可用值], n: 可用值數量 }。種類:非空陣列,必給;元素為一般物件,values為非空陣列(元素為可用值,任意值,由funFit自行解讀,不檢),n為有效數字,正整數(數字字串轉換後使用)且須等於values長度;不改寫呼叫端之陣列 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
funFit |
function | 輸入適應函數,將傳入各設計變數所取之值陣列,需回傳適應函數值,以求解最小值為目標。種類:函數,必給 |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{}。種類:一般物件,undefined或null視為未給,其餘非一般物件為無效;未宣告之鍵忽略;各鍵給了而不符種類即為無效,不改用預設 Properties
|
Returns:
回傳Promise,resolve為結果物件,含鍵值bestSolution,hists,stopMode,stopNl,stopExecutions;輸入無效時reject字串,格式為invalid <名稱>: must be <種類描述>,不含輸入值
- Type
- Promise
(async, static) sdm(dps, funFit, optopt) → {Promise}
- Description:
最陡下降法(Steepest Descent Method)
此版本同時支援「主演算法」與「LocalSearch helper」雙用途
Unit Test: Github
- Source:
Parameters:
| Name | Type | Attributes | Default | Description | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
dps |
Array | 輸入設計變數陣列,各元素為{ values: [可用值], n: 可用值數量 }。種類:非空陣列,必給;元素為一般物件,values為非空陣列(元素為可用值,任意值,由funFit自行解讀,不檢),n為有效數字,正整數(數字字串轉換後使用)且須等於values長度;不改寫呼叫端之陣列 |
||||||||||||||||||||||||||||||||
funFit |
function | 輸入適應函數,將傳入各設計變數所取之值陣列,需回傳適應函數值,以求解最小值為目標。種類:函數,必給 |
||||||||||||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{}。種類:一般物件,undefined或null視為未給,其餘非一般物件為無效;未宣告之鍵忽略;各鍵給了而不符種類即為無效,不改用預設 Properties
|
Returns:
回傳Promise,resolve為結果物件,含鍵值bestSolution,stopExecutions;輸入無效時reject字串,格式為invalid <名稱>: must be <種類描述>,不含輸入值
- Type
- Promise
(async, static) setpBracket(fun, x, optopt) → {Promise}
- Description:
步階搜尋法(1維),求解最小值所在起訖範圍
Fork: bracket-minimum.js
Unit Test: Github
- Source:
Example
async function test() {
function fun(param) {
let x = param / 180 * Math.PI
return Math.sin(x)
}
console.log(await setpBracket(fun, 0, { min: -360, max: 360 }))
// => {
// count: 13,
// bounded: true,
// min: -204.70000000000002,
// max: 0.1,
// guess: -102.30000000000001,
// fMin: 0.4178670738010769,
// fMax: 0.0017453283658983088,
// fGuess: -0.9770455744352636
// }
console.log(await setpBracket(fun, 87, { min: -360, max: 360 }))
// => {
// count: 14,
// bounded: true,
// min: -322.5,
// max: 87.1,
// guess: -117.70000000000002,
// fMin: 0.6087614290087209,
// fMax: 0.9987193571841863,
// fGuess: -0.8853936257544158
//}
console.log(await setpBracket(fun, 90, { min: -360, max: 360 }))
// => {
// count: 20,
// bounded: true,
// min: -319.5,
// max: 102.7,
// guess: -114.70000000000002,
// fMin: 0.6494480483301841,
// fMax: 0.9755345439458566,
// fGuess: -0.9085081775267217
//}
}
test()
.catch((err) => {
console.log(err)
})
Parameters:
| Name | Type | Attributes | Default | Description | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
fun |
function | 輸入適應函數,將傳入變數x,需回傳適應函數值,以求解最小值所在起訖範圍為目標。種類:函數,必給 |
|||||||||||||||||||||||||||
x |
Number | String | 輸入初始值數字或數字字串。種類:有效數字,必給,數字字串轉換後使用,'-'、'NP'、''等非數字字串與null、布林、NaN、Infinity皆為無效 |
|||||||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{}。種類:一般物件,undefined或null視為未給,其餘非一般物件為無效;未宣告之鍵忽略;各鍵給了而不符種類即為無效,不改用預設 Properties
|
Returns:
回傳Promise,resolve為求解後結果物件,含鍵值count,bounded,min,max,guess,fMin,fMax,fGuess;輸入無效時reject字串,格式為invalid <名稱>: must be <種類描述>,不含輸入值
- Type
- Promise