Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help ( in Python ) with the implementation of dependency _ scheduler _ cooking _ redo method and class AND _ OR _

I need help (in Python) with the implementation of dependency_scheduler_cooking_redo method and class AND_OR_Scheduler. My Dependency Scheduler almost works, but is failing a few tests. import networkx as nx # Library for displaying graphs.
import matplotlib.pyplot as
p.tt
import random
class DependencyScheduler(object):
def self):
self.tasks = set()
# The successors of a task are the tasks that depend on it, and can
# only be done once the task is completed.
# The predecessors of a task (set)
self.predecessors = defaultdict(set)
self.completed_tasks =?set() # completed tasks
def add_task(self,t, dependencies):
""".Adds a task t'with given dependencies. "'."
# Makes sure we know about all tasks mentioned
assert t not in self.tasks or len(self.predecessors[t])==0, "The task was already present."
self.tasks.add(t)
self.tasks.update(dependencies)
# The predecessors are the tasks that need to be done before.
self.predecessors [t]= set(dependencies)
# The new task is a successor of its dependencies
for u in dependencies:
def reset(self):
self.completed_tasks = set()
@property
def done(self):
return self.completed_tasks == self.tasks
def show(self):
""".We use the nx graph to display the graph."".
g= nx.DiGraph()
g.add_nodes_from(self.tasks)
hode colges_from([(u, v) for u in self.tasks for v in self.successors[u]])
('green' if v in self.completed_tasks
else 'red' if v in self.available_tasks
else 'gray')
nx.draw(g, with_labels=True, node_color=node_colors)
p.lt.show()
@property
def uncompleted(self):
"""Returns the tasks that have not been completed
This is a property, so you can say scheduler.uncompleted rather than
scheduler.uncompleted()"I"
return self.tasks - self.completed_tasks
@property
def available_tasks(self):
""."Returns the available tasks.
This is just a placeholder; we will let you implement this.".".
return set()
def mark_completed(self, t):
"1"Marks that the task t is completed, and returns the set of tasks
that become available as a consequence.
This is just a placeholder, we will let you implement this.""!"
return set()
def check(self):
"."We check that if t is a successor of u, then u is a predecessor
of t.""
for u in self.tasks
for t in self.successors[u]:
assert u in self.predecessors[t]
#@title Implementation of and .
def scheduler_available_tasks(self):
"""Returns the set of tasks that can be done in parallel.
A task can be done if all its predecessors have been completed
And of course, we don't return any task that has already been
available_tasks = set()
avallable in tasks.
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions