public void TrapezoidalMembershipFunction_Trapezoid1245_ShouldBe1_4()
        {
            IMembershipFunction trapezoidalMembershipFunction = new TrapezoidalMembershipFunction(1, 2, 4, 5);
            double expected     = 1;
            double valueToCount = 4;
            double result       = trapezoidalMembershipFunction.GetMembershipFunctionValue(valueToCount);

            Assert.AreEqual(expected, result);
        }
        public void TrapezoidalMembershipFunction_Trapezoid0033_ShouldBe1Middle()
        {
            IMembershipFunction trapezoidalMembershipFunction = new TrapezoidalMembershipFunction(0, 0, 3, 3);
            double expected     = 1;
            double valueToCount = 2;
            double result       = trapezoidalMembershipFunction.GetMembershipFunctionValue(valueToCount);

            Assert.AreEqual(expected, result);
        }
        public void TrapezoidalMembershipFunction_Trapezoid0134_ShouldBe0_5Left()
        {
            IMembershipFunction trapezoidalMembershipFunction = new TrapezoidalMembershipFunction(0, 1, 3, 4);
            double expected     = 0.5;
            double valueToCount = 0.5;
            double result       = trapezoidalMembershipFunction.GetMembershipFunctionValue(valueToCount);

            Assert.AreEqual(expected, result);
        }
        public void CreateLinguisticVariableEntity_ReturnsCorrectLinguisticVariable()
        {
            // Arrange
            List <double> firstFunctionValues = new List <double> {
                0, 20, 20, 30
            };
            List <double> secondFunctionValues = new List <double> {
                50, 60, 60, 80
            };
            List <MembershipFunctionStrings> membershipFunctionStringsList = new List <MembershipFunctionStrings>
            {
                new MembershipFunctionStrings("Cold", "Trapezoidal", firstFunctionValues),
                new MembershipFunctionStrings("Hot", "Trapezoidal", secondFunctionValues)
            };
            string linguisticVariable            = "Water:Initial:[Cold:Trapezoidal:(0,20,20,30)|Hot:Trapezoidal:(50,60,60,80)]";
            var    linguisticVariableStrings     = new LinguisticVariableStrings("Water", "Initial", membershipFunctionStringsList);
            var    linguisticVariableStringsList = new List <LinguisticVariableStrings> {
                linguisticVariableStrings
            };

            _linguisticVariableParserMock.Expect(x => x.ParseLinguisticVariable(linguisticVariable)).Return(linguisticVariableStringsList);

            var firstMembershipFunction  = new TrapezoidalMembershipFunction("Cold", 0, 20, 20, 30);
            var secondMembershipFunction = new TrapezoidalMembershipFunction("Hot", 50, 60, 60, 80);

            _membershipFunctionCreatorMock.Expect(x => x.CreateMembershipFunctionEntity(MembershipFunctionType.Trapezoidal, "Cold", firstFunctionValues))
            .Return(firstMembershipFunction);
            _membershipFunctionCreatorMock.Expect(x => x.CreateMembershipFunctionEntity(MembershipFunctionType.Trapezoidal, "Hot", secondFunctionValues))
            .Return(secondMembershipFunction);

            var expectedLinguisticVariable = new LinguisticVariable(
                "Water",
                new MembershipFunctionList {
                firstMembershipFunction, secondMembershipFunction
            },
                true);

            // Act
            var actualLinguisticVariables = _linguisticVariableCreator.CreateLinguisticVariableEntities(linguisticVariable);

            // Assert
            Assert.AreEqual(1, actualLinguisticVariables.Count);
            Assert.IsTrue(ObjectComparer.LinguisticVariablesAreEqual(expectedLinguisticVariable, actualLinguisticVariables[0]));
        }
        public void CreateMembershipFunctionEntity_ReturnsCorrectMembershipFunction()
        {
            // Arrange
            MembershipFunctionType membershipFunctionType = MembershipFunctionType.Trapezoidal;
            string        membershipFunctionName          = "High";
            List <double> points = new List <double> {
                1, 2, 3, 5
            };

            TrapezoidalMembershipFunction expectedMembershipFunction = new TrapezoidalMembershipFunction(membershipFunctionName,
                                                                                                         points[0], points[1], points[2], points[3]);

            // Act
            TrapezoidalMembershipFunction actualMembershipFunction = (TrapezoidalMembershipFunction)
                                                                     _membershipFunctionCreator.CreateMembershipFunctionEntity(membershipFunctionType, membershipFunctionName, points);

            // Assert
            Assert.IsNotNull(actualMembershipFunction);
            Assert.IsTrue(ObjectComparer.MembershipFunctionsAreEqual(expectedMembershipFunction, actualMembershipFunction));
        }
