コード例 #1
0
ファイル: KiersteadAnalyzer.cs プロジェクト: landon/WebGraphs
        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()));
        }
コード例 #2
0
ファイル: ReductionAnalyzer.cs プロジェクト: landon/WebGraphs
        public bool Analyze(Knowledge knowledge, Board board)
        {
            var g = knowledge.GraphKnowledge.Graph;

            foreach (var e in knowledge.GraphKnowledge.Edges)
            {
                var x = Math.Min(e.Item1, e.Item2);
                var y = Math.Max(e.Item1, e.Item2);

                var common = board.Stacks[x] & board.Stacks[y];
                var stacks = board.Stacks.ToList();

                foreach (var c in common.EnumerateBits())
                {
                    stacks[x] = board.Stacks[x].ClearBit(c);
                    stacks[y] = board.Stacks[y].ClearBit(c);

                    if (g.DegreeCondition(stacks, board.Pot, X => X.Contains(x) && X.Contains(y) ? 1 : 0))
                    {
                        knowledge[board.Template.Value][board.ColorCount].AddWin(board, string.Format("color {0}{1} with {2}", x + 1, y + 1, c));
                        return(true);
                    }
                }
            }

            return(false);
        }
コード例 #3
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));
        }
コード例 #4
0
        public Mind(Graph g)
        {
            FindCleanestWin     = true;
            SuperabundantOnly   = true;
            NearlyColorableOnly = false;
            MaxPot    = MetaKnowledge.Infinity;
            Knowledge = new Knowledge(g);

            BoardAnalysisPipeline = new List <IBoardAnalyzer>();
            BoardAnalysisPipeline.Add(new ColoringAnalyzer());
        }
コード例 #5
0
        public bool Analyze(Knowledge knowledge, Board board)
        {
            var commonColor = board.Stacks.Aggregate(-1L, (t, s) => t & s) != 0;

            if (commonColor)
            {
                knowledge[board.Template.Value][board.ColorCount].AddWin(board, Reason);
            }

            return(commonColor);
        }
コード例 #6
0
        public bool Analyze(Knowledge knowledge, Board board)
        {
            var lineGraph = knowledge.GraphKnowledge.LineGraph;
            var canColor  = lineGraph.IsChoosable(Enumerable.Range(0, lineGraph.N).Select(e => knowledge.GetEdgeColorList(board, e)).ToList());

            if (canColor)
            {
                knowledge[board.Template.Value][board.ColorCount].AddWin(board, Reason);
            }

            return(canColor);
        }
コード例 #7
0
        public static bool ColorableWithoutEdge(Knowledge knowledge, List <long> stacks, int edgeIndex)
        {
            var lg = knowledge.GraphKnowledge.LineGraph;

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

                return knowledge.GetEdgeColorList(stacks, e);
            }).ToList()));
        }
コード例 #8
0
        public bool Analyze(Knowledge knowledge, Board board)
        {
            foreach (var template in SubTemplates)
            {
                int color;
                int x;
                if (ContainsSuberabundantSubStacksAboveTemplate(knowledge, board, template, out color, out x))
                {
                    knowledge[board.Template.Value][board.ColorCount].AddWin(board, string.Format("set L({0}) = L({0}) - {1}", x + 1, color));
                    return(true);
                }
            }

            return(false);
        }
コード例 #9
0
ファイル: KiersteadAnalyzer.cs プロジェクト: landon/WebGraphs
        public static bool IsKierstead(Knowledge knowledge, Board board, int edgeIndex, bool enforceDegreeRequirement = false)
        {
            foreach (var ordering in knowledge.GraphKnowledge.ConnectedOrderings.Value[knowledge.GraphKnowledge.Edges[edgeIndex]])
            {
                var penultimate = ordering[ordering.Count - 2];
                if (enforceDegreeRequirement && board.Stacks[penultimate].PopulationCount() <= knowledge.GraphKnowledge.Graph.Degree(penultimate))
                {
                    continue;
                }

                if (IsKierstead(knowledge, board, edgeIndex, ordering))
                {
                    return(true);
                }
            }

            return(false);
        }
コード例 #10
0
ファイル: KiersteadAnalyzer.cs プロジェクト: landon/WebGraphs
        public static IEnumerable <int> EnumerateKiersteadEdgeIndices(Knowledge knowledge, Board board, bool enforceDegreeRequirement = false, string reason = "")
        {
            var g = knowledge.GraphKnowledge.Graph;
            var internalVertices = g.Vertices.Where(v => g.Degree(v) >= 2).ToList();

            foreach (var edgeIndex in knowledge.GraphKnowledge.EdgeIndices)
            {
                var e = knowledge.GraphKnowledge.Edges[edgeIndex];
                if (internalVertices.Contains(e.Item1) && internalVertices.Contains(e.Item2))
                {
                    continue;
                }

                if (IsKierstead(knowledge, board, edgeIndex))
                {
                    knowledge[board.Template.Value][board.ColorCount].AddWin(board, reason);
                    yield return(edgeIndex);
                }
            }
        }
コード例 #11
0
        bool PushLeftOrdering(Knowledge knowledge, Board board, Board childBoard)
        {
            var x = GetVectors(childBoard);
            var y = GetVectors(board);

            for (int i = 0; i < Math.Min(x.Count, y.Count); i++)
            {
                var c = CompareVectors(x[i], y[i]);

                if (c > 0)
                {
                    return(true);
                }
                if (c < 0)
                {
                    return(false);
                }
            }

            return(x.Count < y.Count);
        }
