-
Notifications
You must be signed in to change notification settings - Fork 0
/
mystrategy.cpp
212 lines (176 loc) · 4.81 KB
/
mystrategy.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
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
#ifndef HEADER_OLNY_INLINE
#include "mystrategy.hpp"
#endif
#include <cmath>
#include <algorithm>
#ifndef HEADER_OLNY_INLINE
#define HEADER_OLNY_INLINE
#define HEADER_OLNY_STATIC
#endif
HEADER_OLNY_INLINE int getNearestToLevelDestination(Simulator &sim, MyElevator& elevator, int level)
{
int delta = 1000;
int targetFloor = level;
for (int id : elevator.passengers)
{
MyPassenger &pass = sim.passengers[id];
int d = std::abs(level - pass.dest_floor);
if (d < delta)
{
delta = d;
targetFloor = pass.dest_floor;
}
}
if (targetFloor == level)
{
return sim.random.get_random() % 8 + 1;
}
return targetFloor;
}
HEADER_OLNY_INLINE int getNearestToLevelDestinationNoRand(Simulator &sim, MyElevator& elevator, int level)
{
int targetFloor = level;
std::map<int, int> points;
std::map<int, int> destNum;
for (int id : elevator.passengers)
{
MyPassenger &pass = sim.passengers[id];
points[pass.dest_floor] -= pass.getValue(elevator.side);
destNum[pass.dest_floor]++;
}
for (std::pair<const int, int> &p : destNum)
{
int k = p.second;
if (k > 1)
{
if (k == 2)
points[p.first] -= 10.0;
else if (k == 3)
points[p.first] -= 20.0;
else if (k == 4)
points[p.first] -= 40.0;
else
points[p.first] -= (40 + (k - 4) * 30);
}
}
auto x = std::min_element(points.begin(), points.end(),
[level](const std::pair<int, int>& p1, const std::pair<int, int>& p2) {
return (p1.second + 70*std::abs(level - p1.first)) < (p2.second + 70*std::abs(level - p2.first)); });
if (x != points.end())
return x->first;
return targetFloor;
}
HEADER_OLNY_INLINE MyStrategy::MyStrategy(Side side) : side(side)
{
strategy1.side = side;
strategy2.side = side;
strategy3.side = side;
strategy4.side = side;
strategy1.firstMoveMinDest = 6;
strategy2.firstMoveMinDest = 5;
strategy3.firstMoveMinDest = 3;
strategy4.firstMoveMinDest = 2;
}
HEADER_OLNY_INLINE MyStrategy::~MyStrategy()
{
}
HEADER_OLNY_INLINE void MyStrategy::makeMove()
{
for (MyElevator & elevator : sim.elevators)
{
if (elevator.side == side && elevator.ind == 0)
{
strategy1.makeMove(sim, elevator, this);
}
else if (elevator.side == side && elevator.ind == 1)
{
strategy2.makeMove(sim, elevator, this);
}
else if (elevator.side == side && elevator.ind == 2)
{
strategy3.makeMove(sim, elevator, this);
}
else if (elevator.side == side && elevator.ind == 3)
{
strategy4.makeMove(sim, elevator, this);
}
}
}
HEADER_OLNY_INLINE void MyStrategy::makeMove(Simulator &inputSim)
{
sim.synchronizeWith(inputSim);
makeMove();
inputSim.copyCommandsFrom(sim, side);
sim.step();
}
HEADER_OLNY_INLINE void ElevatorStrategyUpDown::recalcDestinationBeforeDoorsClose(Simulator &sim, MyElevator &elevator, MyStrategy *strategy)
{
int res = -100000;
int targetFloor = -1;
Side enemySide = inverseSide(strategy->side);
int maxTicks = 7200 - sim.tick - 2;
float coef = 1.0;
if (maxTicks < 1500)
coef = ((float)maxTicks - 300.0f) / 1200.0f;
for (int i = 0; i < LEVELS_COUNT; ++i)
{
if (i != elevator.getFloor())
{
MyStrategy copy = *strategy;
copy.strategy1.doPredictions = false;
copy.strategy2.doPredictions = false;
copy.strategy3.doPredictions = false;
copy.strategy4.doPredictions = false;
MyElevator &el = copy.sim.elevators[elevator.id];
el.go_to_floor = i;
int tick = 0;
for (; tick < std::min(400 + elevator.ind * 40, maxTicks); ++tick)
{
//makeMoveSimple(enemySide, copy.sim);
copy.makeMove();
copy.sim.step();
}
int points;
int valLeft, valRight;
valLeft = copy.sim.scores[0] + copy.sim.passengersTotal[0]*1 + copy.sim.totalCargoValue2(Side::LEFT).points*0.5 * coef;
valRight = copy.sim.scores[1] + copy.sim.passengersTotal[1]*1 + copy.sim.totalCargoValue2(Side::RIGHT).points*0.5 * coef;
if (copy.side == Side::LEFT)
points = valLeft - valRight;
else
points = valRight - valLeft;
int passGone = 0;
for (int id : elevator.passengers)
{
if (!el.passengers.count(id))
{
++passGone;
}
}
if (passGone < 10 && i == 0)
points -= 100;
if (i == elevator.next_floor)
++points;
for (MyElevator &e : sim.elevators)
{
if (e.id != elevator.id && e.state == EState::MOVING && e.next_floor == i)
{
if (e.side == elevator.side && e.ind < elevator.ind)
{
points -= 200;
}
}
}
if (points > res)
{
res = points;
targetFloor = i;
}
}
}
if (targetFloor != -1)
{
int prevTarget = elevator.next_floor;
elevator.go_to_floor = targetFloor;
//std::cout << " Target " << targetFloor << " OLD " << prevTarget << std::endl;
}
}