File size: 7,106 Bytes
295accb | 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 | import numpy as np
import pandas as pd
import random
import warnings
warnings.filterwarnings('ignore')
def fill_demand_data(data, count_data, mode):
for i in range(len(count_data)):
Date = count_data.loc[i, 'Date']
hour = count_data.loc[i, 'start_hour']
cluster = count_data.loc[i, 'cluster']
num = count_data.loc[i, 'count']
if mode == 'rent':
data.loc[(data['Date'] == Date) & (data['start_hour'] == hour), '{}_rent'.format(cluster)] = num
else:
data.loc[(data['Date'] == Date) & (data['start_hour'] == hour), '{}_return'.format(cluster)] = num
return data
def fill_inventory(target_data, bike_inventory, battery_inventory):
data = target_data.copy()
data['penalty_1'] = 0
data['penalty_0'] = 0
for i in range(len(data)):
if data["1_I_0"][i] < 0:
for s in range(station_num):
pure_demand = data["{}_rent".format(s)][i - 1] - data["{}_return".format(s)][i - 1]
high_surplus = data["{}_I_2".format(s)][i - 1] + data["{}_dispatch_2".format(s)][i - 1] + \
data["{}_exchange_0".format(s)][i - 1] + data["{}_exchange_1".format(s)][i - 1]
data.loc[i, "{}_I_2".format(s)] = max(0, high_surplus - pure_demand) # -data["{}_pick".format(s)][i-1]
low_satisfy = pure_demand - min(pure_demand, high_surplus)
low_surplus = data["{}_I_1".format(s)][i - 1] + data["{}_dispatch_1".format(s)][i - 1] - \
data["{}_exchange_1".format(s)][i - 1]
data.loc[i, "{}_I_1".format(s)] = max(0, low_surplus - low_satisfy)
no_satisfy = low_satisfy - min(low_surplus, low_satisfy)
data.loc[i, "{}_I_0".format(s)] = max(0,
data["{}_I_0".format(s)][i - 1] + data["{}_dispatch_0".format(s)][
i - 1] - data["{}_exchange_0".format(s)][i - 1])
data.loc[i, "penalty_1"] = data.loc[i, "penalty_1"] + low_satisfy - no_satisfy
data.loc[i, "penalty_0"] = data.loc[i, "penalty_0"] + no_satisfy
else:
for s in range(station_num):
for e in soc:
data.loc[i, "{}_I_{}".format(s, e)] = int(data["{}_I_{}".format(s, e)][i] * random.random())
car_bike_inventory = [0, 0, bike_inventory]
car_battery_inventory = battery_inventory
if data['start_hour'][i] == 14:
for s in range(station_num):
high_low = int(random.random() * data["{}_I_2".format(s)][i] * 0.5)
data.loc[i, "{}_I_2".format(s)] = data["{}_I_2".format(s)][i] - high_low
low_no = int(random.random() * data["{}_I_1".format(s)][i] * 0.5)
data.loc[i, "{}_I_1".format(s)] = data["{}_I_1".format(s)][i] + high_low - low_no
data.loc[i, "{}_I_0".format(s)] = data["{}_I_0".format(s)][i] + low_no
# dispatch ebike
for s in range(station_num):
dock = dock_dict[s]
station_inventory = [data["{}_I_{}".format(s, e)][i] for e in soc]
for e in soc:
num = random.randint(-5, 5)
if num > 0:
real_num = min(car_bike_inventory[e], num, max(0, dock - sum(station_inventory)))
else:
real_num = -min(-num, bike_capacity - car_bike_inventory[e], station_inventory[e])
# data.loc[i,"{}_pick".format(s)]=real_num
# car_inventory=car_inventory+real_num
data.loc[i, "{}_dispatch_{}".format(s, e)] = real_num
car_bike_inventory[e] = car_bike_inventory[e] - real_num
# exchange battery
for s in range(station_num):
station_inventory = [data["{}_I_{}".format(s, e)][i]+data["{}_dispatch_{}".format(s, e)][i] for e in soc]
for e in range(2):
num = random.randint(0, 5)
real_num = min(car_battery_inventory, num, station_inventory[e])
# real_num=-min(-num,battery_capacity-car_bike_inventory[e],station_inventory[e])
# # data.loc[i,"{}_pick".format(s)]=real_num
# # car_inventory=car_inventory+real_num
data.loc[i, "{}_exchange_{}".format(s, e)] = real_num
car_battery_inventory = car_battery_inventory - real_num
return data
def fill_label(target_data):
data = target_data.copy()
data['label'] = 0
for i in range(len(data)):
label = 0
for s in range(station_num):
cost = sum(abs(data["{}_dispatch_{}".format(s, e)][i]) for e in soc) * bike_cost + sum(
data["{}_exchange_{}".format(s, e)][i] for e in range(2)) * battery_cost + data["penalty_1"][
i] * penalty_1 + data["penalty_0"][i] * penalty_0
# index=data["{}_I".format(s)][i]+data["{}_return".format(s)][i]+-data["{}_rent".format(s)][i]
# # -data["{}_pick".format(s)][i]
# if index<0:
# label=label+abs(index)
# else:
# docks=dock_dict[s]
# label=label+max(0,index-docks)
label = label + cost
data.loc[i, 'label'] = label
return data
def generate_train_data(origin_data, time=10):
for t in range(time):
data = origin_data.copy()
grouped = data.groupby(['Date']).head(1)
for i in range(station_num):
num = dock_dict[i]
grouped['{}_I_2'.format(i)] = num * 0.7
grouped['{}_I_1'.format(i)] = num * 0.1
grouped['{}_I_0'.format(i)] = num * 0.1
for i in range(station_num):
for e in soc:
data.loc[grouped.index, '{}_I_{}'.format(i, e)] = grouped.loc[:, '{}_I_{}'.format(i, e)]
bike_inventory = random.randint(int(bike_capacity* 0.7) , bike_capacity)
battery_inventory = random.randint(int(battery_capacity* 0.7) , battery_capacity)
data_1 = fill_inventory(data, bike_inventory, battery_inventory)
data_2 = fill_label(data_1)
if t == 0:
target_data = data_2
else:
target_data = pd.concat([target_data, data_2], ignore_index=True)
target_data.to_csv(r"ebike_data/data04_cluster_5.csv", index=False)
return
if __name__ == '__main__':
station_num = 5 #55
bike_capacity = 60
battery_capacity = 200
random.seed(10)
soc = [0, 1, 2]
bike_cost = 1
battery_cost = 1
penalty_1 = 0.4
penalty_0 = 0.8
dock_dict = {}
for i in range(station_num):#各个站点总停车位数量
dock_dict[i] = int(50 * np.random.triangular(0.6, 0.8, 1))
print(dock_dict)
data = pd.read_csv(r"ebike_data/origin_data_4_cluster_5.csv")
generate_train_data(data, time=1) #要改文件名
|