File size: 27,683 Bytes
079c32c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 |
import pytest
from itertools import product
import time
import os
from copy import deepcopy
from ding.entry import serial_pipeline, collect_demo_data, serial_pipeline_offline
from dizoo.classic_control.cartpole.config.cartpole_dqn_config import cartpole_dqn_config, cartpole_dqn_create_config
from dizoo.classic_control.cartpole.config.cartpole_dqn_stdim_config import cartpole_dqn_stdim_config, \
cartpole_dqn_stdim_create_config
from dizoo.classic_control.cartpole.config.cartpole_ppo_config import cartpole_ppo_config, cartpole_ppo_create_config
from dizoo.classic_control.cartpole.config.cartpole_ppo_offpolicy_config import cartpole_ppo_offpolicy_config, \
cartpole_ppo_offpolicy_create_config
from dizoo.classic_control.cartpole.config.cartpole_impala_config import cartpole_impala_config, cartpole_impala_create_config # noqa
from dizoo.classic_control.cartpole.config.cartpole_rainbow_config import cartpole_rainbow_config, cartpole_rainbow_create_config # noqa
from dizoo.classic_control.cartpole.config.cartpole_iqn_config import cartpole_iqn_config, cartpole_iqn_create_config # noqa
from dizoo.classic_control.cartpole.config.cartpole_fqf_config import cartpole_fqf_config, cartpole_fqf_create_config # noqa
from dizoo.classic_control.cartpole.config.cartpole_c51_config import cartpole_c51_config, cartpole_c51_create_config # noqa
from dizoo.classic_control.cartpole.config.cartpole_qrdqn_config import cartpole_qrdqn_config, cartpole_qrdqn_create_config # noqa
from dizoo.classic_control.cartpole.config.cartpole_sqn_config import cartpole_sqn_config, cartpole_sqn_create_config # noqa
from dizoo.classic_control.cartpole.config.cartpole_ppg_config import cartpole_ppg_config, cartpole_ppg_create_config # noqa
from dizoo.classic_control.cartpole.config.cartpole_acer_config import cartpole_acer_config, cartpole_acer_create_config # noqa
from dizoo.classic_control.cartpole.config.cartpole_sac_config import cartpole_sac_config, cartpole_sac_create_config # noqa
from dizoo.classic_control.cartpole.entry.cartpole_ppg_main import main as ppg_main
from dizoo.classic_control.cartpole.entry.cartpole_ppo_main import main as ppo_main
from dizoo.classic_control.cartpole.config.cartpole_r2d2_config import cartpole_r2d2_config, cartpole_r2d2_create_config # noqa
from dizoo.classic_control.pendulum.config import pendulum_ddpg_config, pendulum_ddpg_create_config
from dizoo.classic_control.pendulum.config import pendulum_td3_config, pendulum_td3_create_config
from dizoo.classic_control.pendulum.config import pendulum_sac_config, pendulum_sac_create_config
from dizoo.classic_control.pendulum.config import pendulum_d4pg_config, pendulum_d4pg_create_config
from dizoo.bitflip.config import bitflip_her_dqn_config, bitflip_her_dqn_create_config
from dizoo.bitflip.entry.bitflip_dqn_main import main as bitflip_dqn_main
from dizoo.petting_zoo.config import ptz_simple_spread_atoc_config, ptz_simple_spread_atoc_create_config # noqa
from dizoo.petting_zoo.config import ptz_simple_spread_collaq_config, ptz_simple_spread_collaq_create_config # noqa
from dizoo.petting_zoo.config import ptz_simple_spread_coma_config, ptz_simple_spread_coma_create_config # noqa
from dizoo.petting_zoo.config import ptz_simple_spread_qmix_config, ptz_simple_spread_qmix_create_config # noqa
from dizoo.petting_zoo.config import ptz_simple_spread_qtran_config, ptz_simple_spread_qtran_create_config # noqa
from dizoo.petting_zoo.config import ptz_simple_spread_vdn_config, ptz_simple_spread_vdn_create_config # noqa
from dizoo.petting_zoo.config import ptz_simple_spread_wqmix_config, ptz_simple_spread_wqmix_create_config # noqa
from dizoo.petting_zoo.config import ptz_simple_spread_madqn_config, ptz_simple_spread_madqn_create_config # noqa
from dizoo.league_demo.league_demo_ppo_config import league_demo_ppo_config
from dizoo.league_demo.selfplay_demo_ppo_main import main as selfplay_main
from dizoo.league_demo.league_demo_ppo_main import main as league_main
from dizoo.classic_control.pendulum.config.pendulum_sac_data_generation_config import pendulum_sac_data_genearation_config, pendulum_sac_data_genearation_create_config # noqa
from dizoo.classic_control.pendulum.config.pendulum_cql_config import pendulum_cql_config, pendulum_cql_create_config # noqa
from dizoo.classic_control.cartpole.config.cartpole_qrdqn_generation_data_config import cartpole_qrdqn_generation_data_config, cartpole_qrdqn_generation_data_create_config # noqa
from dizoo.classic_control.cartpole.config.cartpole_cql_config import cartpole_discrete_cql_config, cartpole_discrete_cql_create_config # noqa
from dizoo.classic_control.cartpole.config.cartpole_dt_config import cartpole_discrete_dt_config, cartpole_discrete_dt_create_config # noqa
from dizoo.classic_control.pendulum.config.pendulum_td3_data_generation_config import pendulum_td3_generation_config, pendulum_td3_generation_create_config # noqa
from dizoo.classic_control.pendulum.config.pendulum_td3_bc_config import pendulum_td3_bc_config, pendulum_td3_bc_create_config # noqa
from dizoo.classic_control.pendulum.config.pendulum_ibc_config import pendulum_ibc_config, pendulum_ibc_create_config
from dizoo.gym_hybrid.config.gym_hybrid_ddpg_config import gym_hybrid_ddpg_config, gym_hybrid_ddpg_create_config
from dizoo.gym_hybrid.config.gym_hybrid_pdqn_config import gym_hybrid_pdqn_config, gym_hybrid_pdqn_create_config
from dizoo.gym_hybrid.config.gym_hybrid_mpdqn_config import gym_hybrid_mpdqn_config, gym_hybrid_mpdqn_create_config
from dizoo.classic_control.pendulum.config.pendulum_bdq_config import pendulum_bdq_config, pendulum_bdq_create_config # noqa
from dizoo.classic_control.cartpole.config.cartpole_mdqn_config import cartpole_mdqn_config, cartpole_mdqn_create_config
@pytest.mark.platformtest
@pytest.mark.unittest
def test_dqn():
config = [deepcopy(cartpole_dqn_config), deepcopy(cartpole_dqn_create_config)]
config[0].policy.learn.update_per_collect = 1
config[0].exp_name = 'cartpole_dqn_unittest'
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
finally:
os.popen('rm -rf cartpole_dqn_unittest')
@pytest.mark.platformtest
@pytest.mark.unittest
def test_mdqn():
config = [deepcopy(cartpole_mdqn_config), deepcopy(cartpole_mdqn_create_config)]
config[0].policy.learn.update_per_collect = 1
config[0].exp_name = 'cartpole_mdqn_unittest'
try:
serial_pipeline(config, seed=0, max_train_iter=1, dynamic_seed=False)
except Exception:
assert False, "pipeline fail"
finally:
os.popen('rm -rf cartpole_mdqn_unittest')
@pytest.mark.platformtest
@pytest.mark.unittest
def test_bdq():
config = [deepcopy(pendulum_bdq_config), deepcopy(pendulum_bdq_create_config)]
config[0].policy.learn.update_per_collect = 1
config[0].exp_name = 'pendulum_bdq_unittest'
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
finally:
os.popen('rm -rf pendulum_bdq_unittest')
@pytest.mark.platformtest
@pytest.mark.unittest
def test_ddpg():
config = [deepcopy(pendulum_ddpg_config), deepcopy(pendulum_ddpg_create_config)]
config[0].policy.learn.update_per_collect = 1
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
# @pytest.mark.platformtest
# @pytest.mark.unittest
def test_hybrid_ddpg():
config = [deepcopy(gym_hybrid_ddpg_config), deepcopy(gym_hybrid_ddpg_create_config)]
config[0].policy.learn.update_per_collect = 1
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
# @pytest.mark.platformtest
# @pytest.mark.unittest
def test_hybrid_pdqn():
config = [deepcopy(gym_hybrid_pdqn_config), deepcopy(gym_hybrid_pdqn_create_config)]
config[0].policy.learn.update_per_collect = 1
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
# @pytest.mark.platformtest
# @pytest.mark.unittest
def test_hybrid_mpdqn():
config = [deepcopy(gym_hybrid_mpdqn_config), deepcopy(gym_hybrid_mpdqn_create_config)]
config[0].policy.learn.update_per_collect = 1
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_dqn_stdim():
config = [deepcopy(cartpole_dqn_stdim_config), deepcopy(cartpole_dqn_stdim_create_config)]
config[0].policy.learn.update_per_collect = 1
config[0].exp_name = 'cartpole_dqn_stdim_unittest'
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
finally:
os.popen('rm -rf cartpole_dqn_stdim_unittest')
@pytest.mark.platformtest
@pytest.mark.unittest
def test_td3():
config = [deepcopy(pendulum_td3_config), deepcopy(pendulum_td3_create_config)]
config[0].policy.learn.update_per_collect = 1
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_rainbow():
config = [deepcopy(cartpole_rainbow_config), deepcopy(cartpole_rainbow_create_config)]
config[0].policy.learn.update_per_collect = 1
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_iqn():
config = [deepcopy(cartpole_iqn_config), deepcopy(cartpole_iqn_create_config)]
config[0].policy.learn.update_per_collect = 1
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_fqf():
config = [deepcopy(cartpole_fqf_config), deepcopy(cartpole_fqf_create_config)]
config[0].policy.learn.update_per_collect = 1
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_c51():
config = [deepcopy(cartpole_c51_config), deepcopy(cartpole_c51_create_config)]
config[0].policy.learn.update_per_collect = 1
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_qrdqn():
config = [deepcopy(cartpole_qrdqn_config), deepcopy(cartpole_qrdqn_create_config)]
config[0].policy.learn.update_per_collect = 1
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_ppo():
config = [deepcopy(cartpole_ppo_offpolicy_config), deepcopy(cartpole_ppo_offpolicy_create_config)]
config[0].policy.learn.update_per_collect = 1
config[0].exp_name = 'ppo_offpolicy_unittest'
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_ppo_nstep_return():
config = [deepcopy(cartpole_ppo_offpolicy_config), deepcopy(cartpole_ppo_offpolicy_create_config)]
config[0].policy.learn.update_per_collect = 1
config[0].policy.nstep_return = True
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_sac():
config = [deepcopy(pendulum_sac_config), deepcopy(pendulum_sac_create_config)]
config[0].policy.learn.update_per_collect = 1
config[0].policy.learn.auto_alpha = False
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_sac_auto_alpha():
config = [deepcopy(pendulum_sac_config), deepcopy(pendulum_sac_create_config)]
config[0].policy.learn.update_per_collect = 1
config[0].policy.learn.auto_alpha = True
config[0].policy.learn.log_space = False
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_sac_log_space():
config = [deepcopy(pendulum_sac_config), deepcopy(pendulum_sac_create_config)]
config[0].policy.learn.update_per_collect = 1
config[0].policy.learn.auto_alpha = True
config[0].policy.learn.log_space = True
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_discrete_sac():
auto_alpha, log_space = True, False
config = [deepcopy(cartpole_sac_config), deepcopy(cartpole_sac_create_config)]
config[0].policy.learn.update_per_collect = 1
config[0].policy.learn.auto_alpha = auto_alpha
config[0].policy.learn.log_space = log_space
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_discrete_sac_twin_critic():
config = [deepcopy(cartpole_sac_config), deepcopy(cartpole_sac_create_config)]
config[0].cuda = True
config[0].policy.learn.update_per_collect = 1
config[0].policy.learn.auto_alpha = True
config[0].policy.learn.log_space = True
config[0].policy.model.twin_critic = False
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_r2d2():
config = [deepcopy(cartpole_r2d2_config), deepcopy(cartpole_r2d2_create_config)]
config[0].policy.learn.update_per_collect = 1
try:
serial_pipeline(config, seed=0, max_train_iter=5)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_impala():
config = [deepcopy(cartpole_impala_config), deepcopy(cartpole_impala_create_config)]
config[0].policy.learn.update_per_collect = 1
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_her_dqn():
bitflip_her_dqn_config.policy.cuda = False
try:
bitflip_dqn_main(bitflip_her_dqn_config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_collaq():
config = [deepcopy(ptz_simple_spread_collaq_config), deepcopy(ptz_simple_spread_collaq_create_config)]
config[0].policy.cuda = False
config[0].policy.learn.update_per_collect = 1
config[0].policy.collect.n_sample = 100
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
finally:
os.popen('rm -rf log ckpt*')
@pytest.mark.platformtest
@pytest.mark.unittest
def test_coma():
config = [deepcopy(ptz_simple_spread_coma_config), deepcopy(ptz_simple_spread_coma_create_config)]
config[0].policy.cuda = False
config[0].policy.learn.update_per_collect = 1
config[0].policy.collect.n_sample = 100
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
finally:
os.popen('rm -rf log ckpt*')
@pytest.mark.platformtest
@pytest.mark.unittest
def test_qmix():
config = [deepcopy(ptz_simple_spread_qmix_config), deepcopy(ptz_simple_spread_qmix_create_config)]
config[0].policy.cuda = False
config[0].policy.learn.update_per_collect = 1
config[0].policy.collect.n_sample = 100
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
finally:
os.popen('rm -rf log ckpt*')
@pytest.mark.platformtest
@pytest.mark.unittest
def test_wqmix():
config = [deepcopy(ptz_simple_spread_wqmix_config), deepcopy(ptz_simple_spread_wqmix_create_config)]
config[0].policy.cuda = False
config[0].policy.learn.update_per_collect = 1
config[0].policy.collect.n_sample = 100
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
finally:
os.popen('rm -rf log ckpt*')
@pytest.mark.platformtest
@pytest.mark.unittest
def test_madqn():
config = [deepcopy(ptz_simple_spread_madqn_config), deepcopy(ptz_simple_spread_madqn_create_config)]
config[0].policy.cuda = False
config[0].policy.learn.update_per_collect = 1
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
finally:
os.popen('rm -rf log ckpt*')
@pytest.mark.platformtest
@pytest.mark.unittest
def test_qtran():
config = [deepcopy(ptz_simple_spread_qtran_config), deepcopy(ptz_simple_spread_qtran_create_config)]
config[0].policy.cuda = False
config[0].policy.learn.update_per_collect = 1
config[0].policy.collect.n_sample = 100
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
finally:
os.popen('rm -rf log ckpt*')
@pytest.mark.platformtest
@pytest.mark.unittest
def test_atoc():
config = [deepcopy(ptz_simple_spread_atoc_config), deepcopy(ptz_simple_spread_atoc_create_config)]
config[0].policy.cuda = False
config[0].policy.collect.n_sample = 100
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
finally:
os.popen('rm -rf log ckpt*')
@pytest.mark.platformtest
@pytest.mark.unittest
def test_ppg():
cartpole_ppg_config.policy.use_cuda = False
try:
ppg_main(cartpole_ppg_config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_sqn():
config = [deepcopy(cartpole_sqn_config), deepcopy(cartpole_sqn_create_config)]
config[0].policy.learn.update_per_collect = 8
config[0].policy.learn.batch_size = 8
try:
serial_pipeline(config, seed=0, max_train_iter=2)
except Exception:
assert False, "pipeline fail"
finally:
os.popen('rm -rf log ckpt*')
@pytest.mark.platformtest
@pytest.mark.unittest
def test_selfplay():
try:
selfplay_main(deepcopy(league_demo_ppo_config), seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_league():
try:
league_main(deepcopy(league_demo_ppo_config), seed=0, max_train_iter=1)
except Exception as e:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_acer():
config = [deepcopy(cartpole_acer_config), deepcopy(cartpole_acer_create_config)]
config[0].policy.learn.update_per_collect = 1
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_cql():
# train expert
config = [deepcopy(pendulum_sac_config), deepcopy(pendulum_sac_create_config)]
config[0].policy.learn.update_per_collect = 1
config[0].exp_name = 'sac_unittest'
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
# collect expert data
import torch
config = [deepcopy(pendulum_sac_data_genearation_config), deepcopy(pendulum_sac_data_genearation_create_config)]
collect_count = 1000
expert_data_path = config[0].policy.collect.save_path
state_dict = torch.load('./sac_unittest/ckpt/iteration_0.pth.tar', map_location='cpu')
try:
collect_demo_data(
config, seed=0, collect_count=collect_count, expert_data_path=expert_data_path, state_dict=state_dict
)
except Exception:
assert False, "pipeline fail"
# test cql
config = [deepcopy(pendulum_cql_config), deepcopy(pendulum_cql_create_config)]
config[0].policy.learn.train_epoch = 1
config[0].policy.eval.evaluator.eval_freq = 1
try:
serial_pipeline_offline(config, seed=0)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_ibc():
# train expert
config = [deepcopy(pendulum_sac_config), deepcopy(pendulum_sac_create_config)]
config[0].policy.learn.update_per_collect = 1
config[0].exp_name = 'sac_unittest'
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
# collect expert data
import torch
config = [deepcopy(pendulum_sac_data_genearation_config), deepcopy(pendulum_sac_data_genearation_create_config)]
collect_count = 1000
expert_data_path = config[0].policy.collect.save_path
state_dict = torch.load('./sac_unittest/ckpt/iteration_0.pth.tar', map_location='cpu')
try:
collect_demo_data(
config, seed=0, collect_count=collect_count, expert_data_path=expert_data_path, state_dict=state_dict
)
except Exception:
assert False, "pipeline fail"
# test cql
config = [deepcopy(pendulum_ibc_config), deepcopy(pendulum_ibc_create_config)]
config[0].policy.learn.train_epoch = 1
config[0].policy.eval.evaluator.eval_freq = 1
config[0].policy.model.stochastic_optim.iters = 2
try:
serial_pipeline_offline(config, seed=0)
except Exception:
assert False, "pipeline fail"
@pytest.mark.platformtest
@pytest.mark.unittest
def test_d4pg():
config = [deepcopy(pendulum_d4pg_config), deepcopy(pendulum_d4pg_create_config)]
config[0].policy.learn.update_per_collect = 1
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception as e:
assert False, "pipeline fail"
print(repr(e))
@pytest.mark.platformtest
@pytest.mark.unittest
def test_discrete_cql():
# train expert
config = [deepcopy(cartpole_qrdqn_config), deepcopy(cartpole_qrdqn_create_config)]
config[0].policy.learn.update_per_collect = 1
config[0].exp_name = 'cql_cartpole'
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
# collect expert data
import torch
config = [deepcopy(cartpole_qrdqn_generation_data_config), deepcopy(cartpole_qrdqn_generation_data_create_config)]
state_dict = torch.load('./cql_cartpole/ckpt/iteration_0.pth.tar', map_location='cpu')
try:
collect_demo_data(config, seed=0, collect_count=1000, state_dict=state_dict)
except Exception as e:
assert False, "pipeline fail"
print(repr(e))
# train cql
config = [deepcopy(cartpole_discrete_cql_config), deepcopy(cartpole_discrete_cql_create_config)]
config[0].policy.learn.train_epoch = 1
config[0].policy.eval.evaluator.eval_freq = 1
try:
serial_pipeline_offline(config, seed=0)
except Exception:
assert False, "pipeline fail"
finally:
os.popen('rm -rf cartpole cartpole_cql')
@pytest.mark.platformtest
@pytest.mark.unittest
def test_discrete_dt():
# train expert
config = [deepcopy(cartpole_qrdqn_config), deepcopy(cartpole_qrdqn_create_config)]
config[0].policy.learn.update_per_collect = 1
config[0].exp_name = 'dt_cartpole'
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
# collect expert data
import torch
config = [deepcopy(cartpole_qrdqn_generation_data_config), deepcopy(cartpole_qrdqn_generation_data_create_config)]
state_dict = torch.load('./dt_cartpole/ckpt/iteration_0.pth.tar', map_location='cpu')
try:
collect_demo_data(config, seed=0, collect_count=1000, state_dict=state_dict)
except Exception as e:
assert False, "pipeline fail"
print(repr(e))
# train dt
config = [deepcopy(cartpole_discrete_dt_config), deepcopy(cartpole_discrete_dt_create_config)]
config[0].policy.eval.evaluator.eval_freq = 5
try:
from ding.framework import task, ding_init
from ding.framework.context import OfflineRLContext
from ding.envs import SubprocessEnvManagerV2, BaseEnvManagerV2
from ding.envs.env_wrappers.env_wrappers import AllinObsWrapper
from dizoo.classic_control.cartpole.envs import CartPoleEnv
from ding.utils import set_pkg_seed
from ding.data import create_dataset
from ding.config import compile_config
from ding.model import DecisionTransformer
from ding.policy import DTPolicy
from ding.framework.middleware import interaction_evaluator, trainer, CkptSaver, \
OfflineMemoryDataFetcher, offline_logger, termination_checker
ding_init(config[0])
config = compile_config(config[0], create_cfg=config[1], auto=True)
with task.start(async_mode=False, ctx=OfflineRLContext()):
evaluator_env = BaseEnvManagerV2(
env_fn=[lambda: AllinObsWrapper(CartPoleEnv(config.env)) for _ in range(config.env.evaluator_env_num)],
cfg=config.env.manager
)
set_pkg_seed(config.seed, use_cuda=config.policy.cuda)
dataset = create_dataset(config)
model = DecisionTransformer(**config.policy.model)
policy = DTPolicy(config.policy, model=model)
task.use(termination_checker(max_train_iter=1))
task.use(interaction_evaluator(config, policy.eval_mode, evaluator_env))
task.use(OfflineMemoryDataFetcher(config, dataset))
task.use(trainer(config, policy.learn_mode))
task.use(CkptSaver(policy, config.exp_name, train_freq=100))
task.use(offline_logger())
task.run()
except Exception:
assert False, "pipeline fail"
finally:
os.popen('rm -rf cartpole cartpole_dt')
@pytest.mark.platformtest
@pytest.mark.unittest
def test_td3_bc():
# train expert
config = [deepcopy(pendulum_td3_config), deepcopy(pendulum_td3_create_config)]
config[0].exp_name = 'td3'
config[0].policy.learn.update_per_collect = 1
try:
serial_pipeline(config, seed=0, max_train_iter=1)
except Exception:
assert False, "pipeline fail"
# collect expert data
import torch
config = [deepcopy(pendulum_td3_generation_config), deepcopy(pendulum_td3_generation_create_config)]
state_dict = torch.load('./td3/ckpt/iteration_0.pth.tar', map_location='cpu')
try:
collect_demo_data(config, seed=0, collect_count=1000, state_dict=state_dict)
except Exception:
assert False, "pipeline fail"
# train td3 bc
config = [deepcopy(pendulum_td3_bc_config), deepcopy(pendulum_td3_bc_create_config)]
config[0].exp_name = 'td3_bc'
config[0].policy.learn.train_epoch = 1
config[0].policy.eval.evaluator.eval_freq = 1
try:
serial_pipeline_offline(config, seed=0)
except Exception:
assert False, "pipeline fail"
finally:
os.popen('rm -rf td3 td3_bc')
|