/// <summary> /// berechnet die nächsten Blocker /// </summary> /// <param name="limit">maximale Anzahl der Berechnungen, oder 0, wenn die Berechnung beendet werden soll</param> /// <returns>true, wenn noch weitere Berechnungen anstehen</returns> public bool Next(int limit) { int maxKisten = basisRaum.FeldData.Where(c => c == '$' || c == '*').Count(); if (limit <= 0) // Marker für Abbruch { return false; } switch (status) { #region # case BlockerStatus.Init: // Start einer Blocker-Sucher (eine neue Kistenanzahl wird ausprobiert) case BlockerStatus.Init: { if (suchKistenAnzahl + 1 >= maxKisten) { Abbruch(); return false; // Kisten-Limit erreicht } suchKistenAnzahl++; SammleKistenInit(false); status = BlockerStatus.SammleStartStellungen; return true; } #endregion #region # case BlockerStatus.SammleStartStellungen: // sammelt alle Start-Stellungen mit der entsprechenden Kistenanzahl (sind automatisch auch gleichzeitig Stellungen, mit denen das Ziel erreichbar ist) case BlockerStatus.SammleStartStellungen: { limit--; while (limit > 0 && SammleKistenNext()) limit--; if (SammleKistenNext()) { return true; } else { SammleKistenInit(true); status = BlockerStatus.SammleZielStellungen; return true; } } #endregion #region # case BlockerStatus.SammleZielStellungen: // sammelt alle Ziel-Stellungen, wo jede Kiste auf ein Zielfeld steht case BlockerStatus.SammleZielStellungen: { limit--; while (limit > 0 && SammleKistenNext()) limit--; if (!SammleKistenNext()) status = BlockerStatus.SucheVarianten; return true; } #endregion #region # case BlockerStatus.SucheVarianten: // sucht vorwärts alle möglichen Varianten (eventuell bereits vorhandene Blocker werden beachten) case BlockerStatus.SucheVarianten: { if (prüfListe.SatzAnzahl == 0) { prüfListe.Dispose(); prüfListe = prüfListeSammler; prüfListeSammler = new SokowahnLinearList2(suchKistenAnzahl + 1, Environment.CurrentDirectory + "\\temp\\", listeMax / 32768); if (prüfListe.SatzAnzahl == 0) { prüfListeSammler.Dispose(); status = BlockerStatus.VerschmelzeZielStellungen; verschmelzenRest = prüfListeBöse.SatzAnzahl; return true; } } limit = (int)Math.Min((long)limit, prüfListe.SatzAnzahl); var ergebnisse = Enumerable.Range(0, limit).Select(i => prüfListe.Pop()).AsParallel().SelectMany(stellung => { SokowahnRaum raum = threadRäume[Thread.CurrentThread.ManagedThreadId]; raum.LadeStellung(stellung, 0, 0); return raum.GetVarianten(this); }).Where(x => bekannteStellungen.Get(x.crc64) == 65535).ToArray(); foreach (var stellung in ergebnisse) { int find = bekannteStellungen.Get(stellung.crc64); if (find == 65535) { bekannteStellungen.Add(stellung.crc64, 12345); prüfListeSammler.Add(stellung.raumSpielerPos, stellung.kistenZuRaum); prüfListeBöse.Add(stellung.raumSpielerPos, stellung.kistenZuRaum); } } return true; } #endregion #region # case BlockerStatus.VerschmelzeZielStellungen: // ermittelt (anhand der Rückwärts-Suche) welche der ermittelten Stellungen zum Ziel führen können und markiert diese case BlockerStatus.VerschmelzeZielStellungen: { if (prüfListe.SatzAnzahl == 0) { prüfListe.Dispose(); prüfListe = prüfListeGut; prüfListeGut = new SokowahnLinearList2(suchKistenAnzahl + 1, Environment.CurrentDirectory + "\\temp\\", listeMax / 32768); if (prüfListe.SatzAnzahl == 0) { prüfListe.Dispose(); prüfListeGut.Dispose(); prüfListe = null; prüfListeGut = null; status = BlockerStatus.ErstelleBlocker; tempBlocker = new BlockerFeld[raumAnzahl]; for (int i = 0; i < tempBlocker.Length; i++) { tempBlocker[i].geprüfteStellungen = bekannteStellungen.HashAnzahl; tempBlocker[i].kistenNummerLeer = suchKistenAnzahl; } return true; } } limit = (int)Math.Min((long)limit, prüfListe.SatzAnzahl); verschmelzenRest -= (long)limit; var ergebnisse = Enumerable.Range(0, limit).Select(i => prüfListe.Pop()).AsParallel().SelectMany(stellung => { SokowahnRaum raum = threadRäume[Thread.CurrentThread.ManagedThreadId]; raum.LadeStellung(stellung, 0, 0); return raum.GetVariantenRückwärts(); }).Where(x => bekannteStellungen.Get(x.crc64) < 65535).ToArray(); foreach (var stellung in ergebnisse) { int find = bekannteStellungen.Get(stellung.crc64); if (find == 60000) { continue; } else { prüfListeGut.Add(stellung.raumSpielerPos, stellung.kistenZuRaum); bekannteStellungen.Update(stellung.crc64, 60000); } } return true; } #endregion #region # case BlockerStatus.ErstelleBlocker: // erstellt die Blocker anhand der restlichen Stellungen, welche nicht zum Ziel führten case BlockerStatus.ErstelleBlocker: { limit = (int)Math.Min((long)limit, prüfListeBöse.SatzAnzahl); var ergebnisse = Enumerable.Range(0, limit).Select(i => prüfListeBöse.Pop()).Select(x => { tmpRaum.LadeStellung(x, 0, 0); return tmpRaum.GetStellung(); }).Where(stellung => bekannteStellungen.Get(stellung.crc64) == 12345).ToArray(); foreach (var stellung in ergebnisse) { tempBlocker[stellung.raumSpielerPos].Add(stellung.kistenZuRaum, suchKistenAnzahl); } if (prüfListeBöse.SatzAnzahl == 0) { int startPos = bekannteBlocker.Length; Array.Resize(ref bekannteBlocker, startPos + raumAnzahl); for (int i = 0; i < raumAnzahl; i++) bekannteBlocker[startPos + i] = tempBlocker[i]; for (int i = 0; i < bekannteBlocker.Length; i++) bekannteBlocker[i].kistenNummerLeer = suchKistenAnzahl + 1; long geprüfteStellungenGesamt = 0; for (int i = 0; i < bekannteBlocker.Length; i += raumAnzahl) geprüfteStellungenGesamt += bekannteBlocker[i].geprüfteStellungen; if (geprüfteStellungenGesamt > 100000) SpeichereAlleBlocker(); status = BlockerStatus.Init; bekannteStellungen = null; } return true; } #endregion #region # case BlockerStatus.Fertig: // Blockersuche wurde beendet, (nur noch der Check-Methode steht bereit) case BlockerStatus.Fertig: return false; #endregion default: throw new Exception("Status unbekant: " + status); } }
/// <summary> /// bricht die Berechnung vorzeitig ab (bereits fertig berechnete Blocker jedoch weiter genutzt werden) /// </summary> public void Abbruch() { status = BlockerStatus.Fertig; int maxKisten = basisRaum.FeldData.Where(c => c == '$' || c == '*').Count(); for (int i = 0; i < bekannteBlocker.Length; i++) bekannteBlocker[i].kistenNummerLeer = maxKisten; }
/// <summary> /// Konstruktor /// </summary> /// <param name="blockerDatei">Pfad zur Datei, worin sich eventuell archivierte Blocker-Daten befinden</param> /// <param name="basisRaum">Raum mit dem originalen Spielfeld</param> public SokowahnBlockerB2(string blockerDatei, SokowahnRaum basisRaum) { this.basisRaum = basisRaum; this.blockerDatei = blockerDatei; this.blockerStatus = BlockerStatus.init; //if (file.exists(blockerdatei)) //{ // ladealleblocker(); //} }
/// <summary> /// Konstruktor /// </summary> /// <param name="blockerDatei">Pfad zur Datei, worin sich eventuell archivierte Blocker-Daten befinden</param> /// <param name="basisRaum">Raum mit dem originalen Spielfeld</param> public SokowahnBlocker(string blockerDatei, SokowahnRaum basisRaum) { this.basisRaum = basisRaum; bekannteBlocker = new BlockerFeld[0]; status = BlockerStatus.Init; suchKistenAnzahl = 0; this.blockerDatei = blockerDatei; if (File.Exists(blockerDatei)) { LadeAlleBlocker(); } }
/// <summary> /// berechnet die nächsten Blocker /// </summary> /// <param name="limit">maximale Anzahl der Berechnungen, oder 0, wenn die Berechnung beendet werden soll</param> /// <returns>true, wenn noch weitere Berechnungen anstehen</returns> public bool Next(int limit) { switch (blockerStatus) { #region # case BlockerStatus.init: case BlockerStatus.init: { sammlerKistenAnzahl++; if (sammlerKistenAnzahl == basisRaum.KistenAnzahl) { Abbruch(); return false; } sammlerAbfrage = SammlerBerechneZielStellungen().GetEnumerator(); sammlerHash = new SokowahnHash_Index16Multi(); sammlerStats = new long[60001]; rückwärtsSucher = Enumerable.Range(0, maxVorschau).Select(x => new SokowahnLinearList2(sammlerKistenAnzahl + 1, Environment.CurrentDirectory + "\\temp\\", multi32k)).ToArray(); rückwärtsTiefe = 0; alleStellungen = new SokowahnLinearList2(sammlerKistenAnzahl + 1, Environment.CurrentDirectory + "\\temp\\", multi32k * 4); alleBlockerHash = new HashSet<ulong>(); char[] feldData = basisRaum.FeldData; int feldBreite = basisRaum.FeldBreite; bool[] spielerRaum = SokowahnStaticTools.SpielfeldRaumScan(feldData, feldBreite); raumAnzahl = spielerRaum.Count(x => x); tempBlocker = new SokowahnBlockerB.BlockerFeld[raumAnzahl]; for (int i = 0; i < tempBlocker.Length; i++) tempBlocker[i].kistenNummerLeer = sammlerKistenAnzahl; blockerStatus = BlockerStatus.sammleZiele; return true; } #endregion #region # case BlockerStatus.sammleZiele: case BlockerStatus.sammleZiele: { while (limit-- > 0 && sammlerAbfrage.MoveNext()) { var satz = sammlerAbfrage.Current; if (sammlerHash.Get(satz.crc64) == 65535) { sammlerHash.Add(satz.crc64, 60000); sammlerStats[60000]++; rückwärtsSucher[0].Add(satz.raumSpielerPos, satz.kistenZuRaum); alleStellungen.Add(satz.raumSpielerPos, satz.kistenZuRaum); } } if (limit >= 0) { tempRaum = new SokowahnRaum(basisRaum); tempRaum.KistenAnzahl = sammlerKistenAnzahl; threadRäume = Enumerable.Range(0, 256).Select(i => new SokowahnRaum(tempRaum)).ToArray(); blockerStatus = BlockerStatus.suchModus; } return true; } #endregion #region # case BlockerStatus.suchModus: case BlockerStatus.suchModus: { if (SucheRückwärts(limit)) return true; Array.Resize(ref bekannteBlockerHashes, sammlerKistenAnzahl); bekannteBlockerHashes[sammlerKistenAnzahl - 1] = sammlerHash; Array.Resize(ref bekannteSammlerStats, sammlerKistenAnzahl); var tmp = sammlerStats.Reverse().ToList(); while (tmp[tmp.Count - 1] == 0) tmp.RemoveAt(tmp.Count - 1); bekannteSammlerStats[sammlerKistenAnzahl - 1] = tmp.ToArray(); for (int i = 0; i < rückwärtsSucher.Length; i++) rückwärtsSucher[i].Dispose(); blockerStatus = BlockerStatus.blockerSuche; return true; } #endregion #region # case BlockerStatus.blockerSuche: case BlockerStatus.blockerSuche: { limit = (int)Math.Min((long)limit, alleStellungen.SatzAnzahl); if (limit == 0) { int startPos = bekannteBlocker.Length; Array.Resize(ref bekannteBlocker, startPos + raumAnzahl); for (int i = 0; i < raumAnzahl; i++) bekannteBlocker[startPos + i] = tempBlocker[i]; for (int i = 0; i < bekannteBlocker.Length; i++) { bekannteBlocker[i].geprüfteStellungen = sammlerHash.HashAnzahl; bekannteBlocker[i].kistenNummerLeer = sammlerKistenAnzahl + 1; } long geprüfteStellungenGesamt = 0; for (int i = 0; i < bekannteBlocker.Length; i += raumAnzahl) geprüfteStellungenGesamt += bekannteBlocker[i].geprüfteStellungen; for (int i = 0; i < bekannteBlocker.Length; i++) bekannteBlocker[i].Sortieren(); blockerStatus = BlockerStatus.init; return true; } var stellungen = alleStellungen.Pop(limit); int satzGröße = alleStellungen.SatzGröße; var ergebnisse = Enumerable.Range(0, limit) #if !parallelDeaktivieren .AsParallel() #if parallelGeordnet .AsOrdered() #endif #endif .SelectMany(stellung => { SokowahnRaum raum = threadRäume[Thread.CurrentThread.ManagedThreadId]; raum.LadeStellung(stellungen, stellung * satzGröße, 0); // return raum.GetVarianten(this).Where(x => sammlerHash.Get(x.crc64) == 65535); var ausgabe = raum.GetVarianten(this).Where(x => { if (sammlerHash.Get(x.crc64) == 65535) return true; x.zugTiefe = 60000 - sammlerHash.Get(x.crc64); //string dbg = x.Debug(basisRaum); return false; } ); return ausgabe; }).ToArray(); foreach (var stellung in ergebnisse) { if (alleBlockerHash.Contains(stellung.crc64)) continue; alleBlockerHash.Add(stellung.crc64); tempBlocker[stellung.raumSpielerPos].Add(stellung.kistenZuRaum, sammlerKistenAnzahl); } return true; } #endregion #region # case BlockerStatus.bereit: case BlockerStatus.bereit: { return false; } #endregion default: throw new NotImplementedException(); } }