int main() { int n,m,ji,f,g; int i,cas,ans; yu(); scanf("%d",&cas); for(i=0;i<cas;i++) { ji=0; if(0 != i) printf("\n"); while(scanf("%d%d",&n,&m),0 != n+m) { ji++; ans=0; for(f=1;f<n;f++) { for(g=f+1;g<n;g++) { if(0 == (add[f][g]+m)%muti[f][g]) ans++; } } printf("Case %d: %d\n",ji,ans); } } return 0; }
int main() { int f,i; while(scanf("%d",&nn)!=EOF) { group=nn; n=2*group; for(i=1;i<=nn;i++) { for(f=1;f<=nn;f++) { scanf("%d",&b[i][f]); } } if(0 == pan()) { printf("NO\n"); continue; } for(now=1;now<=31;now++) { chu(); for(i=1;i<=nn;i++) { for(f=i+1;f<=nn;f++) { if(i == f) { continue; } else if((i%2)==0 && (f%2)==0) { huo(i,f); } else if((i%2)==1 && (f%2)==1) { yu(i,f); } else { yihuo(i,f); } } } slove(); if(0 == check()) { printf("NO\n"); break; } } if(32 == now) { printf("YES\n"); } } return 0; }
int main() { int s,t; int u,v; int i,f,cas; scanf("%d",&cas); for(i=0;i<cas;i++) { scanf("%d%d",&n,&d); init(); for(f=0;f<n;f++) scanf("%d",hei+f); yu(); for(f=0;f<n-1;f++) add(f+1,f,-1); for(f=0;f<n-1;f++) { u=tem[f].id; v=tem[f+1].id; if(u < v) add(u,v,d); else add(v,u,d); } if(tem[0].id < tem[n-1].id) s=tem[0].id,t=tem[n-1].id; else s=tem[n-1].id,t=tem[0].id; spfa(s); printf("Case %d: %d\n",i+1,1==cycle?-1:dis[t]); } return 0; }
sc_signed bar() { sc_signed y(6); sc_unsigned yu(6); foo(y, yu); sc_signed z(10); sc_unsigned zu(10); foo(z, zu); sc_signed x(8); sc_unsigned xu(8); foo(x, xu); return x + y + z; }
int main() { yu(); while(scanf("%s%s",ch1,ch2)!=EOF) { if(1 == ex()) break; chu(); fun(); cal(); print(); } return 0; }
void StandardModel<Two_scale>::set(const DoubleVector& y) { int i, j, k = 0; for (i = 1; i <= 3; i++) for (j = 1; j <= 3; j++) { k++; yu(i, j) = y.display(k); yd(i, j) = y.display(k + 9); ye(i, j) = y.display(k + 18); } k = 27; for (i = 1; i <= 3; i++) { k++; g(i) = y.display(k); } }
void StandardModel<Two_scale>::setYukawaElement(yukawa k, int i, int j, double f) { switch (k) { case YU: yu(i, j) = f; break; case YD: yd(i, j) = f; break; case YE: ye(i, j) = f; break; default: assert(false && "StandardModel<Two_scale>::setYukawaElement called with illegal k"); break; } }
int main() { int i,f; while(scanf("%d%d%d%d",&n,&m,&x,&t)!=EOF) { chu(); for(i=1;i<=n;i++) { for(f=1;f<=m;f++) { scanf("%d",&sro[i][f]); } } yu(); printf("%I64d\n",dp()); } return 0; }
int main() { int i,cas,f; string ch; scanf("%d",&cas); for(i=0;i<cas;i++) { scanf("%d",&n); M=(1<<n)-1; chu(); for(f=1;f<=n;f++) { cin>>ch; id[f]=ch; scanf("%d%d",&deadline[f],&tt[f]); } yu(); spfa(); printf("%d\n",dis[M]); print(); } return 0; }
int main(int argc, char *argv[]) { Teuchos::GlobalMPISession mpiSession(&argc, &argv); // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. int iprint = argc - 1; Teuchos::RCP<std::ostream> outStream; Teuchos::oblackholestream bhs; // outputs nothing if (iprint > 0) outStream = Teuchos::rcp(&std::cout, false); else outStream = Teuchos::rcp(&bhs, false); int errorFlag = 0; // *** Example body. try { std::string filename = "input.xml"; Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); RealT V_th = parlist->get("Thermal Voltage", 0.02585); RealT lo_Vsrc = parlist->get("Source Voltage Lower Bound", 0.0); RealT up_Vsrc = parlist->get("Source Voltage Upper Bound", 1.0); RealT step_Vsrc = parlist->get("Source Voltage Step", 1.e-2); RealT true_Is = parlist->get("True Saturation Current", 1.e-12); RealT true_Rs = parlist->get("True Saturation Resistance", 0.25); RealT init_Is = parlist->get("Initial Saturation Current", 1.e-12); RealT init_Rs = parlist->get("Initial Saturation Resistance", 0.25); RealT lo_Is = parlist->get("Saturation Current Lower Bound", 1.e-16); RealT up_Is = parlist->get("Saturation Current Upper Bound", 1.e-1); RealT lo_Rs = parlist->get("Saturation Resistance Lower Bound", 1.e-2); RealT up_Rs = parlist->get("Saturation Resistance Upper Bound", 30.0); // bool use_lambertw = parlist->get("Use Analytical Solution",true); bool use_scale = parlist->get("Use Scaling For Epsilon-Active Sets",true); bool use_sqp = parlist->get("Use SQP", true); // bool use_linesearch = parlist->get("Use Line Search", true); // bool datatype = parlist->get("Get Data From Input File",false); // bool use_adjoint = parlist->get("Use Adjoint Gradient Computation",false); // int use_hessvec = parlist->get("Use Hessian-Vector Implementation",1); // 0 - FD, 1 - exact, 2 - GN // bool plot = parlist->get("Generate Plot Data",false); // RealT noise = parlist->get("Measurement Noise",0.0); EqualityConstraint_DiodeCircuit<RealT> con(V_th,lo_Vsrc,up_Vsrc,step_Vsrc); RealT alpha = 1.e-4; // regularization parameter (unused) int ns = 101; // number of Vsrc components int nz = 2; // number of optimization variables Objective_DiodeCircuit<RealT> obj(alpha,ns,nz); // Initialize iteration vectors. Teuchos::RCP<std::vector<RealT> > z_rcp = Teuchos::rcp( new std::vector<RealT> (nz, 0.0) ); Teuchos::RCP<std::vector<RealT> > yz_rcp = Teuchos::rcp( new std::vector<RealT> (nz, 0.0) ); Teuchos::RCP<std::vector<RealT> > soln_rcp = Teuchos::rcp( new std::vector<RealT> (nz, 0.0) ); (*z_rcp)[0] = init_Is; (*z_rcp)[1] = init_Rs; (*yz_rcp)[0] = init_Is; (*yz_rcp)[1] = init_Rs; (*soln_rcp)[0] = true_Is; (*soln_rcp)[1] = true_Rs; ROL::StdVector<RealT> z(z_rcp); ROL::StdVector<RealT> yz(yz_rcp); ROL::StdVector<RealT> soln(soln_rcp); Teuchos::RCP<ROL::Vector<RealT> > zp = Teuchos::rcp(&z,false); Teuchos::RCP<ROL::Vector<RealT> > yzp = Teuchos::rcp(&yz,false); Teuchos::RCP<std::vector<RealT> > u_rcp = Teuchos::rcp( new std::vector<RealT> (ns, 0.0) ); Teuchos::RCP<std::vector<RealT> > yu_rcp = Teuchos::rcp( new std::vector<RealT> (ns, 0.0) ); std::ifstream input_file("measurements.dat"); RealT temp, temp_scale; for (int i=0; i<ns; i++) { input_file >> temp; input_file >> temp; temp_scale = pow(10,int(log10(temp))); (*u_rcp)[i] = temp_scale*(RealT)rand()/(RealT)RAND_MAX; (*yu_rcp)[i] = temp_scale*(RealT)rand()/(RealT)RAND_MAX; } input_file.close(); ROL::StdVector<RealT> u(u_rcp); ROL::StdVector<RealT> yu(yu_rcp); Teuchos::RCP<ROL::Vector<RealT> > up = Teuchos::rcp(&u,false); Teuchos::RCP<ROL::Vector<RealT> > yup = Teuchos::rcp(&yu,false); Teuchos::RCP<std::vector<RealT> > jv_rcp = Teuchos::rcp( new std::vector<RealT> (ns, 1.0) ); ROL::StdVector<RealT> jv(jv_rcp); Teuchos::RCP<ROL::Vector<RealT> > jvp = Teuchos::rcp(&jv,false); ROL::Vector_SimOpt<RealT> x(up,zp); ROL::Vector_SimOpt<RealT> y(yup,yzp); // Check derivatives obj.checkGradient(x,x,y,true,*outStream); obj.checkHessVec(x,x,y,true,*outStream); con.checkApplyJacobian(x,y,jv,true,*outStream); con.checkApplyAdjointJacobian(x,yu,jv,x,true,*outStream); con.checkApplyAdjointHessian(x,yu,y,x,true,*outStream); // Check consistency of Jacobians and adjoint Jacobians. con.checkAdjointConsistencyJacobian_1(jv,yu,u,z,true,*outStream); con.checkAdjointConsistencyJacobian_2(jv,yz,u,z,true,*outStream); // Check consistency of solves. con.checkSolve(u,z,jv,true,*outStream); con.checkInverseJacobian_1(jv,yu,u,z,true,*outStream); con.checkInverseAdjointJacobian_1(yu,jv,u,z,true,*outStream); // Initialize reduced objective function. Teuchos::RCP<std::vector<RealT> > p_rcp = Teuchos::rcp( new std::vector<RealT> (ns, 0.0) ); ROL::StdVector<RealT> p(p_rcp); Teuchos::RCP<ROL::Vector<RealT> > pp = Teuchos::rcp(&p,false); Teuchos::RCP<ROL::Objective_SimOpt<RealT> > pobj = Teuchos::rcp(&obj,false); Teuchos::RCP<ROL::EqualityConstraint_SimOpt<RealT> > pcon = Teuchos::rcp(&con,false); ROL::Reduced_Objective_SimOpt<RealT> robj(pobj,pcon,up,pp); // Check derivatives. *outStream << "Derivatives of reduced objective" << std::endl; robj.checkGradient(z,z,yz,true,*outStream); robj.checkHessVec(z,z,yz,true,*outStream); // Bound constraints RealT tol = 1.e-12; Teuchos::RCP<std::vector<RealT> > g0_rcp = Teuchos::rcp( new std::vector<RealT> (nz, 0.0) );; ROL::StdVector<RealT> g0p(g0_rcp); robj.gradient(g0p,z,tol); *outStream << std::scientific << "Initial gradient = " << (*g0_rcp)[0] << " " << (*g0_rcp)[1] << "\n"; *outStream << std::scientific << "Norm of Gradient = " << g0p.norm() << "\n"; // Define scaling for epsilon-active sets (used in inequality constraints) RealT scale; if(use_scale){ scale = 1.0e-2/g0p.norm();} else{ scale = 1.0;} *outStream << std::scientific << "Scaling: " << scale << "\n"; /// Define constraints on Is and Rs BoundConstraint_DiodeCircuit<RealT> bcon(scale,lo_Is,up_Is,lo_Rs,up_Rs); //bcon.deactivate(); // Optimization *outStream << "\n Initial guess " << (*z_rcp)[0] << " " << (*z_rcp)[1] << std::endl; if (!use_sqp){ // Trust Region ROL::Algorithm<RealT> algo_tr("Trust Region",*parlist); std::clock_t timer_tr = std::clock(); algo_tr.run(z,robj,bcon,true,*outStream); *outStream << "\n Solution " << (*z_rcp)[0] << " " << (*z_rcp)[1] << "\n" << std::endl; *outStream << "Trust-Region required " << (std::clock()-timer_tr)/(RealT)CLOCKS_PER_SEC << " seconds.\n"; } else{ // SQP. //Teuchos::RCP<std::vector<RealT> > gz_rcp = Teuchos::rcp( new std::vector<RealT> (nz, 0.0) ); //ROL::StdVector<RealT> gz(gz_rcp); //Teuchos::RCP<ROL::Vector<RealT> > gzp = Teuchos::rcp(&gz,false); Teuchos::RCP<std::vector<RealT> > gu_rcp = Teuchos::rcp( new std::vector<RealT> (ns, 0.0) ); ROL::StdVector<RealT> gu(gu_rcp); Teuchos::RCP<ROL::Vector<RealT> > gup = Teuchos::rcp(&gu,false); //ROL::Vector_SimOpt<RealT> g(gup,gzp); ROL::Vector_SimOpt<RealT> g(gup,zp); Teuchos::RCP<std::vector<RealT> > c_rcp = Teuchos::rcp( new std::vector<RealT> (ns, 0.0) ); Teuchos::RCP<std::vector<RealT> > l_rcp = Teuchos::rcp( new std::vector<RealT> (ns, 0.0) ); ROL::StdVector<RealT> c(c_rcp); ROL::StdVector<RealT> l(l_rcp); ROL::Algorithm<RealT> algo_cs("Composite Step",*parlist); //x.zero(); std::clock_t timer_cs = std::clock(); algo_cs.run(x,g,l,c,obj,con,true,*outStream); *outStream << "\n Solution " << (*z_rcp)[0] << " " << (*z_rcp)[1] << "\n" << std::endl; *outStream << "Composite Step required " << (std::clock()-timer_cs)/(RealT)CLOCKS_PER_SEC << " seconds.\n"; } soln.axpy(-1.0, z); *outStream << "Norm of error: " << soln.norm() << std::endl; if (soln.norm() > 1e4*ROL::ROL_EPSILON) { errorFlag = 1; } } catch (std::logic_error err) { *outStream << err.what() << "\n"; errorFlag = -1000; }; // end try if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n"; else std::cout << "End Result: TEST PASSED\n"; return 0; }
void testMat() { int k=FreqSys(1,1)/FreqSys(1,2); //cout<<k<<endl; math::matrix<double> yu(4,4); for (int i=0;i<4;i++) { yu(0,i)=i+1; yu(i,0)=i+1; if (i==1) { yu(1,1)=5;yu(1,i+1)=9;yu(1,i+2)=10; yu(1+i,1)=9;yu(i+2,1)=10; } yu(2,2)=22;yu(2,3)=20; yu(3,2)=20; yu(3,3)=37; } //cout<<yu<<endl; math::matrix<double> yk(4,4); math::matrix<double> ol(4,1); math::matrix<double>* yyy=new math::matrix<double>[2]; for (int i=0;i<4;i++) { for (int j=0;j<1;j++) { ol(i,j)=(i+1.2)+j*0.2; } //math::matrix<double> ols(4,1); } yyy[0]=ol; math::matrix<double>I(2,2); //cout<<yu<<endl; //cout<<InsertZeroCol(yu,0,1); for (int i=0;i<2;i++) { I(i,i)=12.0; } //cout<<Kronecker(yu,I,2); }
int main(int argc, char *argv[]) { Teuchos::GlobalMPISession mpiSession(&argc, &argv); // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. int iprint = argc - 1; Teuchos::RCP<std::ostream> outStream; Teuchos::oblackholestream bhs; // outputs nothing if (iprint > 0) outStream = Teuchos::rcp(&std::cout, false); else outStream = Teuchos::rcp(&bhs, false); int errorFlag = 0; // *** Example body. try { // Initialize full objective function. int nx = 256; // Set spatial discretization. RealT alpha = 1.e-3; // Set penalty parameter. RealT nu = 1e-2; // Viscosity parameter. Objective_BurgersControl<RealT> obj(alpha,nx); // Initialize equality constraints EqualityConstraint_BurgersControl<RealT> con(nx,nu); Teuchos::ParameterList list; list.sublist("SimOpt").sublist("Solve").set("Residual Tolerance",1.e2*ROL::ROL_EPSILON); con.setSolveParameters(list); // Initialize iteration vectors. Teuchos::RCP<std::vector<RealT> > z_rcp = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) ); Teuchos::RCP<std::vector<RealT> > gz_rcp = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) ); Teuchos::RCP<std::vector<RealT> > yz_rcp = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) ); for (int i=0; i<nx+2; i++) { (*z_rcp)[i] = (RealT)rand()/(RealT)RAND_MAX; (*yz_rcp)[i] = (RealT)rand()/(RealT)RAND_MAX; } ROL::StdVector<RealT> z(z_rcp); ROL::StdVector<RealT> gz(gz_rcp); ROL::StdVector<RealT> yz(yz_rcp); Teuchos::RCP<ROL::Vector<RealT> > zp = Teuchos::rcp(&z,false); Teuchos::RCP<ROL::Vector<RealT> > gzp = Teuchos::rcp(&z,false); Teuchos::RCP<ROL::Vector<RealT> > yzp = Teuchos::rcp(&yz,false); Teuchos::RCP<std::vector<RealT> > u_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) ); Teuchos::RCP<std::vector<RealT> > gu_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) ); Teuchos::RCP<std::vector<RealT> > yu_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) ); for (int i=0; i<nx; i++) { (*u_rcp)[i] = (RealT)rand()/(RealT)RAND_MAX; (*yu_rcp)[i] = (RealT)rand()/(RealT)RAND_MAX; } ROL::StdVector<RealT> u(u_rcp); ROL::StdVector<RealT> gu(gu_rcp); ROL::StdVector<RealT> yu(yu_rcp); Teuchos::RCP<ROL::Vector<RealT> > up = Teuchos::rcp(&u,false); Teuchos::RCP<ROL::Vector<RealT> > gup = Teuchos::rcp(&u,false); Teuchos::RCP<ROL::Vector<RealT> > yup = Teuchos::rcp(&yu,false); Teuchos::RCP<std::vector<RealT> > c_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) ); Teuchos::RCP<std::vector<RealT> > l_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) ); ROL::StdVector<RealT> c(c_rcp); ROL::StdVector<RealT> l(l_rcp); ROL::Vector_SimOpt<RealT> x(up,zp); ROL::Vector_SimOpt<RealT> g(gup,gzp); ROL::Vector_SimOpt<RealT> y(yup,yzp); // Check derivatives. obj.checkGradient(x,x,y,true,*outStream); obj.checkHessVec(x,x,y,true,*outStream); con.checkApplyJacobian(x,y,c,true,*outStream); con.checkApplyAdjointJacobian(x,yu,c,x,true,*outStream); con.checkApplyAdjointHessian(x,yu,y,x,true,*outStream); // Initialize reduced objective function. Teuchos::RCP<std::vector<RealT> > p_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) ); ROL::StdVector<RealT> p(p_rcp); Teuchos::RCP<ROL::Vector<RealT> > pp = Teuchos::rcp(&p,false); Teuchos::RCP<ROL::Objective_SimOpt<RealT> > pobj = Teuchos::rcp(&obj,false); Teuchos::RCP<ROL::EqualityConstraint_SimOpt<RealT> > pcon = Teuchos::rcp(&con,false); ROL::Reduced_Objective_SimOpt<RealT> robj(pobj,pcon,up,pp); // Check derivatives. robj.checkGradient(z,z,yz,true,*outStream); robj.checkHessVec(z,z,yz,true,*outStream); // Get parameter list. std::string filename = "input.xml"; Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); parlist->sublist("Status Test").set("Gradient Tolerance",1.e-14); parlist->sublist("Status Test").set("Constraint Tolerance",1.e-14); parlist->sublist("Status Test").set("Step Tolerance",1.e-16); parlist->sublist("Status Test").set("Iteration Limit",1000); // Declare ROL algorithm pointer. Teuchos::RCP<ROL::Algorithm<RealT> > algo; // Run optimization with Composite Step. algo = Teuchos::rcp(new ROL::Algorithm<RealT>("Composite Step",*parlist,false)); RealT zerotol = std::sqrt(ROL::ROL_EPSILON); z.zero(); con.solve(c,u,z,zerotol); c.zero(); l.zero(); algo->run(x, g, l, c, obj, con, true, *outStream); Teuchos::RCP<ROL::Vector<RealT> > zCS = z.clone(); zCS->set(z); // Run Optimization with Trust-Region algorithm. algo = Teuchos::rcp(new ROL::Algorithm<RealT>("Trust Region",*parlist,false)); z.zero(); algo->run(z,robj,true,*outStream); // Check solutions. Teuchos::RCP<ROL::Vector<RealT> > err = z.clone(); err->set(*zCS); err->axpy(-1.,z); errorFlag += ((err->norm()) > 1.e-8) ? 1 : 0; } catch (std::logic_error err) { *outStream << err.what() << "\n"; errorFlag = -1000; }; // end try if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n"; else std::cout << "End Result: TEST PASSED\n"; return 0; }