⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 中小学排课系统代码 用于算法研究
💻 CS
📖 第 1 页 / 共 2 页
字号:
                            Sum += OneDayForTowCourse;
                        }
                    }
                    //2天2节
                    for (int k = (j / ClassUnit.CourseCount + 1) * ClassUnit.CourseCount; k < (j / ClassUnit.CourseCount + 1) * ClassUnit.CourseCount + ClassUnit.CourseCount && k < ClassUnit.WeekDay * ClassUnit.CourseCount; k++)
                    {
                        if (cu[j] == cu[k] && j != k)
                        {
                            Sum += TowDayForTowCourse;
                        }
                    }
                    //4天2节
                    for (int k = (j / ClassUnit.CourseCount + 3) * ClassUnit.CourseCount; k < (j / ClassUnit.CourseCount + 3) * ClassUnit.CourseCount + ClassUnit.CourseCount && k < ClassUnit.WeekDay * ClassUnit.CourseCount; k++)
                    {
                        if (cu[j] == cu[k] && k != j)
                        {
                            Sum += TowDayForTowCourse;
                        }
                    }
                    int t1=CoursePriority.Priority[j % ClassUnit.CourseCount];
                    int t2=CourseList[cu[j]-1].Priority;
                    if ( t1<t2)
                    {
                        Sum +=CoursePr*(t2-t1);// * (Math.Max((j % 4 + 1 - cu[j]), 0));
                    }
                    
                }

            }
            return Sum;
        }
        int ComputeChongtuValue(int[] next, int cc)
        {
            int Sum = 0;
            for (int i = 0; i < ClassUnit.WeekDay * ClassUnit.CourseCount; i++)
            {
                for (int j = 0; j < ClassList.Count; j++)
                {
                    for (int k = j + 1; k < ClassList.Count; k++)
                    {
                        
                        int t11 = ClassList[j].XuLie[i];
                        int t22 = ClassList[k].XuLie[i];
                        if (j == cc) { t11 = next[i]; }
                        if (k == cc) { t22 = next[i]; }

                       
                        
                        int t1 = CourseList[ClassList[j].XuLie[i] - 1].TeacherID;
                        int t2 = CourseList[ClassList[k].XuLie[i] - 1].TeacherID;

                        if (j == cc) { t1 = CourseList[next[i] - 1].TeacherID; }
                        if (k == cc) { t2 = CourseList[next[i] - 1].TeacherID; }
                     
                        if (t1 == t2)
                        {
                            Sum += ChongTu;
                        }
                    }
                }
            }
            return Sum;
        }
        int ComputeChongtuValue()
        {
            int Sum = 0;
            for (int i = 0; i < ClassUnit.WeekDay * ClassUnit.CourseCount; i++)
            {
                for (int j = 0; j < ClassList.Count; j++)
                {
                    for (int k = j + 1; k < ClassList.Count; k++)
                    {
                        /*
                        int t1 = ClassList[j].XuLie[i];
                        int t2 = ClassList[k].XuLie[i];
                        */
                        int t1 = CourseList[ClassList[j].XuLie[i] - 1].TeacherID;
                        int t2 = CourseList[ClassList[k].XuLie[i] - 1].TeacherID;

                        if (t1 == t2)
                        {
                            //Sum += ChongTu;
                        }
                        if (t1 == t2)
                        {
                            Sum += ChongTu;
                        }
                    }
                }
            }
            return Sum;
        }
        void PrntList()
        {
            for (int i = 0; i < ClassList.Count; i++)
            {
                Console.Write("班级{0}:\n", i + 1);
                PrintDetail(ClassList[i]);
            }
        }
        public void Test()
        {
            ArrayList list = new ArrayList();
            //primary configuration of cities
            Init();
            int[] next = new int[ClassUnit.WeekDay * ClassUnit.CourseCount];
            int iteration = -1;
            //the probability
            double proba;
            double alpha = 0.999;
            double temperature = 4000.0;
            double epsilon = 0.002;
            double delta;
            int i;
            double Value = 0;
            for (i = 0; i < ClassList.Count; i++)
            {
                Value += ComputeValue(ClassList[0].XuLie);
            }
            Console.WriteLine("-----------当前最优值:{0}-------------", Value);
            //PrintDetail(ClassList[0]);
            //while the temperature didnt reach epsilon
            while (temperature > epsilon)
            {
                iteration++;
                //get the next random permutation of distances 
                CreateNext(ClassList[0].XuLie, next);
                //compute the distance of the new permuted configuration
                delta = ComputeValue(next) - Value;
                //if the new distance is better accept it and assign it
                if (delta < 0)
                {
                    assign(ClassList[0].XuLie, next);
                    Value = delta + Value;
                    Console.WriteLine("-----------当前最优值:{0}-------------", Value);
                    PrintDetail(ClassList[0]);

                    ComputeValue(ClassList[0].XuLie);
                }
                else
                {
                    proba = rnd.Next();
                    //if the new distance is worse accept it but with a probability level
                    // if the probability is less than E to the power -delta/temperature.
                    //otherwise the old value is kept
                    if (proba < Math.Exp(-delta / temperature))
                    {
                        assign(ClassList[0].XuLie, next);
                        Value = delta + Value;
                    }
                }
                //cooling proces on every iteration
                temperature *= alpha;
                //print every 400 iterations
                if (iteration % 400 == 0)
                {
                    // Console.WriteLine(Value);
                }
            }


        }
        public void Test1()
        {
            ArrayList list = new ArrayList();
            //primary configuration of cities
            Init();

            int[] next = new int[ClassUnit.WeekDay * ClassUnit.CourseCount];
            int iteration = -1;
            //the probability
            double proba;
            double alpha = 0.999;
            double temperature = 400.0;
            double epsilon = 0.002;
            double delta;
            int i;
            int cc = 0;

            double Value = 0;
            for (i = 0; i < ClassList.Count; i++)
            {
                Value += ComputeValue(ClassList[0].XuLie);
            }
            Value += ComputeChongtuValue();
            Value += 20000;
            Console.WriteLine("-----------当前最优值:{0}-------------", Value);
            //PrintDetail(ClassList[0]);
            PrntList();
            //while the temperature didnt reach epsilon
            while (temperature > epsilon)
            {
                iteration++;
                cc = cc % ClassList.Count;
                //get the next random permutation of distances 
                CreateNext(ClassList[cc].XuLie, next);
                //compute the distance of the new permuted configuration
                delta = ComputeValue(next);
                for (i = 0; i < ClassList.Count; i++)
                {
                    if (i == cc) { continue; }
                    delta += ComputeValue(ClassList[i].XuLie); ;
                }
                delta += ComputeChongtuValue(next, cc);
                delta = delta - Value;
                //if the new distance is better accept it and assign it
                if (delta < 0)
                {
                    assign(ClassList[cc].XuLie, next);
                    Value = delta + Value;
                    Console.WriteLine("-----------当前最优值:{0}-------------", Value);
                    PrntList();
                    //ComputeValue(ClassList[0].XuLie);
                }
                else
                {
                    proba = rnd.Next();
                    //if the new distance is worse accept it but with a probability level
                    // if the probability is less than E to the power -delta/temperature.
                    //otherwise the old value is kept
                    if (proba < Math.Exp(-delta / temperature))
                    {
                        assign(ClassList[cc].XuLie, next);
                        Value = delta + Value;
                    }
                }
                //cooling proces on every iteration
                temperature *= alpha;
                //print every 400 iterations
                if (iteration % 400 == 0)
                {
                    // Console.WriteLine(Value);
                }
                cc++;
            }
        }

        
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -