forked from Zistack/Satisfactory-Optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objective_function.py
206 lines (149 loc) · 4.34 KB
/
objective_function.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
import linprog as lp
import utils
from item import get_item, load_finite_item_quantities
from recipe_utils import get_recipe, get_recipe_set
def item_production (item, recipe_registry):
return item . production (
recipe_registry . item_producing_recipes [item]
)
def item_consumption (item, recipe_registry):
return item . consumption (
recipe_registry . item_consuming_recipes [item]
)
def item_output (item, recipe_registry):
return (
item . production (recipe_registry . item_producing_recipes [item])
- item . consumption (recipe_registry . item_consuming_recipes [item])
)
def item_input (item, recipe_registry):
return (
item . consumption (recipe_registry . item_consuming_recipes [item])
- item . production (recipe_registry . item_producing_recipes [item])
)
maximize = lambda v: - v
minimize = lambda v: v
class ItemObjectiveFunction:
def __init__ (self, item, item_expression, objective_type):
self . item = item
self . item_expression = item_expression
self . objective_type = objective_type
def add_objective (self, constraints, objectives, recipe_registry):
objectives . append (
self . objective_type (
self . item_expression (self . item, recipe_registry)
)
)
class ItemsObjectiveFunction:
def __init__ (self, item_ratios, item_expression, objective_type):
self . item_ratios = item_ratios
self . item_expression = item_expression
self . objective_type = objective_type
pretty_name = ' ' . join (
str (ratio) + ' ' + item . pretty_name
for item, ratio in self . item_ratios . items ()
)
name = utils . name (pretty_name)
self . combination_variable = lp . Variable (name)
def add_objective (self, constraints, objectives, recipe_registry):
constraints . append (self . combination_variable >= 0)
for item, ratio in self . item_ratios . items ():
constraints . append (
self . item_expression (item, recipe_registry)
== self . combination_variable * ratio
)
objectives . append (self . objective_type (self . combination_variable))
class RecipeObjectiveFunction:
def __init__ (self, raw_recipes, objective_type):
self . raw_recipes = raw_recipes
self . objective_type = objective_type
def add_objective (
self,
constraints,
objectives,
recipe_registry
):
encoded_recipes = recipe_registry . get_encoded_recipes (
self . raw_recipes
)
objectives . append (
self . objective_type (
sum (
encoded_recipe . output_magnitude ()
for encoded_recipe in encoded_recipes
)
)
)
def load_objective_function (
function_type,
function_data,
items,
searchable_recipes,
groups
):
if function_type == 'maximize_item_production':
return ItemObjectiveFunction (
get_item (function_data, items),
item_production,
maximize
)
if function_type == 'minimize_item_production':
return ItemObjectiveFunction (
get_item (function_data, items),
item_production,
minimize
)
if function_type == 'maximize_item_consumption':
return ItemObjectiveFunction (
get_item (function_data, items),
item_consumption,
maximize
)
if function_type == 'minimize_item_consumption':
return ItemObjectiveFunction (
get_item (function_data, items),
item_consumption,
minimize
)
if function_type == 'maximize_item_input':
return ItemObjectiveFunction (
get_item (function_data, items),
item_input,
maximize
)
if function_type == 'minimize_item_input':
return ItemObjectiveFunction (
get_item (function_data, items),
item_input,
minimize
)
if function_type == 'maximize_item_output':
return ItemObjectiveFunction (
get_item (function_data, items),
item_output,
maximize
)
if function_type == 'minimize_item_output':
return ItemObjectiveFunction (
get_item (function_data, items),
item_output,
minimize
)
if function_type == 'maximize_items_output':
return ItemsObjectiveFunction (
load_finite_item_quantities (function_data, items),
item_output,
maximize
)
if function_type == 'maximize_recipe':
return RecipeObjectiveFunction (
get_recipe_set (function_data, searchable_recipes, groups),
maximize
)
if function_type == 'minimize_recipe':
return RecipeObjectiveFunction (
get_recipe_set (function_data, searchable_recipes, groups),
minimize
)
raise ValueError (
'\'' + function_type + '\' does not name a valid production goal'
)