Chinaunix

标题: 修改程序 C++ [打印本页]

作者: jackietin827    时间: 2009-12-03 04:19
标题: 修改程序 C++
谁能帮我把以下程序改为C++的啊?感谢

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Xml;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace PaiKe
{
    class Program
    {
        static void Main(string[] args)
        {
           PaiKe paike = new PaiKe();
           paike.Test1();
                Console.ReadLine();
        }
    }
    class ClassUnit
    {
        static List<Course> CourseList = new List<Course>();
        public const int WeekDay = 5;
        public const int CourseCount = 6;
        public int ID;
        public int[] XuLie = new int[WeekDay * ClassUnit.CourseCount];
        public ClassUnit(int id, int[] xulie)
        {
            ID = id;
            XuLie = xulie;
        }
    }
    [Serializable()]
    class Course
    {
        static int Seed = 0;
        public int ID;
        public int ClassID;
        public string TeacherName;
        public int TeacherID;
        public string Name;
        public int Priority = 0;
        public Course(int id, int techerid ,string name, string teacherName, int priority)
        {
            Seed++;
            Name = name;
            ID = id;
            TeacherID = id;// techerid;
            TeacherName = teacherName;
            Priority = priority;
        }
    }
    class CoursePriority
    {         
      public static int[] Priority=new int[ClassUnit.CourseCount]{5,5,4,4,2,1};
    }
    class PaiKe
    {
        Random rnd = new Random();
        ClassUnit tClassUnit;// = new ClassUnit(1);            
        List<ClassUnit> ClassList = new List<ClassUnit>();
        List<Course> CourseList = new List<Course>();
        //---------------惩罚值列表--------------
        //冲突
        const int ChongTu = 200;
        //一天有2节相同的课
        const int OneDayForTowCourse = 150;
        //连续2天有2节相同课
        const int TowDayForTowCourse = 40;
        //课程优先级单位值
        const int CoursePr = 15;
        void Init()
        {
            int[] Tshuzu ;
            Tshuzu = new int[ClassUnit.CourseCount * ClassUnit.WeekDay]
            {
               1,1,1,1,1,2,
               2,2,2,2,3,3,
               3,3,3,4,4,4,
               5,5,6,6,6,8,
               8,9,9,7,7,10
            };
            tClassUnit = new ClassUnit(1, Tshuzu);
            ClassList.Add(tClassUnit);

            Tshuzu = new int[ClassUnit.CourseCount * ClassUnit.WeekDay]
            {
               1,1,1,1,1,2,
               2,2,2,2,3,3,
               3,3,3,4,4,4,
               5,5,6,6,6,8,
               8,9,9,7,7,10
            };
            tClassUnit = new ClassUnit(1, Tshuzu);
            ClassList.Add(tClassUnit);
            Tshuzu = new int[ClassUnit.CourseCount * ClassUnit.WeekDay]
            {
               11,11,11,11,11,12,
               12,12,12,12,13,13,
               13,13,13,14,14,14,
               15,15,16,16,16,18,
               18,19,19,17,17,20
            };
            tClassUnit = new ClassUnit(1, Tshuzu);
            //ClassList.Add(tClassUnit);
            Tshuzu = new int[ClassUnit.CourseCount * ClassUnit.WeekDay]
            {
               21,21,21,21,21,21,
                21,21,21,21,21,21,
                21,21,21,21,21,21,
                21,21,21,21,21,21,
                21,21,21,21,21,21
            };
            tClassUnit = new ClassUnit(1, Tshuzu);
            //ClassList.Add(tClassUnit);
            Tshuzu = new int[ClassUnit.CourseCount * ClassUnit.WeekDay]
            {
               21,21,21,21,21,22,
               22,22,22,22,23,23,
               23,23,23,24,24,24,
               25,25,26,26,26,28,
               28,29,29,27,27,30
            };
            tClassUnit = new ClassUnit(1, Tshuzu);
            //ClassList.Add(tClassUnit);
            Tshuzu = new int[ClassUnit.CourseCount * ClassUnit.WeekDay]
            {
               21,21,21,21,21,22,
               22,22,22,22,23,23,
               23,23,23,24,24,24,
               25,25,26,26,26,28,
               28,29,29,27,27,30
            };
            tClassUnit = new ClassUnit(1, Tshuzu);

            
            CourseList.Add(new Course(1, 1, "语文", "全伟", 5));
            CourseList.Add(new Course(2, 2, "数学", "全伟", 5));
            CourseList.Add(new Course(3, 3, "英语", "全伟", 4));
            CourseList.Add(new Course(4, 4, "物理", "全伟", 2));
            CourseList.Add(new Course(5, 5, "化学", "全伟", 2));
            CourseList.Add(new Course(6, 6, "生物", "全伟", 2));
            CourseList.Add(new Course(7, 7, "地理", "全伟", 2));
            CourseList.Add(new Course(8, 8, "历史", "全伟", 2));
            CourseList.Add(new Course(9, 9, "政治", "全伟", 2));
            CourseList.Add(new Course(10, 10, "音乐", "全伟", 1));
            
            CourseList.Add(new Course(11, 1, "语文", "全伟", 5));
            CourseList.Add(new Course(12, 1, "数学", "全伟", 5));
            CourseList.Add(new Course(13, 1, "英语", "全伟", 4));
            CourseList.Add(new Course(14, 1, "物理", "全伟", 2));
            CourseList.Add(new Course(15, 1, "化学", "全伟", 2));
            CourseList.Add(new Course(16, 1, "生物", "全伟", 2));
            CourseList.Add(new Course(17, 1, "地理", "全伟", 2));
            CourseList.Add(new Course(18, 1, "历史", "全伟", 2));
            CourseList.Add(new Course(19, 1, "政治", "全伟", 2));
            CourseList.Add(new Course(20, 1, "音乐", "全伟", 1));
            CourseList.Add(new Course(21, 1, "test", "全伟", 1));

        }
        void Print(ClassUnit cu)
        {
            for (int i = 0; i < ClassUnit.WeekDay * ClassUnit.CourseCount; i++)
            {
                Console.Write(cu.XuLie.ToString() + "  ");
                if ((i + 1) % ClassUnit.CourseCount == 0)
                {
                    Console.Write("\n");
                }
            }
        }
        void PrintDetail(ClassUnit cu)
        {
            Console.Write("       ");
            for (int i = 0; i < ClassUnit.CourseCount; i++)
            {
                Console.Write("第{0}节    ", i + 1);
            }
            Console.Write("\n----------------------------------\n");
            for (int i = 0; i < ClassUnit.WeekDay * ClassUnit.CourseCount; i++)
            {
                if ((i) % (ClassUnit.CourseCount) == 0)
                {
                    Console.Write("周{0}:   ", i / ClassUnit.CourseCount + 1);
                }
                Console.Write(CourseList[(cu.XuLie - 1)].Name.ToString() + "     ");
                if ((i + 1) % ClassUnit.CourseCount == 0)
                {
                    Console.Write("\n");
                }
            }
        }
        void assign(int[] c, int[] n)
        {
            for (int i = 0; i < c.Length; i++)
                c = n;
        }
        void CreateNext(int[] c, int[] n)
        {
            for (int i = 0; i < c.Length; i++)
                n = c;
            int i1 = (int)(rnd.Next(ClassUnit.WeekDay * ClassUnit.CourseCount));
            int i2 = (int)(rnd.Next(ClassUnit.WeekDay * ClassUnit.CourseCount));
            int aux = n[i1];
            n[i1] = n[i2];
            n[i2] = aux;
        }
        int ComputeValue(int[] cu)
        {
            int Sum = 0;
            for (int i = 0; i < ClassUnit.WeekDay; i++)
            {
                int row = i * ClassUnit.CourseCount;
                for (int j = row; j < row + ClassUnit.CourseCount; j++)
                {
                    //一天2节
                    for (int k = j + 1; k < row + ClassUnit.CourseCount; k++)
                    {
                        if (cu[j] == cu[k])
                        {
                            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;
                        int t22 = ClassList[k].XuLie;
                        if (j == cc) { t11 = next; }
                        if (k == cc) { t22 = next; }

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

                        if (j == cc) { t1 = CourseList[next - 1].TeacherID; }
                        if (k == cc) { t2 = CourseList[next - 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;
                        int t2 = ClassList[k].XuLie;
                        */
                        int t1 = CourseList[ClassList[j].XuLie - 1].TeacherID;
                        int t2 = CourseList[ClassList[k].XuLie - 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);
            }
        }
        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.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++;
            }
        }

        
    }
}
作者: lenky0401    时间: 2009-12-03 08:48

作者: hellioncu    时间: 2009-12-03 09:20
等着被关闭
作者: pagx    时间: 2009-12-03 09:31

作者: evaspring    时间: 2009-12-03 13:13

作者: slay78    时间: 2009-12-03 20:45
什么是"全伟"啊? 看不懂




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2