Exemple #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();
        }
        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("");
            //}
        }
Exemple #3
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();
        }