public static int ConsoleGTP(string filename, bool loadSilent) { MemFile lMemFile = new MemFile(filename); if (!lMemFile.IsError()) return ConsoleGTP(lMemFile, loadSilent); Console.WriteLine("unable to load file: " + filename + " because: " + lMemFile.GetErrorMessage()); return -1; }
public DFAMatrix(string filename) { MemFile lMemFile = new MemFile(filename); if (lMemFile.IsError()) { SetErrorMessage(lMemFile); return; } PatternCollection lPatternCollection = new PatternCollection(lMemFile); if (lPatternCollection.IsError()) SetErrorMessage(lPatternCollection); _LoadMatrix(lMemFile, lPatternCollection); }
public static int ConsoleGTP(MemFile file, bool loadSilent) { GoBoard lGoBoard = new GoBoard(9); GTPGoBoard lGTPGoBoard = new GTPGoBoard(lGoBoard); GTPCommConsole lGTPCommConsole = new GTPCommConsole(); GTPEngine lGTPEngine = new GTPEngine(lGTPGoBoard, lGTPCommConsole); lGTPCommConsole.Silent = loadSilent; if (file != null) while (!file.EOF) { string lLine = file.ReadLine(); Console.Error.WriteLine(lLine.Trim('\n')); lGTPCommConsole.SendToEngine(lLine + '\n'); } lGTPCommConsole.Silent = false; lGTPCommConsole.Listen(); return 0; }
public static int IGS31() { GameRecords lGameRecords = new GameRecords(); SGFCollection lSGFCollection = new SGFCollection(@"Regression\IGS_31\Source\IGS_31_counted.sgf"); lGameRecords.Load(lSGFCollection, true); Dictionary<SafetyStatus, int> lSafetyUsage = new Dictionary<SafetyStatus, int>(); foreach (GameRecord lGameRecord in lGameRecords.Games) { GoBoard lGoBoard = new GoBoard(19); GameRecordBoardAdapter.Apply(lGameRecord, lGoBoard); //lGameRecord.Apply(lGoBoard); foreach (GoCell lCell in lGoBoard.Cells) { SafetyStatus lSafetyStatus = lGoBoard.GetSafetyStatus(lCell.Index); if (lSafetyUsage.ContainsKey(lSafetyStatus)) lSafetyUsage[lSafetyStatus] += 1; else lSafetyUsage[lSafetyStatus] = 1; } Console.Write("."); } Console.WriteLine(); foreach (SafetyStatus lSafetyStatus in lSafetyUsage.Keys) { Console.Write(lSafetyStatus.ToInteger()); Console.Write(" | {0}", lSafetyStatus); Console.WriteLine(" | " + lSafetyUsage[lSafetyStatus].ToString()); } MemFile lMemFile = new MemFile(); lMemFile.WriteLine(lGameRecords.ToString()); lMemFile.SaveFile(@"Regression\IGS_31\IGS_31-combined.sgf"); SafetySolverType lSafetySolverType = SafetySolverType.Muller97; for (int i = 0; i < lGameRecords.Games.Count; i++) { GoBoard lGoBoard = new GoBoard(19); GameRecordBoardAdapter.Apply(lGameRecords[i], lGoBoard); //lGameRecords[i].Apply(lGoBoard); GameRecordBoardAdapter.UpdateTerritory(lGameRecords[i], lGoBoard); //lGameRecords[i].UpdateTerritory(lGoBoard); //Console.WriteLine(lGoBoard.ToString()); Console.Write(i.ToString()); Console.Write(" : "); Console.Write(lGoBoard.CountSafePoints(Color.Both, lSafetySolverType).ToString()); Console.Write(" "); Console.Write(lGoBoard.CountSafePoints(Color.Black, lSafetySolverType).ToString()); Console.Write(" "); Console.WriteLine(lGoBoard.CountSafePoints(Color.White, lSafetySolverType).ToString()); } for (int i = 0; i < lGameRecords.Games.Count; i++) { MemFile lMemFile2 = new MemFile(); lMemFile2.WriteLine(lGameRecords[i].ToString()); lMemFile2.SaveFile(@"Regression\IGS_31\IGS_31-" + ((i + 1).ToString()) + ".sgf"); } return 0; }
public SGFProperty(MemFile memFile, string propertyID) { Read(memFile, propertyID); }
public bool Read(MemFile memFile, string propertyID) { PropertyID = propertyID; Text = string.Empty; while (true) { char c = memFile.Get(); if (c == ']') break; if (c == '\\') c = memFile.Get(); Text = Text + c; if (memFile.EOF) return SetErrorMessage("Unexpected EOF."); } return true; }
public SGFNode(MemFile memFile) { Properties = new List<SGFProperty>(); Read(memFile); }
public bool Load(MemFile memFile, bool continueOnError) { ClearErrorMessages(); StringBuilder lBuffer = new StringBuilder(); bool pStarted = false; int lLineNbr = 0; while (!memFile.EOF) { string lLine = memFile.ReadLine('\n'); if (lLine.Trim().ToUpper() == "###MATRIX") return IsError(); if (lLine.Length > 0) { if (memFile.EOF) lBuffer.Append(lLine); if ((lLine[0] == 'P') || memFile.EOF) { if (pStarted) { // EOF of a Pattern Pattern lPattern = new Pattern(lBuffer.ToString(), lLineNbr); if (lPattern.IsOk()) Add(lPattern); else { SetErrorMessage("ERROR: Unable to compile pattern - Line # " + lLineNbr.ToString() + ":", lPattern); Console.Error.WriteLine("ERROR: Unable to compile pattern."); Console.Error.WriteLine(lPattern.GetErrorMessage()); // return true; if (!continueOnError) return false; } lBuffer = new StringBuilder(); pStarted = false; } else { pStarted = true; lLineNbr = memFile.LineNbr; } } if (lLine[0] == 'P') pStarted = true; if (pStarted) lBuffer.Append(lLine); } } return true; }
public bool Read(MemFile memFile) { char c = memFile.Get(); if (c != '(') { return(SetErrorMessage("Expecting open-parentheses, found: " + c.ToString())); } while (true) { c = memFile.Peek(); if (c == '(') { SGFSequence lSGFSequence = new SGFSequence(memFile); if (lSGFSequence.IsError()) { return(SetErrorMessage(lSGFSequence)); } AddVariation(lSGFSequence); char p = memFile.Get(); if (p != ')') // eat this character { return(SetErrorMessage("Expecting closing-parenthese, found: " + p.ToString())); } } else { if (c == ')') { break; } if (c == ';') { SGFNode lSGFNode = new SGFNode(memFile); if (lSGFNode.IsError()) { return(SetErrorMessage(lSGFNode)); } AddNode(lSGFNode); } else { memFile.Get(); // eat this character } } if (memFile.EOF) { return(SetErrorMessage("Unexpected EOF.")); } } return(true); }
public bool Read(MemFile memFile) { char c = memFile.Get(); if (c != ';') { return(SetErrorMessage("Expecting semi-colon, found: " + c.ToString())); } while (true) { if (memFile.EOF) { return(true); } c = memFile.Peek(); if (c == '(') { break; // variation } if ((c == ';') || (c == ')')) { break; } if (Char.IsLetter(c)) { SGFProperty lSGFProperty = new SGFProperty(memFile); if (lSGFProperty.IsError()) { return(SetErrorMessage(lSGFProperty)); } AddProperty(lSGFProperty); while (true) { c = memFile.Peek(); if (c == ')') { break; } if (c == '(') { break; } if (c == '[') { memFile.Get(); // eat this character SGFProperty lSGFProperty2 = new SGFProperty(memFile, lSGFProperty.PropertyID); if (lSGFProperty2.IsError()) { return(SetErrorMessage(lSGFProperty2)); } ; AddProperty(lSGFProperty2); } else if (!Char.IsLetter(c)) { memFile.Get(); // eat this character if (memFile.EOF) { return(SetErrorMessage("Unexpected EOF.")); } } else { break; } } } else { memFile.Get(); // eat this character } if (memFile.EOF) { return(SetErrorMessage("Unexpected EOF.")); } } return(true); }
public SGFNode(MemFile memFile) { Properties = new List <SGFProperty>(); Read(memFile); }
public static int CZD() { GameRecords lGameRecords = new GameRecords(); lGameRecords.Load(@"Regression\CZD\Source", @"CZD_*.sgf", true); Dictionary <SafetyStatus, int> lSafetyUsage = new Dictionary <SafetyStatus, int>(); foreach (GameRecord lGameRecord in lGameRecords.Games) { GoBoard lGoBoard = new GoBoard(19); GameRecordBoardAdapter.Apply(lGameRecord, lGoBoard); //lGameRecord.Apply(lGoBoard); foreach (GoCell lCell in lGoBoard.Cells) { SafetyStatus lSafetyStatus = lGoBoard.GetSafetyStatus(lCell.Index); if (lSafetyUsage.ContainsKey(lSafetyStatus)) { lSafetyUsage[lSafetyStatus] += 1; } else { lSafetyUsage[lSafetyStatus] = 1; } } Console.Write("."); } Console.WriteLine(); foreach (SafetyStatus lSafetyStatus in lSafetyUsage.Keys) { Console.Write(lSafetyStatus.ToInteger()); Console.Write(" | {0}", lSafetyStatus); Console.WriteLine(" | " + lSafetyUsage[lSafetyStatus].ToString()); } MemFile lMemFile = new MemFile(); lMemFile.WriteLine(lGameRecords.ToString()); lMemFile.SaveFile(@"Regression\CZD\CZD-combined.sgf"); SafetySolverType lSafetySolverType = SafetySolverType.Muller04; for (int i = 0; i < lGameRecords.Games.Count; i++) { GoBoard lGoBoard = new GoBoard(19); GameRecordBoardAdapter.Apply(lGameRecords[i], lGoBoard); //lGameRecords[i].Apply(lGoBoard); GameRecordBoardAdapter.UpdateTerritory(lGameRecords[i], lGoBoard); //lGameRecords[i].UpdateTerritory(lGoBoard); //Console.WriteLine(lGoBoard.ToString()); Console.Write(i.ToString()); Console.Write(" : "); Console.Write(lGoBoard.CountSafePoints(Color.Both, lSafetySolverType).ToString()); Console.Write(" "); Console.Write(lGoBoard.CountSafePoints(Color.Black, lSafetySolverType).ToString()); Console.Write(" "); Console.WriteLine(lGoBoard.CountSafePoints(Color.White, lSafetySolverType).ToString()); } for (int i = 0; i < lGameRecords.Games.Count; i++) { MemFile lMemFile2 = new MemFile(); lMemFile2.WriteLine(lGameRecords[i].ToString()); lMemFile2.SaveFile(@"Regression\CZD\" + lGameRecords[i].GameName.Insert(3, "-") + ".sgf"); } return(0); }
public static bool ExecuteTest(MemFile memFile, string directory) { GoBoard lGoBoard = new GoBoard(19); GTPGoBoard lGTPGoBoard = new GTPGoBoard(lGoBoard); GTPCommInternal lGTPCommInternal = new GTPCommInternal(); GTPEngine lGTPEngine = new GTPEngine(lGTPGoBoard, lGTPCommInternal); lGTPGoBoard.Directory = directory; GTPCommand lGTcommand = new GTPCommand(""); string lInput = string.Empty; int[] lRegressionResults = new int[5]; while (!memFile.EOF) { string lBuffer = memFile.ReadLine('\n'); if ((lBuffer.Length >= 4) && (lBuffer.Substring(0, 4) == "quit")) break; if ((lBuffer.Length >= 2) && (lBuffer.Substring(0, 2) == "#?")) { GTPRegressionPattern lPattern = new GTPRegressionPattern(lBuffer); GTPRegressionResult lRegressionResult = GTPRegressionPattern.Test(lPattern, lInput); lRegressionResults[(int)lRegressionResult]++; switch (lRegressionResult) { case GTPRegressionResult.passed: /* Console.WriteLine("PASSED");*/ break; case GTPRegressionResult.PASSED: Console.WriteLine(lGTcommand.ToString()); Console.WriteLine(lGTcommand.CommandNbr.ToString() + " unexpected PASS!"); break; case GTPRegressionResult.failed: Console.WriteLine(lGTcommand.ToString()); Console.WriteLine(lGTcommand.CommandNbr.ToString() + " failed: Correct '" + lPattern + "', got '" + Proper(lInput) + "'"); break; case GTPRegressionResult.FAILED: Console.WriteLine(lGTcommand.ToString()); Console.WriteLine(lGTcommand.CommandNbr.ToString() + " unexpected failure: Correct '" + lPattern + "', got '" + Proper(lInput) + "'"); break; case GTPRegressionResult.ignore: Console.WriteLine(lGTcommand.ToString()); Console.WriteLine(lGTcommand.CommandNbr.ToString() + " ignoring '" + lPattern + "', got '" + Proper(lInput) + "'"); break; default: Console.WriteLine("ERROR!!!"); break; } } else if (lBuffer.Length > 0) { GTPCommand lGTcommand2 = new GTPCommand(lBuffer); if (lGTcommand2.Command.Length != 0) { lGTPCommInternal.SendToEngine(lBuffer); lGTPCommInternal.SendToEngine("\n"); lInput = lGTPCommInternal.GetResponse(); lGTcommand = lGTcommand2; } } } for (int lRegressionResultIndex = 0; lRegressionResultIndex < 5; lRegressionResultIndex++) Console.WriteLine((GTPRegressionResult)lRegressionResultIndex + " " + lRegressionResults[lRegressionResultIndex]); return (lRegressionResults[3] == 0); // false, only for unexpected failures }
public static bool ExecuteTest(string filename, string directory) { MemFile lMemFile = new MemFile(filename); return ExecuteTest(lMemFile, directory); }
public PatternCollection(MemFile memFile) { Patterns = new List <Pattern>(); Load(memFile, true); }
public bool Load(MemFile memFile, bool continueOnError) { ClearErrorMessages(); StringBuilder lBuffer = new StringBuilder(); bool pStarted = false; int lLineNbr = 0; while (!memFile.EOF) { string lLine = memFile.ReadLine('\n'); if (lLine.Trim().ToUpper() == "###MATRIX") { return(IsError()); } if (lLine.Length > 0) { if (memFile.EOF) { lBuffer.Append(lLine); } if ((lLine[0] == 'P') || memFile.EOF) { if (pStarted) { // EOF of a Pattern Pattern lPattern = new Pattern(lBuffer.ToString(), lLineNbr); if (lPattern.IsOk()) { Add(lPattern); } else { SetErrorMessage("ERROR: Unable to compile pattern - Line # " + lLineNbr.ToString() + ":", lPattern); Console.Error.WriteLine("ERROR: Unable to compile pattern."); Console.Error.WriteLine(lPattern.GetErrorMessage()); // return true; if (!continueOnError) { return(false); } } lBuffer = new StringBuilder(); pStarted = false; } else { pStarted = true; lLineNbr = memFile.LineNbr; } } if (lLine[0] == 'P') { pStarted = true; } if (pStarted) { lBuffer.Append(lLine); } } } return(true); }
public static bool ExecuteTest(MemFile memFile, string directory) { GoBoard lGoBoard = new GoBoard(19); GTPGoBoard lGTPGoBoard = new GTPGoBoard(lGoBoard); GTPCommInternal lGTPCommInternal = new GTPCommInternal(); GTPEngine lGTPEngine = new GTPEngine(lGTPGoBoard, lGTPCommInternal); lGTPGoBoard.Directory = directory; GTPCommand lGTcommand = new GTPCommand(""); string lInput = string.Empty; int[] lRegressionResults = new int[5]; while (!memFile.EOF) { string lBuffer = memFile.ReadLine('\n'); if ((lBuffer.Length >= 4) && (lBuffer.Substring(0, 4) == "quit")) { break; } if ((lBuffer.Length >= 2) && (lBuffer.Substring(0, 2) == "#?")) { GTPRegressionPattern lPattern = new GTPRegressionPattern(lBuffer); GTPRegressionResult lRegressionResult = GTPRegressionPattern.Test(lPattern, lInput); lRegressionResults[(int)lRegressionResult]++; switch (lRegressionResult) { case GTPRegressionResult.passed: /* Console.WriteLine("PASSED");*/ break; case GTPRegressionResult.PASSED: Console.WriteLine(lGTcommand.ToString()); Console.WriteLine(lGTcommand.CommandNbr.ToString() + " unexpected PASS!"); break; case GTPRegressionResult.failed: Console.WriteLine(lGTcommand.ToString()); Console.WriteLine(lGTcommand.CommandNbr.ToString() + " failed: Correct '" + lPattern + "', got '" + Proper(lInput) + "'"); break; case GTPRegressionResult.FAILED: Console.WriteLine(lGTcommand.ToString()); Console.WriteLine(lGTcommand.CommandNbr.ToString() + " unexpected failure: Correct '" + lPattern + "', got '" + Proper(lInput) + "'"); break; case GTPRegressionResult.ignore: Console.WriteLine(lGTcommand.ToString()); Console.WriteLine(lGTcommand.CommandNbr.ToString() + " ignoring '" + lPattern + "', got '" + Proper(lInput) + "'"); break; default: Console.WriteLine("ERROR!!!"); break; } } else if (lBuffer.Length > 0) { GTPCommand lGTcommand2 = new GTPCommand(lBuffer); if (lGTcommand2.Command.Length != 0) { lGTPCommInternal.SendToEngine(lBuffer); lGTPCommInternal.SendToEngine("\n"); lInput = lGTPCommInternal.GetResponse(); lGTcommand = lGTcommand2; } } } for (int lRegressionResultIndex = 0; lRegressionResultIndex < 5; lRegressionResultIndex++) { Console.WriteLine((GTPRegressionResult)lRegressionResultIndex + " " + lRegressionResults[lRegressionResultIndex]); } return(lRegressionResults[3] == 0); // false, only for unexpected failures }
public SGFProperty(MemFile memFile) { PropertyID = string.Empty; Text = string.Empty; Read(memFile); }
public SGFSequence(MemFile memFile) { Nodes = new List <SGFNode>(); Variations = new List <SGFSequence>(); Read(memFile); }
public bool Load(string filename, bool continueOnError) { MemFile lMemFile = new MemFile(filename); if (lMemFile.IsError()) return SetErrorMessage(lMemFile); return Load(lMemFile, continueOnError); }
public SGFSequence(MemFile memFile) { Nodes = new List<SGFNode>(); Variations = new List<SGFSequence>(); Read(memFile); }
public PatternCollection(MemFile memFile) { Patterns = new List<Pattern>(); Load(memFile, true); }
public bool Read(MemFile memFile) { char c = memFile.Get(); if (c != '(') return SetErrorMessage("Expecting open-parentheses, found: " + c.ToString()); while (true) { c = memFile.Peek(); if (c == '(') { SGFSequence lSGFSequence = new SGFSequence(memFile); if (lSGFSequence.IsError()) return SetErrorMessage(lSGFSequence); AddVariation(lSGFSequence); char p = memFile.Get(); if (p != ')') // eat this character return SetErrorMessage("Expecting closing-parenthese, found: " + p.ToString()); } else { if (c == ')') break; if (c == ';') { SGFNode lSGFNode = new SGFNode(memFile); if (lSGFNode.IsError()) return SetErrorMessage(lSGFNode); AddNode(lSGFNode); } else memFile.Get(); // eat this character } if (memFile.EOF) return SetErrorMessage("Unexpected EOF."); } return true; }
public bool Read(MemFile memFile) { char c = memFile.Get(); if (c != ';') return SetErrorMessage("Expecting semi-colon, found: " + c.ToString()); while (true) { if (memFile.EOF) return true; c = memFile.Peek(); if (c == '(') break; // variation if ((c == ';') || (c == ')')) break; if (Char.IsLetter(c)) { SGFProperty lSGFProperty = new SGFProperty(memFile); if (lSGFProperty.IsError()) return SetErrorMessage(lSGFProperty); AddProperty(lSGFProperty); while (true) { c = memFile.Peek(); if (c == ')') break; if (c == '(') break; if (c == '[') { memFile.Get(); // eat this character SGFProperty lSGFProperty2 = new SGFProperty(memFile, lSGFProperty.PropertyID); if (lSGFProperty2.IsError()) return SetErrorMessage(lSGFProperty2); ; AddProperty(lSGFProperty2); } else if (!Char.IsLetter(c)) { memFile.Get(); // eat this character if (memFile.EOF) return SetErrorMessage("Unexpected EOF."); } else break; } } else memFile.Get(); // eat this character if (memFile.EOF) return SetErrorMessage("Unexpected EOF."); } return true; }
public bool LoadSGFFromMemory(string pSGF) { MemFile lMemFile = new MemFile(); lMemFile.Write(pSGF); lMemFile.Reset(); return LoadSGFFile(lMemFile); }
public bool SaveSGFFile(string filename) { MemFile lMemFile = new MemFile(); lMemFile.Clear(); lMemFile.Write(ToString()); return lMemFile.SaveFile(filename); }
public bool Read(MemFile memFile) { PropertyID = string.Empty; Text = string.Empty; while (true) { char c = memFile.Get(); if (c == '[') break; if (Char.IsLetter(c)) PropertyID = PropertyID + Char.ToUpper(c); if (memFile.EOF) return SetErrorMessage("Unexpected EOF."); } while (true) { char c = memFile.Get(); if (c == ']') break; if (c == '\\') c = memFile.Get(); Text = Text + c; if (memFile.EOF) return SetErrorMessage("Unexpected EOF."); } return true; }
public bool LoadSGFFile(string filename) { MemFile lMemFile = new MemFile(filename); if (lMemFile.IsError()) return SetErrorMessage(lMemFile); return LoadSGFFile(lMemFile); }
// temp. for testing static void MatrixCompileToFile() { PatternCollection lPatterns = new PatternCollection(); if (!lPatterns.Load(@"Patterns/fuseki9.db")) { Console.WriteLine("ERROR: Unable to load patterns"); return; } Console.WriteLine("STATUS: Loaded " + lPatterns.Count + " Patterns."); SimpleTimer lTimer = new SimpleTimer(); Console.WriteLine(lTimer.StartTime.ToString()); DFAMatrixBuilder lDFAMatrixBuilder = new DFAMatrixBuilder(); lDFAMatrixBuilder.Add(lPatterns); Console.WriteLine(DateTime.Now.ToString()); lDFAMatrixBuilder.BuildThreaded(); Console.WriteLine(DateTime.Now.ToString()); Console.WriteLine("Seconds: " + lTimer.SecondsElapsed.ToString()); MemFile lMemFile = new MemFile(); lMemFile.Write(lDFAMatrixBuilder.GetMatrix().ToString()); lMemFile.SaveFile(@"Patterns/fuseki9.cdb"); }
public bool LoadSGFFile(MemFile memFile) { Sequence.Clear(); while (!memFile.EOF) { char c = memFile.Peek(); if (c == '(') { SGFSequence lSGFSequence = new SGFSequence(memFile); if (lSGFSequence.IsError()) return SetErrorMessage(lSGFSequence); Sequence.Add(lSGFSequence); } else memFile.Get(); // eat this character } return true; }
protected bool _LoadMatrix(MemFile memFile, PatternCollection patternCollection) { try { int lMatrixSize = Convert.ToInt32(memFile.ReadLine()); DFANodes = new List<DFANode>(lMatrixSize); for (int i = 1; i < lMatrixSize; i++) { // Console.Error.Write(memFile.LineNbr.ToString() + " - " + i.ToString() + " - "+lMatrixSize.ToString()); DFANode lDFANode = new DFANode(); for (int z = 0; z < 4; z++) lDFANode[z] = Convert.ToInt32(memFile.ReadPart('\t').Trim()); int lNodePatterns = Convert.ToInt32(memFile.ReadPart('\t').Trim()); if (lNodePatterns > 0) lDFANode.Attributes = new List<PatternKey>(lNodePatterns); for (int p = 0; p < lNodePatterns; p++) { int lPatternTransformation = Convert.ToInt32(memFile.ReadPart(':').Trim()); string lPatternKey = memFile.ReadPart('\t').Trim(); int lTransformation = Convert.ToInt32(memFile.ReadPart('\t').Trim()); Pattern lPattern = patternCollection.FindByKey(lPatternKey); lDFANode.Add(new PatternKey(lPattern, lTransformation)); } memFile.ReadLine(); DFANodes.Add(lDFANode); } } catch (Exception e) { return SetErrorMessage("Matrix Error - Line # " + memFile.LineNbr.ToString() + ":", e); } return false; }
public static bool ExecuteTest(string filename, string directory) { MemFile lMemFile = new MemFile(filename); return(ExecuteTest(lMemFile, directory)); }