-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProjectPlanning.py
576 lines (495 loc) · 23.4 KB
/
ProjectPlanning.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
__author__ = 'Andrea Chiappo'
__email__ = '[email protected]'
import numpy as np
import matplotlib.dates as mdates
from datetime import datetime
from scipy.integrate import quad
from more_itertools import random_combination
from scipy.interpolate import InterpolatedUnivariateSpline
###############################################################################
def dataframe_to_dictionary(projects_df, scale=1):
"""
function to cast a pandas dataframe to a dictionary
input
- projects pandas DataFrame
- scale factor to rescale data (optional)
output
- dictionary of dictionaries
(keys of father dictionary are the projects' name
while values contains a dictionary with the projects' data)
"""
# initialise empty dictionary
projects = {}
# get years list
ind = [ln for ln in projects_df.index.values[2:]]
Dyears = [datetime(int(idx.split(' ')[-1]),1,1).date() for idx in ind]
# convert years list to number representation - for interpolation purposes
Nyears = np.array([mdates.date2num(yy) for yy in Dyears])
# extract each project's data
for p in projects_df.columns.values:
# get project data
project = projects_df[p].to_numpy(dtype=float)
# initialise individual project dictionary
project_dict = {}
# get earliest spud year
project_dict['spud'] = int(project[0])
# get drilling duration
project_dict['drill'] = int(project[1])
# get production profile
prod_profile = project[2:]
# retain only non-zero years
non_zero_years = np.nonzero(prod_profile)
prod_profile = prod_profile[non_zero_years]
# rescale production profile
prod_profile *= scale
project_dict['profile'] = prod_profile
# get effective production years
years = Nyears[non_zero_years]
project_dict['years'] = years
# interpolate yearly production profile
curve = InterpolatedUnivariateSpline(years, prod_profile)
# save interpolation object
project_dict['curve'] = curve
# append to father dictionary
projects[p] = project_dict
return projects
###############################################################################
class Projects(object):
"""
Projects base class
Contains methods to calculate useful metrics of the considered projects:
- maximum production profiles' value
- projects' effective production (over the considered time range)
- projects' total production (integrated over the considered time range)
- projects' peak production absolute ordering and relative to global mean
"""
def __init__(self, period, projects, **kwargs):
self.period = period
self.projects = projects
self.projects_names = np.array(list(projects.keys()))
# indices selecting the projects' operation period
# (to calculate effective and total productions)
self.min_year = kwargs['min_year'] if 'min_year' in kwargs else 0
self.max_year = kwargs['max_year'] if 'max_year' in kwargs else -1
# attributes for possible alternative project planning algorithms
self.first_project = kwargs['first_project'] if 'first_project' in kwargs else 'mean'
self.next_project = kwargs['next_project'] if 'next_project' in kwargs else 'mean'
self.initial_iters = kwargs['initial_iters'] if 'initial_iters' in kwargs else 1000
# call local methods to estimate metrics
self.projects_max_prod()
self.projects_total_prod()
self.projects_effective_prod()
self.global_mean = self.total_prod / period
self.projects_prod_diff()
def projects_max_prod(self):
"""
Function to calculate peak production of all projects
(achieved on the first operation day)
"""
max_prod = []
for project in self.projects.values():
max_prod_profile = project['profile']
project['max'] = max_prod_profile[0]
max_prod.append(max_prod_profile[0])
self.max_project_prod = np.array(max_prod)
def projects_effective_prod(self):
"""
Function to calculate effective production of all projects
(calculating interpolation object over operation period)
"""
for project in self.projects.values():
years = project['years']
prof_curve = project['curve']
iyear = years[self.min_year]
fyear = years[self.max_year]
yrange = np.arange(iyear,fyear)
effective_prod = prof_curve(yrange)
project['effective'] = effective_prod
def projects_total_prod(self):
"""
Function to calculate each project's total production
(integrating interpolation object over operation period)
and the cumulative portfolio total production
"""
T = 0
for project in self.projects.values():
years = project['years']
iyear = years[self.min_year]
fyear = years[self.max_year]
prof_curve = project['curve']
proj_tot_prod = quad(prof_curve, iyear, fyear)[0]
project['total'] = proj_tot_prod
T += proj_tot_prod
self.total_prod = T
def projects_prod_diff(self):
"""
Function to calculate the projects ordering
- relative to the global production averaged over the whole period
- absolute ordering
"""
# get projects ordering relative the global mean
prod_diffs_rel = np.abs(self.max_project_prod - self.global_mean)
order_indxs_rel = np.argsort(prod_diffs_rel)
order_projects_names_rel = self.projects_names[order_indxs_rel]
self.order_projects_names_rel = order_projects_names_rel
# get projects absolute ordering for max production
prod_diffs_abs = self.max_project_prod
order_indxs_abs = np.argsort(prod_diffs_abs)
order_projects_names_abs = self.projects_names[order_indxs_abs]
self.order_projects_names_abs = order_projects_names_abs
class Projects_Planner(Projects):
"""
Project Planner class
This class collects the methods to operate with the portfolio projects
Specifically, the moethods contained can be used to
- search for a candidate first or collection of projects
- search a successive project to execute
- find the projects' optimal ordering
- add projects to the execution list
- add projects to the execution sequence
- remove projects from the execution lists
- clear projects execution lists
- update projects execution lists
- calculate daily production (using active projects)
"""
def __init__(self, first_day, period, projects, **kwargs):
super(Projects_Planner, self).__init__(period, projects, **kwargs)
# ascertain that projects is a dictionary
assert isinstance(projects, dict), "projects must be a dictionary!"
# define operational attributes
self.call = 0
self.projects_length = len(projects)
self.used_projects = np.array([])
self.projects_time_sequence = []
self.active_projects = np.array([])
self.projects_first_day = np.array([])
# time reference attributes
self.first_day = first_day
first_year = mdates.num2date(self.first_day).date().year
self.first_year = first_year
def search_first_project(self):
"""
Function to search for the first project to execute and
add it to the execution list
Three possible scenarios avaialable, controlled by the
argument 'first_project':
- 'mean' = search for the combinations of projects whose sum of
maximum (initial) values is closest to the global mean
(default option)
- 'minmax' = search for the project which yields the highest
maximum (initial) value
- None = simpy return a project randomly chosen
All three cases allow for iteration over the list of proposed
projects in search for the candidate complying with the requirement
that the project's spud year must be smaller than the initial year
"""
if self.first_project=='mean':
# get portfolio project's indices list
initial_indx = range(self.projects_length)
# calculate N=initial_iters possible combinations of possible
# initial projects
combs_sum = []
combs_ind = []
for it in range(self.initial_iters):
random_length = np.random.choice(self.projects_length)
indxs = random_combination(initial_indx,random_length)
combs_ind.append(indxs)
combs_sum.append(self.max_project_prod[list(indxs)].sum())
# search for projects' combination summing closest to global mean
combs_mean_diff = np.abs(np.array(combs_sum) - self.global_mean)
sort_indxs = combs_mean_diff.argsort()
# iteratively search for compliance with spud year requirement
n = 0
found = False
while not found:
projects_indxs = combs_ind[sort_indxs[n]]
projects = [self.projects_names[ind] for ind in projects_indxs]
spud_years = [self.projects[p]['spud'] for p in projects]
if all(np.array(spud_years) < self.first_year):
found = True
n += 1
elif self.first_project=='minmax':
# sort proejcts' production maxima
max_project_prod_sort = self.max_project_prod.argsort()
# iteratively search for compliance with spud year requirement
n = -1
found = False
while not found:
ind = max_project_prod_sort[n]
projects = self.order_projects_names_abs[ind]
if self.projects[projects]['spud'] <= self.first_year:
found = True
n -= 1
else:
# iteratively search for compliance with spud year requirement
found = False
while not found:
n = np.random.choice(self.projects_length)
projects = self.projects_names[n]
if self.projects[projects]['spud'] < self.first_year:
found = True
first_day = 0
self.add_projects(projects, first_day)
def search_next_project(self, day, last_prod):
"""
Function to search for successive project to execute.
Analogously to search_first_project(), three options are available
- 'mean' = search for project whose initial top production is
closest to the last daily portfolio production
- 'minmax' = alternate between the project having the smallest
and highest initial porduction
- None = randomdly select a project from the available ones
All three cases allow for iteration over the list of proposed
projects in search for the candidate complying with the requirement
that the project's spud year must be smaller or equal to the initial
year. The present iteration allows for the possibility that
a project's spud year is the current one, but checks that the
drilling occured within the present year.
"""
current_day = self.first_day+day
current_year = mdates.num2date(current_day).date().year
# determine available (unused) projects
left_projects_ind = ~np.in1d(self.projects_names, self.used_projects)
left_projects_name = self.projects_names[left_projects_ind]
if self.next_project=='mean':
# determine difference of last production point from global mean
last_mean_diff = last_prod - self.global_mean
last_prod_diffs = self.max_project_prod + last_mean_diff
# order remaining projects using the difference between
# their max production, the last production and the mean
new_project_prod = last_prod_diffs[left_projects_ind]
new_project_prod_ind = new_project_prod.argsort()[::-1]
new_project_name_ord = left_projects_name[new_project_prod_ind]
# iteratively search for compliance with spud year requirement
n = 0
found = False
while not found:
project = new_project_name_ord[n]
spud_year = self.projects[project]['spud']
drill = self.projects[project]['drill']
spud_date = mdates.num2date(current_day-drill).date().year
if spud_year <= current_year <= spud_date:
found = True
n += 1
self.add_projects(project, day)
elif self.next_project=='minmax':
self.call += 1
# determine peak production of left projects
new_project_prod = self.max_project_prod[left_projects_ind]
new_project_prod_ind = new_project_prod.argsort()
new_project_name_ord = left_projects_name[new_project_prod_ind]
# return a maximum in the peak production list
if self.call % 2 == 0:
# iteratively search for compliance with spud year requirement
n = -1
found = False
while not found:
project = new_project_name_ord[n]
drill = self.projects[project]['drill']
spud_date = mdates.num2date(current_day-drill).date().year
if spud_year <= current_year <= spud_date:
found = True
n -= 1
# return a minimum in the peak production list
else:
# iteratively search for compliance with spud year requirement
n = 0
found = False
while not found:
project = new_project_name_ord[n]
drill = self.projects[project]['drill']
spud_date = mdates.num2date(current_day-drill).date().year
if spud_year <= current_year <= spud_date:
found = True
n += 1
self.add_projects(project, day)
else:
# iteratively search for compliance with spud year requirement
found = False
left_porjects_num = range(len(left_projects_name))
while not found:
n = np.random.choice(left_porjects_num)
project = left_projects_name[n]
if self.projects[project]['spud'] <= year:
found = True
self.add_projects(project, day)
def projects_ordering(self):
"""
Function to search for the projects' order leading to the maximum
production during the operation phase.
Returns the sequence of projects and their execution day
Scheme:
- split portfolio projects based on earlier spud year
- consider projects with spud year earlier than period start
- order resulting projects in decreasing total production
- order resulting projects in decreasing drill duration
- starting from longest drilling project(s), populate the
projects sequence including the new projects and subtracting
the drilling time from the period start date
- consider projects with spud year later or equal than period start
- order resulting projects in decreasing total production
- order resulting projects in decreasing drill duration
- starting from longest drilling project(s), populate the
projects sequence inclduing the new projects and adding
the drilling time to the period start date
- combine to the sequences
"""
# get initial lists of indices, spud years, drilling time and totals
indxs = np.arange(self.projects_length)
spuds = np.array([pr['spud'] for pr in self.projects.values()])
drill = np.array([pr['drill'] for pr in self.projects.values()])
totals = np.array([pr['total'] for pr in self.projects.values()])
# consider only projects with spud year earlier than period start
before = np.where(spuds<self.first_year)[0]
indxs_before = indxs[before]
drill_before = drill[before]
totals_before = totals[before]
# order projects in decreasing total production
order = totals_before.argsort()[::-1]
indxs_order = indxs_before[order]
drill_order = drill_before[order]
# order projects in decreasing drill duration
unique_drills = np.unique(drill_order)
days_before = max(unique_drills)
ind_sequence_before = []
for dr in np.sort(unique_drills)[::-1]:
ind_drill = np.where(drill_order==dr)[0]
ind_sequence_before.append(indxs_order[ind_drill])
proj_sequence_before = []
# build sequence of projects than can execute before period start
n = 0
for day in range(days_before):
for ind in ind_sequence_before:
if (days_before-day) == drill[ind[0]]:
for dd,ii in enumerate(ind):
exec_day = days_before-day-dd
proj_sequence_before.append((-exec_day, ii))
#----------------------------------------------------------------------
# consider only projects with spud year later/equal than period start
after = np.where(spuds>=self.first_year)[0]
indxs_after = indxs[after]
drill_after = drill[after]
totals_after = totals[after]
# order projects in decreasing total production
new_order = totals_after.argsort()[::-1]
indxs_new_order = indxs_after[new_order]
drill_new_order = drill_after[new_order]
# order projects in decreasing drill duration
unique_drills = np.unique(drill_new_order)
days_after = len(drill_new_order)
ind_sequence_after = []
for dr in np.sort(unique_drills)[::-1]:
ind_drill = np.where(drill_new_order==dr)[0]
ind_sequence_after.append(indxs_new_order[ind_drill])
ind_sequence_after = np.hstack(ind_sequence_after)
proj_sequence_after = []
# build sequence of projects than can execute after period start
n = 0
for day in range(days_after):
exec_day = day
ii = ind_sequence_after[n]
proj_sequence_after.append((exec_day, ii))
n += 1
# combine two sequences to obtain complete projects' sequence
self.projects_sequence = [*proj_sequence_before,*proj_sequence_after]
def add_projects(self, projects, first_day):
"""
Function to add projects to the lists of executable projects on
a given day.
input:
- project(s) name (can be a list or a string)
- first operations day of the project(s) added
Three operational lists:
- used_projects = collects all projects used over entire period
- active_projects = collects only projects currently active
- projects_first_day = collects the projects' operation starting day
"""
if not isinstance(projects, list):
projects = [projects]
for project in projects:
assert project not in self.used_projects, "Project already used!"
self.add_project_sequence(project, first_day)
self.used_projects = np.append(self.used_projects, project)
self.active_projects = np.append(self.active_projects, project)
self.projects_first_day = np.append(self.projects_first_day, first_day)
def add_project_sequence(self, project, first_day):
"""
Function to populate the list of executed projects
including the execution day and the project name
input:
- project name
- first operations day
"""
drill = self.projects[project]['drill']
exec_date_num = self.first_day + first_day - drill
exec_date = mdates.num2date(exec_date_num).date()
self.projects_time_sequence.append((exec_date, project))
def clear_projects(self):
"""
Function to clear the content of the executable projects lists
"""
self.used_projects = np.array([])
self.active_projects = np.array([])
self.projects_first_day = np.array([])
def remove_projects(self, indxs):
"""
Function to delete a project from the list of active projects
and the list of first project operations day
input:
- project index
"""
self.projects_first_day = np.delete(self.projects_first_day, indxs)
self.active_projects = np.delete(self.active_projects, indxs)
def update_simulatenous_projects(self, day):
"""
Function to update the operational lists of active projects
input:
- operations day
"""
removed_projects_indxs = []
for p,project in enumerate(self.active_projects):
first_day = self.projects_first_day[p]
drill_duration = self.projects[project]['drill']
effective_prod = self.projects[project]['effective']
if day-first_day == len(effective_prod):
removed_projects_indxs.append(p)
self.remove_projects(removed_projects_indxs)
def project_production(self, day):
"""
Function to calculate the cumulative production of the
projects active on a given day
input:
- operations day
output:
- cumulative (active) project(s) production
"""
# update the list of currently active projects
self.update_simulatenous_projects(day)
temp = 0
for p,project in enumerate(self.active_projects):
first_day = int(self.projects_first_day[p])
temp += self.projects[project]['effective'][day-first_day]
return temp
def __call__(self):
"""
Function to calculate the cumulative production profile
of all utilisable portfolio projects over the defined period
"""
self.projects_ordering()
production = []
execution = np.arange(self.period)
n = 0
for start,ind in self.projects_sequence:
if start<0:
self.add_projects(self.projects_names[ind], 0)
n += 1
P = self.project_production(0)
production.append(P)
for day in execution[1:]:
if n < self.projects_length:
start,ind = self.projects_sequence[n]
self.add_projects(self.projects_names[ind], day)
n += 1
P = self.project_production(day)
production.append(P)
return production