コード例 #1
0
class PRLinkIntegration(Integration):
    def _process(self):
        self._prlinks = OrderedSet()
        for match in itertools.chain(
                re.finditer(REGEX_TEMPLATE.format(PATH_REGEX), self._message),
                re.finditer(SHORT_REGEX_TEMPLATE.format(PATH_REGEX),
                            self._message),
        ):
            self._prlinks.add(PrLink(match.group("path")))

    @property
    def message(self):
        out = self._message
        for prlink in self._prlinks:
            out = re.sub(
                REGEX_TEMPLATE.format(prlink.path),
                "pr/{}".format(prlink.path),
                out,
            )
            out = re.sub(
                SHORT_REGEX_TEMPLATE.format(prlink.path),
                r"<https://github.com/Cal-CS-61A-Staff/berkeley-cs61a/pull/{}|pr/{}>"
                .format(prlink.path, prlink.path),
                out,
            )
        return out
コード例 #2
0
    def __init__(self, idx, tasks, task_duration, wall_time, np_random, cpu,
                 mem):
        self.idx = idx
        self.tasks = tasks
        self.wall_time = wall_time
        self.np_random = np_random

        self.task_duration = task_duration
        self.cpu = cpu
        self.mem = mem
        self.num_tasks = len(tasks)
        self.num_finished_tasks = 0
        # self.next_task_idx = 0
        self.remain_tasks = set(list(range(len(tasks))))

        self.no_more_tasks = False
        self.tasks_all_done = False
        self.node_finish_time = np.inf

        self.executors = OrderedSet()

        # uninitialized
        self.parent_nodes = []
        self.child_nodes = []
        self.descendant_nodes = []
        self.job_dag = None

        self.assign_node_to_tasks()
コード例 #3
0
 def _process(self):
     self._issues = OrderedSet()
     for match in itertools.chain(
         re.finditer(REGEX_TEMPLATE.format(PATH_REGEX), self._message),
         re.finditer(SHORT_REGEX_TEMPLATE.format(PATH_REGEX), self._message),
     ):
         self._issues.add(Issue(match.group("path")))
コード例 #4
0
def mark_hubs_after_initial_secants(builder, original_schemas, schemas,
                                    new_schemas, classes, named_lookups,
                                    add_rule):
    lookup = Lookup(
        'dist',
        'dupl',
        'dflt',
        mark_filtering_set='all',
        reversed=True,
    )
    hubs = OrderedSet()
    for schema in new_schemas:
        if isinstance(schema.path, Hub) and not schema.path.initial_secant:
            hubs.add(schema)
            classes['all'].append(schema)
        elif isinstance(
                schema.path, Line
        ) and schema.path.secant and schema.glyph_class == GlyphClass.JOINER:
            classes['secant'].append(schema)
    for hub in hubs:
        initial_secant_hub = hub.clone(path=hub.path.clone(
            initial_secant=True))
        classes[phases.HUB_CLASS].append(initial_secant_hub)
        classes[phases.CONTINUING_OVERLAP_OR_HUB_CLASS].append(
            initial_secant_hub)
        add_rule(lookup, Rule(
            ['secant'],
            [hub],
            [],
            [initial_secant_hub],
        ))
    return [lookup]
コード例 #5
0
class GoLinkIntegration(Integration):
    def _process(self):
        self._golinks = OrderedSet()
        for match in itertools.chain(
                re.finditer(REGEX_TEMPLATE.format(PATH_REGEX), self._message),
                re.finditer(SHORT_REGEX_TEMPLATE.format(PATH_REGEX),
                            self._message),
        ):
            self._golinks.add(GoLink(match.group("path")))

    @property
    def message(self):
        out = self._message
        for golink in self._golinks:
            out = re.sub(
                REGEX_TEMPLATE.format(golink.path),
                "go/{}".format(golink.path),
                out,
            )
            out = re.sub(
                SHORT_REGEX_TEMPLATE.format(golink.path),
                r"<https://go.cs61a.org/{}|go/{}>".format(
                    golink.path, golink.path),
                out,
            )
        return out
コード例 #6
0
ファイル: database.py プロジェクト: YukSeungChan/palvin
    def __tablename__(cls):
        global exclude_modules, inflect_engine

        modules = cls.__module__.split('.')
        modules[-1] = inflect_engine.plural(
            camel_case_to_lower_case_underscore(cls.__name__))
        return '_'.join(
            list(OrderedSet(modules) - OrderedSet(exclude_modules)))
