Beispiel #1
0
        public void Lemma13Test(CspInstance instance)
        {
            foreach (var v in instance.Variables)
            {
                foreach (var c in v.AvalibleColors)
                {
                    if (c.Restrictions.Count == 3)
                    {
                        foreach (var restrictionPair in c.Restrictions)
                        {
                            (var v2, var c2) = restrictionPair;
                            if (c2.Restrictions.Count == 2) // Lemma13 applies
                            {
                                if (c.Restrictions.Any(r => r.Variable.AvalibleColors.Count != 3))
                                {
                                    Assert.Throws <ArgumentException>(() => CSPLemmas.Lemma13(instance, v, c));
                                }

                                var res = CSPLemmas.Lemma13(instance, v, c);
                                Assert.Equal(3, res.Count);
                                Assert.Contains(res, inst => inst.Result.Any(p => p.Variable == v && p.Color == c));
                            }
                            else
                            {
                                var res = CSPLemmas.Lemma13(instance, v, c);
                                Assert.Single(res);
                            }
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public void Lemma11Test(CspInstance instance)
        {
            foreach (var v in instance.Variables)
            {
                foreach (var c in v.AvalibleColors)
                {
                    if ((c.Restrictions.Count >= 3 && v.AvalibleColors.Count == 4) ||
                        (c.Restrictions.Count >= 4 && v.AvalibleColors.Count == 3)) // Lemma11 applies
                    {
#if DEBUG
                        if (c.Restrictions.Select(r => r.Variable).Distinct().Count() != c.Restrictions.Select(r => r.Variable).Count())
                        {
                            Assert.Throws <ArgumentException>(() => CSPLemmas.Lemma11(instance, v, c));
                        }
                        else
                        {
                            var res = CSPLemmas.Lemma11(instance, v, c);
                            Assert.Equal(2, res.Count);
                            Assert.Contains(res, inst => inst.Result.Any(p => p.Variable == v && p.Color == c));
                        }
#endif
                    }
                    else
                    {
                        var res = CSPLemmas.Lemma11(instance, v, c);
                        Assert.Single(res);
                    }
                }
            }
        }
Beispiel #3
0
        public void RemoveVariableWith2ColorsTest(CspInstance instance, Variable variable)
        {
            int variableCount = instance.Variables.Count;

            List <Pair> c1Neighbors = new();

            c1Neighbors.AddRange(variable.AvalibleColors[0].Restrictions);

            List <Pair> c2Neighbors = new();

            c2Neighbors.AddRange(variable.AvalibleColors[1].Restrictions);

            CSPLemmas.RemoveVariableWith2Colors(instance, variable);

            Assert.False(instance.Variables.Contains(variable));
            Assert.Equal(variableCount - 1, instance.Variables.Count);
            foreach (var p1 in c1Neighbors)
            {
                foreach (var p2 in c2Neighbors)
                {
                    if (p1.Variable != p2.Variable)
                    {
                        Assert.Contains(instance.Restrictions, r => r.Contains(p1.Color) && r.Contains(p2.Color));
                    }
                }
            }
        }
Beispiel #4
0
        public void Lemma9Test(CspInstance instance, Variable v, Color c, Variable v2, Color c2)
        {
            var res = CSPLemmas.Lemma9(instance, v, c, out _);

            Assert.Equal(2, res.Count);
            Assert.True(
                res.Any(inst => inst.Result.Any(p => p.Variable == v && p.Color == c)) ||
                res.Any(inst => inst.Result.Any(p => p.Variable == v2 && p.Color == c2)));
        }
Beispiel #5
0
        public void Lemma17Test(CspInstance instance)
        {
            var res = CSPLemmas.Lemma17(instance, out bool applied);

            if (applied == false)
            {
                Assert.Null(CSPLemmas.FindBadThreeComponent(instance, false));
            }
            else
            {
                foreach (var inst in res)
                {
                    Lemma17Test(inst);
                }
            }
        }
Beispiel #6
0
        public void Lemma15Test(CspInstance instance)
        {
            var res = CSPLemmas.Lemma15(instance, out bool b);

            if (b == true)
            {
                foreach (var inst in res)
                {
                    if (inst != null)
                    {
                        Lemma15Test(inst);
                    }
                }
            }
            else
            {
                Assert.Null(CSPLemmas.FindBadThreeComponent(instance, true));
            }
        }
Beispiel #7
0
 public void Lemma8Test(CspInstance instance, Variable v, Color c, Variable v2, Color c2)
 {
     //if two 3 - color vertices changed to one 4 - color
     if (v.AvalibleColors.Count == 3 && v2.AvalibleColors.Count == 3)
     {
         var oldColors = v.AvalibleColors.Select(c => new Color(c.Value)).Union(v2.AvalibleColors.Select(c => new Color(c.Value))).ToList();
         var res       = CSPLemmas.Lemma8(instance, v, c, out _);
         Assert.Single(res);
         Assert.Null(res[0].Variables.FirstOrDefault(vbl => vbl == v));
         Assert.Null(res[0].Variables.FirstOrDefault(vbl => vbl == v2));
         var vCombined = res[0].Variables.FirstOrDefault(vbl =>
                                                         vbl.AvalibleColors.Count == 4);
         Assert.NotNull(vCombined);
     }
     //if returend two correct instances
     else
     {
         var res = CSPLemmas.Lemma8(instance, v, c, out _);
         Assert.Equal(2, res.Count);
         Assert.True(
             res.Any(inst => inst.Result.Any(p => p.Variable == v && p.Color == c)) ||
             res.Any(inst => inst.Result.Any(p => p.Variable == v2 && p.Color == c2)));
     }
 }
Beispiel #8
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();
                }
            }
        }
Beispiel #9
0
        public void Lemma10Test(CspInstance instance)
        {
            List <CspInstance> instances = new() { instance };

            Lemma10TestInternal();

            foreach (CspInstance inst in instances)
            {
                foreach (var var in inst.Variables)
                {
                    foreach (var col in var.AvalibleColors)
                    {
                        if (col.Restrictions.Count == 2)
                        {
                            var tempTab = col.Restrictions.ToArray();
                            Assert.NotEqual(tempTab[0].Variable, tempTab[1].Variable);
                        }
                    }
                }
            }


            void Lemma10TestInternal()
            {
                bool     foundFlag = false;
                int      indexOfInst = 0;
                Variable v = new(1), v2 = new(1);
                Color    c = new(1), c2_1 = new(1), c2_2 = new(1);

                foreach (CspInstance inst in instances)
                {
                    foreach (var var in inst.Variables)
                    {
                        foreach (var col in var.AvalibleColors)
                        {
                            foreach (var pair in col.Restrictions)
                            {
                                int resToNeigh = col.Restrictions.Where(r => r.Variable == pair.Variable).Count();
                                if (resToNeigh == pair.Variable.AvalibleColors.Count)
                                {
                                    inst.RemoveRestriction(new Pair(var, col), pair);
                                    resToNeigh--;
                                }
                                if (resToNeigh > 1)
                                {
                                    c           = col;
                                    v           = var;
                                    indexOfInst = instances.IndexOf(inst);
                                    v2          = pair.Variable;
                                    foundFlag   = true;
                                    break;
                                }
                            }
                            if (foundFlag)
                            {
                                break;
                            }
                        }
                        if (foundFlag)
                        {
                            break;
                        }
                    }
                    if (foundFlag)
                    {
                        break;
                    }
                }
                if (foundFlag)
                {
                    CspInstance foundInst = instances[indexOfInst];
                    instances.RemoveAt(indexOfInst);
                    var aaa = CSPLemmas.Lemma10(foundInst, v, c, v2);
                    instances.AddRange(aaa);
                    Lemma10TestInternal();
                }
            }
        }
Beispiel #10
0
        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);
            }
        }