public IEnumerable <UBasCov> IEGet_BaseSet(int sz, int rnk) { if (UGLLst == null) { yield break; } __checkCC = 0; List <UGLink> basUGLs = new List <UGLink>(); Bit981 HB981 = new Bit981(); //BaseSet bitPattern Bit324 usedLK = new Bit324(); //usedLink(by serial number) List <int> usedLKLst = new List <int>(); Bit81Chk coverChk = new Bit81Chk(sz, rnk, basUGLs, usedLKLst, usedLKIgnrLst); var cmbBas = new Combination(UGLLst.Count, sz); long rcbn36 = 0; int jkC = 0; int nxt = int.MaxValue; //(skip function) while (cmbBas.Successor(nxt)) { GeneralLogicGen.ChkBas1++; //***** jkC++; //*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==* sz=1 if (sz == 1) { UGLink UGL = UGLLst[cmbBas.Index[0]]; if (UGL.UC is UCell) { goto LNextSet; // if size=1, cell-type is invalid } // UGL.rcBit81 is Bit81 int blkB = (int)UGL.Get_rcbnFrame(2); if (blkB.BitCount() > 1) { goto LNextSet; } int no = UGL.rcBit81.no, b = blkB.BitToNum(); var P = UGLLst.Find(U => U.Equal_no_block(no, b)); if (P == null) { goto LNextSet; } if ((P.rcBit81 - UGL.rcBit81).Count == 0) { goto LNextSet; } //there are numbers only within one block HB981.Clear(); HB981.BPSet(UGL.rcBit81.no, UGL.rcBit81, tfbSet: false); basUGLs.Clear(); basUGLs.Add(UGL); GeneralLogicGen.ChkBas2++; //***** goto LBSFound; //possibility of solution } //*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==*==* sz>=2 HB981.Clear(); basUGLs.Clear(); usedLKLst.Clear(); coverChk.Clear(); rcbn36 = 0; //1..4....8.9.1...5.....63.....13.5.79..3...8..76.2.94.....75.....1...6.4.8....4..2 for (int k = 0; k < sz; k++) { nxt = k; UGLink UGL = UGLLst[cmbBas.Index[k]]; rcbn36 |= UGL.rcbnFrame2; if (!Check_rcbnCondition(sz, rnk, k, rcbn36)) { goto LNextSet; //# Extremely efficient } if (UGL.rcBit81 is Bit81) // ............ rcb ............ { if (k > 0 && HB981.IsHit(UGL.rcBit81.no, UGL.rcBit81)) { goto LNextSet; //included in BaseSet } HB981.BPSet(UGL.rcBit81.no, UGL.rcBit81, tfbSet: true); //register to BaseSet usedLKLst.Add(UGL.rcBit81.ID); //tfx<<4 | no int no = UGL.rcBit81.no; coverChk.noB |= 1 << no; //qq } else // ........... Cell ............ { UCell UC = UGL.UC; int rc = UC.rc; foreach (var n in UC.FreeB.IEGet_BtoNo(9)) { if (k > 0 && HB981.IsHit(n, rc)) { goto LNextSet; } HB981.BPSet(n, rc, tfbSet: true); } int IDrc = rc << 17 | 1 << 16; usedLKLst.Add(IDrc); //tfx<<4 | no } basUGLs.Add(UGL); //qq } GeneralLogicGen.ChkBas2++; //***** bool niceB = coverChk.Check_BaseSetCondition(HB981); //########## check_rcB9 ########## if (!niceB) { goto LNextSet; } //1..4....8.9.1...5.....63.....13.5.79..3...8..76.2.94.....75.....1...6.4.8....4..2 if (sz >= 2) { int noBP = HB981.nzBit; int noCC = noBP.BitCount(); if (noCC >= 2) { List <int> IX = noBP.IEGet_BtoNo().ToList(); if (sz >= 2) { for (int k = 0; k < noCC; k++) { Bit81 A = new Bit81(), B = new Bit81(); int no = IX[k]; foreach (var P in basUGLs) { if (P.rcBit81 is Bit81) { if (P.rcBit81.no == no) { A |= P.rcBit81; } else { B |= P.rcBit81; } } else { int rc = P.UC.rc; foreach (var n in P.UC.FreeB.IEGet_BtoNo()) { if (n == no) { A.BPSet(rc); } else { B.BPSet(rc); } } } } int nOL = (B & A).Count; if (rnk == 0 && nOL < 2) { goto LNextSet; } if (rnk > 0 && nOL < 1) { goto LNextSet; } // WriteLine("---------- no:{0} sz:{1} rnk:{2} nOL:{3} (A-B):{4} (B-A):{5}", no, sz,rnk, nOL, (A-B).Count, (B-A).Count ); } //1..4....8.9.1...5.....63.....13.5.79..3...8..76.2.94.....75.....1...6.4.8....4..2 } } } //--------------------------------------------------------------------------------------- LBSFound: if (SDK_Ctrl.UGPMan.stageNo == 12 && sz >= 3)// && rnk==1 ){ { WriteLine("\r sz:{0} rnk:{1} jkc:{2}", sz, rnk, jkC); Check_rcbnCondition(sz, rnk, sz, rcbn36, printB: true); basUGLs.ForEach(P => WriteLine(P.ToString("BaseSet"))); } usedLK.Clear(); basUGLs.ForEach(P => usedLK.BPSet(P.SrNum)); //IDrc: rc<<17 | 1<<16 UBasCov UBC = new UBasCov(basUGLs, HB981, sz, usedLK); yield return(UBC); LNextSet: continue; } yield break; }
private void _generalLogicResult(UBasCov UBCc) { try{ Bit81 Q = new Bit81(); foreach (var P in UBCc.covUGLs) { Q |= P.rcbn2.CompressToHitCells(); } foreach (var UC in Q.IEGetRC().Select(rc => pBDL[rc])) { UC.SetCellBgColor(SolBkCr2); } for (int rc = 0; rc < 81; rc++) { int noB = UBCc.HB981.IsHit(rc); if (noB > 0) { pBDL[rc].SetNoBBgColor(noB, AttCr, SolBkCr); } } string msg = "\r BaseSet: "; string msgB = ""; foreach (var P in UBCc.basUGLs) { if (P.rcBit81 is Bit81) { msgB += P.tfx.tfxToString() + $"#{(P.rcBit81.no+1)} "; } else { msgB += P.UC.rc.ToRCString() + " "; } } msg += ToString_SameHouseComp1(msgB); msg += "\r CoverSet: "; string msgC = ""; foreach (var P in UBCc.covUGLs) { if (P.rcBit81 is Bit81) { msgC += P.tfx.tfxToString() + $"#{(P.rcBit81.no+1)} "; } else { msgC += P.UC.rc.ToRCString() + " "; } } msg += ToString_SameHouseComp1(msgC); string st = $"GeneralLogic N:{UBCc.sz} rank:{UBCc.rnk}"; Result = st; msg += $"\r\r ChkBas:{ChkBas4}/{ChkBas1} ChkCov:{ChkCov2}/{ChkCov1}"; ResultLong = st + msg; } catch (Exception ex) { WriteLine(ex.Message); WriteLine(ex.StackTrace); } }
//1..4....8.9.1...5.....63.....13.5.79..3...8..76.2.94.....75.....1...6.4.8....4..2 public IEnumerable <UBasCov> IEGet_CoverSet(UBasCov UBC, int rnk) { if (UGLLst == null) { yield break; } List <UGLink> basUGLs = UBC.basUGLs; Bit981 HB981 = UBC.HB981; Bit324 usedLK = UBC.usedLK; int nzBit = HB981.nzBit; int nzBitCC = nzBit.BitCount(); int sz = UBC.sz; List <UGLink> UGLCovLst = new List <UGLink>(); // UGLCovLst:candidate link #region Create UGLCovLst Bit81 Bcmp = HB981.CompressToHitCells(); foreach (var P in UGLLst.Where(q => !usedLK.IsHit(q.SrNum))) { if (P.rcBit81 is Bit81) //Row, column, block link { if ((nzBit & (1 << P.rcBit81.no)) == 0) { continue; } int B = (HB981._BQ[P.rcBit81.no] & P.rcBit81).BitCount(); if (B == 0) { continue; } // if rank:0, the CoverSet has two or more common items if (rnk == 0 && (HB981._BQ[P.rcBit81.no] & P.rcBit81).Count < 2) { continue; } UGLCovLst.Add(P); } else //Cell { if (nzBitCC <= 1) { continue; } if ((nzBit & P.UC.FreeB) == 0) { continue; } int B = nzBit & P.UC.FreeB; if (B == 0) { continue; } int rc = P.UC.rc; int kcc = 0, kccLim = (rnk == 0)? 2:1; foreach (var no in B.IEGet_BtoNo()) { if (!HB981._BQ[no].IsHit(rc)) { continue; } if (++kcc >= kccLim) { UGLCovLst.Add(P); break; } } } } #endregion if (UGLCovLst.Count < sz + rnk) { yield break; } Bit981 HC981 = new Bit981(); Bit981 HLapB = new Bit981(); Bit981 Can981 = new Bit981(); Combination cmbCvr = new Combination(UGLCovLst.Count, sz + rnk); int nxt = int.MaxValue; while (cmbCvr.Successor(nxt)) { ++GeneralLogicGen.ChkCov1; HC981.Clear(); Array.ForEach(cmbCvr.Index, m => HC981 |= UGLCovLst[m].rcbn2); if (!(HB981 - HC981).IsZero()) { goto LNextSet; //BaseSet is covered? } Bit981 CsubB = HC981 - HB981; if (CsubB.IsZero()) { goto LNextSet; //excludable candidates is exist? } List <UGLink> covUGLs = new List <UGLink>(); Array.ForEach(cmbCvr.Index, m => covUGLs.Add(UGLCovLst[m])); if (rnk == 0) { Can981 = CsubB; } else //if(rnk>0){ { bool SolFound = false; foreach (int n in CsubB.nzBit.IEGet_BtoNo()) { foreach (int rc in CsubB._BQ[n].IEGetRC()) { int kc = covUGLs.Count(Q => Q.IsHit(n, rc)); if (kc == rnk + 1) { Can981.BPSet(n, rc); SolFound = true; } } } if (!SolFound) { continue; } } ++GeneralLogicGen.ChkCov2; //***** UBC.addCoverSet(covUGLs, HC981, Can981, rnk); yield return(UBC); LNextSet: continue; } yield break; }