Пример #1
0
        private void minimizePOSButton_Click(object sender, EventArgs e)
        {
            if (!checks())
            {
                return;
            }

            QMSolver s = new QMSolver(this.variables, maxterm, dontcare, 0);

            s.solve();
            s.PRINT();
            KMapOutput ko = new KMapOutput(s, maxterm);

            ko.Show();
        }
Пример #2
0
 void fillKMapHeaders(int var, DataGridView dgv, QMSolver qs)
 {
     if (var == 2)
     {
         for (int i = 0; i < 2; i++)
         {
             dgv.Columns.Add("col_" + i.ToString(), qs.CONVERT(colheadSOP_2[i]));
             dgv.Rows.Add();
             dgv.Rows[i].HeaderCell.Value = qs.CONVERT(rowheadSOP_2_3[i]);
         }
     }
     else if (var == 3)
     {
         for (int i = 0; i < 4; i++)
         {
             dgv.Columns.Add("col_" + i.ToString(), qs.CONVERT(colheadSOP_3[i]));
         }
         for (int i = 0; i < 2; i++)
         {
             dgv.Rows.Add();
             dgv.Rows[i].HeaderCell.Value = qs.CONVERT(rowheadSOP_2_3[i]);
         }
     }
     else if (var == 4)
     {
         for (int i = 0; i < 4; i++)
         {
             dgv.Columns.Add("col_" + i.ToString(), qs.CONVERT(colheadSOP_4[i]));
             dgv.Rows.Add();
             dgv.Rows[i].HeaderCell.Value = qs.CONVERT(rowheadSOP_4[i]);
         }
     }
     else if (var == 5)
     {
         for (int i = 0; i < 4; i++)
         {
             dgv.Columns.Add("col_" + i.ToString(), qs.CONVERT(colheadSOP_5[i]));
         }
         for (int i = 0; i < 8; i++)
         {
             dgv.Rows.Add();
             dgv.Rows[i].HeaderCell.Value = qs.CONVERT(rowheadSOP_5[i]);
         }
     }
 }
Пример #3
0
 void fillKMap(int var, int mode, QMSolver qs)
 {
     if (var == 2)
     {
         for (int i = 0; i < 2; i++)
         {
             for (int j = 0; j < 2; j++)
             {
                 fillCell(i, j, qs);
             }
         }
     }
     else if (var == 3)
     {
         for (int i = 0; i < 2; i++)
         {
             for (int j = 0; j < 4; j++)
             {
                 fillCell(i, j, qs);
             }
         }
     }
     else if (var == 4)
     {
         for (int i = 0; i < 4; i++)
         {
             for (int j = 0; j < 4; j++)
             {
                 fillCell(i, j, qs);
             }
         }
     }
     else if (var == 5)
     {
         for (int i = 0; i < 8; i++)
         {
             for (int j = 0; j < 4; j++)
             {
                 fillCell(i, j, qs);
             }
         }
     }
 }