コード例 #7
0
def compute_path(grid,start,goal,cost,heuristic):
   
    # Use the OrderedSet for your closed list
    closed_set = OrderedSet()
    
    # Use thePriorityQueue for the open list
    open_set = PriorityQueue(order=min, f=lambda v: v.f)      

    # Keep track of the parent of each node. Since the car can take 4 distinct orientations, 
    # for each orientation we can store a 2D array indicating the grid cells. 
    # E.g. parent[0][2][3] will denote the parent when the car is at (2,3) facing up    
    parent = [[[' ' for row in range(len(grid[0]))] for col in range(len(grid))], 
             [[' ' for row in range(len(grid[0]))] for col in range(len(grid))],
             [[' ' for row in range(len(grid[0]))] for col in range(len(grid))],
             [[' ' for row in range(len(grid[0]))] for col in range(len(grid))]]

    # The path of the car
    path =[['-' for row in range(len(grid[0]))] for col in range(len(grid))]
    
    x = start[0] 		
    y = start[1]
    theta = start[2]   
    h = heuristic[x][y]
    g = 0
    f = g+h
    open_set.put(start, Value(f=f,g=g))

    # your code: implement A*
    # Initially you may want to ignore theta, that is, plan in 2D.
    # To do so, set actions=forward, cost = [1, 1, 1, 1], and action_name = ['U', 'L', 'R', 'D']
    # Similarly, set parent=[[' ' for row in range(len(grid[0]))] for col in range(len(grid))]
    while open_set:
        node = open_set.pop()
        closed_set.add(node[0])
        if node[0] == goal:
            break
      
#finding child of node
#write function for finding child node
#setting the cost values for neighboring nodes
        neighbors = get_neighbors(node[0])
        print(neighbors)
        for i in neighbors:
            x = i[0] 
            y = i[1]
            theta = i[2]
            h = heuristic[x][y]
            g = node[1].g + costfunction(node[0],i)
            f = g+h
            if i not in open_set or i not in closed_set:
                open_set.put(i,Value(f,g))
            elif i in open_set and f < open_set.get(i).f:
                open_set.put(i,Value(f,g))
                
        
        return path, closed_set
コード例 #8
0
def get_neighbors(grid, current_cell):
    neighbors = OrderedSet()
    x, y = current_cell
    for move in delta:
        x_new = x + move[0]
        y_new = y + move[1]
        if len(grid) > x_new >= 0 and len(grid[0]) > y_new >= 0:
            if not grid[x_new][y_new]:
                neighbors.add((x_new, y_new))
    return neighbors
コード例 #9
0
 def reset(self):
     for node in self.nodes:
         node.reset()
     self.num_nodes_done = 0
     self.executors = OrderedSet()
     self.frontier_nodes = OrderedSet()
     for node in self.nodes:
         if node.is_schedulable():
             self.frontier_nodes.add(node)
     self.arrived = False
     self.completed = False
     self.completion_time = np.inf
コード例 #10
0
def compute_value(grid, goal, cost):
    value = get_init_value_grid(grid, goal, MAX_COST)
    policy = get_init_policy(grid, goal)
    change = OrderedSet()
    change |= (get_neighbors(grid, goal))
    while change:
        current_cell = change.pop()
        stochastic_cost, optimal_direction = get_min_cost(
            grid, value, current_cell)
        if stochastic_cost + cost < value[current_cell[0]][current_cell[1]]:
            value[current_cell[0]][current_cell[1]] = stochastic_cost + cost
            policy[current_cell[0]][current_cell[1]] = optimal_direction
            neighbors = get_neighbors(grid, current_cell)
            change |= (neighbors)
    return value, policy
コード例 #11
0
def compute_path(grid,start,goal,cost,heuristic):
   
    # Use the OrderedSet for your closed list
    closed_set = OrderedSet()
    
    # Use thePriorityQueue for the open list
    open_set = PriorityQueue(order=min, f=lambda v: v.f)      

    # Keep track of the parent of each node. Since the car can take 4 distinct orientations, 
    # for each orientation we can store a 2D array indicating the grid cells. 
    # E.g. parent[0][2][3] will denote the parent when the car is at (2,3) facing up    
    parent = [[[' ' for row in range(len(grid[0]))] for col in range(len(grid))], 
             [[' ' for row in range(len(grid[0]))] for col in range(len(grid))],
             [[' ' for row in range(len(grid[0]))] for col in range(len(grid))],
             [[' ' for row in range(len(grid[0]))] for col in range(len(grid))]]

    # The path of the car
    path =[['-' for row in range(len(grid[0]))] for col in range(len(grid))]
    
    x = start[0]
    y = start[1]
    theta = start[2]
    h = heuristic[x][y]
    g = 0
    f = g+h
    open_set.put(start, Value(f=f,g=g))

    # your code: implement A*

    # Initially you may want to ignore theta, that is, plan in 2D.
    # To do so, set actions=forward, cost = [1, 1, 1, 1], and action_name = ['U', 'L', 'R', 'D']
    # Similarly, set parent=[[' ' for row in range(len(grid[0]))] for col in range(len(grid))]

    return path, closed_set
