Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
icecoldfire committed Mar 8, 2017
0 parents commit 25a3e5c
Show file tree
Hide file tree
Showing 19 changed files with 764,160 additions and 0 deletions.
92 changes: 92 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover
.hypothesis/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# IPython Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# dotenv
.env

# virtualenv
venv/
ENV/

# Spyder project settings
.spyderproject

# Rope project settings
.ropeproject

#PyCharm
/.idea
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Google Hashcode 2017 Online Qualification Round
Code of team Yoshi (Johannes Bergé, Matthias Wens and Stijn Goethals).

Score: 2175853

1st place Antwerp Hashcode

15th place Belgium

358th place World
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Google Hash Code 2017 Online Qualification Round
--------
Code of team Yoshi (Johannes Bergé, Matthias Wens and Stijn Goethals).

Score: 2175853

1st place Antwerp Hashcode

15th place Belgium

358th place World
Empty file added hash_code_2017_q/__init__.py
Empty file.
43 changes: 43 additions & 0 deletions hash_code_2017_q/caching_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class CachingServer:
def __init__(self, server_id, max_size):
self.server_id = server_id
self.max_size = max_size
self.endpoints_count = 0
self.videos_server = []
self.lijst = []
self.size_gebruikt = 0

def __str__(self):
string = str(self.server_id) + " " + str(self.max_size)
for i in self.videos_server:
string += "\n" + str(i)
return string

def __repr__(self):
return str(self.server_id)

def update_videos(self, videos, endpoints):
# This function makes a list of all posible videos that this server can host.
# This list is sorted, so the videos with the highest score will be put in position 0.
self.lijst = []
for video in videos:
score_berekend = video.requestPerCache(self.server_id, endpoints)
if score_berekend == 0:
continue
self.lijst.append((video, score_berekend))
self.lijst.sort(key=lambda x: x[1], reverse=True)

def vullen(self, endpoints):
# This function fills the server with the videos that received the highest score.
i = 0
while True:
if i >= len(self.lijst):
break
video = self.lijst[i][0]
if self.size_gebruikt + video.groote > self.max_size:
i += 1
continue
self.videos_server.append(video)
video.add_caching_server(self, endpoints)
self.size_gebruikt += video.groote
i += 1
5 changes: 5 additions & 0 deletions hash_code_2017_q/command_line.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from hash_code_2017_q.main import main as package_main


def main():
package_main()
15 changes: 15 additions & 0 deletions hash_code_2017_q/endpoint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class EndPoint:
def __init__(self, endpoint_id, latency, caching_servers):
self.endpoint_id = endpoint_id
self.latency = latency
self.caching_servers = caching_servers
self.caching_servers_in_use = []

def __str__(self):
string = str(self.endpoint_id) + " " + str(self.latency)
for key, val in self.caching_servers.items():
string += "\n " + str(key) + " " + str(val)
return string

def __repr__(self):
return self.endpoint_id
59 changes: 59 additions & 0 deletions hash_code_2017_q/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import os
import time
import math
from read import ReadFile
from tqdm import tqdm
from write import WriteFile
import argparse


def main():
parser = argparse.ArgumentParser(description='Process an map to an route.')
parser.add_argument('-f', '--filename', type=argparse.FileType('r'),
required=True, dest="file", help="Path to the input map file")

args = parser.parse_args()
filename = os.path.splitext(args.file.name)[0].split("/")[-1]
read = ReadFile(args.file)

# Read the input
videos = read.videoList
endpoints = read.epList
requests = read.rqList
servers = read.cachList

start = time.time()

# Solution
# Sort server that are most connected with endpoints
servers.sort(key=lambda x: x.endpoints_count, reverse=True)

# Fill all the servers
for server in tqdm(servers):
server.update_videos(videos, endpoints)
server.vullen(endpoints)
stop = time.time()
print("===========================================")
print("| Time needed: %s ms" % ((stop - start) * 1000))
# print(str(servers))

WriteFile(filename, servers)
calculateScore(requests, endpoints, filename)
print("Problem solved!")


def calculateScore(requests, endpoints, filename):
score = 0
totalReqs = 0
for r in requests:
ep = endpoints[r.endpoint]
score += r.aantal * (ep.latency - r.minLatency) * 1000
totalReqs += r.aantal

totalScore = math.floor(score / totalReqs)
print("| Total Score:" + str(totalScore) + " - File: " + filename)
print("===========================================")


if __name__ == "__main__":
main()
67 changes: 67 additions & 0 deletions hash_code_2017_q/read.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
from endpoint import EndPoint
from video import Video
from request import Request
from caching_server import CachingServer


class ReadFile:
def __init__(self, fp):
"""Read one file to an string"""
string = fp.read()
fp.close()

lines = string.splitlines()

# Analyse the first line of the input
lijn = lines[0].split()
aantalVideos = int(lijn[0])
aantalEndP = int(lijn[1])
aantalReq = int(lijn[2])
aantalCach = int(lijn[3])
cachList = [] # List of caching servers
for i in range(aantalCach): # make caching servers
cach = CachingServer(i, int(lijn[4])) # lijn[int(4)] = size of the servers
cachList.append(cach)

# Analyse the second line of the input
# Each element describes the size of the video
lijn = lines[1].split()
videoList = [] # List of videos
for i in range(aantalVideos):
video = Video(i, int(lijn[i]))
videoList.append(video)

nr = 2 # iteration variable

# The next lines of the input contain the description of the endpoints
epList = [] # List of endpoints
for ep in range(aantalEndP):
lijn = lines[
nr].split() # The first line contains the latency to the datacenter and the number of connected caches
nr += 1
datacLatency = int(lijn[0])
epCache = int(lijn[1])
cach = {}
for i in range(epCache):
lijn = lines[nr].split() # The next i lines describe the latency from the endpoint to each cache
nr += 1
cach[int(lijn[0])] = int(lijn[1])
cachList[int(lijn[0])].endpoints_count += 1
epObject = EndPoint(i, datacLatency, cach)
epList.append(epObject)

# The last lines of the input contain the description of the requests.
# Each line contains the concerning video, endpoint and number of requests.
rqList = [] # List of requests
for rq in range(aantalReq):
lijn = lines[nr].split()
nr += 1
rqObject = Request(int(lijn[0]), int(lijn[1]), int(lijn[2]), epList[int(lijn[1])].latency)
rqList.append(rqObject)
videoList[rqObject.video].addRequest(rqObject) # add the request to the video (for scoring algoritm)

# return videoList,epList,rqList,cachList
self.videoList = videoList
self.epList = epList
self.rqList = rqList
self.cachList = cachList
20 changes: 20 additions & 0 deletions hash_code_2017_q/request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Request:
def __init__(self, video, endpoint, aantal, latency):
self.video = video
self.endpoint = endpoint
self.aantal = aantal
self.minLatency = latency
self.saved = 0

def __str__(self):
string = str(self.video) + " x " + str(self.aantal) + " ->" + str(self.endpoint)
return string

def latencySaved(self):
# Saved latency = self.saved, self.latency = real latency
# dataLatency = latency (endpoint -> datacenter)
data_latency = self.endpoint.latency
self.saved = self.aantal * (data_latency - self.latency)

def __repr__(self):
return str(self.video)
Empty file.
552,883 changes: 552,883 additions & 0 deletions hash_code_2017_q/tests/input/kittens.in

Large diffs are not rendered by default.

Loading

0 comments on commit 25a3e5c

Please sign in to comment.