| import json |
| import random |
| from parameter import * |
| import numpy as np |
| |
| |
| |
| |
| |
| def get_selected_commodity(array, number): |
| array_sum = 0 |
| selected_index = -1 |
| for i in range(len(array)): |
| if array_sum < number <= array_sum + array[i]: |
| selected_index = i |
| break |
| else: |
| array_sum += array[i] |
| return selected_index |
| |
| |
| |
| |
| |
| def get_selected_pod(array): |
| random_index = random.randint(0, len(array) - 1) |
| while True: |
| if array[random_index] == 3: |
| random_index = random.randint(0, len(array) - 1) |
| else: |
| return random_index |
| |
| |
| |
| |
| def g_pod(): |
| if length * weight < n_pod: |
| |
| raise Exception("仓库布局违法") |
| else: |
| Tp, Td = [], [] |
| loc = [] |
| mark_numbers = list(range(length * weight)) |
| random.shuffle(mark_numbers) |
| for i in range(n_pod): |
| Tp.append(c_tp(mark_numbers[i])) |
| Td.append(ratio_T * c_tp(mark_numbers[i])) |
| loc.append(g_loc(mark_numbers[i])) |
| |
| |
| return Tp, Td, loc, [(weight - 1) / 2, length] |
| |
| |
| |
| |
| |
| def c_tp(mark_number): |
| terminus = [(weight - 1) / 2, length] |
| x, y = g_loc(mark_number) |
| return abs(x - terminus[0]) + abs(y - terminus[1]) |
| |
| |
| |
| |
| |
| def g_loc(mark_number): |
| x = mark_number // length |
| y = mark_number % length |
| return x, y |
| |
| |
| if __name__ == '__main__': |
| '''检测布局是否合法''' |
| if n_pod > length * weight: |
| raise Exception("****货架数量与仓库布局不匹配****") |
| if not (pod_limitation[0] < sum(commodity_limitation) / n_pod < pod_limitation[1]): |
| raise Exception("总商品存货数量与货架数量不匹配") |
| if total_order < n_order or total_order > sum(commodity_limitation): |
| raise Exception("订单数量与订单总需求商品不匹配") |
| |
| json_datas = [] |
| for n_data in range(n_datas): |
| |
| '''生成货架分布''' |
| Tp, Td, loc_pod, loc_station = g_pod() |
| |
| |
| |
| |
| '''生成商品分布''' |
| commodities_distribution = np.zeros((n_pod, n_commodity_type), dtype=int) |
| remain_commodity = commodity_limitation[:] |
| remain_commodity_sum = sum(remain_commodity) |
| pod_commodity_sum = [0] * n_pod |
| pod_state = [0] * n_pod |
| |
| for i in range(n_pod): |
| selected_pod = i |
| for _ in range(pod_limitation[0]): |
| random_number = random.randint(1, remain_commodity_sum) |
| selected_commodity = get_selected_commodity(remain_commodity, random_number) |
| |
| commodities_distribution[selected_pod, selected_commodity] += 1 |
| remain_commodity[selected_commodity] -= 1 |
| remain_commodity_sum = sum(remain_commodity) |
| pod_commodity_sum[selected_pod] += 1 |
| pod_state[selected_pod] = 0 if pod_commodity_sum[selected_pod] < pod_limitation[0] else ( |
| 1 if pod_commodity_sum[selected_pod] == pod_limitation[0] else ( |
| 2 if pod_commodity_sum[selected_pod] < pod_limitation[1] else 3)) |
| while remain_commodity_sum != 0: |
| selected_pod = get_selected_pod(pod_state) |
| random_number = random.randint(1, remain_commodity_sum) |
| selected_commodity = get_selected_commodity(remain_commodity, random_number) |
| |
| commodities_distribution[selected_pod, selected_commodity] += 1 |
| remain_commodity[selected_commodity] -= 1 |
| remain_commodity_sum = sum(remain_commodity) |
| pod_commodity_sum[selected_pod] += 1 |
| pod_state[selected_pod] = 0 if pod_commodity_sum[selected_pod] < pod_limitation[0] else ( |
| 1 if pod_commodity_sum[selected_pod] == pod_limitation[0] else ( |
| 2 if pod_commodity_sum[selected_pod] < pod_limitation[1] else 3)) |
| |
| |
| '''生成订单分布''' |
| orders = np.zeros((n_order, n_commodity_type), dtype=int) |
| remain_commodity = commodity_limitation[:] |
| remain_commodity_sum = sum(remain_commodity) |
| remain_order = total_order |
| for i in range(n_order): |
| selected_order = i |
| random_number = random.randint(1, remain_commodity_sum) |
| selected_commodity = get_selected_commodity(remain_commodity, random_number) |
| |
| orders[selected_order, selected_commodity] += 1 |
| remain_commodity[selected_commodity] -= 1 |
| remain_commodity_sum = sum(remain_commodity) |
| remain_order -= 1 |
| while remain_order != 0: |
| selected_order = random.randint(0, n_order - 1) |
| random_number = random.randint(1, remain_commodity_sum) |
| selected_commodity = get_selected_commodity(remain_commodity, random_number) |
| |
| orders[selected_order, selected_commodity] += 1 |
| remain_commodity[selected_commodity] -= 1 |
| remain_commodity_sum = sum(remain_commodity) |
| remain_order -= 1 |
| |
| |
| |
| |
| '''生成json格式数据''' |
| |
| |
| |
| |
| |
| |
| |
| json_data = {'length': length, 'weight': weight, 'n_pod': n_pod, 'pod_limitation': pod_limitation, 'loc_pod': loc_pod, |
| 'loc_station': loc_station, 'n_robot': n_robot, 'ratio_T': ratio_T, 'Tp': Tp, 'Td': Td, |
| 'n_commodity_type': n_commodity_type, 'commodity_limitation': commodity_limitation, |
| 'A': commodities_distribution.tolist(), 'total_order': total_order, 'n_order': n_order, 'B': orders.tolist(), 'Th': Th} |
| json_datas.append(json_data) |
| |
| '''生成json文件''' |
| with open('datas/' + str(n_pod) + 'p_' + str(n_robot) + 'r_' + str(n_datas) + '.json', 'w', encoding='utf8') as fp: |
| |
| |
| json.dump(json_datas, fp, ensure_ascii=False, indent=2) |
| |
| fp.close() |
| print('json file \'' + str(n_pod) + 'p_' + str(n_robot) + 'r_' + str(n_datas) + '.json\'' + ' generates succeed') |