コード例 #1
0
ファイル: CAFuzzyARTMAP.cs プロジェクト: DrUzair/CompoNet
        public void saveNetworkOnFile(string path)
        {
            // Delete the file if it exists.
            if (File.Exists(path))
            {
                File.Delete(path);
            }
            FileStream fs        = File.Create(path);
            string     netConfig = "";

            netConfig += "Rho " + this.rho + "\r\n";
            netConfig += "RhoInc " + this.rhoIncrement + "\r\n";
            netConfig += "Alpha " + this.alpha + "\r\n";
            netConfig += "Beta " + this.beta + "\r\n";
            netConfig += "CC " + this.complementCoding + "\r\n";
            netConfig += "F1 " + this.ARTModule.F1.Count + "\r\n";
            netConfig += "MFCategoryCount " + this.mapField.getCategoryCount() + "\r\n";
            for (int catIndex = 0; catIndex < this.mapField.getCategoryCount(); catIndex++)
            {
                MapFieldCategory cat = (MapFieldCategory)this.mapField.categories[catIndex];
                netConfig += cat.getCode() + "\r\n";
                netConfig += cat.getName() + "\r\n";
            }
            netConfig += "ContextCount " + this.ARTModule.contextField.Count + "\r\n";
            ICollection contextKeys     = this.ARTModule.contextField.Keys;
            IEnumerator contextKeysEnum = contextKeys.GetEnumerator();

            while (contextKeysEnum.MoveNext())
            {
                int contextKey = Int32.Parse(contextKeysEnum.Current.ToString());
                netConfig += "Context " + contextKey + "\r\n";
                LayerF2 F2 = ((LayerF2)this.ARTModule.contextField[contextKey]);
                netConfig += "F2Count " + F2.Count + "\r\n";
                for (int f2Index = 0; f2Index < F2.Count; f2Index++)
                {
                    netConfig += "Prototype\r\n";
                    double [] prototype = ((F2Neuron)F2[f2Index]).getProtoTypeCluster();
                    for (int i = 0; i < prototype.Length; i++)
                    {
                        netConfig += prototype[i] + "\t";
                    }
                    netConfig += "\r\n";
                    netConfig += "tdConnWs\r\n";
                    SynapticConnection[] conns = ((F2Neuron)F2[f2Index]).getConnections();
                    for (int connIndex = 0; connIndex < conns.Length; connIndex++)
                    {
                        netConfig += ((SynapticConnection)conns[connIndex]).getWeight() + "\t";
                    }
                    netConfig += "\r\n";
                    netConfig += "CatCode\r\n";
                    //for (int f2Index = 0; f2Index < F2.Count; f2Index++)
                    {
                        MapFieldConnection mapFieldConn = ((F2Neuron)F2[f2Index]).getMapFieldConnection();
                        netConfig += mapFieldConn.getCategory().getCode() + "\r\n";
                    }
                }
            }
            AddText(fs, netConfig);
            fs.Close();
        }
コード例 #2
0
        public object[,] getCluster(int contextCode, double[] I)
        {
            LayerF2 F2 = (LayerF2)contextField[contextCode];

            object[,] cluster = new object[1, 2];
            double[] T = new double[F2.Count];
            double[] i = new double[I.Length * 2];
            if (complementCoding)
            {
                i = new double[I.Length * 2];
                i = complement(I);
            }
            else
            {
                i = new double[I.Length];
                i = I;
            }
            T = F1.processInput(i, alpha, F2);
            int repeatCount = 1;

Repeater:   // Control shall come up here in case there are more than one members in T,
            object[,] j_AND_Z_td_J = F2.processInput(T);
            int j = (int)j_AND_Z_td_J[0, 0];

            double[] Z_td_J = (double[])j_AND_Z_td_J[0, 1];
            //Adaptation Phase
            if (vigilancePass(i, Z_td_J))
            {
                cluster[0, 0] = Z_td_J;
                cluster[0, 1] = j;
            }
            else
            {
                if (repeatCount <= T.Length)
                {
                    T[j] = -1;
                    repeatCount++;
                    goto Repeater;
                }
                // MinDist F2Neuron does not resonate with this i, make a new category for it
                else
                {
                    double[] d = { -1.0 };
                    cluster[0, 0] = d;
                    cluster[0, 1] = -1;
                }
            }
            return(cluster);
        }
