-
Notifications
You must be signed in to change notification settings - Fork 0
/
pl3.py
340 lines (253 loc) · 13.6 KB
/
pl3.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
# import numpy as np
# import pandas as pd
# import gurobipy as gp
# from gurobipy import GRB
# # Parameters
# years = [1, 2, 3, 4, 5]
# mines = [1, 2, 3, 4]
# royalties = {1: 5e6, 2: 4e6, 3: 4e6, 4: 5e6}
# capacity = {1: 2e6, 2: 2.5e6, 3: 1.3e6, 4: 3e6}
# quality = {1: 1.0, 2: 0.7, 3: 1.5, 4: 0.5}
# target = {1: 0.9, 2: 0.8, 3: 1.2, 4: 0.6, 5: 1.0}
# time_discount = {year: (1/(1+1/10.0)) ** (year-1) for year in years}
# max_mines = 3
# price = 10
# mining = gp.Model('Mining')
# blend = mining.addVars(years, name="Blend")
# extract = mining.addVars(years, mines, name="Extract")
# working = mining.addVars(years, mines, vtype=GRB.BINARY, name="Working")
# available = mining.addVars(years, mines, vtype=GRB.BINARY, name="Available")
# #1. Operating Mines
# OperatingMines = mining.addConstrs((working.sum(year, '*') <= max_mines for year in years), "Operating_mines")
# #2. Quality
# Quality = mining.addConstrs((gp.quicksum(quality[mine]*extract[year, mine] for mine in mines)
# == target[year]*blend[year] for year in years), "Quality")
# #3. Mass Conservation
# MassConservation = mining.addConstrs((extract.sum(year, '*') == blend[year] for year in years), "Mass Conservation")
# #4. Mine Capacity
# MineCapacity = mining.addConstrs((extract[year, mine] <= capacity[mine]*working[year, mine] for year, mine in extract), "Capacity")
# # Open to operate
# OpenToOperate = mining.addConstrs((working[year, mine] <= available[year, mine] for year, mine in available), "Open to Operate")
# # Shutdown Mine
# ShutdownMine = mining.addConstrs((available[year+1, mine] <= available[year, mine]
# for year, mine in available if year < years[-1]), "Shut down")
# #0. Objective function
# obj = gp.quicksum(price*time_discount[year]*blend[year] for year in years) \
# - gp.quicksum(royalties[mine] * time_discount[year] * available[year, mine] for year, mine in available)
# mining.setObjective(obj, GRB.MAXIMIZE)
# mining.optimize()
# rows = years.copy()
# columns = mines.copy()
# extraction = pd.DataFrame(columns=columns, index=rows, data=0.0)
# for year, mine in extract.keys():
# if (abs(extract[year, mine].x) > 1e-6):
# extraction.loc[year, mine] = np.round(extract[year, mine].x / 1e6, 2)
# extraction
# rows = years.copy()
# sales = pd.DataFrame(columns=['Sales'], index=rows, data=0.0)
# for year in blend.keys():
# if (abs(blend[year].x) > 1e-6):
# sales.loc[year, 'Sales'] = np.round(blend[year].x / 1e6, 2)
# sales
# mining.write("mining-output.sol")
import sys
from PyQt5.QtWidgets import QScrollArea, QApplication, QWidget, QLabel, QLineEdit, QPushButton, QVBoxLayout, QHBoxLayout , QTableView, QTableWidget, QTableWidgetItem, QHeaderView
import numpy as np
import pandas as pd
import gurobipy as gp
from gurobipy import GRB
class MiningOptimizer(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('Mining Optimization GUI')
self.setGeometry(100, 100, 400, 300)
# Create a scroll area
scroll_area = QScrollArea()
scroll_area.setWidgetResizable(True)
# Create a widget to hold the layout
scroll_content = QWidget()
scroll_area.setWidget(scroll_content)
# Create a layout for the content widget
self.input_layout = QVBoxLayout(scroll_content)
# Add input fields and buttons
self.add_input_fields()
self.add_buttons()
# Set the main layout of the window
main_layout = QVBoxLayout(self)
main_layout.addWidget(scroll_area)
def add_input_fields(self):
self.years_label = QLabel('Number of years:')
self.input_layout.addWidget(self.years_label)
self.years_input = QLineEdit()
self.years_input.setPlaceholderText('Number of years')
self.input_layout.addWidget(self.years_input)
self.mines_label = QLabel('Number of mines:')
self.input_layout.addWidget(self.mines_label)
self.mines_input = QLineEdit()
self.mines_input.setPlaceholderText('Number of mines')
self.input_layout.addWidget(self.mines_input)
self.discount_label = QLabel('Discount Rate:')
self.input_layout.addWidget(self.discount_label)
self.discount_input = QLineEdit()
self.discount_input.setPlaceholderText('Discount Rate in %')
self.input_layout.addWidget(self.discount_input)
self.royalty_capacity_quality_btn = QPushButton('Add Royalty, Capacity, Quality')
self.royalty_capacity_quality_btn.clicked.connect(self.add_royalty_capacity_quality_input)
self.input_layout.addWidget(self.royalty_capacity_quality_btn)
self.target_value_btn = QPushButton('Add Target Value for Year')
self.target_value_btn.clicked.connect(self.add_target_value_input)
self.input_layout.addWidget(self.target_value_btn)
self.max_mines_input = QLineEdit()
self.max_mines_input.setPlaceholderText('Max Mines')
self.input_layout.addWidget(self.max_mines_input)
self.price_input = QLineEdit()
self.price_input.setPlaceholderText('Price')
self.input_layout.addWidget(self.price_input)
def add_royalty_capacity_quality_input(self):
num_mines =0
if(self.mines_input.text()):
num_mines = int(self.mines_input.text()) or 0
for _ in range(num_mines):
mine_layout = QHBoxLayout()
royalty_input = QLineEdit()
royalty_input.setPlaceholderText('Royalty')
mine_layout.addWidget(royalty_input)
capacity_input = QLineEdit()
capacity_input.setPlaceholderText('Capacity')
mine_layout.addWidget(capacity_input)
quality_input = QLineEdit()
quality_input.setPlaceholderText('Quality')
mine_layout.addWidget(quality_input)
self.input_layout.insertLayout(self.input_layout.indexOf(self.royalty_capacity_quality_btn)+1, mine_layout)
def add_target_value_input(self):
num_years = 0
if(self.years_input.text()):
num_years = int(self.years_input.text())
for year in range(1, num_years + 1):
target_layout = QHBoxLayout()
target_label = QLabel(f'Target value for Year {year}:')
target_layout.addWidget(target_label)
target_input = QLineEdit()
target_input.setPlaceholderText('Target Value')
target_layout.addWidget(target_input)
self.input_layout.insertLayout(self.input_layout.indexOf(self.target_value_btn)+1, target_layout)
def add_buttons(self):
self.optimize_btn = QPushButton('Optimize')
self.optimize_btn.clicked.connect(self.run_optimization)
self.input_layout.addWidget(self.optimize_btn)
def run_optimization(self):
# Get user inputs
num_years = int(self.years_input.text())
num_mines = int(self.mines_input.text())
discount = float(self.discount_input.text())
max_mines = int(self.max_mines_input.text())
price = float(self.price_input.text())
royalties = []
capacities = []
qualities = []
targets = []
# Gather royalty, capacity, and quality inputs for each mine
for i in range(num_mines):
royalties.append(float(self.input_layout.itemAt(self.input_layout.indexOf(self.royalty_capacity_quality_btn)+1+i).layout().itemAt(0).widget().text()))
capacities.append(float(self.input_layout.itemAt(self.input_layout.indexOf(self.royalty_capacity_quality_btn)+1+i).layout().itemAt(1).widget().text()))
qualities.append(float(self.input_layout.itemAt(self.input_layout.indexOf(self.royalty_capacity_quality_btn)+1+i).layout().itemAt(2).widget().text()))
# Gather target values for each year
for i in range(num_years):
targets.append(float(self.input_layout.itemAt(self.input_layout.indexOf(self.target_value_btn)+1+i).layout().itemAt(1).widget().text()))
# Perform optimization
years = list(range(1, num_years + 1))
mines = list(range(1, num_mines + 1))
print(years)
print(mines)
print(royalties)
print(capacities)
print(qualities)
print(targets)
print(max_mines)
print(price)
royalties_dict = {mine: royalties[i] for i, mine in enumerate(mines)}
capacities_dict = {mine: capacities[i] for i, mine in enumerate(mines)}
qualities_dict = {mine: qualities[i] for i, mine in enumerate(mines)}
targets_dict = {year: targets[i] for i, year in enumerate(years)}
time_discount = {year: (1/(1+1/discount)) ** (year-1) for year in years}
mining = gp.Model('Mining')
blend = mining.addVars(years, name="Blend")
extract = mining.addVars(years, mines, name="Extract")
working = mining.addVars(years, mines, vtype=GRB.BINARY, name="Working")
available = mining.addVars(years, mines, vtype=GRB.BINARY, name="Available")
OperatingMines = mining.addConstrs((working.sum(year, '*') <= max_mines for year in years), "Operating_mines")
Quality = mining.addConstrs((gp.quicksum(qualities_dict[mine]*extract[year, mine] for mine in mines)
== targets_dict[year]*blend[year] for year in years), "Quality")
MassConservation = mining.addConstrs((extract.sum(year, '*') == blend[year] for year in years), "Mass Conservation")
MineCapacity = mining.addConstrs((extract[year, mine] <= capacities_dict[mine]*working[year, mine]
for year, mine in extract), "Capacity")
OpenToOperate = mining.addConstrs((working[year, mine] <= available[year, mine] for year, mine in available), "Open to Operate")
ShutdownMine = mining.addConstrs((available[year+1, mine] <= available[year, mine] for year, mine in available if year < years[-1]), "Shut down")
obj = gp.quicksum(price*time_discount[year]*blend[year] for year in years) \
- gp.quicksum(royalties_dict[mine] * time_discount[year] * available[year, mine] for year, mine in available)
mining.setObjective(obj, GRB.MAXIMIZE)
mining.optimize()
# After running mining.optimize()
if mining.status == GRB.OPTIMAL:
# Objective Value
print("Objective Value:", mining.objVal)
# Format objective value in dollars
objective_value_dollars = '${:,.2f}'.format(mining.objVal)
# Display objective value
objective_label = QLabel(f'Objective Value: {objective_value_dollars}')
self.input_layout.addWidget(objective_label)
# rows = years.copy()
# columns = mines.copy()
# extraction = pd.DataFrame(columns=columns, index=rows, data=0.0)
# for year, mine in extract.keys():
# if (abs(extract[year, mine].x) > 1e-6):
# extraction.loc[year, mine] = np.round(extract[year, mine].x / 1e6, 2)
# extraction
# Display extraction table
extraction_label = QLabel('Extraction Table')
self.input_layout.addWidget(extraction_label)
extraction = pd.DataFrame(columns=mines, index=years, data=0.0)
for year, mine in extract.keys():
if abs(extract[year, mine].x) > 1e-6:
extraction.loc[year, mine] = np.round(extract[year, mine].x / 1e6, 2)
extraction_table = QTableWidget()
extraction_table.setRowCount(len(years))
extraction_table.setColumnCount(len(mines))
for i, year in enumerate(years):
for j, mine in enumerate(mines):
item = QTableWidgetItem(str(extraction.loc[year, mine]))
extraction_table.setItem(i, j, item)
extraction_table.setHorizontalHeaderLabels([str(column) for column in extraction.columns])
extraction_table.setVerticalHeaderLabels([str(row) for row in extraction.index])
extraction_table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.input_layout.addWidget(extraction_table)
# rows = years.copy()
# sales = pd.DataFrame(columns=['Sales'], index=rows, data=0.0)
# for year in blend.keys():
# if (abs(blend[year].x) > 1e-6):
# sales.loc[year, 'Sales'] = np.round(blend[year].x / 1e6, 2)
# sales
sales_label = QLabel('Sales Table')
self.input_layout.addWidget(sales_label)
# Display sales table
sales = pd.DataFrame(columns=['Sales'], index=years, data=0.0)
for year in blend.keys():
if abs(blend[year].x) > 1e-6:
sales.loc[year, 'Sales'] = np.round(blend[year].x / 1e6, 2)
sales_table = QTableWidget()
sales_table.setRowCount(len(years))
sales_table.setColumnCount(1)
for i, year in enumerate(years):
item = QTableWidgetItem(str(sales.loc[year, 'Sales']))
sales_table.setItem(i, 0, item)
sales_table.setHorizontalHeaderLabels(['Sales'])
sales_table.setVerticalHeaderLabels([str(row) for row in sales.index])
sales_table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
self.input_layout.addWidget(sales_table)
# if __name__ == '__main__':
# app = QApplication(sys.argv)
# window = MiningOptimizer()
# window.show()
# sys.exit(app.exec_())