Exemplo n.º 1
0
        protected override bool IsChildBoardBetter(Knowledge knowledge, Board board, Board childBoard)
        {
            var x = MetaKnowledge.GetColorGraphs(childBoard.Stacks, childBoard.Pot).Select(c => c.Count).ToList();
            var y = MetaKnowledge.GetColorGraphs(board.Stacks, board.Pot).Select(c => c.Count).ToList();

            return(StandardDeviation(x) < StandardDeviation(y));
        }
Exemplo n.º 2
0
        public static bool IsKierstead(Knowledge knowledge, Board board, int edgeIndex, List <int> ordering)
        {
            var gn = knowledge.GraphKnowledge;

            return(gn.LineGraph.IsChoosable(Enumerable.Range(0, gn.LineGraph.N).Select(e =>
            {
                if (e == edgeIndex)
                {
                    return -1;
                }

                var edge = gn.Edges[e];
                var j = Math.Max(ordering.IndexOf(edge.Item1), ordering.IndexOf(edge.Item2));
                var priorStacks = board.Stacks.Where((stack, i) => ordering.IndexOf(i) < j).ToList();

                long allowedList = 0;
                var list = knowledge.GetEdgeColorList(board, e);
                foreach (var alpha in list.EnumerateBits())
                {
                    if (MetaKnowledge.d_H(alpha, priorStacks) % 2 == 0)
                    {
                        allowedList = allowedList.SetBit(alpha);
                    }
                }

                return allowedList;
            }).ToList()));
        }
Exemplo n.º 3
0
        public IEnumerable <KnowledgeTree> EnumerateKnowledgeTrees(Template template)
        {
            var templateKnowledge = Knowledge[template];
            var colorCount        = MetaKnowledge.Naturals().First(n => templateKnowledge.KnowledgeExists(n) && !templateKnowledge.KnowledgeExists(n + 1));

            return(templateKnowledge[colorCount].EnumerateBoardKnowledge().Select(kvp => GenerateKnowledgeTree(kvp.Key)));
        }
Exemplo n.º 4
0
        void FillKnowledgeTree(KnowledgeTree knowledgeTree, Board board, Dictionary <Board, Tuple <int, Board> > seenCache)
        {
            knowledgeTree.Board  = board.ToString();
            knowledgeTree.Number = TreeNodeNumber++;

            var knowledge = Knowledge[board.Template.Value][board.ColorCount][board];

            if (knowledge.Exists())
            {
                if (knowledge.Reason != "good swap")
                {
                    knowledgeTree.Note = knowledge.Reason;
                }
                else
                {
                    Tuple <int, Board> seenBoard;
                    if (seenCache.TryGetValue(board, out seenBoard))
                    {
                        knowledgeTree.Note = "same as #" + seenBoard.Item1;
                        return;
                    }

                    var f         = knowledge.Board.FindPermutation(board);
                    var colors    = Board.ApplyMapping(f, new[] { knowledge.ColorPair.Item1, knowledge.ColorPair.Item2 }).ToList();
                    var colorPair = new Tuple <int, int>(colors[0], colors[1]);

                    foreach (var swap in knowledge.Swaps)
                    {
                        var tempBoard = board.Clone();
                        var moves     = swap.Item2.Select(m => MetaKnowledge.MapMove(f, m)).ToList();
                        tempBoard.DoMoveCombination(moves);

                        var childKnowledgeTree = knowledgeTree.AddChild(swap);
                        childKnowledgeTree.ColorPair = colorPair;
                        FillKnowledgeTree(childKnowledgeTree, tempBoard, seenCache);
                    }
                }
            }
            else
            {
                throw new Exception("something went wrong in building search tree");
            }

            seenCache[board] = new Tuple <int, Board>(knowledgeTree.Number, board);
        }
