Convert dole code to follow project style

This commit is contained in:
Hornwitser committed 2020-10-23 21:22:45 +02:00
1 parent 8586dfb95f
commit ceade431ef
2 files changed
+109 -64

No files matched your search

+37 -24
View File
@@ -6,7 +6,8 @@ const { Gauge } = require("@clusterio/lib/prometheus");
const prometheusNNDoleGauge = new Gauge(
"clusterio_subspace_storage_nn_dole_gauge",
"Current demand being supplied by Neural Network ; 1 means all demand covered, 0.5 means about half of each supply is covered, 0 means no items are given",
"Current demand being supplied by Neural Network ; 1 means all demand covered, "+
"0.5 means about half of each supply is covered, 0 means no items are given",
{ labels: ["resource"] }
);
const prometheusDoleFactorGauge = new Gauge(
@@ -16,13 +17,13 @@ const prometheusDoleFactorGauge = new Gauge(
);
class NeuralDole {
getRequestStats(itemname,samples)
{
var sum=0;
getRequestStats(itemname, samples) {
let sum = 0;
samples = Math.min(samples, (this.stats[itemname] || []).length - 1);
if (samples<1) return 0.1;
for (var i = 1; i <= samples; i++) {
if (samples < 1) {
return 0.1;
}
for (let i = 1; i <= samples; i++) {
sum += this.stats[itemname][i].req;
}
if (sum === 0) {
@@ -54,7 +55,9 @@ class NeuralDole {
);
this.stats[name] = this.stats[name] || [];
this.stats[name].unshift({ req: 0, given: 0 }); // stats[name][0] is the one we currently collect
if (this.stats[name].length>10) this.stats[name].pop();//remove if too many samples in stats
if (this.stats[name].length>10) {
this.stats[name].pop(); // remove if too many samples in stats
}
this.dole[name] = magicData[0];
// DONE handle magicData[1] for graphing for our users
@@ -69,23 +72,22 @@ class NeuralDole {
this.items.getItemCount(object.name),
this.itemsLastTick.get(object.name) || 0,
this.dole[object.name],
this.carry[object.name+" "+object.instanceID] || 0,
this.lastRequest[object.name+"_"+object.instanceID+"_"+object.instanceName] || 0,
this.carry[`${object.name} ${object.instanceID}`] || 0,
this.lastRequest[`${object.name}_${object.instanceID}_${object.instanceName}`] || 0,
this.getRequestStats(object.name, 5),
this.debt[object.name+" "+object.instanceID] || 0
this.debt[`${object.name} ${object.instanceID}`] || 0
);
if ((this.stats[object.name] || []).length>0)
{
if ((this.stats[object.name] || []).length > 0) {
this.stats[object.name][0].req += Number(object.count);
this.stats[object.name][0].given += Number(magicData[0]);
}
this.lastRequest[object.name+"_"+object.instanceID+"_"+object.instanceName] = object.count;
this.lastRequest[`${object.name}_${object.instanceID}_${object.instanceName}`] = object.count;
// 0. Number of items to give in that dose
// 1. New dole for item X
// 2. New carry for item X slave Y
this.dole[object.name] = magicData[1];
this.carry[object.name+" "+object.instanceID] = magicData[2];
this.debt[object.name+" "+object.instanceID] = magicData[3];
this.carry[`${object.name} ${object.instanceID}`] = magicData[2];
this.debt[`${object.name} ${object.instanceID}`] = magicData[3];
// Remove item from DB and send it
this.items.removeItem(object.name, magicData[0]);
@@ -94,7 +96,8 @@ class NeuralDole {
}
}
// If the server regularly can't fulfill requests, this number grows until it can. Then it slowly shrinks back down.
// If the server regularly can't fulfill requests, this number grows until
// it can. Then it slowly shrinks back down.
let _doleDivisionFactor = {};
function doleDivider({
object,
@@ -102,14 +105,22 @@ function doleDivider({
logItemTransfers,
}) {
let itemCount = items.getItemCount(object.name);
const doleDivisionRetardation = 10; //lower rates will equal more dramatic swings
const maxDoleDivision = 250; //a higher cap will divide the store more ways, but will take longer to recover as supplies increase
// lower rates will equal more dramatic swings
const doleDivisionRetardation = 10;
// a higher cap will divide the store more ways, but will take longer to
// recover as supplies increase
const maxDoleDivision = 250;
const originalCount = Number(object.count) || 0;
object.count /= ((_doleDivisionFactor[object.name] || 0) + doleDivisionRetardation) / doleDivisionRetardation;
object.count = Math.round(object.count);
if (logItemTransfers) {
console.info(`Serving ${object.count}/${originalCount} ${object.name} from ${itemCount} ${object.name} with dole division factor ${(_doleDivisionFactor[object.name]||0)} (real=${((_doleDivisionFactor[object.name]||0)+doleDivisionRetardation)/doleDivisionRetardation}), item is ${itemCount > object.count?"stocked":"short"}.`);
console.info(
`Serving ${object.count}/${originalCount} ${object.name} from ${itemCount} ${object.name} `+
`with dole division factor ${(_doleDivisionFactor[object.name]||0)} `+
`(real=${((_doleDivisionFactor[object.name] || 0) + doleDivisionRetardation) / doleDivisionRetardation}), `+
`item is ${itemCount > object.count?"stocked":"short"}.`
);
}
// Update existing items if item name already exists
@@ -120,13 +131,15 @@ function doleDivider({
prometheusDoleFactorGauge.labels(object.name).set(_doleDivisionFactor[object.name] || 0);
return object.count;
} else {
}
// if we didn't have enough, attempt giving out a smaller amount next time
_doleDivisionFactor[object.name] = Math.min(maxDoleDivision, Math.max((_doleDivisionFactor[object.name]||0)||1, 1) * 2);
_doleDivisionFactor[object.name] = Math.min(
maxDoleDivision, Math.max((_doleDivisionFactor[object.name] || 0) || 1, 1) * 2
);
prometheusDoleFactorGauge.labels(object.name).set(_doleDivisionFactor[object.name] || 0);
return 0;
//console.log('failure out of ' + object.name + " | " + object.count + " from " + object.instanceID + " ("+object.instanceName+")");
}
// console.log(`failure out of ${object.name}/${object.count} from ${object.instanceID} (${object.instanceName})`);
}
module.exports = {
+72 -40
View File
@@ -1,39 +1,39 @@
// Sigmoids...
"use strict";
function sigmoid0(x)
{
function sigmoid0(x) {
return x / (1 + Math.abs(x));
}
function sigmoid1(x)
{
function sigmoid1(x) {
if (x >= 0.0) {
return 1.0;
} else {
}
return 0.0;
}
}
function sigmoid2(x)
{
function sigmoid2(x) {
if (x >= 0.0) {
return x;
} else {
}
return 0.0;
}
}
function sigmoid3(x)
{
function sigmoid3(x) {
return x;
}
// DOSE - Doses items for item X slave Y
// Numreq - number of items that are requested for item X for slave Y
// Instore - number of items for item X in master gauge (currently)
//store_last_tick - Number of items for item X in master gauge at time of last tick
// store_last_tick -
// Number of items for item X in master gauge at time of last tick
// dole - Current dole for item X
// carry - carry for item X slave Y
// prev_req - Numreq for previous request for item X slave Y
//* doleinfo_last - Object with 2 values for last "tick period": numreq - total number or requests for item, numslave - number of slaves requesting that item
// * doleinfo_last -
// Object with 2 values for last "tick period": numreq - total number or
// requests for item, numslave - number of slaves requesting that item
// debt - debt for item X slave Y
// Returns: Array of 3 elements
@@ -44,17 +44,26 @@ function sigmoid3(x)
function dose(numreq, instore, store_last_tick, dole, carry, prev_req, numreq_total_adj, debt) {
numreq = Number(numreq);
instore = Number(instore);
var instore_adj=instore;
let instore_adj = instore;
if (instore_adj === 0) {
instore_adj = 0.1;
}
var b=Math.trunc(prev_req/instore_adj*numreq);
var inp1=[numreq,instore_adj,store_last_tick/numreq,dole,carry];//0..4
var inp2=[prev_req/numreq,numreq/numreq_total_adj,store_last_tick/numreq_total_adj,prev_req/instore_adj,b,numreq_total_adj,store_last_tick,debt];//5..12
var inputs=inp1.concat(inp2);
var internal=new Array(20).fill(0);
var outputs=[0,0,0,0];
let b = Math.trunc(prev_req / instore_adj * numreq);
let inp1 = [numreq, instore_adj, store_last_tick / numreq, dole, carry]; // 0..4
let inp2 = [
prev_req / numreq,
numreq / numreq_total_adj,
store_last_tick / numreq_total_adj,
prev_req/instore_adj,
b,
numreq_total_adj,
store_last_tick,
debt,
]; // 5..12
let inputs = inp1.concat(inp2);
let internal = new Array(20).fill(0);
let outputs = [0, 0, 0, 0];
// NN: Please don't touch :), code is generated
outputs[0] = inputs[7] * 0.8058582544;
@@ -65,44 +74,63 @@ function dose(numreq, instore, store_last_tick, dole, carry, prev_req, numreq_to
outputs[3] = sigmoid0(outputs[3]);
debt = debt || 0;
if (outputs[0]>0.99) outputs[0]=1.0;
if (outputs[0] > 0.99) {
outputs[0]=1.0;
}
var res1=Number(outputs[0])*numreq+outputs[3]+debt;
var res_capped=Math.round(res1);
let res1 = Number(outputs[0]) * numreq+outputs[3] + debt;
let res_capped = Math.round(res1);
if (res_capped>numreq) res_capped=numreq;
if (res_capped>instore) res_capped=instore;
if (res_capped<0) res_capped=0;
if (res_capped > numreq) { res_capped = numreq; }
if (res_capped > instore) { res_capped = instore; }
if (res_capped < 0) { res_capped = 0; }
res1 = res1 || 0;
res_capped = res_capped || 0; // Safeguard against wrong values
debt = res1 - res_capped;
debt_cap = Math.round(numreq * 1.5) + 5;
if (debt>debt_cap) debt=debt_cap;
if (debt > debt_cap) {
debt = debt_cap;
}
carry = sigmoid0(outputs[2] * carry + outputs[0]) || 0;
return [res_capped, dole + outputs[1], carry, debt];
}
//TICK - Updates values for item X, should be called once for each item for each round of all slaves requesting that item
//Instore - number of items for item X in master gauge (currently)
// TICK -
// Updates values for item X, should be called once for each item for
// each round of all slaves requesting that item Instore - number of
// items for item X in master gauge (currently)
// dole - Current dole for item X
//store_last_tick - Number of items for item X in master gauge at time of last tick (not this one)
//* doleinfo_last - Object with 2 values for last "tick period": numreq - total number or requests for item, numslave - number of slaves requesting that item
//* doleinfo_new - Object with 2 values for new "tick period": numreq - total number or requests for item, numslave - number of slaves requesting that item
// store_last_tick -
// Number of items for item X in master gauge at time of last tick (not this one)
// * doleinfo_last -
// Object with 2 values for last "tick period": numreq - total number or
// requests for item, numslave - number of slaves requesting that item
//
// * doleinfo_new -
// Object with 2 values for new "tick period": numreq - total number or
// requests for item, numslave - number of slaves requesting that item
// Returns: Array of 2 elements
// 0.New dole for item X
// 1.Guessed percentage average of items supplied in last 10 ticks
function tick(instore, dole, store_last_tick, numreq_total_adj) {
var instore_adj=instore;
let instore_adj = instore;
if (instore_adj === 0) {
instore_adj = 0.1;
}
var inputs=[instore,dole,store_last_tick/instore_adj,store_last_tick/numreq_total_adj,store_last_tick,numreq_total_adj];
var internal=new Array(10).fill(0);
var outputs=[0,0];
let inputs = [
instore,
dole,
store_last_tick / instore_adj,
store_last_tick / numreq_total_adj,
store_last_tick,
numreq_total_adj,
];
let internal = new Array(10).fill(0);
let outputs = [0, 0];
// NN: Please don't touch :), code is generated
outputs[0] = inputs[3] * 0.9364774823;
@@ -112,9 +140,13 @@ function tick(instore, dole, store_last_tick, numreq_total_adj) {
outputs[1] = sigmoid3(outputs[1]);
avg = outputs[1];
if (numreq_total_adj<0.11) avg=1;//When no item's are requested, result is 0.1 ; In that case there is no deficit, so return 1 = 100% demand fulfilled
if (avg>1) avg=1;
if (avg<0) avg=0;
if (numreq_total_adj < 0.11) {
// When no item's are requested, result is 0.1; In that case there is
// no deficit, so return 1 = 100% demand fulfilled
avg = 1;
}
if (avg > 1) { avg=1; }
if (avg < 0) { avg=0; }
return [sigmoid0(outputs[0]), avg];
}