コード例 #3
0
ファイル: CAFuzzyARTMAP.cs プロジェクト: DrUzair/CompoNet
        public MapFieldCategory recallCat(int contextCode, double[] Pattern) //THIS METHOD ASSUMES THAT EACH F2 NODE IS CONNECTED WITH ONLY ONE MAPFIELD NODE
        {
            ARTModule.reSetRho(rho);
            LayerF2 contextNeurons = (LayerF2)ARTModule.contextField[contextCode];

            object[,] artCluster = ARTModule.getCluster(contextCode, Pattern);
            if ((int)artCluster[0, 1] == -1)
            {
                return(null);
            }
            else
            {
                F2Neuron           f2Neuron = (F2Neuron)contextNeurons[(int)artCluster[0, 1]];
                MapFieldConnection mfConn   = f2Neuron.getMapFieldConnection();
                return(mfConn.getCategory());
            }
        }
コード例 #4
0
ファイル: CAFuzzyARTMAP.cs プロジェクト: DrUzair/CompoNet
        //public int recall(int contextCode, double[] Pattern) // THIS METHOD ASSUMES THAT EACH F2 NODE HAS CONNECTIONS WITH ALL MAPFIELD NODES
        //{
        //    ARTModule.reSetRho(rho);
        //    int code = -1; // Don't Know
        //    LayerF2 contextNeurons = (LayerF2)ARTModule.contextField[contextCode];
        //    object [,]artCluster = ARTModule.getCluster(contextCode, Pattern);
        //    if ((int)artCluster[0, 1] == -1)
        //        code = -1;
        //    else
        //    {
        //        F2Neuron f2Neuron = (F2Neuron)contextNeurons[(int)artCluster[0, 1]];
        //        f2Neuron.getMapFieldConnections();
        //        ArrayList mapFieldConnections = f2Neuron.getMapFieldConnections();
        //        IEnumerator mapFieldConnEnum = mapFieldConnections.GetEnumerator();
        //        while (mapFieldConnEnum.MoveNext())
        //        {
        //            MapFieldConnection mapFieldConn = (MapFieldConnection)mapFieldConnEnum.Current;
        //            if (mapFieldConn.getWeight() == 1)
        //            {
        //                MapFieldCategory category = mapFieldConn.getCategory();
        //                code = category.getCode();
        //                //break;
        //            }
        //        }
        //    }
        //       // code = mapField.getAssociatedCategory(contextNeurons, (int)artCluster[0, 1]);
        //    return code;
        //}
        public int recall(int contextCode, double[] Pattern) //THIS METHOD ASSUMES THAT EACH F2 NODE IS CONNECTED WITH ONLY ONE MAPFIELD NODE
        {
            ARTModule.reSetRho(rho);
            int     code           = -1; // Don't Know
            LayerF2 contextNeurons = (LayerF2)ARTModule.contextField[contextCode];

            object[,] artCluster = ARTModule.getCluster(contextCode, Pattern);
            if ((int)artCluster[0, 1] == -1)
            {
                code = -1;
            }
            else
            {
                F2Neuron           f2Neuron = (F2Neuron)contextNeurons[(int)artCluster[0, 1]];
                MapFieldConnection mfConn   = f2Neuron.getMapFieldConnection();
                MapFieldCategory   category = mfConn.getCategory();
                code = category.getCode();
            }
            // code = mapField.getAssociatedCategory(contextNeurons, (int)artCluster[0, 1]);
            return(code);
        }
コード例 #5
0
ファイル: LayerF1.cs プロジェクト: DrUzair/CompoNet
        public double[] processInput(double[] i, double alpha, LayerF2 F2Neurons) // Orienting Subsystem
        {
            int f2index = 0;

            double[] f1Output = new double[F2Neurons.Count];
            while (f2index < F2Neurons.Count)
            {
                F2Neuron f2Neuron     = (F2Neuron)F2Neurons[f2index];
                int      f1index      = 0;
                double[] z            = new double[i.Length];
                double   I_FuzzyAND_Z = 0.0;
                while (f1index < base.Count)
                {
                    z[f1index]    = f2Neuron.getWeight(f1index);
                    I_FuzzyAND_Z += Math.Min(z[f1index], i[f1index]);
                    f1index++;
                }
                f1Output[f2index++] = Math.Round((I_FuzzyAND_Z / (alpha + norm(z))), 4);
            }
            return(f1Output);
        }
