Python调度算法

我正在编写一个用于测试目的的调度算法,并遵循“算法简介”一书,但这是我所能得到的。截至目前,这项工作正在“某处无休止的循环”中消亡。一般来说,算法应该有利润,我们根据利润对所有其他数组进行排序“但我现在只是忽略它”并且我的输入已经排序。因此,截止日期和通过的工作已经排序。需要一些帮助,因为它仍然无法正常工作。


#!/usr/bin/python3.6


class Scheduling:


  def schedule(self, n, deadline, jobs):

    self.fdeadline = deadline

    self.J = []

    self.J.append(jobs[0])

    self.i = 1

    while self.i <= n:

      self.K = self.J.copy() 

      self.K.append(jobs[self.i]) 

      self.i = self.i + 1

      if self.feasible(self.K, self.fdeadline) == True :

        self.J = self.K


    return self.J  


  def feasible(self, K, fdl):

    self.tmp = K

    self.isFeasible = True


    self.i = 0

    self.j = 1

    self.k = 0 


    while self.i < len(self.tmp):

      while self.j < len(self.tmp):

        self.index1 = self.i

        self.index2 = self.j

        if (fdl[self.index1] > fdl[self.index2]):

          self.tmp[i], self.tmp[j]  =  self.tmp[j], self.tmp[i]  


    while self.k < len(self.tmp):

       self.job  = self.tmp[self.k]

       if (fdl[self.job] < k + 1):

         isFeasible = False

         break


    return isFeasible 



def main():

   sins = Scheduling()

   n = 4

   deadline = [1,1,2,2]

   jobs = [4, 2, 1, 3]

   sjobs = sins.schedule(n, deadline, jobs)

   print (sjobs)  


if __name__ == "__main__":

  main()


阿晨1998
浏览 253回答 2
2回答

holdtom

首先了解selfpython。其次了解作业排序问题。然后看下面的代码class Scheduling:&nbsp; def schedule(self, n, deadline, jobs):&nbsp; &nbsp; # max number of jobs you can schedule is the max deadline available.&nbsp; &nbsp; filledJobs = ['dummy']*max(deadline);&nbsp; &nbsp; i = 0&nbsp; &nbsp; # start assigning the jobs in a greeedy way&nbsp; &nbsp; while i < n:&nbsp; &nbsp; &nbsp; &nbsp; job = jobs[i]&nbsp; &nbsp; &nbsp; &nbsp; j = deadline[i]&nbsp; &nbsp; &nbsp; &nbsp; # assign the job from the last deadline&nbsp; &nbsp; &nbsp; &nbsp; while j > 0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(filledJobs[j-1] == 'dummy'):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; filledJobs[j-1] = job&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j = j - 1&nbsp; &nbsp; &nbsp; &nbsp; i = i + 1&nbsp; &nbsp; return filledJobsdef main():&nbsp; &nbsp;sins = Scheduling()&nbsp; &nbsp;n = 4&nbsp; &nbsp;deadline = [1,1,2,2]&nbsp; &nbsp;# assuming jobs are sorted w.r.t&nbsp; profits&nbsp; &nbsp;# I represented the jobs with string to be clear&nbsp; &nbsp;jobs = ['a', 'b', 'c', 'd']&nbsp; &nbsp;sjobs = sins.schedule(n, deadline, jobs)&nbsp; &nbsp;print (sjobs)&nbsp;&nbsp;if __name__ == "__main__":&nbsp; main()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python