Пример #1
0
        // public decimal Work(decimal rain, decimal visibility, decimal temperature, decimal hour)
        public Defuzzification Work(decimal rainValue, decimal temperatureValue, decimal fogValue /*, decimal darknessValue*/,
                                    DefuzzificationFactory.DefuzzificationMethod method)
        {
            FuzzySet zimno        = _temperatureParam.NegativeSet;
            FuzzySet cieplo       = _temperatureParam.PositiveSet;
            FuzzySet mokro        = _rainParam.PositiveSet;
            FuzzySet sucho        = _rainParam.NegativeSet;
            FuzzySet mgliscie     = _fogParam.NegativeSet;
            FuzzySet przejrzyscie = _fogParam.PositiveSet;
            // FuzzySet ciemno = _darknessParam.PositiveSet;
            //  FuzzySet jasno = _darknessParam.NegativeSet;

            //Definition of dimensions on which we will measure the input values
            //ContinuousDimension height = new ContinuousDimension("Height", "Personal height", "cm", 100, 250);
            //ContinuousDimension weight = new ContinuousDimension("Weight", "Personal weight", "kg", 30, 200);

            //Definition of dimension for output value
            ContinuousDimension consequent = new ContinuousDimension("", "0 = not good, 10 = very good", "grade", 0, 10);

            //Definition of basic fuzzy sets with which we will work
            //  input sets:
            //FuzzySet tall = new LeftQuadraticSet(height, "Tall person", 150, 180, 200);
            //FuzzySet weighty = new LeftLinearSet(weight, "Weighty person", 80, 100);
            //_tall = new LeftQuadraticSet(height, "Tall person", 150, 180, 200);
            //_weighty = new LeftLinearSet(weight, "Weighty person", 80, 100);

            //  output set:
            FuzzySet goodConditions = _resultParam.PositiveSet; //*/new LeftQuadraticSet(consequent, "Good conditions", 5, 7.5m, 10);
            FuzzySet badConditions  = _resultParam.NegativeSet; //*/new RightQuadraticSet(consequent, "Bad conditions", 5, 7.5m, 10);
            //FuzzySet badConditions = new RightLinearSet(consequent, "Good in basket ball", 0, 10);
            //FuzzySet mediumConditions = new TrapezoidalSet(consequent, "", 4,6,2,8);



            //Implication
            //FuzzyRelation term =/* ((tall & !weighty) & goodForBasket); |*/ (!(tall & !weighty) & !goodForBasket);
            //FuzzyRelation term = ((_rainParam.PositiveSet & !_temperatureParam.PositiveSet) & goodForBasket) | ((!_rainParam.PositiveSet & _temperatureParam.PositiveSet) & !goodForBasket);
            FuzzyRelation term =
                //((zimno) & badConditions) |
                ((sucho & cieplo) & goodConditions) |
                ((zimno & mokro) & badConditions) |
                ((sucho & przejrzyscie) & goodConditions) |
                ((cieplo & przejrzyscie) & goodConditions) |
                ((mgliscie & zimno) & badConditions) |
                ((mgliscie & mokro) & badConditions);
            //((mglisto & mokro) & badConditions) |
            //((mglisto & mokro) & badConditions) |
            //((mglisto & mokro) & badConditions) |
            //((mglisto & mokro) & badConditions);
            //((zimno & deszcz & mgla & noc) & badConditions) |
            //((zimno & deszcz & brakMgly & dzien) & badConditions) |
            //((zimno & deszcz & brakMgly & noc) & badConditions) |
            //((cieplo & brakDeszczu & mgla & dzien) & goodConditions) |
            //((cieplo & brakDeszczu & mgla & noc) & mediumConditions) |
            //((cieplo & brakDeszczu & brakMgly & dzien) & goodConditions) |
            //((cieplo & brakDeszczu & brakMgly & noc) & goodConditions) |
            //((cieplo & deszcz & mgla & dzien) & mediumConditions) |
            //((cieplo & deszcz & mgla & noc) & mediumConditions) |
            //((cieplo & deszcz & brakMgly & dzien) & goodConditions) |
            //((cieplo & deszcz & brakMgly & noc) & goodConditions);

            var result = DefuzzificationFactory.GetDefuzzification(term, new Dictionary <IDimension, decimal>
            {
                { _rainParam.Dimension, rainValue },
                { _temperatureParam.Dimension, temperatureValue },
                { _fogParam.Dimension, fogValue },
                // { _darknessParam.Dimension, darknessValue },
            }, method);

            //Defuzzification result = new MeanOfMaximum(
            //    term,
            //    new Dictionary<IDimension, decimal>{
            //        { _rainParam.Dimension, rainValue },
            //       { _temperatureParam.Dimension, temperatureValue },
            //        { _fogParam.Dimension, fogValue },
            //       // { _darknessParam.Dimension, darknessValue },
            //    }
            //);

            return(result);
        }
