-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTurbo_utils.py
More file actions
181 lines (147 loc) · 8.04 KB
/
Turbo_utils.py
File metadata and controls
181 lines (147 loc) · 8.04 KB
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
import numpy as np
def is_valid_number(x):
return isinstance(x, (int, float)) and not np.isnan(x) and not np.isinf(x)
def check_valid_params(params):
for key, value in params.items():
if isinstance(value, (list, np.ndarray)):
if np.any(np.isnan(value)) or np.any(np.isinf(value)):
print(f"Invalid value in {key}: {value}")
return False
elif np.isnan(value) or np.isinf(value):
print(f"Invalid value for {key}: {value}")
return False
return True
def find_equilibrium_price(demand, supply, bid, ask, bid_max, ask_min):
price = 0
match bid, ask, demand, supply:
case bid, ask, _, _ if bid >= ask:
price = (bid + ask) / 2
case bid, ask, _, _ if bid < ask:
if demand >= supply:
if bid_max >= ask:
price = (bid_max + ask) / 2
else:
price = bid_max
else:
if bid >= ask_min:
price = (bid + ask_min) / 2
else:
price = ask_min
case _:
print("Weird stuff")
price = (bid + ask) / 2
return price
def best_response_exact(price_decision_data, debug = False):
"""
Compute a heuristic bid or ask price for a player in the two-round market clearing mechanism,
using a simplified approach based on market conditions and private reservation price.
"""
scenario = determine_scenario(price_decision_data)
print(f" market_type: {price_decision_data['market_type']}, scenario: {scenario}")
is_buyer = price_decision_data['is_buyer']
return best_response_scenario_buyer(price_decision_data, debug) if is_buyer else best_response_scenario_seller(price_decision_data, debug)
def determine_scenario(price_decision_data):
"""Determine the current scenario for a buyer or seller."""
#market_type = price_decision_data['market_type']
round_num = price_decision_data['round_num']
demand = price_decision_data['demand']
supply = price_decision_data['supply']
buyer_price = price_decision_data['bid']
seller_price = price_decision_data['ask']
buyer_max_price = price_decision_data['bid_max']
seller_min_price = price_decision_data['ask_min']
market_type = price_decision_data['market_type']
if demand > supply:
market_balance = "ExcessDemand"
elif supply > demand:
market_balance = "ExcessSupply"
else:
market_balance = "Equilibrium"
if round_num == 1:
trade_condition = "Trade" if buyer_price >= seller_price else "NoTrade"
return f"Round1_{market_balance}_{trade_condition}"
else: # Round 2
if market_balance == "ExcessDemand":
trade_condition = "Trade" if buyer_max_price >= seller_price else "NoTrade"
return f"Round2_SellerAdv_{market_balance}_{trade_condition}"
else: # ExcessSupply or Equilibrium (Buyer Advantage)
trade_condition = "Trade" if buyer_price >= seller_min_price else "NoTrade"
return f"Round2_BuyerAdv_{market_balance}_{trade_condition}"
def best_response_scenario_buyer(price_decision_data, debug=False):
scenario = determine_scenario(price_decision_data)
avg_buyer_price = price_decision_data['bid']
avg_seller_price = price_decision_data['ask']
avg_buyer_max_price = price_decision_data['bid_max']
avg_seller_min_price = price_decision_data['ask_min']
pvt_res_price = price_decision_data['pvt_res_price']
previous_price = price_decision_data['previous_price']
demand = price_decision_data['demand']
supply = price_decision_data['supply']
market_type = price_decision_data['market_type']
def adjust_for_imbalance(base_price, upper_bound=None, lower_bound=None):
imbalance_factor = (demand - supply) / (demand + supply)
adjustment = 0.1 * abs(imbalance_factor) + np.random.uniform(0, 0.01) # Ensure some minimum adjustment
if imbalance_factor >= 0:
return min(base_price * (1 + adjustment), upper_bound or float('inf'))
else:
return max(base_price * (1 - adjustment), lower_bound or 0)
def blend_with_previous(target_price):
return previous_price + 0.2 * (target_price - previous_price) # Increased adjustment speed
match scenario:
case "Round1_ExcessDemand_Trade" | "Round1_ExcessDemand_NoTrade":
upper_bound = min(avg_buyer_max_price, pvt_res_price)
target_price = adjust_for_imbalance(max(avg_seller_price, avg_buyer_price), upper_bound=upper_bound)
case "Round2_SellerAdv_ExcessDemand_Trade" | "Round2_SellerAdv_ExcessDemand_NoTrade":
target_price = min(avg_buyer_max_price, pvt_res_price)
case "Round1_Equilibrium_Trade" | "Round1_Equilibrium_NoTrade" | "Round2_BuyerAdv_Equilibrium_Trade" | "Round2_BuyerAdv_Equilibrium_NoTrade":
target_price = (avg_seller_price + avg_buyer_price) / 2 # Meet in the middle
case "Round1_ExcessSupply_Trade" | "Round1_ExcessSupply_NoTrade":
lower_bound = avg_seller_min_price
target_price = adjust_for_imbalance(min(avg_buyer_price, avg_seller_price), lower_bound=lower_bound)
case "Round2_BuyerAdv_ExcessSupply_Trade" | "Round2_BuyerAdv_ExcessSupply_NoTrade":
target_price = avg_seller_min_price
case _:
raise ValueError(f"Unknown scenario: {scenario}")
final_price = target_price #+ np.random.uniform(-0.01, 0.01)
if debug:
print(f"Scenario: {scenario}, Final Price: {final_price}")
return final_price
def best_response_scenario_seller(price_decision_data, debug=False):
scenario = determine_scenario(price_decision_data)
avg_buyer_price = price_decision_data['bid']
avg_seller_price = price_decision_data['ask']
avg_buyer_max_price = price_decision_data['bid_max']
avg_seller_min_price = price_decision_data['ask_min']
pvt_res_price = price_decision_data['pvt_res_price']
previous_price = price_decision_data['previous_price']
demand = price_decision_data['demand']
supply = price_decision_data['supply']
market_type = price_decision_data['market_type']
def adjust_for_imbalance(base_price, lower_bound=None, upper_bound=None):
imbalance_factor = (demand - supply) / (demand + supply)
adjustment = 0.1 * abs(imbalance_factor) + np.random.uniform(-0.01, 0.01) # Ensure some minimum adjustment
if imbalance_factor >= 0:
return min(base_price * (1 + adjustment), upper_bound or float('inf'))
else:
return max(base_price * (1 - adjustment), lower_bound or 0)
def blend_with_previous(target_price):
return previous_price + 0.2 * (target_price - previous_price) # Increased adjustment speed
match scenario:
case "Round1_ExcessDemand_Trade" | "Round1_ExcessDemand_NoTrade":
upper_bound = avg_buyer_max_price
target_price = adjust_for_imbalance(max(avg_seller_price, avg_buyer_price), upper_bound=upper_bound)
case "Round2_SellerAdv_ExcessDemand_Trade" | "Round2_SellerAdv_ExcessDemand_NoTrade":
target_price = avg_buyer_max_price
case "Round1_Equilibrium_Trade" | "Round1_Equilibrium_NoTrade" | "Round2_BuyerAdv_Equilibrium_Trade" | "Round2_BuyerAdv_Equilibrium_NoTrade":
target_price = (avg_seller_price + avg_buyer_price) / 2 # Meet in the middle
case "Round1_ExcessSupply_Trade" | "Round1_ExcessSupply_NoTrade":
lower_bound = max(pvt_res_price, avg_buyer_price)
target_price = adjust_for_imbalance(min(avg_seller_price, avg_buyer_price), lower_bound=lower_bound)
case "Round2_BuyerAdv_ExcessSupply_Trade" | "Round2_BuyerAdv_ExcessSupply_NoTrade":
target_price = max(avg_seller_min_price, pvt_res_price)
case _:
raise ValueError(f"Unknown scenario: {scenario}")
final_price = target_price #+ np.random.uniform(-0.01, 0.01)
if debug:
print(f"Scenario: {scenario}, Final Price: {final_price}")
return final_price