swarm_sim_header.py 9.11 KB
Newer Older
1
2
3
4
import math
import random
from enum import Enum

Karol Actun's avatar
Karol Actun committed
5
6
from lib.visualization.grid_models import CubicGrid

7
8
9
10
11
12
13
14
15
16

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


black = 1
gray = 2
red = 3
green = 4
blue = 5
yellow = 6
orange = 7
cyan = 8
29
violet = 9
30
31
32
33
34
35
36
37
38
39
40
41
42
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],
43
    violet: [0.8, 0.2, 0.6]
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
}


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


90
91
def coordinates_to_sim(coordinates):
    return coordinates[0], coordinates[1] * math.sqrt(3 / 4)
92
93


94
def sim_to_coordinates(x, y):
95
96
97
    return x, round(y / math.sqrt(3 / 4), 0)


98
def get_coordinates_in_direction(coordinates, direction):
99
100
101
    """
    Returns the coordination data of the pointed directions

102
    :param coordinates: particles actual staying coordination
103
104
105
    :param direction: The direction. Options:  E, SE, SW, W, NW, or NE
    :return: The coordinaiton of the pointed directions
    """
Karol Actun's avatar
Karol Actun committed
106

Karol Actun's avatar
Karol Actun committed
107
    return coordinates[0] + direction[0], coordinates[1] + direction[1], coordinates[2] + direction[2]
108
109


Karol Actun's avatar
Karol Actun committed
110
def global_scanning(matter_map_coordinates_dict, hop, coordinates):
111
    hop_list = []
Karol Actun's avatar
Karol Actun committed
112
113
114
115
116
117
118
119
120
121
122
123

    directions = list(grid.get_directions_dictionary().values())


    def mult(a, s):
        return a[0]*s, a[1]*s, a[2]*s

    for d in directions:
        check_coords = get_coordinates_in_direction(coordinates, d)
        if check_coords in matter_map_coordinates_dict:
            hop_list.append(matter_map_coordinates_dict[check_coords])

124
    for i in range(1, hop):
Karol Actun's avatar
Karol Actun committed
125
126
127
128
129
        for d in directions:
            check_coords = get_coordinates_in_direction(coordinates, mult(d,i))
            if check_coords in matter_map_coordinates_dict:
                hop_list.append(matter_map_coordinates_dict[check_coords])

130
131
132
133
134
135
136
137
138
139
140
141
    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
142
        if (x, y) not in world.tile_map_coordinates:
143
144
145
146
147
148
            world.add_particle(x, y)
        else:
            print(" x and y ", (x, y))
    print("Max Size of created Particle", len(world.particles))


Karol Actun's avatar
Karol Actun committed
149
def create_particle_in_line(world, max_size_particle, start_coordinates, direction = None):
150

Karol Actun's avatar
Karol Actun committed
151
152
153
154
155
156
157
158
159
    # setting direction to first direction of grid
    if direction is None:
        direction = list(grid.directions.values())[0]

    if not grid.is_valid_location(start_coordinates):
        start_coordinates = (0, 0, 0)

    particle_counter = 0
    current_location = start_coordinates
160

Karol Actun's avatar
Karol Actun committed
161
162
163
164
165
166
    while particle_counter < max_size_particle:
        world.add_particle(*current_location)
        current_location = (current_location[0]+direction[0],
                            current_location[1]+direction[1],
                            current_location[2]+direction[2])
        particle_counter += 1
167

168
def create_particle_in_square(world, max_size_particle, start_coordinates):
169

170
171
    for y in range(start_coordinates[1], round(max_size_particle/2)):
        for x in range(start_coordinates[0], round(max_size_particle/2)):
172
173
174
175
176
177
178
179
180
181
            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)


182
def add_particles_as_hexagon(world, radius, color=black):
183
184
185
186
187
188
189
190
191
192
193
194
195
196
    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


197
def add_tiles_as_hexagon(world, radius, color=black):
198
199
200
201
202
203
204
205
206
207
208
209
210
211
    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


212
def add_markers_as_hexagon(world, radius, color=black):
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
    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
    """
273
    next_dir = get_next_direction_to(particle.coordinates[0], particle.coordinates[1], destiny.coordinates[0], destiny.coordinates[1])
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
    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