批處理作業調度-回溯法

        批處理作業調度不同於流水作業的地方在於要求所有作業完成的時間總和最小。

package test;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zuohao on 2018/12/9.
 * 批處理作業調度
 */
public class BatchJobSchedulingProblem {
    public static void main(String[] arg) {
        new BatchJobSchedulingProblem().backtracking();
    }

    /**
     * 回溯法
     */
    public void backtracking(){
        List<Task> taskList=new ArrayList<>();
        taskList.add(new Task("J1",2,1));
        taskList.add(new Task("J2",3,1));
        taskList.add(new Task("J3",2,3));
        Node root=new Node();
        root.setT1(0);
        root.setT2(0);
        root.setSt(0);
        Node best=new Node();
        best.setSt(Integer.MAX_VALUE);
        tree(root,taskList,best);
        for (Task task:best.getTaskList()){
            System.out.println(task.getName());
        }
        System.out.println(best.getSt());
    }
    public void tree(Node parentNode,List<Task> taskList,Node best){
        int t1=parentNode.getT1();
        int t2=parentNode.getT2();
        int st=parentNode.getSt();
        for (Task task:taskList){
            int nextt1=t1+task.getA();
            int nestt2=(t2-t1)>task.getA()?t2+task.getB():nextt1+task.getB();
            if (st+nestt2>best.getSt()){
                continue;
            }
            Node node=new Node();
            node.setTask(task);
            node.setT1(nextt1);
            node.setT2(nestt2);
            node.setSt(st+nestt2);
            node.getTaskList().addAll(parentNode.getTaskList());
            node.getTaskList().add(task);
            parentNode.getChildNodeList().add(node);
            List<Task> newTaskList=new ArrayList<>();
            newTaskList.addAll(taskList);
            newTaskList.remove(task);
            if (newTaskList.size()==0){
                best.setSt(node.getSt());
                best.setTaskList(node.getTaskList());
//                System.out.println(node.getSt());
            }
            tree(node,newTaskList,best);
        }
    }

    class Node{
        private Task task;
        private int t1;
        private int t2;
        private int st;
        //這裏定義子節點只爲根據概念上形成物理樹結構,如果樹在之後不再使用可以不定義子節點列表
        private List<Node> childNodeList=new ArrayList<>();
        private List<Task> taskList=new ArrayList<>();

        public List<Node> getChildNodeList() {
            return childNodeList;
        }

        public void setChildNodeList(List<Node> childNodeList) {
            this.childNodeList = childNodeList;
        }

        public Task getTask() {
            return task;
        }

        public void setTask(Task task) {
            this.task = task;
        }

        public int getT1() {
            return t1;
        }

        public void setT1(int t1) {
            this.t1 = t1;
        }

        public int getT2() {
            return t2;
        }

        public void setT2(int t2) {
            this.t2 = t2;
        }

        public int getSt() {
            return st;
        }

        public void setSt(int st) {
            this.st = st;
        }

        public List<Task> getTaskList() {
            return taskList;
        }

        public void setTaskList(List<Task> taskList) {
            this.taskList = taskList;
        }
    }

    class Task{
        private String name;
        private int a;
        private int b;

        public Task(String name, int a, int b) {
            this.name = name;
            this.a = a;
            this.b = b;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getA() {
            return a;
        }

        public void setA(int a) {
            this.a = a;
        }

        public int getB() {
            return b;
        }

        public void setB(int b) {
            this.b = b;
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章