Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        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]);
                    }
                }
            }
        }