コード例 #12
0
ファイル: page.py プロジェクト: brettgoss/datazoomer
    def __init__(self, content='', callback=None, css=''):

        self.content = tools.load_content(content) or content
        self.template = system.default_template
        self.callback = callback
        self.css = css
        self.theme = None
        self.js = ''
        self.head = ''
        self.tail = ''
        self.title = ''
        self.subtitle = ''
        self.search = None
        self.actions = None
        self.libs = OrderedSet()
        self.styles = OrderedSet()
コード例 #13
0
def compute_value(grid, goal, cost):
    value = get_init_value_grid(grid, goal, 99)
    policy = get_init_policy(grid, goal)
    change = OrderedSet()
    change |= (get_neighbors(grid, goal))
    while change:
        current_cell = change.pop(last=False)
        min_cost, min_direction = get_min_cost(grid, value, current_cell)
        if min_cost + cost < value[current_cell[0]][current_cell[1]]:
            value[current_cell[0]][current_cell[1]] = min_cost + cost
            policy[current_cell[0]][current_cell[1]] = min_direction
            neighbors = get_neighbors(grid, current_cell)
            change |= (neighbors)
    for row in policy: print(row)
    for row in value: print(row)
    return value
    
コード例 #14
0
    def __init__(self, nodes, adj_mat, name):
        # nodes: list of N nodes
        # adj_mat: N by N 0-1 adjacency matrix, e_ij = 1 -> edge from i to j
        assert len(nodes) == adj_mat.shape[0]
        assert adj_mat.shape[0] == adj_mat.shape[1]

        self.name = name

        self.nodes = nodes
        self.adj_mat = adj_mat

        self.num_nodes = len(self.nodes)
        self.num_nodes_done = 0

        # set of executors currently running on the job
        self.executors = OrderedSet()

        # the computation graph needs to be a DAG
        assert is_dag(self.num_nodes, self.adj_mat)

        # get the set of schedule nodes
        self.frontier_nodes = OrderedSet()
        for node in self.nodes:
            if node.is_schedulable():
                self.frontier_nodes.add(node)

        # assign job dag to node
        self.assign_job_dag_to_node()

        # dag is arrived
        self.arrived = False

        # dag is completed
        self.completed = False

        # dag start ime
        self.start_time = None

        # dag completion time
        self.completion_time = np.inf

        # map a executor number to an interval
        self.executor_interval_map = \
            self.get_executor_interval_map()
コード例 #15
0
def compute_path(grid, start, goal, cost, heuristic):
    # Use the OrderedSet for your closed list
    closed_set = OrderedSet()
    # Use thePriorityQueue for the open list
    open_set = PriorityQueue(order=min, f=lambda v: v.f)

    parent = [[[' ' for row in range(len(grid[0]))]
               for col in range(len(grid))],
              [[' ' for row in range(len(grid[0]))]
               for col in range(len(grid))],
              [[' ' for row in range(len(grid[0]))]
               for col in range(len(grid))],
              [[' ' for row in range(len(grid[0]))]
               for col in range(len(grid))]]

    # The path of the car
    path = [['-' for row in range(len(grid[0]))] for col in range(len(grid))]

    x = start[0]
    y = start[1]
    theta = start[2]
    g_value = 0
    h_value = heuristic[x][y]
    f = g_value + h_value
    parent_cost = [[[1000000000 for row in range(len(grid[0]))]
                    for col in range(len(grid))],
                   [[1000000000 for row in range(len(grid[0]))]
                    for col in range(len(grid))],
                   [[1000000000 for row in range(len(grid[0]))]
                    for col in range(len(grid))],
                   [[1000000000 for row in range(len(grid[0]))]
                    for col in range(len(grid))]]
    open_set.put(start, Value(f=f, g=g_value))

    ########################### IMPLEMENTATION OF DIJKSTRA ALGORITHM ###################################
    ####################################################################################################
    ####################################################################################################
    def dijkstra(grid, source):
        length = len(grid)
        Q_value = [(0, source)]
        value0 = [inf for i in range(length)]
        value0[source] = 0

        while len(Q) != 0:
            (cost, u) = hq.heappop(Q_value)

            for v in range(n):
                if value0[v] > value0[u] + grid[u][v]:
                    value0[v] = value0[u] + grid[u][v]
                    hq.heappush(Q, (value0[v], v))

        return value0
        print('The dijkstra value is:', value0)

    return path, closed_set
コード例 #16
0
    def _process(self):
        course_id = piazza_course_id(course=self._course)

        self._posts = OrderedSet()
        for match in itertools.chain(
                re.finditer(SHORT_REGEX, self._message),
                re.finditer(LONG_REGEX.format(course_id), self._message),
        ):
            cid = int(match.group("cid"))
            fid_str = match.group("fid")
            full_cid = match.group("cid") + ("_f{}".format(fid_str)
                                             if fid_str else "")
            post = perform_piazza_action(
                action="get_post",
                course=self._course,
                as_staff=True,
                kwargs=dict(cid=cid),
            )
            subject = post["history"][0]["subject"]
            content = post["history"][0]["content"]

            if fid_str:
                fid = int(fid_str)  # 1 indexed
                curr_id = 0
                for child in post["children"]:
                    if child["type"] != "followup":
                        continue
                    curr_id += 1
                    if fid == curr_id:
                        break
                else:
                    return
                content = child["subject"]

            subject = unescape(subject)
            content = unescape(re.sub("<[^<]+?>", "", content))
            url = "https://piazza.com/class/{}?cid={}".format(
                course_id, full_cid)

            self._posts.add(Post(subject, content, url, full_cid))
