swarm_sim_header.py 11.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import math
import random
from enum import Enum


class Colors(Enum):
    black = 1
    gray = 2
    red = 3
    green = 4
    blue = 5
    yellow = 6
    orange = 7
    cyan = 8
15
    violet = 9
16
17
18
19
20
21
22
23
24
25
26
    dark_green = 10


black = 1
gray = 2
red = 3
green = 4
blue = 5
yellow = 6
orange = 7
cyan = 8
27
violet = 9
28
29
30
31
32
33
34
35
36
37
38
39
40
dark_green = 10


color_map = {
    black: [0.0, 0.0, 0.0],
    gray: [0.3, 0.3, 0.3],
    red: [0.8, 0.0, 0.0],
    green: [0.0, 0.8, 0.0],
    dark_green: [0.2, 1, 0.6],
    blue: [0.0, 0.0, 0.8],
    yellow: [0.8, 0.8, 0.0],
    orange: [0.8, 0.3, 0.0],
    cyan: [0.0, 0.8, 0.8],
41
    violet: [0.8, 0.2, 0.6]
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
}


NE=0
E = 1
SE = 2
SW = 3
W = 4
NW = 5


direction_list = [NE, E, SE, SW, W, NW]

x_offset = [0.5, 1,  0.5,   -0.5,   -1, -0.5 ]
y_offset = [ 1, 0, -1,   -1,    0,  1]


def direction_number_to_string(direction):
    """
    :param direction: the direction that should get converted to a string
    :return: the string of the direction
    """
    if direction == 0:
        return "NE"
    elif direction == 1:
        return "E"
    elif direction == 2:
        return "SE"
    elif direction == 3:
        return "SW"
    elif direction == 4:
        return "W"
    elif direction == 5:
        return "NW"
    else:
        return "Error"


def get_the_invert(direction):
    return (direction + 3) % 6


def direction_in_range(direction):
    return direction % 6


88
def check_values_are_coordinates(coordinates_x, coordinates_y):
89
90
91
92
    """
    Checks if the given coordinates are matching the
    hexagon coordinates

93
94
    :param coordinates_x: proposed x coordinate
    :param coordinates_y: proposed y coordinate
95
96
97
    :return: True: Correct x and y coordinates; False: Incorrect coordinates
    """

98
99
    if (coordinates_x / 0.5) % 2 == 0:
        if coordinates_y % 2 != 0:
100
101
102
103
            return False
        else:
            return True
    else:
104
        if coordinates_y % 2 == 0:
105
106
107
108
109
            return False
        else:
            return True


110
111
def coordinates_to_sim(coordinates):
    return coordinates[0], coordinates[1] * math.sqrt(3 / 4)
112
113


114
def sim_to_coordinates(x, y):
115
116
117
    return x, round(y / math.sqrt(3 / 4), 0)


118
def get_coordinates_in_direction(coordinates, direction):
119
120
121
    """
    Returns the coordination data of the pointed directions

122
    :param coordinates: particles actual staying coordination
123
124
125
    :param direction: The direction. Options:  E, SE, SW, W, NW, or NE
    :return: The coordinaiton of the pointed directions
    """
126
    return coordinates[0] + x_offset[direction], coordinates[1] + y_offset[direction]
127
128


129
def global_scanning(matter_map_coordinates_dict, hop, starting_x, starting_y):
130
    hop_list = []
131
132
133
134
135
136
137
138
139
140
141
142
    if (hop / 2 + starting_x, hop + starting_y) in matter_map_coordinates_dict:
        hop_list.append(matter_map_coordinates_dict[(hop / 2 + starting_x, hop + starting_y)])
    if (hop + starting_x, starting_y) in matter_map_coordinates_dict:
        hop_list.append(matter_map_coordinates_dict[(hop + starting_x, starting_y)])
    if (hop / 2 + starting_x, -hop + starting_y) in matter_map_coordinates_dict:
        hop_list.append(matter_map_coordinates_dict[(hop / 2 + starting_x, -hop + starting_y)])
    if (-hop / 2 + starting_x, -hop + starting_y) in matter_map_coordinates_dict:
        hop_list.append(matter_map_coordinates_dict[(-hop / 2 + starting_x, -hop + starting_y)])
    if (-hop + starting_x, starting_y) in matter_map_coordinates_dict:
        hop_list.append(matter_map_coordinates_dict[(-hop + starting_x, starting_y)])
    if (-hop / 2 + starting_x, hop + starting_y) in matter_map_coordinates_dict:
        hop_list.append(matter_map_coordinates_dict[(-hop / 2 + starting_x, hop + starting_y)])
