/* * Selects the smallest group of implicants which satisfy the equation from the matrix. */ private static ImplicantCollection SelectImplicants(ImplicantCollection implicants, int[] inputs) { var lstToRemove = new List <int>(inputs); var final = new ImplicantCollection(); int runnumber = 0; while (lstToRemove.Count != 0) { //Implicant[] weightedTerms = WeightImplicants(implicants, final, lstToRemove); foreach (var m in implicants) { bool add = false; if (ContainsSubList(lstToRemove, m.Minterms)) { add = true; if (lstToRemove.Count < m.Minterms.Count) { break; } } else { add = false; } if (((lstToRemove.Count <= m.Minterms.Count) && add == false) || runnumber > 5) { if (ContainsAtleastOne(lstToRemove, m.Minterms) && runnumber > 5) { add = true; } } if (add) { final.Add(m); foreach (int r in m.Minterms) { lstToRemove.Remove(r); } } } foreach (var item in final) { implicants.Remove(item); //ami benne van már 1x, az még 1x ne } ++runnumber; } return(final); }
/* * Simplifies a givenset of implicants. */ private static bool Simplify(ref ImplicantCollection implicants) { /* * Group by number of 1's and determine relationships by comparing. */ ImplicantGroup group = Group(implicants); ImplicantRelationshipCollection relationships = new ImplicantRelationshipCollection(); for (int i = 0; i < group.Keys.Count; i++) { if (i == (group.Keys.Count - 1)) { break; } ImplicantCollection thisGroup = group[group.Keys.ElementAt(i)]; ImplicantCollection nextGroup = group[group.Keys.ElementAt(i + 1)]; foreach (Implicant a in thisGroup) { foreach (Implicant b in nextGroup) { if (GetDifferences(a.Mask, b.Mask) == 1) { relationships.Add(new ImplicantRelationship(a, b)); } } } } /* * For each relationship, find the affected minterms and remove them. * Then add a new implicant which simplifies the affected minterms. */ foreach (ImplicantRelationship r in relationships) { ImplicantCollection rmList = new ImplicantCollection(); foreach (Implicant m in implicants) { if (r.a.Equals(m) || r.b.Equals(m)) { rmList.Add(m); } } foreach (Implicant m in rmList) { implicants.Remove(m); } Implicant newImplicant = new Implicant(); newImplicant.Mask = GetMask(r.a.Mask, r.b.Mask); newImplicant.Minterms.AddRange(r.a.Minterms); newImplicant.Minterms.AddRange(r.b.Minterms); bool exist = false; foreach (Implicant m in implicants) { if (m.Mask == newImplicant.Mask) { exist = true; } } if (!exist) //Why am I getting dupes? { implicants.Add(newImplicant); } } //Return true if simplification occurred, false otherwise. return(!(relationships.Count == 0)); }
/* * Simplifies a givenset of implicants. */ private static bool Simplify(ref ImplicantCollection implicants) { /* * Group by number of 1's and determine relationships by comparing. */ var groups = (from i in Group(implicants) orderby i.Key select i).ToDictionary(i => i.Key, i => i.Value); var relationships = new ImplicantRelationshipCollection(); for (int i = 0; i < groups.Keys.Count; i++) { if (i == (groups.Keys.Count - 1)) { break; } var thisGroup = groups[groups.Keys.ElementAt(i)]; var nextGroup = groups[groups.Keys.ElementAt(i + 1)]; var q = from a in thisGroup from b in nextGroup where GetDifferences(a.Mask, b.Mask) == 1 select new ImplicantRelationship(a, b); relationships.AddRange(q); } /* * For each relationship, find the affected minterms and remove them. * Then add a new implicant which simplifies the affected minterms. */ foreach (ImplicantRelationship r in relationships) { var rmList = new ImplicantCollection(); foreach (Implicant m in implicants) { if (r.a.Equals(m) || r.b.Equals(m)) { rmList.Add(m); } } foreach (Implicant m in rmList) { implicants.Remove(m); } var newImplicant = new Implicant(); newImplicant.Mask = GetMask(r.a.Mask, r.b.Mask); newImplicant.Minterms.AddRange(r.a.Minterms); newImplicant.Minterms.AddRange(r.b.Minterms); bool exist = false; foreach (Implicant m in implicants) { if (m.Mask == newImplicant.Mask) { exist = true; } } if (!exist) //Why am I getting dupes? { implicants.Add(newImplicant); } } //Return true if simplification occurred, false otherwise. return(!(relationships.Count == 0)); }