-
Notifications
You must be signed in to change notification settings - Fork 41
/
Main.java
349 lines (348 loc) · 15.2 KB
/
Main.java
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
import java.util.*;
public class Main {
static class Order{
int normalNum;
int spNum;
int sspNum;
}
static class Path{
List<Integer> path;
int distance;
Path(){
path=new ArrayList<>();
}
}
static int[][] adjacencyMatrix;
static Order[][] orders;
static Random random=new Random(42);
public static void main(String[] args) {
setAdjacencyMatrix();
generateOrders();
problemSolver();
}
static void setAdjacencyMatrix(){
adjacencyMatrix=new int[][]{
{0,0,0,0,0,3,2,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,4,0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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,3,0,0,0,0},
{0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,2,1,0,0,0,0,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,1,0}
};
printAdjacencyMatrix();
}
static void printAdjacencyMatrix(){
System.out.println("邻接矩阵:");
for(int i=0;i<25;i++){
for(int j=0;j<25;j++) {
if(j!=0)System.out.print(" ");
System.out.print(adjacencyMatrix[i][j]);
}
System.out.println();
}
}
static void generateOrders(){
orders=new Order[48][20];
for(int i=0;i<48;i++){
for(int j=0;j<20;j++){
orders[i][j]=new Order();
}
int orderCount=random.nextInt(51);
while(orderCount!=0){
orderCount--;
int index=random.nextInt(20);
int p=random.nextInt(10);
if(p<5){
orders[i][index].normalNum++;
}else if(p<8){
orders[i][index].spNum++;
}else{
orders[i][index].spNum++;
}
}
}
printOrders();
}
static void printOrders(){
System.out.println("订单:");
for(int i=0;i<48;i++){
for(int j=0;j<20;j++){
System.out.print("("+(j+6)+" "+orders[i][j].normalNum+" "+orders[i][j].spNum+" "+orders[i][j].sspNum+")");
if(j!=19) System.out.print(",");
else System.out.println();
}
}
}
static void problemSolver(){
//生成每个配送中心的距离小于等于20的全部回环路径
Map<Integer,List<Path>> loopMap=new HashMap<>();
for(int i=1;i<=5;i++){
int index=i-1;
List<Path> paths=new ArrayList<>();
Queue<Path> q=new LinkedList<>();
Path pth=new Path();
pth.path.add(index);
q.add(pth);
while(q.size()!=0){
pth=q.poll();
if(pth.distance>20) continue;
if(pth.distance!=0&&pth.path.getLast()==index) {
pth.path.removeLast();
pth.path.removeFirst();
paths.add(pth);
continue;
}
for(int j=5;j<25;j++){
if(adjacencyMatrix[pth.path.getLast()][j]!=0){
if(pth.path.size()>=2&&pth.path.get(pth.path.size()-2)==j) continue;
Path t=new Path();
t.distance=pth.distance;
t.path=new ArrayList<>(pth.path);
t.distance+=adjacencyMatrix[pth.path.getLast()][j];
t.path.add(j);
q.add(t);
}
}
if(adjacencyMatrix[pth.path.getLast()][index]!=0){
Path t=new Path();
t.distance=pth.distance;
t.path=new ArrayList<>(pth.path);
t.distance+=adjacencyMatrix[pth.path.getLast()][index];
t.path.add(index);
q.add(t);
}
}
Collections.sort(paths, Comparator.comparingInt((Path a) -> a.distance));
List<Path> removeDuplicatesPaths=new ArrayList<>();
for(int k=0;k<paths.size();k++){
boolean mark=true;
for(int w=0;w<removeDuplicatesPaths.size();w++){
if(removeDuplicatesPaths.get(w).distance==paths.get(k).distance
&&removeDuplicatesPaths.get(w).path.size()==paths.get(k).path.size()){
boolean flag=true;
for(int x=0;x<paths.get(k).path.size();x++){
if(removeDuplicatesPaths.get(w).path.get(removeDuplicatesPaths.get(w).path.size()-x-1)
!=paths.get(k).path.get(x)) {
flag=false;
break;
}
}
if(flag) {
mark=false;
break;
}
}
}
if(mark) removeDuplicatesPaths.add(paths.get(k));
}
loopMap.put(i,removeDuplicatesPaths);
}
System.out.println("每个配送中心的距离小于等于20的全部回环路径:");
for(int i:loopMap.keySet()){
System.out.println("配送中心"+i+":");
List<Path> paths=loopMap.get(i);
for(Path path:paths){
System.out.print("总距离:"+path.distance+",路径:");
for(int j=0;j<path.path.size();j++){
System.out.print(path.path.get(j)+1);
if(j!=path.path.size()-1) System.out.print('-');
else System.out.println();
}
}
}
//生成每个卸货点到配送中心的最短路径
Map<Integer,Path> minimalDistanceMap=new HashMap<>();
for(int i=5;i<25;i++){
PriorityQueue<Path> q=new PriorityQueue<>(Comparator.comparingInt((Path x) -> x.distance));
Path pth=new Path();
pth.path.add(i);
q.add(pth);
while(q.size()!=0){
pth=q.poll();
if(pth.distance>10) continue;
if(pth.path.getLast()<5) {
minimalDistanceMap.put(i,pth);
break;
}
for(int j=0;j<25;j++){
if(adjacencyMatrix[pth.path.getLast()][j]!=0){
if(pth.path.size()>=2&&pth.path.get(pth.path.size()-2)==j) continue;
Path t=new Path();
t.distance=pth.distance;
t.path=new ArrayList<>(pth.path);
t.distance+=adjacencyMatrix[pth.path.getLast()][j];
t.path.add(j);
q.add(t);
}
}
}
}
System.out.println("每个卸货点到配送中心的最短路径:");
for(int i=5;i<25;i++){
Path ph=minimalDistanceMap.get(i);
System.out.print("卸货点:"+(i+1)+",距离:"+ph.distance+",路径:");
for(int k=0;k<ph.path.size();k++){
System.out.print(ph.path.get(k)+1);
if(k!=ph.path.size()-1) System.out.print('-');
else System.out.println();
}
}
//计算每个卸货点到达配送中心小于等于19的全部路径,按距离分组
Map<Integer,Map<Integer,List<Path>>> returnMap=new HashMap<>();
for(int i=5;i<25;i++){
returnMap.put(i,new HashMap<>());
Queue<Path> q=new LinkedList<>();
Path pth=new Path();
pth.path.add(i);
q.add(pth);
while(q.size()!=0){
pth=q.poll();
if(pth.distance>19) continue;
if(pth.path.getLast()<5) {
if (returnMap.get(i).get(pth.distance) ==null){
List<Path> li=new ArrayList<>();
li.add(pth);
returnMap.get(i).put(pth.distance,new ArrayList<>(li));
}else returnMap.get(i).get(pth.distance).add(pth);
continue;
}
for(int j=0;j<25;j++){
if(adjacencyMatrix[pth.path.getLast()][j]!=0){
if(pth.path.size()>=2&&pth.path.get(pth.path.size()-2)==j) continue;
Path t=new Path();
t.distance=pth.distance;
t.path=new ArrayList<>(pth.path);
t.distance+=adjacencyMatrix[pth.path.getLast()][j];
t.path.add(j);
q.add(t);
}
}
}
}
System.out.println("每个卸货点到配送中心的小于等于19的路径:");
for(int i=5;i<25;i++){
System.out.println("卸货点"+(i+1)+":");
Map<Integer,List<Path>> singleLocation=returnMap.get(i);
for(int k:singleLocation.keySet()){
System.out.print("距离为"+k+"的路径:");
for(Path p:singleLocation.get(k)){
for(int j=0;j<p.path.size();j++){
System.out.print(p.path.get(j)+1);
if(j!=p.path.size()-1) System.out.print('-');
else System.out.print(',');
}
}
System.out.println();
}
}
PriorityQueue<Integer> waitingOrder[]=new PriorityQueue[20];
//订单动态分配配送中心,放入等待池批量响应
int nowtime=0;
for(int i=0;i<48;i++){
System.out.println("时间:"+(30*i));
for(int j=0;j<20;j++){
waitingOrder[j]=new PriorityQueue<>();
int normalNum=orders[i][j].normalNum;
int spNum=orders[i][j].spNum;
int sspNum=orders[i][j].sspNum;
for(int k=0;k<normalNum;k++) waitingOrder[j].add(nowtime+30);
for(int k=0;k<spNum;k++) waitingOrder[j].add(nowtime+90);
for(int k=0;k<sspNum;k++) waitingOrder[j].add(nowtime+180);
}
System.out.println(" 批量响应阶段:");
for(int j=0;j<5;j++){
boolean mark=false;
List<Path> deliveryCenterPaths=loopMap.get(j+1);
for(Path p:deliveryCenterPaths){
int cnt;
do{
cnt=0;
List<Integer> opNum=new ArrayList<>();
for(int item:p.path){
int min=Math.min(10-cnt,waitingOrder[item-5].size());
cnt+=min;
opNum.add(min);
if(cnt==10) {
if(!mark){
mark=true;
System.out.println(" 配送中心:"+(j+1));
}
System.out.println(" 检测到无人机满载订单进行响应:");
System.out.print(" ");
for(int x=0;x<opNum.size();x++){
int count=opNum.get(x);
int location=p.path.get(x);
System.out.print("卸货点:"+(location+1)+"-响应订单数:"+count);
if(x!=opNum.size()-1) System.out.print(",");
else System.out.println();
while(count!=0){
waitingOrder[location-5].poll();
count--;
}
}
break;
}
}
}while(cnt==10);
}
}
System.out.println(" 最大等待时间订单响应阶段:");
for(int y=0;y<20;y++){
int cnt=0;
while(waitingOrder[y].size()>0&&waitingOrder[y].peek()==nowtime+30){
waitingOrder[y].poll();
cnt++;
}
Path p= minimalDistanceMap.get(y+5);
while(cnt>10){
System.out.println(" 检测到最大等待时间订单进行响应:");
System.out.print(" ");
for(int z=0;z<p.path.size();z++){
int count=p.path.get(z)==y+5?10:0;
int location=p.path.get(z);
System.out.print("卸货点:"+(location+1)+"-响应订单数:"+count);
if(z!=p.path.size()-1) System.out.print(",");
else System.out.println();
}
cnt-=10;
}
if(cnt>0){
int remains=10-cnt;
System.out.print(" 检测到最大等待时间订单进行响应:");
for(int z=p.path.size()-2;z>=1;z--){
int min=Math.min(remains,waitingOrder[p.path.get(z)-5].size());
int count=min;
int location=p.path.get(z);
System.out.print("卸货点:"+(location+1)+"-响应订单数:"+count);
remains-=min;
while(min!=0){
waitingOrder[p.path.get(z)-5].poll();
min--;
}
}
System.out.println("卸货点:"+(y+5)+"-响应订单数:"+cnt);
}
}
nowtime+=30;
}
}
}