Skip to content

Commit 3529fde

Browse files
authored
Changed to a different method of generating arrays using binary numbers (#753)
1 parent 9545c4e commit 3529fde

File tree

3 files changed

+53
-34
lines changed

3 files changed

+53
-34
lines changed

lib/model/bayesian_network.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -120,17 +120,13 @@ export default class BayesianNetwork {
120120
for (let i = 0; i < this._n; i++) {
121121
localScores[i] = new ArrayKeyMap()
122122
bpss[i] = new ArrayKeyMap()
123-
for (let k = 0; k < 2 ** (this._n - 1); k++) {
124-
const p = k
125-
.toString(2)
126-
.padStart(this._n - 1, '0')
127-
.split('')
128-
p.splice(i, 0, '1')
129-
const key = p.map(v => +v)
130-
const c = p
131-
.map((v, j) => [v, j])
132-
.filter(v => v[0] === '1')
133-
.map(v => v[1])
123+
const key = Array(this._n).fill(0)
124+
key[i] = 1
125+
do {
126+
const c = []
127+
for (let j = 0; j < this._n; j++) {
128+
if (key[j]) c.push(j)
129+
}
134130
const xi = x.map(r => c.map(idx => r[idx]))
135131
const cand = c.map(idx => this._cand[idx])
136132
const g = []
@@ -144,9 +140,9 @@ export default class BayesianNetwork {
144140
}
145141
}
146142
const score = this._score(xi, g, cand)
147-
localScores[i].set(key, score)
143+
localScores[i].set(key.concat(), score)
148144

149-
let bps = [key, score]
145+
let bps = [key.concat(), score]
150146
for (let d = 0; d < key.length; d++) {
151147
if (key[d] === 0) {
152148
continue
@@ -161,17 +157,22 @@ export default class BayesianNetwork {
161157
bps = bscore
162158
}
163159
}
164-
bpss[i].set(key, bps)
165-
}
160+
bpss[i].set(key.concat(), bps)
161+
for (let j = this._n - 1; j >= 0; j--) {
162+
if (i === j) continue
163+
if (!key[j]) {
164+
key[j] = 1
165+
break
166+
}
167+
key[j] = 0
168+
}
169+
} while (key.some((v, j) => j !== i && v === 1))
166170
}
167171

168172
const sinkScores = new ArrayKeyMap()
169-
for (let k = 1; k < 2 ** this._n; k++) {
170-
const key = k
171-
.toString(2)
172-
.padStart(this._n, '0')
173-
.split('')
174-
.map(v => +v)
173+
const key = Array(this._n).fill(0)
174+
key[this._n - 1] = 1
175+
do {
175176
const m = key.reduce((s, v) => s + v, 0)
176177
let sink = [null, -Infinity]
177178
for (let i = 0; i < bpss.length; i++) {
@@ -189,8 +190,15 @@ export default class BayesianNetwork {
189190
}
190191
}
191192

192-
sinkScores.set(key, sink)
193-
}
193+
sinkScores.set(key.concat(), sink)
194+
for (let j = this._n - 1; j >= 0; j--) {
195+
if (!key[j]) {
196+
key[j] = 1
197+
break
198+
}
199+
key[j] = 0
200+
}
201+
} while (key.some(v => v === 1))
194202

195203
const v = Array(this._n).fill(1)
196204
const order = []

lib/model/n_linear_interpolation.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,17 @@ export default class NLinearInterpolation {
1515
this._grids = grids
1616

1717
this._offset_idx = []
18-
for (let k = 0; k < 2 ** this._grids.length; k++) {
19-
this._offset_idx.push(k.toString(2).padStart(this._grids.length, '0').split(''))
20-
}
18+
const idx = Array(this._grids.length).fill(0)
19+
do {
20+
this._offset_idx.push(idx.concat())
21+
for (let i = idx.length - 1; i >= 0; i--) {
22+
if (!idx[i]) {
23+
idx[i] = 1
24+
break
25+
}
26+
idx[i] = 0
27+
}
28+
} while (idx.some(v => v === 1))
2129
}
2230

2331
/**
@@ -37,7 +45,7 @@ export default class NLinearInterpolation {
3745
for (let i = 0; i < this._grids[d].length && t[d] >= this._grids[d][i]; k = i++);
3846
i[d] = k === this._grids[d].length - 1 ? k - 1 : k
3947
}
40-
const values = this._offset_idx.map(off => off.reduce((v, f, d) => v[i[d] + +f], this._v))
48+
const values = this._offset_idx.map(off => off.reduce((v, f, d) => v[i[d] + f], this._v))
4149

4250
for (let d = 0; d < this._grids.length; d++) {
4351
const v1 = this._grids[d][i[d]]

lib/model/split_merge.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,17 @@ export default class SplitAndMerge {
6767
const n = range.map(r => r[1] - r[0])
6868
const c = n.map(v => Math.floor(v / 2))
6969
const r = []
70-
for (let i = 0; i < 2 ** n.length; i++) {
71-
const p = i
72-
.toString(2)
73-
.padStart(n.length, '0')
74-
.split('')
75-
.map(v => !!+v)
70+
const p = Array(n.length).fill(false)
71+
do {
7672
r.push(p.map((v, i) => (v ? [0, c[i]] : [c[i], n[i]])))
77-
}
73+
for (let i = 0; i < p.length; i++) {
74+
if (!p[i]) {
75+
p[i] = true
76+
break
77+
}
78+
p[i] = false
79+
}
80+
} while (p.some(v => v))
7881

7982
for (const [r1, r2] of r) {
8083
const d = []

0 commit comments

Comments
 (0)