-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.js
128 lines (128 loc) · 4.02 KB
/
solver.js
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
function solver(type, typeOrder, typeWeight, typeWeightZero, setSuccess, goal, statMin, randomCount) {
function achieveCheck(stat, statMin) {
let achieve = 0;
let i = stat.length;
while (i--)
if (statMin[i] != -1)
achieve += Math.min(stat[i], statMin[i]);
return achieve == goal ? 1 : achieve / goal;
}
function cloneArr(arr) {
let clone = [];
for (let i = arr.length - 1; i >= 0; i--) {
clone[i] = arr[i].concat();
}
return clone;
}
let failCount = 0;
sampling: while (randomCount--) {
let relic = 0;
let epic = 0;
let currentStat = [0, 0, 0, 0, 0, 0];
let currentIndex = [];
let currentID = [];
let currentScore = 0;
for (let i = type.length - 1; i >= 0; i--) {
let j = Math.floor(Math.random() * (i + 1));
[typeOrder[i], typeOrder[j]] = [typeOrder[j], typeOrder[i]];
}
for (let m = type.length - 1; m >= 0; m--) {
let i = typeOrder[m];
let j;
let typeWeightTemp = [];
for (let n = 0; n < 999; n++) {
let k = Math.floor(Math.random() * typeWeight[i].length);
j = typeWeight[i][k];
if (!typeWeightTemp.includes(j))
typeWeightTemp.push(j);
else if (typeWeightTemp.length == typeWeightZero[i].length)
break;
if (
(type[i][j][1] == 0 && relic > 0)
|| (type[i][j][1] == 1 && epic > 0)
|| (i == 7 && j == currentIndex[9])
|| (i == 9 && j == currentIndex[7])
)
continue;
break;
}
currentIndex[i] = j;
currentID[i] = type[i][j][0];
if (type[i][j][1] == 0)
relic++;
if (type[i][j][1] == 1 || (currentID[i] > 26493 && currentID[i] < 26498))
epic++;
if (relic > 1 || epic > 1)
continue sampling;
currentScore += type[i][j][8];
for (let m = statMin.length - 1; m >= 0; m--) {
let n = m + 2;
currentStat[m] += type[i][j][n];
}
}
let achieveRate = achieveCheck(currentStat, statMin);
if (achieveRate == 1) {
for (let i = 0; i < type.length; i++) {
if (type[i][currentIndex[i]][1] < 2)
continue;
for (let j of typeWeightZero[i]) {
if (j == currentIndex[i])
break;
if (
type[i][j][1] < 2
|| (i == 7 && j == currentIndex[9])
|| (i == 9 && j == currentIndex[7])
)
continue;
let tempStat = currentStat.concat();
for (let m = 0; m < statMin.length; m++) {
let n = m + 2;
tempStat[m] += type[i][j][n] - type[i][currentIndex[i]][n];
}
if (achieveCheck(tempStat, statMin) == 1) {
currentID[i] = type[i][j][0];
currentScore += type[i][j][8] - type[i][currentIndex[i]][8];
currentStat = tempStat.concat();
currentIndex[i] = j;
break;
}
}
}
if (currentID[10] > 26493 && currentID[10] < 26498) {
currentStat.every((e, i) => currentStat[i] -= type[9][currentIndex[9]][i + 2]);
if (achieveCheck(currentStat, statMin) < 1)
continue sampling;
currentScore -= type[9][currentIndex[9]][8];
currentID[9] = currentID[10] + 81;
}
typeWeight = cloneArr(typeWeightZero);
if (setSuccess.length > 0) {
for (let i = type.length - 1; i >= 0; i--) {
for (let j = Math.ceil(setSuccess.length / 10); j >= 0; j--) {
let k = Math.floor(Math.random() * setSuccess.length);
typeWeight[i].push(setSuccess[k].index[i]);
}
}
}
if (currentID[7] == currentID[9] || setSuccess.some(x => currentID.every(y => x.id.some(z => y == z))))
continue;
setSuccess.push({id:currentID, score:currentScore.toFixed(1), index:currentIndex, stat:currentStat});
if (setSuccess.length > 99)
setSuccess.sort((a, b) => b.score - a.score).splice(50);
}
else if (achieveRate > 0.8) {
failCount++;
if (failCount > 99) {
typeWeight = cloneArr(typeWeightZero);
failCount = 0;
}
else {
for (let i = type.length - 1; i >= 0; i--) {
typeWeight[i].push(currentIndex[i]);
}
}
}
}
return setSuccess;
}
onmessage = e => postMessage(solver(e.data[0], e.data[1], e.data[2], e.data[3], e.data[4], e.data[5], e.data[6], e.data[7]));