/** The main function */
  @Override
  public void run() {

    for (Iterator it = getTaskList().iterator(); it.hasNext(); ) {
      Task task = (Task) it.next();
      double duration = task.getCloudletLength() / 1000;

      for (int i = 0; i < task.getParentList().size(); i++) {
        Task parent = task.getParentList().get(i);
      }

      for (int i = 0; i < task.getChildList().size(); i++) {
        Task child = task.getChildList().get(i);
      }

      int vmNum = getVmList().size();
      /** Randomly choose a vm */
      Random random = new Random((long) duration);
      int vmId = random.nextInt(vmNum);

      CondorVM vm = (CondorVM) getVmList().get(vmId);
      // This shows the cpu capability of a vm
      double mips = vm.getMips();

      task.setVmId(vm.getId());

      long deadline = Parameters.getDeadline();
    }
  }
  /** The main function */
  @Override
  public void run() {

    double[][] bandwidths = Parameters.getBandwidths();
    int vmNum = getVmList().size();
    int taskNum = getTaskList().size();
    double[] availableTime = new double[vmNum];
    // cloudlet id starts from 1
    double[][] earliestStartTime = new double[taskNum + 1][vmNum];
    double[][] earliestFinishTime = new double[taskNum + 1][vmNum];
    int[] allocation = new int[taskNum + 1];

    List<Task> taskList = new ArrayList<Task>(getTaskList());
    List<Task> readyList = new ArrayList<Task>();
    while (!taskList.isEmpty()) {
      readyList.clear();
      for (Task task : taskList) {
        boolean ready = true;
        for (Task parent : task.getParentList()) {
          if (taskList.contains(parent)) {
            ready = false;
            break;
          }
        }
        if (ready) {
          readyList.add(task);
        }
      }
      taskList.removeAll(readyList);
      // schedule readylist
      for (Task task : readyList) {
        long[] fileSizes = new long[task.getParentList().size()];
        int parentIndex = 0;
        for (Task parent : task.getParentList()) {
          long fileSize = 0;
          for (Iterator fileIter = task.getFileList().iterator(); fileIter.hasNext(); ) {
            File file = (File) fileIter.next();
            if (file.getType() == 1) {
              for (Iterator fileIter2 = parent.getFileList().iterator(); fileIter2.hasNext(); ) {
                File file2 = (File) fileIter2.next();
                if (file2.getType() == 2 && file2.getName().equals(file.getName())) {
                  fileSize += file.getSize();
                }
              }
            }
          }
          fileSizes[parentIndex] = fileSize;
          parentIndex++;
        }

        double minTime = Double.MAX_VALUE;
        int minTimeIndex = 0;

        for (int vmIndex = 0; vmIndex < getVmList().size(); vmIndex++) {
          Vm vm = (Vm) getVmList().get(vmIndex);
          double startTime = availableTime[vm.getId()];
          parentIndex = 0;
          for (Task parent : task.getParentList()) {
            int allocatedVmId = allocation[parent.getCloudletId()];
            double actualFinishTime = earliestFinishTime[parent.getCloudletId()][allocatedVmId];
            double communicationTime =
                fileSizes[parentIndex] / bandwidths[allocatedVmId][vm.getId()];

            if (actualFinishTime + communicationTime > startTime) {
              startTime = actualFinishTime + communicationTime;
            }
            parentIndex++;
          }
          earliestStartTime[task.getCloudletId()][vm.getId()] = startTime;
          double runtime = task.getCloudletLength() / vm.getMips();
          earliestFinishTime[task.getCloudletId()][vm.getId()] = runtime + startTime;

          if (runtime + startTime < minTime) {
            minTime = runtime + startTime;
            minTimeIndex = vmIndex;
          }
        }

        allocation[task.getCloudletId()] =
            minTimeIndex; // we do not really need it use task.getVmId
        task.setVmId(minTimeIndex);
        availableTime[minTimeIndex] = minTime;
      }
    }
  }