コード例 #6
0
        public object[,] feedInput(int contextCode, double[] I, int catCode)
        {
            object[,] output = new object[1, 2];
            double[] z_td_J_new = new double[i.Length];
            if (complementCoding)
            {
                i = complement(I);
            }
            else
            {
                i = I;
            }
            if (!contextField.ContainsKey(contextCode))
            {
                //Add Context Code
                contextField.Add(contextCode, new LayerF2());
            }
            //Load Context
            LayerF2 F2Neurons = (LayerF2)contextField[contextCode];

            if (F2Neurons.Count == 0)
            {
                output[0, 1] = F2Neurons.AddF2Neuron(F1, i);
                output[0, 0] = ((F2Neuron)F2Neurons[0]).getProtoTypeCluster();
            }
            else
            {
                double[] T = new double[F2Neurons.Count];
                // Recognition Phase (Orientation Subsystem)
                T = F1.processInput(i, alpha, F2Neurons);
                int repeatCount = 1;
Repeater:       // Control shall come up here in case there are more than one members in T,
                object[,] j_AND_Z_td_J = F2Neurons.processInput(T);
                int      J      = (int)j_AND_Z_td_J[0, 0];
                double[] Z_td_J = (double[])j_AND_Z_td_J[0, 1];
                //Adaptation Phase
                if (vigilancePass(i, Z_td_J))
                {
                    if (((F2Neuron)F2Neurons[J]).getMapFieldConnection().getCategory().getCode() == catCode)
                    {
                        z_td_J_new   = F2Neurons.updateWeights(Z_td_J, i, J, beta);
                        output[0, 0] = z_td_J_new;
                        output[0, 1] = J;
                    }
                    else   // invoke match tracking
                    {
                        double normIandZ = 0.0;
                        double normI     = 0.0;
                        for (int index = 0; index < i.Length; index++)
                        {
                            normI     += Math.Abs(i[index]);
                            normIandZ += Math.Abs((Math.Min(i[index], Z_td_J[index])));
                        }
                        double ro = Math.Round((normIandZ / normI), 4) + this.epsilon;
                        reSetRho(ro);
                        T[J] = -1;
                        repeatCount++;
                        goto Repeater;
                    }
                }
                else
                {
                    if (repeatCount < T.Length)
                    {
                        T[J] = -1;
                        repeatCount++;
                        goto Repeater;
                    }
                    // MinDist F2Neuron does not resonate with this i, make a new category for it
                    else
                    {
                        if (CategoryCountLimit != -1 & F2Neurons.Count == CategoryCountLimit)
                        {
                            output[0, 1] = "-1";
                            output[0, 0] = i;
                        }
                        else
                        {
                            output[0, 1] = F2Neurons.AddF2Neuron(F1, i);
                            output[0, 0] = ((F2Neuron)F2Neurons[F2Neurons.Count - 1]).getProtoTypeCluster();
                        }
                    }
                }
            }

            return(output);
        }
