Example #1
0
 private void _init()
 {
     Inputs         = new Dictionary <string, MemberFunctionSet>();
     InputLookupMap = new Map <int, string>();
     Rules          = new List <Rule>();
     Outputs        = new MemberFunctionSet();
     OutputName     = "";
 }
Example #2
0
 /// <summary>
 /// Add a set of membership functions that make up an input variable.
 /// </summary>
 /// <param name="sName">The name of the input variable. Cannot contain spaces.</param>
 /// <param name="mfs">The membership function set.</param>
 /// <returns></returns>
 public void addInputMFSet(String sName, MemberFunctionSet mfs)
 {
     if (Inputs.ContainsKey(sName))
     {
         throw new ArgumentException(string.Format("The name '{0}' is already in use.", sName));
     }
     else if (sName.Contains(" "))
     {
         throw new ArgumentException(string.Format("Invalid name '{0}'. Spaces are not allowed.", sName));
     }
     else
     {
         Inputs[sName] = mfs;
         InputLookupMap.Add(Inputs.Count - 1, sName);
     }
 }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        private void parseInputOutput(List <string> sLines, bool isInput)
        {
            string name     = "";
            double low      = 0;
            double high     = 0;
            int    numMfs   = 0;
            int    inputNum = 0; //starts at 1

            Dictionary <string, MemberFunction> mfs = new Dictionary <string, MemberFunction>();

            foreach (string line in sLines)
            {
                // First let's see if we can figure out what input index we have
                if (regexes[FISFileSection.INPUT].IsMatch(line))
                {
                    //[Input1] needs to be a number
                    string lineTrimmed = line.Trim();

                    if (!int.TryParse(lineTrimmed.Substring(6, lineTrimmed.Length - 7), out inputNum))
                    {
                        throw new Exception("Could not extract input/output index: " + line);
                    }
                }
                else
                {
                    string[] sLSplit = line.Split('=');
                    sLSplit[0] = sLSplit[0].ToLower();

                    // Example: Name='Depth'
                    if (sLSplit[0] == "name")
                    {
                        name = sLSplit[1].Replace("'", "");
                    }

                    // Example: Range=[0 4]
                    else if (sLSplit[0] == "range")
                    {
                        List <double> sRanges = RangeSquareBrackets(sLSplit[1]);

                        if (sRanges.Count != 2)
                        {
                            throw new Exception("Wrong number of ranges: " + line);
                        }

                        low  = sRanges[0];
                        high = sRanges[1];
                    }

                    // Example: NumMFs=4
                    else if (sLSplit[0] == "nummfs")
                    {
                        if (!int.TryParse(sLSplit[1], out numMfs))
                        {
                            throw new Exception("Could not parse number of member functions: " + line);
                        }
                    }

                    // Example: MF2='Moderate':'trapmf',[0.09 0.17 0.32 0.46]
                    else if (sLSplit[0].StartsWith("mf"))
                    {
                        int mfnum;
                        int.TryParse(sLSplit[0].Substring(2), out mfnum);
                        if (mfnum != mfs.Count + 1)
                        {
                            throw new Exception("Wrong number of memberfunctions: " + line);
                        }

                        string[] mflinesplit      = sLSplit[1].Split(',');
                        string[] mflinesplitsplit = mflinesplit[0].Split(':');
                        string   mfname           = mflinesplitsplit[0].Replace("'", "");
                        string   mftype           = mflinesplitsplit[1].Replace("'", "");

                        if (mfs.ContainsKey(mfname))
                        {
                            throw new Exception("Duplicate MF name detected: " + line);
                        }

                        List <double> vertices = RangeSquareBrackets(mflinesplit[1]);

                        switch (mftype)
                        {
                        case "trapmf":
                            if (vertices.Count != 4)
                            {
                                throw new Exception("Wrong number of vertices: " + line);
                            }
                            mfs[mfname] = new MemberFunction(vertices[0], vertices[1], vertices[2], vertices[3], 1);
                            break;

                        case "trimf":
                            if (vertices.Count != 3)
                            {
                                throw new Exception("Wrong number of vertices: " + line);
                            }
                            mfs[mfname] = new MemberFunction(vertices[0], vertices[1], vertices[2], 1);
                            break;

                        default:
                            throw new Exception("Unsupported MF type: " + line);
                        }
                    }
                }
            }

            // Now try to create a memberfunction set from all our parsed functions
            MemberFunctionSet mfSet = new MemberFunctionSet(low, high);

            foreach (KeyValuePair <string, MemberFunction> mfkvp in mfs)
            {
                mfSet.addMF(mfkvp.Key, mfkvp.Value);
            }

            if (isInput)
            {
                ruleset.setInputMFSet(inputNum - 1, name, mfSet);
            }
            else
            {
                ruleset.addOutputMFSet(name, mfSet);
            }
        }
Example #4
0
 /// <summary>
 /// Add a set of membership functions that make up the output variable.
 /// </summary>
 /// <param name="sName"></param>
 /// <param name="mfs"></param>
 public void addOutputMFSet(String sName, MemberFunctionSet mfs)
 {
     Outputs    = mfs;
     OutputName = sName;
 }