Repository navigation
Expand file tree
/
Copy pathSimulation.cs
More file actions
153 lines (125 loc) · 4.44 KB
/
Copy pathSimulation.cs
File metadata and controls
153 lines (125 loc) · 4.44 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Shell;
namespace Hash2016
{
static class Simulation
{
public static bool isRunning;
public static int Rows;
public static int Columns;
public static int Deadline;
public static int DroneMaxLoad;
public static int ProductsCount;
public static int[] ProductsWeights;
public static List<Drone> drones = new List<Drone>(1024);
public static List<Warehouse> Warehouses = new List<Warehouse>(1024);
public static List<Order> Orders = new List<Order>(1024);
public static CommandBuffer Commands = new CommandBuffer();
public static void Run(string input)
{
if (isRunning)
return;
isRunning = true;
// Clear previous data
Clear();
// Load data
Loader.Load(input);
// Pre simulation works do be done:
Orders.Sort();
divideDrones();
// Simulation
Deadline = 10;
List<Warehouse> www = new List<Warehouse>(Warehouses.Count);
for (int i = 0; i < Deadline; i++)
{
int dronesInUse = 0;
while (dronesInUse < drones.Count - 1)
{
Order o = Orders[0];
Orders.RemoveAt(0);
www.AddRange(Warehouses);
while (o.Set.Count > 0)
{
Warehouse closeW = findCLosestWarehouse(www, o.GetDestination(), o.Set);
Drone d = drones[dronesInUse++];
int productID;
int count;
for (int a = 0; a < ProductsCount; a++)
{
if (closeW.Set.Items[a] > 0 && o.Set.Items[a] > 0)
{
productID = a;
count = Math.Min(closeW.Set.Items[a], o.Set.Items[a]);
closeW.Set.Remove(productID, count);
o.Set.Remove(productID, count);
Commands.Load(d, closeW, productID, count);
Commands.Deliver(d, o, productID, count);
}
}
if (dronesInUse == drones.Count)
break;
}
JumpItem:
www.Clear();
}
if (Orders.Count == 0)
break;
}
// Write commands
Commands.Write(input + ".out");
isRunning = false;
MessageBox.Show("Done.");
}
public static void divideDrones()
{
int allProducts= 0;
foreach (Warehouse wh in Warehouses)
{
allProducts+= wh.Set.Count;
}
int dronesUsed=0;
foreach (Warehouse wh in Warehouses)
{
wh.numberOfDrones = (wh.Set.Count / allProducts) * drones.Count;
dronesUsed += wh.numberOfDrones;
}
if (drones.Count - dronesUsed > 0)//rozdanie wolnych dronów
{
Warehouse w = Warehouses.ElementAt(0);
w.numberOfDrones += drones.Count - dronesUsed;
}
}
public static Warehouse findCLosestWarehouse(List<Warehouse> ww, IntVector2 v, ProductsSet set)
{
int minDst = int.MaxValue;
Warehouse reslt = null;
for (int i = 0; i < Warehouses.Count; i++)
{
if (ww[i].Set.HaveSthSimilar(set))
{
int dst = (int)Math.Ceiling(ww[i].Pos.Distance(v));
if (dst < minDst)
{
reslt = Warehouses[i];
minDst = dst;
}
}
}
return reslt;
}
public static void Clear()
{
Warehouses.Clear();
Orders.Clear();
drones.Clear();
Commands.Clear();
ProductsWeights = null;
GC.Collect();
}
}
}