批处理作业调度-分支界限法
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;
}
}
}
©著作权归作者所有:来自51CTO博客作者塞上名猪的原创作品,如需转载,请注明出处,否则将追究法律责任