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

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

2个回答

界面设计代码如下:
<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="com.autosortcourse.beans.CInfoImpl"%>
<%@page import="com.autosortcourse.beans.SelectCmd"%>


自动排课管理


<%
String optClass=SelectCmd.getClassOptions(false);
String optTerm=SelectCmd.getTermOptions(false);

%>

function goStart(pfg){ var form2 = document.getElementById("form2") ; var classid = document.getElementById("classid") ; var termid = document.getElementById("termid") ; var fg = document.getElementById("fg") ; //form1.submit(); if(pfg=="auto") { fg.value=pfg; if(termid.value=="") { alert("请选择学期!"); } else { form2.submit(); } } else{ fg.value=pfg; if(termid.value=="" || classid.value=="") { alert("请选择学期!"); }else { form2.submit(); } } } function autoPaike(){ var termid = document.getElementById("termid") ; if(termid.value=="") { alert("请选择学期!"); } else { var divMsg = document.getElementById("divMsg") ; createxmlhttprequest(); pUrl="../../OptCInfo"; pArgs="termid="+termid.value+"&fg=paike"; xmlhttp.open("post", pUrl, false); xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); //表单中enctype="multipart/form-data"的意思,是设置表单的多用途互联网邮件扩展类型。 //是设定某种扩展名的文件用一种应用程序来打开的方式类型.默认情况, //这个编码格式是application/x-www-form-urlencoded,不能用于文件上传 xmlhttp.send(pArgs); var mesg = xmlhttp.responseText; xmlhttp = null; var cIds = mesg.split(","); //alert(mesg); if(cIds[0]=="0") { divMsg.innerHTML=cIds[1]; } else { var strc=cIds.length - 1; //var strct=strc / 2 divMsg.innerHTML="<h2>自动排课开始,共有"+strc.toString()+"个班级,请耐心等待...</h2>"; var i=1; while (i <= strc) { autoPaikeClass(termid.value,cIds[i]); i = i + 1; } divMsg.innerHTML=divMsg.innerHTML+"<b>排课结束。</b>"; } //divMsg.innerHTML=divMsg.innerHTML+mesg; } } function autoPaikeClass(pTermid ,pClassid){ //alert(pTermid+pClassid); var divMsg = document.getElementById("divMsg") ; createxmlhttprequest(); pUrl="../../OptCInfo"; pArgs="termid="+pTermid+"&classid="+pClassid+"&fg=auto"; xmlhttp.open("post", pUrl, false); xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xmlhttp.send(pArgs); var mesg = xmlhttp.responseText; divMsg.innerHTML=divMsg.innerHTML+mesg; xmlhttp = null; }

 学期:    
 班级:
  </td>
  </tr>



var termid=document.getElementById("termid"); var d = new Date(); termid.value=String(d.getYear())+"0";


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();
    //}
    

    }

    }

weixin_45048213
weixin_45048213 老哥 会了吗 求助啊!
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