def find_all_detectable_asteroids(asteroid, other_asteroids): detectable = SortedDict() for other_asteroid in other_asteroids: if other_asteroid == asteroid: continue v = other_asteroid[1] - asteroid[1], asteroid[0] - other_asteroid[0] rad = angle((0, 1), v) dist = distance(asteroid, other_asteroid) if rad not in detectable or dist < distance(asteroid, detectable[rad]): detectable[rad] = other_asteroid return detectable
def main(): root = Node(10) root.left = Node(15) root.left.left = Node(35) root.left.right = Node(20) root.right = Node(25) root.left.left.left = Node(40) root.left.right.right = Node(75) root.left.right.right.right = Node(80) a = vertical_traversal(root, SortedDict()) for i in a.values(): print(i)
def add_service(self, zeroconf, type, name): info = zeroconf.get_service_info(type, name) self.availableServices[name] = info props = { k.decode(): info.properties[k].decode() for k in info.properties } # row = {'algorithm':props['algorithm'],'address':socket.inet_ntoa(info.addresses[0]),'port':info.port,'FaRO version':props['version']} address = socket.inet_ntoa(info.addresses[0]) if self.hostname == address: address = "localhost" row = SortedDict({ 'address': address, 'port': info.port, 'found via': 'bonjour' }) row.update(props) if 'functionality' in row: del row['functionality'] self.availableServices_tableform[name] = row self.lastUpdate = time.time()
def update(self, episode): if self.log_enabled: self.logger.debug('OB update %s starts', episode[0]["microtimestamp"]) episode = sorted(episode, key=lambda e: e["event_no"]) self.changed_price_levels = SortedDict() for e in episode: if e["event_no"] > 1: self._remove(e["order_id"]) if self.log_enabled: self.logger.debug(spread_log(self)) self._add(e) if self.log_enabled: self.logger.debug(spread_log(self)) depth_changes = self._post_update() if self.log_enabled: self.logger.debug('OB update %s ends', episode[0]["microtimestamp"]) return depth_changes
def getRunningLocalWorkers(options): #no zeroconf localservices = [] for domain in faro.util.getServiceDomains(): # print('looking at', domain) if faro.util.pingDomain(domain): portrange = range(50000, 50254) if options.verbose: portrange = tqdm(portrange) options.service_name = None for portnum in portrange: t0 = time.time() port = domain + ":" + str(portnum) options.port = port face_client = connectToFaroClient(options, no_exit=True, quiet=True, timeout=2) available, message = face_client.status(timeout=2) if available: row = SortedDict({ 'address': domain, 'port': str(portnum), 'Algorithm': message.algorithm, "workers": message.worker_count, "Name": message.instance_name, "FaRO version": message.faro_version, "found via": 'port sweep' }) localservices.append(row) t1 = time.time() if t1 - t0 > 2: #this connection is taking too long break else: if options.verbose: print('Domain ', domain, ' is unavailable') return localservices
class Context: constants = { # constants in this dict are recognized in output bool: { True: (None, None, 'True', 'True', 'True'), False: (None, None, 'False', 'False', 'False') }, float: {}, complex: { complex(0, 1): (None, None, 'i', 'i', 'i') } } variables = {} # dict of variables to subsitute in expr functions = SortedDict( ) # only functions listed in this dict can be used in Expr # supported _operators with precedence and text + LaTeX repr # precedence as in https://docs.python.org/reference/expressions.html#operator-precedence # # table of allowed operators # note we very slightly prefer + over - and * over / for simpler expression generation operators = { ast.Or: (op.or_, 300, ' or ', ' or ', ' \\vee '), ast.And: (op.and_, 400, ' and ', ' and ', ' \\wedge '), ast.Not: (op.not_, 500, 'not ', 'not ', '\\neg'), ast.Eq: (op.eq, 600, '=', ' == ', ' = '), ast.Gt: (op.gt, 600, ' > ', ' > ', ' \\gtr '), ast.GtE: (op.ge, 600, ' >= ', ' >= ', ' \\gec '), ast.Lt: (op.lt, 600, ' < ', ' < ', ' \\ltr '), ast.LtE: (op.le, 600, ' <= ', ' <= ', ' \\leq '), ast.BitXor: (op.xor, 800, ' xor ', ' xor ', ' xor '), ast.LShift: (op.lshift, 1000, ' << ', ' << ', ' \\ll '), ast.RShift: (op.rshift, 1000, ' >> ', ' >> ', ' \\gg '), ast.Add: (op.add, 1100, '+', '+', '+'), ast.Sub: (op.sub, 1101, '-', '-', '-'), ast.Mult: (op.mul, 1200, '*', '*', ' \\cdot '), ast.Div: (op.truediv, 1201, '/', '/', '\\frac{%s}{%s}'), ast.FloorDiv: (op.floordiv, 1201, '//', '//', '\\left\\lfloor\\frac{%s}{%s}\\right\\rfloor'), ast.Mod: (op.mod, 1200, ' mod ', '%', ' \\bmod '), ast.Invert: (op.not_, 1300, '~', '~', '\\sim '), ast.UAdd: (op.pos, 1150, '+', '+', '+'), ast.USub: (op.neg, 1150, '-', '-', '-'), # returns an integer when result is integer ... ast.Pow: (math2.pow, 1400, '^', '**', '^'), # precedence of other types below ast.Call: (None, 9000), ast.Name: (None, 9000), ast.Num: (None, 9000), } def add_function(self, f, s=None, r=None, l=None): ''' add a function to those allowed in Expr. :param f: function :param s: string representation, should be formula-like :param r: repr representation, should be cut&pastable in a calculator, or in python ... :param l: LaTeX representation ''' self.functions[f.__name__] = (f, 9999, s, r or s, l) return self.functions[f.__name__] def add_constant(self, c, name, s=None, r=None, l=None): ''' add a constant to those recognized in Expr. :param c: constant :param s: string representation, should be formula-like :param r: repr representation, should be cut&pastable in a calculator, or in python ... :param l: LaTeX representation ''' self.constants[type(c)][c] = (None, None, s or name, r or name, l or '\\' + name) def add_module(self, module): for fname, f in module.__dict__.items(): if fname[0] == '_': continue if isinstance(f, collections.Callable): self.add_function(f) elif math2.is_number(f): self.add_constant(f, fname) def eval(self, node): '''safe eval of ast node : only functions and _operators listed above can be used :param node: ast.AST to evaluate :param ctx: dict of varname : value to substitute in node :return: number or expression string ''' if isinstance(node, ast.Num): # <number> return node.n elif isinstance(node, ast.Name): return self.variables.get(node.id, node.id) # return value or var elif isinstance(node, ast.Attribute): return getattr(self.variables, [node.value.id], node.attr) elif isinstance(node, ast.Tuple): return tuple(self.eval(e) for e in node.elts) elif isinstance(node, ast.Call): params = [self.eval(arg) for arg in node.args] if node.func.id not in self.functions: raise NameError('%s function not allowed' % node.func.id) f = self.functions[node.func.id][0] res = f(*params) # try to correct small error return math2.int_or_float(res, 0, 1e-12) elif isinstance(node, ast.BinOp): # <left> <operator> <right> op = self.operators[type(node.op)] left = self.eval(node.left) right = self.eval(node.right) if math2.is_number(left) and math2.is_number(right): res = op[0](left, right) # no correction here ! return res else: return "%s%s%s" % (left, op[_dialect_python], right) elif isinstance(node, ast.UnaryOp): # <operator> <operand> e.g., -1 right = self.eval(node.operand) return self.operators[type(node.op)][0](right) elif isinstance(node, ast.Compare): left = self.eval(node.left) for op, right in zip(node.ops, node.comparators): # TODO: find what to do when multiple items in list return self.operators[type(op)][0](left, self.eval(right)) elif isinstance(node, ast.NameConstant): return node.value else: logging.warning(ast.dump(node, False, False)) return self.eval(node.body) # last chance def __init__(self): self.add_module(math) self.add_function(abs, l='\\lvert{%s}\\rvert') self.add_function(math.fabs, l='\\lvert{%s}\\rvert') self.add_function(math.factorial, '%s!', 'fact', '%s!') self.add_function(math2.factorial2, '%s!', 'fact', '%s!!') self.add_function(math2.sqrt, l='\\sqrt{%s}') self.add_function(math.trunc, l='\\left\\lfloor{%s}\\right\\rfloor') self.add_function(math.floor, l='\\left\\lfloor{%s}\\right\\rfloor') self.add_function(math.ceil, l='\\left\\lceil{%s}\\right\\rceil') self.add_function(math.asin, l='\\arcsin') self.add_function(math.acos, l='\\arccos') self.add_function(math.atan, l='\\arctan') self.add_function(math.asinh, l='\\sinh^{-1}') self.add_function(math.acosh, l='\\cosh^{-1}') self.add_function(math.atanh, l='\\tanh^{-1}') self.add_function(math.log, l='\\ln') self.add_function(math.log1p, l='\\ln\\left(1-{%s}\\rvert)') self.add_function(math.log10, l='\\log_{10}') self.add_function(math.log2, l='\\log_2') self.add_function(math.gamma, l='\\Gamma') self.add_function(math.exp, l='e^{%s}') self.add_function(math.expm1, l='e^{%s}-1') self.add_function(math.lgamma, 'log(abs(gamma(%s)))', l='\\ln\\lvert\\Gamma\\left({%s}\\rvert)\\right)') self.add_function(math.degrees, l='%s\\cdot\\frac{360}{2\\pi}') self.add_function(math.radians, l='%s\\cdot\\frac{2\\pi}{360}')
class OrderBook(object): def __init__(self, log_enabled=False): self.by_price = SortedDict() self.by_order_id = {} self.best_bid_level = None self.best_ask_level = None self.log_enabled = log_enabled self.logger = logging.getLogger(__name__) def _add(self, e): if e["next_microtimestamp"] != '-infinity': if self.log_enabled: self.logger.debug('Add %s', event_log(e)) price_level = self.by_price.get(e["price"]) if price_level is None: price_level = PriceLevel(e["price"]) self.by_price[e["price"]] = price_level price_level.add(e) self.by_order_id[e["order_id"]] = e self.changed_price_levels[e["price"]] = price_level if e["side"] == 'b': if self.best_bid_level is None or\ e["price"] > self.best_bid_level.price: self.best_bid_level = price_level else: if self.best_ask_level is None or\ e["price"] < self.best_ask_level.price: self.best_ask_level = price_level if self.log_enabled: self.logger.debug('PL-add: %s', price_level_log(e["price"], price_level)) return True else: if self.log_enabled: self.logger.debug('Ign %s', event_log(e)) return False def _remove(self, order_id): old_event = self.by_order_id.pop(order_id, None) if old_event is not None: if self.log_enabled: self.logger.debug('Del %s', event_log(old_event)) old_price_level = self.by_price[old_event["price"]] old_price_level.remove(old_event) if self.log_enabled: self.logger.debug( 'PL-del: %s', price_level_log(old_event["price"], old_price_level)) self.changed_price_levels[old_event["price"]] = old_price_level if old_event["side"] == 'b' and\ self.best_bid_level == old_price_level and\ old_price_level.amount('b') == Decimal(0): self.best_bid_level = None new_best_bid_level = old_price_level i = self.by_price.bisect_left(new_best_bid_level.price) while i > 0: new_best_bid_level = self.by_price.values()[i - 1] if new_best_bid_level.amount('b') > Decimal(0): self.best_bid_level = new_best_bid_level break else: i = self.by_price.bisect_left(new_best_bid_level.price) elif old_event["side"] == 's' and\ self.best_ask_level == old_price_level and\ old_price_level.amount('s') == Decimal(0): self.best_ask_level = None i = self.by_price.bisect_right(old_price_level.price) while i < len(self.by_price): new_best_ask_level = self.by_price.values()[i] if new_best_ask_level.amount('s') > Decimal(0): self.best_ask_level = new_best_ask_level break else: i = self.by_price.bisect_right( new_best_ask_level.price) return old_event def _post_update(self): depth_changes = [] for price in self.changed_price_levels.keys(): price_level = self.changed_price_levels[price] if self.log_enabled: self.logger.debug('Changed PL: %s', price_level_log(price, price_level)) for side in ['b', 's']: amount = price_level.amount(side) if amount >= Decimal(0): # obanalytics.level2_depth_record depth_changes.append({ "price": price, "volume": amount, "side": side, "bps_level": None }) price_level.purge() if price_level.amount("s") is None and\ price_level.amount("b") is None: del self.by_price[price] return depth_changes def update(self, episode): if self.log_enabled: self.logger.debug('OB update %s starts', episode[0]["microtimestamp"]) episode = sorted(episode, key=lambda e: e["event_no"]) self.changed_price_levels = SortedDict() for e in episode: if e["event_no"] > 1: self._remove(e["order_id"]) if self.log_enabled: self.logger.debug(spread_log(self)) self._add(e) if self.log_enabled: self.logger.debug(spread_log(self)) depth_changes = self._post_update() if self.log_enabled: self.logger.debug('OB update %s ends', episode[0]["microtimestamp"]) return depth_changes def event(self, order_id): return self.by_order_id.get(order_id) def spread(self): spread = {} if self.best_bid_level is not None: spread["best_bid_price"] = self.best_bid_level.price spread["best_bid_qty"] = self.best_bid_level.amount('b') else: spread["best_bid_price"] = None spread["best_bid_qty"] = None if self.best_ask_level is not None: spread["best_ask_price"] = self.best_ask_level.price spread["best_ask_qty"] = self.best_ask_level.amount('s') else: spread["best_ask_price"] = None spread["best_ask_qty"] = None return spread def events(self, price): price_level = self.by_price.get(price) if price_level is not None: return price_level.events() else: return None def all_events(self): best_bid_price = None best_sell_price = None for price in self.by_price: for e in self.by_price[price].events(): if e["side"] == 'b': if best_bid_price is None or e["price"] > best_bid_price: best_bid_price = e["price"] if best_sell_price is None: e["is_maker"] = True e["is_crossed"] = False elif e["price"] <= best_sell_price: e["is_maker"] = True if e["price"] > best_sell_price: e["is_crossed"] = True else: e["is_crossed"] = False else: e["is_maker"] = False e["is_crossed"] = True else: if best_sell_price is None or e["price"] < best_sell_price: best_sell_price = e["price"] if best_bid_price is None: e["is_maker"] = True e["is_crossed"] = False elif e["price"] >= best_bid_price: e["is_maker"] = True if e["price"] < best_bid_price: e["is_crossed"] = True else: e["is_crossed"] = False else: e["is_maker"] = False e["is_crossed"] = True yield e
def __init__(self): self.sd = SortedDict( ) # maintain a sorted dictionary, key is time stamp, value is (sid, idd, start/end)
def algorithmSPT(aJobsList, machinesList): """ SPT/SJF heuristic algorithm for job shop problem """ time = {} # 记录某一时间各机器前的任务等待队列,相当于时间进度条,模拟时间流逝,推进排队 waitingOperations = {} currentTimeOnMachines = {} #当前机器时间,可以用来更新time jobsListToExport = [] # initialize machines times and get # first waiting operations for each machine # global machinesList, itinerariesList #TODO 修改以处理一个任务可选多台机器的问题 for machine in machinesList: currentTimeOnMachines[machine.name] = 0 #初始化各机器当前等待队列 for machine in machinesList: waitingOperations[machine.name] = [] for job in aJobsList: if job.idOperation == 1 and machine.name in job.machine: #找出当前任务可选机器中机器时间最小的机器 if len(job.machine) == 1: waitingOperations[machine.name].append(job) else: minTimeMachine = machine.name for mac in job.machine: if currentTimeOnMachines[mac] < currentTimeOnMachines[minTimeMachine]: minTimeMachine = mac.name if minTimeMachine == machine.name: waitingOperations[machine.name].append(job) waitingOperations[machine.name].sort(key=lambda j: j.duration) time[0] = waitingOperations for keyMach, operations in waitingOperations.items(): # for each waiting task in front of machine set time to 0, update # properties if len(operations): operations[0].startTime = 0 operations[0].completed = True operations[0].assignedMachine = keyMach # push task to production, and create new event to stop at, # on ending time, then update machines time jobsListToExport.append(operations[0]) currentTimeOnMachines[keyMach] = operations[0].getEndTime() time[currentTimeOnMachines[keyMach]] = {} while len(jobsListToExport) != len(aJobsList): for t, operations in time.items(): operations = getWaitingOperationsSPT(aJobsList, float(t), machinesList, currentTimeOnMachines) for keyMach, tasks in operations.items(): if len(tasks): if float(t) < currentTimeOnMachines[keyMach]: continue tasks[0].startTime = float(t) tasks[0].completed = True tasks[0].assignedMachine = keyMach jobsListToExport.append(tasks[0]) currentTimeOnMachines[keyMach] = tasks[0].getEndTime() time[currentTimeOnMachines[keyMach]] = {} del time[t] break time = SortedDict(time) # chronological order return jobsListToExport
def algorithmLIFO(aJobsList): """ LIFO/LCFS heuristic algorithm for job shop problem """ time = {} waitingOperations = {} recentOperations = {} currentTimeOnMachines = {} jobsListToExport = [] global machinesList for machine in machinesList: waitingOperations[machine.name] = [ job for job in aJobsList if job.machine == machine.name and job.idOperation == 1 ] currentTimeOnMachines[machine.name] = 0 time[0] = waitingOperations recentOperations = deepcopy(waitingOperations) for keyMach, operations in waitingOperations.items(): #for each waiting task in front of machine set time to 0, update #properties if len(operations): for job in aJobsList: if job == operations[0]: job.completed = True job.startTime = 0 job.itineraryEndTime = job.getEndTime() operations[0].startTime = 0 operations[0].completed = True #push task to production, and create new event to stop at, #on ending time, then update machines time jobsListToExport.append(operations[0]) currentTimeOnMachines[keyMach] = operations[0].getEndTime() del recentOperations[keyMach][0] time[currentTimeOnMachines[keyMach]] = {} while len(jobsListToExport) != len(aJobsList): for t, operations in time.items(): operations = deepcopy( getWaitingOperationsLIFO(aJobsList, float(t), recentOperations)) for keyMach, tasks in operations.items(): if len(tasks): if float(t) < currentTimeOnMachines[tasks[0].machine]: continue for job in aJobsList: if job == tasks[0]: job.completed = True job.startTime = float(t) job.itineraryEndTime = job.getEndTime() tasks[0].startTime = float(t) tasks[0].completed = True jobsListToExport.append(tasks[0]) del recentOperations[keyMach][0] currentTimeOnMachines[keyMach] = tasks[0].getEndTime() time[currentTimeOnMachines[keyMach]] = {} del time[t] break time = SortedDict(time) #chronological order return jobsListToExport
def __init__(self, symbol: str): self.asks = SortedDict() self.bids = SortedDict()
class Book: def __init__(self, symbol: str): self.asks = SortedDict() self.bids = SortedDict() def update(self, *entries: Iterable[dict]) -> None: for entry in entries: if entry['side'] == 'BID': self.bids[entry['price']] = entry else: self.asks[entry['price']] = entry def remove(self, side: str, price: float) -> None: if side == 'BID': self.bids.pop(price, None) else: self.asks.pop(price, None) def get_bids(self, size=-1) -> List[dict]: if size == -1 or size >= len(self.bids): return list(map(add_key, enumerate(reversed(self.bids.values())))) result = list(self.bids.values())[len(self.bids) - size:] result.reverse() return list(map(add_key, enumerate(result))) def get_asks(self, size=-1) -> List[dict]: if size == -1 or size >= len(self.asks): return list(map(add_key, enumerate(self.asks.values()))) return list(map(add_key, enumerate(self.asks.values()[:size]))) def clear(self) -> None: self.asks.clear() self.bids.clear()
class TimingDiagram: """Two-state (True/False or 1/0) timing diagram with boolean algebra operations.""" def __init__(self, time_state_pairs): """Creates a timing diagram out of a series of (time, state) pairs. Notes ===== The input states can be any truthy/falsey values. The input times can be any type with a partial ordering. The input sequence does not need to be sorted (input is sorted during initialization). Compresses duplicate sequential states and stores them in the `timeline` attribute. Example ======= >>> diagram = TimingDiagram([(0, True), (1, False), (5, False), (10, True)]) >>> print(~diagram) TimingDiagram([(0, False), (1, True), (10, False)]) """ self.timeline = SortedDict( _compress(time_state_pairs, key=operator.itemgetter(1)) ) def __getitem__(self, item): return self.timeline[item] def __matmul__(self, time): """Alias for at()""" return self.at(time) def __eq__(self, other): """Returns a new timing diagram, True where the two diagrams are equal.""" return self.compare(other, key=operator.eq) def __ne__(self, other): """Returns a new timing diagram, True where the two diagrams are equal.""" return ~(self == other) def __and__(self, other): """Returns a new timing diagram, True where the two diagrams are both True.""" return self.compare(other, key=operator.and_) def __or__(self, other): """Returns a new timing diagram, True where either diagram is True.""" return self.compare(other, key=operator.or_) def __xor__(self, other): """Returns a new timing diagram, True where the two diagrams are not equal.""" return self != other def __invert__(self): """Returns a new timing diagram with states flipped.""" return TimingDiagram(((t, not s) for t, s in self.timeline.items())) def at(self, time): """Returns the state at a particular time. Uses bisection for search (binary search).""" idx = max(0, self.timeline.bisect(time) - 1) return self.timeline.values()[idx] def compare(self, other, key): """Constructs a new timing diagram based on comparisons between two diagrams, with (time, key(self[time], other[time])) for each time in the timelines. """ # TODO: Implement linear algorithm instead of .at() for each time, which is O(n log n). return TimingDiagram( ( (k, key(self.at(k), other.at(k))) for k in merge(self.timeline.keys(), other.timeline.keys()) ) ) def __repr__(self): return f"{self.__class__.__qualname__}({list(self.timeline.items())})"
def recheduleChangePriority(jobsListExportPrevious, rescheduleTime, priorItinerary, machinesList): """ :param jobsListExportPrevious: 初始调度结果 :param rescheduleTime: 重调度时间 :param priorItinerary: 优先的任务序号 :param machinesList: 可用机器列表 :return: jobsListToExportNew: 重调度方案 """ time = {} allPreviousOperations = {} rescheduleOperations = {} rescheduleJobsList = [] currentTimeOnMachines = {} jobsListToExportNew = [] unchangedOperations = {} # 遍历机器,生成每个机器前工序集合和需要重调度的工序集合 for machine in machinesList: allPreviousOperations[machine.name] = [ job for job in jobsListExportPrevious if job.assignedMachine == machine.name ] allPreviousOperations[machine.name].sort(key=lambda j: j.startTime) rescheduleOperations[machine.name] = [ job for job in jobsListExportPrevious if job.assignedMachine == machine.name and job.startTime >= rescheduleTime ] rescheduleOperations[machine.name].sort(key=lambda j: j.startTime) # 重调度时刻机器时间更新 unchangedLength = len(allPreviousOperations[machine.name]) - len( rescheduleOperations[machine.name]) unchangedOperations[machine.name] = allPreviousOperations[ machine.name][0:unchangedLength] currentTimeOnMachines[machine.name] = unchangedOperations[ machine.name][-1].endTime if currentTimeOnMachines[machine.name] < rescheduleTime: currentTimeOnMachines[machine.name] = rescheduleTime # 初始化各机器时间 time[currentTimeOnMachines[machine.name]] = {} # 更新各机器下重调度工序状态 unchangedOperations[machine.name][-1].completed = True for job in rescheduleOperations[machine.name]: job.startTime = 0 job.completed = False # 输出无需重调度的任务结果 for job in unchangedOperations[machine.name]: jobsListToExportNew.append(job) for machine in machinesList: # 生成重调度列表 for job in rescheduleOperations[machine.name]: if job.idItinerary == priorItinerary: job.priority = 5 rescheduleJobsList.append(job) allJobsList = jobsListToExportNew + rescheduleJobsList # 调度时间初始化 time = SortedDict(time) while len(jobsListToExportNew) < len(jobsListExportPrevious): for t, operations in time.items(): operations = GetWaitingOperationsSPT(allJobsList, float(t), machinesList, currentTimeOnMachines) for keyMach, tasks in operations.items(): if len(tasks): if float(t) < currentTimeOnMachines[keyMach]: continue tasks[0].startTime = float(t) tasks[0].completed = True tasks[0].assignedMachine = keyMach jobsListToExportNew.append(tasks[0]) currentTimeOnMachines[keyMach] = tasks[0].getEndTime() time[currentTimeOnMachines[keyMach]] = {} del time[t] break time = SortedDict(time) # chronological order return jobsListToExportNew
def recheduleMachineFault(jobsListExportPrevious, rescheduleTime, faultyMachine, machinesList): pass #TODO time = {} allPreviousOperations = {} rescheduleOperations = {} rescheduleJobsList = [] currentTimeOnMachines = {} jobsListToExportNew = [] unchangedOperations = {} unscheduleItinerarys = [] # 遍历机器,生成每个机器前工序集合和需要重调度的工序集合 for machine in machinesList: allPreviousOperations[machine.name] = [ job for job in jobsListExportPrevious if job.assignedMachine == machine.name ] allPreviousOperations[machine.name].sort(key=lambda j: j.startTime) rescheduleOperations[machine.name] = [ job for job in jobsListExportPrevious if job.assignedMachine == machine.name and job.startTime >= rescheduleTime ] rescheduleOperations[machine.name].sort(key=lambda j: j.startTime) # 重调度时刻机器时间更新 unchangedLength = len(allPreviousOperations[machine.name]) - len( rescheduleOperations[machine.name]) unchangedOperations[machine.name] = allPreviousOperations[ machine.name][0:unchangedLength] #如果故障机器上有未完工任务,则把该任务工艺路线后续任务不参与重调度 currentTimeOnMachines[machine.name] = unchangedOperations[ machine.name][-1].endTime if currentTimeOnMachines[machine.name] < rescheduleTime: currentTimeOnMachines[machine.name] = rescheduleTime # 初始化各机器时间 time[currentTimeOnMachines[machine.name]] = {} # 更新各机器下重调度工序状态 unchangedOperations[machine.name][-1].completed = True for job in rescheduleOperations[machine.name]: job.completed = False # 输出无需重调度的任务结果 for job in unchangedOperations[machine.name]: jobsListToExportNew.append(job) # 1.先识别出故障机器前未完工任务(包括正在加工的) # 2. 遍历上述任务列表: # 如果任务可选机器列表长度为1,则重调度任务列表中不加入该任务; # 并且记录该任务的工艺路线号,后续是该工艺路线号的任务不参与调度; # 否则将任务加入重调度任务列表 if unchangedOperations[faultyMachine][-1].endTime > rescheduleTime: unscheduleItinerarys.append( unchangedOperations[faultyMachine][-1].idItinerary) # 生成重调度初始列表 for machine in machinesList: for job in rescheduleOperations[machine.name]: rescheduleJobsList.append(job) rescheduleJobsList.sort(key=lambda j: j.startTime) #得到受影响的工艺路线号 for job in rescheduleOperations[faultyMachine]: if len(job.machine) == 1: unscheduleItinerarys.append(job.idItinerary) #去除受机器故障影响的任务 rescheduleJobsListUpdate = [] for job in rescheduleJobsList: if job.idItinerary in unscheduleItinerarys: continue else: rescheduleJobsListUpdate.append(job) # 机器列表更新 machinesAvailableList = [ machine for machine in machinesList if machine.name != faultyMachine ] allJobsList = jobsListToExportNew + rescheduleJobsListUpdate # 调度时间初始化 time = SortedDict(time) while len(jobsListToExportNew) < len(allJobsList): for t, operations in time.items(): operations = GetWaitingOperationsSPT(allJobsList, float(t), machinesAvailableList, currentTimeOnMachines, faultyMachine) for keyMach, tasks in operations.items(): if len(tasks): if float(t) < currentTimeOnMachines[keyMach]: continue tasks[0].startTime = float(t) tasks[0].completed = True tasks[0].assignedMachine = keyMach jobsListToExportNew.append(tasks[0]) currentTimeOnMachines[keyMach] = tasks[0].getEndTime() time[currentTimeOnMachines[keyMach]] = {} del time[t] break time = SortedDict(time) # chronological order return jobsListToExportNew
def algorithmLPT(aJobsList): """ LPT/LJF heuristic algorithm for job shop problem """ time = {} waitingOperations = {} currentTimeOnMachines = {} jobsListToExport = [] #initialize machines times and get first #waiting operations for each machine global machinesList for machine in machinesList: waitingOperations[machine.name] = [ job for job in aJobsList if job.machine == machine.name and job.idOperation == 1 ] waitingOperations[machine.name].sort(key=lambda j: j.duration, reverse=True) currentTimeOnMachines[machine.name] = 0 time[0] = waitingOperations for keyMach, operations in waitingOperations.items(): #for each waiting task in front of machine set time to 0, update #properties if len(operations): operations[0].startTime = 0 operations[0].completed = True #push task to production, and create new event to stop at, at #on ending time, then update machines time jobsListToExport.append(operations[0]) currentTimeOnMachines[keyMach] = operations[0].getEndTime() time[currentTimeOnMachines[keyMach]] = {} while len(jobsListToExport) != len(aJobsList): for t, operations in time.items(): operations = getWaitingOperationsLPT(aJobsList, float(t)) for keyMach, tasks in operations.items(): if len(tasks): if float(t) < currentTimeOnMachines[tasks[0].machine]: continue tasks[0].startTime = float(t) tasks[0].completed = True jobsListToExport.append(tasks[0]) currentTimeOnMachines[keyMach] = tasks[0].getEndTime() time[currentTimeOnMachines[keyMach]] = {} del time[t] break time = SortedDict(time) #chronological order return jobsListToExport
def getFaceWorkers(asDict=False): # Scan for faro workers import_dir = faro.__path__[0] scripts = os.listdir(os.path.join(import_dir, 'face_workers')) scripts = filter(lambda x: x.endswith('FaceWorker.py'), scripts) sys.path.append(os.path.join(import_dir, 'face_workers')) scripts = list(scripts) script_locations = [import_dir] * len(scripts) scripts.sort() FACE_WORKER_LIST = SortedDict() # Scan for other workers #TODO make the services path an env, not hard coded SERVICE_DIRS = [] if os.getenv('FARO') is not None: SERVICE_DIRS.append(os.path.join(os.getenv('FARO'), 'services')) print("service dirs:", SERVICE_DIRS) if 'FARO_WORKER_PATH' in os.environ: worker_dirs = os.environ['FARO_WORKER_PATH'].split(":") for worker_dir in worker_dirs: print('worker_dir:', worker_dirs) # import_dir = faro.__path__[0] try: worker_scripts = os.listdir(worker_dir) except: print("ERROR - Could not read directory in FARO_WORKER_PATH:", worker_dir) raise worker_scripts = list( filter(lambda x: x.endswith('FaceWorker.py'), worker_scripts)) sys.path.append(worker_dir) scripts += list(worker_scripts) script_locations += [worker_dir] * len(list(worker_scripts)) scripts.sort() tablerows = [] availableServices = {} for sdir in SERVICE_DIRS: if os.path.exists(sdir) and os.path.isdir(sdir): availableServices.update( {d: os.path.join(sdir, d) for d in os.listdir(sdir)}) for each, loc in zip(scripts, script_locations): name = each[:-13].lower() loadable = True # Check if the given FaceWorker has an associated service environment serviceLocation = None serviceLoadType = [] loc2 = loc if name in availableServices: serviceLocation = availableServices[name] serviceFiles = os.listdir(serviceLocation) loc2 = serviceLocation if "Dockerfile" in serviceFiles: serviceLoadType.append("Docker") if "environment.yml" in serviceFiles: serviceLoadType.append("Conda") if "requirements.txt" in serviceFiles: serviceLoadType.append("venv") if len(serviceLoadType) == 0: serviceLoadType.append('native') row = SortedDict({ "Algorithm": name, 'location': loc2, 'service files': serviceLocation, 'environment Type': serviceLoadType }) try: module = importlib.import_module(each[:-3]) class_obj = getattr(module, each[:-3]) # print(" Loaded: ", name, '-', class_obj) FACE_WORKER_LIST[name] = [class_obj, None, None] row['gallery capabilities'] = False if 'getOptionsGroup' in dir(module): FACE_WORKER_LIST[name][1] = module.getOptionsGroup if 'getGalleryWorker' in dir(module): FACE_WORKER_LIST[name][2] = module.getGalleryWorker row['gallery capabilities'] = True row['natively loadable'] = True except Exception as e: row['natively loadable'] = False row['error'] = e tablerows.append(row) if asDict: return {s['Algorithm']: s for s in tablerows} return tablerows
def randomSolutionByPriority(aJobsList): """ Choose jobs in random order for job shop problem (worst case scenario) """ time = {} waitingOperations = {} currentTimeOnMachines = {} jobsListToExport = [] #initialize machines times and get first #waiting operations for each machine global machinesList for machine in machinesList: waitingOperations[machine.name] = [ job for job in aJobsList if job.machine == machine.name and job.idOperation == 1 ] currentTimeOnMachines[machine.name] = 0 time[0] = waitingOperations for keyMach, operations in waitingOperations.items(): #for each waiting task in front of machine set time to 0, #update properties if len(operations): #r = choice(len(operations),1,p=[j.priority for j in operations]) r = random.choices(range(0, len(operations)), weights=[j.priority for j in operations]) operations[r[0]].startTime = 0 operations[r[0]].completed = True #push task to production, and create new event to stop at, #on ending time, then update machines time jobsListToExport.append(operations[r[0]]) currentTimeOnMachines[keyMach] = operations[r[0]].getEndTime() time[currentTimeOnMachines[keyMach]] = {} while len(jobsListToExport) != len(aJobsList): for t, operations in time.items(): #doesnt really matter the order if you choose random operation from it operations = getWaitingOperationsLPT(aJobsList, float(t)) for keyMach, tasks in operations.items(): if len(tasks): #if more than 1 operation in queue, choose the random one r = random.choices(range(0, len(tasks)), weights=[j.priority for j in tasks]) if float(t) < currentTimeOnMachines[tasks[r[0]].machine]: continue tasks[r[0]].startTime = float(t) tasks[r[0]].completed = True jobsListToExport.append(tasks[r[0]]) currentTimeOnMachines[keyMach] = tasks[r[0]].getEndTime() time[currentTimeOnMachines[keyMach]] = {} del time[t] break time = SortedDict(time) #chronological order return jobsListToExport
def __init__(self): self.children = SortedDict() self.leaf = True