Пример #2
0
        protected void buildRelationNow(bool initial)
        {
            if (!_ready)
            {
                return;
            }

            _waitingForBuild = false;
            _building        = true;

            bool _expressionChanged = false;

            decimal inputProduct = ddlProduct.SelectedIndex + 1;
            decimal inputPrice   = txtPrice.Value;

            #region Realtime expression evaluation by means of C# parser
            string strExpression = txtExpression.Text;
            prependFullName(ref strExpression, "cheap");
            prependFullName(ref strExpression, "fruits");
            prependFullName(ref strExpression, "buyIt");

            object obj = Evaluator.Eval(strExpression);

            if (obj != null)
            {
                if (!(obj is FuzzyRelation))
                {
                    MessageBox.Show(String.Format("ERROR: Object of type FuzzyRelation expected as the result of the expression.\r\nThis object is type {0}.", obj.GetType().FullName),
                                    "Error evaluating expression", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
                else
                {
                    _relation = (FuzzyRelation)obj;
                    if (_expression != txtExpression.Text)
                    {
                        _expressionChanged = true;
                    }
                    _expression = txtExpression.Text;
                }
            }
            #endregion

            #region Defuzzification
            DefuzzificationFactory.DefuzzificationMethod method = (DefuzzificationFactory.DefuzzificationMethod)ddlDefuzMethod.SelectedIndex;

            _defuzzification = DefuzzificationFactory.GetDefuzzification(
                _relation,
                new Dictionary <IDimension, decimal> {
                { product, inputProduct },
                { price, inputPrice }
            },
                method
                );

            _defuzzMethod = method;
            #endregion

            #region Output value
            string unit = ((IContinuousDimension)_defuzzification.OutputDimension).Unit;
            lblOutput.Text = _defuzzification.CrispValue.ToString("F5") + (string.IsNullOrEmpty(unit) ? "" : " " + unit);
            #endregion

            Cursor.Current = Cursors.WaitCursor;

            #region storing TreeView selection
            //Store information about currenlty selected node. It will become handy
            //when selecting the same node after the refresh (if applicable)
            List <int> selectedNodePath = new List <int>();

            if (treeViewRelation.SelectedNode != null)
            {
                TreeNode pointer = treeViewRelation.SelectedNode;
                while (pointer != null)
                {
                    selectedNodePath.Add(pointer.Index);
                    pointer = pointer.Parent;
                }
            }
            else if (initial)
            {
                selectedNodePath.Add(0);
            }
            #endregion

            TreeSource ts = new TreeSource(_defuzzification);
            ts.DrawImageOnNodeSelect = false;
            ts.BuildTree(treeViewRelation, pictureBoxGraph, lblGraphCaption);
            //Cursor.Current = Cursors.Default;

            #region restoring TreeView selection
            if ((!_expressionChanged || initial) && selectedNodePath.Count() > 0 && selectedNodePath[selectedNodePath.Count() - 1] < treeViewRelation.Nodes.Count)
            {
                //We will now try to restore the selection
                TreeNode pointer = treeViewRelation.Nodes[selectedNodePath[selectedNodePath.Count() - 1]];

                for (int i = selectedNodePath.Count() - 2; i >= 0; i--)
                {
                    if (selectedNodePath[i] >= pointer.Nodes.Count)
                    {
                        pointer = null;
                        break;
                    }
                    pointer = pointer.Nodes[selectedNodePath[i]];
                }

                if (pointer != null)
                {
                    treeViewRelation.SelectedNode = pointer;
                    ts.DrawDetailImage(pointer);
                }
            }

            Cursor.Current           = Cursors.Default;
            ts.DrawImageOnNodeSelect = true;
            #endregion

            _building = false;
        }