Beispiel #6
0
        public static (List <Quantifier>, List <Qualifier>, List <Summarizer>, LogicalOperation) ParseFuzzySetFile(
            int dataCount,
            string path = DefaultPath)
        {
            path = $"{FuzzySetsFolderPath}/{path}";

            var quantifiers = new List <Quantifier>();
            var qualifiers  = new List <Qualifier>();
            var summarizers = new List <Summarizer>();

            var currentParsingMode          = ParsingMode.Quantifier;
            var currentSummarizersOperation = LogicalOperation.And;

            using (StreamReader sr = File.OpenText(path))
            {
                while (sr.Peek() >= 0)
                {
                    string line = sr.ReadLine().Trim();

                    if (line.StartsWith("#") || String.IsNullOrWhiteSpace(line))
                    {
                        continue;
                    }

                    if (line.StartsWith("QUALIFIERS"))
                    {
                        currentParsingMode = ParsingMode.Qualifier;
                        continue;
                    }

                    if (line.StartsWith("SUMMARIZERS"))
                    {
                        currentParsingMode = ParsingMode.Summarizer;
                        if (line.ToLowerInvariant().Contains("or"))
                        {
                            currentSummarizersOperation = LogicalOperation.Or;
                        }
                        continue;
                    }

                    if (line.StartsWith("QUANTIFIERS"))
                    {
                        currentParsingMode = ParsingMode.Quantifier;
                        continue;
                    }


//                    var parameters = line.Split(':');
                    var input = line.Split(new[] { ':' }, StringSplitOptions.None);

                    string label = input[0];
                    string membershipExpression = input[2];
                    var    memInfo   = membershipExpression.Split(new[] { '(', ')' });
                    string memType   = memInfo[0];
                    var    memParams = memInfo[1].Split(',');
                    IMembershipFunction membership;
                    if (memType.ToLowerInvariant().StartsWith("trap"))
                    {
                        membership = new TrapezoidalMembershipFunction(memParams[0], memParams[1], memParams[2], memParams[3]);
                    }
                    else
                    {
                        membership = new TriangularMembershipFunction(memParams[0], memParams[1], memParams[2]);
                    }

                    int xmin = 0;
                    int xmax = 100;
                    if (input.Length >= 5)
                    {
                        xmin = Int32.Parse(input[3]);
                        xmax = Int32.Parse(input[4]);
                    }

                    switch (currentParsingMode)
                    {
                    case ParsingMode.Quantifier:
                        bool isAbsolute = input[1].ToLowerInvariant().StartsWith("a");
                        quantifiers.Add(new Quantifier(
                                            label,
                                            membership,
                                            isAbsolute ? QuantifierType.Absolute : QuantifierType.Relative,
                                            0,
                                            isAbsolute ? dataCount : 1));
                        break;

                    case ParsingMode.Qualifier:
                        qualifiers.Add(new Qualifier(
                                           label,
                                           input[1],
                                           membership,
                                           xmin, xmax));
                        break;

                    case ParsingMode.Summarizer:
                        summarizers.Add(new Summarizer(
                                            label,
                                            input[1],
                                            membership,
                                            xmin, xmax));
                        break;
                    }
                }
            }

            return(quantifiers, qualifiers, summarizers, currentSummarizersOperation);
        }
 public void SetUp()
 {
     _trapezoidalMembershipFunction = new TrapezoidalMembershipFunction(LinguisticVariableName, X0, X1, X2, X3);
 }
