批处理作业调度问题·回溯法·java版

问题描述:

/*************************************************************
批处理作业调度
给定n个作业的集合{J1,J2,…,Jn}。
每个作业必须先由机器1处理,然后由机器2处理。
作业Ji需要机器j的处理时间为tji。
对于一个确定的作业调度,设Fji是作业i在机器j上完成处理的时间。
所有作业在机器2上完成处理的时间和称为该作业调度的完成时间和。
f = F21 + F22 + F23 + ... + F2n
批处理作业调度问题要求对于给定的n个作业,
制定最佳作业调度方案,使其完成时间和达到最小。
*************************************************************/

public class FlowShop

    int n,         //作业数;
        f1,        //机器1完成处理时间;
        f,         //完成时间和;
        bestf;     //当前最优值;
   
    int [][]m;      //各作业所需的处理时间;
    int []x;        //当前作业调度;
    int []bestx;    //当前最优作业调度;
    int []f2;       //机器2完成处理时间;
   
   
   private  void backtrack(int i)
   {
    if(i>n)
    {
     for (int j=1;j<=n;j++)
      {
      bestx[j]=x[j];
      System.out.print(x[j]+" ");
   }
     System.out.println();
      bestf=f;
      System.out.println("每条深度优先搜索结果为:"+bestf);
    
    }
    else
     for(int j=i;j<=n;j++)
     {
      f1+=m[x[j]][0];
      f2[i]=((f2[i-1]>f1)? f2[i-1]:f1)+m[x[j]][1];
      f+=f2[i];
 //      if(f<bestf)
      if(true)
      {
       MyMath.swap(x,i,j);
       backtrack(i+1);
       MyMath.swap(x,i,j); 
    }
      f1-=m[x[j]][0];
      f-=f2[i];
     
     
     }
   
   
   }
   
   public void ShowTest()
    {
    n=3;
    bestf=Integer.MAX_VALUE;
    f1=0;
    f=0;
     
   int [][]m={{0,0},{2,1},{3,1},{2,3}};
   int []x={0,1,2,3};
   int []bestx={0,1,2,3};
   f2=new int[4];
   this.m = m;
   this.x=x;
   this.bestx=bestx;
   this.f2=f2;
  
 
   backtrack(1);
   System.out.println("当前最优值:"+bestf);
   System.out.println("当前最优作业调度");
   for(int i=1;i<=n;i++)
    {
    System.out.print(bestx[i]+"  ");
    }
   
    }
   
 public static void main(String[] args)
 {
  // TODO Auto-generated method stub
  
  FlowShop fs=new FlowShop();
  fs.ShowTest();

 }
 
}


class MyMath
{
 public MyMath(){}
 public static int[] swap(int[] x,int i,int j)
 {
 // int[] returnString=new int[3];
  int ss;
  ss=x[j];
  x[j]=x[i];
  x[i]=ss;
  return x;
    
 }
}

转载于:https://www.cnblogs.com/mushroom_lb/archive/2008/11/04/1326451.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值