Example #1
0
        public async Task <IActionResult> Index(ICollection <IFormFile> files, UserInput u)
        {
            /* Uploader File */
            user_input = u;
            var uploads = Path.Combine(_environment.WebRootPath, "uploads");

            foreach (var file in files)
            {
                if (file.Length > 0)
                {
                    uploads = Path.Combine(uploads, file.FileName);
                    using (var fileStream = new FileStream(uploads, FileMode.Create))
                    {
                        await file.CopyToAsync(fileStream);
                    }
                }
            }
            var openFile = new FileStream(uploads, FileMode.Open, FileAccess.Read);

            using (var readStream = new StreamReader(openFile))
            {
                int    conflict = 0;
                string line;

                Boolean           isRuangan = false;
                Boolean           isKelas   = false;
                string            temp      = "";
                char              b;
                RuanganManagement listOfRuangan = new RuanganManagement();
                KelasManagement   listOfKelas   = new KelasManagement();


                /* ADT RUANGAN */
                string     nama      = "";      // Contoh: "7602"
                int        jam_buka  = 0;       // Contoh: 7 (buka mulai jam 7)
                int        jam_tutup = 0;       // Contoh: 14 (tutup jam 14)
                List <int> hari_buka = new List <int>();

                /* ADT KELAS */
                // Domain value yang dimiliki 'Kelas'
                string        ruangan       = "";
                List <string> domainRuangan = new List <string>();
                List <int>    domainMulai   = new List <int>(); // Contoh: [7, 8, 9] (Kelas hanya bisa mulai jam 7, 8, atau 9)
                List <int>    domainHari    = new List <int>(); // Contoh: [1, 3, 5] (Kelas hanya bisa dilakukan hari Senin, Rabu, atau Jumat)
                int           durasi        = 0;                // Contoh: 4 (4 jam)


                while ((line = readStream.ReadLine()) != null)
                {
                    if (line.Equals(""))
                    {
                        // Do nothing
                    }
                    else if (line.Equals("Ruangan"))
                    {
                        isRuangan = true;
                        isKelas   = false;
                    }
                    else if (line.Equals("Jadwal"))
                    {
                        isRuangan = false;
                        isKelas   = true;
                    }
                    /* Membaca data ruangan */
                    else if (isRuangan)
                    {
                        var sr    = new StringReader(line);
                        int point = 0;
                        temp        = "";
                        hari_buka   = new List <int>();
                        domainMulai = new List <int>();
                        for (int k = 0; k < line.Length; k++)
                        {
                            b = (char)sr.Read();
                            if (b == ';')
                            {
                                if (point == 0)
                                {
                                    nama = temp;
                                }
                                else if (point == 1)
                                {
                                    jam_buka = timeToInt(temp);
                                }
                                else if (point == 2)
                                {
                                    jam_tutup = timeToInt(temp);
                                }
                                point++;
                                temp = "";
                            }
                            else
                            {
                                if (b != ',')
                                {
                                    temp += b;
                                }
                            }
                        }
                        var parse = new StringReader(temp);
                        for (int j = 0; j < temp.Length; j++)
                        {
                            char val = (char)parse.Read();
                            hari_buka.Add(Int32.Parse(val.ToString()));
                        }
                        listOfRuangan.addRuangan(new Ruangan(nama, jam_buka, jam_tutup, hari_buka));
                    }
                    /* Menambah info kelas */
                    else if (isKelas)
                    {
                        var sr    = new StringReader(line);
                        int point = 0;
                        temp          = "";
                        hari_buka     = new List <int>();
                        domainRuangan = new List <string>();
                        for (int k = 0; k < line.Length; k++)
                        {
                            b = (char)sr.Read();
                            if ((point == 1) && (b != ';'))
                            {
                                if (b == ',')
                                {
                                    domainRuangan.Add(temp);
                                    temp = "";
                                }
                                else
                                {
                                    temp += b;
                                }
                            }
                            else if (b == ';')
                            {
                                if (point == 0)
                                {
                                    nama = temp;
                                }
                                else if (point == 1)
                                {
                                    domainRuangan.Add(temp);
                                }
                                else if (point == 2)
                                {
                                    jam_buka = timeToInt(temp);
                                }
                                else if (point == 3)
                                {
                                    jam_tutup = timeToInt(temp);
                                }
                                else if (point == 4)
                                {
                                    durasi = Int32.Parse(temp);
                                }
                                point++;
                                temp = "";
                            }
                            else
                            {
                                if (b != ',')
                                {
                                    temp += b;
                                }
                            }
                        }
                        var parse = new StringReader(temp);
                        for (int j = 0; j < temp.Length; j++)
                        {
                            char val = (char)parse.Read();
                            hari_buka.Add(Int32.Parse(val.ToString()));
                        }
                        Kelas c = new Kelas(nama, domainRuangan, jam_buka, jam_tutup, durasi, hari_buka, listOfRuangan);
                        listOfKelas.addKelas(c);
                    }
                }

                KelasManagement ans = new KelasManagement();
                if (user_input.choice == 0)
                {
                    HillClimbing hc = new HillClimbing(listOfKelas, listOfRuangan);
                    ans = hc.getSol();
                    ViewData["Choice"] = "Hill Climbing";
                }
                else if (user_input.choice == 1)
                {
                    SimulatedAnnealing sa = new SimulatedAnnealing(listOfKelas, listOfRuangan);
                    sa.execute(1000, 0.9f);
                    ans = sa.getSol();
                    ViewData["Choice"] = "Simulated Annealing";
                }
                else if (user_input.choice == 2)
                {
                    Population p = new Population();
                    p.generatePopulation(200, listOfKelas);
                    GeneticAlgorithm ga = new GeneticAlgorithm(p);
                    for (int ax = 0; ax < 100000; ax++)
                    {
                        if (ga.getSolution().getConflict() == 0)
                        {
                            break;
                        }
                        else
                        {
                            ga.crossover();
                            ga.mutation();
                        }
                    }
                    ans = ga.getSolution();
                    ViewData["Choice"] = "Genetic Algorithm";
                }

                ViewData["tes"] = ans.getConflict();
                int i = 0;
                // Inisialisasi
                for (int j = 7; j < 18; j++)
                {
                    for (int k = 1; k < 6; k++)
                    {
                        ViewData["marker" + j.ToString() + k.ToString()]  = 5;
                        ViewData["nama" + j.ToString() + k.ToString()]    = "";
                        ViewData["ruangan" + j.ToString() + k.ToString()] = "";
                    }
                }
                // Pengisian kelas di tabel

                foreach (Kelas k in ans.getArrayKelas())
                {
                    ViewData["nama" + i.ToString()]    = k.getNama();
                    ViewData["ruangan" + i.ToString()] = k.getNamaRuangan();
                    ViewData["durasi" + i.ToString()]  = k.getDurasi();
                    ViewData["hari" + i.ToString()]    = k.getHari();
                    ViewData["jam" + i.ToString()]     = k.getMulai();
                    for (int z = k.getMulai(); z < (k.getMulai() + k.getDurasi()); z++)
                    {
                        if (ViewData["ruangan" + z.ToString() + k.getHari().ToString()].Equals(""))
                        {
                            ViewData["kelas" + z.ToString() + k.getHari().ToString()]   = i.ToString();
                            ViewData["marker" + z.ToString() + k.getHari().ToString()]  = 0;
                            ViewData["nama" + z.ToString() + k.getHari().ToString()]    = k.getNama();
                            ViewData["ruangan" + z.ToString() + k.getHari().ToString()] = k.getNamaRuangan();
                            //Kurangi waktu available dan tambahkan waktu terpakai;
                            listOfRuangan.getRuangan(k.getNamaRuangan()).decrement_waktu_available();
                        }
                        else if (cekRuanganSama(k.getNamaRuangan(), ViewData["ruangan" + z.ToString() + k.getHari().ToString()].ToString()))
                        {
                            conflict++;
                            ViewData["kelas" + z.ToString() + k.getHari().ToString()]    = i.ToString();
                            ViewData["marker" + z.ToString() + k.getHari().ToString()]   = 1;
                            ViewData["nama" + z.ToString() + k.getHari().ToString()]    += " " + k.getNama();
                            ViewData["ruangan" + z.ToString() + k.getHari().ToString()] += " " + k.getNamaRuangan();
                        }
                        else
                        {
                            ViewData["kelas" + z.ToString() + k.getHari().ToString()]   += " " + i.ToString();
                            ViewData["marker" + z.ToString() + k.getHari().ToString()]   = 2;
                            ViewData["nama" + z.ToString() + k.getHari().ToString()]    += " " + k.getNama();
                            ViewData["ruangan" + z.ToString() + k.getHari().ToString()] += " " + k.getNamaRuangan();
                            /* Kurangi waktu available dan tambahkan waktu terpakai*/
                            listOfRuangan.getRuangan(k.getNamaRuangan()).decrement_waktu_available();
                        }
                    }
                    i++;
                }
                ViewData["tes"]    = conflict;
                ViewData["Length"] = i;

                /* Memasukkan data keefektifan untuk ditampilkan di web */
                int nRuangan = 0;
                foreach (Ruangan ruang_ruang in listOfRuangan.getAllRuangan())
                {
                    ViewData["namaruangan" + nRuangan.ToString()] = ruang_ruang.getName();
                    ViewData["efektifitas" + nRuangan.ToString()] = ruang_ruang.getEfektifitasRuangan();
                    nRuangan++;
                }
                ViewData["banyakruangan"] = nRuangan;
            }
            ViewData["color0"] = "green";
            ViewData["color1"] = "yellow";
            ViewData["color2"] = "brown";
            ViewData["color3"] = "blue";
            ViewData["color4"] = "black";
            ViewData["color5"] = "aqua";
            ViewData["color6"] = "purple";
            ViewData["color7"] = "wood";
            ViewData["color8"] = "pink";
            ViewData["color9"] = "orange";
            ViewData["Start"]  = 1;
            return(View());
        }
Example #2
0
 /* ctor */
 public HillClimbing(KelasManagement kelas_management, RuanganManagement ruangan_management)
 {
     this.ruangan_management = ruangan_management;
     this.kelas_management   = kelas_management;
     solution = new KelasManagement(kelas_management.getArrayKelas());
 }