-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathitem.py
185 lines (125 loc) · 3.74 KB
/
item.py
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import commentjson
import linprog as lp
import utils
class Item:
def __init__ (self, pretty_name):
self . pretty_name = pretty_name
def production (self, producing_recipes):
return sum (
producing_recipe . output_rate (self)
for producing_recipe in producing_recipes
)
def consumption (self, consuming_recipes):
return sum (
consuming_recipe . input_rate (self)
for consuming_recipe in consuming_recipes
)
def add_constraints (
self,
constraints,
encoded_producing_recipes,
encoded_consuming_recipes,
input_rate,
output_rate
):
if input_rate == 'unlimited' and output_rate == 'unlimited':
# There are no meaningful constraints
pass
elif input_rate == 'unlimited':
constraints . append (
self . production (encoded_producing_recipes)
<= self . consumption (encoded_consuming_recipes) + output_rate
)
elif output_rate == 'unlimited':
constraints . append (
self . production (encoded_producing_recipes) + input_rate
>= self . consumption (encoded_consuming_recipes)
)
else:
constraints . append (
self . production (encoded_producing_recipes) + input_rate
== self . consumption (encoded_consuming_recipes) + output_rate
)
constraint = (
self . production (encoded_producing_recipes) + input_rate
== self . consumption (encoded_consuming_recipes) + output_rate
)
def interpret (
self,
model,
precision,
interpreted_producing_recipes,
interpreted_consuming_recipes,
input_rate,
output_rate
):
item_report = dict ()
amount_produced = self . production (interpreted_producing_recipes)
amount_consumed = self . consumption (interpreted_consuming_recipes)
if utils . interpret_approximate (amount_produced, precision) != 0:
item_report ['produced'] = utils . format_value (
amount_produced,
precision
)
item_report ['produced_by'] = dict (
[
(
recipe . pretty_name (),
utils . format_value (
recipe . output_rate (self),
precision
)
)
for recipe in interpreted_producing_recipes
if utils . interpret_approximate(recipe . output_rate (self), precision) != 0
]
)
if utils . interpret_approximate (amount_consumed, precision) != 0:
item_report ['consumed'] = utils . format_value (
amount_consumed,
precision
)
item_report ['consumed_by'] = dict (
[
(
recipe . pretty_name (),
utils . format_value (
recipe . input_rate (self),
precision
)
)
for recipe in interpreted_consuming_recipes
if utils . interpret_approximate(recipe . input_rate (self), precision) != 0
]
)
if len (item_report) != 0:
return item_report
else:
return None
def load_items (items_file_name):
with open (items_file_name, 'r') as items_file:
items_data = commentjson . load (items_file)
items = dict ()
for item_pretty_name in items_data:
items [item_pretty_name] = Item (item_pretty_name)
return items
def get_item (item_name, items):
if item_name not in items:
raise ValueError ('\'' + item_name + '\' does not name a valid item')
return items [item_name]
def load_item_quantities (item_quantities_data, items):
item_quantities = dict ()
for item_name, quantity in item_quantities_data . items ():
item = get_item (item_name, items)
item_quantity = (
'unlimited' if quantity == 'unlimited' else utils . real (quantity)
)
item_quantities [item] = item_quantity
return item_quantities
def load_finite_item_quantities (item_quantities_data, items):
item_quantities = dict ()
for item_name, quantity in item_quantities_data . items ():
item = get_item (item_name, items)
item_quantity = utils . real (quantity)
item_quantities [item] = item_quantity
return item_quantities