private static bool ModificatedDualIteration() { var vectorCollection = new List<Vector<double>>(); foreach (var item in _task.Jb) { vectorCollection.Add(_task.A.Column(item)); } var aBMatrix = DenseMatrix.OfColumnVectors(vectorCollection.ToArray()); var bMatrix = aBMatrix.Inverse(); // Step3 var kappaList = bMatrix * _task.b; Vector<double> allKappaList = new DenseVector(_task.A.ColumnCount, 0); for (int i = 0; i < allKappaList.Count; i++) { if (_JNbUpper.Contains(i)) { allKappaList[i] = _task.dLower[i]; } else if (_JNbLower.Contains(i)) { allKappaList[i] = _task.dUpper[i]; } else { allKappaList[i] = 0; } } kappaList = bMatrix * (_task.b - _task.A * allKappaList); for (int i = 0; i < _task.Jb.Count; i++) { allKappaList[_task.Jb[i]] = kappaList[i]; } // Step3 double kappaValue = 0; int kappaIndex = 0; var check = false; for (int i = 0; i < allKappaList.Count; i++) { if (allKappaList[i] > _task.dUpper[i] || allKappaList[i] < _task.dLower[i]) { check = true; kappaValue = allKappaList[i]; kappaIndex = i; break; } } if (!check) { _task.xo = allKappaList; //Logger.Log("Stopped at third step"); _stopStep = 3; return false; } // Step4 is unnecessary // Step5 var nk = kappaValue < _task.dLower[kappaIndex] ? 1 : -1; var deltaYT = nk * DenseVector.Create(_task.Jb.Count, i => i == _task.Jb.ToList().IndexOf(kappaIndex) ? 1 : 0) * DenseMatrix.OfColumnVectors(vectorCollection.ToArray()).Inverse(); var nVector = new DenseVector(_task.A.ColumnCount); for (int i = 0; i < _task.A.ColumnCount; i++) { if (!_task.Jb.Contains(i)) { nVector[i] = (deltaYT * _task.A.Column(i)); } } // Step6 Vector<double> sigmaVector = new DenseVector(_task.A.ColumnCount); for (int i = 0; i < sigmaVector.Count; i++) { if (_task.dLower[i] == _task.dUpper[i]) { sigmaVector[i] = double.PositiveInfinity; } else if (_JNbUpper.Contains(i) && nVector[i] < Eps) { sigmaVector[i] = -deltas[i] / nVector[i]; } else if (_JNbLower.Contains(i) && nVector[i] > Eps) { sigmaVector[i] = -deltas[i] / nVector[i]; } else { sigmaVector[i] = double.PositiveInfinity; } } var sigma0 = sigmaVector.Min(); var sigma0Index = sigmaVector.MinimumIndex(); // Step7 if (sigma0 == double.PositiveInfinity) { //Logger.Log("Stopped at seventh step"); _stopStep = 7; return false; } // Step8 Vector<double> newDeltas = new DenseVector(_task.A.ColumnCount); for (int i = 0; i < newDeltas.Count; i++) { if (_task.Jb.Contains(i) && i != kappaIndex) { newDeltas[i] = 0; } else if (i == kappaIndex) { newDeltas[i] = sigma0 * nk; } else { newDeltas[i] = deltas[i] + sigma0 * nVector[i]; } } deltas = newDeltas.ToList(); // Step9 _task.Jb[_task.Jb.ToList().IndexOf(kappaIndex)] = sigma0Index; // Step10 if (nk == 1.0) { if (_JNbUpper.Contains(sigma0Index)) { _JNbUpper[_JNbUpper.IndexOf(sigma0Index)] = kappaIndex; } else { _JNbUpper.Add(kappaIndex); } } else if (nk == -1.0) { if (_JNbUpper.Contains(sigma0Index)) { _JNbUpper.Remove(sigma0Index); } } _JNbLower.Clear(); for (int i = 0; i < _task.A.ColumnCount; i++) { if (!_JNbUpper.Contains(i) && !_task.Jb.Contains(i)) { _JNbLower.Add(i); } } return true; }
public void Train(DenseMatrix X, DenseVector d, DenseVector Kd) { int R = X.RowCount; int N = X.ColumnCount; int U = 0; //the number of neurons in the structure var c = new DenseMatrix(R, 1); var sigma = new DenseMatrix(R, 1); var Q = new DenseMatrix((R + 1), (R + 1)); var O = new DenseMatrix(1, (R + 1)); var pT_n = new DenseMatrix((R + 1), 1); double maxPhi = 0; int maxIndex; var Psi = new DenseMatrix(N, 1); Console.WriteLine("Running..."); //for each observation n in X for (int i = 0; i < N; i++) { Console.WriteLine(100*(i/(double) N) + "%"); var x = new DenseVector(R); X.Column(i, x); //if there are neurons in structure, //update structure recursively. if (U == 0) { c = (DenseMatrix) x.ToColumnMatrix(); sigma = new DenseMatrix(R, 1, SigmaZero); U = 1; Psi = CalculatePsi(X, c, sigma); UpdateStructure(X, Psi, d, ref Q, ref O); pT_n = (DenseMatrix) (CalculateGreatPsi((DenseMatrix) x.ToColumnMatrix(), (DenseMatrix) Psi.Row(i).ToRowMatrix())) .Transpose(); } else { StructureRecurse(X, Psi, d, i, ref Q, ref O, ref pT_n); } bool KeepSpinning = true; while (KeepSpinning) { //Calculate the error and if-part criteria double ee = pT_n.Multiply(O)[0, 0]; double approximationError = Math.Abs(d[i] - ee); DenseVector Phi; double SumPhi; CalculatePhi(x, c, sigma, out Phi, out SumPhi); maxPhi = Phi.Maximum(); maxIndex = Phi.MaximumIndex(); if (approximationError > delta) { if (maxPhi < threshold) { var tempSigma = new DenseVector(R); sigma.Column(maxIndex, tempSigma); double minSigma = tempSigma.Minimum(); int minIndex = tempSigma.MinimumIndex(); sigma[minIndex, maxIndex] = k_sigma*minSigma; Psi = CalculatePsi(X, c, sigma); UpdateStructure(X, Psi, d, ref Q, ref O); var psi = new DenseVector(Psi.ColumnCount); Psi.Row(i, psi); pT_n = (DenseMatrix) CalculateGreatPsi((DenseMatrix) x.ToColumnMatrix(), (DenseMatrix) psi.ToRowMatrix()) .Transpose(); } else { //add a new neuron and update strucutre double distance = 0; var cTemp = new DenseVector(R); var sigmaTemp = new DenseVector(R); //foreach input variable for (int j = 0; j < R; j++) { distance = Math.Abs(x[j] - c[j, 0]); int distanceIndex = 0; //foreach neuron past 1 for (int k = 1; k < U; k++) { if ((Math.Abs(x[j] - c[j, k])) < distance) { distanceIndex = k; distance = Math.Abs(x[j] - c[j, k]); } } if (distance < Kd[j]) { cTemp[j] = c[j, distanceIndex]; sigmaTemp[j] = sigma[j, distanceIndex]; } else { cTemp[j] = x[j]; sigmaTemp[j] = distance; } } //end foreach c = (DenseMatrix) c.InsertColumn(c.ColumnCount - 1, cTemp); sigma = (DenseMatrix) sigma.InsertColumn(sigma.ColumnCount - 1, sigmaTemp); Psi = CalculatePsi(X, c, sigma); UpdateStructure(X, Psi, d, ref Q, ref O); U++; KeepSpinning = false; } } else { if (maxPhi < threshold) { var tempSigma = new DenseVector(R); sigma.Column(maxIndex, tempSigma); double minSigma = tempSigma.Minimum(); int minIndex = tempSigma.MinimumIndex(); sigma[minIndex, maxIndex] = k_sigma*minSigma; Psi = CalculatePsi(X, c, sigma); UpdateStructure(X, Psi, d, ref Q, ref O); var psi = new DenseVector(Psi.ColumnCount); Psi.Row(i, psi); pT_n = (DenseMatrix) CalculateGreatPsi((DenseMatrix) x.ToColumnMatrix(), (DenseMatrix) psi.ToRowMatrix()) .Transpose(); } else { KeepSpinning = false; } } } } out_C = c; out_O = O; out_Sigma = sigma; Console.WriteLine("Done."); }