Пример #4
0
        void fillCell(int i, int j, QMSolver qs)
        {
            int dec = int.Parse(dataGridView3.Rows[i].Cells[j].Value.ToString());

            if (qs.minterms_orig.Contains(dec))
            {
                if (qs.mode == 1)
                {
                    dataGridView2.Rows[i].Cells[j].Value = 1;
                }
                if (qs.mode == 0)
                {
                    dataGridView2.Rows[i].Cells[j].Value = 0;
                }
                dataGridView2.Rows[i].Cells[j].Style.BackColor = Color.LightYellow;
            }
            else if (qs.dontcares.Contains(dec))
            {
                dataGridView2.Rows[i].Cells[j].Value = "X";
            }
        }
        public void solve()
        {
            int    N  = int.Parse(Math.Pow(2, variables).ToString());
            string s1 = VECTOSTR(function1);
            string s2 = VECTOSTR(function2);

            List <int> empty = new List <int>();

            for (int i = 1; i < int.Parse(Math.Pow(2, N).ToString()); i++)
            {
                int        mask = i, cnt = 0, f = 1;
                List <int> com = new List <int>();
                while (mask > 0)
                {
                    if (mask % 2 == 1)
                    {
                        if (s1[cnt] == '1' && s2[cnt] == '1')
                        {
                            com.Add(cnt);
                        }
                        else
                        {
                            f = 0;
                            break;
                        }
                    }
                    cnt++;
                    mask = mask / 2;
                }

                if (f == 1)
                {
                    s1.Reverse();
                    int S1 = BINTODEC(s1);
                    s1.Reverse();

                    s2.Reverse();
                    int S2 = BINTODEC(s2);
                    s2.Reverse();

                    string sc = VECTOSTR(com);
                    sc.Reverse();
                    int SC = BINTODEC(sc);
                    sc.Reverse();

                    //Console.WriteLine(S1.ToString() + " " + S2.ToString() + " "+ SC.ToString());

                    S1 = S1 ^ SC;
                    S2 = S2 ^ SC;

                    //Console.WriteLine(S1.ToString() + " " + S2.ToString() + " " + SC.ToString());

                    string ts1 = (new QMSolver(N)).DECTOBIN(S1);
                    string ts2 = (new QMSolver(N)).DECTOBIN(S2);
                    string tsc = (new QMSolver(N)).DECTOBIN(SC);
                    ts1 = new string(ts1.ToCharArray().Reverse().ToArray());
                    ts2 = new string(ts2.ToCharArray().Reverse().ToArray());
                    tsc = new string(tsc.ToCharArray().Reverse().ToArray());
                    //ts1 = ts1.Reverse();
                    //ts2 = ts2.Reverse();
                    //tsc.Reverse();

                    //Console.WriteLine(ts1 + " " + ts2 + " " + tsc);

                    List <int> t_mt1 = STRTOVEC(ts1);
                    List <int> t_mt2 = STRTOVEC(ts2);
                    List <int> t_mtc = STRTOVEC(tsc);
                    //Console.WriteLine("anaklakcsaklcmklasmcklasmcklasmckllasmkcmaskcmas");
                    //for (int j = 0; j < t_mt1.Count; j++) Console.Write(t_mt1[j] + " ");
                    //Console.Write("\n");
                    //for (int j = 0; j < t_mt2.Count; j++) Console.Write(t_mt2[j] + " ");
                    //Console.Write("\n");
                    //for (int j = 0; j < t_mtc.Count; j++) Console.Write(t_mtc[j] + " ");
                    //Console.Write("\n");

                    List <int> emp = new List <int>();

                    QMSolver q1 = new QMSolver(variables, t_mt1, emp), q2 = new QMSolver(variables, t_mt2, emp), q3 = new QMSolver(variables, t_mtc, emp);
                    q1.solve(false);
                    q2.solve(false);
                    q3.solve(false);
                    //q1.PRINT();
                    //q2.PRINT();
                    //q3.PRINT();


                    //Console.WriteLine("q1: ");
                    //for (int k = 0; k < q1.essentialPi.Count; k++)
                    //{
                    //    for (int j = 0; j < q1.essentialPi[k].Count; j++) Console.Write(q1.essentialPi[k][j] + " ");
                    //    Console.WriteLine("");
                    //}
                    //Console.WriteLine("q2: ");
                    //for (int k = 0; k < q2.essentialPi.Count; k++)
                    //{
                    //    for (int j = 0; j < q2.essentialPi[k].Count; j++) Console.Write(q2.essentialPi[k][j] + " ");
                    //    Console.WriteLine("");
                    //}
                    //Console.WriteLine("q3: ");
                    //for (int k = 0; k < q3.essentialPi.Count; k++)
                    //{
                    //    for (int j = 0; j < q3.essentialPi[k].Count; j++) Console.Write(q3.essentialPi[k][j] + " ");
                    //    Console.WriteLine("");
                    //}
                    List <MINOUTPUT> tempo = COSTUTILITY(q1.essentialPi, q2.essentialPi, q3.essentialPi);
                    for (int j = 0; j < tempo.Count; j++)
                    {
                        answer.Add(new MINOUTPUT(tempo[j]));
                    }
                }
            }

            //NO COMMON TERMS
            QMSolver qo1 = new QMSolver(variables, function1, empty), qo2 = new QMSolver(variables, function2, empty);

            qo1.solve(false);
            qo2.solve(false);
            //Console.WriteLine("qo1: ");
            //for (int k = 0; k < qo1.essentialPi.Count; k++)
            //{
            //    for (int j = 0; j < qo1.essentialPi[k].Count; j++) Console.Write(qo1.essentialPi[k][j] + " ");
            //    Console.WriteLine("");
            //}
            //Console.WriteLine("qo2: ");
            //for (int k = 0; k < qo2.essentialPi.Count; k++)
            //{
            //    for (int j = 0; j < qo2.essentialPi[k].Count; j++) Console.Write(qo2.essentialPi[k][j] + " ");
            //    Console.WriteLine("");
            //}
            List <List <string> > empty1 = new List <List <string> >();
            List <MINOUTPUT>      temp = COSTUTILITY(qo1.essentialPi, qo2.essentialPi, empty1);

            for (int i = 0; i < temp.Count; i++)
            {
                answer.Add(new MINOUTPUT(temp[i]));
            }

            //Console.WriteLine("Answer before: " + answer.Count.ToString());
            answer = new List <MINOUTPUT>(MINCOST(answer));
            //Console.WriteLine("Answer After: " + answer.Count.ToString());

            //Console.WriteLine("FINAL COSTS:");
            //for(int i=0;i<answer.Count;i++)
            //{
            //    Console.Write(answer[i].tc.ToString());
            //    Console.WriteLine("");
            //}
        }
        public void PRNMINOUTPUT()
        {
            Console.Write("\n");
            dis += "\n";
            for (int i = 0; i < answer.Count; i++)
            {
                List <List <string> > temp = new List <List <string> >();
                temp.Add(new List <string>(answer[i].Y1));
                temp.Add(new List <string>(answer[i].Y2));
                temp.Add(new List <string>(answer[i].C));
                //Console.WriteLine("TOTAL COST = " + answer[i].tc);
                QMSolver q = new QMSolver(temp);
                q.CONVERT();
                temp = new List <List <string> >();
                for (int j = 0; j < q.essentialPi.Count; j++)
                {
                    List <string> copy = new List <string>(q.essentialPi[j]);
                    temp.Add(new List <string>(copy));
                }
                answer[i].Y1 = new List <string>(temp[0]);
                answer[i].Y2 = new List <string>(temp[1]);
                answer[i].C  = new List <string>(temp[2]);

                Console.Write("Y1 = ");
                dis += "Y1 = ";
                for (int j = 0; j < answer[i].C.Count; j++)
                {
                    Console.Write(answer[i].C[j] + " + ");
                    dis += answer[i].C[j] + " + ";
                }
                for (int j = 0; j < answer[i].Y1.Count; j++)
                {
                    Console.Write(answer[i].Y1[j] + " + ");
                    dis += answer[i].Y1[j] + " + ";
                }

                dis = dis.Substring(0, dis.Length - 2);
                Console.Write("\n");
                dis += "\n";

                Console.Write("Y2 = ");
                dis += "Y2 = ";
                for (int j = 0; j < answer[i].C.Count; j++)
                {
                    Console.Write(answer[i].C[j] + " + ");
                    dis += answer[i].C[j] + " + ";
                }

                for (int j = 0; j < answer[i].Y2.Count; j++)
                {
                    Console.Write(answer[i].Y2[j] + " + ");
                    dis += answer[i].Y2[j] + " + ";
                }
                dis = dis.Substring(0, dis.Length - 2);

                Console.Write("\n");
                dis += "\n";

                Console.WriteLine("Total Cost = " + answer[i].tc);
                Console.Write("\n");
                dis += "Total Cost = " + answer[i].tc + "\n\n";
            }
        }
