void IGradient::Gradient(const RnFunction *f, double step, const DoubleVector &x, DoubleVector &g) { double h = step; for (unsigned int i=0; i<g.length(); i++) { double cx = x[i]; const_cast<DoubleVector&>(x)[i] = cx - h; double f1 = f->fx(x); const_cast<DoubleVector&>(x)[i] = cx + h; double f2 = f->fx(x); const_cast<DoubleVector&>(x)[i] = cx; g[i] = (f2 - f1) / (2.0 * h); } }
void HeatControl2Delta::gradient(const DoubleVector& x, DoubleVector& g) { px = &x; DoubleMatrix u; IParabolicEquation2D::calculateMVD(u, h1, h2, ht, N1, N2, M, a1, a2); pu = &u; DoubleCube psi; IBackwardParabolicEquation2D::calculateMVD(psi, h1, h2, ht, N1, N2, M, a1, a2); for (unsigned int i=0; i<g.length(); i++) g[i] = 0.0; for (unsigned int k=M; k!=(unsigned int)0-1; k--) { calculateGX(x, psi.matrix(k), g, k); //calculateGF(x, psi[k], g, k); } // for (unsigned int k=0; k<=M; k++) // { // unsigned int i1 = (unsigned int)round(E[0]/h1); // unsigned int j1 = (unsigned int)round(E[1]/h2); // g[0*(M+1)+k] = -psi[k][j1][i1] + 2.0*alpha*(v[0*(M+1)+k] - v1(k*ht)); // unsigned int i2 = (unsigned int)round(E[2]/h1); // unsigned int j2 = (unsigned int)round(E[3]/h2); // g[1*(M+1)+k] = -psi[k][j2][i2] + 2.0*alpha*(v[1*(M+1)+k] - v2(k*ht)); // unsigned int i3 = (unsigned int)round(E[4]/h1); // unsigned int j3 = (unsigned int)round(E[5]/h2); // g[2*(M+1)+k] = -psi[k][j3][i3] + 2.0*alpha*(v[2*(M+1)+k] - v3(k*ht)); // } psi.clear(); // IGradient::Gradient(this, 0.0001, x, g); }
void IFunctional::gradient(const DoubleVector &pv, DoubleVector &g) const { IFunctional *ifunc = const_cast<IFunctional*>(this); unsigned int L = mTimeDimension.sizeN(); double ht = mTimeDimension.step(); ifunc->fromVector(pv, ifunc->mParameter); forward->setParameter(mParameter); backward->setParameter(mParameter); DoubleMatrix u; DoubleMatrix p; vector<ExtendedSpaceNode2D> u_info; forward->calculateMVD(u, u_info, true); backward->u = &u; backward->U = &ifunc->U; backward->info = &u_info; vector<ExtendedSpaceNode2D> p_info; backward->calculateMVD(p, p_info, true); g.clear(); g.resize(pv.length(), 0.0); unsigned int gi = 0; // k if (optimizeK) { for (unsigned int i=0; i<mParameter.Lc; i++) { ExtendedSpaceNode2D &pi = p_info[i]; for (unsigned int j=0; j<mParameter.Lo; j++) { ExtendedSpaceNode2D &uj = u_info[j]; double grad_Kij = 0.0; grad_Kij += 0.5 * (pi.value(0)+2.0*r*gpi(i,0,u_info)*sgn(g0i(i,0,u_info))) * (uj.value(0) - mParameter.z.at(i,j)); for (unsigned int m=1; m<=L-1; m++) { grad_Kij += (pi.value(m)+2.0*r*gpi(i,m,u_info)*sgn(g0i(i,m,u_info))) * (uj.value(m) - mParameter.z.at(i,j)); } grad_Kij += 0.5 * (pi.value(L)+2.0*r*gpi(i,L,u_info)*sgn(g0i(i,L,u_info))) * (uj.value(L) - mParameter.z.at(i,j)); grad_Kij *= -ht; g[gi++] = grad_Kij + 2.0*regEpsilon*(mParameter.k.at(i,j) - mParameter0.k.at(i,j)); } } } else { for (unsigned int i=0; i<mParameter.Lc; i++) { for (unsigned int j=0; j<mParameter.Lo; j++) { g[gi++] = 0.0; } } } // z if (optimizeZ) { for (unsigned int i=0; i<mParameter.Lc; i++) { ExtendedSpaceNode2D &pi = p_info[i]; for (unsigned int j=0; j<mParameter.Lo; j++) { double grad_Zij = 0.0; grad_Zij += 0.5 * (pi.value(0)+2.0*r*gpi(i,0,u_info)*sgn(g0i(i,0,u_info))) * mParameter.k.at(i,j); for (unsigned int m=1; m<=L-1; m++) { grad_Zij += (pi.value(m)+2.0*r*gpi(i,m,u_info)*sgn(g0i(i,m,u_info))) * mParameter.k.at(i,j); } grad_Zij += 0.5 * (pi.value(L)+2.0*r*gpi(i,L,u_info)*sgn(g0i(i,L,u_info))) * mParameter.k.at(i,j); grad_Zij *= ht; g[gi++] = grad_Zij + 2.0*regEpsilon*(mParameter.z[i][j] - mParameter0.z[i][j]); } } } else { for (unsigned int i=0; i<mParameter.Lc; i++) { for (unsigned int j=0; j<mParameter.Lo; j++) { g[gi++] = 0.0; } } } // xi if (optimizeO) { for (unsigned int j=0; j<mParameter.Lo; j++) { ExtendedSpaceNode2D &uj = u_info[j]; double gradXijX = 0.0; double gradXijY = 0.0; double vi = 0.0; vi = 0.0; for (unsigned int i=0; i<mParameter.Lc; i++) vi += mParameter.k.at(i,j) * (p_info[i].value(0)+2.0*r*gpi(i,0,u_info)*sgn(g0i(i,0,u_info))); gradXijX += 0.5 * uj.valueDx(0) * vi; gradXijY += 0.5 * uj.valueDy(0) * vi; for (unsigned int m=1; m<=L-1; m++) { vi = 0.0; for (unsigned int i=0; i<mParameter.Lc; i++) vi += mParameter.k.at(i,j)*(p_info[i].value(m)+2.0*r*gpi(i,m,u_info)*sgn(g0i(i,m,u_info))); gradXijX += uj.valueDx(m) * vi; gradXijY += uj.valueDy(m) * vi; } vi = 0.0; for (unsigned int i=0; i<mParameter.Lc; i++) vi += mParameter.k.at(i,j)*(p_info[i].value(L)+2.0*r*gpi(i,L,u_info)*sgn(g0i(i,L,u_info))); gradXijX += 0.5 * uj.valueDx(L) * vi; gradXijY += 0.5 * uj.valueDy(L) * vi; gradXijX *= -ht; gradXijY *= -ht; g[gi++] = gradXijX + 2.0*regEpsilon*(mParameter.xi[j].x - mParameter0.xi[j].x); g[gi++] = gradXijY + 2.0*regEpsilon*(mParameter.xi[j].y - mParameter0.xi[j].y); } } else { for (unsigned int j=0; j<mParameter.Lo; j++) { g[gi++] = 0.0; g[gi++] = 0.0; } } // eta if (optimizeC) { for (unsigned int i=0; i<mParameter.Lc; i++) { ExtendedSpaceNode2D &pi = p_info[i]; double grad_EtaiX = 0.0; double grad_EtaiY = 0.0; double vi = 0.0; vi = 0.0; for (unsigned int j=0; j<mParameter.Lo; j++) vi += mParameter.k.at(i,j) * (u_info[j].value(0) - mParameter.z.at(i,j)); grad_EtaiX += 0.5 * pi.valueDx(0) * vi; grad_EtaiY += 0.5 * pi.valueDy(0) * vi; for (unsigned int m=1; m<=L-1; m++) { vi = 0.0; for (unsigned int j=0; j<mParameter.Lo; j++) vi += mParameter.k.at(i,j) * (u_info[j].value(m) - mParameter.z.at(i,j)); grad_EtaiX += pi.valueDx(m) * vi; grad_EtaiY += pi.valueDy(m) * vi; } vi = 0.0; for (unsigned int j=0; j<mParameter.Lo; j++) vi += mParameter.k.at(i,j) * (u_info[j].value(L) - mParameter.z.at(i,j)); grad_EtaiX += 0.5 * pi.valueDx(L) * vi; grad_EtaiY += 0.5 * pi.valueDy(L) * vi; grad_EtaiX *= -ht; grad_EtaiY *= -ht; g[gi++] = grad_EtaiX + 2.0*regEpsilon*(mParameter.eta[i].x - mParameter0.eta[i].x); g[gi++] = grad_EtaiY + 2.0*regEpsilon*(mParameter.eta[i].y - mParameter0.eta[i].y); } } else { for (unsigned int i=0; i<mParameter.Lc; i++) { g[gi++] = 0.0; g[gi++] = 0.0; } } u_info.clear(); p_info.clear(); }