コード例 #17
0
    def _process(self):
        course_id = ed_course_id(course=self._course)
        ed_posts = perform_ed_action(
            action="get_feed",
            course=self._course,
            as_staff=True,
            kwargs=dict(limit=999999),
        )["threads"]

        self._posts = OrderedSet()
        for match in itertools.chain(
            re.finditer(SHORT_REGEX, self._message),
            re.finditer(LONG_REGEX.format(course_id), self._message),
        ):
            num = int(match.group("num"))
            pid = int(match.group("pid"))
            post = None
            if pid:
                for p in ed_posts:
                    if p.get("id", 0) == pid:
                        post = p
                        break
            elif num:
                for p in ed_posts:
                    if p.get("number", 0) == num:
                        post = p
                        break
            if not post:
                continue
            subject = post["title"]
            content = post["document"]

            subject = unescape(subject)
            content = unescape(re.sub("<[^<]+?>", "", content))
            url = "https://edstem.org/us/courses/{}/discussion/{}".format(
                course_id, post["id"]
            )

            self._posts.add(Post(subject, content, url, post["number"], post["id"]))
コード例 #18
0
class IssueIntegration(Integration):
    def _process(self):
        self._issues = OrderedSet()
        for match in itertools.chain(
            re.finditer(REGEX_TEMPLATE.format(PATH_REGEX), self._message),
            re.finditer(SHORT_REGEX_TEMPLATE.format(PATH_REGEX), self._message),
        ):
            self._issues.add(Issue(match.group("path")))

    @property
    def message(self):
        out = self._message
        for issue in self._issues:
            out = re.sub(
                REGEX_TEMPLATE.format(issue.path), "is/{}".format(issue.path), out
            )
            out = re.sub(
                SHORT_REGEX_TEMPLATE.format(issue.path),
                r"<https://github.com/Cal-CS-61A-Staff/berkeley-cs61a/issues/{}|is/{}>".format(
                    issue.path, issue.path
                ),
                out,
            )
        return out
コード例 #19
0
    def get_msg_path(self, job_dags):
        if len(self.job_dags) != len(job_dags):
            job_dags_changed = True
        else:
            job_dags_changed = not(all(i is j for \
                (i, j) in zip(self.job_dags, job_dags)))

        if job_dags_changed:
            self.msg_mats, self.msg_masks = get_msg_path(job_dags)
            self.dag_summ_backward_map = get_dag_summ_backward_map(job_dags)
            self.running_dag_mat = get_running_dag_mat(job_dags)
            self.job_dags = OrderedSet(job_dags)

        return self.msg_mats, self.msg_masks, \
               self.dag_summ_backward_map, self.running_dag_mat, \
               job_dags_changed
コード例 #20
0
ファイル: altervista.py プロジェクト: Rdbaker/AI-Final-Proj
def canon(word):
    """Given a word, returns a list of synonyms in order of relevance"""
    url = URI.format(word=word)
    r = get(url)
    try:
        # get the word lists from the response
        word_lists = [
            lst['list']['synonyms'].split('|') for lst in r.json()['response']
        ]
        # concatenate the word lists and eliminate duplicates
        return list(
            OrderedSet([
                item for sublist in word_lists for item in sublist
                if '(antonym)' not in item
            ]))
    except:
        return []