コード例 #7
0
ファイル: CAFuzzyARTMAP.cs プロジェクト: DrUzair/CompoNet
        public ConSelfARTMAP(string path)
        {
            string data = "";

            if (netConfigFound(path))
            {
                try
                {
                    System.IO.StreamReader reader = new System.IO.StreamReader(path);
                    data = reader.ReadToEnd();
                    reader.Close();
                }
                catch (Exception ioerror)
                {
                    Console.Out.WriteLine(ioerror.ToString());
                    return;
                } // cath ends
            }
            //if (data.Contains("Rho")) POCKET PC .NET DO NOT SUPPORT String.Contains() Method
            {
                float rho = float.Parse(data.Substring(4, data.IndexOf("\r") - (4)));
                this.rho = rho;
                data     = data.Substring(data.IndexOf("\r") + 2);
            }
            {
                float rhoIncrement = float.Parse(data.Substring(7, data.IndexOf("\r") - 7));
                this.rhoIncrement = rhoIncrement;
                data = data.Substring(data.IndexOf("\r") + 2);
            }
            //if (data.Contains("Alpha"))
            {
                float alpha = float.Parse(data.Substring(5, data.IndexOf("\r") - 5));
                this.alpha = alpha;
                data       = data.Substring(data.IndexOf("\r") + 2);
            }
            //if (data.Contains("Beta"))
            {
                float beta = float.Parse(data.Substring(4, data.IndexOf("\r") - 4));
                this.beta = beta;
                data      = data.Substring(data.IndexOf("\r") + 2);
            }
            //if (data.Contains("CC"))

            bool CC = bool.Parse(data.Substring(2, data.IndexOf("\r") - 2));

            this.complementCoding = CC;
            data = data.Substring(data.IndexOf("\r") + 2);

            //if (data.Contains("F1"))

            {
                int F1Count = int.Parse(data.Substring(2, data.IndexOf("\r") - 2));
                if (CC)
                {
                    this.ARTModule = new ConSelFAM.NET.ConSelfART(F1Count / 2, -1, this.rho, this.alpha, this.beta, this.complementCoding);
                }
                else
                {
                    this.ARTModule = new ConSelFAM.NET.ConSelfART(F1Count, -1, this.rho, this.alpha, this.beta, this.complementCoding);
                }
                data = data.Substring(data.IndexOf("\r") + 2);
            }
            {
                int MFCategoryCount = Int32.Parse(data.Substring(16, data.IndexOf("\r") - 16)); //16 characters in 'MFCategoryCount '
                data          = data.Substring(data.IndexOf("\r") + 2);
                this.mapField = new MAPField();
                for (int i = 0; i < MFCategoryCount; i++)
                {
                    int catCode = Int32.Parse(data.Substring(0, data.IndexOf("\r")));
                    data = data.Substring(data.IndexOf("\r") + 2);
                    string catName = data.Substring(0, data.IndexOf("\r"));
                    data = data.Substring(data.IndexOf("\r") + 2);
                    this.mapField.addNewCategory(catName, catCode);
                }
                int contextCount = Int32.Parse(data.Substring(13, data.IndexOf("\r") - 13)); //13 characters in 'ContextCount '
                data = data.Substring(data.IndexOf("\r") + 2);
                for (int contextIndex = 0; contextIndex < contextCount; contextIndex++)
                {
                    int     contextCode = Int32.Parse(data.Substring(8 /*7 characters in 'Context '*/, data.IndexOf("\r") - 8));
                    LayerF2 F2Layer     = new LayerF2();
                    this.ARTModule.contextField.Add(contextCode, F2Layer);
                    data = data.Substring(data.IndexOf("\r") + 2);
                    int F2Count = 0;
                    //if (data.Contains("F2"))
                    F2Count = int.Parse(data.Substring(8 /*8 characters in 'F2Count '*/, data.IndexOf("\r") - 8));
                    data    = data.Substring(data.IndexOf("\r") + 2);
                    for (int i = 0; i < F2Count; i++)
                    {
                        // Parse and set F2 Neuron Prototype
                        double[] prototype = new double[this.ARTModule.F1.Count];
                        data = data.Substring(data.IndexOf("\r") + 2);
                        string pt      = data.Substring(0, data.IndexOf("\r"));
                        int    F1count = this.ARTModule.F1.Count;
                        for (int j = 0; j < F1count; j++)
                        {
                            prototype[j] = double.Parse(pt.Substring(0, pt.IndexOf("\t")).Trim());
                            if (j < (this.ARTModule.F1.Count - 1))
                            {
                                pt = pt.Substring(pt.IndexOf("\t") + 1);
                            }
                        }
                        data = data.Substring(data.IndexOf("\r") + 2);
                        data = data.Substring(data.IndexOf("\r") + 2);//discard tdConnWs word from data string
                        // Parse and set tdConnection Weights
                        double[] weights = new double[this.ARTModule.F1.Count];
                        string   ws      = data.Substring(0, data.IndexOf("\r"));
                        for (int j = 0; j < this.ARTModule.F1.Count; j++)
                        {
                            weights[j] = double.Parse(ws.Substring(0, ws.IndexOf("\t")).Trim());
                            if (j < (this.ARTModule.F1.Count - 1))
                            {
                                ws = ws.Substring(ws.IndexOf("\t") + 1);
                            }
                        }
                        data = data.Substring(data.IndexOf("\r") + 2);
                        int f2NeuronIndex = F2Layer.AddF2Neuron((LayerF1)this.ARTModule.F1, weights, prototype);
                        // Parse and set map field connection weights
                        data = data.Substring(data.IndexOf("\r") + 2);
                        string catcode = data.Substring(0, data.IndexOf("\r")).Trim();
                        int    code    = int.Parse(catcode);
                        mapField.getCategory(code).addConnection((F2Neuron)F2Layer[f2NeuronIndex]);
                    }
                }
            }
        }