Exemplo n.º 5
0
        public bool Analyze(Template template, Action <ThoughtProgress> progress = null)
        {
            TotalPositions = 0;
            FixerWonAllNearlyColorableBoards = true;

            EnsureSwapAnalyzerExistence();

            var minimumColorCount = Math.Max(MinPot, template.Sizes.Max());
            var maximumColorCount = Math.Min(MaxPot, template.Sizes.Sum());

            var foundAtLeastOneBoard = false;

            foreach (var colorCount in MetaKnowledge.Interval(minimumColorCount, maximumColorCount))
            {
                if (progress != null)
                {
                    progress(new ThoughtProgress()
                    {
                        IsInitialThought = true
                    });
                }

                var boards = GenerateAllBoards(template, colorCount, progress).ToList();
                if (foundAtLeastOneBoard && boards.Count <= 0)
                {
                    break;
                }

                TotalPositions      += boards.Count;
                foundAtLeastOneBoard = true;

                var breakerWin = !Analyze(boards, progress);
                if (breakerWin && StopAnalysisOnBreakerWin)
                {
                    return(false);
                }

                Knowledge[template].Promote(colorCount);
            }

            return(Knowledge[template].LostBoards.Count() <= 0);
        }
Exemplo n.º 6
0
 List <List <int> > GetVectors(Board b)
 {
     return(MetaKnowledge.GetColorGraphs(b.Stacks, b.Pot).OrderByDescending(c => c.Count).ToList());
 }
Exemplo n.º 7
0
        public Tuple <Graph, Dictionary <int, KeyValuePair <Board, BoardKnowledge> >, Dictionary <Tuple <int, int>, Tuple <List <List <int> >, List <Move>, string> > > GenerateTemplateKnowledgeGraph(Template template)
        {
            var templateKnowledge = Knowledge[template];
            var colorCount        = MetaKnowledge.Naturals().First(n => templateKnowledge.KnowledgeExists(n) && !templateKnowledge.KnowledgeExists(n + 1));

            var nextVertexID = 0;
            var vertexLookup = new Dictionary <Board, int>();
            var swapLookup   = new Dictionary <Tuple <int, int>, Tuple <List <List <int> >, List <Move>, string> >();
            var boardLookup  = new Dictionary <int, KeyValuePair <Board, BoardKnowledge> >();
            var outEdges     = new Dictionary <int, List <int> >();

            foreach (var kvp in templateKnowledge[colorCount].EnumerateBoardKnowledge())
            {
                vertexLookup[kvp.Key]     = nextVertexID;
                boardLookup[nextVertexID] = kvp;
                outEdges[nextVertexID]    = new List <int>();
                nextVertexID++;
            }

            foreach (var b in templateKnowledge.LostBoards)
            {
                vertexLookup[b]           = nextVertexID;
                boardLookup[nextVertexID] = new KeyValuePair <Board, BoardKnowledge>(b, new BoardKnowledge(b, "breaker wins"));
                outEdges[nextVertexID]    = new List <int>();
                nextVertexID++;
            }

            foreach (var kvp in templateKnowledge[colorCount].EnumerateBoardKnowledge())
            {
                if (kvp.Value.Reason == "good swap")
                {
                    var v = vertexLookup[kvp.Key];

                    var f         = kvp.Value.Board.FindPermutation(kvp.Key);
                    var colors    = Board.ApplyMapping(f, new[] { kvp.Value.ColorPair.Item1, kvp.Value.ColorPair.Item2 }).ToList();
                    var colorPair = new Tuple <int, int>(colors[0], colors[1]);

                    foreach (var swap in kvp.Value.Swaps)
                    {
                        var tempBoard = kvp.Key.Clone();
                        var moves     = swap.Item2.Select(m => MetaKnowledge.MapMove(f, m)).ToList();
                        tempBoard.DoMoveCombination(moves);

                        var w = vertexLookup[tempBoard];
                        outEdges[v].Add(w);

                        swapLookup[new Tuple <int, int>(v, w)] = swap;
                    }
                }
            }

            var edgeWeights = new List <int>();

            for (int i = 0; i < nextVertexID; i++)
            {
                for (int j = i + 1; j < nextVertexID; j++)
                {
                    if (outEdges[i].Contains(j))
                    {
                        edgeWeights.Add(1);
                    }
                    else if (outEdges[j].Contains(i))
                    {
                        edgeWeights.Add(-1);
                    }
                    else
                    {
                        edgeWeights.Add(0);
                    }
                }
            }

            return(new Tuple <Graph, Dictionary <int, KeyValuePair <Board, BoardKnowledge> >, Dictionary <Tuple <int, int>, Tuple <List <List <int> >, List <Move>, string> > >(new Graph(edgeWeights), boardLookup, swapLookup));
        }