コード例 #21
0
ファイル: _new.py プロジェクト: Naraharirahul/MotionPlanning
def compute_path(grid,start,goal,cost,heuristic):
   
    # Use the OrderedSet for your closed list
    closed_set = OrderedSet()
    
    # Use thePriorityQueue for the open list
    open_set = PriorityQueue(order=min, f=lambda v: v.f)      

    # Keep track of the parent of each node. Since the car can take 4 distinct orientations, 
    # for each orientation we can store a 2D array indicating the grid cells. 
    # E.g. parent[0][2][3] will denote the parent when the car is at (2,3) facing up    
    parent = [[[' ' for row in range(len(grid[0]))] for col in range(len(grid))], 
             [[' ' for row in range(len(grid[0]))] for col in range(len(grid))],
             [[' ' for row in range(len(grid[0]))] for col in range(len(grid))],
             [[' ' for row in range(len(grid[0]))] for col in range(len(grid))]]

    # The path of the car
    path =[['-' for row in range(len(grid[0]))] for col in range(len(grid))]
    dist =[]
    P =[]
    x = start[0]
    y = start[1]
    theta = start[2]
    h = heuristic[x][y]
    g = 0
    f = g+h
    open_set.put(start, Value(f=f,g=g))
    for i in range(0,6):
        for j in range(0,5):
            if grid[i][j] == 0:
                dist = 10000000
                x = P(i)
                y = P(j
        c dfd #cIm just)

                open_set.put([i][j], dist)
    open_set.insert(goal,0)

    while not open_set.empty():
        x,y = open_set.pop()


    # your code: implement A*

    # Initially you may want to ignore theta, that is, plan in 2D.
    # To do so, set actions=forward, cost = [1, 1, 1, 1], and action_name = ['U', 'L', 'R', 'D']
    # Similarly, set parent=[[' ' for row in range(len(grid[0]))] for col in range(len(grid))]

    return path, closed_set


if __name__ == "__main__":
    path,closed=compute_path(grid, init, goal, cost, heuristic)

    for i in range(len(path)):
        print(path[i])

    print("\nExpanded Nodes")    
    for node in closed:
        print(node)

"""
コード例 #22
0
def canon(word):
    """Given a word, returns a list of synonyms in order of relevance"""
    # this is kind of fragile, maybe do something better about this
    return OrderedSet([syn.unicode_repr().split("'")[1].split('.')[0] for syn in wn.synsets(word)])
コード例 #23
0
def compute_path(grid,start,goal,cost,heuristic):
    global forward

    # Use the OrderedSet for your closed list
    closed_set = OrderedSet()

    # Use thePriorityQueue for the open list
    open_set = PriorityQueue(order=min, f=lambda v: v.f)

    # Keep track of the parent of each node. Since the car can take 4 distinct orientations,
    # for each orientation we can store a 2D array indicating the grid cells.
    # E.g. parent[0][2][3] will denote the parent when the car is at (2,3) facing up
    parent = [[[' ' for row in range(len(grid[0]))] for col in range(len(grid))],
             [[' ' for row in range(len(grid[0]))] for col in range(len(grid))],
             [[' ' for row in range(len(grid[0]))] for col in range(len(grid))],
             [[' ' for row in range(len(grid[0]))] for col in range(len(grid))]]

    # The path of the car
    path =[['-' for row in range(len(grid[0]))] for col in range(len(grid))]

    x = start[0]
    y = start[1]
    theta = start[2]
    h = heuristic[x][y]
    g = 0
    f = g+h
    open_set.put(start, Value(f=f,g=g))

    # your code: implement A*
    # Create start and end node
    while len(open_set)!=0:
        current_node,current_node_add=open_set.pop()
        if current_node==goal:
            path[current_node[0]][current_node[1]]='*'
            closed_set.add(current_node)
            print("Goal reached")
            while current_node!=start:
                parent_node = parent[current_node[2]][current_node[0]][current_node[1]]
                or_diff = parent_node[2] - current_node[2]
                #print(or_diff,"\t",current_node,"\n")
                if or_diff== -3:
                    or_diff = -1
                elif or_diff>0:
                    or_diff = -1
                elif or_diff<0:
                    or_diff = 1
                action_value = action.index(or_diff)
                act = action_name[action_value]
                path[parent_node[0]][parent_node[1]] = act
                current_node = parent_node
            break
        closed_set.add(current_node)

    # Generate children
        for index,_ in enumerate(action): # Adjacent squares
            theta = current_node[2] + action[index]
            if theta==4:
                theta=0
            if theta==-1:
                theta=3
            # Get node position
            child_node = ( current_node[0]+forward[theta][0], current_node[1]+forward[theta][1],theta)

            # Make sure within range
            if child_node[0] <= (len(grid)-1) and child_node[0] >= 0 and child_node[1] <= (len(grid[len(grid)-1])-1) and child_node[1] >= 0:
                #Make sure walkable terrain
                if (grid[child_node[0]][child_node[1]]) == 0:
                    if child_node in closed_set:
                        continue
                    else:
                        g = current_node_add.g + cost[index]
                        h =  heuristic[child_node[0]][child_node[1]]
                        g = current_node_add.g + cost[index]
                        f = g+h
                        open_set.put(child_node, Value(f=f,g=g))
                        parent[theta][child_node[0]][child_node[1]] = current_node[0],current_node[1],current_node[2]
                        """if child_node not in open_set:
                            #print(False)
                            h =  heuristic[child_node[0]][child_node[1]]
                            g = current_node_add.g + cost[index]
                            f = g+h
                            open_set.put(child_node, Value(f=f,g=g))
                            parent[theta][child_node[0]][child_node[1]] = current_node[0],current_node[1],current_node[2]
                        elif child_node in open_set and g>current_node_add.g:
                            #print(True)
                            h =  heuristic[child_node[0]][child_node[1]]
                            g = current_node_add.g + cost[index]
                            f = g+h
                            open_set.put(child_node, Value(f=f,g=g))
                            parent[theta][child_node[0]][child_node[1]] = current_node[0],current_node[1],current_node[2]"""

    # Initially you may want to ignore theta, that is, plan in 2D.
    # To do so, set actions=forward, cost = [1, 1, 1, 1], and action_name = ['U', 'L', 'R', 'D']
    # Similarly, set parent=[[' ' for row in range(len(grid[0]))] for col in range(len(grid))]

    return path, closed_set
コード例 #24
0
ファイル: node.py プロジェクト: zhangyazhe/decima-sim
class Node(object):
    def __init__(self, idx, tasks, task_duration, wall_time, np_random):
        self.idx = idx
        self.tasks = tasks
        self.wall_time = wall_time
        self.np_random = np_random

        self.task_duration = task_duration

        self.num_tasks = len(tasks)
        self.num_finished_tasks = 0
        self.next_task_idx = 0
        self.no_more_tasks = False
        self.tasks_all_done = False
        self.node_finish_time = np.inf

        self.executors = OrderedSet()

        # uninitialized
        self.parent_nodes = []
        self.child_nodes = []
        self.descendant_nodes = []
        self.job_dag = None

        self.assign_node_to_tasks()

    def assign_node_to_tasks(self):
        for task in self.tasks:
            task.node = self

    def get_node_duration(self):
        # Warning: this is slow O(num_tasks)
        # get the total duration over all tasks
        duration = 0
        for task in self.tasks:
            duration += task.get_duration()
        return duration

    def is_schedulable(self):
        if self.no_more_tasks:  # no more tasks
            return False
        if self.tasks_all_done:  # node done
            return False
        for node in self.parent_nodes:
            if not node.tasks_all_done:  # a parent node not done
                return False
        return True

    def reset(self):
        for task in self.tasks:
            task.reset()
        self.executors.clear()
        self.num_finished_tasks = 0
        self.next_task_idx = 0
        self.no_more_tasks = False
        self.tasks_all_done = False
        self.node_finish_time = np.inf

    def sample_executor_key(self, num_executors):
        (left_exec, right_exec) = \
            self.job_dag.executor_interval_map[num_executors]

        executor_key = None

        if left_exec == right_exec:
            executor_key = left_exec

        else:
            rand_pt = self.np_random.randint(1, right_exec - left_exec + 1)
            if rand_pt <= num_executors - left_exec:
                executor_key = left_exec
            else:
                executor_key = right_exec

        if executor_key not in self.task_duration['first_wave']:
            # more executors than number of tasks in the job
            largest_key = 0
            for e in self.task_duration['first_wave']:
                if e > largest_key:
                    largest_key = e
            executor_key = largest_key

        return executor_key

    def schedule(self, executor):
        assert self.next_task_idx < self.num_tasks
        task = self.tasks[self.next_task_idx]

        # task duration is determined by wave
        num_executors = len(self.job_dag.executors)
        assert num_executors > 0

        # sample an executor point in the data
        executor_key = self.sample_executor_key(num_executors)

        if executor.task is None or \
            executor.task.node.job_dag != task.node.job_dag:
            # the executor never runs a task in this job
            # fresh executor incurrs a warmup delay
            if len(self.task_duration['fresh_durations'][executor_key]) > 0:
                # (1) try to directly retrieve the warmup delay from data
                fresh_durations = \
                    self.task_duration['fresh_durations'][executor_key]
                i = np.random.randint(len(fresh_durations))
                duration = fresh_durations[i]
            else:
                # (2) use first wave but deliberately add in a warmup delay
                first_wave = \
                    self.task_duration['first_wave'][executor_key]
                i = np.random.randint(len(first_wave))
                duration = first_wave[i] + args.warmup_delay

        elif executor.task is not None and \
                executor.task.node == task.node and \
                len(self.task_duration['rest_wave'][executor_key]) > 0:
            # executor was working on this node
            # the task duration should be retrieved from rest wave
            rest_wave = self.task_duration['rest_wave'][executor_key]
            i = np.random.randint(len(rest_wave))
            duration = rest_wave[i]
        else:
            # executor is fresh to this node, use first wave
            if len(self.task_duration['first_wave'][executor_key]) > 0:
                # (1) try to retrieve first wave from data
                first_wave = \
                    self.task_duration['first_wave'][executor_key]
                i = np.random.randint(len(first_wave))
                duration = first_wave[i]
            else:
                # (2) first wave doesn't exist, use fresh durations instead
                # (should happen very rarely)
                fresh_durations = \
                    self.task_duration['fresh_durations'][executor_key]
                i = np.random.randint(len(fresh_durations))
                duration = fresh_durations[i]

        # detach the executor from old node
        # the executor can run task means it is local
        # to the job at this point
        executor.detach_node()

        # schedule the task
        task.schedule(self.wall_time.curr_time, duration, executor)

        # mark executor as running in the node
        self.executors.add(executor)
        executor.node = self

        self.next_task_idx += 1
        self.no_more_tasks = (self.next_task_idx >= self.num_tasks)

        if self.no_more_tasks:
            if self in self.job_dag.frontier_nodes:
                self.job_dag.frontier_nodes.remove(self)

        return task
コード例 #25
0
    def build_sql(self, data):
        '''
        start with assigning data items to the underlying tables

        it seems sqlite really doesn't give a hoot about data types, and
        I don't see a difference in execution time between "like" and "=" -
        I guess sqlite is smart enough to optimize if there is no wild card.

        actually there IS a difference, so we will use 'like' only if there
        is a '%' in the search value.

        So that will make things a lot more straightforward.
        '''
        from_tables = OrderedSet(["refs"])
        restraints = OrderedSet()
        alias_counter = 0

        for field, value in list(data.items()):
            if not value:
                continue

            if field in ("bibtexkey", "title", "year"):
                key = "refs.%s" % field
                restraints.add(self.translate_restraints(key, value))
                continue

            if field == 'reftype':
                from_tables.add('reftypes')
                restraints.add('(reftypes.reftype_id = refs.reftype_id)')
                restraints.add(self.translate_restraints('reftypes.name', value))
                continue

            table = 'uniqid' if field in ("doi", "pmid") else 'optional'
            alias_counter += 1
            alias = "%s%s" % (table[:3], alias_counter)

            from_tables.add('%s as %s' % (table, alias))

            restraints.add('(%s.ref_id = refs.ref_id)' % alias)
            restraints.add('(%s.field_id = %s)' % (alias, self.field_types[field]))
            restraints.add(self.translate_restraints('%s.content' % alias, value))

        # format the sql
        start_clause = """
                   select
                       refs.ref_id
                   from """

        line = '    %s\n'


        sql = textwrap.dedent(start_clause) + '\n'

        for i, clause in enumerate(from_tables):
            if i+1 < len(from_tables):
                clause += ','
            sql += line % clause

        sql += 'where\n'

        for i, r in enumerate(restraints):
            if i > 0:
                r = 'and ' + r
            sql += line % r

        # open('/home/mpalmer/sql.log','w').write(sql)

        return sql.lstrip()
コード例 #26
0
ファイル: astar.py プロジェクト: SidSong01/Motion-Planning
def compute_path(grid, start, goal, cost, heuristic):

    # Use the OrderedSet for your closed list
    closed_set = OrderedSet()

    # Use thePriorityQueue for the open list
    open_set = PriorityQueue(order=min, f=lambda v: v.f)

    # Keep track of the parent of each node. Since the car can take 4 distinct orientations,
    # for each orientation we can store a 2D array indicating the grid cells.
    # E.g. parent[0][2][3] will denote the parent when the car is at (2,3) facing up
    parent = [[[' ' for row in range(len(grid[0]))]
               for col in range(len(grid))],
              [[' ' for row in range(len(grid[0]))]
               for col in range(len(grid))],
              [[' ' for row in range(len(grid[0]))]
               for col in range(len(grid))],
              [[' ' for row in range(len(grid[0]))]
               for col in range(len(grid))]]

    # The path of the car
    path = [['-' for row in range(len(grid[0]))] for col in range(len(grid))]

    x = start[0]
    y = start[1]
    theta = start[2]
    h = heuristic[x][y]
    g = 0
    f = g + h

    # your code: implement A*
    open_set.put(start, Value(f=f, g=g))
    while open_set.__len__() != 0:
        node = open_set.pop()
        g = node[1].g
        x = node[0][0]
        y = node[0][1]
        theta = node[0][2]
        #if reach the goal, break
        if (node[0] == goal):
            closed_set.add(node[0])
            break
        closed_set.add(node[0])
        for idx, act in enumerate(action):
            new_theta = (theta + act) % 4
            #use orientation to determine the position
            new_x = x + forward[new_theta][0]
            new_y = y + forward[new_theta][1]
            #to make sure it won't break the node is naviable
            if (new_x < 0 or new_x > 4 or new_y < 0 or new_y > 5
                    or grid[new_x][new_y] == 1):
                continue
            new_g = g + cost[idx]
            new_h = heuristic[new_x][new_y]
            new_f = new_g + new_h
            child = [(new_x, new_y, new_theta), Value(f=new_f, g=new_g)]
            if (child[0] not in closed_set and child[0] not in open_set):
                open_set.put(child[0], child[1])
                parent[new_theta][new_x][new_y] = (action_name[idx], node[0])
            #if the cost decreased, add it to the openlist
            elif (open_set.has(child[0]) and open_set.get(child[0]).g > new_g):
                open_set.put(child[0], child[1])
                parent[new_theta][new_x][new_y] = (action_name[idx], node[0])
            #recording the path in parent

    #reach the goal
    path[x][y] = '*'
    #find out the path by recalling step by step
    while (x, y, theta) != start:
        pre_step = parent[theta][x][y]
        x = pre_step[1][0]
        y = pre_step[1][1]
        theta = pre_step[1][2]
        path[x][y] = pre_step[0]

    # Initially you may want to ignore theta, that is, plan in 2D.
    # To do so, set actions=forward, cost = [1, 1, 1, 1], and action_name = ['U', 'L', 'R', 'D']
    # Similarly, set parent=[[' ' for row in range(len(grid[0]))] for col in range(len(grid))]

    return path, closed_set
コード例 #27
0
def compute_path(grid, start, goal, cost, heuristic):

    # Use the OrderedSet for your closed list
    closed_set = OrderedSet()

    # Use thePriorityQueue for the open list
    open_set = PriorityQueue(order=min, f=lambda v: v.f)

    # Keep track of the parent of each node. Since the car can take 4 distinct orientations,
    # for each orientation we can store a 2D array indicating the grid cells.
    # E.g. parent[0][2][3] will denote the parent when the car is at (2,3) facing up
    parent = [[[' ' for row in range(len(grid[0]))]
               for col in range(len(grid))],
              [[' ' for row in range(len(grid[0]))]
               for col in range(len(grid))],
              [[' ' for row in range(len(grid[0]))]
               for col in range(len(grid))],
              [[' ' for row in range(len(grid[0]))]
               for col in range(len(grid))]]

    # The path of the car
    path = [['-' for row in range(len(grid[0]))] for col in range(len(grid))]

    x = start[0]
    y = start[1]
    theta = start[2]
    h = heuristic[x][y]
    g = 0
    f = g + h
    children = []
    # print(start)

    open_set.put(start, Value(f=f, g=g))

    while open_set:
        node, new_cost = open_set.pop()
        print('The current node is:', node)
        print('==============================')
        if node == goal:
            return path, closed_set
        closed_set.add(node)

        row_grid = len(grid) - 1  #4   Boundry of the grids
        column_grid = (len(grid[0])) - 1  #5
        x1 = node[0]  #Parrent x co-ordinate
        y1 = node[1]  # Parrent y co-ordinate
        theta1 = node[2]  # Parrent orientation
        f1 = new_cost.f  #Parent total cost
        h = heuristic[x1][y1]  #Heuristic of parrent
        g1 = new_cost.g  #Path cost of parrent
        neighbor = []
        f12 = []
        h12 = []
        g12 = []
        for i in range(len(action)):
            temp_dir = collections.deque(
                direction
            )  # creating a deque tuple            # 0 N   :::: 1 west     2 south :::: east 3
            index = theta1  # orientation of the Parent
            child_index = action[i]  # possible index of the child
            n = -1 * child_index
            temp_dir.rotate(n)
            child_theta = temp_dir[
                theta1]  # using the index of the parent to find out the current orientation of the child
            """ calculating the distances of children"""

            x_pos = node[0] + forward[child_theta][
                0]  # this gives the position of x,y of the child
            y_pos = node[1] + forward[child_theta][1]
            pos = (x_pos, y_pos, child_theta)
            g1 = []
            if (pos[0] > row_grid or pos[1] > (column_grid) or pos[1] < 0
                    or pos[0] < 0):
                continue
            elif (grid[pos[0]][pos[1]] == 1):
                continue
            else:
                neighbor.append(pos)
                h1 = heuristic[x_pos][
                    y_pos]  # heuristic of neighbor that is dist from the goal
                g1 = new_cost.g + cost[i]  # path function
                print(new_cost.g)
                print("---------------------")
                print(cost[i])
                f1 = g1 + h1  # total cost
                f12 = np.append(f12, f1)
                g12 = np.append(g12, g1)
                h12 = np.append(h12, h1)

            gmin = min(g12)
            posi = np.argmin(g12)
            c = 0

            for neigh in neighbor:  #Condition for child which is closest to the goal
                if neigh not in open_set or neigh not in closed_set:
                    open_set.put(neigh, Value(f=f12[c], g=g12[c]))
                    # print(open_set.get(neigh).g)
                    print(neigh)
                    print(open_set.get(neigh).g)

                elif neigh in open_set and gmin < open_set.get(neigh).g:
                    open_set.put(neigh, Value(f=f12[c], g=g12[c]))
                    # print("ggggggggg")
                c = c + 1
            c = 0

    return path, closed_set
コード例 #28
0
 def reset(self, executors):
     self.free_executors = {}
     self.free_executors[None] = OrderedSet()
     for executor in executors:
         self.free_executors[None].add(executor)
コード例 #29
0
 def add_job(self, job):
     self.free_executors[job] = OrderedSet()
コード例 #30
0
 def reset(self):
     self.job_dags = OrderedSet()
     self.msg_mats = []
     self.msg_masks = []
     self.dag_summ_backward_map = None
     self.running_dag_mat = None