-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute.cpp
186 lines (162 loc) · 7.04 KB
/
compute.cpp
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
#include <vector>
#include <cmath>
#include <iostream>
#include "compute.h"
using namespace std;
const double INF = (1L << 42);
/*
struct Warehouse
{
int row;
int column;
vector<int> products;
};
struct Order
{
int row;
int column;
int nbProducts;
vector<pair<int,int> > products;
};
struct Drone
{
int row;
int column;
int turnFree;
vector<pair<int,int> > products;
vector<string> commands;
};
int rows;
int columns;
int nbDrones;
int nbTicks;
int maxLoad;
int nbProducts;
int nbWarehouses;
int nbOrders;
vector<int> weights;
vector<Warehouse> warehouses;
vector<Order> orders;
vector<Drone> drones;
*/
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
typedef pair<ii,ii> pii;
double distance(int r1, int c1, int r2, int c2) {
return sqrt((r1-r2)*(r1-r2) + (c1-c2)*(c1-c2));
}
void compute(int &rows,int &columns,int &nbDrones,int &nbTicks,int &maxLoad,int &nbProducts,int &nbWarehouses,int &nbOrders,vector<int> &weights,vector<Warehouse> &warehouses,vector<Order> &orders, vector<Drone> &drones)
{
for(int turn = 0; turn < nbTicks; turn++) {
//cerr << turn << endl;
//find an objective for all free drones
for(int d=0; d<drones.size(); ++d) {
if(drones[d].turnFree < turn)
continue;
double maxScore = -1;
pair< vector<pair<int,int>>, int> bestMove;
int bestOrder = -1;
//TODO : take multiples products
for(int o=0;o<orders.size();++o) {
if(orders[o].nbProducts==0)
continue;
double productDistance, droneToWare, dist;
vector<pii> productsToBring;
double maxScoreWare = -1;
int bestWare = -1;
for(int w=0;w<warehouses.size();++w) {
double orderDistance = distance(orders[o].row, orders[o].column, warehouses[w].row, warehouses[w].column);
double scoreWare = 0;
for(int p=0;p<orders[o].products.size();++p) {
int nbOrderProducts = orders[o].products[p].second;
if(warehouses[w].products[orders[o].products[p].first] == 0)
continue;
scoreWare += min(warehouses[w].products[orders[o].products[p].first], nbOrderProducts);
}
scoreWare /= orderDistance;
if(scoreWare > maxScoreWare) {
maxScoreWare = scoreWare;
bestWare = w;
}
}
for(int p=0;p<orders[o].products.size();++p) {
if(orders[o].products[p].second==0)
continue;
if(warehouses[bestWare].products[orders[o].products[p].first] == 0)
continue;
int nbOrderProducts = orders[o].products[p].second;
double orderDistance = distance(orders[o].row, orders[o].column, warehouses[bestWare].row, warehouses[bestWare].column);
productsToBring.push_back(make_pair(
make_pair(p, bestWare),
make_pair(min(warehouses[bestWare].products[orders[o].products[p].first], nbOrderProducts), orderDistance)
));
}
double currentWeight = 0;
while(true) {
cerr << d << endl;
double maxScore = 0;
int bestProduct = -1;
for(int pindex = 0; pindex<productsToBring.size(); pindex++) {
auto p = productsToBring[pindex];
double score = p.second.first;
int pd = (maxLoad - currentWeight)/weights[orders[o].products[p.first.first].first];
score = min((int)score, pd);
if(weights[p.first.first]*p.second.first + currentWeight > maxLoad) {
score = -1;
}
if(score > maxScore) {
bestProduct = pindex;
maxScore = score;
}
}
if(bestProduct==-1)
break;
drones[d].products.push_back(make_pair(productsToBring[bestProduct].first.first, productsToBring[bestProduct].second.first));
currentWeight += maxScore * weights[orders[o].products[productsToBring[bestProduct].first.first].first];
}
if(drones[d].products.size() > maxScore)
{
maxScore = drones[d].products.size();
bestMove = make_pair(drones[d].products, bestWare);
bestOrder = o;
}
cerr << "yo" << endl;
}
if(maxScore > 0) {
cerr << "hello" << endl;
//choose best move and update drone
double droneToWare = distance(drones[d].row, drones[d].column, warehouses[bestMove.second].row, warehouses[bestMove.second].column);
double wareToOrder = distance(orders[bestOrder].row, orders[bestOrder].column, warehouses[bestMove.second].row, warehouses[bestMove.second].column);
drones[d].turnFree += ceil(droneToWare + wareToOrder) + 2*bestMove.first.size();
drones[d].row = orders[bestOrder].row;
drones[d].column = orders[bestOrder].column;
cerr << "hello2" << endl;
//update warehouse
vector<pair<int,int> > products;
for(auto pindex : drones[d].products) {
int p = orders[bestOrder].products[pindex.first].first;
warehouses[bestMove.second].products[p] -= pindex.second;
for(int p2=0; p2<orders[bestOrder].products.size(); ++p2) {
if(orders[bestOrder].products[p2].first == p)
orders[bestOrder].products[p2].second -= pindex.second;
}
}
cerr << "hello3" << endl;
//update order
orders[bestOrder].nbProducts-=bestMove.first.size();
orders[bestOrder].turnDone = drones[d].turnFree;
cerr << "hello4" << endl;
//write command for output
for(auto pindex : drones[d].products) {
auto p = orders[bestOrder].products[pindex.first];
drones[d].commands.push_back(toString(d) + " L " + toString(bestMove.second) + " " + toString(p.first) + " "+ toString(p.second));
}
for(auto pindex : drones[d].products) {
auto p = orders[bestOrder].products[pindex.first];
drones[d].commands.push_back(toString(d) + " D " + toString(bestOrder) + " " + toString(p.first) + " "+toString(p.second));
}
cerr << "duh" << endl;
}
}
}
}