Exemplo n.º 1
0
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            //label1.Text = "Dejan";
            Raspored temp = new Raspored(best);
            best.RemoveEvent(podaci, 4);
            temp.evaluateSchedule(podaci);
            //temp = podaci.deepCopy();

            int[] bad0 = new int[7];
            bad0 = temp.evaluateSchedule(podaci);
            label3.Text = bad0[0].ToString();
            label4.Text = bad0[3].ToString();
            label5.Text = bad0[1].ToString();
            label16.Text = bad0[4].ToString();
            //temp.generateHtml("temp.html", podaci);
            //best = temp.deepCopy();
            //Console.WriteLine("{0}", temp.getScore());

            int tb = podaci.EventList.Count / 10;
            int korak = 0;
            int br = 0;
            int promjenjeniEvent = 0;
            bool foo = false;
            double stop = Double.Parse(textBox1.Text);
            while (br < numericUpDown1.Value && best.Score < stop)
            {
                label2.Text = br.ToString();
                progressBar1.PerformStep();
                if (foo) continue;
                bool stuck = true;
                korak++;

                //backgroundWorker1.ReportProgress(br);
                //label2.Text = br.ToString();
                br++;
                //Console.WriteLine("===============");
                Raspored eval = new Raspored(temp);//temp.deepCopy();
              //      Schedule localBest = podaci.deepCopy();
              //      localBest.evaluateSchedule();
                foreach (Event f in podaci.EventList)
                {
                    int eventId = f.Id;
                    int LecId = f.getLecturer().Id;
                    if (f.tabu != 0 && korak - f.tabu < tb) continue;
                    int t = f.Duration;
                    foreach (Classroom c in podaci.ClassroomList)
                    {
                        //int Roo
                        if (c.canHost(f))
                        {
                            for (int i = 0; i < 5; i++)
                            {
                                for (int j = 0; j < 12; j++)
                                {

                                    //if (c.isAvailable(i, j, t) && f.getLecturer().isAvailable(i, j, t))
                                    if (eval.IsRoomAvailable(c.Id, i, j, t) && eval.IsLecturerAvailable(LecId, i, j, t))
                                    {
                                        bool indikator = false;
                                        foreach (string g in f.getGroups())
                                        {

                                            if (eval.IsGroupAvailable(podaci.findGroup(g).Id, i, j, t) == false)
                                                indikator = true;
                                        }
                                        if (indikator)
                                            continue;

                                        //TU SMO DAKLE, na eval radim remove

                                        //eval.getEventByID(f.getID()).getClassroom().removeEvent(f.getID());
                                        //eval.getEventByID(f.getID()).getLecturer().removeEvent(f.getID());
                                        //eval.getEventByID(f.getID()).getCourse().removeEvent(f.getPlace().i, f.getPlace().j, f.getDuration());

                                        //foreach (string g in f.getGroups())
                                        //{
                                        //    eval.findGroup(g).removeEvent(f.getID());
                                        //}

                                        eval.RemoveEvent(podaci, eventId);

                                       /* Place p = new Place(eval.getRoomByID(c.getID()), i, j);
                                        eval.getEventByID(f.getID()).setPlace(p);

                                        eval.getRoomByID(c.getID()).setEvent(i, j, f);
                                        eval.getEventByID(f.getID()).getLecturer().setEvent(i, j, f);
                                        eval.getEventByID(f.getID()).getCourse().setEvent(i, j, f);
                                        eval.getEventByID(f.getID()).setClassroom(eval.getRoomByID(c.getID()));
                                        foreach (string g in f.getGroups())
                                        {
                                            eval.findGroup(g).setEvent(i, j, f);
                                        }
                                        */
                                        eval.SetEvent(podaci, eventId, c.Id, i, j);
                                        bad0 = eval.evaluateSchedule(podaci);
                                        if (eval.Score >= best.Score)
                                        {
                                            label10.Text = bad0[0].ToString();
                                            label11.Text = bad0[3].ToString();
                                            label12.Text = bad0[1].ToString();
                                            label17.Text = bad0[4].ToString();
                                            best = new Raspored(eval);
                                            label1.Text = best.Score.ToString().Substring(0, Math.Min(10, best.Score.ToString().Length));
                                            //string html = best.drawGroupSchedule(best.findGroup("MA1_1"));
                                            //webBrowser1.DocumentText = html;
                                            //Console.WriteLine("{0}", best.getScore());
                                            //best.generateHtml("best.html");
                                            promjenjeniEvent = f.getID();
                                            //f.tabu = 10;
                                            stuck = false;
                                            //temp.generateHtml("stuck.html", podaci);
                                        }
                                  /*      if (eval.getScore() > localBest.getScore())
                                        {
                                            localBest = eval.deepCopy();

                                        }*/
                                        eval = new Raspored(temp);
                                    }
                                }
                            }
                        }
                    }
                }
                if (stuck)
                {
                    //best = localBest.deepCopy();
                    //label1.ForeColor = Color.Red;
                    foo = true;
                    //label2.Text = br.ToString();
                    int[] bad = new int[7];
                    bad = best.evaluateSchedule(podaci);
                    label10.Text = bad0[0].ToString();
                    label11.Text = bad0[3].ToString();
                    label12.Text = bad0[1].ToString();
                    label17.Text = bad0[4].ToString();
                    //temp.generateHtml("stuck.html", podaci);
                }
                temp = new Raspored(best);
                podaci.getEventByID(promjenjeniEvent).tabu = korak;
                podaci.getEventByID(promjenjeniEvent).F++;
                //label2.Text = promjenjeniEvent.ToString();

            }
            //label2.Text = "ERROR" + br.ToString();
        }
