批處理作業調度-分支界限法

        批處理作業調度-分支界限法

package test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

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

    /**
     * 分支界限法-優先隊列式
     * 優先隊列式求解時,到達第一個沒有子結點的活結點時,即爲最優解
     */
    public void branchAndBoundMethod() {
        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);
        for (Task task:taskList){
            Node node=new Node();
            node.setTask(task);
            node.setT1(task.getA());
            node.setT2(task.getA()+task.getB());
            node.setSt(node.getT2());
            root.getChildNodeList().add(node);
        }
        List<Node> nodeList=new ArrayList<>();
        nodeList.add(root);
        while (nodeList.size()>0){
            addNode(nodeList.get(0),nodeList);
            Collections.sort(nodeList, new Comparator<Node>() {
                @Override
                public int compare(Node o1, Node o2) {
                    return o1.getSt()-o2.getSt();
                }
            });
        }
    }

    public void addNode(Node parentNode,List<Node> nodeList){
        //移除活結點
        nodeList.remove(parentNode);
        int t1=parentNode.getT1();
        int t2=parentNode.getT2();
        int st=parentNode.getSt();
        if (parentNode.getChildNodeList().size()==0){
            //第一次執行到這裏時其實已經得到最優解
            for (Task task:parentNode.getTaskList()){
                System.out.print(task.getName()+",");
            }
            System.out.println();
            System.out.println(parentNode.getSt());
            return;
        }
        for (Node node:parentNode.getChildNodeList()){
            Task task=node.getTask();
            Node newNode=new Node();
            int nextt1=t1+task.getA();
            int nestt2=(t2-t1)>task.getA()?t2+task.getB():nextt1+task.getB();
            newNode.setTask(task);
            newNode.setT1(nextt1);
            newNode.setT2(nestt2);
            newNode.setSt(st+nestt2);
            List<Task> newTaskList=new ArrayList<>();
            newTaskList.addAll(parentNode.getTaskList());
            newTaskList.add(task);
            newNode.setTaskList(newTaskList);
            List<Node> newChildNodeList=new ArrayList<>();
            newChildNodeList.addAll(parentNode.getChildNodeList());
            newChildNodeList.remove(node);
            newNode.setChildNodeList(newChildNodeList);
            nodeList.add(newNode);
        }
    }

    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;
        }
    }
}


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