-
Notifications
You must be signed in to change notification settings - Fork 6
/
totals.js
118 lines (110 loc) · 4.13 KB
/
totals.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
/*Copyright 2021 Kirk McDonald
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.*/
import { zero } from "./rational.js"
function add(map, key, rate) {
let r = map.get(key)
if (r === undefined) {
r = rate
} else {
r = r.add(rate)
}
map.set(key, r)
}
function set(map, key1, key2, value) {
let submap = map.get(key1)
if (submap === undefined) {
submap = new Map()
map.set(key1, submap)
}
submap.set(key2, value)
}
export class Totals {
constructor(spec, products, rates, surplus) {
this.products = products
this.rates = rates
this.surplus = surplus
//this.topo = topo
// Construct the rest of the solution-graph. This graph consists of
// recipe nodes, which point to item nodes, which in turn point to
// recipe nodes. Each item node and edge contains an item-rate. Each
// recipe node contains a recipe-rate.
//
// The graph begins with resource recipes and ignored items, and ends
// with the "output" and (possibly) "surplus" pseudo-recipes.
// Maps item to total item-rate present in the solution.
this.items = new Map()
// Maps item to map of {recipe: item-rate} of recipes which produce
// that item.
this.producers = new Map()
// Maps item to map of {recipe: item-rate} of recipes which consume
// that item.
this.consumers = new Map()
for (let [recipe, rate] of rates) {
for (let ing of recipe.ingredients) {
let itemRate = rate.mul(ing.amount)
set(this.consumers, ing.item, recipe, itemRate)
add(this.items, ing.item, itemRate)
}
for (let ing of recipe.products) {
let itemRate = rate.mul(ing.amount)
set(this.producers, ing.item, recipe, itemRate)
}
}
// List of {item, from, to, rate} links, apportioned proportionately
// between multiple consumers and producers of each item.
this.proportionate = []
for (let [recipe, recipeRate] of rates) {
for (let ing of recipe.ingredients) {
let totalRate = this.items.get(ing.item)
let rate = recipeRate.mul(ing.amount)
let ratio = rate.div(totalRate)
for (let subRecipe of spec.getRecipes(ing.item)) {
if (!rates.has(subRecipe)) {
continue
}
let subRate = rates.get(subRecipe).mul(subRecipe.gives(ing.item)).mul(ratio)
this.proportionate.push({
"item": ing.item,
"from": subRecipe,
"to": recipe,
"rate": subRate,
})
}
}
}
}
/*add(recipe, rate) {
this.topo.push(recipe)
this.rates.set(recipe, (this.rates.get(recipe) || zero).add(rate))
}
updateHeight(recipe, height) {
let knownHeight = this.heights.get(recipe)
if (knownHeight === undefined || knownHeight < height) {
this.heights.set(recipe, height)
}
}
combine(other) {
let newTopo = []
for (let recipe of this.topo) {
if (!other.rates.has(recipe)) {
newTopo.push(recipe)
}
}
newTopo = newTopo.concat(other.topo)
for (let [recipe, rate] of other.rates) {
this.add(recipe, rate)
}
this.topo = newTopo
for (let [recipe, height] of other.heights) {
this.updateHeight(recipe, height + 1)
}
}*/
}