protected override void Callback() { if (where == GRB.Callback.MIPSOL) // new mip incumbent { var height = fields.GetLength(0); var width = fields.GetLength(1); Console.WriteLine("-----------MIPSOL------------"); Field[,] res = new Field[height, width]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (!inputCw.HasBlock(y, x)) { var letterOrQuestion = GetSolution(fields[y, x]) > 0.5 ? 1 : 0; if (letterOrQuestion == 1) { if (specialQuestionType != null) { for (int type = 0; type < 4; type++) { if ((object)specialQuestionType[y, x, type] != null) { if (GetSolution(specialQuestionType[y, x, type]) > 0.5) { // 0 = Down, then right // 1 = Left, then down // 2 = Right, then down // 3 = Up, then right switch (type) { case 0: res[y, x] = new Question(Question.ArrowType.DownRight); break; case 1: res[y, x] = new Question(Question.ArrowType.LeftDown); break; case 2: res[y, x] = new Question(Question.ArrowType.RightDown); break; case 3: res[y, x] = new Question(Question.ArrowType.UpRight); break; } break; } } } } if (res[y, x] == null) { var qType = GetSolution(questionType[y, x]) > 0.5 ? 1 : 0; res[y, x] = new Question(qType == 0 ? Question.ArrowType.Right : Question.ArrowType.Down); } } else { res[y, x] = new Empty(); } } else { res[y, x] = new Blocked(); } } } Crossword cw = new Crossword(res); cw.Draw(); if (wordCounts != null) { var counts = wordCounts.Select(wc => GetSolution(wc)).ToArray(); var total = counts.Sum(); var hist = new Dictionary <int, int>() { { 2, 0 }, { 3, 18 }, { 4, 24 }, { 5, 20 }, { 6, 18 }, { 7, 12 }, { 8, 4 }, { 9, 4 } }; var diffs = counts.Select((c, i) => Math.Round(Math.Abs(c / total * 100 - hist[i + 2]))).ToArray(); Console.WriteLine("WordCounts Model: " + string.Join(",", counts)); Console.WriteLine("In %: " + string.Join(",", counts.Select(c => Math.Round(c / total * 100)).ToArray())); Console.WriteLine("Diff: " + string.Join(",", diffs)); Console.WriteLine("Diff Total: " + diffs.Average()); } if (saveBest) { var newScore = cw.Score(); double newScoreTotal = 0d; foreach (var k in newScore.Keys) { newScoreTotal += Math.Max(0, newScore[k]); } newScoreTotal /= newScore.Count; for (int i = 0; i < 3; i++) { if (BestScores[i] < newScoreTotal) { var cw_temp = Best[i]; var score_temp = BestScores[i]; Best[i] = cw; BestScores[i] = newScoreTotal; cw.Save("_" + (i + 1)); cw = cw_temp; newScoreTotal = score_temp; } } } Console.WriteLine("-----------------------------"); } }
public WordsSolver(Crossword cwd) { var wordsArray = File.ReadLines(@"C:\Users\Roman Bolzern\Documents\GitHub\Crossword\docs\useful\wordlist.txt").ToArray(); var wordsList = wordsArray.GroupBy(f => f.Length).ToDictionary(f => f.Key, f => f.ToList()); int sizeY = cwd.Grid.GetLength(0); int sizeX = cwd.Grid.GetLength(1); Random rnd = new Random(); for (int it = 0; it < 10000; it++) { var fullList = Enumerable.Range(0, sizeY * sizeX).ToList(); fullList.Shuffle(rnd); Crossword cwdCopy = cwd.DeepClone(); try { foreach (var i in fullList) { int y = i / sizeX; int x = i - y * sizeX; if (cwd.Grid[y, x] is Question) { switch (((Question)cwd.Grid[y, x]).Arrow) { case Question.ArrowType.Down: PutWordY(cwdCopy, wordsList, sizeY, rnd, y + 1, x); break; case Question.ArrowType.DownRight: PutWordX(cwdCopy, wordsList, sizeX, rnd, y + 1, x); break; case Question.ArrowType.LeftDown: PutWordY(cwdCopy, wordsList, sizeY, rnd, y, x - 1); break; case Question.ArrowType.Right: PutWordX(cwdCopy, wordsList, sizeX, rnd, y, x + 1); break; case Question.ArrowType.RightDown: PutWordY(cwdCopy, wordsList, sizeY, rnd, y, x + 1); break; case Question.ArrowType.UpRight: PutWordX(cwdCopy, wordsList, sizeX, rnd, y - 1, x); break; } } } } catch (ArgumentException e) { //cwdCopy.Draw(); Console.WriteLine(it + " " + e.Message); continue; } //worked cwd = cwdCopy; cwd.Draw(); } }