你发呆时好傻 2018-04-28 05:30 采纳率: 20%
浏览 2514
已结题

使用遗传算法进行的自动排课设计,需要指出算法具体对应的代码中

需要指出遗传算法对应步骤是用哪些代码实现的,大家帮帮忙,谢谢谢谢!

  • 写回答

2条回答 默认 最新

  • 你发呆时好傻 2018-04-28 05:30
    关注

    java代码如下:

    package com.autosortcourse.beans;

    import com.autosortcourse.database.PersistenceManager;

    //import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    //import java.sql.Statement;
    import java.util.ArrayList;
    //import java.util.List;
    //import java.lang.*;
    import java.util.Random;
    import com.autosortcourse.beans.CInfoImpl;
    //import com.autosortcourse.beans.SelectCmd;
    import com.autosortcourse.bean.CInfoBean;

    /**
    *

    • @author Administrator
      */
      public class AutoPlan
      {
      private PersistenceManager per = null;

      public final int CType_PuTong=1;
      public final int CType_JaoNan=2;
      public final int CType_TiYu=3;

      public final int RType_YinYue=2;
      public final int RType_TiYu=3;
      public final int RType_JSJ=4;
      public final int RType_WuLi=5;
      public final int RType_HuaXue=6;
      public int[] Jc15=new int[2];
      public int[] Jc36=new int[2];
      public int[] Jc47=new int[2];
      public int[] XqOld=new int[4];
      int tryCount;
      int byXq,byJc,byRm;
      int j,jCount;
      int CourseType;

      public AutoPlan()
      {
      per = new PersistenceManager();

      LoadRoomData();
      

      }

      public String msgErr;

      public String startPlan(String pTermIdStr,String pClassId)
      {
      int ClassIdold, ClassIdnew;
      int RoomTypeIdold, RoomTypeIdnew;

      int classSizenew,classSizeold,teacherId,courseId,roomCount;
      msgErr = "";
      String cName="";
      ClassIdold=0;
      RoomTypeIdold=0;
      classSizeold=0;
      CInfoImpl objDb = new CInfoImpl(per);
      CInfoBean curobj=null;
      roomCount=0;

      //初始化Jc15,Jc47,其中Jc15为(必修)较难课程准备,Jc47为体育课程准备
      Jc15[0]=1;
      Jc15[1]=5;
      Jc36[0]=3;
      Jc36[1]=6;  
      Jc47[0]=4;
      Jc47[1]=7;
      PersistenceManager pdb = new PersistenceManager();//定义一个数据库连接方法名字为pdb
      String sqlstr="select * from vwClCo where  CcStatus='未排' and TermId=" + pTermIdStr+" and ClassId=" + pClassId ;
      sqlstr +=  " order by CourseType desc ,RoomTypeId desc";//CourseType 
      ResultSet rst = pdb.executeQuery(sqlstr);//rst染色体
      
      //int iCount=0;
      int XqId,JcId,RmId;//星期,节次,教室Id
      java.util.Random rd =new java.util.Random();//
      XqId = rd.nextInt(5) + 1;//初始化学期数据,确定编码
      JcId = rd.nextInt(7) + 1;//初始化课程类型,jc为必修课程作准备数据,确定编码
      RmId=0;//初始化教室数据
      boolean isHulue=false;
      try 
      {
          while (rst.next())//rst执行查询获取数据库里面的信息装入变量rst内
          {
              ClassIdnew = rst.getInt("ClassId");
              RoomTypeIdnew = rst.getInt("RoomTypeId");
              classSizenew = rst.getInt("ClassSize");
              teacherId = rst.getInt("TeacherId");
              courseId = rst.getInt("courseId");
              CourseType = rst.getInt("CourseType");
              isHulue=false;
               if(ClassIdold !=ClassIdnew)
              {
                  SetClassList(ClassIdnew, pTermIdStr);//创建班级信息列表,
                  cName=rst.getString("ClassName");//获取班级名称
                  msgErr +="<p style=\"padding-left:30px;\"><h3>"+cName +"班级排课开始</h3>";
              }    
      
               if (teacherId == 0 )
               {
                   msgErr +=  "任课教师未设置,跳过【" ;
                   msgErr += rst.getString("ClassName") + "】【" + rst.getString("CourseName") + "】【" + rst.getString("TermName") + "】<br>";
                   isHulue=true;
                   continue ; //任课教师未设置,跳过当前未排课记录;         
               }
      
               if(RoomTypeIdnew >1 && 
                      (RoomTypeIdold != RoomTypeIdnew ||  classSizenew > classSizeold)){
                      SetRoomData(RoomTypeIdnew,classSizenew);
                      roomCount=RoomList.size();          
               }
      
               if (RoomTypeIdnew >1 && roomCount < 0 )
               {
                   msgErr += "教室不够,跳过【" ;
                   msgErr += rst.getString("ClassName") + "】【" + rst.getString("CourseName") + "】【" + rst.getString("TermName") + "】<br>";
                   isHulue=true;
                   continue ; //教室不够,跳过当前未排课记录;         
               }
      
              ClassIdold = ClassIdnew;
              classSizeold = classSizenew;
              RoomTypeIdold = RoomTypeIdnew;//变异生成前编码=变异生成后的编码
      
              jCount = rst.getInt("WeekCount");//课程种群项大小,获取周课时数
              j = 0;//循环变量j=0
              tryCount = 0;//初始化尝试次数
      
              XqOld[0] = 0;
              XqOld[1] = 0;
              XqOld[2] = 0;
              XqOld[3] = 0;
              if (jCount < 1 )
              {
                  msgErr += "周课时数设置不当<1,跳过【" ;
                  msgErr += rst.getString("ClassName") + "】【" + rst.getString("CourseName") + "】【" + rst.getString("TermName") + "】<br>";
                  isHulue=true;
                  continue ; //教室不够,跳过当前未排课记录;         
              }
      
              while(j<jCount){
                  //tryCount=0;
      
                  BianYiXqJc(ClassIdnew,rd);
                  XqId=byXq;
                  JcId=byJc;
      
                  isHulue=false;
      
                  if(RoomTypeIdnew <=1){
                      RmId=0;
                  }else if(j==0 || tryCount >100){
                      BianYiRm(roomCount,rd);
                      RmId=byRm;
                  }
      
                  if(tryCount>=200){
                      if(j>0){
                          objDb.delObjs(pTermIdStr,ClassIdnew,courseId);
                      }
                      msgErr += "尝试次数太多,跳过【" ;
                      msgErr += rst.getString("ClassName") + "】【" + rst.getString("CourseName") + "】【" + rst.getString("TermName") + "】<br>";
                      isHulue=true;
                      break ; //尝试次数太多,跳过当前未排课记录
                  }
      
                  //检查教师和教室的适应性
                  if(ThRmNoBusy(XqId,JcId,RmId,Integer.parseInt(pTermIdStr),teacherId)){
                      //找到位置
                      if(j<4){
                          XqOld[j]=XqId;
                      }
                      j=j+1;
                      tryCount=0;
                      ClassList[(JcId-1)*5 + XqId -1]=ClassIdnew;
                      curobj= objDb.GetBean("", pTermIdStr, String.valueOf(ClassIdnew)
                              , String.valueOf(courseId), String.valueOf(teacherId), String.valueOf(RmId), XqId, JcId, "自动");
                       objDb.addObj(curobj);
      
                  }       
              }//j结束,待排课记录完成
      
              if(!isHulue){
                  String strsql1 = "update ClCo set CcStatus='已排' where ClassId=" + ClassIdnew
                   + " and courseId =" + courseId+ " and TermId=" + pTermIdStr;
      
                   per.executeProcedure(strsql1) ;
                   }
      
               }//i结束,所有课程完成
      
               msgErr +=cName+"自动排课完成!<br></p>";
           }
           catch (Exception e)
           {
           e.printStackTrace();
           msgErr +=cName+"自动排课出错!"+e.getMessage()+"<br></p>";
           }
           finally
           {
                per.close();
                pdb.close();
           }
      
           //msgErr=SelectCmd.getOptReturnStr(msgErr);
      
           return msgErr;
      

      }

      public boolean ThRmNoBusy(int xq,int jc,int rmid,int termId,int thid){
      //负责检测班级老师 教室在给定时间片是否冲突
      boolean nobusy=true;
      String sql ="select LessonOrders,Weeks,ClassId,TeacherId,ClassRoomId ";
      sql +="from CourseInfoTb ";
      sql +=" where TermId=" + termId ;

      sql +=" and Weeks=" + xq ;
      sql +=" and LessonOrders=" + jc ;
      sql +=" and (TeacherId=" + thid ;
      if(rmid !=0){
      sql += " or ClassRoomId=" + rmid ;
      }
      sql +=")" ;

      try {
      
          ResultSet rs = per.executeQuery(sql);       
          if (rs !=null&&rs.next()) {
              nobusy = false;
          }
      } catch (SQLException e) {
          e.printStackTrace();
      } 
      
      return nobusy;
      

      }

      public boolean ClassThRmNoBusy(int xq,int jc,int rmid,int termId,int thid,int pclassid){
      boolean nobusy=true;
      String sql ="select LessonOrders,Weeks,ClassId,TeacherId,ClassRoomId ";
      sql +="from CourseInfoTb ";
      sql +=" where TermId=" + termId ;

      sql +=" and Weeks=" + xq ;
      sql +=" and LessonOrders=" + jc ;
      sql +=" and (TeacherId=" + thid + " or ClassRoomId=" + rmid + " or ClassId=" + pclassid + ")" ;

      try {
      
          ResultSet rs = per.executeQuery(sql);       
          if (rs !=null&&rs.next()) {
              nobusy = false;
          }
      } catch (SQLException e) {
          e.printStackTrace();
      } 
      
      return nobusy;
      

      }

      private void BianYiXqJc(int pclassid,Random rd){//变异运算
      tryCount = tryCount + 1;//初始化尝试次数
      if (tryCount < 100 ){
      if( j > 0 && jCount < 6 ){//循环变量j,课程种群项大小jCount
      BianYiXq(rd);

      }else{
      byXq = rd.nextInt(5) + 1;

      }
      if(CourseType==CType_JaoNan){
      if(tryCount<40)
      {
      byJc=Jc15[rd.nextInt(2)];
      }
      else if(tryCount<70)
      {
      byJc=2;
      }
      else
      {
      byJc=Jc36[rd.nextInt(2)];
      }
      }else if(CourseType==CType_TiYu){
      byJc=Jc47[rd.nextInt(2)];

      }else{
      byJc = rd.nextInt(7) + 1;
      }

      }else{
      byXq = rd.nextInt(5) + 1;
      byJc = rd.nextInt(7) + 1;

      }

        if(ClassList[(byJc-1)*5 + byXq -1]==pclassid && tryCount <200){
            BianYiXqJc(pclassid, rd);
        }
      

      }

      private void BianYiXq(Random rd){ //变异运算、学期
      byXq = rd.nextInt(5) + 1;
      for(int i=0;i<3;i++){
      if(XqOld[i]==byXq){
      BianYiXq(rd);
      break;
      }
      }
      }

      private void BianYiRm(int roomCount, Random rd){ //变异运算、教室
      int rdid = rd.nextInt(roomCount) ;
      byRm=RoomList.get(rdid)[0];
      }

      private void SetClassList(int pClassId,String pTermIdStr)
      {
      String sql="select LessonOrders,Weeks from CourseInfoTb ";

      sql+=" where TermId=" + pTermIdStr + " and ClassId=" + pClassId;
      
      ResultSet rs=null;
      
      try {
          rs = per.executeQuery(sql);
          while (rs.next()) {
              int jc=rs.getInt(1);
              int wk=rs.getInt(2);
      
              ClassList[(jc - 1) * 5 + wk - 1] = pClassId;
          }
      } catch (SQLException e) {
          e.printStackTrace();
      } 
      

      }

      private void SetRoomData(int pRoomTypeId,int pClassSize)
      {
      RoomList=new ArrayList();
      ArrayList roomList=RoomList;
      switch(pRoomTypeId){
      case RType_YinYue:
      roomList=roomList2;
      break;
      case RType_TiYu:
      roomList=roomList3;
      break;
      case RType_JSJ:
      roomList=roomList4;
      break;
      case RType_WuLi:
      roomList=roomList5;
      break;
      case RType_HuaXue:
      roomList=roomList6;
      break;
      default:
      break;
      }
      int iCount=roomList.size();
      for(int i=0;i int[] cr= roomList.get(i);
      if(cr[2]>pClassSize){
      RoomList.add(cr);
      }

      }
      

      }

      private ArrayList roomList2=new ArrayList();
      private ArrayList roomList3;
      private ArrayList roomList4;
      private ArrayList roomList5;
      private ArrayList roomList6;
      public ArrayList RoomList;

      public int[] ClassList=new int[35];

      private void LoadRoomData()
      {
      roomList2=new ArrayList();
      roomList3=new ArrayList();

      roomList4=new ArrayList();
      roomList5=new ArrayList();
      roomList6=new ArrayList();

      String sql="select CRoomId,RoomTypeId,CRoomSeatingCapacity ";
      sql+="from ClassRoomTb order by RoomTypeId";
      
      ResultSet rs=null;
      
      try {
          rs = per.executeQuery(sql);
          while (rs.next()) {
              int t1=rs.getInt(1);
              int t2=rs.getInt(2);
              int t3=rs.getInt(3);
              int[] data = {t1,t2,t3};
      
              switch(t2){
                  case RType_YinYue:
                      roomList2.add(data);
                      break;
                  case RType_TiYu:
                      roomList3.add(data);
                      break;
                  case RType_JSJ:
                      roomList4.add(data);
                      break;
                  case RType_WuLi:
                      roomList5.add(data);
                      break;
                  case RType_HuaXue:
                      roomList6.add(data);
                      break;
                  default:
                      break;
              }
          }
      } catch (SQLException e) {
          e.printStackTrace();
      } //finally {
      //      per.close();
      //}
      

      }

      }

    评论

报告相同问题?

悬赏问题

  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题