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 | 輸入數據陣列 |
||||||||||||
x |
Number | 輸入尋找數字 |
||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{} Properties
|
Returns:
回傳求解後結果物件,含鍵值x,y,x為求解後變數組,y為最優適應函數值
- 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 | 輸入數據陣列 |
||||||||||||
x |
Number | 輸入尋找數字 |
||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{} Properties
|
Returns:
回傳求解後最接近值物件,含鍵值x,y,x為求解後變數組,y為最優適應函數值
- Type
- Object
(static) binarySearch(fun, xMin, xMax, delta, 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 | 輸入適應函數,將傳入變數組params,需回傳適應函數值,以求解最小值為目標 |
||||||||||||
xMin |
Number | 輸入初始搜尋範圍最小值數字 |
||||||||||||
xMax |
Number | 輸入初始搜尋範圍最大值數字 |
||||||||||||
delta |
Number | 輸入步長數字 |
||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{} Properties
|
Returns:
回傳求解後結果物件,含鍵值x,y,x為求解後變數組,y為最優適應函數值
- 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]陣列作為初始值 |
|||||||||||||||||||||||||||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{} Properties
|
Returns:
回傳Promise,resolve為求解後結果物件,含鍵值x,y,x為求解後變數組,y為最優適應函數值,reject為失敗訊息
- 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 | 輸入初始最小值數字 |
|||||||||||||||||
xU |
Number | 輸入初始最大值數字 |
|||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{} Properties
|
Returns:
回傳Promise,resolve為求解後結果物件,含鍵值x,y,x為求解後變數組,y為最優適應函數值,reject為失敗訊息
- Type
- Promise
(async, static) goldenSectionLiberate(fun, 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,需回傳適應函數值,以求解最小值所在起訖範圍為目標 |
|||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{} Properties
|
Returns:
回傳Promise,resolve為求解後結果物件,含鍵值x,y,x為求解後變數組,y為最優適應函數值,reject為失敗訊息
- 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]陣列作為初始值 |
||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{} Properties
|
Returns:
回傳Promise,resolve為求解後結果物件,含鍵值x,y,x為求解後變數組,y為最優適應函數值,reject為失敗訊息
- 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]陣列作為初始值 |
|||||||||||||||||||||||||||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{} Properties
|
Returns:
回傳Promise,resolve為求解後結果物件,含鍵值x,y,x為求解後變數組,y為最優適應函數值,reject為失敗訊息
- 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 | 輸入初始值數字 |
||||||||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{} Properties
|
Returns:
回傳Promise,resolve為求解後結果物件,含鍵值x,y,x為求解後變數組,y為最優適應函數值,reject為失敗訊息
- Type
- Promise