Created
June 14, 2019 14:17
-
-
Save Tsuk1ko/124561025098a1ee8f3639ec2438f5a6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// npm i axios lodash javascript-lp-solver | |
const _ = require('lodash'); | |
const linprog = require('javascript-lp-solver/src/solver'); | |
const { get } = require('axios'); | |
const url_stats = 'https://penguin-stats.io/PenguinStats/api/result/matrix?show_stage_details=true&show_item_details=true'; | |
const url_rules = 'https://ak.graueneko.xyz/akmaterial.json'; | |
const slove = async need => { | |
let data_stats = await get(url_stats).then(r => r.data); | |
let data_rules = await get(url_rules).then(r => r.data); | |
// 处理合成列表 | |
let synthesisTable = {}; | |
let materialConstraints = {}; | |
for (let { name, madeof } of data_rules) { | |
materialConstraints[name] = { min: 0 }; | |
if (_.size(madeof) == 0) continue; | |
let product = {}; | |
product[name] = 1; | |
synthesisTable[`合成-${name}`] = { | |
...product, | |
..._.mapValues(madeof, v => -v), | |
cost: 0 | |
}; | |
} | |
// 处理掉落信息 | |
let dropTable = {}; | |
for (let m of data_stats.matrix) { | |
if (!(m.item.name in materialConstraints)) continue; | |
let { | |
item: { name }, | |
stage: { apCost, code }, | |
quantity, | |
times | |
} = m; | |
if (!dropTable[code]) dropTable[code] = { cost: apCost }; | |
dropTable[code][name] = quantity / times; | |
} | |
// 线性规划 | |
let variables = Object.assign({}, synthesisTable, dropTable); | |
let model = { | |
optimize: 'cost', | |
opType: 'min', | |
constraints: { | |
...materialConstraints, | |
..._.mapValues(need, v => ({ min: v })) | |
}, | |
variables | |
//限定合成次数为整数 | |
//ints: _.transform(synthesisTable, (r, v, k) => (r[k] = 1), {}) | |
}; | |
return linprog.Solve(model); | |
}; | |
slove({ 聚合剂: 300, 双极纳米片: 300, D32钢: 300 }).then(console.log); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment