Beispiel #1
0
 public IGMN(Vector maxMin)
 {
     this.initialGauss = maxMin;
     this.initialGauss.Multiply(INITIAL_VARIANCE);
     cortical = new List<IGMNData>();
     xtree = new XTree<IGMNData>();
 }
Beispiel #2
0
        public DenseMatrix Simulate(DenseMatrix inputs) //rows: t, cols: inputcount
        {
            states = new Vector(states.Elements.Length);

            DenseMatrix temp = inputs.MatrixMultiply(inputWeights);

            DenseMatrix ret = new DenseMatrix(inputs.Rows, states.Elements.Length);

            for (int t = 0; t < temp.Rows; ++t)
            {
                Vector v = temp.GetRow(t);                
                Vector states2 = innerConnections.MatrixMultiplyRight(states);
                states2.Add(v);
                states2.Add(biasWeights);
                for (int i = 0; i < states2.Elements.Length; ++i)
                {
                    states2.Elements[i] = (double)Math.Tanh(states2.Elements[i]);
                }

                states = states2;
                for (int i = 0; i < states.Elements.Length; ++i)
                {
                    ret[t, i] = states.Elements[i];
                }
            }

            return ret;
        }
Beispiel #3
0
 public IGMNData(IGMN owner, Vector mean)
 {
     this.owner = owner;
     this.gauss = new Gaussian(mean, getStarterCovariance());
     this.inputGauss = new Gaussian(mean.Part(0, mean.Elements.Length - 1), getInputStarterCovariance());
     Age = 1;
     Accumlator = 1;
 }
 public void AddDiad(Vector v, double factor)
 {                    
     //hozzaad (v) 
     //det(X + cr) = det(X)(1 + rX^(−1)c)
     //(A+cr)^(-1)=A^(-1) - (A^(-1)*c*r*A^(-1))/(1 + rX^(−1)c)            
     double sci = Distance(v);            
     covariance.Add(DenseMatrix.CreateDiad(v, v), factor);
     Vector inv = inverseCovariance.VectorMultiplyRight(v);
     inverseCovariance.Add(DenseMatrix.CreateDiad(inv, inv), -factor / (1 + factor * sci));
     determinant *= (1 + factor * sci);            
 }
 public CovarianceMatrix(Vector diagonal)
 {
     determinant = 1;
     covariance = DenseMatrix.Diag(diagonal);
     inverseCovariance = DenseMatrix.Diag(diagonal);
     for (int i = 0; i < diagonal.Elements.Length; ++i)
     {
         double temp = covariance[i, i];
         inverseCovariance[i, i] = 1 / temp;
         determinant *= temp;
     }                        
 }
Beispiel #6
0
        public void Div(Vector v)
        {
            if (Elements.Length != v.Elements.Length)
            {
                return;
            }

            for (int i = 0; i < Elements.Length; ++i)
            {
                Elements[i] /= v.Elements[i];
            }
        }
Beispiel #7
0
        public double Likelihood(Vector x, out double e)
        {
            Vector vv = x - Mean;
          
            int dim = vv.Elements.Length;

            double c = 1 / Math.Pow(2 * Math.PI, dim / 2.0);
            double d = 1 / Math.Sqrt(Covariance.Determinant);
            e = Math.Exp(-0.5 * Covariance.Distance(vv));
            
            return c * d * e;
        }        
Beispiel #8
0
        public void Add(Vector v, double factor)
        {
            if (Elements.Length != v.Elements.Length)
            {
                return;
            }

            for (int i = 0; i < Elements.Length; ++i)
            {
                Elements[i] += factor * v.Elements[i];
            }
        }
        public double Distance(Vector v)
        {
            //eps^T * inv(C) * eps
            double sum = 0;

            for (int c = 0; c < inverseCovariance.Cols; ++c)
            {
                double sum2 = 0;
                for (int r = 0; r < inverseCovariance.Rows; ++r)
                {
                    sum2 += inverseCovariance[r, c] * v.Elements[r];
                }
                sum += sum2 * v.Elements[c];
            }
            return sum;
        }
Beispiel #10
0
        public void RefineWithData(Vector x, double w)
        {
            Vector eps = x - gauss.Mean;
            Vector eps2 = x.Part(0, x.Elements.Length - 1) - inputGauss.Mean;

            /**/double oldQvalue = gauss.Mean.Elements[gauss.Mean.Elements.Length - 1];            
            gauss.Mean.Add(eps, w);

            /**/gauss.Mean.Elements[gauss.Mean.Elements.Length - 1] = Math.Max(oldQvalue, x.Elements[x.Elements.Length - 1]);//max Q value
            
            inputGauss.Mean.Add(eps2, w);

            gauss.Covariance.MultiplyScalar(1 - w);
            inputGauss.Covariance.MultiplyScalar(1 - w);

            gauss.Covariance.AddDiad(eps, -w * w + w);//-w * w + w
            inputGauss.Covariance.AddDiad(eps2, -w * w + w);
        }
Beispiel #11
0
        public ESN(int reservoirSize, int inputSize, int outputSize) 
        {
            states = new Vector(reservoirSize);
            innerConnections = GenerateInnerWeights(reservoirSize);
            inputWeights = new DenseMatrix(inputSize,reservoirSize);
            biasWeights = new Vector(reservoirSize);
            Random r = new Random();
            for (int row = 0; row < inputSize; ++row)
            {
                for (int col = 0; col < reservoirSize; ++col)
                {
                    inputWeights[row, col] = (double)r.NextDouble();
                }                
            }

            for (int i = 0; i < reservoirSize; ++i)
            {
                biasWeights.Elements[i] = (double)r.NextDouble();
            }
        }
Beispiel #12
0
        public CovarianceMatrix(IGMN owner, int inputLength, int outputLength)
        {
            Vector scaledMaxMin = new Vector(owner.MaxMin.Elements.Length);
            double prod = 1;
            double prod2 = 1;
            Vector invMaxMin = new Vector(owner.MaxMin.Elements.Length);
            for (int i = 0; i < owner.MaxMin.Elements.Length; ++i)
            {
                double temp = Math.Pow(DELTA * owner.MaxMin.Elements[i], 1);
                scaledMaxMin.Elements[i] = temp;
                prod *= temp;
                if (i < inputLength) prod2 *= temp;
                invMaxMin.Elements[i] = 1 / temp;
            }
            determinant = prod;
            covariance = DenseMatrix.Diag(scaledMaxMin);
            inverseCovariance = DenseMatrix.Diag(invMaxMin);

            inputDeterminant = prod2;
            inputInverseCovariance = inverseCovariance.Part(0, 0, inputLength, inputLength);

        }
        public void Train()
        {
            GenerateRVector();
            LoadMMatrices("mmatrices.dat");

            Vector[] Q = new Vector[CarNavigationQStore.LENACTION];
            for (int i = 0; i < CarNavigationQStore.LENACTION; ++i)
            {
                Q[i] = new Vector(statenum);
            }

            for (int i = 0; i < CarNavigationQStore.LENACTION; ++i)
            {
                for (int j = 0; j < CarNavigationQStore.LENXY; ++j)
                {
                    for (int k = 0; k < CarNavigationQStore.LENXY; ++k)
                    {
                        for (int l = 0; l < CarNavigationQStore.LENANG; ++l)
                        {
                            Q[i].Elements[l * CarNavigationQStore.LENXY * CarNavigationQStore.LENXY + k * CarNavigationQStore.LENXY + j] = qstore.value[i, j, k, l];
                        }                        
                    }
                }
            }

            Vector[] p_ = new Vector[CarNavigationQStore.LENACTION];
            float max_ = float.MinValue;
            for (int i = 0; i < CarNavigationQStore.LENACTION; ++i)
            {
                p_[i] = new Vector(Q[i]);
                for (int j = 0; j < statenum; ++j)
                {
                    if (max_ < p_[i].Elements[j]) max_ = (float)p_[i].Elements[j];
                }
            }

            for (int i = 0; i < CarNavigationQStore.LENACTION; ++i)
            {                
                p_[i].Exp(-max_);
            }

            Vector sum = new Vector(statenum);
            for (int i = 0; i < CarNavigationQStore.LENACTION; ++i)
            {
                sum += p_[i];
            }

            SparseMatrix[] Ma_ = new SparseMatrix[CarNavigationQStore.LENACTION];
            for (int i = 0; i < CarNavigationQStore.LENACTION; ++i)
            {
                p_[i].Div(sum);
                Ma_[i] = new SparseMatrix(Ma[i]);
                Ma_[i].Multiply(p_[i]);
            }

            //M matrix kiszamitasa
            SparseMatrix M = new SparseMatrix(statenum);
            for (int i = 0; i < CarNavigationQStore.LENACTION; ++i)
            {
                M.Add(Ma_[i]);                
            }
            M.Multiply(0.9999f);

            SparseMatrix IM = SparseMatrix.Identity(statenum) - M;

            //IM.WriteToFile("IM.txt");

            Vector utility = IM.SolveLinearEquation2(R);

            for (int i = 0; i < CarNavigationQStore.LENACTION; ++i)
            {
                Q[i] = Ma[i].MatrixMultiplyRight(utility);
            }

            Vector QMax = new Vector(statenum);
            for (int j = 0; j < statenum; ++j)
            {
                float max = float.MinValue;
                int best = 0;
                for (int i = 0; i < CarNavigationQStore.LENACTION; ++i)
                {
                    if (Q[i].Elements[j] > max)
                    {
                        max = (float)Q[i].Elements[j];
                        best = i;
                    }
                }
                QMax.Elements[j] = best;
            }


            for (int i = 0; i < CarNavigationQStore.LENACTION; ++i)
            {
                for (int j = 0; j < CarNavigationQStore.LENXY; ++j)
                {
                    for (int k = 0; k < CarNavigationQStore.LENXY; ++k)
                    {
                        for (int l = 0; l < CarNavigationQStore.LENANG; ++l)
                        {
                            qstore.value[i, j, k, l] = (float)Q[i].Elements[l * CarNavigationQStore.LENXY * CarNavigationQStore.LENXY + k * CarNavigationQStore.LENXY + j];
                        }
                    }
                }                               
            }

            utility.WriteToFile("u.txt");

            qstore.Save("qstore.dat");

            //Q[0].WriteToFile("q1.txt");
            //Q[1].WriteToFile("q2.txt");
            //Q[2].WriteToFile("q3.txt");
            //Q[3].WriteToFile("q4.txt");
            //Q[4].WriteToFile("q5.txt");
            //Q[5].WriteToFile("q6.txt");
            //Q[6].WriteToFile("q7.txt");
            //Q[7].WriteToFile("q8.txt");
            //Q[8].WriteToFile("q9.txt");
            //Q[9].WriteToFile("q10.txt");
            //Q[10].WriteToFile("q11.txt");            
        }
        private void GenerateRVector()
        {
            CarNavigationEnvironment testws = new CarNavigationEnvironment();
            R = new Vector(statenum);
            float sum = 0;
            for (int ii = 0; ii < statenum; ++ii)
            {
                int l = ii / (CarNavigationQStore.LENXY * CarNavigationQStore.LENXY);
                int iil = ii - l * (CarNavigationQStore.LENXY * CarNavigationQStore.LENXY);
                int k = iil / CarNavigationQStore.LENXY;
                int j = iil % CarNavigationQStore.LENXY;

                CarNavigationState state;
                CarNavigationQStore.GetState(j, k, l, out state);

                testws.x = state.x;
                testws.y = state.y;
                testws.alpha = state.alpha;

                R.Elements[ii] = testws.Reward();
                sum += (float)R.Elements[ii];
            }
            Console.Out.Write(sum);
        }
Beispiel #15
0
        public void ModifyWithVector(Vector eps, double w, double scalarinverse, double inputscalarinverse)
        {            
            //szoroz (1-w)
            //det(s*X) = s^rank(X)*det(X)
            //(s*X)^(-1) = 1/s*X^(-1)
            covariance.Multiply(1 - w);
            inverseCovariance.Multiply(1 / (1 - w));
            inputInverseCovariance.Multiply(1 / (1 - w));
            determinant = (double)(determinant * Math.Pow(1 - w, covariance.Rows));
            inputDeterminant = (double)(determinant * Math.Pow(1 - w, inputInverseCovariance.Rows));
            double newscalarinverse = scalarinverse / (1 - w);
            double newinputscalarinverse = inputscalarinverse / (1 - w);

            //hozzaad (-w*w+w) * v
            //det(X + cr) = det(X)(1 + rX^(−1)c)
            //(A+cr)^(-1)=A^(-1) - (A^(-1)*c*r*A^(-1))/(1 + rX^(−1)c)

            double factor = -w * w + w;  
            covariance.Add(DenseMatrix.CreateDiad(eps, eps), factor);
            Vector inveps = inverseCovariance.VectorMultiplyRight(eps);
            Vector inpinveps = inputInverseCovariance.VectorMultiplyRight(eps);
            inverseCovariance.Add(DenseMatrix.CreateDiad(inveps, inveps), -factor / (1 + factor * newscalarinverse));
            inputInverseCovariance.Add(DenseMatrix.CreateDiad(inpinveps, inpinveps), -factor / (1 + factor * newinputscalarinverse));
            determinant *= (1 + factor * newscalarinverse);
            inputDeterminant *= (1 + factor * newinputscalarinverse);
        }
Beispiel #16
0
 public double Recall(Vector input) {
     double e;
     return Recall(input, out e);
 }
Beispiel #17
0
 public Gaussian(int dimension)
 {
     mean = new Vector(dimension);
     covariance = new CovarianceMatrix(dimension);
 }
Beispiel #18
0
        public Vector SolveLinearEquation2(Vector y) // this * result = y
        {
            List<int> rows = new List<int>();
            List<int> cols = new List<int>();
            List<double> fvalues = new List<double>();

            foreach (long i in values.Keys)
            {
                rows.Add(Row(i)+1);
                cols.Add(Column(i)+1);
                fvalues.Add(this[i]);
            }

            MWNumericArray _rows = new MWNumericArray(rows.Count, 1, rows.ToArray());
            MWNumericArray _cols = new MWNumericArray(cols.Count, 1, cols.ToArray());
            MWNumericArray _values = new MWNumericArray(fvalues.Count, 1, fvalues.ToArray());
            MWNumericArray _y = new MWNumericArray(y.Elements.Length, 1, y.Elements);            

            solveclass solve = new solveclass();
            MWNumericArray ret = (MWNumericArray)solve.solve(_rows, _cols, _values, _y);
            double[,] fret = (double[,])ret.ToArray(MWArrayComponent.Real);
            double[] fret2 = new double[fret.GetLength(0)];
            for (int i = 0; i < fret2.Length; ++i)
            {
                fret2[i] = (double)fret[i, 0];
            }
            return new Vector(fret2);            
        }
Beispiel #19
0
        public Vector SolveLinearEquation(Vector y) // this * result = y
        {
            Vector ret = new Vector(y.Elements);
            for (int row = 0; row < size; ++row)
            {
                double z = 1 / this[row, row];
                MultiplyRow(row, z);
                ret.Elements[row] *= z;

                List<int> temp = new List<int>(columnElements[row]);                             
                foreach (int row2 in temp)
                {
                    if (row2 > row)
                    {
                        double f = this[row2, row];
                        SubtractRowFromRow(row2, row, f);
                        ret.Elements[row2] -= ret.Elements[row] * f;
                    }
                }
                Console.Out.WriteLine(row);
            }

            for (int row = size-1; row >= 0; --row)
            {
                List<int> temp = new List<int>(columnElements[row]);
                foreach (int row2 in temp)
                {
                    if (row2 < row)
                    {
                        double f = this[row2, row];
                        SubtractRowFromRow(row2, row, f);
                        ret.Elements[row2] -= ret.Elements[row] * f;
                    }
                }
                Console.Out.WriteLine(row);
            }

            return ret;
        }
Beispiel #20
0
        public Vector MatrixMultiplyRight(Vector v) // return M * v
        {
            if (v.Elements.Length != size)
            {
                return null;
            }

            Vector ret = new Vector(size);

            for (int row = 0; row < size; ++row)
            {
                double sum = 0;
                foreach (int col in rowElements[row])
                {
                    sum += this[row, col] * v.Elements[col];
                }
                ret.Elements[row] = sum;
            }

            return ret;
        }
Beispiel #21
0
        public void Multiply(Vector v)
        {
            if (v.Elements.Length != size)
            {
                return;
            }

            foreach (long i in values.Keys.ToArray())
            {
                ChangeValue(i, this[i] * v.Elements[Row(i)]);
            }

        }
Beispiel #22
0
 private XTVector convertVector(Vector v)
 {
     return new XTVector((double[])v.Elements.Clone());
 }
Beispiel #23
0
 private XTVector convertInputVector(Vector v, double lastValue)
 {
     double[] vv = new double[v.Elements.Length+1];
     for(int i=0; i<vv.Length-1; ++i) 
     {
         vv[i] = v.Elements[i];
     }
     vv[vv.Length - 1] = lastValue;
     return new XTVector(vv);
 }
Beispiel #24
0
 public Gaussian(Vector mean, Vector diagCovariance)
 {
     this.mean = mean;
     covariance = new CovarianceMatrix(diagCovariance);
 }
Beispiel #25
0
        public double Recall(Vector input, out double variance)//input vector is 1 dimension shorter then the gaussians
        {
            variance = -1;//TODO!

            List<IGMNData> possibleRelevant = xtree.rangeQuery(new MBR(convertInputVector(input, double.MinValue), convertInputVector(input, double.MaxValue)));
            
            double sum = 0;
            for (int i = 0; i < possibleRelevant.Count; ++i)//cortical
            {
                sum += possibleRelevant[i].Accumlator;
            }
            sum /= possibleRelevant.Count;

            
            List<double> posterior = new List<double>();
            double sum2 = 0;
            List<IGMNData> relevant = new List<IGMNData>();
            for (int i = 0; i < possibleRelevant.Count; ++i)
            {
                double e;
                double relev = possibleRelevant[i].InputGaussian.Likelihood(input, out e) * (possibleRelevant[i].Accumlator / sum); // likelihood * prior
                if (e > RelevanceLevel)
                {
                    posterior.Add(relev);
                    sum2 += relev;
                    relevant.Add(possibleRelevant[i]);
                }
            }
            
            for (int i = 0; i < posterior.Count; ++i)
            {
                posterior[i] /= sum2;
            }

            double sumV = 0;
            for (int i = 0; i < relevant.Count; ++i)
            {
                int inputLength = relevant[i].InputGaussian.Mean.Elements.Length;
                DenseMatrix temp = relevant[i].Gaussian.Covariance.Covariance.MatrixMultiply(relevant[i].InputGaussian.Covariance.InverseCovariance, inputLength, 0, 1, inputLength);
                Vector vv = temp.VectorMultiplyRight(input - relevant[i].InputGaussian.Mean);
                double v = vv.Elements[0];
                v += relevant[i].Gaussian.Mean.Elements[inputLength];
                v *= posterior[i];
                sumV += v;
            }

            return sumV;
        }
Beispiel #26
0
 public Gaussian(Vector mean, CovarianceMatrix matrix)
 {
     this.mean = mean;
     this.covariance = matrix;
 }
Beispiel #27
0
        public void Train(Vector x)
        {
            double sum = 0;

            XTVector min = convertVector(x); min[min.Length - 1] = double.MinValue;
            XTVector max = convertVector(x); max[max.Length - 1] = double.MaxValue;
            List<IGMNData> possibleRelevant2 = xtree.rangeQuery(new MBR(min, max));

            for (int i = 0; i < possibleRelevant2.Count; ++i)
            {
                sum += possibleRelevant2[i].Accumlator;
                possibleRelevant2[i].Age++;
            }
            sum /= possibleRelevant2.Count;


            List<IGMNData> possibleRelevant = xtree.pointQuery(convertVector(x));
            List<double> posterior = new List<double>();
            double sum2 = 0;
            List<IGMNData> relevant = new List<IGMNData>();
            for (int i = 0; i < possibleRelevant.Count; ++i)
            {
                double e;
                double relev = possibleRelevant[i].Gaussian.Likelihood(x, out e) * (possibleRelevant[i].Accumlator / sum); // likelihood * prior
                if (e > RelevanceLevel)
                {
                    posterior.Add(relev);
                    sum2 += relev;
                    relevant.Add(possibleRelevant[i]);
                }
            }

            if (relevant.Count == 0)
            {
                IGMNData newdata = new IGMNData(this, x);
                cortical.Add(newdata);
                xtree.Insert(newdata);
            }
            else
            {
                //relevance normalization
                for (int i = 0; i < posterior.Count; ++i)
                {
                    posterior[i] /= sum2;
                }
                
                //true relevant gaussians update
                for (int i = 0; i < relevant.Count; ++i)
                {                    
                    relevant[i].Accumlator += posterior[i];
                    double w = posterior[i] / relevant[i].Accumlator;
                    
                    xtree.Delete(relevant[i]);
                    relevant[i].RefineWithData(x, w);//0.25|w
                    xtree.Insert(relevant[i]);
                }              
            }

            //delete unneeded gaussians
            List<IGMNData> deleteList = new List<IGMNData>();
            foreach (IGMNData g in possibleRelevant2)
            {
                if ((g.Age > AGE_MIN) && (g.Accumlator < ACCUMLATOR_MIN))
                {
                    deleteList.Add(g);
                }
            }

            foreach (IGMNData g in deleteList)
            {
                cortical.Remove(g);
                xtree.Delete(g);
            }
        }
Beispiel #28
0
 public double Likelihood(Vector x)
 {
     double e;
     return Likelihood(x, out e);
 }
Beispiel #29
0
 public Gaussian(Vector mean)
 {
     this.mean = mean;
     covariance = new CovarianceMatrix(mean.Elements.Length);
 }
Beispiel #30
0
 public void ModifyWithVector(Vector eps, double w)
 {
     double sci = GetScalarInverse(eps);
     double isci = GetInputScalarInverse(eps);
     ModifyWithVector(eps, w, sci, isci);
 }