コード例 #12
0
        bool ContainsSuberabundantSubStacksAboveTemplate(Knowledge knowledge, Board board, Template template, out int color, out int x)
        {
            color = -1;
            x     = -1;
            var g         = knowledge.GraphKnowledge.Graph;
            var stackSets = board.Stacks.Select(s => s.ToSet()).ToList();
            var bigs      = Enumerable.Range(0, stackSets.Count).Count(i => stackSets[i].Count > template.Sizes[i]);

            if (bigs <= 0 || bigs > 1)
            {
                return(false);
            }

            var shrinkable = Enumerable.Range(0, stackSets.Count).Where(i => stackSets[i].Count == template.Sizes[i] + 1).ToList();

            if (shrinkable.Count <= 0)
            {
                return(false);
            }

            x = shrinkable[0];
            foreach (var c in stackSets[x].ToList())
            {
                stackSets[x].Remove(c);

                if (g.DegreeCondition(stackSets.Select(s => s.ToInt64()).ToList(), board.Pot))
                {
                    color = c;
                    return(true);
                }

                stackSets[x].Add(c);
            }

            return(false);
        }
コード例 #13
0
 public static bool ColorableWithoutEdge(Knowledge knowledge, Board board, int edgeIndex)
 {
     return(ColorableWithoutEdge(knowledge, board.Stacks, edgeIndex));
 }
コード例 #14
0
        public bool Analyze(Knowledge knowledge, Board board)
        {
            var potKnowledge   = knowledge[board.Template.Value][board.ColorCount];
            var graphKnowledge = knowledge.GraphKnowledge;

            var boardKnowledge = potKnowledge[board];

            if (boardKnowledge.Exists())
            {
                return(true);
            }

            var win      = false;
            var winDepth = MetaKnowledge.Infinity;

            foreach (var colorPair in potKnowledge.ColorPairs.OrderByDescending(pair => Chronicle.BranchGenerator.EnumerateExactlyOneIntersecters(board, pair).Count() % 2))
            {
                var colorPairWinDepth         = 0;
                var winningSwaps              = new List <Tuple <List <List <int> >, List <Move>, string> >();
                var improvingSwaps            = new List <Tuple <List <List <int> >, List <Move>, string> >();
                var winningSwapAlwaysExists   = true;
                var improvingSwapAlwaysExists = true;

                foreach (var branch in Chronicle.BranchGenerator.EnumerateBranches(board, colorPair))
                {
                    Tuple <List <List <int> >, List <Move>, string> winningSwap   = null;
                    Tuple <List <List <int> >, List <Move>, string> improvingSwap = null;

                    var branchWinDepth = MetaKnowledge.Infinity;

                    foreach (var swap in branch)
                    {
                        var childBoard = board.Clone();
                        childBoard.DoMoveCombination(swap);

                        if (DoSuperabundantCheck && !graphKnowledge.Graph.DegreeCondition(childBoard))
                        {
                            continue;
                        }

                        var childBoardKnowledge = potKnowledge[childBoard];
                        if (childBoardKnowledge.Exists() && childBoardKnowledge.Depth + 1 < branchWinDepth)
                        {
                            branchWinDepth = childBoardKnowledge.Depth + 1;
                            winningSwap    = new Tuple <List <List <int> >, List <Move>, string>(branch.SwapComponents, swap.ToList(), childBoardKnowledge.Reason);

                            if (!FindCleanestWin)
                            {
                                break;
                            }
                        }
                        else if (IsChildBoardBetter(knowledge, board, childBoard))
                        {
                            improvingSwap = new Tuple <List <List <int> >, List <Move>, string>(branch.SwapComponents, swap.ToList(), ChildReason + " better");
                        }
                    }

                    colorPairWinDepth = Math.Max(colorPairWinDepth, branchWinDepth);

                    if (winningSwap.Exists())
                    {
                        winningSwaps.Add(winningSwap);
                        improvingSwaps.Add(winningSwap);
                    }
                    else
                    {
                        winningSwapAlwaysExists = false;
                        if (improvingSwap.Exists())
                        {
                            improvingSwaps.Add(improvingSwap);
                        }
                        else
                        {
                            improvingSwapAlwaysExists = false;
                            break;
                        }
                    }
                }

                if (winningSwapAlwaysExists)
                {
                    if (colorPairWinDepth < winDepth)
                    {
                        winDepth = colorPairWinDepth;

                        win = true;
                        potKnowledge.AddWin(board, Reason, winDepth, colorPair, winningSwaps);

                        if (!FindCleanestWin)
                        {
                            break;
                        }
                    }
                }
                else if (improvingSwapAlwaysExists)
                {
                    potKnowledge.AddImprovement(board, ChildReason, colorPairWinDepth, colorPair, improvingSwaps);
                }
            }

            return(win);
        }
コード例 #15
0
 protected virtual bool IsChildBoardBetter(Knowledge knowledge, Board board, Board childBoard)
 {
     return(false);
 }
コード例 #16
0
ファイル: KiersteadAnalyzer.cs プロジェクト: landon/WebGraphs
 public bool Analyze(Knowledge knowledge, Board board)
 {
     return(IsKierstead(knowledge, board, true, Reason));
 }
コード例 #17
0
ファイル: KiersteadAnalyzer.cs プロジェクト: landon/WebGraphs
 public static bool IsKierstead(Knowledge knowledge, Board board, bool enforceDegreeRequirement = false, string reason = "")
 {
     return(EnumerateKiersteadEdgeIndices(knowledge, board, enforceDegreeRequirement, reason).Any());
 }