def punishment_latency(clouds: {}, fogs: {}, peers: {}, tasks: {}, schedule: Schedule) -> float: def punish_func(delay): if delay <= 0: return 0 elif 0 < delay <= 1: return delay else: return (delay**2 + 1) / 2 task_latency_dict = {} ## Calculate transmission latency for single task task_handlers_dict = schedule.get_list_task_handlers( ) # { taskId: [ fogID, CloudId ] } for task_id, handlers in task_handlers_dict.items(): task = tasks[task_id] fog = fogs[handlers[0]] cloud = clouds[handlers[1]] task_latency_dict[task_id] = fog.eta * (task.r_p + task.r_s) + ( fog.eta + cloud.eta) * (task.q_p + task.q_s) ## Calculate processing latency for single task task_handlers_order_dict = schedule.get_list_task_handlers_with_order( ) # { taskId: { fogID: [], CloudId: [] } } for task_id, handlers in task_handlers_order_dict.items(): task = tasks[task_id] task_latency = 0 for idx, (handler_id, list_task_before) in enumerate(handlers.items()): if idx == 0: fog = fogs[handler_id] task_latency += fog.lamda * task.r_p start_time = fog.tau - len(list_task_before) if start_time > 0 and len(list_task_before) != 0: factor = 1 / (start_time**2 + 1) task_latency += factor * fog.lamda * task.r_s else: cloud = clouds[handler_id] task_latency += cloud.lamda * task.q_p start_time = len(list_task_before) factor = 1 / (start_time**2 + 1) task_latency += factor * cloud.lamda * task.q_s task_latency_dict[task_id] += task_latency ## Calculate total punishment total_punish = [ punish_func(task_latency - tasks[task_id].sl_max) for task_id, task_latency in task_latency_dict.items() ] return sum(total_punish)
def test4(): data = Data() data.add_user(123456, 'Will', User("Will", Schedule())) [print('%s: %s\n' % (k, v)) for k, v in data.db['users'].items()] print(data.db) # print(data.db['users']['Will'].schedule.events['Skate'].time.in_time(data.db['users']['Michael'].schedule.courses['CSCI140'].time.start)) #print(data.db) data.write_data()
def test3(): print("Test 3:") michael = User("Michael", Schedule()) michael.schedule.add_course( Course("CSCI140", EventTime(("Mon", "Wed", "Fri"), 1200, 1400), "GOL")) print(michael.schedule.courses["CSCI140"].time.in_time(1300)) # True michael.schedule.add_event(Event("Skate", EventTime(("Mon"), 1800, 1900))) print(michael)
def before_schedule_service( pid, user, sid) -> (code_list.CodeWithMessage, Project, Schedule): e, p = before_project_service(pid=pid, user=user) if e is not None: return e, None, None s = Schedule.get_by_id(sid) if s is None or not p.has_schedule(s): return code_list.ScheduleNoExists, None, None return None, p, s
def postschedule(): req = request.json print(req) stuno = req.get('stuno') schedule = req.get('schedule') stu1 = Student.query.filter(Student.stuNo == stuno).first() newsch = Schedule(*schedule) stu1.schedule = newsch db.session.add(newsch) db.session.commit() return jsonify({'res': True})
def data_forwarding_power(clouds: {}, fogs: {}, peers: {}, tasks: {}, schedule: Schedule) -> float: fog_power = 0 cloud_power = 0 peer_power = 0 tasks_fogs_schedule = {} for fog_id, list_task_id in schedule.schedule_fogs_tasks.items(): for task_id in list_task_id: tasks_fogs_schedule[task_id] = fog_id tasks_clouds_schedule = {} for cloud_id, list_task_id in schedule.schedule_clouds_tasks.items(): for task_id in list_task_id: tasks_clouds_schedule[task_id] = cloud_id for time_slot in range(schedule.total_time): for fog_id, list_task_id in schedule.schedule_fogs_tasks.items(): fog = fogs[fog_id] fog_power += fog.alpha_device_idle + fog.alpha_idle if len(list_task_id) > time_slot: task = tasks[list_task_id[time_slot]] fog_power += (fog.alpha_device + fog.alpha) * (task.r_p + task.r_s) for cloud_id, list_task_id in schedule.schedule_clouds_tasks.items(): if len(list_task_id) <= time_slot: continue task_id = list_task_id[time_slot] task = tasks[task_id] fog = fogs[tasks_fogs_schedule[task_id]] cloud = clouds[cloud_id] cloud_power += fog.alpha_device_idle + fog.alpha_idle + cloud.alpha_idle cloud_power += (fog.alpha_device + fog.alpha + cloud.alpha) * (task.q_p + task.q_s) list_task_handlers = schedule.get_list_task_handlers() for peer_id, list_task_id in schedule.schedule_peers_tasks.items(): for task_id in list_task_id: fog_id, cloud_id = list_task_handlers[task_id] fog = fogs[fog_id] cloud = clouds[cloud_id] peer = peers[peer_id] task = tasks[task_id] peer_power += (fog.alpha_device_idle + fog.alpha_idle + peer.alpha_sm) + \ (fog.alpha_device + fog.alpha + peer.alpha) * (task.r_p + task.r_s) + \ (fog.alpha_device_idle + fog.alpha_idle + cloud.alpha_idle + peer.alpha_sm) + \ (fog.alpha_device + fog.alpha + cloud.alpha + peer.alpha) * (task.q_p + task.q_s) return fog_power + cloud_power + peer_power
def test_save_and_load(self): '''Test the persistance of the schedule settings''' initial_schedule = Schedule() initial_schedule.first_day = date(2012, 1, 12) initial_schedule.last_day = date(2012, 12, 21) # End of the world initial_schedule.save() loaded_schedule = Schedule.load() self.assertEqual(loaded_schedule.first_day, initial_schedule.first_day) self.assertEqual(loaded_schedule.last_day, initial_schedule.last_day)
def schedule_create(pid, user, content, remarks, t_set, t_remind, label): e, p = before_project_service(pid, user) if e is not None: return e, None if len(label) > 5: if not all([len(la) <= 5 for la in label.split(' ')]): return code_list.LabelTooLong if t_remind is None: t_remind = t_set s = Schedule.new(content, p.id, user.id, remarks=remarks, t_set=t_set, t_remind=t_remind, label=label) Action.new(user_id=user.id, project_id=p.id, type_name=action_type.schedule_create.name, content=s.content, link=s.link) return code_list.Success
def add_user(self, user_id, name): self.db['users'][user_id] = User(name, Schedule(dict(), dict()))
def test2(): print("Test 2:") reynaldo = User("Reynaldo", Schedule()) reynaldo.schedule.add_course( Course("CSCI141", EventTime(("Mon"), 1000, 1200), "GOL")) print(reynaldo)
def test1(): print("Test 1:") will = User("Will", Schedule()) print(will)
def matrix_to_schedule(problem:dict, solution: ndarray) -> Schedule: """ Convert matrix data to schedule object :param solution: [ n_clouds + n_fogs, t_tasks ] :return: Schedule obj or None """ clouds = problem["clouds"] fogs = problem["fogs"] tasks = problem["tasks"] fogs_dict = make_dict_from_list_object(fogs) clouds_dict = make_dict_from_list_object(clouds) tasks_dict = make_dict_from_list_object(tasks) n_clouds = problem["n_clouds"] n_fogs = problem["n_fogs"] n_tasks = problem["n_tasks"] n_peers = problem["n_peers"] schedule = Schedule(problem) matrix_cloud, matrix_fog = solution[:n_clouds,], solution[n_clouds:,] # convert matrix_cloud to schedule.schedule_clouds_tasks list_task_stt = argmin(matrix_cloud, axis=0) # List of [task-stt: cloud-stt] (not task_id) for cl_stt in range(n_clouds): list_task_id = [] for task_stt, cloud_stt in enumerate(list_task_stt): if cl_stt == cloud_stt: list_task_id.append(tasks[task_stt].id) schedule.schedule_clouds_tasks[clouds[cl_stt].id] = list_task_id # convert matrix_fog to schedule.schedule_flogs_tasks list_task_stt = argmin(matrix_fog, axis=0) for fg_stt in range(n_fogs): list_task_id = [] for task_stt, fog_stt in enumerate(list_task_stt): if fg_stt == fog_stt: list_task_id.append(tasks[task_stt].id) schedule.schedule_fogs_tasks[fogs[fg_stt].id] = list_task_id # create a schedule for blockchain peers task_peers = {} # task_id: list[Peer] for fog_id, list_task_id in schedule.schedule_fogs_tasks.items(): fog = fogs_dict[fog_id] list_peers = fog.linked_peers for task_id in list_task_id: task_peers[task_id] = list_peers for cloud_id, list_task_id in schedule.schedule_clouds_tasks.items(): cloud = clouds_dict[cloud_id] list_peers = cloud.linked_peers for task_id in list_task_id: if task_id in task_peers.keys(): task_peers[task_id] = list(set(task_peers[task_id] + list_peers)) ### Remove task which not saved in blockchain task_peers_important = {} for task_id, peers in task_peers.items(): if tasks_dict[task_id].label == DefaultData.TASK_LABEL_IMPORTANT: task_peers_important[task_id] = peers ### peer_tasks = {} for task_id, list_peer_id in task_peers_important.items(): for peer_id in list_peer_id: if peer_id in peer_tasks: peer_tasks[peer_id].append(task_id) else: peer_tasks[peer_id] = [task_id] schedule.schedule_peers_tasks = peer_tasks return schedule
def adminSchedule(request): if "login_user" not in request.session: return redirect("/") adminScheduler = loader.get_template("../UI/addSchedule.html") context={} faculty = request.POST["faculty"] subject = request.POST["subject"] day = request.POST["day"] classno = request.POST["classno"] fulltime = request.POST["fulltime"] schedule = Schedule() schedule.subject=subject schedule.faculty = faculty schedule.class_no = classno schedule.day_no = day if fulltime == "11": schedule.start_time = "11:00" schedule.end_time = "12:00" elif fulltime == "12": schedule.start_time = "12:00" schedule.end_time = "01:00" elif fulltime == "1": schedule.start_time = "01:00" schedule.end_time = "02:00" elif fulltime == "2.5": schedule.start_time = "02:30" schedule.end_time = "03:30" elif fulltime == "3.5": schedule.start_time = "03:30" schedule.end_time = "04:30" subjectRepo = SubjectRepo() schedule.subject_id=subjectRepo.get_subjectid(faculty, subject)[0] if schedule.subject_id is None: context["error_msg"] = "Subject didn't matched the faculty" elif not schedule.class_no: context["error_msg"] = "Please enter the class no" else: created_at= timestamp() scheduleRepo = ScheduleRepo() if scheduleRepo.save(schedule,created_at): context["success_msg"] = "Schedule added" else: context["error_msg"] = "Schedule not added" return HttpResponse(adminScheduler.render(context,request))