143
    for i in range(1, hop):
144
145
146
        if (-hop / 2 + i + starting_x, hop + starting_y) in matter_map_coordinates_dict:
            hop_list.append(matter_map_coordinates_dict[(-hop / 2 + i + starting_x, hop + starting_y)])
        if (hop / 2 + (0.5 * i) + starting_x, hop - i + starting_y) in matter_map_coordinates_dict:
147
            hop_list.append(
148
149
                matter_map_coordinates_dict[(hop / 2 + (0.5 * i) + starting_x, hop - i + starting_y)])
        if (hop / 2 + (0.5 * i) + starting_x, -hop + i + starting_y) in matter_map_coordinates_dict:
150
            hop_list.append(
151
152
153
154
                matter_map_coordinates_dict[(hop / 2 + (0.5 * i) + starting_x, -hop + i + starting_y)])
        if (-hop / 2 + i + starting_x, -hop + starting_y) in matter_map_coordinates_dict:
            hop_list.append(matter_map_coordinates_dict[(-hop / 2 + i + starting_x, -hop + starting_y)])
        if (-hop / 2 - (0.5 * i) + starting_x, -hop + i + starting_y) in matter_map_coordinates_dict:
155
            hop_list.append(
156
157
                matter_map_coordinates_dict[(-hop / 2 - (0.5 * i) + starting_x, -hop + i + starting_y)])
        if (-hop / 2 - (0.5 * i) + starting_x, hop - i + starting_y) in matter_map_coordinates_dict:
158
            hop_list.append(
159
                matter_map_coordinates_dict[(-hop / 2 - (0.5 * i) + starting_x, hop - i + starting_y)])
160
161
162
163
164
165
166
167
168
169
170
171
    return hop_list



# Helping Methods for creating scenarios

def generating_random_spraded_particles (world, max_size_particle):
    for _ in range(0, max_size_particle):
        x = random.randrange(-world.get_world_x_size(), world.get_world_x_size())
        y = random.randrange(-world.get_world_y_size(), world.get_world_y_size())
        if y % 2 == 1:
            x = x + 0.5
172
        if (x, y) not in world.tile_map_coordinates:
173
174
175
176
177
178
            world.add_particle(x, y)
        else:
            print(" x and y ", (x, y))
    print("Max Size of created Particle", len(world.particles))


179
180
181
def create_particle_in_line(world, max_size_particle, start_coordinates):
    if start_coordinates[0] % 1 != 0:
        start_i = int(start_coordinates[0] - 0.5)
182
        for i in range(start_i, start_i+max_size_particle):
183
            world.add_particle(i + 1.5, start_coordinates[1])
184
185

    else:
186
187
        for i in range(int(start_coordinates[0] + 1), int(start_coordinates[0] + 1) + max_size_particle):
            world.add_particle(i, start_coordinates[1])
188
189


190
def create_particle_in_square(world, max_size_particle, start_coordinates):
191

192
193
    for y in range(start_coordinates[1], round(max_size_particle/2)):
        for x in range(start_coordinates[0], round(max_size_particle/2)):
194
195
196
197
198
199
200
201
202
203
            world.add_particle(x + 0.5, 2 * y + 1.0)
            world.add_particle(-(x + 0.5), 2 * y + 1.0)
            world.add_particle(x + 0.5, -(2 * y + 1.0))
            world.add_particle(-(x + 0.5), -(2 * y + 1.0))
            world.add_particle(x, 2 * y)
            world.add_particle(-x, 2 * y)
            world.add_particle(x, -2 * y)
            world.add_particle(-x, -  2 * y)


204
def add_particles_as_hexagon(world, radius, color=black):
205
206
207
208
209
210
211
212
213
214
215
216
217
218
    world.add_particle(0, 0, color)
    displacement = - radius + 0.5
    iteration = 0
    for i in range(1, radius + 1):
        world.add_particle(i, 0, color)
        world.add_particle(-i, 0, color)
    for i in range(1, radius + 1):
        for j in range(0, (2 * radius) - iteration):
            world.add_particle(displacement + j, i, color)
            world.add_particle(displacement + j, -i, color)
        iteration = iteration + 1
        displacement = displacement + 0.5


219
def add_tiles_as_hexagon(world, radius, color=black):
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    world.add_tile(0, 0, color)
    displacement = - radius + 0.5
    iteration = 0
    for i in range(1, radius + 1):
        world.add_tile(i, 0, color)
        world.add_tile(-i, 0, color)
    for i in range(1, radius + 1):
        for j in range(0, (2 * radius) - iteration):
            world.add_tile(displacement + j, i, color)
            world.add_tile(displacement + j, -i, color)
        iteration = iteration + 1
        displacement = displacement + 0.5


234
def add_markers_as_hexagon(world, radius, color=black):
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
    world.add_marker(0, 0, color)
    displacement = - radius + 0.5
    iteration = 0
    for i in range(1, radius + 1):
        world.add_marker(i, 0, color)
        world.add_marker(-i, 0, color)
    for i in range(1, radius + 1):
        for j in range(0, (2 * radius) - iteration):
            world.add_marker(displacement + j, i, color)
            world.add_marker(displacement + j, -i, color)
        iteration = iteration + 1
        displacement = displacement + 0.5


def create_tiles_formed_as_hexagons_border(world, radius, starting_x = 0, starting_y = 0):
    offset_x = 0
    if starting_y % 2 != 0:
        offset_x = 0.5
    world.add_tile(radius/2 + starting_x + offset_x, radius + starting_y)
    world.add_tile(radius + starting_x + offset_x, starting_y)
    world.add_tile(radius/2 + starting_x + offset_x, -radius  + starting_y)
    world.add_tile(-radius/2 + starting_x + offset_x, -radius + starting_y)
    world.add_tile(-radius + starting_x + offset_x, starting_y)
    world.add_tile(-radius/2 + starting_x + offset_x, radius + starting_y)

    for i in range(1, radius):
        world.add_tile(-radius/2 + i + starting_x + offset_x, radius + starting_y)
        world.add_tile(radius/2 + (0.5 * i ) + starting_x + offset_x, radius - i + starting_y)
        world.add_tile(radius/2 + (0.5 * i) + starting_x + offset_x, -radius + i + starting_y)
        world.add_tile(-radius/2 + i + starting_x + offset_x, -radius + starting_y)
        world.add_tile(-radius/2 - (0.5 * i) + starting_x + offset_x, -radius + i + starting_y)
        world.add_tile(-radius/2 - (0.5 * i) + starting_x + offset_x, radius - i + starting_y)


# Helping methods for Solution


def scan_neighborhood(particle):
    """
    :param particle:
    :return: a dictionary with the direction and the founded matter
    """
    nh_dict={}
    for direction in direction_list:
        nh_dict[direction] = particle.get_matter_in(direction)


def move_to_dest_in_one_rnd(particle, destiny):
    if move_to_dest_step_by_step(particle, destiny):
        return True
    move_to_dest_in_one_rnd(particle, destiny)


def move_to_dest_step_by_step(particle, destiny):
    """

    :param particle:
    :param destiny:
    :return: True if movement occured, False if not movment and a Matter if the next direction point has a matter on it
    """
295
    next_dir = get_next_direction_to(particle.coordinates[0], particle.coordinates[1], destiny.coordinates[0], destiny.coordinates[1])
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
    if particle.matter_in(next_dir):
        particle.get_matter_in(next_dir)
        return particle.get_matter_in(next_dir)
    particle.move_to(next_dir)
    print("\n P", particle.number, " moves to", direction_number_to_string(next_dir))
    return False


def get_next_direction_to(src_x, src_y, dest_x, dest_y):
    """
    :param src_x: x coordinate of the source
    :param src_y: y coordinate of the source
    :param dest_x: x coordinate of the destiny
    :param dest_y: y coordinate of the destiny
    :return: the next direction that brings the matter closer to the destiny
    """
    next_dir = -1
    if (src_x < dest_x or src_x == dest_x) and src_y < dest_y:
        next_dir = NE
    elif src_y < dest_y and src_x > dest_x:
        next_dir = NW
    elif src_y > dest_y and src_x < dest_x:
        next_dir = SE
    elif (src_x > dest_x or src_x == dest_x) and src_y > dest_y :
        next_dir = SW
    elif src_y == dest_y and src_x > dest_x:
        next_dir = W
    elif src_y == dest_y and src_x < dest_x:
        next_dir = E
    return next_dir