├── .gitignore ├── .idea ├── libraries │ ├── poi_3_13_20150929.xml │ └── poi_3_13_20150929__2_.xml ├── uiDesigner.xml └── vcs.xml ├── README.md ├── image ├── 1.png ├── 2.png └── 3.png └── p2 ├── .classpath ├── .project ├── 1.xls ├── bin ├── p2 │ ├── FileChooser.class │ ├── GA$1.class │ ├── GA$Best.class │ ├── GA$createtable$1.class │ ├── GA$createtable$2.class │ ├── GA$createtable$3.class │ ├── GA$createtable$4$1.class │ ├── GA$createtable$4.class │ ├── GA$createtable$5.class │ ├── GA$createtable$6.class │ ├── GA$createtable$7.class │ ├── GA$createtable$8.class │ ├── GA$createtable$MyTable.class │ ├── GA$createtable.class │ ├── GA.class │ ├── file.class │ ├── main$1.class │ ├── main$Best.class │ ├── main$createtable$1.class │ ├── main$createtable$2.class │ ├── main$createtable$3.class │ ├── main$createtable$4.class │ ├── main$createtable$5$1.class │ ├── main$createtable$5.class │ ├── main$createtable$6.class │ ├── main$createtable$7.class │ ├── main$createtable$8.class │ ├── main$createtable$9.class │ ├── main$createtable$MyTable.class │ ├── main$createtable.class │ ├── main.class │ └── mm.class ├── poi-3.13-20150929.jar ├── poi-examples-3.13-20150929.jar ├── poi-excelant-3.13-20150929.jar ├── poi-ooxml-3.13-20150929.jar ├── poi-ooxml-schemas-3.13-20150929.jar └── poi-scratchpad-3.13-20150929.jar ├── lib ├── poi-3.13-20150929.jar ├── poi-examples-3.13-20150929.jar ├── poi-excelant-3.13-20150929.jar ├── poi-ooxml-3.13-20150929.jar ├── poi-ooxml-schemas-3.13-20150929.jar └── poi-scratchpad-3.13-20150929.jar ├── p2 遗传.lxe └── src └── p2 ├── Best.java ├── Course.java ├── EntryWindow.java ├── FileChooser.java ├── FileProcesser.java ├── GA.java ├── Mission.java ├── MyTable.java ├── Test.java ├── TextAreaCellRenderer.java └── Unit.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | p2/bin -------------------------------------------------------------------------------- /.idea/libraries/poi_3_13_20150929.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /.idea/libraries/poi_3_13_20150929__2_.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /.idea/uiDesigner.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # -JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm 2 | 3 | 使用前先把lib下的jar包导入到library里,然后直接运行EntryWindow.java即可 4 | 5 | p2 遗传是演示视频 用屏幕录像工具就能打开(很早以前的演示了, 可能和实际有所区别) 6 | 7 | p2.lxe is the example video which shows how it works. 8 | 9 | ### 排课结果 10 |
11 | 12 |
13 | 14 | ### 训练中: 15 |
16 | 17 |
18 | 19 | ### 查询课表: 20 |
21 | 22 |
23 | 24 | ### 数据格式 25 | 26 | p2目录下的1.xls 27 | 28 | ###s 编码 29 | 30 | 总共长25,表示某个**教师**在某个**时间**,某个**教室**教某个**班级**,取的都是map映射的值 31 | 32 | 0~5: 教师 33 | 34 | 5~10: 班级 35 | 36 | 10~15: 日期 (星期几 * 4+ 第几节课) 37 | 38 | 15~25: 地点 39 | -------------------------------------------------------------------------------- /image/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/image/1.png -------------------------------------------------------------------------------- /image/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/image/2.png -------------------------------------------------------------------------------- /image/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/image/3.png -------------------------------------------------------------------------------- /p2/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /p2/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | p2 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /p2/1.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/1.xls -------------------------------------------------------------------------------- /p2/bin/p2/FileChooser.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/FileChooser.class -------------------------------------------------------------------------------- /p2/bin/p2/GA$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/GA$1.class -------------------------------------------------------------------------------- /p2/bin/p2/GA$Best.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/GA$Best.class -------------------------------------------------------------------------------- /p2/bin/p2/GA$createtable$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/GA$createtable$1.class -------------------------------------------------------------------------------- /p2/bin/p2/GA$createtable$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/GA$createtable$2.class -------------------------------------------------------------------------------- /p2/bin/p2/GA$createtable$3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/GA$createtable$3.class -------------------------------------------------------------------------------- /p2/bin/p2/GA$createtable$4$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/GA$createtable$4$1.class -------------------------------------------------------------------------------- /p2/bin/p2/GA$createtable$4.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/GA$createtable$4.class -------------------------------------------------------------------------------- /p2/bin/p2/GA$createtable$5.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/GA$createtable$5.class -------------------------------------------------------------------------------- /p2/bin/p2/GA$createtable$6.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/GA$createtable$6.class -------------------------------------------------------------------------------- /p2/bin/p2/GA$createtable$7.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/GA$createtable$7.class -------------------------------------------------------------------------------- /p2/bin/p2/GA$createtable$8.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/GA$createtable$8.class -------------------------------------------------------------------------------- /p2/bin/p2/GA$createtable$MyTable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/GA$createtable$MyTable.class -------------------------------------------------------------------------------- /p2/bin/p2/GA$createtable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/GA$createtable.class -------------------------------------------------------------------------------- /p2/bin/p2/GA.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/GA.class -------------------------------------------------------------------------------- /p2/bin/p2/file.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/file.class -------------------------------------------------------------------------------- /p2/bin/p2/main$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/main$1.class -------------------------------------------------------------------------------- /p2/bin/p2/main$Best.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/main$Best.class -------------------------------------------------------------------------------- /p2/bin/p2/main$createtable$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/main$createtable$1.class -------------------------------------------------------------------------------- /p2/bin/p2/main$createtable$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/main$createtable$2.class -------------------------------------------------------------------------------- /p2/bin/p2/main$createtable$3.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/main$createtable$3.class -------------------------------------------------------------------------------- /p2/bin/p2/main$createtable$4.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/main$createtable$4.class -------------------------------------------------------------------------------- /p2/bin/p2/main$createtable$5$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/main$createtable$5$1.class -------------------------------------------------------------------------------- /p2/bin/p2/main$createtable$5.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/main$createtable$5.class -------------------------------------------------------------------------------- /p2/bin/p2/main$createtable$6.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/main$createtable$6.class -------------------------------------------------------------------------------- /p2/bin/p2/main$createtable$7.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/main$createtable$7.class -------------------------------------------------------------------------------- /p2/bin/p2/main$createtable$8.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/main$createtable$8.class -------------------------------------------------------------------------------- /p2/bin/p2/main$createtable$9.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/main$createtable$9.class -------------------------------------------------------------------------------- /p2/bin/p2/main$createtable$MyTable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/main$createtable$MyTable.class -------------------------------------------------------------------------------- /p2/bin/p2/main$createtable.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/main$createtable.class -------------------------------------------------------------------------------- /p2/bin/p2/main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/main.class -------------------------------------------------------------------------------- /p2/bin/p2/mm.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/p2/mm.class -------------------------------------------------------------------------------- /p2/bin/poi-3.13-20150929.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/poi-3.13-20150929.jar -------------------------------------------------------------------------------- /p2/bin/poi-examples-3.13-20150929.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/poi-examples-3.13-20150929.jar -------------------------------------------------------------------------------- /p2/bin/poi-excelant-3.13-20150929.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/poi-excelant-3.13-20150929.jar -------------------------------------------------------------------------------- /p2/bin/poi-ooxml-3.13-20150929.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/poi-ooxml-3.13-20150929.jar -------------------------------------------------------------------------------- /p2/bin/poi-ooxml-schemas-3.13-20150929.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/poi-ooxml-schemas-3.13-20150929.jar -------------------------------------------------------------------------------- /p2/bin/poi-scratchpad-3.13-20150929.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/bin/poi-scratchpad-3.13-20150929.jar -------------------------------------------------------------------------------- /p2/lib/poi-3.13-20150929.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/lib/poi-3.13-20150929.jar -------------------------------------------------------------------------------- /p2/lib/poi-examples-3.13-20150929.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/lib/poi-examples-3.13-20150929.jar -------------------------------------------------------------------------------- /p2/lib/poi-excelant-3.13-20150929.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/lib/poi-excelant-3.13-20150929.jar -------------------------------------------------------------------------------- /p2/lib/poi-ooxml-3.13-20150929.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/lib/poi-ooxml-3.13-20150929.jar -------------------------------------------------------------------------------- /p2/lib/poi-ooxml-schemas-3.13-20150929.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/lib/poi-ooxml-schemas-3.13-20150929.jar -------------------------------------------------------------------------------- /p2/lib/poi-scratchpad-3.13-20150929.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/lib/poi-scratchpad-3.13-20150929.jar -------------------------------------------------------------------------------- /p2/p2 遗传.lxe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zzzyyyxxxmmm/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/05534e3c4b30af6dea501d7aaba47c7637bd28fd/p2/p2 遗传.lxe -------------------------------------------------------------------------------- /p2/src/p2/Best.java: -------------------------------------------------------------------------------- 1 | package p2; 2 | 3 | /** 4 | * @author jikangwang 5 | */ 6 | public class Best { 7 | public int generations; 8 | public String str[]; 9 | private double fitness; 10 | public String x[] = new String[1000]; 11 | 12 | public Best() { 13 | } 14 | 15 | public void setFitness(double fitness) { 16 | this.fitness = fitness; 17 | } 18 | 19 | public int getGenerations() { 20 | return generations; 21 | } 22 | 23 | public void setGenerations(int generations) { 24 | this.generations = generations; 25 | } 26 | 27 | public String[] getStr() { 28 | return str; 29 | } 30 | 31 | public void setStr(String[] str) { 32 | this.str = str; 33 | } 34 | 35 | public double getFitness() { 36 | return fitness; 37 | } 38 | 39 | public String[] getX() { 40 | return x; 41 | } 42 | 43 | public void setX(String[] x) { 44 | this.x = x; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /p2/src/p2/Course.java: -------------------------------------------------------------------------------- 1 | package p2; 2 | 3 | /** 4 | * @author jikangwang 5 | */ 6 | public class Course { 7 | private String courseId; //课程ID 8 | private String courseName; //课程名称 9 | private String courseMajor; //专业 10 | private String courseClass; //班级 11 | private String time1; //理论课时 12 | private String time2; //实验课时 13 | private int teacherID; //任课教师ID 14 | private String teacherName; //任课教师姓名 15 | 16 | public Course() { 17 | 18 | } 19 | 20 | public Course(String courseId, String courseName, String courseMajor, String courseClass, int teacherID, String teacherName) { 21 | this.courseId = courseId; 22 | this.courseName = courseName; 23 | this.courseMajor = courseMajor; 24 | this.courseClass = courseClass; 25 | this.teacherID = teacherID; 26 | this.teacherName = teacherName; 27 | this.time1 = ""; 28 | this.time2 = ""; 29 | } 30 | 31 | public Course(String courseId, String courseName, String courseMajor, String courseClass, String time1, String time2, int teacherID, String teacherName) { 32 | this.courseId = courseId; 33 | this.courseName = courseName; 34 | this.courseMajor = courseMajor; 35 | this.courseClass = courseClass; 36 | this.time1 = time1; 37 | this.time2 = time2; 38 | this.teacherID = teacherID; 39 | this.teacherName = teacherName; 40 | } 41 | 42 | public String getCourseId() { 43 | return courseId; 44 | } 45 | 46 | public String getCourseName() { 47 | return courseName; 48 | } 49 | 50 | public String getCourseMajor() { 51 | return courseMajor; 52 | } 53 | 54 | public String getCourseClass() { 55 | return courseClass; 56 | } 57 | 58 | public String getTime1() { 59 | return time1; 60 | } 61 | 62 | public String getTime2() { 63 | return time2; 64 | } 65 | 66 | public int getTeacherID() { 67 | return teacherID; 68 | } 69 | 70 | public String getTeacherName() { 71 | return teacherName; 72 | } 73 | } -------------------------------------------------------------------------------- /p2/src/p2/EntryWindow.java: -------------------------------------------------------------------------------- 1 | package p2; 2 | 3 | import java.awt.Dimension; 4 | import java.awt.EventQueue; 5 | 6 | import javax.swing.*; 7 | 8 | import java.awt.BorderLayout; 9 | 10 | import java.awt.event.ActionListener; 11 | import java.awt.event.ActionEvent; 12 | import java.awt.event.WindowAdapter; 13 | import java.awt.event.WindowEvent; 14 | 15 | 16 | public class EntryWindow { 17 | 18 | 19 | public static final boolean DEBUG = false; 20 | 21 | 22 | private JFrame frame; 23 | private JPanel panel; 24 | private JButton startBruteButton; 25 | private JButton startGAButton; 26 | MyTable table = null; 27 | MyTable myTable = new MyTable(); 28 | 29 | 30 | public JTextField textField; 31 | private JLabel status; 32 | public String Dir; 33 | private GA ga; 34 | 35 | 36 | public static void main(String[] args) { 37 | EventQueue.invokeLater(new Runnable() { 38 | public void run() { 39 | try { 40 | 41 | 42 | EntryWindow window = new EntryWindow(); 43 | window.frame.setVisible(true); 44 | } catch (Exception e) { 45 | e.printStackTrace(); 46 | } 47 | } 48 | }); 49 | } 50 | 51 | /** 52 | * Create the application. 53 | */ 54 | public EntryWindow() { 55 | initialize(); 56 | } 57 | 58 | private void initialize() { 59 | 60 | frame = new JFrame(); 61 | frame.setBounds(100, 100, 450, 300); 62 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 63 | 64 | ga = new GA(); 65 | createTable(); 66 | 67 | 68 | ga.setTable(table.getTable()); 69 | ga.setMyTable(myTable); 70 | ga.setStatus(status); 71 | 72 | } 73 | 74 | 75 | private void createTable() { 76 | myTable = new MyTable(); 77 | table = myTable; 78 | // Data to be displayed in the JTable 79 | JScrollPane s = new JScrollPane(table.getTable()); 80 | frame.getContentPane().add(s, BorderLayout.CENTER); 81 | frame.setTitle("JAVA Course Scheduling System Based on Genetic Algorithm"); 82 | frame.pack(); 83 | frame.setVisible(true); 84 | frame.addWindowListener(new WindowAdapter() { 85 | public void windowClosing(WindowEvent e) { 86 | System.exit(0); 87 | } 88 | }); 89 | frame.setSize(1200,700); 90 | 91 | panel = new JPanel(); 92 | startBruteButton = new JButton("开始排课(暴力算法)"); 93 | frame.getContentPane().add(panel, BorderLayout.SOUTH); 94 | status = new JLabel("排课"); 95 | panel.add(status); 96 | status.setPreferredSize(new Dimension(150,15)); 97 | 98 | status.setText("排课"); 99 | 100 | startBruteButton.addActionListener(new ActionListener() { 101 | 102 | public void actionPerformed(ActionEvent arg0) { 103 | if(ga.getCoursesListSize()==0){ 104 | JOptionPane.showMessageDialog(null, "请先导入p2目录下的excel文件", "注意", JOptionPane.ERROR_MESSAGE); 105 | return; 106 | } 107 | myTable.clear(); 108 | table.getTable().repaint();//系统重新绘制表格 109 | ga.bruteForce(); 110 | 111 | } 112 | }); 113 | panel.add(startBruteButton); 114 | 115 | startGAButton = new JButton("开始排课(GA)"); 116 | 117 | startGAButton.addActionListener(new ActionListener() { 118 | 119 | public void actionPerformed(ActionEvent arg0) { 120 | if(ga.getCoursesListSize()==0){ 121 | JOptionPane.showMessageDialog(null, "请先导入p2目录下的excel文件", "注意", JOptionPane.ERROR_MESSAGE); 122 | return; 123 | } 124 | myTable.clear(); 125 | table.getTable().repaint();//系统重新绘制表格 126 | ga.start(); 127 | } 128 | }); 129 | panel.add(startGAButton); 130 | table.getTable().repaint();//系统重新绘制表格 131 | 132 | 133 | JMenuBar menuBar = new JMenuBar(); 134 | frame.setJMenuBar(menuBar); 135 | 136 | 137 | JMenu fmenu = new JMenu("文件"); 138 | menuBar.add(fmenu); 139 | JMenuItem fMenuItem1 = new JMenuItem("excel导入"); 140 | fMenuItem1.addActionListener(new ActionListener() { 141 | public void actionPerformed(ActionEvent Event) { 142 | FileChooser ab = new FileChooser(); 143 | Dir = ab.ac(); 144 | FileProcesser fileProcesser = new FileProcesser(); 145 | fileProcesser.read(Dir); 146 | ga.setCoursesList(fileProcesser.getCourseList()); 147 | 148 | //table.repaint(); 149 | } 150 | }); 151 | 152 | if (DEBUG) { 153 | Dir = "/Users/jikangwang/JavaProject/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/p2/1.xls"; 154 | FileProcesser fileProcesser = new FileProcesser(); 155 | fileProcesser.read(Dir); 156 | ga.setCoursesList(fileProcesser.getCourseList()); 157 | } 158 | 159 | fmenu.add(fMenuItem1); 160 | 161 | 162 | JMenu menu = new JMenu("操作"); 163 | menuBar.add(menu); 164 | 165 | JMenuItem menuItem1_1 = new JMenuItem("手动排课"); 166 | menuItem1_1.addActionListener(new ActionListener() { 167 | public void actionPerformed(ActionEvent arg0) { 168 | 169 | final JFrame frame1 = new JFrame(); 170 | frame1.setBounds(250, 250, 900, 200); 171 | frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 172 | frame1.setVisible(true); 173 | 174 | JPanel panel1 = new JPanel(); 175 | frame1.getContentPane().add(panel1, BorderLayout.CENTER); 176 | 177 | JLabel label = new JLabel("请输入需要修改的教师班级和时间"); 178 | panel1.add(label); 179 | 180 | textField = new JTextField(); 181 | panel1.add(textField); 182 | textField.setColumns(10); 183 | 184 | 185 | final JTextField textField_4 = new JTextField(); 186 | panel1.add(textField_4); 187 | textField_4.setColumns(10); 188 | 189 | final JTextField textField_5 = new JTextField(); 190 | panel1.add(textField_5); 191 | textField_5.setColumns(10); 192 | 193 | 194 | JPanel panel_2 = new JPanel(); 195 | frame1.getContentPane().add(panel_2, BorderLayout.SOUTH); 196 | frame1.setSize(1200,100); 197 | 198 | JLabel lblNewLabel = new JLabel("请输入时间和地点"); 199 | panel_2.add(lblNewLabel); 200 | 201 | final JTextField textField_1 = new JTextField(); 202 | panel_2.add(textField_1); 203 | textField_1.setColumns(10); 204 | 205 | 206 | final JTextField textField_2 = new JTextField(); 207 | panel_2.add(textField_2); 208 | textField_2.setColumns(10); 209 | 210 | JPanel panel_3 = new JPanel(); 211 | frame1.getContentPane().add(panel_3, BorderLayout.EAST); 212 | 213 | JLabel lblNewLabel_1 = new JLabel("日期按照天-课次 例如星期三第三四节课,输入3-2"); 214 | panel_3.add(lblNewLabel_1); 215 | 216 | JButton btnNewButton = new JButton("排课"); 217 | textField.setText("沈明玉"); 218 | textField_4.setText("计科-1"); 219 | textField_5.setText("1-1"); 220 | textField_1.setText("4-1"); 221 | textField_2.setText("1"); 222 | 223 | btnNewButton.addActionListener(new ActionListener() { 224 | public void actionPerformed(ActionEvent arg0) { 225 | String s1 = textField.getText(); 226 | String s2 = textField_4.getText(); 227 | String s3 = textField_5.getText(); 228 | String ss1 = textField_1.getText(); 229 | String ss2 = textField_2.getText(); 230 | boolean result=myTable.sp(s1, s2, s3, ss1, ss2); 231 | if(!result){ 232 | lblNewLabel_1.setText("未找到对应班级"); 233 | } else { 234 | lblNewLabel_1.setText("修改成功"); 235 | } 236 | table.getTable().repaint(); 237 | } 238 | 239 | }); 240 | panel1.add(btnNewButton); 241 | 242 | 243 | } 244 | }); 245 | 246 | 247 | menu.add(menuItem1_1); 248 | 249 | 250 | JMenu menu_1 = new JMenu("查询"); 251 | menuBar.add(menu_1); 252 | 253 | JMenuItem menuItem = new JMenuItem("按班级"); 254 | menuItem.addActionListener(new ActionListener() { 255 | public void actionPerformed(ActionEvent arg0) { 256 | queryDialog(0); 257 | 258 | } 259 | }); 260 | menu_1.add(menuItem); 261 | 262 | JMenuItem menuItem_1 = new JMenuItem("按教师"); 263 | menuItem_1.addActionListener(new ActionListener() { 264 | public void actionPerformed(ActionEvent arg0) { 265 | queryDialog(1); 266 | 267 | } 268 | }); 269 | menu_1.add(menuItem_1); 270 | 271 | JMenuItem menuItem_2 = new JMenuItem("按教室"); 272 | menuItem_2.addActionListener(new ActionListener() { 273 | public void actionPerformed(ActionEvent arg0) { 274 | queryDialog(2); 275 | 276 | } 277 | }); 278 | menu_1.add(menuItem_2); 279 | 280 | JMenuItem menuItem_3 = new JMenuItem("恢复"); 281 | menuItem_3.addActionListener(new ActionListener() { 282 | public void actionPerformed(ActionEvent arg0) { 283 | myTable.restore(); 284 | table.getTable().repaint(); 285 | } 286 | }); 287 | menu_1.add(menuItem_3); 288 | 289 | JMenu menu_2 = new JMenu("帮助"); 290 | menuBar.add(menu_2); 291 | 292 | 293 | } 294 | 295 | private void queryDialog(final int queryType) { 296 | final JFrame frame1 = new JFrame(); 297 | frame1.setBounds(500, 500, 200, 100); 298 | frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 299 | frame1.setVisible(true); 300 | 301 | JPanel panel1 = new JPanel(); 302 | frame1.getContentPane().add(panel1, BorderLayout.CENTER); 303 | JLabel label = new JLabel(); 304 | 305 | 306 | if (queryType == 0) 307 | label.setText("请输入需要查询的班级"); 308 | else if (queryType == 1) 309 | label.setText("请输入需要查询的教师"); 310 | else if (queryType == 2) 311 | label.setText("请输入需要查询的教室"); 312 | 313 | panel1.add(label); 314 | 315 | textField = new JTextField(); 316 | panel1.add(textField); 317 | textField.setColumns(10); 318 | 319 | JButton btnNewButton = new JButton("查询"); 320 | btnNewButton.addActionListener(new ActionListener() { 321 | public void actionPerformed(ActionEvent arg0) { 322 | 323 | myTable.clear(); 324 | String xx = textField.getText(); 325 | if (queryType == 0) 326 | ga.queryByClassId(xx); 327 | else if (queryType == 1) 328 | ga.queryByTeacherName(xx); 329 | else if (queryType == 2) 330 | ga.queryByClassRoom(xx); 331 | table.getTable().repaint(); 332 | } 333 | 334 | }); 335 | panel1.add(btnNewButton); 336 | 337 | table.getTable().repaint(); 338 | 339 | } 340 | 341 | } -------------------------------------------------------------------------------- /p2/src/p2/FileChooser.java: -------------------------------------------------------------------------------- 1 | package p2; 2 | import java.awt.event.ActionEvent; 3 | import java.awt.event.ActionListener; 4 | import java.io.File; 5 | 6 | import javax.swing.JButton; 7 | import javax.swing.JFileChooser; 8 | import javax.swing.JFrame; 9 | import javax.swing.JLabel; 10 | 11 | public class FileChooser extends JFrame implements ActionListener{ 12 | JButton open=null; 13 | public static void main(String[] args) { 14 | new FileChooser(); 15 | } 16 | public FileChooser(){ 17 | } 18 | 19 | public void actionPerformed(ActionEvent e) { 20 | // TODO Auto-generated method stub 21 | JFileChooser jfc=new JFileChooser(); 22 | jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES ); 23 | jfc.showDialog(new JLabel(), "选择"); 24 | File file=jfc.getSelectedFile(); 25 | if(file.isDirectory()){ 26 | System.out.println("文件夹:"+file.getAbsolutePath()); 27 | }else if(file.isFile()){ 28 | System.out.println("文件:"+file.getAbsolutePath()); 29 | } 30 | System.out.println(jfc.getSelectedFile().getName()); 31 | } 32 | public String ac(){ 33 | JFileChooser jfc=new JFileChooser(); 34 | jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES ); 35 | jfc.showDialog(new JLabel(), "选择"); 36 | File file=jfc.getSelectedFile(); 37 | if(file.isDirectory()){ 38 | System.out.println("文件夹:"+file.getAbsolutePath()); 39 | }else if(file.isFile()){ 40 | System.out.println("文件:"+file.getAbsolutePath()); 41 | } 42 | System.out.println(jfc.getSelectedFile().getName()); 43 | return file.getAbsolutePath(); 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /p2/src/p2/FileProcesser.java: -------------------------------------------------------------------------------- 1 | package p2; 2 | 3 | import java.io.FileInputStream; 4 | import java.io.FileOutputStream; 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.util.ArrayList; 8 | 9 | import org.apache.poi.ss.usermodel.Cell; 10 | import org.apache.poi.ss.usermodel.DateUtil; 11 | import org.apache.poi.ss.usermodel.Row; 12 | import org.apache.poi.ss.usermodel.Workbook; 13 | import org.apache.poi.ss.usermodel.WorkbookFactory; 14 | 15 | public class FileProcesser { 16 | 17 | /** 18 | * @param dir 19 | * @param wb 20 | * @throws IOException 21 | */ 22 | public static Workbook wb; 23 | ArrayList courseList = new ArrayList<>(); 24 | 25 | public static void save(String dir, Object[][] p) throws IOException { 26 | // TODO Auto-generated method stub 27 | 28 | 29 | try { 30 | InputStream inp = new FileInputStream(dir); 31 | wb = WorkbookFactory.create(inp); 32 | org.apache.poi.ss.usermodel.Sheet sheet1 = wb.getSheetAt(0); 33 | int rowStart = sheet1.getFirstRowNum(); 34 | int rowEnd = sheet1.getLastRowNum(); 35 | Row tr = sheet1.getRow(0); 36 | int ColumnEnd = tr.getLastCellNum(); 37 | String s[] = new String[ColumnEnd]; 38 | for (int i = 0; i < ColumnEnd; i++) { 39 | Cell c = tr.getCell(i, Row.RETURN_BLANK_AS_NULL); 40 | String temps = c.getRichStringCellValue().getString(); 41 | 42 | s[i] = temps; 43 | } 44 | 45 | for (int rowNum = rowStart + 1; rowNum <= rowEnd; rowNum++) { 46 | Row r = sheet1.getRow(rowNum); 47 | int lastColumn = r.getLastCellNum(); 48 | for (int cn = 0; cn < lastColumn; cn++) { 49 | Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL); 50 | switch (c.getCellType()) { 51 | case Cell.CELL_TYPE_STRING: 52 | 53 | // System.out.println(c.getRichStringCellValue().getString()); 54 | p[rowNum - 1][cn] = (Object) c.getRichStringCellValue().getString(); 55 | break; 56 | case Cell.CELL_TYPE_NUMERIC: 57 | if (DateUtil.isCellDateFormatted(c)) { 58 | // System.out.println(c.getDateCellValue()); 59 | c.setCellValue(p[rowNum - 1][cn].toString()); 60 | } else { 61 | //System.out.println(c.getNumericCellValue()); 62 | c.setCellValue(p[rowNum - 1][cn].toString()); 63 | } 64 | break; 65 | case Cell.CELL_TYPE_BOOLEAN: 66 | // System.out.println(c.getBooleanCellValue()); 67 | c.setCellValue(p[rowNum - 1][cn].toString()); 68 | break; 69 | case Cell.CELL_TYPE_FORMULA: 70 | // System.out.println(c.getCellFormula()); 71 | c.setCellValue(p[rowNum - 1][cn].toString()); 72 | break; 73 | default: 74 | System.out.println(); 75 | } 76 | } 77 | } 78 | 79 | } catch (Exception e1) { 80 | e1.printStackTrace(); 81 | } 82 | FileOutputStream out = null; 83 | out = new FileOutputStream(dir); 84 | wb.write(out); 85 | } 86 | 87 | public void read(final String Dir) { 88 | 89 | try { 90 | 91 | InputStream inp = new FileInputStream(Dir); 92 | Workbook wb = WorkbookFactory.create(inp); 93 | org.apache.poi.ss.usermodel.Sheet sheet1 = wb.getSheetAt(0); 94 | int rowStart = sheet1.getFirstRowNum(); 95 | int rowEnd = sheet1.getLastRowNum(); 96 | Row tr = sheet1.getRow(0); 97 | int ColumnEnd = tr.getLastCellNum(); 98 | String s[] = new String[ColumnEnd]; 99 | for (int i = 0; i < ColumnEnd; i++) { 100 | Cell c = tr.getCell(i, Row.RETURN_BLANK_AS_NULL); 101 | String temps = c.getRichStringCellValue().getString(); 102 | s[i] = temps; 103 | } 104 | 105 | Object[][] cellData=new Object[rowEnd-rowStart][10]; 106 | for (int rowNum = rowStart + 1; rowNum <= rowEnd; rowNum++) { 107 | Row r = sheet1.getRow(rowNum); 108 | int lastColumn = r.getLastCellNum(); 109 | for (int cn = 0; cn < lastColumn; cn++) { 110 | Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL); 111 | switch (c.getCellType()) { 112 | case Cell.CELL_TYPE_STRING: 113 | //System.out.println(c.getRichStringCellValue().getString()); 114 | cellData[rowNum - 1][cn] = c.getRichStringCellValue().getString(); 115 | break; 116 | case Cell.CELL_TYPE_NUMERIC: 117 | if (DateUtil.isCellDateFormatted(c)) { 118 | // System.out.println(c.getDateCellValue()); 119 | cellData[rowNum - 1][cn] = c.getDateCellValue(); 120 | } else { 121 | // System.out.println(c.getNumericCellValue()); 122 | cellData[rowNum - 1][cn] = (int) c.getNumericCellValue(); 123 | } 124 | break; 125 | case Cell.CELL_TYPE_BOOLEAN: 126 | // System.out.println(c.getBooleanCellValue()); 127 | cellData[rowNum - 1][cn] = c.getBooleanCellValue(); 128 | break; 129 | case Cell.CELL_TYPE_FORMULA: 130 | // System.out.println(c.getCellFormula()); 131 | cellData[rowNum - 1][cn] = c.getCellFormula(); 132 | break; 133 | default: 134 | System.out.println(); 135 | } 136 | } 137 | } 138 | getCourseObjectFromCell(cellData, rowEnd - rowStart); 139 | 140 | } catch (Exception e1) { 141 | e1.printStackTrace(); 142 | } 143 | } 144 | 145 | public ArrayList getCourseList() { 146 | return courseList; 147 | } 148 | 149 | private void getCourseObjectFromCell(Object[][] cellData, int rowNum) { 150 | for (int i = 0; i < rowNum; i++) { 151 | 152 | Course course = new Course(cellData[i][0].toString(), cellData[i][1].toString(), cellData[i][2].toString(), 153 | cellData[i][3].toString(), (cellData[i][4].toString()), (cellData[i][5].toString()), Integer.valueOf(cellData[i][6].toString()), 154 | cellData[i][7].toString()); 155 | 156 | courseList.add(course); 157 | } 158 | } 159 | } -------------------------------------------------------------------------------- /p2/src/p2/GA.java: -------------------------------------------------------------------------------- 1 | package p2; 2 | 3 | import javafx.util.Pair; 4 | 5 | import javax.swing.*; 6 | import java.util.*; 7 | 8 | /** 9 | * @author jikangwang 10 | */ 11 | public class GA { 12 | 13 | public static final int IterationTimes = 1000; 14 | public static final String CLASSROOM_NUMBER_BINARY = "0000000111"; 15 | public int opp = 0; 16 | private Best best; // 记录最佳答案的对象 17 | private final int POP_SIZE = 30; // 种群大小(本题的种群由30个个体(即x)组成) 18 | private String[][] pop = new String[POP_SIZE + 5][1000]; // String型数组,存放种群中每个个体(即x)的编码 19 | private String[][] result = new String[POP_SIZE + 5][1000]; // double型数组,经过进化后的种群中的每个个体(即x) 20 | private int[] fitness = new int[POP_SIZE + 5];//double型,存放种群中每个个体(即x)的适应值 21 | private final int LENGTH = 22; // x的编码长度,因为要精确到小数点后六位,所以十进制的整数部分(1位)与小数部分(6位)共有7位。这需要用22位的二进制数表示。 22 | private final int conversionFactor = 4194303;//转换因子,22位二进制数所能表示最大的十进制数为2^22-1 23 | private Random random = new Random(); // 用于产生随机数的工具 24 | private static final double PM = 0.01; // 变异率 25 | private double[] p = new double[POP_SIZE + 5];// 轮盘赌方法个体适应度概率 26 | private double[] q = new double[POP_SIZE + 5];// q[i]是前n项p之和 27 | private int[] unfi = new int[POP_SIZE + 5]; 28 | private int k1, k2; //被选出来杂交的两个个体 29 | 30 | public int getCoursesListSize() { 31 | return coursesList.size(); 32 | } 33 | 34 | ArrayList coursesList = new ArrayList<>(); //原本的a 35 | private int misNum = 0; //总共需要排的任务数,也就是coursesList.size() 36 | 37 | private Best best1; 38 | 39 | private Map mapt = new HashMap(); 40 | private Map mapc = new HashMap(); 41 | private Map rmapt = new HashMap(); 42 | private Map rmapc = new HashMap(); 43 | private Map mmap = new HashMap(); 44 | 45 | JTable table = null; 46 | MyTable myTable = null; 47 | 48 | JLabel status; 49 | 50 | private int cishu = 0; 51 | Course aa[][][] = new Course[100][100][100]; 52 | Course w[] = new Course[10000]; 53 | ArrayList missionList = new ArrayList<>(); 54 | 55 | private static final int DAY_NUM = 5; 56 | private static final int COURSE_NUM = 4; 57 | private static final int CLASS_NUM = 7; 58 | 59 | private static final int OVERRIDE_NUM=5; //最佳种群覆盖数 60 | 61 | public GA() { 62 | best1 = new Best(); 63 | } 64 | 65 | public void setTable(JTable table) { 66 | this.table = table; 67 | } 68 | 69 | public void setMyTable(MyTable myTable) { 70 | this.myTable = myTable; 71 | } 72 | 73 | public void setCoursesList(ArrayList coursesList) { 74 | this.coursesList = coursesList; 75 | this.misNum = coursesList.size(); 76 | } 77 | 78 | public void setStatus(JLabel status) { 79 | this.status = status; 80 | } 81 | 82 | public void bruteForce() { 83 | 84 | System.out.println("start bruteforce..."); 85 | missionList.clear(); 86 | table.repaint(); 87 | boolean vis[] = new boolean[10000]; 88 | //弱弱的暴力算法已淘汰 89 | for (int i = 0; i < 9999; i++) { 90 | w[i] = new Course(); 91 | } 92 | for (int i = 0; i < 100; i++) { 93 | for (int j = 0; j < 100; j++) { 94 | for (int k = 0; k < 100; k++) { 95 | aa[i][j][k] = new Course(); 96 | } 97 | } 98 | } 99 | 100 | for (int pp = 0; pp <= 5; pp++) { 101 | //周一至周五 102 | for (int i = 1; i <= DAY_NUM; i++) { 103 | //目前每天只排四节课 104 | for (int j = 1; j <= COURSE_NUM; j++) { 105 | 106 | for (int k = 0; k < CLASS_NUM; k++) { 107 | 108 | for (int ii = 0; ii < misNum; ii++) { //尝试排ii号任务 109 | if (vis[ii]) continue; 110 | if (check(i, j, k, ii)) { //检查是否和之前排过的课程发生冲突 111 | 112 | aa[i][j][k] = coursesList.get(ii); 113 | 114 | String teacherName = coursesList.get(ii).getTeacherName(); 115 | int day = j - 1; 116 | int time = i; 117 | String courseName = coursesList.get(ii).getCourseName(); 118 | int classRoom = k; 119 | String classId = coursesList.get(ii).getCourseClass(); 120 | Mission mission = new Mission(teacherName, courseName, String.valueOf(day), String.valueOf(time), String.valueOf(classRoom), classId); 121 | missionList.add(mission); 122 | 123 | myTable.appendValueAt(j - 1, i, mission); 124 | vis[ii] = true; 125 | break; 126 | } else { 127 | continue; 128 | } 129 | 130 | } 131 | 132 | } 133 | } 134 | } 135 | for (int ii = 0; ii < misNum; ii++) { //尝试排ii号任务 136 | if (!vis[ii]) { 137 | System.out.println(coursesList.get(ii).getCourseName()); 138 | } 139 | } 140 | myTable.sava(); 141 | table.repaint();//系统重新绘制表格 142 | } 143 | } 144 | 145 | 146 | int max = -999999999; 147 | 148 | /* 149 | * 取得结果 150 | */ 151 | private double findBestOne() { 152 | 153 | for (int i = 0; i < POP_SIZE; i++) { 154 | 155 | if (fitness[i] > max) { 156 | max = fitness[i]; 157 | best1.setFitness(max); 158 | for (int j = 0; j < misNum; j++) { 159 | //System.out.println("----"+result[i][j]); 160 | best1.x[j] = result[i][j]; 161 | } 162 | best1.str = pop[i]; 163 | 164 | } 165 | } 166 | return max; 167 | } 168 | 169 | public void fitness() { 170 | 171 | int Max = Integer.MIN_VALUE; 172 | int fit1 = 0; 173 | int sum = 0; 174 | int conflict = 0; //种群所有个体总冲突 175 | int bestConflict = Integer.MAX_VALUE; //最佳种群冲突数 176 | 177 | //最佳种群会覆盖掉 178 | //System.out.println(best1.x[0]); 179 | if (cishu >= 10) { 180 | for (int w = 0; w < OVERRIDE_NUM; w++) { 181 | for (int j = 0; j < misNum; j++) { 182 | result[opp + w][j] = best1.x[j]; 183 | } 184 | } 185 | } 186 | 187 | ArrayList> fit=new ArrayList<>(); 188 | 189 | for (int i = 0; i < POP_SIZE; i++) { 190 | Map map1 = new HashMap(); 191 | Map map2 = new HashMap(); 192 | Map map3 = new HashMap(); 193 | fit1 = 0; //不能在同一天一个班有相同的课 194 | int tot1 = 0; 195 | int tot2 = 0; 196 | int tot3 = 0; 197 | int t = 0; 198 | for (int j = 0; j < misNum; j++) { 199 | if (!map1.containsKey(result[i][j].substring(10, 25))) { //时间 教室 200 | map1.put(result[i][j].substring(10, 25), tot1); 201 | tot1++; 202 | } else { 203 | conflict++; 204 | t++; 205 | } 206 | if (!map2.containsKey(result[i][j].substring(5, 15))) { //班级 时间 207 | map2.put(result[i][j].substring(5, 15), tot2); 208 | tot2++; 209 | } else { 210 | conflict++; 211 | t++; 212 | } 213 | if (!map3.containsKey(result[i][j].substring(0, 5) + result[i][j].substring(10, 15))) { //教师 时间 214 | map3.put(result[i][j].substring(0, 5) + result[i][j].substring(10, 15), tot3); 215 | tot3++; 216 | } else { 217 | conflict++; 218 | t++; 219 | } 220 | for (int k = j + 1; k < misNum; k++) { 221 | /* 222 | * 223 | * 这里加上一个班级不能在同一天上相同的课 224 | * 225 | * 226 | */ 227 | if (result[i][j].substring(5, 10).equals(result[i][k].substring(5, 10)) && result[i][j].substring(0, 5).equals(result[i][k].substring(0, 5))) { //学生相同,并且课程相同 228 | int t1 = 0; 229 | for (int ii = 4; ii >= 0; ii--) { 230 | if (result[i][j].substring(10, 15).charAt(ii) == '1') t1 += Math.pow(2, 4 - j); 231 | } 232 | int t2 = 0; 233 | for (int ii = 4; ii <= 0; ii--) { 234 | if (result[i][k].substring(10, 15).charAt(ii) == '1') t2 += Math.pow(2, 4 - j); 235 | } 236 | t1 /= 4; 237 | t2 /= 4; 238 | if (Math.abs(t1 - t2) >= 2) { 239 | fit1++; 240 | } 241 | } 242 | } 243 | } 244 | 245 | bestConflict = Math.min(bestConflict, t); 246 | fitness[i] = 1000 + fit1 * 5 - t * 50; 247 | fit.add(new Pair(i,fitness[i])); 248 | 249 | } 250 | 251 | Collections.sort(fit, new Comparator>() { 252 | @Override 253 | public int compare(Pair o1, Pair o2) { 254 | return o2.getValue()-o1.getValue(); 255 | } 256 | }); 257 | opp += OVERRIDE_NUM; 258 | opp %= POP_SIZE; 259 | 260 | 261 | double base=0.5; //最大概率 262 | for (int i = 0; i < POP_SIZE; i++) { 263 | p[fit.get(i).getKey()] = base; 264 | base=base/2; 265 | } 266 | 267 | 268 | 269 | cishu++; 270 | System.out.println("繁殖次数:" + cishu + " " + "适应度:" + fit.get(0).getValue() + " 冲突数量:" + conflict + " 最佳个体冲突数:" + bestConflict); 271 | for (int i = 0; i < POP_SIZE; i++) { 272 | for (int j = 0; j < misNum; j++) { 273 | pop[i][j] = result[i][j]; 274 | } 275 | } 276 | 277 | } 278 | 279 | /* 280 | * 编码方法,将解值表示为二进制字节码形式 281 | */ 282 | public void encoding() { 283 | 284 | 285 | ArrayList encodedCourseString = new ArrayList<>(); 286 | int tott = 0; 287 | int totc = 0; 288 | int mis = 0; 289 | int ii = 1; 290 | int jj = 0; 291 | for (int i = 0; i < misNum; i++) { 292 | if (!mapt.containsKey(coursesList.get(i).getTeacherName())) { 293 | mmap.put(coursesList.get(i).getTeacherName(), coursesList.get(i).getCourseName()); 294 | } 295 | if (!mapt.containsKey(coursesList.get(i).getTeacherName())) { 296 | rmapt.put(tott, coursesList.get(i).getTeacherName()); 297 | mapt.put(coursesList.get(i).getTeacherName(), tott); 298 | tott++; 299 | } 300 | int t1 = mapt.get(coursesList.get(i).getTeacherName()).hashCode(); 301 | String s1 = ""; 302 | int temp = t1; 303 | int w = 5; 304 | while (true) { 305 | s1 = String.valueOf((temp & 1)) + s1; 306 | temp >>= 1; 307 | w--; 308 | if (temp == 0) break; 309 | } 310 | while (w != 0) { 311 | s1 = "0" + s1; 312 | w--; 313 | } 314 | 315 | if (mapc.containsKey(coursesList.get(i).getCourseClass()) == false) { 316 | rmapc.put(totc, coursesList.get(i).getCourseClass()); 317 | mapc.put(coursesList.get(i).getCourseClass(), totc); 318 | totc++; 319 | } 320 | int t2 = mapc.get(coursesList.get(i).getCourseClass()).hashCode(); 321 | String s2 = ""; 322 | temp = t2; 323 | w = 5; 324 | while (true) { 325 | s2 = String.valueOf((temp & 1)) + s2; 326 | temp >>= 1; 327 | w--; 328 | if (temp == 0) break; 329 | } 330 | while (w != 0) { 331 | s2 = "0" + s2; 332 | w--; 333 | } 334 | 335 | //System.out.println(s1+" "+s2); 336 | 337 | 338 | String ss1 = ""; 339 | temp = ii; 340 | w = 5; 341 | while (true) { 342 | ss1 = String.valueOf((temp & 1)) + ss1; 343 | temp >>= 1; 344 | w--; 345 | if (temp == 0) break; 346 | } 347 | while (w != 0) { 348 | ss1 = "0" + ss1; 349 | w--; 350 | } 351 | //System.out.println("ss1="+ss1); 352 | String ss2 = ""; 353 | temp = jj; 354 | w = 10; 355 | while (true) { 356 | ss2 = String.valueOf((temp & 1)) + ss2; 357 | temp >>= 1; 358 | w--; 359 | if (temp == 0) break; 360 | } 361 | while (w != 0) { 362 | ss2 = "0" + ss2; 363 | w--; 364 | } 365 | 366 | //System.out.println(s1+s2); 367 | encodedCourseString.add(s1 + s2 +/*ss1+ss2*/"000010000000001"); 368 | 369 | 370 | if (ii == 19 && jj == 4) { 371 | continue; 372 | } 373 | ii++; 374 | if (ii == 19) { 375 | jj++; 376 | ii %= 19; 377 | } 378 | 379 | } 380 | 381 | for (int i = 0; i < POP_SIZE; i++) { 382 | for (int j = 0; j < misNum; j++) 383 | result[i][j] = encodedCourseString.get(j); 384 | } 385 | } 386 | 387 | /* 388 | * 选择操作,依据轮盘赌算法从种群中选出两个个体进行杂交。 389 | */ 390 | public void selection() { 391 | do { 392 | k1 = roulettewheel(); 393 | k2 = roulettewheel(); 394 | } while (k1 != k2); 395 | } 396 | 397 | /* 398 | * 轮盘赌算法,适应值大的个体被选中的机率更大些。 399 | */ 400 | 401 | int roulettewheel() { 402 | double m = 0; 403 | double r = random.nextDouble(); //r为0至1的随机数 404 | int i = 0; 405 | int flag = 0; 406 | int max = 9999999; 407 | for (i = 0; i < POP_SIZE; i++) { 408 | /* 产生的随机数在m~m+P[i]间则认为选中了i 409 | * 因此i被选中的概率是P[i] 410 | */ 411 | m = m + p[i]; 412 | if (r <= m) 413 | break; 414 | } 415 | 416 | //选择性淘汰 417 | 418 | return i; 419 | } 420 | 421 | /* 422 | * 解码方法,将二进制字节码还原 423 | */ 424 | public void decoding() { 425 | int i, j; 426 | for (i = 0; i < POP_SIZE; i++) { 427 | for (j = 0; j < misNum; j++) { 428 | 429 | result[i][j] = pop[i][j]; 430 | } 431 | } 432 | 433 | } 434 | 435 | /* 436 | * 交叉操作 437 | */ 438 | public void crossover() { 439 | //随机产生是染色体发生交叉操作的位置 440 | int position1 = random.nextInt(29); 441 | int position2 = random.nextInt(29); 442 | //s1字符串被切成s11和s12,s2字符串被切成s21,s22 443 | String s11 = null, s12 = null, s21 = null, s22 = null; 444 | //System.out.println(pop[k1][position1]+" "+k1+" "+k2); 445 | s11 = pop[k1][position1].substring(0, 15); 446 | s12 = pop[k1][position1].substring(15, 25); 447 | s21 = pop[k2][position2].substring(0, 15); 448 | s22 = pop[k2][position2].substring(15, 25); 449 | //重新拼接字符串,并放入种群 450 | pop[k1][position1] = s11 + s22; 451 | pop[k2][position2] = s21 + s12; 452 | 453 | 454 | } 455 | 456 | /* 457 | * 变异操作,变异在染色体上的每个基因都可能发生 458 | */ 459 | public void mutation() { 460 | //第i个个体(即染色体) 461 | for (int i = 0; i < POP_SIZE; i++) { 462 | //第i个个体第j号基因 463 | for (int j = 0; j < misNum; j++) { 464 | double k = random.nextDouble(); 465 | //如果产生的随机数k小于变异率,则进行变异操作。 466 | if (0.5 > k) { 467 | mutation(i, j); 468 | } 469 | } 470 | } 471 | } 472 | 473 | //如果基因的位置是“1”则换为“0”。如果基因的位置是“0”则换为“1”。 474 | public void mutation(int i, int j) { 475 | 476 | //只更改教学时间和教学地点,不更改教师和课程 477 | String s = pop[i][j]; 478 | //随机选择更改的基因 479 | 480 | //System.out.println("变异前"+s); 481 | StringBuffer sb = new StringBuffer(s); 482 | 483 | String w = s.substring(0, 10); 484 | String w1 = (String) s.subSequence(10, 15); //教学时间 一定是小于20的,因此变异时注意控制 485 | StringBuffer sb1 = new StringBuffer(w1); 486 | while (true) { 487 | int p1 = random.nextInt(5); //时间更改 488 | //System.out.println(sb1+" "+p1); 489 | if (sb1.charAt(p1) == '0') 490 | sb1.setCharAt(p1, '1'); 491 | else 492 | sb1.setCharAt(p1, '0'); 493 | w1 = sb1.toString(); 494 | if (w1.compareTo("10011") < 1) { //符合变异结果 495 | break; 496 | } 497 | } 498 | 499 | String w2 = (String) s.subSequence(15, 25); //教学地点 一定是小于20的,因此变异时注意控制 500 | StringBuffer sb2 = new StringBuffer(w2); 501 | while (true) { 502 | int p2 = random.nextInt(10); //时间更改 503 | //System.out.println(sb2+" "+p2); 504 | if (sb2.charAt(p2) == '0') 505 | sb2.setCharAt(p2, '1'); 506 | else 507 | sb2.setCharAt(p2, '0'); 508 | w2 = sb2.toString(); 509 | if (w2.compareTo(CLASSROOM_NUMBER_BINARY) < 1) { //符合变异结果 510 | break; 511 | } 512 | } 513 | s = w + w1 + w2; 514 | //System.out.println("变异后"+s); 515 | pop[i][j] = s; 516 | 517 | 518 | 519 | 520 | } 521 | 522 | /* 523 | * 一次进化 524 | */ 525 | public void evolution() { 526 | 527 | fitness(); //计算适应度 528 | selection(); //选择 529 | crossover(); //交叉 530 | 531 | mutation(); //变异 532 | 533 | decoding(); //解码 534 | } 535 | 536 | /* 537 | * 整个进化过程,n 表示进化多少代 538 | */ 539 | public void dispose(int n) { 540 | 541 | for (int i = 0; i <= n; i++) { 542 | 543 | 544 | if(i%100==0) status.setText(i+"/"+IterationTimes); 545 | status.paintImmediately(status.getVisibleRect()); 546 | evolution(); 547 | 548 | findBestOne(); 549 | } 550 | } 551 | 552 | /* 553 | * 保存最佳个体的对象 554 | */ 555 | 556 | 557 | private boolean check(int x, int y, int cl, int z) { //检查是否和之前排过的课程发生冲突 558 | int i, j, k; 559 | 560 | Course ww = coursesList.get(z); 561 | boolean flag = true; 562 | 563 | //确定在这个班没有其他课,确定这个老师在其他教师也没课 564 | for (k = 0; k < cl; k++) { 565 | if (aa[x][y][k].getCourseClass() == null) continue; 566 | 567 | if (aa[x][y][k].getCourseClass().equals(ww.getCourseClass()) || aa[x][y][k].getCourseName().equals(ww.getCourseName())) { 568 | return false; 569 | } 570 | } 571 | 572 | 573 | int ch[] = new int[5]; 574 | ch[0] = x - 1; 575 | ch[1] = x + 1; 576 | ch[2] = x; 577 | //System.out.println("****"); 578 | for (int ii = 0; ii < 3; ii++) { 579 | int qq = ch[ii]; 580 | if (qq < 1 || qq > 5) continue; 581 | 582 | for (j = 1; j <= 4; j++) { 583 | for (k = 0; k < 7; k++) { 584 | if (aa[qq][j][k].getCourseClass() == null) continue; 585 | //System.out.println(aa[i][j][k].cl+" "+aa[i][j][k].courseName+" "+i+" "+j+" "+k); 586 | if (aa[qq][j][k].getCourseClass().equals(ww.getCourseClass()) && aa[qq][j][k].getCourseName().equals(ww.getCourseName())) { //在间隔不到一天的时间内,存在给相同的班上相同的课 587 | flag = false; 588 | } 589 | } 590 | } 591 | } 592 | 593 | return flag; 594 | 595 | } 596 | 597 | private void init() { 598 | //String d[] = {"0000100001000010000000001" ,"0000100010000010000000001","0001000010000010000000001","0001000010000010000000001"}; 599 | // 初始化初始种群及其他数据 600 | System.out.println("种群进化中...."); 601 | encoding(); //编码 602 | // 进化,这里进化10000次 603 | dispose(IterationTimes); 604 | 605 | findBestOne(); //取得结果 606 | System.out.println("+++++++++++++++++++++++++++结果为:"); 607 | missionList.clear(); 608 | for (int i = 0; i < misNum; i++) { 609 | String a1 = best1.x[i].substring(0, 5); //教师 610 | int t1 = 0; 611 | for (int j = a1.length() - 1; j >= 0; j--) { 612 | if (a1.charAt(j) == '1') { 613 | t1 += Math.pow(2, a1.length() - 1 - j); 614 | } 615 | } 616 | String a2 = best1.x[i].substring(5, 10); //班级 617 | int t2 = 0; 618 | for (int j = a2.length() - 1; j >= 0; j--) { 619 | if (a2.charAt(j) == '1') { 620 | t2 += Math.pow(2, a2.length() - 1 - j); 621 | } 622 | } 623 | String a3 = best1.x[i].substring(10, 15); //日期 624 | int t3 = 0; 625 | for (int j = a3.length() - 1; j >= 0; j--) { 626 | if (a3.charAt(j) == '1') { 627 | t3 += Math.pow(2, a3.length() - 1 - j); 628 | } 629 | } 630 | String a4 = best1.x[i].substring(15, 25); //教室 631 | int t4 = 0; 632 | for (int j = a4.length() - 1; j >= 0; j--) { 633 | if (a4.charAt(j) == '1') { 634 | t4 += Math.pow(2, a4.length() - 1 - j); 635 | } 636 | } 637 | //System.out.println(t1+" "+t2+" "+t3+" "+t4); 638 | 639 | String teacherName = (String) rmapt.get(t1); 640 | int day = t3 % COURSE_NUM; 641 | int time = t3 / COURSE_NUM + 1; 642 | String courseName = (String) mmap.get((String) rmapt.get(t1)); 643 | int classRoom = t4; 644 | String classId = (String) rmapc.get(t2); 645 | Mission mission = new Mission(teacherName, courseName, String.valueOf(day), String.valueOf(time), String.valueOf(classRoom), classId); 646 | missionList.add(mission); 647 | myTable.appendValueAt(day, time, mission); 648 | myTable.sava(); 649 | } 650 | System.out.println("f=" + best1.getFitness()); 651 | } 652 | 653 | public void start() { 654 | init(); 655 | } 656 | 657 | 658 | void queryByClassId(String id) { 659 | for (Mission m : missionList) { 660 | if (id.equals(m.getClassId())) { 661 | myTable.appendValueAt(Integer.valueOf(m.getDay()), Integer.valueOf(m.getTime()), m); 662 | } 663 | } 664 | } 665 | 666 | void queryByTeacherName(String name) { 667 | for (Mission m : missionList) { 668 | if (name.equals(m.getTeacherName())) { 669 | myTable.appendValueAt(Integer.valueOf(m.getDay()), Integer.valueOf(m.getTime()), m); 670 | } 671 | } 672 | } 673 | 674 | void queryByClassRoom(String room) { 675 | for (Mission m : missionList) { 676 | if (room.equals(m.getClassroom())) { 677 | myTable.appendValueAt(Integer.valueOf(m.getDay()), Integer.valueOf(m.getTime()), m); 678 | } 679 | } 680 | } 681 | 682 | } 683 | -------------------------------------------------------------------------------- /p2/src/p2/Mission.java: -------------------------------------------------------------------------------- 1 | package p2; 2 | 3 | /** 4 | * @author jikangwang 5 | */ 6 | public class Mission { 7 | private String teacherName; 8 | private String courseName; 9 | private String day; 10 | private String time; 11 | private String classroom; 12 | private String classId; 13 | 14 | public Mission(String teacherName, String courseName, String day, String time, String classroom, String classId) { 15 | this.teacherName = teacherName; 16 | this.courseName = courseName; 17 | this.day = day; 18 | this.time = time; 19 | this.classroom = classroom; 20 | this.classId = classId; 21 | } 22 | 23 | public String getCourseName() { 24 | return courseName; 25 | } 26 | 27 | public void setCourseName(String courseName) { 28 | this.courseName = courseName; 29 | } 30 | 31 | public String getTeacherName() { 32 | return teacherName; 33 | } 34 | 35 | public void setTeacherName(String teacherName) { 36 | this.teacherName = teacherName; 37 | } 38 | 39 | public String getDay() { 40 | return day; 41 | } 42 | 43 | public void setDay(String day) { 44 | this.day = day; 45 | } 46 | 47 | public String getTime() { 48 | return time; 49 | } 50 | 51 | public void setTime(String time) { 52 | this.time = time; 53 | } 54 | public String getClassroom() { 55 | return classroom; 56 | } 57 | 58 | public void setClassroom(String classroom) { 59 | this.classroom = classroom; 60 | } 61 | 62 | public String getClassId() { 63 | return classId; 64 | } 65 | 66 | public void setClassId(String classId) { 67 | this.classId = classId; 68 | } 69 | 70 | @Override 71 | public String toString() { 72 | return "Mission{" + 73 | "teacherName='" + teacherName + '\'' + 74 | ", courseName='" + courseName + '\'' + 75 | ", day='" + day + '\'' + 76 | ", time='" + time + '\'' + 77 | ", classroom='" + classroom + '\'' + 78 | ", classId='" + classId + '\'' + 79 | '}'; 80 | } 81 | 82 | public String out(){ 83 | if(teacherName.length()==0) return ""; 84 | return teacherName+"老师"+"("+courseName+")"+"在"+classroom+"教室"+"教"+classId+"班"; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /p2/src/p2/MyTable.java: -------------------------------------------------------------------------------- 1 | package p2; 2 | 3 | import javax.swing.*; 4 | import javax.swing.table.AbstractTableModel; 5 | import javax.swing.table.TableCellRenderer; 6 | import java.awt.*; 7 | 8 | /** 9 | * @author jikangwang 10 | */ 11 | class MyTable{//使用AbstractTableModel来创建表格模型 12 | 13 | 14 | JTable table; 15 | Object[][] rowData = 16 | { 17 | {"1-2", null, null, null, null, null}, 18 | {"3-4", null, null, null, null, null}, 19 | {"5-6", null, null, null, null, null}, 20 | {"7-8", null, null, null, null, null}, 21 | {"9-10", null, null, null, null, null}, 22 | }; 23 | Object[][] cache = 24 | { 25 | {"1-2", null, null, null, null, null}, 26 | {"3-4", null, null, null, null, null}, 27 | {"5-6", null, null, null, null, null}, 28 | {"7-8", null, null, null, null, null}, 29 | {"9-10", null, null, null, null, null}, 30 | }; 31 | String[] columnNames = {"", "\u661F\u671F\u4E00", "\u661F\u671F\u4E8C", "\u661F\u671F\u4E09", "\u661F\u671F\u56DB", "\u661F\u671F\u4E94"}; 32 | 33 | public JTable getTable() { 34 | return table; 35 | } 36 | 37 | public MyTable() { 38 | table=new JTable(rowData, columnNames); 39 | table.setDefaultRenderer(Object.class, new TableCellTextAreaRenderer()); 40 | } 41 | 42 | public void sava() { 43 | for (int i = 0; i < 5; i++) { 44 | for (int j = 0; j <= 5; j++) { 45 | cache[i][j] = rowData[i][j]; 46 | } 47 | } 48 | } 49 | 50 | public void restore() { 51 | for (int i = 0; i < 5; i++) { 52 | for (int j = 0; j <= 5; j++) { 53 | rowData[i][j] = cache[i][j]; 54 | } 55 | } 56 | } 57 | 58 | public Boolean sp(String s1, String s2, String s3, String ss1, String ss2) //教师 班级 时间 时间 教室 59 | { 60 | String[] t1=s3.split("-"); 61 | Unit u=(Unit)getValueAt(Integer.parseInt(t1[1])-1,Integer.parseInt(t1[0])); 62 | if(u==null) return false; 63 | Mission m=u.getByName(s1, s2); 64 | if(m==null) return false; 65 | 66 | Mission nm=new Mission(m.getTeacherName(),m.getCourseName(),m.getDay(),m.getTime(),m.getClassroom(),m.getClassId()); 67 | u.Delele(m); 68 | 69 | String[] t2=ss1.split("-"); 70 | Unit u2=(Unit)getValueAt(Integer.parseInt(t2[1])-1,Integer.parseInt(t2[0])); 71 | nm.setClassroom(ss2); 72 | u2.Add(nm); 73 | this.sava(); 74 | 75 | return true; 76 | } 77 | 78 | public int getColumnCount() { 79 | return columnNames.length; 80 | } 81 | 82 | public int getRowCount() { 83 | return rowData.length; 84 | } 85 | 86 | public String getColumnName(int col) { 87 | return columnNames[col]; 88 | } 89 | 90 | public Object getValueAt(int row, int col) { 91 | return rowData[row][col]; 92 | } 93 | 94 | public boolean isCellEditable(int rowIndex, int columnIndex) { //判断单元格是否可以编辑 95 | return true; 96 | } 97 | 98 | public void setValueAt(int row, int col ,Object value) { 99 | table.setValueAt(value, row, col); 100 | } 101 | 102 | public void appendValueAt(int row, int col,Mission value ) { 103 | Unit u=(Unit) table.getValueAt(row, col); 104 | u.Add(value); 105 | } 106 | 107 | public void clear() { 108 | for (int i = 0; i < 5; i++) { 109 | for (int j = 1; j <= 5; j++) { 110 | rowData[i][j] = new Unit(); 111 | } 112 | } 113 | } 114 | 115 | class TableCellTextAreaRenderer extends JTextArea implements TableCellRenderer { 116 | public TableCellTextAreaRenderer() { 117 | setLineWrap(true); 118 | setWrapStyleWord(true); 119 | } 120 | 121 | public Component getTableCellRendererComponent(JTable table, Object value, 122 | boolean isSelected, boolean hasFocus, int row, int column) { 123 | // 计算当下行的最佳高度 124 | int maxPreferredHeight = 0; 125 | for (int i = 0; i < table.getColumnCount(); i++) { 126 | setText("" + table.getValueAt(row, i)); 127 | setSize(table.getColumnModel().getColumn(column).getWidth(), 0); 128 | maxPreferredHeight = Math.max(maxPreferredHeight, getPreferredSize().height); 129 | } 130 | 131 | if (table.getRowHeight(row) != maxPreferredHeight) // 少了这行则处理器瞎忙 132 | table.setRowHeight(row, maxPreferredHeight); 133 | 134 | setText(value == null ? "" : value.toString()); 135 | return this; 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /p2/src/p2/Test.java: -------------------------------------------------------------------------------- 1 | package p2; 2 | 3 | /** 4 | * @author jikangwang 5 | */ 6 | public class Test { 7 | 8 | public static void main(String[] args) { 9 | GA ga=new GA(); 10 | String Dir = "/Users/jikangwang/JavaProject/-JAVA-Course-Scheduling-System-Based-on-Genetic-Algorithm/p2/1.xls"; 11 | FileProcesser fileProcesser = new FileProcesser(); 12 | fileProcesser.read(Dir); 13 | ga.setCoursesList(fileProcesser.getCourseList()); 14 | 15 | 16 | ga.start(); 17 | 18 | 19 | } 20 | 21 | } 22 | -------------------------------------------------------------------------------- /p2/src/p2/TextAreaCellRenderer.java: -------------------------------------------------------------------------------- 1 | package p2; 2 | 3 | import javax.swing.*; 4 | import javax.swing.table.TableCellRenderer; 5 | import java.awt.*; 6 | 7 | /** 8 | * @author jikangwang 9 | */ 10 | public class TextAreaCellRenderer extends JTextArea implements TableCellRenderer { 11 | 12 | public TextAreaCellRenderer() { 13 | setLineWrap(true); 14 | setWrapStyleWord(true); 15 | } 16 | 17 | public Component getTableCellRendererComponent(JTable table, Object value, 18 | boolean isSelected, boolean hasFocus, int row, int column) { 19 | // 计算当下行的最佳高度 20 | int maxPreferredHeight = 0; 21 | for (int i = 0; i < table.getColumnCount(); i++) { 22 | setText("" + table.getValueAt(row, i)); 23 | setSize(table.getColumnModel().getColumn(column).getWidth(), 0); 24 | maxPreferredHeight = Math.max(maxPreferredHeight, 25 | getPreferredSize().height); 26 | } 27 | 28 | if (table.getRowHeight(row) != maxPreferredHeight) // 少了这行则处理器瞎忙 29 | table.setRowHeight(row, maxPreferredHeight); 30 | 31 | setText(value == null ? "" : value.toString()); 32 | return this; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /p2/src/p2/Unit.java: -------------------------------------------------------------------------------- 1 | package p2; 2 | 3 | import java.util.ArrayList; 4 | 5 | /** 6 | * @author jikangwang 7 | * 用来存储每个时间点的所有教学任务 8 | */ 9 | public class Unit { 10 | 11 | private ArrayList list=new ArrayList<>(); 12 | 13 | Unit(){ 14 | } 15 | public void Add(Mission m){ 16 | list.add(m); 17 | } 18 | 19 | public Mission getByName(String teacher, String cls){ 20 | for(Mission m:list){ 21 | if(m.getTeacherName().equals(teacher)&&m.getClassId().equals(cls)){ 22 | return m; 23 | } 24 | } 25 | return null; 26 | } 27 | 28 | public void Delele(Mission m){ 29 | list.remove(m); 30 | } 31 | 32 | public void Show(){ 33 | for(Mission m:list){ 34 | m.out(); 35 | } 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | if(list.size()==0) return "*空*"; 41 | StringBuffer s=new StringBuffer(); 42 | for(Mission m:list){ 43 | if(m.out().length()==0) continue; 44 | s.append(m.out()+"\n"); 45 | } 46 | return s.toString(); 47 | } 48 | } 49 | --------------------------------------------------------------------------------