Exemplo n.º 2
0
        public Raspored timetabler()
        {
            int[] w = {1, 1, 10, 20, 20};
            Raspored ra = new Raspored(this);
            while (true)
            {
                foreach (Event it in UnasignedEvents)
                {
                    int studentNo = it.StudentNumber;
                    Course kolegij = it.getCourse();
                    int t = it.Duration;

                    int score = 0;

                    List<Classroom> sobe = new List<Classroom>();
                    foreach (Classroom j in ClassroomList)
                    {
                        if (j.canHost(it))
                            sobe.Add(j);
                    }

                    int tmp_score = 0;
                    foreach (Classroom soba in sobe)
                    {
                        //int[,] week = new int[5,12];

                        for (int i = 0; i < 5; i ++)
                            for (int j = 0; j < 12; j++)
                            {
                                // za sad samo brojim koliko soba ima slobodnih termina!!
                                if (ra.IsRoomAvailable(soba.Id, i, j, t) == false)
                                    tmp_score++;
                            }

                    }
                    it.Score = tmp_score;
                }
                int min = 0;// = 100000;
                Event minEvent = null;// = new Event();

                /*minEvent = UnasignedEvents.First();
                min = minEvent.getScore();
                foreach (Event it in UnasignedEvents)
                {

                    if (it.getScore() < min)
                    {
                        min = it.getScore();
                        minEvent = it;
                    }
                }*/
                //////////////////////
                min = UnasignedEvents.First().Score;
                foreach (Event it in UnasignedEvents)
                {
                    if (it.Score < min)
                    {
                        min = it.Score;
                        //minEvent = it;
                    }

                }
                List<Event> minimalniEventi = new List<Event>();
                int brMin = 0;
                foreach (Event it in UnasignedEvents)
                {
                    if (it.Score == min)
                    {
                        brMin++;
                        minimalniEventi.Add(it);
                    }
                }
                Random random = new Random();
                int slBroj = random.Next(0, brMin);

                foreach(Event it in minimalniEventi)
                {
                    if (slBroj == 0)
                    {
                        minEvent = it;
                    }
                    slBroj--;
                }
                //////////////////////

                string isvu = minEvent.getCourse().getIsvu();

                List<Lecturer> prof = new List<Lecturer>();

                foreach (Lecturer pi in LecturerList)
                {
                    if (pi.teaches(isvu) && pi.getType() == minEvent.getLType())
                        prof.Add(pi);
                }

                Lecturer profesor = null;
                int minLoad = 0;
                profesor = prof.First();
                minLoad = profesor.getLoad();
                foreach (Lecturer pi in prof)
                {

                    if (pi.getLoad() <= minLoad)
                    {
                        minLoad = pi.getLoad();
                        profesor = pi;
                    }
                }
                getEventByID(minEvent.getID()).setLecturer(findLecturer(profesor.getID()));

                ra.SetLecturer(minEvent.getID(), profesor.getID());

                Course k = minEvent.getCourse();

                List<Classroom> predavaone = new List<Classroom>();
                foreach (Classroom j in ClassroomList)
                {
                    //if(j.getCapacity() >= minEvent.StudentNumber && j.getProjector() >= k.needsProjector() && j.getType() == k.getClassroomType())
                    if(j.canHost(minEvent))
                    {
                        predavaone.Add(j);
                    }
                }

                List<Place> mjesta = new List<Place>();

                foreach (Classroom tt in predavaone)
                {
                    for (int i = 0; i < 5; i++)
                    {
                        for (int j = 0; j < 12; j++)
                        {
                            if (ra.IsRoomAvailable(tt.Id, i, j, minEvent.Duration) == false)
                                continue;
                            if (ra.IsLecturerAvailable(profesor.Id, i, j, minEvent.Duration) == false)
                                continue;
                            int free = 0;
                            foreach (string g in minEvent.getGroups())
                            {
                                if (ra.IsGroupAvailable(findGroup(g).Id, i, j, minEvent.Duration) == false)
                                {
                                    free++;
                                }

                            }
                            if (free != 0)
                                continue;
                            Place tmp = new Place(tt, i, j);
                            //string s = "d";

                            int q1 = 0;

                            foreach (Event it in UnasignedEvents){
                            //Group *g = findGroup(it->getGroupName());

                                Course c = it.getCourse();
                                if (tt.getCapacity() >= it.StudentNumber && c.getClassroomType() == tt.getType() && c.needsProjector() == tt.getProjector() )
                                    q1++;
                            }
                            tmp.q[0] = q1;

                            int q2 = 0;

                            foreach (Classroom ci in predavaone)
                            {
                                if (ra.IsRoomAvailable(ci.Id, i, j, minEvent.Duration) == false)
                                    q2++;
                            }
                            tmp.q[1] = q2;

                            int q3 = 0;
                            if (tmp.j + minEvent.Duration >= 10)
                                q3 = 1;
                            tmp.q[2] = q3;

                            int q4 = 0;
                            tmp.q[3] = q4;

                            int q5 = 0;
                            tmp.q[4] = q5;

                            /*for (int i = 0; i < 11; i++) {
                        if (grupa->week[in][i] != 0 && grupa->week[in][i] != grupa->week[in][i + 1])
                            q4++;
                    }
                    tmp.q[3] = q4;*/
                            for (int ia = 0; ia < 5; ia++)
                                tmp.score += (tmp.q[ia] * w[ia]);

                            mjesta.Add(tmp);
                        }
                    }
                }
                Place minPlace = mjesta.First();
                int mini = minPlace.score; // ovo nam treba :=
                /*foreach(Place it in mjesta)
                {
                    if (it.score < mini)
                    {
                        mini = it.score;
                        minPlace = it;
                    }
                }*/

                slBroj = random.Next(0, mjesta.Count-1);

                foreach (Place it in mjesta)
                {
                    if (slBroj == 0)
                    {
                       // mini = it.score;
                        minPlace = it;
                    }
                    slBroj--;
                }
                /////
                int ID = minEvent.getID();
                getEventByID(ID).setClassroom(getRoomByID(minPlace.soba.getID()));
               /* getEventByID(ID).setPlace(minPlace);*/
                //getEventByID(ID).getCourse().setEvent(minPlace.i, minPlace.j, minEvent);

                ra.SetClassroom(ID, minPlace.soba.Id);
                ra.SetCourseEvent(minEvent.getCourse().getID(), minPlace.i, minPlace.j, minEvent.Duration, minEvent.getID());

                Classroom b = getRoomByID(minPlace.soba.getID());

               // b.setEvent(minPlace.i, minPlace.j, minEvent);
                ra.SetClassroomEvent(b.Id, minPlace.i, minPlace.j, minEvent.Duration, minEvent.getID());
               // profesor.setEvent(minPlace.i, minPlace.j, minEvent);
                ra.SetLecturerEvent(profesor.Id, minPlace.i, minPlace.j, minEvent.Duration, minEvent.getID());
                profesor.incLoad();
                foreach (string g in minEvent.getGroups())
                {
                    //findGroup(g).setEvent(minPlace.i, minPlace.j, minEvent);
                    ra.SetGroupEvent(findGroup(g).Id, minPlace.i, minPlace.j, minEvent.Duration, minEvent.getID());
                }
                UnasignedEvents.Remove(minEvent);

                if (UnasignedEvents.Count == 0)
                    break;
               /*

            list<Event>::iterator ita;
            for (ita = unasignedEvents.begin(); ita != unasignedEvents.end(); ita++)
            {
            if (ita->getID() == ID) {
                unasignedEvents.erase(ita);
                break;
            }
            }
            if(unasignedEvents.empty())
            break;*/

            }
            return ra;
        }