コード例 #1
0
        public void Lemma18Test(CspInstance instance)
        {
            List <CspInstance> instances = new() { instance };
            List <Pair>        TwoComponent;

            Lemma18TestInternal();


            foreach (CspInstance inst in instances)  // sprawdzamy czy nie został jakis 2 komponent
            {
                foreach (Variable var in inst.Variables)
                {
                    foreach (Color color in var.AvalibleColors)
                    {
                        if (color.Restrictions.Count == 2)
                        {
                            TwoComponent = new();
                            Color  currColor = color;
                            Color  lastColor = color;
                            Pair[] tempList  = new Pair[2];

                            TwoComponent.Add(currColor.Restrictions.First());
                            currColor = currColor.Restrictions.First().Color;

                            while (currColor.Restrictions.Count == 2)
                            {
                                tempList = currColor.Restrictions.ToArray();
                                if (tempList[0].Color != lastColor)
                                {
                                    TwoComponent.Add(tempList[0]);
                                    lastColor = currColor;
                                    currColor = tempList[0].Color;
                                }
                                else
                                {
                                    TwoComponent.Add(tempList[1]);
                                    lastColor = currColor;
                                    currColor = tempList[1].Color;
                                }
                                Assert.NotEqual(currColor, color);
                            }
                        }
                    }
                }
            }

            void Lemma18TestInternal()
            {
                bool flag = false;

                foreach (CspInstance inst in instances)
                {
                    var afterLemmaInst = CSPLemmas.Lemma18(inst, out bool applied);
                    if (applied)
                    {
                        instances.Remove(inst);
                        instances.AddRange(afterLemmaInst);
                        flag = true;
                        break;
                    }
                }
                if (flag)
                {
                    Lemma18TestInternal();
                }
            }
        }
コード例 #2
0
ファイル: CspColoring.cs プロジェクト: APTO-Team-1/3-Coloring
        int[] Rec(CspInstance instance)
        {
            foreach (var restriction in instance.Restrictions)
            {
                if (restriction.Pair1 == restriction.Pair2)
                {
                    instance.RemoveColor(restriction.Pair1);
                }
            }

            foreach (var v in instance.Variables)
            {
                if (v.AvalibleColors.Count == 0)
                {
                    return(null);
                }
            }

            bool applied;

            foreach (var v in instance.Variables)
            {
                CSPLemmas.Lemma2(instance, v, out applied);
                if (applied)
                {
                    return(Rec(instance));
                }
            }
            foreach (var v in instance.Variables)
            {
                CSPLemmas.Lemma3(instance, v, out applied);
                if (applied)
                {
                    return(Rec(instance));
                }
            }
            foreach (var v in instance.Variables)
            {
                CSPLemmas.Lemma4(instance, v, out applied);
                if (applied)
                {
                    return(Rec(instance));
                }
            }
            foreach (var v in instance.Variables)
            {
                CSPLemmas.Lemma5(instance, v, out applied);
                if (applied)
                {
                    return(Rec(instance));
                }
            }
            foreach (var v in instance.Variables)
            {
                CSPLemmas.Lemma6(instance, v, out applied);
                if (applied)
                {
                    return(Rec(instance));
                }
            }

            List <CspInstance> instances;

            //lemma 8
            foreach (var variable in instance.Variables)
            {
                foreach (var color in variable.AvalibleColors)
                {
                    instances = CSPLemmas.Lemma8(instance, variable, color, out applied);
                    if (applied)
                    {
                        foreach (var retInstance in instances)
                        {
                            var result = Rec(retInstance);
                            if (result != null)
                            {
                                return(result);
                            }
                        }
                        return(null);
                    }
                }
            }

            //lemma 9
            foreach (var variable in instance.Variables)
            {
                foreach (var color in variable.AvalibleColors)
                {
                    instances = CSPLemmas.Lemma9(instance, variable, color, out applied);
                    if (applied)
                    {
                        foreach (var retInstance in instances)
                        {
                            var result = Rec(retInstance);
                            if (result != null)
                            {
                                return(result);
                            }
                        }
                        return(null);
                    }
                }
            }

            //lemma 10
            foreach (var variable in instance.Variables)
            {
                foreach (var color in variable.AvalibleColors)
                {
                    var neighbors = color.Restrictions.Select(r => r.Variable).Distinct();
                    foreach (var neighbor in neighbors)
                    {
                        var restrictionToNeighbor = color.Restrictions.Where(r => r.Variable == neighbor);
                        if (restrictionToNeighbor.Count() >= 2)
                        {
                            instances = CSPLemmas.Lemma10(instance, variable, color, restrictionToNeighbor.First().Variable);
                            if (instances.Count > 1)
                            {
                                foreach (var retInstance in instances)
                                {
                                    var result = Rec(retInstance);
                                    if (result != null)
                                    {
                                        return(result);
                                    }
                                }
                                return(null);
                            }
                        }
                    }
                }
            }

            //lemma 11
            foreach (var variable in instance.Variables)
            {
                foreach (var color in variable.AvalibleColors)
                {
                    instances = CSPLemmas.Lemma11(instance, variable, color);
                    if (instances.Count > 1)
                    {
                        foreach (var retInstance in instances)
                        {
                            var result = Rec(retInstance);
                            if (result != null)
                            {
                                return(result);
                            }
                        }
                        return(null);
                    }
                }
            }


            //lemma 12
            foreach (var variable in instance.Variables)
            {
                foreach (var color in variable.AvalibleColors)
                {
                    instances = CSPLemmas.Lemma12(instance, variable, color);
                    if (instances.Count > 1)
                    {
                        foreach (var retInstance in instances)
                        {
                            var result = Rec(retInstance);
                            if (result != null)
                            {
                                return(result);
                            }
                        }
                        return(null);
                    }
                }
            }


            //lemma 13
            foreach (var variable in instance.Variables)
            {
                foreach (var color in variable.AvalibleColors)
                {
                    instances = CSPLemmas.Lemma13(instance, variable, color);
                    if (instances.Count > 1)
                    {
                        foreach (var retInstance in instances)
                        {
                            var result = Rec(retInstance);
                            if (result != null)
                            {
                                return(result);
                            }
                        }
                        return(null);
                    }
                }
            }

            //lemma 15
            instances = CSPLemmas.Lemma15(instance, out applied);
            if (applied)
            {
                foreach (var retInstance in instances)
                {
                    var result = Rec(retInstance);
                    if (result != null)
                    {
                        return(result);
                    }
                }
                return(null);
            }

            //lemma 17
            instances = CSPLemmas.Lemma17(instance, out applied);
            if (applied)
            {
                foreach (var retInstance in instances)
                {
                    var result = Rec(retInstance);
                    if (result != null)
                    {
                        return(result);
                    }
                }
                return(null);
            }

            //lemma 18
            instances = CSPLemmas.Lemma18(instance, out applied);
            if (applied)
            {
                foreach (var retInstance in instances)
                {
                    var result = Rec(retInstance);
                    if (result != null)
                    {
                        return(result);
                    }
                }
                return(null);
            }

            //lemma 19
            var isColoring = CSPLemmas.Lemma19(instance);

            if (isColoring)
            {
                return(instance.GetResult());
            }
            else
            {
                return(null);
            }
        }