forked from Zistack/Satisfactory-Optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem.py
723 lines (517 loc) · 15.5 KB
/
problem.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
import commentjson
import sys
from collections import defaultdict
import linprog as lp
import utils
from node_type import get_node_type
from well_type import get_well_type
from well_configuration import load_well_configuration
from item import load_item_quantities
from well_recipe import WellRecipe
from recipe import RawRecipe
from recipe_utils import get_recipe, get_recipe_set
from recipe_registry import RecipeRegistry
from overclock_limits import load_overclock_limits
from objective_function import load_objective_function
class Problem:
def __init__ (
self,
node_types,
well_configurations,
searchable_recipes,
input_items,
output_items,
overclock_limits,
max_power_consumption,
available_somersloops,
power_augmentation_recipes,
max_machine_count,
objective_functions,
minimize_machine_count
):
self . node_types = node_types
self . well_configurations = well_configurations
self . searchable_recipes = searchable_recipes
self . input_items = input_items
self . output_items = output_items
self . overclock_limits = overclock_limits
self . available_somersloops = available_somersloops
self . power_augmentation_recipes = power_augmentation_recipes
self . max_power_consumption = max_power_consumption
self . max_machine_count = max_machine_count
self . objective_functions = objective_functions
self . minimize_machine_count = minimize_machine_count
def __model_productivity (self):
return self . available_somersloops != 0
def __encode_recipe (self, constraints, recipe, recipe_registry):
if type (recipe) == RawRecipe:
encoded_recipe = recipe . encode (
constraints,
self . overclock_limits [recipe],
self . __model_productivity ()
)
recipe_registry . register_recipe (recipe, encoded_recipe)
return [encoded_recipe]
if type (recipe) == WellRecipe:
encoded_recipes = list ()
for well_configuration \
in self . well_configurations [recipe . well_type] . keys ():
raw_recipe = recipe . specialize (well_configuration)
encoded_recipe = raw_recipe . encode (
constraints,
self . overclock_limits [recipe],
self . __model_productivity ()
)
recipe_registry . register_recipe (raw_recipe, encoded_recipe)
recipe_registry . register_well_recipe (recipe, encoded_recipe)
encoded_recipes . append (encoded_recipe)
return encoded_recipes
def __encode_recipes (self, constraints):
print ('Encoding recipes', file = sys . stderr)
recipe_registry = RecipeRegistry ()
total_power_augmentation_factor = 0
for recipe in self . searchable_recipes:
self . __encode_recipe (constraints, recipe, recipe_registry)
for power_augmentation_recipe, machine_count \
in self . power_augmentation_recipes . items ():
encoded_recipes = self . __encode_recipe (
constraints,
power_augmentation_recipe,
recipe_registry
)
constraints . append (
sum (
encoded_recipe . machine_count ()
for encoded_recipe in encoded_recipes
)
== machine_count
)
total_power_augmentation_factor += (
machine_count
* power_augmentation_recipe . power_augmentation_factor
)
return (
recipe_registry,
total_power_augmentation_factor
)
def __encode_somersloops (self, constraints, recipe_registry):
total_allocated_somersloops = sum (
recipe . allocated_somersloops ()
for recipe in recipe_registry . somersloop_allocating_recipes
)
constraints . append (total_allocated_somersloops >= 0)
constraints . append (
total_allocated_somersloops <= self . available_somersloops
)
def __encode_node_types (self, constraints, recipe_registry):
print ('Encoding nodes', file = sys . stderr)
for node_type, consuming_recipes \
in recipe_registry . node_type_consuming_recipes . items ():
if node_type in self . node_types:
count = self . node_types [node_type]
else:
count = 0
constraints . append (
sum (recipe . machine_count () for recipe in consuming_recipes)
<= count
)
def __encode_well_configurations (self, constraints, recipe_registry):
print ('Encoding wells', file = sys . stderr)
for well_type, well_configurations in (
self . well_configurations . items ()
):
for well_configuration, count in well_configurations . items ():
well_configuration . add_constraints (
constraints,
count,
recipe_registry . well_configuration_consuming_recipes [
well_configuration
]
)
def __input_rate (self, item):
if item in self . input_items:
return self . input_items [item]
else:
return 0.0
def __output_rate (self, item):
if item in self . output_items:
return self . output_items [item]
else:
return 0.0
def __encode_items (self, constraints, recipe_registry):
print ('Encoding items', file = sys . stderr)
used_items = set ()
used_items |= self . input_items . keys ()
used_items |= self . output_items . keys ()
used_items |= recipe_registry . item_producing_recipes . keys ()
used_items |= recipe_registry . item_consuming_recipes . keys ()
for item in used_items:
item . add_constraints (
constraints,
recipe_registry . item_producing_recipes [item],
recipe_registry . item_consuming_recipes [item],
self . __input_rate (item),
self . __output_rate (item)
)
return used_items
# This whole step is arguably a complete no-op now.
def __encode_machines (self, constraints, recipe_registry):
print ('Encoding machines', file = sys . stderr)
for machine, using_recipes \
in recipe_registry . machine_using_recipes . items ():
machine . add_constraints (
constraints,
recipe_registry . machine_using_recipes [machine]
)
def __net_power_consumption (
self,
recipe_registry,
total_power_augmentation_factor
):
total_power_production = sum (
- recipe . power_consumption ()
for recipe in recipe_registry . power_producing_recipes
)
augmented_power_production = (
total_power_production
* (1 + total_power_augmentation_factor)
)
total_power_consumption = sum (
recipe . power_consumption ()
for recipe in recipe_registry . power_consuming_recipes
)
return total_power_consumption - augmented_power_production
def __encode_power_consumption (
self,
constraints,
recipe_registry,
total_power_augmentation_factor
):
print ('Encoding total power consumption', file = sys . stderr)
if self . max_power_consumption == None:
return
constraints . append (
self . __net_power_consumption (
recipe_registry,
total_power_augmentation_factor
)
<= self . max_power_consumption
)
def __total_machine_count (self, recipe_registry):
return sum (
machine . total_count (using_recipes)
for machine, using_recipes
in recipe_registry . machine_using_recipes . items ()
)
def __encode_machine_count (self, constraints, recipe_registry):
print ('Encoding total machine count', file = sys . stderr)
if self . max_machine_count == None:
return
constraints . append (
self . __total_machine_count (recipe_registry)
<= self . max_machine_count
)
def __encode_objective_functions (
self,
constraints,
objectives,
recipe_registry,
total_power_augmentation_factor
):
print ('Encoding objective functions', file = sys . stderr)
for objective_function in self . objective_functions:
objective_function . add_objective (
constraints,
objectives,
recipe_registry
)
if (self . minimize_machine_count):
objectives . append (self . __total_machine_count (recipe_registry))
else:
objectives . append (
self . __net_power_consumption (
recipe_registry,
total_power_augmentation_factor
)
)
def __interpret_model (
self,
model,
precision,
recipe_registry,
total_power_augmentation_factor,
used_items
):
interpreted_recipe_registry = recipe_registry . interpret (model)
def has_report (tuple):
name, report = tuple
return report != None
searchable_recipe_interpretations = dict (
filter (
has_report,
[
(
interpreted_searchable_recipe . pretty_name (),
interpreted_searchable_recipe . get_report (precision)
)
for interpreted_searchable_recipe
in interpreted_recipe_registry . get_interpreted_recipes (
self . searchable_recipes
)
]
)
)
power_augmentation_recipe_interpretations = dict (
filter (
has_report,
[
(
interpreted_power_augmentation_recipe . pretty_name (),
interpreted_power_augmentation_recipe
. get_report (precision)
)
for interpreted_power_augmentation_recipe
in interpreted_recipe_registry . get_interpreted_recipes (
self . power_augmentation_recipes . keys ()
)
]
)
)
item_interpretations = dict (
filter (
has_report,
[
(
item . pretty_name,
item . interpret (
model,
precision,
interpreted_recipe_registry
. item_producing_recipes [item],
interpreted_recipe_registry
. item_consuming_recipes [item],
self . __input_rate (item),
self . __output_rate (item)
)
)
for item in used_items
]
)
)
machine_interpretations = dict (
filter (
has_report,
[
(
machine . pretty_name,
machine . interpret (model, precision, using_recipes)
)
for machine, using_recipes
in interpreted_recipe_registry
. machine_using_recipes
. items ()
]
)
)
total_power_consumption_value = utils . format_value (
self . __net_power_consumption (
interpreted_recipe_registry,
total_power_augmentation_factor
),
precision
)
total_machine_count_value = utils . format_value (
self . __total_machine_count (interpreted_recipe_registry),
precision
)
return {
'items': item_interpretations,
'machines': machine_interpretations,
'recipes': searchable_recipe_interpretations,
'power_augmentation_recipes':
power_augmentation_recipe_interpretations,
'total_power_consumption': total_power_consumption_value,
'total_machine_count': total_machine_count_value
}
def solve (self, precision):
print ('Encoding problem', file = sys . stderr)
constraints = list ()
objectives = list ()
(
recipe_registry,
total_power_augmentation_factor
) = self . __encode_recipes (constraints)
self . __encode_somersloops (constraints, recipe_registry)
self . __encode_node_types (constraints, recipe_registry)
self . __encode_well_configurations (constraints, recipe_registry)
used_items = self . __encode_items (constraints, recipe_registry)
self . __encode_machines (constraints, recipe_registry)
self . __encode_power_consumption (
constraints,
recipe_registry,
total_power_augmentation_factor
)
self . __encode_machine_count (constraints, recipe_registry)
self . __encode_objective_functions (
constraints,
objectives,
recipe_registry,
total_power_augmentation_factor
)
print ('Solving problem', file = sys . stderr)
model = lp . solve (constraints, objectives)
if model == None:
return None
print ('Interpreting model', file = sys . stderr)
return self . __interpret_model (
model,
precision,
recipe_registry,
total_power_augmentation_factor,
used_items
)
def load_problem (
problem_file_name,
node_types,
well_types,
items,
searchable_recipes,
power_augmentation_recipes,
groups
):
with open (problem_file_name, 'r') as problem_file:
problem_data = commentjson . load (problem_file)
# Nodes
problem_node_types = dict ()
if 'nodes' in problem_data:
for node_type_name, count in problem_data ['nodes'] . items ():
node_type = get_node_type (node_type_name, node_types)
problem_node_types [node_type] = int (count)
# Wells
problem_well_configurations = defaultdict (dict)
if 'wells' in problem_data:
for well_configuration_data in problem_data ['wells']:
well_type = get_well_type (
well_configuration_data ['type'],
well_types
)
well_configuration = load_well_configuration (
well_type,
well_configuration_data
)
if 'count' in well_configuration_data:
count = int (well_data ['count'])
else:
count = 1
problem_well_configurations [well_type] [well_configuration] = count
# Recipes
included_recipes = problem_data ['included_recipes']
problem_searchable_recipes = set ()
for included_recipe in included_recipes:
problem_searchable_recipes |= get_recipe_set (
included_recipe,
searchable_recipes,
groups
)
if 'excluded_recipes' in problem_data:
excluded_recipes = problem_data ['excluded_recipes']
for excluded_recipe in excluded_recipes:
problem_searchable_recipes -= get_recipe_set (
excluded_recipe,
searchable_recipes,
groups
)
# Input items
if 'input_items' in problem_data:
problem_input_items = load_item_quantities (
problem_data ['input_items'],
items
)
else:
problem_input_items = dict ()
# Output items
if 'output_items' in problem_data:
problem_output_items = load_item_quantities (
problem_data ['output_items'],
items
)
else:
problem_output_items = dict ()
# Overclocking
problem_overclock_limits = defaultdict (lambda: None)
if 'overclocking' in problem_data:
for recipe_name, overclock_limits_data in (
problem_data ['overclocking'] . items ()
):
overclock_limits = load_overclock_limits (overclock_limits_data)
for recipe in get_recipe_set (recipe_name, searchable_recipes, groups):
if not recipe . supports_overclocking ():
raise ValueError (
'\'' + recipe_name + '\' does not support overclocking'
)
problem_overclock_limits [recipe] = overclock_limits
# Power Consumption
if 'max_power_consumption' in problem_data:
problem_max_power_consumption = utils . real (
problem_data ['max_power_consumption']
)
else:
problem_max_power_consumption = None
# Available Somersloops
if 'available_somersloops' in problem_data:
problem_available_somersloops = utils . real (
problem_data ['available_somersloops']
)
else:
problem_available_somersloops = 0
# Power Augmentation
problem_power_augmentation_recipes = dict ()
if 'power_augmentation' in problem_data:
for recipe_name, machine_count \
in problem_data ['power_augmentation'] . items ():
recipe = get_recipe (recipe_name, power_augmentation_recipes)
machine_count = utils . real (machine_count)
problem_power_augmentation_recipes [recipe] = machine_count
# Machine Count
if 'max_machine_count' in problem_data:
problem_max_machine_count = utils . real (
problem_data ['max_machine_count']
)
else:
problem_max_machine_count = None
# Objective Functions
if 'optimization_goals' in problem_data:
objective_functions_data = problem_data ['optimization_goals']
problem_objective_functions = list (
load_objective_function (
function_type,
function_data,
items,
searchable_recipes,
groups
)
for function_type, function_data in objective_functions_data
)
else:
problem_objective_functions = list ()
# Passive Minimization
if 'minimize_machine_count' in problem_data:
problem_minimize_machine_count = bool (
problem_data ['minimize_machine_count']
)
else:
problem_minimize_machine_count = False
return Problem (
problem_node_types,
problem_well_configurations,
problem_searchable_recipes,
problem_input_items,
problem_output_items,
problem_overclock_limits,
problem_max_power_consumption,
problem_available_somersloops,
problem_power_augmentation_recipes,
problem_max_machine_count,
problem_objective_functions,
problem_minimize_machine_count
)