/// <summary> /// Performs a simulation step using Symplectic integration with constrained dynamics. /// The constraints are treated as implicit /// </summary> private void stepSymplecticConstraints() { // TO BE COMPLETED VectorXD v = new DenseVectorXD(m_numDoFs); VectorXD f = new DenseVectorXD(m_numDoFs); MatrixXD Minv = new DenseMatrixXD(m_numDoFs); MatrixXD J = new DenseMatrixXD(m_numConstraints, m_numDoFs); MatrixXD A = new DenseMatrixXD(m_numDoFs); VectorXD B = new DenseVectorXD(m_numDoFs); VectorXD c = new DenseVectorXD(m_numConstraints); VectorXD b = new DenseVectorXD(m_numDoFs); VectorXD lamda = new DenseVectorXD(m_numConstraints); MatrixXD I = DenseMatrixXD.CreateIdentity(m_numDoFs); f.Clear(); Minv.Clear(); J.Clear(); foreach (ISimulable obj in m_objs) { obj.GetVelocity(v); obj.GetForce(f); obj.GetMassInverse(Minv); } foreach (IConstraint constraint in m_constraints) { constraint.GetForce(f); constraint.GetConstraints(c); constraint.GetConstraintJacobian(J); } foreach (ISimulable obj in m_objs) { obj.FixVector(f); obj.FixMatrix(Minv); } b = v + TimeStep * Minv * f; A = J * I * J.Transpose(); B = J * I * b + (1.0f / TimeStep) * c; lamda = A.Solve(B); v = I * (b - J.Transpose() * lamda); VectorXD x = TimeStep * v; foreach (ISimulable obj in m_objs) { obj.AdvanceIncrementalPosition(x); obj.SetVelocity(v); } }
/// <summary> /// Performs a simulation step using Symplectic integration. /// </summary> private void stepSymplectic() { // TO BE COMPLETED VectorXD v = new DenseVectorXD(m_numDoFs); VectorXD f = new DenseVectorXD(m_numDoFs); MatrixXD Minv = new DenseMatrixXD(m_numDoFs); f.Clear(); Minv.Clear(); foreach (ISimulable obj in m_objs) { obj.GetVelocity(v); obj.GetForce(f); obj.GetMassInverse(Minv); } foreach (IConstraint constraint in m_constraints) { constraint.GetForce(f); } foreach (ISimulable obj in m_objs) { obj.FixVector(f); obj.FixMatrix(Minv); } v += TimeStep * (Minv * f); VectorXD x = TimeStep * v; foreach (ISimulable obj in m_objs) { obj.AdvanceIncrementalPosition(x); obj.SetVelocity(v); } }
/// <summary> /// CALCULATE BUTTON CLICK /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void calculateButton_Click(object sender, EventArgs e) { _currMethod.AlphaMethod = null; Vector<double> start = new DenseVector(_varCount); try { var text = startingPointTextBox.Text; var textValues = text.Split(','); for (var i = 0; i < _varCount; i++) { start[i] = double.Parse(textValues[i]); } } catch (Exception) { start.Clear(); } var fh = new FunctionHolder(_currFunction, start); _currMethod.Fh = fh; if (_alphaMethodChosen) { _currAlphaMethod.F = fh.AlphaFunction; _currAlphaMethod.Df = fh.AlphaDiffFunction; _currMethod.AlphaMethod = _currAlphaMethod; } double eps; try { eps = double.Parse(precisionTextBox.Text); } catch (Exception) { eps = 1e-5; precisionTextBox.Text = @"1e-5"; } _currMethod.Eps = eps; //Делаем сам метод var sw = Stopwatch.StartNew(); _currMethod.Execute(); sw.Stop(); //Вывод, Двуменрная функция var coord = _currMethod.Answer; answerTextBox.Text = @"Answer:" + Environment.NewLine + coord.ToString(); iterationTextBox.Text = Convert.ToString("Iterations: " + _currMethod.IterationCount); timeTextBox.Text = @"Time (ms):" + Environment.NewLine +((sw.ElapsedMilliseconds == 0) ? ("<1") : (sw.ElapsedMilliseconds.ToString())); }
/// <summary> /// Нажатие кнопки "TEST ALL" /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void materialFlatButton1_Click(object sender, EventArgs e) { var exports = new List<TestExport>(_functions.Count*_methods.Count); foreach (var function in _functions) { ParseFunction(function); foreach (var method in _methods) { _currMethod = method; _currMethod.AlphaMethod = null; Vector<double> start = new DenseVector(_varCount); try { var text = startingPointTextBox.Text; var textValues = text.Split(','); for (var i = 0; i < _varCount; i++) { start[i] = double.Parse(textValues[i]); } } catch (Exception) { start.Clear(); } var fh = new FunctionHolder(_currFunction, start); _currMethod.Fh = fh; if (_alphaMethodChosen) { _currAlphaMethod.F = fh.AlphaFunction; _currAlphaMethod.Df = fh.AlphaDiffFunction; _currMethod.AlphaMethod = _currAlphaMethod; } double eps; try { eps = double.Parse(precisionTextBox.Text); } catch (Exception) { eps = 1e-5; precisionTextBox.Text = @"1e-5"; } _currMethod.Eps = eps; //Делаем сам метод var sw = Stopwatch.StartNew(); _currMethod.Execute(); sw.Stop(); //Вывод, Двуменрная функция var coord = _currMethod.Answer; exports.Add(new TestExport(method.Name,function,coord.ToVectorString(),method.IterationCount.ToString(),sw.ElapsedMilliseconds.ToString(),eps.ToString())); } } var testForm = new TestForm(exports); _materialSkinManager.AddFormToManage(testForm); testForm.ShowDialog(); }
private DenseVector CalculateVGradient() { DenseVector vgrad = new DenseVector(V.Count); vgrad.Clear(); //TODO test if this is necessary for(int k = 0; k < vgrad.Count; k++) { for(int m = 0; m < TrainingInputs.Count; m++) { for(int horz = 0; horz < TrainingInputs[m].XSites; horz++) { for(int vert = 0; vert < TrainingInputs[m].YSites; vert++) { //vgrad[k] = sum over image sites in all images of //[ sum over image sites of x_i x_j (mu_ij (y))_k] <- vterm //- //[dzdv]/[z] // //all minus v_k/tau^2 double z = 0; //x_i int x = (int)(TrainingOutputs[m][horz,vert])*2 - 1; //Sum over neighbors of x_i x_j mu_{ij}(y)_k double vterm = 0; foreach(Tuple<int,int> j in TrainingInputs[m].GetNeighbors(horz, vert)) { int jx = (int)(TrainingOutputs[m][j.Item1,j.Item2])*2 - 1; DenseVector mu; if(ImageData.IsEarlier(horz,vert,j.Item1,j.Item2))mu = Crosser.Cross(TrainingInputs[m][horz,vert],TrainingInputs[m][j.Item1,j.Item2]); else mu = Crosser.Cross(TrainingInputs[m][j.Item1,j.Item2], TrainingInputs[m][horz,vert]); vterm += x * jx * mu[k]; } vgrad[k] += vterm; double dzdv = 0; //h_i(y): DenseVector h = Transformer.Transform(TrainingInputs[m][horz,vert]); //Sum over possible x_i for(int tempx = -1; tempx <= 1; tempx += 2) { double logofcoeff = MathWrapper.Log(MathWrapper.Sigma(tempx * W.DotProduct(h))); double dzdvterm = 0; //sum over the neighbors foreach(Tuple<int,int> j in TrainingInputs[m].GetNeighbors(horz,vert)) { int jx = (int)(TrainingOutputs[m][j.Item1,j.Item2])*2 - 1; DenseVector mu; if(ImageData.IsEarlier(horz,vert,j.Item1,j.Item2))mu = Crosser.Cross(TrainingInputs[m][horz,vert],TrainingInputs[m][j.Item1,j.Item2]); else mu = Crosser.Cross(TrainingInputs[m][j.Item1,j.Item2], TrainingInputs[m][horz,vert]); logofcoeff += tempx * jx * V.DotProduct(mu); dzdvterm += tempx * jx * mu[k]; } double coeff = MathWrapper.Exp(logofcoeff); z += coeff; dzdv += coeff * dzdvterm; if(double.IsNaN(dzdv)||double.IsNaN(z)||double.IsInfinity(dzdv)||double.IsInfinity(z)) throw new NotFiniteNumberException(); } if(z <= 0d) throw new NotFiniteNumberException(); vgrad[k] -= dzdv/z; } } } vgrad[k] -= V[k]/(Math.Pow (Tau,2)); } return vgrad; }
private DenseVector CalculateWGradient() { DenseVector wgrad = new DenseVector(W.Count); wgrad.Clear(); //TODO test if this is necessary for(int k = 0; k < wgrad.Count; k++) { for(int m = 0; m < TrainingInputs.Count; m++) { for(int horz = 0; horz < TrainingInputs[m].XSites; horz++) { for(int vert = 0; vert < TrainingInputs[m].YSites; vert++) { //h_i(y): DenseVector h = Transformer.Transform(TrainingInputs[m][horz,vert]); if(double.IsNaN(h[k])) throw new NotFiniteNumberException(); //x_i int x = (int)(TrainingOutputs[m][horz,vert])*2 - 1; //sigma term that keeps reappearing double sig = MathWrapper.Sigma(x * W.DotProduct(h)); //x_i * h_i(y)_k * (1 - sigma(x_i * w^T h_i(y))) //double old = wgrad[k]; wgrad[k] += x*h[k]*(1 - sig); if(double.IsNaN(wgrad[k])) throw new NotFiniteNumberException(); //- ((d/(dw_k)) z_i) / z_i double z = 0; double dzdw = 0; //Sum over possible x_i for(int tempx = -1; tempx <= 1; tempx += 2) { double logofcoeff = MathWrapper.Log(MathWrapper.Sigma(tempx * W.DotProduct(h))); //sum over the neighbors foreach(Tuple<int,int> j in TrainingInputs[m].GetNeighbors(horz,vert)) { int jx = (int)(TrainingOutputs[m][j.Item1,j.Item2])*2 - 1; DenseVector mu; if(ImageData.IsEarlier(horz,vert,j.Item1,j.Item2))mu = Crosser.Cross(TrainingInputs[m][horz,vert],TrainingInputs[m][j.Item1,j.Item2]); else mu = Crosser.Cross(TrainingInputs[m][j.Item1,j.Item2], TrainingInputs[m][horz,vert]); logofcoeff += tempx * jx * V.DotProduct(mu); } double coeff = MathWrapper.Exp(logofcoeff); z += coeff; double multfactor = (1 - MathWrapper.Sigma (tempx * W.DotProduct(h))); dzdw += coeff * tempx*h[k]*multfactor; if(double.IsNaN(dzdw)||double.IsNaN(z)||double.IsInfinity(dzdw)||double.IsInfinity(z)) throw new NotFiniteNumberException(); } if(z <= 0d) throw new NotFiniteNumberException(); wgrad[k] -= dzdw/z; if(double.IsNaN(wgrad[k])) throw new NotFiniteNumberException(); if(double.IsInfinity(wgrad[k])) throw new NotFiniteNumberException(); } } } } return wgrad; }