Пример #7
0
        //private List<string> rowheadPOS_2_3 = new List<string> { "A", "A'" };
        //private List<string> colheadPOS_2 = new List<string> { "B", "B'" };
        //private List<string> colheadPOS_3 = new List<string> { "B+C", "B+C'", "B'+C'", "B'+C" };
        //private List<string> rowheadPOS_4 = new List<string> { "A+B", "A+B'", "A'+B'", "A'+B" };
        //private List<string> colheadPOS_4 = new List<string> { "C+D", "C+D'", "C'+D'", "C'+D" };
        //private List<string> rowheadPOS_5 = new List<string> { "A+B+C", "A+B+C'", "A+B'+C'", "A+B'+C", "A'+B+C", "A'+B+C'", "A'+B'+C'", "A'+B'+C" };
        //private List<string> colheadPOS_5 = new List<string> { "D+E", "D+E'", "D'+E'", "D'+E" };
        public KMapOutput(QMSolver qs, List <int> maxterms)
        {
            InitializeComponent();
            this.noofvarLabel.Text = "Number of variables: " + qs.variables.ToString();
            if (qs.mode == 0)
            {
                dataGridView1.Columns[1].HeaderText = "Maxterms";
            }

            for (int i = 0; i < qs.dontcares.Count; i++)
            {
                dcLabel.Text += " " + qs.dontcares[i].ToString() + ",";
            }
            dcLabel.Text  = dcLabel.Text.Substring(0, dcLabel.Text.Length - 1);
            dcLabel.Text += ")";

            if (qs.mode == 1)
            {
                this.simplificationLabel.Text += " SOP";
                mtLabel.Text = "Σm(";
                for (int i = 0; i < qs.minterms_orig.Count; i++)
                {
                    mtLabel.Text += " " + qs.minterms_orig[i].ToString() + ",";
                }
                mtLabel.Text  = mtLabel.Text.Substring(0, mtLabel.Text.Length - 1);
                mtLabel.Text += ")";
            }
            else
            {
                this.simplificationLabel.Text += " POS";
                mtLabel.Text = "ΠM(";
                for (int i = 0; i < maxterms.Count; i++)
                {
                    mtLabel.Text += " " + maxterms[i].ToString() + ",";
                }
                mtLabel.Text  = mtLabel.Text.Substring(0, mtLabel.Text.Length - 1);
                mtLabel.Text += ")";
                //mtLabel.Text.Replace('Σ', 'Π');
                //mtLabel.Text.Replace('m', 'M');
            }
            if (qs.dontcares.Count == 0)
            {
                dcLabel.Text = "None";
            }

            Font f = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);

            fillKMapHeaders(qs.variables, dataGridView2, qs);
            fillKMapHeaders(qs.variables, dataGridView3, qs);
            fillKMapLayout(qs.variables);
            fillKMap(qs.variables, qs.mode, qs);

            string     epi           = "";
            List <int> all_highlight = new List <int>();

            if (qs.mode == 1)
            {
                for (int i = 0; i < qs.essentialPi.Count; i++)
                {
                    dataGridView1.Rows.Add("Solution " + (i + 1).ToString() + ":", "");
                    for (int j = 0; j < qs.essentialPi[i].Count; j++)
                    {
                        string     group = "";
                        List <int> grp   = new List <int>(qs.GETGROUP(qs.raw_essentialPi[i][j]));
                        all_highlight.AddRange(grp);
                        for (int k = 0; k < grp.Count; k++)
                        {
                            group += grp[k] + ", ";
                        }
                        group = group.Substring(0, group.Length - 2);
                        dataGridView1.Rows.Add(qs.essentialPi[i][j], group);
                    }
                }

                epi += "\nPossible Solutions:\n";
                for (int i = 0; i < qs.essentialPi.Count; i++)
                {
                    epi += "Solution " + (i + 1).ToString() + ":    Y = " + qs.essentialPi[i][0].ToString();
                    for (int j = 1; j < qs.essentialPi[i].Count; j++)
                    {
                        epi += " + " + (qs.essentialPi[i][j]).ToString();
                    }
                    epi += "\n";
                }
            }
            else
            {
                for (int i = 0; i < qs.essentialPi.Count; i++)
                {
                    dataGridView1.Rows.Add("Solution " + (i + 1).ToString() + ":", "");
                    for (int j = 0; j < qs.essentialPi[i].Count; j++)
                    {
                        string group = "";
                        //List<int> grp = new List<int>(getGroupPOS(qs.variables, rawEPIComp(qs.raw_essentialPi[i][j])));
                        Console.WriteLine("Essential PI:" + qs.raw_essentialPi[i][j]);
                        List <int> grp = new List <int>(qs.GETGROUP(qs.raw_essentialPi[i][j]));
                        all_highlight.AddRange(grp);
                        for (int k = 0; k < grp.Count; k++)
                        {
                            group += grp[k] + ", ";
                        }
                        group = group.Substring(0, group.Length - 2);
                        dataGridView1.Rows.Add(qs.CONVERT(qs.raw_essentialPi[i][j].ToString()), group);
                    }
                }

                epi += "\nPossible Solutions:\n";
                for (int i = 0; i < qs.essentialPi.Count; i++)
                {
                    epi += "Solution " + (i + 1).ToString() + ":    Y = ";
                    for (int j = 0; j < qs.essentialPi[i].Count; j++)
                    {
                        epi += "(" + qs.CONVERT(qs.raw_essentialPi[i][j].ToString()) + ")";
                    }
                    epi += "\n";
                }
            }
            Console.WriteLine("ALLL VALUES: ");
            for (int i = 0; i < all_highlight.Count; i++)
            {
                Console.WriteLine(all_highlight[i].ToString() + " ");
            }
            List <int> all_highlight_distinct = all_highlight.Distinct().ToList();

            Console.WriteLine("ALLL VALUES DISTINCT: ");
            for (int i = 0; i < all_highlight_distinct.Count; i++)
            {
                Console.WriteLine(all_highlight_distinct[i].ToString() + " ");
            }
            for (int i = 0; i < dataGridView3.Rows.Count; i++)
            {
                for (int j = 0; j < dataGridView3.Columns.Count; j++)
                {
                    if (all_highlight_distinct.Contains(int.Parse(dataGridView3.Rows[i].Cells[j].Value.ToString())))
                    {
                        dataGridView2.Rows[i].Cells[j].Style.BackColor = Color.LightYellow;
                    }
                }
            }

            Label l2 = new Label()
            {
                Text = epi
            };

            l2.AutoSize = true;
            l2.Font     = f;
            panel1.Controls.Add(l2);

            adjust_dgvs();
        }
