Skip to content

Instantly share code, notes, and snippets.

@Qolt
Created May 19, 2013 17:33
Show Gist options
  • Save Qolt/5608364 to your computer and use it in GitHub Desktop.
Save Qolt/5608364 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import time
import optparse
from datetime import datetime, timedelta
import string
import random
import sys
minute = 60
hour = 60 * minute
time_scale = 0.0002
class time_emulator():
def __init__(self):
self.start_time = time.mktime(datetime.now().timetuple()) + float(datetime.now().strftime('.%f'))
print "Start time: " + str(self.start_time)
def get_current_time(self):
time_delta = (time.mktime(datetime.now().timetuple()) + float(datetime.now().strftime('.%f')))- self.start_time
current_time = int(time_delta / time_scale)
return current_time
class request():
def __init__(self, number, appear_time):
self.number = number
self.appear_time = appear_time
class truck():
def __init__(self):
self.status = "free"
self.work_end_time = 0
self.jobs_counter = 0
self.work_time = 0
if __name__ == "__main__":
requests = []
trucks = [truck(), truck()]
requests_counter = 0
total_requests_counter = 0
link_time_end = 0
current_time = time_emulator()
new_request_time = 5 * minute + random.randint(-4 * minute, 4 * minute)
last_request_time = 0
current_request = 0
truck_number = random.randint(0 ,1)
while (current_time.get_current_time() <= 10 * hour):
if current_time.get_current_time() >= last_request_time + new_request_time:
new_request_time = 5 * minute + random.randint(-4 * minute, 4 * minute)
last_request_time = current_time.get_current_time()
total_requests_counter += 1
if len(requests) < 5:
requests_counter +=1
requests.append(request(requests_counter, current_time.get_current_time()))
if requests != []:
if current_request == requests[0].number:
if truck_number == 1:
truck_number = 0
else:
truck_number = 1
else:
truck_number = random.randint(0 ,1)
current_request = requests[0].number
if requests != [] and link_time_end < current_time.get_current_time():
if trucks[truck_number].status == "free":
trucks[truck_number].status = "busy"
new_job_time = 12 * minute + random.randint(-8 * minute, 8 * minute)
trucks[truck_number].work_end_time = current_time.get_current_time() + new_job_time
trucks[truck_number].work_time += new_job_time
trucks[truck_number].jobs_counter += 1
link_time_end = current_time.get_current_time() + minute
del requests[0]
print "Truck #", truck_number, " Work start at", current_time.get_current_time()," | end at ", trucks[truck_number].work_end_time
if trucks[truck_number].status == "busy":
link_time_end = current_time.get_current_time() + minute
for truck in trucks:
if current_time.get_current_time() > truck.work_end_time:
truck.status = "free"
print "Done ", requests_counter, " requests."
print "Total ", total_requests_counter, " requests."
for i, truck in enumerate(trucks):
print "Truck #",i ,"done ", truck.jobs_counter, " requests. Spent ", truck.work_time, " sec at work."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment