Changeset 1591 for trunk/test_more
 Timestamp:
 Nov 27, 2009 4:59:39 PM (11 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/test_more/optimize.cpp
r1590 r1591 288 288 opt += 3; 289 289 290 // communative binary operator where left is a variable 291 // and right is a parameter 292 Scalar f1 = x[5] * 5.; 293 original += 1; 294 opt += 1; 295 296 // communative binary operator where left is a variable 297 // and right is a variable 298 Scalar g1 = x[5] + x[6]; 299 original += 1; 300 opt += 1; 301 290 302 // duplicate variables 291 303 Scalar a2 = CppAD::exp(x[0]); … … 294 306 Scalar d2 = 3. / x[3]; 295 307 Scalar e2 = pow(x[3], x[4]); // counts for 3 variables 296 original += 8; 308 Scalar f2 = 5. * x[5]; 309 Scalar g2 = x[6] + x[5]; 310 original += 10; 297 311 298 312 // result vector … … 302 316 y[3] = d1 + d2; 303 317 y[4] = e1 + e2; 304 original += 5; 305 opt += 5; 318 y[5] = f1 + f2; 319 y[6] = g1 + g2; 320 original += 7; 321 opt += 7; 306 322 307 323 return; … … 316 332 317 333 // domain space vector 318 size_t n = 5;334 size_t n = 7; 319 335 CPPAD_TEST_VECTOR< AD<double> > X(n); 320 336 for(j = 0; j < n; j++) … … 359 375 } 360 376 //  377 bool case_two(void) 378 { // test that duplicate expression removal is relative to 379 // new and not just old argument indices. 380 bool ok = true; 381 using CppAD::AD; 382 size_t i, j; 383 384 // domain space vector 385 size_t n = 1; 386 CPPAD_TEST_VECTOR< AD<double> > X(n); 387 for(j = 0; j < n; j++) 388 X[j] = double(j + 2); 389 390 // range space vector 391 size_t m = 1; 392 CPPAD_TEST_VECTOR< AD<double> > Y(m); 393 394 // declare independent variables and start tape recording 395 CppAD::Independent(X); 396 397 // create a new variable 398 AD<double> A1 = X[0]  2.; 399 400 // create a duplicate variable 401 AD<double> A2 = X[0]  2.; 402 403 // create a new variable using first version of duplicate 404 AD<double> B1 = A1 / 2.; 405 406 // create a duplicate that can only be dectected using new 407 // argument indices 408 AD<double> B2 = A2 / 2.; 409 410 // Make a new variable for result 411 // and make it depend on all the variables 412 Y[0] = B1 + B2; 413 414 // create f: X > Y and stop tape recording 415 // Y[ X[0] ] = X[1] and other components of Y are zero. 416 CppAD::ADFun<double> F; 417 F.Dependent(X, Y); 418 419 // check number of variables in original function 420 ok &= (F.size_var() == 1 + n + m + 4 ); 421 422 CPPAD_TEST_VECTOR<double> x(n), y(m); 423 for(j = 0; j < n; j++) 424 x[j] = double(j + 2); 425 426 y = F.Forward(0, x); 427 for(i = 0; i < m; i++) 428 ok &= ( y[i] == Value( Y[i] ) ); 429 430 F.optimize(); 431 432 // check number of variables in optimized version 433 ok &= (F.size_var() == 1 + n + m + 2 ); 434 435 y = F.Forward(0, x); 436 for(i = 0; i < m; i++) 437 ok &= ( y[i] == Value( Y[i] ) ); 438 439 return ok; 440 } 441 //  442 bool case_three(void) 443 { // test that duplicate expression removal is relative to 444 // new and not just old argument indices (commutative case). 445 bool ok = true; 446 using CppAD::AD; 447 size_t i, j; 448 449 // domain space vector 450 size_t n = 1; 451 CPPAD_TEST_VECTOR< AD<double> > X(n); 452 for(j = 0; j < n; j++) 453 X[j] = double(j + 2); 454 455 // range space vector 456 size_t m = 1; 457 CPPAD_TEST_VECTOR< AD<double> > Y(m); 458 459 // declare independent variables and start tape recording 460 CppAD::Independent(X); 461 462 // create a new variable 463 AD<double> A1 = X[0] + 2.; 464 465 // create a duplicate variable 466 AD<double> A2 = 2. + X[0]; 467 468 // create a new variable using first version of duplicate 469 AD<double> B1 = A1 * 2.; 470 471 // create a duplicate that can only be dectected using new 472 // argument indices 473 AD<double> B2 = 2. * A2; 474 475 // Make a new variable for result 476 // and make it depend on all the variables 477 Y[0] = B1 + B2; 478 479 // create f: X > Y and stop tape recording 480 // Y[ X[0] ] = X[1] and other components of Y are zero. 481 CppAD::ADFun<double> F; 482 F.Dependent(X, Y); 483 484 // check number of variables in original function 485 ok &= (F.size_var() == 1 + n + m + 4 ); 486 487 CPPAD_TEST_VECTOR<double> x(n), y(m); 488 for(j = 0; j < n; j++) 489 x[j] = double(j + 2); 490 491 y = F.Forward(0, x); 492 for(i = 0; i < m; i++) 493 ok &= ( y[i] == Value( Y[i] ) ); 494 495 F.optimize(); 496 497 // check number of variables in optimized version 498 ok &= (F.size_var() == 1 + n + m + 2 ); 499 500 y = F.Forward(0, x); 501 for(i = 0; i < m; i++) 502 ok &= ( y[i] == Value( Y[i] ) ); 503 504 return ok; 505 } 361 506 } 362 507 363 508 bool optimize(void) 364 509 { bool ok = true; 510 // check reverse dependency analysis optimization 365 511 ok &= CaseOne(); 366 512 ok &= CaseTwo(); 367 513 ok &= CaseThree(); 368 369 //  514 // check removal of duplicate expressions 370 515 ok &= case_one(); 516 ok &= case_two(); 517 ok &= case_three(); 371 518 return ok; 372 519 }
Note: See TracChangeset
for help on using the changeset viewer.