Пример #8
0
        private void minimizeButton_Click(object sender, EventArgs e)
        {
            #region
            //Initialise the lists
            minterms  = new List <int>();
            dontcares = new List <int>();

            //Check if minterms Textbox is empty
            if (mintermTB.Text.Replace(" ", String.Empty) == "")
            {
                MessageBox.Show("Please enter atleast one minterm", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //Add minterms to minterms list
            try
            {
                string[] function = csvToArray(mintermTB.Text.Replace(" ", String.Empty) + ",");
                for (int i = 0; i < function.Length; i++)
                {
                    string temp = function[i].Trim();
                    Console.WriteLine(temp);
                    this.minterms.Add(int.Parse(temp));
                    //Check if minterms are in the correct range
                    if (int.Parse(temp) < 0 || int.Parse(temp) >= Math.Pow(2, this.variables))
                    {
                        MessageBox.Show("Entered Minterms should be between 0 and " + (Math.Pow(2, this.variables) - 1).ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
            catch
            {
                //To catch any random error
                MessageBox.Show("Enter the Minterms properly", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //Check for duplicate minterms
            var total = minterms.GroupBy(_ => _).Where(_ => _.Count() > 1).Sum(_ => _.Count());
            Console.WriteLine(total);
            if (total != 0)
            {
                MessageBox.Show("All minterms should be unique", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            //Add dontcares to dontcares list
            try
            {
                string[] function = csvToArray(dontcareTB.Text.Replace(" ", String.Empty) + ",");
                for (int i = 0; i < function.Length; i++)
                {
                    string temp = function[i].Trim();
                    this.dontcares.Add(int.Parse(temp));
                    //Check if dontcares are in the correct range
                    if (int.Parse(temp) < 0 || int.Parse(temp) >= Math.Pow(2, this.variables))
                    {
                        MessageBox.Show("Entered Don't Care terms should be between 0 and " + (Math.Pow(2, this.variables) - 1).ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
            catch
            {
                //To catch any random error
                if (dontcareTB.Text.Replace(" ", String.Empty) != "")
                {
                    MessageBox.Show("Enter the Don't Cares properly", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }

            //Check if minterms and dontcares have any common terms
            var commonList = minterms.Intersect(dontcares);
            if (commonList.Any())
            {
                MessageBox.Show("Terms are repeating between Minterms and Don't Cares", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            #endregion

            Console.Write("\nMinterms:");
            for (int i = 0; i < minterms.Count; i++)
            {
                Console.Write(minterms[i] + " ");
            }
            Console.WriteLine("\nDont Cares:");
            for (int i = 0; i < dontcares.Count; i++)
            {
                Console.Write(dontcares[i] + " ");
            }
            Console.Write("\n");
            QMSolver s = new QMSolver(this.variables, minterms, dontcares);
            s.solve();
            s.PRINT();
            QMOutput qo = new QMOutput(s);
            qo.Show();
        }
Пример #9
0
        public QMOutput(QMSolver qs)
        {
            InitializeComponent();
            this.noofvarLabel.Text = "Number of variables: " + qs.variables.ToString();
            for (int i = 0; i < qs.minterms_orig.Count; i++)
            {
                mtLabel.Text += " " + qs.minterms_orig[i].ToString() + ",";
            }
            mtLabel.Text  = mtLabel.Text.Substring(0, mtLabel.Text.Length - 1);
            mtLabel.Text += ")";

            for (int i = 0; i < qs.dontcares.Count; i++)
            {
                dcLabel.Text += " " + qs.dontcares[i].ToString() + ",";
            }
            dcLabel.Text  = dcLabel.Text.Substring(0, dcLabel.Text.Length - 1);
            dcLabel.Text += ")";
            if (qs.dontcares.Count == 0)
            {
                dcLabel.Text = "None";
            }

            Font f = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Regular);

            for (int i = 0; i < qs.dis.Count; i++)
            {
                if (i > 0)
                {
                    tableLayoutPanel1.ColumnCount += 1;
                    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
                }
                Label l1 = new Label()
                {
                    Text = qs.dis[i]
                };
                l1.AutoSize = true;
                l1.Font     = f;
                tableLayoutPanel1.Controls.Add(l1, tableLayoutPanel1.ColumnCount - 1, 0);
            }

            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dataGridView1.ColumnHeadersDefaultCellStyle.Font      = new Font("Tahoma", 9.75F, FontStyle.Bold);
            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            dataGridView1.Columns.Cast <DataGridViewColumn>().ToList().ForEach(g => g.SortMode = DataGridViewColumnSortMode.NotSortable);
            for (int i = 0; i < qs.primeImplicant.Count; i++)
            {
                string minterms = "";
                for (int j = 0; j < qs.primeImplicant[i].Item2.Count; j++)
                {
                    minterms += qs.primeImplicant[i].Item2[j] + ", ";
                }
                minterms = minterms.Substring(0, minterms.Length - 2);
                dataGridView1.Rows.Add(qs.primeImplicantTerms[i], qs.primeImplicant[i].Item1, minterms);
            }

            string epi = "";

            epi += "\nEssestial PI:\n";
            for (int i = 0; i < qs.essentialPi.Count; i++)
            {
                epi += "Y = " + qs.essentialPi[i][0].ToString();
                for (int j = 1; j < qs.essentialPi[i].Count; j++)
                {
                    epi += " + " + (qs.essentialPi[i][j]).ToString();
                }
                epi += "\n";
            }
            Label l2 = new Label()
            {
                Text = epi
            };

            l2.AutoSize = true;
            l2.Font     = f;
            panel1.Controls.Add(l2);
            panel2.VerticalScroll.Value = 0;
        }