-
Notifications
You must be signed in to change notification settings - Fork 0
/
function_file.py
587 lines (447 loc) · 25 KB
/
function_file.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
from __future__ import division # converts to float before division , useful and recommended for pyomo
import os
import random
from pathlib import Path
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
from netCDF4 import Dataset
import csv
from collections import OrderedDict
# optimization packages
from scipy.optimize import linprog
import cvxpy as cp
from gurobipy import *
from pyomo.environ import *
# statistical sampling packages
from scipy.stats import norm
from scipy.stats import truncnorm
from scipy.stats import uniform
'''I/O Dataframes'''
def df_buyers(b_v, b_q):
return pd.DataFrame({'price': sorted(b_v, reverse=1), 'quantity': b_q})
def df_sellers(s_v, s_q):
return pd.DataFrame({'price': sorted(s_v), 'quantity': s_q})
def df_MCP():
'''create dataframe for MCP's'''
# columns: may have to accomodate for multiple MCP's, e.g. two price system
return pd.DataFrame(columns=['MCP default',
'MCP VCG buyers', 'MCP VCG sellers',
'MCP Huang buyers', 'MCP Huang sellers'])
def df_allo_eff():
'''create dataframe for model MCP'''
return pd.DataFrame(columns=['allocative efficiency'])
def df_utility_profits():
# need to update
return pd.DataFrame(columns=['Average Utility', 'Average Profit',
'VCG Utility', 'VCG Profit',
'Huang Utility', 'Huang Profit'])
def df_quantity_traded():
return pd.DataFrame(columns=['Average quantity',
'VCG quantity',
'Huang quantity'])
def df_market_size():
return pd.DataFrame(columns=['Aggregate Demand',
'Aggregate Supply'])
def view_df_full(df):
with pd.option_context('display.max_rows', None, 'display.max_columns', None): # more options can be specified also
print(df)
'''Auction Preparation Dataframes'''
#probability distribution functions
def visualization_samples(sample):
'''takes in random sampling created by distribution function, visualize samples by value and sorted values'''
fig, ax = plt.subplots()
ax.plot(range(len(sample)), sample, label='scatter')
sample.sort()
ax.plot(sample, label='sorted')
ax.legend()
return ax
def trunc_visualization(parameters):
[mu, sig, min, max] = parameters
a, b, = (min - mu) / sig, (max - mu) / sig
x_range = np.linspace(0, 1, 1000)
fig, ax = plt.subplots()
sns.lineplot(x_range, truncnorm.pdf(x_range, a, b, loc=mu, scale=sig), label='pdf')
sns.lineplot(x_range, truncnorm.cdf(x_range, a, b, loc=mu, scale=sig), label='cdf')
ax.legend()
return ax
def create_samples_uniform(n, parameters):
"""takes in parameters as a 1x2 vector of lower and uppper bound, generates n number of samples."""
lower_bound = parameters[0]
upper_bound= parameters[1] - parameters[0]
vector = uniform.rvs(size = n, loc = lower_bound, scale = upper_bound)
return vector
def create_samples_truncnorm(n, parameters):
'''takes in number of buyers, and parameters to create list of valuations based on truncated normal distribution'''
[mu_v_b, sig_v_b, min_v, max_v] = parameters
a_para, b_para = (min_v - mu_v_b) / sig_v_b, (max_v - mu_v_b) / sig_v_b
sample_array = truncnorm.rvs(a_para, b_para, size=n, loc=mu_v_b, scale=sig_v_b)
return sample_array
def create_buyers_v_truncnorm(b, parameters):
'''takes in number of buyers, and parameters to create list of valuations based on truncated normal distribution'''
[mu_v_b, sig_v_b, min_v, max_v] = parameters
a_para, b_para = (min_v - mu_v_b) / sig_v_b, (max_v - mu_v_b) / sig_v_b
v_b = truncnorm.rvs(a_para, b_para, size=b, loc=mu_v_b, scale=sig_v_b)
return v_b
def create_buyers_q_truncnorm(b, parameters):
'''takes in number of buyers, and parameters to create list of quantities based on truncated normal distribution'''
[mu_q_b, sig_q_b, min_q, max_q] = parameters
a_para, b_para = (min_q - mu_q_b) / sig_q_b, (max_q - mu_q_b) / sig_q_b
q_b = truncnorm.rvs(a_para, b_para, size=b, loc=mu_q_b, scale=sig_q_b)
return q_b
def create_sellers_v_truncnorm(s, parameters):
'''takes in number of buyers, and parameters to create list of valuations based on truncated normal distribution'''
[mu_v_b, sig_v_b, min_v, max_v] = parameters
a_para, b_para = (min_v - mu_v_b) / sig_v_b, (max_v - mu_v_b) / sig_v_b
v_b = truncnorm.rvs(a_para, b_para, size=s, loc=mu_v_b, scale=sig_v_b)
return v_b
def create_sellers_q_truncnorm(s, parameters):
'''takes in number of buyers, and parameters to create list of quantities based on truncated normal distribution'''
[mu_q_s, sig_q_s, min_q, max_q] = parameters
a_para, b_para = (min_q - mu_q_s) / sig_q_s, (max_q - mu_q_s) / sig_q_s
q_b = truncnorm.rvs(a_para, b_para, size=s, loc=mu_q_s, scale=sig_q_s)
return q_b
"""Default Market Ooperations"""
def run_normal_double_auction(buyers_df, sellers_df):
# define sets and parameter values in indexed tuples
n = len(buyers_df)
m = len(sellers_df)
a = OrderedDict()
b = OrderedDict()
c = OrderedDict()
d = OrderedDict()
sellers_counter = 1
for i in range(len(sellers_df)):
c[sellers_counter] = sellers_df.iloc[i, 0]
d[sellers_counter] = sellers_df.iloc[i, 1]
sellers_counter += 1
buyers_counter = 1
for i in range(len(buyers_df)):
a[buyers_counter] = buyers_df.iloc[i, 0]
b[buyers_counter] = buyers_df.iloc[i, 1]
buyers_counter += 1
# create model
model = AbstractModel()
model.n = Param(within=NonNegativeIntegers, default=n)
model.m = Param(within=NonNegativeIntegers, default=m)
# number of buyers
model.I = RangeSet(1, model.n)
# number of sellers
model.J = RangeSet(1, model.m)
# prices for buyers bids
model.a = Param(model.I, initialize=a)
# upper bounds for quantity demanded
model.b = Param(model.I, initialize=b)
# prices for sellers bids
model.c = Param(model.J, initialize=c)
# upper bounds for quantity supplied
model.d = Param(model.J, initialize=d)
# quantities for buyers bids
model.x = Var(model.I, domain=NonNegativeReals)
# quantities for sellers bids
model.y = Var(model.J, domain=NonNegativeReals)
def obj_expression(model):
return summation(model.a, model.x) - summation(model.c, model.y)
model.OBJ = Objective(rule=obj_expression, sense=maximize)
def x_constraint_rule(model, i):
return model.x[i] <= model.b[i]
def y_constraint_rule(model, j):
return model.y[j] <= model.d[j]
def xy_contraint_rule(model):
return sum(model.x[:]) - sum(model.y[:]) == 0
model.Constraint1 = Constraint(model.I, rule=x_constraint_rule)
model.Constraint2 = Constraint(model.J, rule=y_constraint_rule)
model.Constraint3 = Constraint(rule=xy_contraint_rule)
'''solution space'''
instance = model.create_instance() # this is needed if it is an abstract model
instance.dual = Suffix(direction=Suffix.IMPORT) # needed to store dual values
opt = SolverFactory('gurobi')
result = opt.solve(instance)
instance.solutions.store_to(result)
'''
#call duals from instance
print ("Duals")
for c in instance.component_objects(Constraint, active=True):
print (" Constraint",c)
for index in c:
print (" ", index, instance.dual[c[index]])
'''
'''update amount allocated to each seller and buyer by making new list'''
buyers_allocation = []
sellers_allocation = []
for i in range(n):
buyers_allocation.append(result.solution.variable['x[' + str(i + 1) + ']']['Value'])
for i in range(m):
sellers_allocation.append(result.solution.variable['y[' + str(i + 1) + ']']['Value'])
return buyers_allocation, sellers_allocation, result
'''Normal Market Mechanism MCP'''
def get_average_mechanism_MCP(buyers_df, sellers_df):
''' find the highest supply bid and lowest demand bid, MCP is the average price between those two bid prices '''
buyers_in_merit = buyers_df[['price', 'Average allocation']].values.tolist()
sellers_in_merit = sellers_df[['price', 'Average allocation']].values.tolist()
buyers_in_merit2 = [x for x in buyers_in_merit if x[1] != 0]
sellers_in_merit2 = [x for x in sellers_in_merit if x[1] != 0]
if len(buyers_in_merit2) and len(sellers_in_merit2) != 0:
b = min(x[0] for x in buyers_in_merit2)
s = max(x[0] for x in sellers_in_merit2)
MCP = (b + s) / 2
else:
MCP = 0
return buyers_in_merit2, sellers_in_merit2, MCP
def update_deafult_mechanism(buyers_allocation, sellers_allocation, buyers_df, sellers_df):
'''adds columns to sellers and buyers dataframes with the Average allocated values and MCP'''
# prices_buyers = [MCP if x != 0 else 0 for x in buyers_allocation]
# prices_sellers = [MCP if x != 0 else 0 for x in sellers_allocation]
# buyers_df['Average price'] = prices_buyers
buyers_df['Average allocation'] = buyers_allocation
# sellers_df['SW price'] = prices_sellers
sellers_df['Average allocation'] = sellers_allocation
return buyers_df, sellers_df
def update_average_MCP_to_df(buyers_allocation, sellers_allocation, buyers_df, sellers_df, MCP):
prices_buyers = [MCP if x != 0 else 0 for x in buyers_allocation]
prices_sellers = [MCP if x != 0 else 0 for x in sellers_allocation]
buyers_df['Average price'] = prices_buyers
sellers_df['Average price'] = prices_sellers
return buyers_df, sellers_df
"""Huang mechanism: M-1 and L-1 players trade for quantities, bid prices are static"""
def get_Huang_mechanism_MCP(buyers_df, sellers_df):
'''sorts out M-1 buyers, L-1 sellers and adapts equation (5) of paper: A Game-Theoretic Approach to Energy Trading in the Smart Grid'''
buyers_in_merit = buyers_df[['price', 'Average allocation']].values.tolist()
sellers_in_merit = sellers_df[['price', 'Average allocation']].values.tolist()
buyers_in_merit2 = [x for x in buyers_in_merit if x[1] != 0]
sellers_in_merit2 = [x for x in sellers_in_merit if x[1] != 0]
if len(buyers_in_merit2) and len(sellers_in_merit2) != 1:
MCP_buyers = min(x[0] for x in buyers_in_merit2)
MCP_sellers = max(x[0] for x in sellers_in_merit2)
# removes the last feasible pair of bids from the list
del buyers_in_merit2[-1] # pops the last item out so there are M-1 buyers
del sellers_in_merit2[-1] # pops the last item out so there are L-1 buyers
else:
MCP_buyers = 0
MCP_sellers = 0
buyers_in_merit2, sellers_in_merit2 = [], []
return buyers_in_merit2, sellers_in_merit2, MCP_buyers, MCP_sellers
def allocation_Huang_mechanism_Q_a(buyers_in_merit, sellers_in_merit):
'''takes in the agents in merit from function get_Huang_mechanism_MCP and outputs q and d allocation vectors'''
a_vector = [] # takes sellers list and extracts all available quantities they are willing to sell
for i in sellers_in_merit:
a_vector.append(i[1])
x_vector = [] # takes sellers list and extracts all available quantities they are willing to sell
for i in buyers_in_merit:
x_vector.append(i[1])
if len(a_vector) == 0:
q_vector = [0]
d_vector = [0]
elif len(x_vector) == 0:
q_vector = [0]
d_vector = [0]
else:
supply = sum(a_vector)
demand = sum(x_vector)
if supply > demand: # case where we need to apply equation (7)
oversupply = supply - demand
N = len(a_vector) # number of agents initially within merit
B_i = oversupply / N
q_vector = a_vector # this will keep storing updated allocation until condition is met
def find_q_vector(q_vector, B_i, N, demand):
# check if Bi is greater than any value in a_vector, if it is, take out the a_vector and redistribute, do this until all items in a_vector are 0, be careful on the indexing of the vector
min_q = min(x for x in q_vector if x != 0)
if B_i > min_q and min_q != 0: # this means that agents that result in 0 allocation after sharing oversupply burden gets treated as 'in merit'
# B_i = B_i + (B_i - min_q) / (N - 1) #i = 1 by default, otherwise it accounts for identical vector values and adjusts B_i accordingly.
q_vector = [0 if x == min_q else 0 if x == 0 else x for x in q_vector]
'''make code still work if there are 2 vector items that have same value, rn line above will replace same quantity. Bi needs to change accordingly'''
N = sum(1 for i in q_vector if i != 0)
B_i = (sum(q_vector) - demand) / N
print(N, B_i)
return find_q_vector(q_vector, B_i, N, demand)
else:
q_vector = [0 if x == 0 else x - B_i for x in q_vector]
return q_vector
q_vector = find_q_vector(q_vector, B_i, N, demand) # final allocation of a vector
d_vector = x_vector # allocation of demand vector stays the same
elif demand >= supply:
overdemand = demand - supply
N = len(x_vector) # number of agents initially within merit
B_j = overdemand / N
d_vector = x_vector # this will keep storing updated allocation until condition is met
def find_d_vector(d_vector, B_j, N, supply):
# check if Bi is greater than any value in a_vector, if it is, take out the a_vector and redistribute, do this until all items in a_vector are 0, be careful on the indexing of the vector
min_d = min(x for x in d_vector if x != 0)
if B_j > min_d and min_d != 0: # this means that agents that result in 0 allocation after sharing oversupply burden gets treated as 'in merit'
# B_i = B_i + (B_i - min_q) / (N - 1) #i = 1 by default, otherwise it accounts for identical vector values and adjusts B_i accordingly.
d_vector = [0 if x == min_d else 0 if x == 0 else x for x in d_vector]
'''make code still work if there are 2 vector items that have same value, rn line above will replace same quantity. Bi needs to change accordingly'''
N = sum(1 for i in d_vector if i != 0)
B_j = (sum(d_vector) - supply) / N
return find_d_vector(d_vector, B_j, N, supply)
else:
d_vector = [0 if x == 0 else x - B_j for x in d_vector]
return d_vector
q_vector = a_vector # allocation of supply vector stays the same
d_vector = find_d_vector(d_vector, B_j, N, supply) # final allocation of x vector
return q_vector, d_vector
def update_Huang_to_df(q_vector, d_vector, buyers_df, sellers_df, MCP_buyers, MCP_sellers):
'''adds column to sellers and buyers dataframes with the Average allocated quantities'''
d_vector = d_vector + [0] * (len(buyers_df) - len(d_vector))
q_vector = q_vector + [0] * (len(sellers_df) - len(q_vector))
prices_buyers = [MCP_buyers if x != 0 else 0 for x in d_vector]
prices_sellers = [MCP_sellers if x != 0 else 0 for x in q_vector]
buyers_df['Huang price'] = prices_buyers
buyers_df['Huang allocation'] = d_vector
sellers_df['Huang price'] = prices_sellers
sellers_df['Huang allocation'] = q_vector
return buyers_df, sellers_df
def allocative_efficiency_Huang(buyers_df, sellers_df, MCP_buyers, MCP_sellers, result):
# max allocation utility
profit_sellers = 0
utility_buyers = 0
for i in range(len(sellers_df['price'])):
profit_sellers = profit_sellers + (MCP_sellers - sellers_df.loc[i, 'price']) * sellers_df.loc[
i, 'Huang allocation']
for i in range(len(buyers_df['price'])):
utility_buyers = utility_buyers + (buyers_df.loc[i, 'price'] - MCP_buyers) * buyers_df.loc[
i, 'Huang allocation']
eff = (profit_sellers + utility_buyers) / result.solution.objective['OBJ']['Value']
return eff
def VCG_clarke_pivot_rule(buyers_df, sellers_df, result):
prices_sellers = []
prices_buyers = []
SW = result.solution.objective['OBJ']['Value']
for i in range(len(sellers_df)):
index_sellers = i
# find SW without ith seller's welfare
sellers_wf_neg = SW + sellers_df.iloc[index_sellers]['Average allocation'] * sellers_df.iloc[index_sellers]['price']
# run DA with ith seller absent
VCG_sellers_df = sellers_df.drop(index_sellers).reset_index(drop=True)
b, sellers_allocation_VCG, r_sellers = run_normal_double_auction(buyers_df, VCG_sellers_df)
sellers_wf_wo = r_sellers.solution.objective['OBJ']['Value']
# price seller gets = externality/quantity allocated
if sellers_df.iloc[index_sellers]['Average allocation'] == 0:
seller_price = 0
else:
seller_price = abs((sellers_wf_neg - sellers_wf_wo) / sellers_df.iloc[index_sellers]['Average allocation'])
prices_sellers.insert(len(prices_sellers), seller_price)
for i in range(len(buyers_df)):
index_buyers = i
# find SW without ith buyer's welfare
buyers_wf_neg = SW - buyers_df.iloc[index_buyers]['Average allocation'] * buyers_df.iloc[index_buyers]['price']
# run DA with ith seller absent
VCG_buyers_df = buyers_df.drop(index_buyers).reset_index(drop=True)
buyers_allocation_VCG, s, r_buyers = run_normal_double_auction(VCG_buyers_df, sellers_df)
buyers_wf_wo = r_buyers.solution.objective['OBJ']['Value']
# price buyer pays = externality/quantity allocated
if buyers_df.iloc[index_buyers]['Average allocation'] == 0:
buyer_price = 0
else:
buyer_price = abs((buyers_wf_neg - buyers_wf_wo)) / buyers_df.iloc[index_buyers]['Average allocation']
prices_buyers.insert(len(prices_buyers), buyer_price)
return prices_buyers, prices_sellers
def get_VCG_mechanism_MCP(prices_buyers, prices_sellers):
filtered_buyers = list(filter(lambda num: num != 0, prices_buyers))
filtered_sellers = list(filter(lambda num: num != 0, prices_sellers))
MCP_buyers_VCG = sum(filtered_buyers) / len(filtered_buyers)
MCP_sellers_VCG = sum(filtered_sellers) / len(filtered_sellers)
return MCP_buyers_VCG, MCP_sellers_VCG
def update_VCG_prices_and_allocation(prices_buyers, prices_sellers, buyers_df, sellers_df):
'''adds column to sellers and buyers dataframes with the VCG allocated prices'''
buyers_df['VCG price'] = prices_buyers
sellers_df['VCG price'] = prices_sellers
buyers_df['VCG allocation'] = buyers_df['Average allocation']
sellers_df['VCG allocation'] = sellers_df['Average allocation']
return buyers_df, sellers_df
def update_iteration_MCP(MCP_vector_iteration, MCP_df):
MCP_vector = [{'MCP default': MCP_vector_iteration[0],
'MCP VCG buyers': MCP_vector_iteration[1],
'MCP VCG sellers': MCP_vector_iteration[2],
'MCP Huang buyers': MCP_vector_iteration[3],
'MCP Huang sellers': MCP_vector_iteration[4]}]
# append the averaged MCP of iterations to dataframe
MCP_df = MCP_df.append(MCP_vector, ignore_index=True, sort=False)
return MCP_df
def calculate_BSI():
return
def calculate_profits_utility(buyers_df, sellers_df, utility_profits_df):
cent_ut = sum((buyers_df['price'] - buyers_df['Average price']) * buyers_df['Average allocation'])
cent_prof = sum((sellers_df['Average price'] - sellers_df['price']) * sellers_df['Average allocation'])
VCG_ut = sum((buyers_df['price'] - buyers_df['VCG price']) * buyers_df['VCG allocation'])
VCG_prof = sum((sellers_df['VCG price'] - sellers_df['price']) * sellers_df['VCG allocation'])
Huang_ut = sum((buyers_df['price'] - buyers_df['Huang price']) * buyers_df['Huang allocation'])
Huang_prof = sum((sellers_df['Huang price'] - sellers_df['price']) * sellers_df['Huang allocation'])
utility_profits_df = utility_profits_df.append({'Average Utility': cent_ut,
'Average Profit': cent_prof,
'VCG Utility': VCG_ut,
'VCG Profit': VCG_prof,
'Huang Utility': Huang_ut,
'Huang Profit': Huang_prof}, ignore_index=True)
return utility_profits_df
def calculate_market_liquidity(sellers_df, quantity_traded_df):
average_q = sum(sellers_df['Average allocation'])
VCG_q = sum(sellers_df['VCG allocation'])
Huang_q = sum(sellers_df['Huang allocation'])
quantity_traded_df = quantity_traded_df.append({'Average quantity': average_q,
'VCG quantity': VCG_q,
'Huang quantity': Huang_q}, ignore_index=True)
return quantity_traded_df
def calculate_market_size(sellers_df, buyers_df, market_size_df):
"""finds the total quantity put up on the market by both sellers and buys respectively"""
sellers_quantity = sum(sellers_df['quantity'])
buyers_quantity = sum(buyers_df['quantity'])
market_size_df = market_size_df.append({'Aggregate Demand': buyers_quantity, 'Aggregate Supply': sellers_quantity}, ignore_index=True)
return market_size_df
def calculate_mean_MCP(MCP_df, MCP_df_mean):
MCP_mean_data = [{ 'MCP default': MCP_df.mean().values[0],
'MCP VCG buyers': MCP_df.mean().values[1],
'MCP VCG sellers': MCP_df.mean().values[2],
'MCP Huang buyers': MCP_df.mean().values[3],
'MCP Huang sellers': MCP_df.mean().values[4]}]
# append the averaged MCP of iterations to dataframe
MCP_df_mean = MCP_df_mean.append(MCP_mean_data, ignore_index=True, sort=False)
return MCP_df_mean
def df_read_csv(foldername, filename):
"""for reading results in the form of csv files when saved to dataframe subdirectory"""
path = str('C:/Users/Lawrence/Documents/GitHub/Thesis/dataframes/') + foldername + str('/') + filename + str('.csv')
df = pd.read_csv(path, index_col=0)
return df
def df_to_csv(MCP_df, utility_profits_df, quantity_traded_df, market_size_df):
results_df = pd.concat([MCP_df, utility_profits_df, quantity_traded_df, market_size_df], axis=1)
input_title = input('enter title for csv(use underscores): ')
results_df.to_csv(str(input_title)+'.csv')
return
def df_to_csv_w_title(MCP_df, utility_profits_df, quantity_traded_df, market_size_df, title):
results_df = pd.concat([MCP_df, utility_profits_df, quantity_traded_df, market_size_df], axis=1)
input_title = str(title)
results_df.to_csv(str(input_title)+'.csv')
return
def df_to_csv_auto(MCP_df, utility_profits_df, quantity_traded_df, market_size_df, b, s, total_iterations):
results_df = pd.concat([MCP_df, utility_profits_df, quantity_traded_df, market_size_df], axis=1)
input_title = 'iterations_'+ str(total_iterations) +'_default_b_'+str(b)+'_s_'+str(s)
results_df.to_csv(str(input_title)+'.csv')
return
def df_to_csv_RW(MCP_df, utility_profits_df, quantity_traded_df, market_size_df, b, s, total_iterations, hour_counter):
results_df = pd.concat([MCP_df, utility_profits_df, quantity_traded_df, market_size_df], axis=1)
input_title = 'HR_' + str(hour_counter) + '_iterations_'+ str(total_iterations) +'_default_b_'+str(b)+'_s_'+str(s)
results_df.to_csv('C:/Users/Lawrence/Documents/GitHub/Thesis/dataframes/A4_real_world_model/simulation_3_mean_at_boundaries_population50_100penetration/' + str(input_title)+'.csv')
return
'''GAME THEORY FUNCTIONS'''
'''Test Cases'''
def test_case():
#test 1 with tuple format
tuple_list = [(1001, 13.8, 80),
(1002, 13.5, 90),
(1003, 12.7, 100),
(1004, 12, 100),
(1005, 12.8, 50),
(1006, 12.9, 40)]
print(' Test Case Tuple list: ')
print(tuple_list)
return tuple_list
def test_case_2():
# test 2
sellers_prices = [1, 2, 2.9, 3.9, 5, 6]
sellers_quantities = [1, 1, 1, 1, 1, 1]
buyers_prices = [6, 5, 4.1, 3.1, 2, 1]
buyers_quantities = [1, 1, 1, 1, 1, 1]
buyers_df = pd.DataFrame({'price': buyers_prices, 'quantity': buyers_quantities})
sellers_df = pd.DataFrame({'price': sellers_prices, 'quantity': sellers_quantities})
return buyers_df, sellers_df