Add files

This commit is contained in:
Andros Fenollosa
2024-11-05 11:46:00 +01:00
parent 9715adb115
commit b97d9383a5
17 changed files with 359 additions and 5 deletions

0
waiting_room/__init__.py Normal file
View File

3
waiting_room/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
waiting_room/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class WaitingRoomConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'waiting_room'

26
waiting_room/consumers.py Normal file
View File

@ -0,0 +1,26 @@
import json
from channels.generic.websocket import WebsocketConsumer
from asgiref.sync import async_to_sync
from waiting_room.tasks import calculate_min_distance
class MyConsumer(WebsocketConsumer):
def connect(self):
self.room_group_name = self.scope["url_route"]["kwargs"]["room_name"]
async_to_sync(self.channel_layer.group_add)(self.room_group_name, self.channel_name)
self.accept()
calculate_min_distance(self.room_group_name)
def disconnect(self, close_code):
async_to_sync(self.channel_layer.group_discard)(self.room_group_name, self.channel_name)
self.close()
def receive(self, text_data):
# Echo
self.send(text_data=text_data)
def channel_message(self, event):
message = event['message']
# Send message to WebSocket
self.send(text_data=str(message))

View File

3
waiting_room/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

70
waiting_room/tasks.py Normal file
View File

@ -0,0 +1,70 @@
from huey.contrib.djhuey import task
import operator
from itertools import permutations
from collections import Counter
from functools import reduce
from math import factorial
from channels.layers import get_channel_layer
from asgiref.sync import async_to_sync
def render_progress_bar(group_name, message):
channel_layer = get_channel_layer()
async_to_sync(channel_layer.group_send)(
group_name,
{
'type': 'channel_message',
'message': message
}
)
@task()
def calculate_min_distance(group_name):
# Distance matrix between cities
distances = [
[0, 29, 20, 21, 16, 31, 100, 12, 5, 78],
[29, 0, 15, 29, 28, 40, 72, 21, 29, 41],
[20, 15, 0, 15, 14, 25, 81, 9, 23, 27],
[21, 29, 15, 0, 4, 12, 92, 12, 25, 13],
[16, 28, 14, 4, 0, 16, 94, 9, 20, 16],
[31, 40, 25, 12, 16, 0, 95, 24, 36, 3],
[100, 72, 81, 92, 94, 95, 0, 90, 101, 99],
[12, 21, 9, 12, 9, 24, 90, 0, 15, 25],
[5, 29, 23, 25, 20, 36, 101, 15, 0, 35],
[78, 41, 27, 13, 16, 3, 99, 25, 35, 0],
]
num_cities = len(distances)
cities = list(range(num_cities))
def count_permutations(sequence):
total = factorial(len(sequence))
duplicates = Counter(sequence).values()
divisor = reduce(operator.mul, (factorial(v) for v in duplicates), 1)
return total / divisor
def calculate_shortest_route(distances):
shortest_route = float("inf")
city_count = 0
# Calculate all possible permutations of cities (routes)
total_permutations = count_permutations(cities)
percentaje = 0
for perm in permutations(cities):
# Send progress to the group
temp_percentaje = int(city_count / total_permutations * 100)
if temp_percentaje != percentaje:
percentaje = temp_percentaje
render_progress_bar(group_name, percentaje)
city_count += 1
# Calculate the distance of the route
route_distance = 0
for i in range(num_cities - 1):
route_distance += distances[perm[i]][perm[i + 1]]
route_distance += distances[perm[-1]][perm[0]] # Back to the start city
shortest_route = min(shortest_route, route_distance)
render_progress_bar(group_name, 100)
return shortest_route
return calculate_shortest_route(distances)

3
waiting_room/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

3
waiting_room/views.py Normal file
View File

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.