Beispiel #8
0
        public static SentenceTuple getSentenceElementsFromFile()
        {
            SentenceTuple       sentenceTuple = new SentenceTuple();
            List <Summarizator> summarizators = new List <Summarizator>();
            List <Qualifier>    qualifiers    = new List <Qualifier>();
            List <Quantifier>   quantifiers   = new List <Quantifier>();
            string workingDirectory           = Environment.CurrentDirectory;
            string filepath = Directory.GetParent(workingDirectory).Parent.Parent.FullName + "\\SummarizatorOrQualifier.csv";

            using (TextFieldParser csvParser = new TextFieldParser(filepath))
            {
                csvParser.CommentTokens = new[] { "#" };
                csvParser.SetDelimiters(";");
                csvParser.HasFieldsEnclosedInQuotes = false;
                csvParser.ReadLine();
                var parsingMode            = ElementType.Summarizator;
                var currentLogicalOperator = OperationType.And;
                Quantifier.QuantifierType quantifierType = Quantifier.QuantifierType.Relative;

                while (!csvParser.EndOfData)
                {
                    IMembershipFunction membershipFunction;
                    string[]            singleRow = csvParser.ReadFields();
                    if ((singleRow ?? throw new InvalidOperationException()).Contains(""))
                    {
                        continue;
                    }
                    if (singleRow[0].Contains("summarizators"))
                    {
                        parsingMode            = ElementType.Summarizator;
                        currentLogicalOperator = (singleRow[1] == OperationType.And.ToString().ToLowerInvariant()) ? OperationType.And : OperationType.Or;
                        continue;
                    }
                    else if (singleRow[0] == "qualifiers")
                    {
                        parsingMode = ElementType.Qualifier;
                        continue;
                    }
                    else if (singleRow[0] == "quantifiers")
                    {
                        parsingMode    = ElementType.Quantifier;
                        quantifierType = singleRow[1] == Quantifier.QuantifierType.Relative.ToString().ToLowerInvariant() ? Quantifier.QuantifierType.Relative : Quantifier.QuantifierType.Absolute;
                        continue;
                    }
                    string   description        = singleRow[0];
                    string   attributeName      = singleRow[1];
                    string   functionType       = singleRow[2];
                    string[] functionParametres = singleRow[3].Split(',');
                    int      minValue           = int.Parse(singleRow[4]);
                    int      maxValue           = int.Parse(singleRow[5]);
                    if (functionType == "trian")
                    {
                        membershipFunction = new TriangleMembershipFunction(double.Parse(functionParametres[0], CultureInfo.InvariantCulture), double.Parse(functionParametres[1], CultureInfo.InvariantCulture), double.Parse(functionParametres[2], CultureInfo.InvariantCulture));
                    }
                    else
                    {
                        membershipFunction = new TrapezoidalMembershipFunction(double.Parse(functionParametres[0], CultureInfo.InvariantCulture), double.Parse(functionParametres[1], CultureInfo.InvariantCulture), double.Parse(functionParametres[2], CultureInfo.InvariantCulture), double.Parse(functionParametres[3], CultureInfo.InvariantCulture));
                    }
                    switch (parsingMode)
                    {
                    case ElementType.Summarizator:
                        summarizators.Add(new Summarizator(
                                              description,
                                              attributeName,
                                              membershipFunction,
                                              minValue,
                                              maxValue));
                        break;

                    case ElementType.Qualifier:
                        qualifiers.Add(new Qualifier(
                                           description,
                                           attributeName,
                                           membershipFunction,
                                           minValue,
                                           maxValue));
                        break;

                    case ElementType.Quantifier:
                        quantifiers.Add(new Quantifier(
                                            description,
                                            membershipFunction,
                                            quantifierType,
                                            minValue,
                                            maxValue));
                        break;
                    }
                }
                return(new SentenceTuple(qualifiers, quantifiers, summarizators, currentLogicalOperator));
            }
        }