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 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
|
/* Copyright 2017-2021 PaGMO development team
This file is part of the PaGMO library.
The PaGMO library is free software; you can redistribute it and/or modify
it under the terms of either:
* the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your
option) any later version.
or
* the GNU General Public License as published by the Free Software
Foundation; either version 3 of the License, or (at your option) any
later version.
or both in parallel, as here.
The PaGMO library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received copies of the GNU General Public License and the
GNU Lesser General Public License along with the PaGMO library. If not,
see https://www.gnu.org/licenses/. */
#define BOOST_TEST_MODULE problem_test
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>
#include <initializer_list>
#include <limits>
#include <sstream>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <typeindex>
#include <typeinfo>
#include <utility>
#include <vector>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/lexical_cast.hpp>
#include <pagmo/detail/type_name.hpp>
#include <pagmo/exceptions.hpp>
#include <pagmo/problem.hpp>
#include <pagmo/problems/null_problem.hpp>
#include <pagmo/s11n.hpp>
#include <pagmo/threading.hpp>
#include <pagmo/types.hpp>
using namespace pagmo;
// Generates a dummy simple problem with arbitrary dimensions and return values
struct base_p {
base_p(vector_double::size_type nobj = 1u, vector_double::size_type nec = 0u, vector_double::size_type nic = 0u,
const vector_double &ret_fit = {1.}, const vector_double &lb = {0.}, const vector_double &ub = {1.})
: m_nobj(nobj), m_nec(nec), m_nic(nic), m_ret_fit(ret_fit), m_lb(lb), m_ub(ub)
{
}
vector_double fitness(const vector_double &) const
{
return m_ret_fit;
}
vector_double::size_type get_nobj() const
{
return m_nobj;
}
vector_double::size_type get_nec() const
{
return m_nec;
}
vector_double::size_type get_nic() const
{
return m_nic;
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {m_lb, m_ub};
}
std::string get_name() const
{
return "A base toy problem";
}
std::string get_extra_info() const
{
return "Nothing to report";
}
template <typename Archive>
void serialize(Archive &ar, unsigned)
{
detail::archive(ar, m_nobj, m_nec, m_nic, m_ret_fit, m_lb, m_ub);
}
vector_double::size_type m_nobj;
vector_double::size_type m_nec;
vector_double::size_type m_nic;
vector_double m_ret_fit;
vector_double m_lb;
vector_double m_ub;
};
// Generates a dummy problem with arbitrary dimensions and return values
// having the gradient implemented
struct grad_p : base_p {
grad_p(vector_double::size_type nobj = 1u, vector_double::size_type nec = 0u, vector_double::size_type nic = 0u,
const vector_double &ret_fit = {1}, const vector_double &lb = {0}, const vector_double &ub = {1},
const vector_double &g = {1}, const sparsity_pattern &gs = {{0, 0}})
: base_p(nobj, nec, nic, ret_fit, lb, ub), m_g(g), m_gs(gs)
{
}
vector_double gradient(const vector_double &) const
{
return m_g;
}
sparsity_pattern gradient_sparsity() const
{
return m_gs;
}
template <typename Archive>
void serialize(Archive &ar, unsigned)
{
detail::archive(ar, boost::serialization::base_object<base_p>(*this), m_g, m_gs);
}
vector_double m_g;
sparsity_pattern m_gs;
};
PAGMO_S11N_PROBLEM_EXPORT(grad_p)
// Generates a dummy problem with arbitrary dimensions and return values
// having the gradient implemented but overriding the has methods
struct grad_p_override : grad_p {
grad_p_override(vector_double::size_type nobj = 1u, vector_double::size_type nec = 0u,
vector_double::size_type nic = 0u, const vector_double &ret_fit = {1},
const vector_double &lb = {0}, const vector_double &ub = {1}, const vector_double &g = {1},
const sparsity_pattern &gs = {{0, 0}})
: grad_p(nobj, nec, nic, ret_fit, lb, ub, g, gs)
{
}
bool has_gradient() const
{
return false;
}
bool has_gradient_sparsity() const
{
return false;
}
template <typename Archive>
void serialize(Archive &ar, unsigned)
{
ar &boost::serialization::base_object<grad_p>(*this);
}
};
PAGMO_S11N_PROBLEM_EXPORT(grad_p_override)
// Generates a dummy problem with arbitrary dimensions and return values
// having the hessians implemented
struct hess_p : base_p {
hess_p(vector_double::size_type nobj = 1u, vector_double::size_type nec = 0u, vector_double::size_type nic = 0u,
const vector_double &ret_fit = {1}, const vector_double &lb = {0}, const vector_double &ub = {1},
const std::vector<vector_double> &h = {{1}}, const std::vector<sparsity_pattern> &hs = {{{0, 0}}})
: base_p(nobj, nec, nic, ret_fit, lb, ub), m_h(h), m_hs(hs)
{
}
std::vector<vector_double> hessians(const vector_double &) const
{
return m_h;
}
std::vector<sparsity_pattern> hessians_sparsity() const
{
return m_hs;
}
template <typename Archive>
void serialize(Archive &ar, unsigned)
{
detail::archive(ar, boost::serialization::base_object<base_p>(*this), m_h, m_hs);
}
std::vector<vector_double> m_h;
std::vector<sparsity_pattern> m_hs;
};
PAGMO_S11N_PROBLEM_EXPORT(hess_p)
// Generates a dummy problem with arbitrary dimensions and return values
// having the hessians implemented but overriding the has methods
struct hess_p_override : hess_p {
hess_p_override(vector_double::size_type nobj = 1u, vector_double::size_type nec = 0u,
vector_double::size_type nic = 0u, const vector_double &ret_fit = {1},
const vector_double &lb = {0}, const vector_double &ub = {1},
const std::vector<vector_double> &h = {{1}}, const std::vector<sparsity_pattern> &hs = {{{0, 0}}})
: hess_p(nobj, nec, nic, ret_fit, lb, ub, h, hs)
{
}
bool has_hessians() const
{
return false;
}
bool has_hessians_sparsity() const
{
return false;
}
template <typename Archive>
void serialize(Archive &ar, unsigned)
{
ar &boost::serialization::base_object<hess_p>(*this);
}
};
PAGMO_S11N_PROBLEM_EXPORT(hess_p_override)
// Generates a dummy problem with arbitrary dimensions and return values
// having the hessians and the gradients implemented
struct full_p : grad_p {
full_p(vector_double::size_type nobj = 1u, vector_double::size_type nec = 0u, vector_double::size_type nic = 0u,
const vector_double &ret_fit = {1}, const vector_double &lb = {0}, const vector_double &ub = {1},
const vector_double &g = {1}, const sparsity_pattern &gs = {{0, 0}},
const std::vector<vector_double> &h = {{1}}, const std::vector<sparsity_pattern> &hs = {{{0, 0}}})
: grad_p(nobj, nec, nic, ret_fit, lb, ub, g, gs), m_h(h), m_hs(hs)
{
}
std::vector<vector_double> hessians(const vector_double &) const
{
return m_h;
}
std::vector<sparsity_pattern> hessians_sparsity() const
{
return m_hs;
}
template <typename Archive>
void serialize(Archive &ar, unsigned)
{
detail::archive(ar, boost::serialization::base_object<grad_p>(*this), m_h, m_hs);
}
std::vector<vector_double> m_h;
std::vector<sparsity_pattern> m_hs;
};
PAGMO_S11N_PROBLEM_EXPORT(full_p)
struct empty {
vector_double fitness(const vector_double &) const
{
return {1};
}
vector_double::size_type get_nec() const
{
return 0;
}
vector_double::size_type get_nic() const
{
return 0;
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
};
BOOST_AUTO_TEST_CASE(problem_construction_test)
{
// We check that problems with inconsistent dimensions throw
// std::invalid argument
vector_double lb_2(2, 0);
vector_double ub_2(2, 1);
vector_double lb_3(3, 1);
vector_double ub_3(3, 1);
vector_double fit_1(1, 1);
vector_double fit_2(2, 1);
vector_double fit_12(12, 11);
vector_double lb_11(11, 0);
vector_double ub_11(11, 0);
vector_double grad_2{1, 1};
sparsity_pattern grads_2_outofbounds{{0, 0}, {3, 4}};
sparsity_pattern grads_2_repeats{{0, 0}, {0, 0}};
sparsity_pattern grads_2_correct{{0, 0}, {0, 1}};
std::vector<vector_double> hess_22{{1, 1}, {1, 1}};
std::vector<sparsity_pattern> hesss_22_outofbounds{{{0, 0}, {12, 13}}, {{0, 0}, {1, 0}}};
std::vector<sparsity_pattern> hesss_22_notlowertriangular{{{0, 0}, {0, 1}}, {{0, 0}, {1, 0}}};
std::vector<sparsity_pattern> hesss_22_repeated{{{0, 0}, {0, 0}}, {{0, 0}, {1, 0}}};
std::vector<sparsity_pattern> hesss_22_correct{{{0, 0}, {1, 0}}, {{0, 0}, {1, 0}}};
// 0 - lb size is zero
BOOST_CHECK_THROW(problem{base_p(1, 0, 0, fit_1, {}, {})}, std::invalid_argument);
// 1 - lb > ub
BOOST_CHECK_THROW(problem{base_p(1, 0, 0, fit_1, ub_2, lb_2)}, std::invalid_argument);
// 2 - lb length is wrong
BOOST_CHECK_THROW(problem{base_p(1, 0, 0, fit_1, lb_3, ub_2)}, std::invalid_argument);
// 3 - ub length is wrong
BOOST_CHECK_THROW(problem{base_p(1, 0, 0, fit_1, lb_2, ub_3)}, std::invalid_argument);
// 4 - gradient sparsity has index out of bounds
BOOST_CHECK_THROW(problem{grad_p(1, 0, 0, fit_1, lb_2, ub_2, grad_2, grads_2_outofbounds)}, std::invalid_argument);
// 5 - gradient sparsity has a repeating pair
BOOST_CHECK_THROW(problem{grad_p(1, 0, 0, fit_1, lb_2, ub_2, grad_2, grads_2_repeats)}, std::invalid_argument);
// 6 - hessian sparsity has index out of bounds
BOOST_CHECK_THROW(problem{hess_p(1, 1, 0, fit_2, lb_2, ub_2, hess_22, hesss_22_outofbounds)},
std::invalid_argument);
// 7 - hessian sparsity is not lower triangular
BOOST_CHECK_THROW(problem{hess_p(1, 1, 0, fit_2, lb_2, ub_2, hess_22, hesss_22_notlowertriangular)},
std::invalid_argument);
// 8 - hessian sparsity has repeated indexes
BOOST_CHECK_THROW(problem{hess_p(1, 1, 0, fit_2, lb_2, ub_2, hess_22, hesss_22_repeated)}, std::invalid_argument);
// 9 - hessian sparsity has the wrong length
BOOST_CHECK_THROW(
problem{hess_p(1, 1, 0, fit_2, lb_2, ub_2, hess_22, {{{0, 0}, {1, 0}}, {{0, 0}, {1, 0}}, {{0, 0}}})},
std::invalid_argument);
// 10 - 0 objectives
BOOST_CHECK_THROW(problem{base_p(0, 0, 0, fit_1, {1}, {2})}, std::invalid_argument);
// 11 - many objectives
BOOST_CHECK_THROW(problem{base_p(std::numeric_limits<vector_double::size_type>::max(), 0, 0, fit_2, {1}, {2})},
std::invalid_argument);
// 12 - too many equalities
BOOST_CHECK_THROW(problem{base_p(1, std::numeric_limits<vector_double::size_type>::max(), 0, fit_2, {1}, {2})},
std::invalid_argument);
// 13 - too many inequalities
BOOST_CHECK_THROW(problem{base_p(1, 0, std::numeric_limits<vector_double::size_type>::max(), fit_2, {1}, {2})},
std::invalid_argument);
// We check that the data members are initialized correctly (i.e. counters to zero
// and gradient / hessian dimensions to the right values
{
problem p1{base_p(2, 0, 0, fit_2, lb_2, ub_2)};
problem p2{base_p(3, 4, 5, fit_12, lb_11, ub_11)};
problem p3{grad_p(1, 0, 0, fit_2, lb_2, ub_2, grad_2, grads_2_correct)};
problem p4{hess_p(1, 1, 0, fit_2, lb_2, ub_2, hess_22, hesss_22_correct)};
BOOST_CHECK(p1.get_fevals() == 0u);
BOOST_CHECK(p1.get_gevals() == 0u);
BOOST_CHECK(p1.get_hevals() == 0u);
BOOST_CHECK(p1.get_hevals() == 0u);
}
// We check the move constructor
{
problem p1{full_p(2, 0, 0, fit_2, lb_2, ub_2, grad_2, grads_2_correct, hess_22, hesss_22_correct)};
// We increment the counters so that the default values are changed
p1.fitness({1, 1});
p1.gradient({1, 1});
p1.hessians({1, 1});
auto p1_string = boost::lexical_cast<std::string>(p1);
auto a1 = p1.extract<full_p>();
problem p2(std::move(p1));
auto a2 = p2.extract<full_p>();
auto p2_string = boost::lexical_cast<std::string>(p2);
// 1 - We check the resource pointed by m_ptr has been moved from p1 to p2
BOOST_CHECK(a1 == a2);
// 2 - We check that the two outputs of human_readable are identical
BOOST_CHECK(p1_string == p2_string);
}
// We check the copy constructor
{
problem p1{full_p(2, 0, 0, fit_2, lb_2, ub_2, grad_2, grads_2_correct, hess_22, hesss_22_correct)};
// We increment the counters so that the default values are changed
p1.fitness({1, 1});
p1.gradient({1, 1});
p1.hessians({1, 1});
auto a1 = p1.extract<full_p>();
problem p2(p1);
auto a2 = p2.extract<full_p>();
// 1 - We check the resource pointed by m_ptr has a different address
BOOST_CHECK(a1 != 0);
BOOST_CHECK(a2 != 0);
BOOST_CHECK(a1 != a2);
// 2 - We check that the counters are maintained by the copy operation
BOOST_CHECK(p2.get_fevals() == 1u);
BOOST_CHECK(p2.get_gevals() == 1u);
BOOST_CHECK(p2.get_hevals() == 1u);
// 3 - We check that the decision vector dimension is copied
BOOST_CHECK(p2.get_nx() == p1.get_nx());
}
// Default constructor.
problem p0;
BOOST_CHECK((p0.extract<null_problem>() != nullptr));
// Check copy semantics.
problem p1{p0};
BOOST_CHECK((p0.extract<null_problem>() != nullptr));
BOOST_CHECK((p1.extract<null_problem>() != nullptr));
problem p2{full_p{}};
p2 = p1;
BOOST_CHECK((p2.extract<null_problem>() != nullptr));
BOOST_CHECK((p1.extract<null_problem>() != nullptr));
// Move semantics.
problem p3{std::move(p0)};
BOOST_CHECK((p3.extract<null_problem>() != nullptr));
problem p4{full_p{}};
p4 = std::move(p2);
BOOST_CHECK((p4.extract<null_problem>() != nullptr));
// Check we can revive moved-from objects.
p0 = p4;
BOOST_CHECK((p0.extract<null_problem>() != nullptr));
p2 = std::move(p4);
BOOST_CHECK((p2.extract<null_problem>() != nullptr));
// Check the is_udp type trait.
BOOST_CHECK(is_udp<base_p>::value);
BOOST_CHECK(is_udp<grad_p>::value);
BOOST_CHECK(is_udp<hess_p>::value);
BOOST_CHECK(!is_udp<hess_p &>::value);
BOOST_CHECK(!is_udp<const hess_p &>::value);
BOOST_CHECK(!is_udp<const hess_p>::value);
BOOST_CHECK(!is_udp<int>::value);
BOOST_CHECK(!is_udp<void>::value);
BOOST_CHECK(!is_udp<std::string>::value);
BOOST_CHECK((std::is_constructible<problem, base_p>::value));
BOOST_CHECK((std::is_constructible<problem, grad_p>::value));
BOOST_CHECK((std::is_constructible<problem, hess_p>::value));
BOOST_CHECK((std::is_constructible<problem, hess_p &>::value));
BOOST_CHECK((std::is_constructible<problem, const hess_p &>::value));
BOOST_CHECK((std::is_constructible<problem, hess_p &&>::value));
BOOST_CHECK((!std::is_constructible<problem, int>::value));
BOOST_CHECK((!std::is_constructible<problem, std::string>::value));
}
BOOST_AUTO_TEST_CASE(problem_assignment_test)
{
vector_double lb_2(2, 0);
vector_double ub_2(2, 1);
vector_double fit_2(2, 1);
vector_double grad_2{1, 1};
sparsity_pattern grads_2_correct{{0, 0}, {0, 1}};
std::vector<vector_double> hess_22{{1, 1}, {1, 1}};
std::vector<sparsity_pattern> hesss_22_correct{{{0, 0}, {1, 0}}, {{0, 0}, {1, 0}}};
// We check the move assignment
{
problem p1{full_p(2, 0, 0, fit_2, lb_2, ub_2, grad_2, grads_2_correct, hess_22, hesss_22_correct)};
// We increment the counters so that the default values are changed
p1.fitness({1, 1});
p1.gradient({1, 1});
p1.hessians({1, 1});
auto p1_string = boost::lexical_cast<std::string>(p1);
auto a1 = p1.extract<full_p>();
problem p2{base_p{}};
p2 = std::move(p1);
auto a2 = p2.extract<full_p>();
auto p2_string = boost::lexical_cast<std::string>(p2);
// 1 - We check the resource pointed by m_ptr has been moved from p1 to p2
BOOST_CHECK(a1 == a2);
// 2 - We check that the two outputs of human_readable are identical
BOOST_CHECK(p1_string == p2_string);
}
// We check the copy assignment
{
problem p1{full_p(2, 0, 0, fit_2, lb_2, ub_2, grad_2, grads_2_correct, hess_22, hesss_22_correct)};
// We increment the counters so that the default values are changed
p1.fitness({1, 1});
p1.gradient({1, 1});
p1.hessians({1, 1});
auto a1 = p1.extract<full_p>();
problem p2{base_p{}};
p2 = p1;
auto a2 = p2.extract<full_p>();
// 1 - We check the resource pointed by m_ptr has a different address
BOOST_CHECK(a1 != 0);
BOOST_CHECK(a2 != 0);
BOOST_CHECK(a1 != a2);
// 2 - We check that the counters are reset by the copy operation
BOOST_CHECK(p2.get_fevals() == 1u);
BOOST_CHECK(p2.get_gevals() == 1u);
BOOST_CHECK(p2.get_hevals() == 1u);
// 3 - We check that the decision vector dimension is copied
BOOST_CHECK(p2.get_nx() == p1.get_nx());
}
}
BOOST_AUTO_TEST_CASE(problem_extract_is_test)
{
problem p1{base_p{2, 2, 2, {1, 1}, {5, 5}, {10, 10}}};
auto user_problem = p1.extract<base_p>();
// We check we have access to public data members
BOOST_CHECK(user_problem->m_nobj == 2);
BOOST_CHECK(user_problem->m_nec == 2);
BOOST_CHECK(user_problem->m_nic == 2);
BOOST_CHECK((user_problem->m_ret_fit == vector_double{1, 1}));
BOOST_CHECK((user_problem->m_lb == vector_double{5, 5}));
BOOST_CHECK((user_problem->m_ub == vector_double{10, 10}));
// We check that a non successful cast returns a null pointer
BOOST_CHECK(!p1.extract<full_p>());
// We check the is method
BOOST_CHECK(p1.is<base_p>());
BOOST_CHECK(!p1.is<full_p>());
}
BOOST_AUTO_TEST_CASE(problem_fitness_test)
{
problem p1{base_p{2, 2, 2, {12, 13, 14, 15, 16, 17}, {5, 5}, {10, 10}}};
problem p1_wrong_retval{base_p{2, 2, 2, {1, 1, 1}, {5, 5}, {10, 10}}};
// We check the fitness checks
BOOST_CHECK_THROW(p1.fitness({3, 3, 3, 3}), std::invalid_argument);
BOOST_CHECK_THROW(p1_wrong_retval.fitness({3, 3}), std::invalid_argument);
// We check the fitness returns the correct value
BOOST_CHECK((p1.fitness({3, 3}) == vector_double{12, 13, 14, 15, 16, 17}));
}
BOOST_AUTO_TEST_CASE(problem_gradient_test)
{
problem p1{grad_p{1, 0, 0, {12}, {5, 5}, {10, 10}, {12, 13}, {{0, 0}, {0, 1}}}};
problem p1_wrong_retval{grad_p{1, 0, 0, {12}, {5, 5}, {10, 10}, {1, 2, 3, 4}}};
// We check the gradient checks
BOOST_CHECK_THROW(p1.gradient({3, 3, 3}), std::invalid_argument);
BOOST_CHECK_THROW(p1_wrong_retval.gradient({3, 3}), std::invalid_argument);
// We check the fitness returns the correct value
BOOST_CHECK((p1.gradient({3, 3}) == vector_double{12, 13}));
{
problem p2{base_p{2, 2, 2, {12, 13, 14, 15, 16, 17}, {5, 5}, {10, 10}}};
BOOST_CHECK_THROW(p2.gradient({3, 3}), not_implemented_error);
BOOST_CHECK_THROW(p2.hessians({3, 3}), not_implemented_error);
}
}
BOOST_AUTO_TEST_CASE(problem_hessians_test)
{
problem p1{hess_p{1, 0, 0, {12}, {5, 5}, {10, 10}, {{12, 13}}, {{{0, 0}, {1, 0}}}}};
problem p1_wrong_retval{hess_p{1, 0, 0, {12}, {5, 5}, {10, 10}, {{12, 13, 14}}, {{{0, 0}, {1, 0}}}}};
// We check the gradient checks
BOOST_CHECK_THROW(p1.hessians({3, 3, 3}), std::invalid_argument);
BOOST_CHECK_THROW(p1_wrong_retval.hessians({3, 3}), std::invalid_argument);
// We check the fitness returns the correct value
BOOST_CHECK((p1.hessians({3, 3}) == std::vector<vector_double>{{12, 13}}));
}
// We add a problem signalling gradient_sparsity() as present, but not implementing it
struct hgs_not_impl {
vector_double fitness(const vector_double &) const
{
return {1., 1.};
}
vector_double::size_type get_nobj() const
{
return 1u;
}
bool has_gradient_sparsity() const
{
return true;
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0.}, {1.}};
}
};
// We add a problem signalling hessians_sparsity() as present, but not implementing it
struct hhs_not_impl {
vector_double fitness(const vector_double &) const
{
return {1., 1.};
}
vector_double::size_type get_nobj() const
{
return 1u;
}
bool has_hessians_sparsity() const
{
return true;
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0.}, {1.}};
}
};
// We add a problem signalling set_seed() as present, but not implementing it
struct ss_not_impl {
vector_double fitness(const vector_double &) const
{
return {1., 1.};
}
vector_double::size_type get_nobj() const
{
return 1u;
}
bool has_set_seed() const
{
return true;
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0.}, {1.}};
}
};
BOOST_AUTO_TEST_CASE(problem_has_test)
{
problem p1{base_p{}};
problem p2{grad_p{}};
problem p3{hess_p{}};
problem p4{grad_p_override{}};
problem p5{hess_p_override{}};
BOOST_CHECK(!p1.has_gradient());
BOOST_CHECK(!p1.has_gradient_sparsity());
BOOST_CHECK(!p1.has_hessians());
BOOST_CHECK(!p1.has_hessians_sparsity());
BOOST_CHECK(p2.has_gradient());
BOOST_CHECK(p2.has_gradient_sparsity());
BOOST_CHECK(!p2.has_hessians());
BOOST_CHECK(!p2.has_hessians_sparsity());
BOOST_CHECK(!p3.has_gradient());
BOOST_CHECK(!p3.has_gradient_sparsity());
BOOST_CHECK(p3.has_hessians());
BOOST_CHECK(p3.has_hessians_sparsity());
BOOST_CHECK(!p4.has_gradient());
BOOST_CHECK(!p4.has_gradient_sparsity());
BOOST_CHECK(!p4.has_hessians());
BOOST_CHECK(!p4.has_hessians_sparsity());
problem p6{ss_not_impl{}};
BOOST_CHECK_THROW(p6.set_seed(32u), not_implemented_error);
problem p7{base_p{}};
BOOST_CHECK_THROW(p7.set_seed(32u), not_implemented_error);
// These two implement the has_sparsity() methods without the sparsity() methods.
// They will not error out because the lack of the sparsity() methods makes the has_sparsity()
// methods return always false.
BOOST_CHECK_NO_THROW(problem{hgs_not_impl{}});
BOOST_CHECK(!problem{hgs_not_impl{}}.has_gradient_sparsity());
BOOST_CHECK_NO_THROW(problem{hgs_not_impl{}}.gradient_sparsity());
BOOST_CHECK_NO_THROW(problem{hhs_not_impl{}});
BOOST_CHECK(!problem{hhs_not_impl{}}.has_hessians_sparsity());
BOOST_CHECK_NO_THROW(problem{hhs_not_impl{}}.hessians_sparsity());
}
BOOST_AUTO_TEST_CASE(problem_getters_test)
{
vector_double lb_2(2, 13);
vector_double ub_2(2, 17);
vector_double fit_2(2, 1);
vector_double grad_2{1, 1};
sparsity_pattern grads_2_correct{{0, 0}, {0, 1}};
std::vector<vector_double> hess_22{{1, 1}, {1, 1}};
std::vector<sparsity_pattern> hesss_22_correct{{{0, 0}, {1, 0}}, {{0, 0}, {1, 0}}};
problem p1{base_p(2, 3, 4, {3, 4, 5, 6, 7, 8, 9, 0, 1}, lb_2, ub_2)};
problem p2{full_p(2, 0, 0, fit_2, lb_2, ub_2, grad_2, grads_2_correct, hess_22, hesss_22_correct)};
problem p3{empty{}};
BOOST_CHECK(p1.get_nobj() == 2);
BOOST_CHECK(p1.get_nx() == 2);
BOOST_CHECK(p1.get_nec() == 3);
BOOST_CHECK(p1.get_nic() == 4);
BOOST_CHECK(p1.get_nc() == 4 + 3);
BOOST_CHECK((p1.get_c_tol() == vector_double{0., 0., 0., 0., 0., 0., 0.}));
BOOST_CHECK(p1.get_nf() == 2 + 3 + 4);
BOOST_CHECK((p1.get_bounds() == std::pair<vector_double, vector_double>{{13, 13}, {17, 17}}));
BOOST_CHECK((p1.get_lb() == vector_double{13, 13}));
BOOST_CHECK((p1.get_ub() == vector_double{17, 17}));
// Making some evaluations
auto N = 1235u;
for (auto i = 0u; i < N; ++i) {
p2.fitness({0, 0});
p2.gradient({0, 0});
p2.hessians({0, 0});
}
BOOST_CHECK(p2.get_fevals() == N);
BOOST_CHECK(p2.get_gevals() == N);
BOOST_CHECK(p2.get_hevals() == N);
// User implemented
BOOST_CHECK(p1.get_name() == "A base toy problem");
BOOST_CHECK(p1.get_extra_info() == "Nothing to report");
// Default
BOOST_CHECK(p3.get_name() == detail::demangle_from_typeid(typeid(*p3.extract<empty>()).name()));
BOOST_CHECK(p3.get_extra_info() == "");
}
BOOST_AUTO_TEST_CASE(problem_serialization_test)
{
// Do the checking with the full problem.
problem p{full_p{}}, p2{p};
// Call objfun, grad and hess to increase the internal counters.
p.fitness({1.});
p.gradient({1.});
p.hessians({1.});
// Store the string representation.
std::stringstream ss;
auto before = boost::lexical_cast<std::string>(p);
// Now serialize, deserialize and compare the result.
{
boost::archive::binary_oarchive oarchive(ss);
oarchive << p;
}
// Change the content of p before deserializing.
p = problem{grad_p{}};
{
boost::archive::binary_iarchive iarchive(ss);
iarchive >> p;
}
auto after = boost::lexical_cast<std::string>(p);
BOOST_CHECK_EQUAL(before, after);
// Check that the properties of base_p where restored as well.
BOOST_CHECK_EQUAL(p.extract<full_p>()->m_nobj, p2.extract<full_p>()->m_nobj);
BOOST_CHECK_EQUAL(p.extract<full_p>()->m_nec, p2.extract<full_p>()->m_nec);
BOOST_CHECK_EQUAL(p.extract<full_p>()->m_nic, p2.extract<full_p>()->m_nic);
BOOST_CHECK(p.extract<full_p>()->m_ret_fit == p2.extract<full_p>()->m_ret_fit);
BOOST_CHECK(p.extract<full_p>()->m_lb == p2.extract<full_p>()->m_lb);
BOOST_CHECK(p.extract<full_p>()->m_ub == p2.extract<full_p>()->m_ub);
}
// Full minimal problems to test constraints number
// Only equality
struct c_01 {
vector_double fitness(const vector_double &) const
{
return {2, 2};
}
vector_double::size_type get_nec() const
{
return 1u;
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
};
// Only inequality
struct c_02 {
vector_double fitness(const vector_double &) const
{
return {2, 2};
}
vector_double::size_type get_nic() const
{
return 1u;
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
};
// Both equality and inequality
struct c_03 {
vector_double fitness(const vector_double &) const
{
return {2, 2, 2};
}
vector_double::size_type get_nec() const
{
return 1u;
}
vector_double::size_type get_nic() const
{
return 1u;
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
};
BOOST_AUTO_TEST_CASE(problem_constraint_dimension_test)
{
BOOST_CHECK(problem{c_01{}}.get_nec() == 1u);
BOOST_CHECK(problem{c_01{}}.get_nic() == 0u);
BOOST_CHECK(problem{c_01{}}.get_nc() == 1u);
BOOST_CHECK(problem{c_02{}}.get_nec() == 0u);
BOOST_CHECK(problem{c_02{}}.get_nic() == 1u);
BOOST_CHECK(problem{c_02{}}.get_nc() == 1u);
BOOST_CHECK(problem{c_03{}}.get_nec() == 1u);
BOOST_CHECK(problem{c_03{}}.get_nic() == 1u);
BOOST_CHECK(problem{c_03{}}.get_nc() == 2u);
}
struct s_02 {
vector_double fitness(const vector_double &) const
{
return {2, 2, 2};
}
vector_double::size_type get_nec() const
{
return 1u;
}
vector_double::size_type get_nic() const
{
return 1u;
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
void set_seed(unsigned seed)
{
m_seed = seed;
}
unsigned m_seed = 0u;
};
struct s_03 {
vector_double fitness(const vector_double &) const
{
return {2, 2, 2};
}
vector_double::size_type get_nec() const
{
return 1u;
}
vector_double::size_type get_nic() const
{
return 1u;
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
void set_seed(unsigned seed)
{
m_seed = seed;
}
bool has_set_seed() const
{
return false;
}
unsigned m_seed = 0u;
};
BOOST_AUTO_TEST_CASE(problem_stochastic_test)
{
problem prob{s_02{}};
BOOST_CHECK(prob.is_stochastic() == true);
BOOST_CHECK(prob.has_set_seed() == true);
prob.set_seed(32u);
BOOST_CHECK(prob.extract<s_02>()->m_seed == 32u);
BOOST_CHECK(problem{s_03{}}.is_stochastic() == false);
BOOST_CHECK(problem{s_03{}}.has_set_seed() == false);
}
struct extra_info_case {
vector_double fitness(const vector_double &) const
{
return {2, 2, 2};
}
vector_double::size_type get_nec() const
{
return 1u;
}
vector_double::size_type get_nic() const
{
return 1u;
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
void set_seed(unsigned seed)
{
m_seed = seed;
}
bool has_set_seed() const
{
return true;
}
std::string get_extra_info() const
{
return std::to_string(m_seed);
}
unsigned m_seed = 0u;
};
BOOST_AUTO_TEST_CASE(problem_extra_info_test)
{
problem prob{extra_info_case{}};
problem prob2(prob);
BOOST_CHECK(prob.get_extra_info() == prob2.get_extra_info());
prob.set_seed(32u);
BOOST_CHECK(prob.get_extra_info() == "32");
}
struct with_get_nobj {
vector_double fitness(const vector_double &) const
{
return {2, 2, 2};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
vector_double::size_type get_nobj() const
{
return 3u;
}
};
struct without_get_nobj {
vector_double fitness(const vector_double &) const
{
return {2, 2, 2};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
};
BOOST_AUTO_TEST_CASE(problem_get_nobj_detection)
{
BOOST_CHECK(problem{with_get_nobj{}}.get_nobj() == 3u);
BOOST_CHECK(problem{without_get_nobj{}}.get_nobj() == 1u);
BOOST_CHECK_NO_THROW(problem{with_get_nobj{}}.fitness({1.}));
BOOST_CHECK_THROW(problem{without_get_nobj{}}.fitness({1.}),
std::invalid_argument); // detects a returned size of 3 but has the default
}
BOOST_AUTO_TEST_CASE(problem_auto_sparsity_test)
{
problem p{base_p(2u, 2u, 2u, {1., 1., 1., 1., 1., 1.}, {0., 0.}, {1., 1.})};
BOOST_CHECK(p.gradient_sparsity() == detail::dense_gradient(6u, 2u));
BOOST_CHECK(p.hessians_sparsity() == detail::dense_hessians(6u, 2u));
}
BOOST_AUTO_TEST_CASE(problem_get_set_c_tol_test)
{
problem prob{base_p(2u, 1u, 1u, {1., 1., 1., 1.}, {0., 0.}, {1., 1.})};
BOOST_CHECK((prob.get_c_tol() == vector_double{0., 0.}));
prob.set_c_tol({1., 2.});
BOOST_CHECK((prob.get_c_tol() == vector_double{1., 2.}));
prob.set_c_tol({12., 22.});
BOOST_CHECK((prob.get_c_tol() == vector_double{12., 22.}));
if (std::numeric_limits<double>::has_quiet_NaN) {
BOOST_CHECK_THROW(prob.set_c_tol({std::numeric_limits<double>::quiet_NaN(), 22.}), std::invalid_argument);
BOOST_CHECK((prob.get_c_tol() == vector_double{12., 22.}));
}
BOOST_CHECK_THROW(prob.set_c_tol({-12., 22.}), std::invalid_argument);
BOOST_CHECK((prob.get_c_tol() == vector_double{12., 22.}));
BOOST_CHECK_THROW(prob.set_c_tol({12., 22., 33.});, std::invalid_argument);
BOOST_CHECK((prob.get_c_tol() == vector_double{12., 22.}));
// checking the overload method
BOOST_CHECK_THROW(prob.set_c_tol(-12.), std::invalid_argument);
if (std::numeric_limits<double>::has_quiet_NaN) {
BOOST_CHECK_THROW(prob.set_c_tol(std::numeric_limits<double>::quiet_NaN()), std::invalid_argument);
}
prob.set_c_tol(22);
BOOST_CHECK((prob.get_c_tol() == vector_double{22., 22.}));
}
BOOST_AUTO_TEST_CASE(problem_feasibility_methods_test)
{
problem test01{base_p(2u, 1u, 1u, {1., 1., 1., 1.}, {0., 0.}, {1., 1.})};
problem test02{base_p(2u, 1u, 1u, {1., 1., 1e-9, -1.}, {0., 0.}, {1., 1.})};
BOOST_CHECK(test01.feasibility_x({1., 1.}) == false);
BOOST_CHECK(test01.feasibility_f({2., 3., 1e-10, 3.}) == false);
test01.set_c_tol({2., 2.});
BOOST_CHECK(test01.feasibility_x({1., 1.}) == true);
BOOST_CHECK(test01.feasibility_f({2., 3., 1e-10, 1.}) == true);
BOOST_CHECK(test02.feasibility_x({1., 1.}) == false);
BOOST_CHECK(test02.feasibility_f({2., 3., 1e-10, 3.}) == false);
test02.set_c_tol({2., 2.});
BOOST_CHECK(test02.feasibility_x({1., 1.}) == true);
BOOST_CHECK(test02.feasibility_f({2., 3., 1e-10, 1.5}) == true);
BOOST_CHECK_THROW(test02.feasibility_f({1., -23, 1e-10, 2., 34.}), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(null_problem_test)
{
// Problem instantiation
problem p;
BOOST_CHECK_EQUAL(p.get_name(), "Null problem");
// Pick a few reference points
vector_double x1 = {1};
vector_double x2 = {2};
// Fitness test
BOOST_CHECK((p.fitness(x1) == vector_double{0}));
BOOST_CHECK((p.fitness(x2) == vector_double{0}));
p = problem{null_problem{2}};
BOOST_CHECK(null_problem{2}.get_nobj() == 2u);
BOOST_CHECK(null_problem{2}.get_nec() == 0u);
BOOST_CHECK(null_problem{2}.get_nic() == 0u);
BOOST_CHECK(null_problem{2}.get_nix() == 0u);
BOOST_CHECK((null_problem{2, 3, 4, 1}.get_nobj() == 2u));
BOOST_CHECK((null_problem{2, 3, 4, 1}.get_nec() == 3u));
BOOST_CHECK((null_problem{2, 3, 4, 1}.get_nic() == 4u));
BOOST_CHECK((null_problem{2, 3, 4, 1}.get_nix() == 1u));
BOOST_CHECK((null_problem{2, 3, 4, 0}.get_nix() == 0u));
BOOST_CHECK(p.get_nobj() == 2u);
BOOST_CHECK((p.fitness(x1) == vector_double{0, 0}));
BOOST_CHECK((p.fitness(x2) == vector_double{0, 0}));
BOOST_CHECK_THROW(p = problem{null_problem{0}}, std::invalid_argument);
BOOST_CHECK_THROW((p = problem{null_problem{2, 3, 4, 2}}), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(null_problem_serialization_test)
{
problem p{null_problem{2, 3, 4}};
// Call objfun to increase the internal counter.
p.fitness({1});
// Store the string representation of p.
std::stringstream ss;
auto before = boost::lexical_cast<std::string>(p);
// Now serialize, deserialize and compare the result.
{
boost::archive::binary_oarchive oarchive(ss);
oarchive << p;
}
// Change the content of p before deserializing.
p = problem{};
BOOST_CHECK_EQUAL(p.get_nobj(), 1u);
{
boost::archive::binary_iarchive iarchive(ss);
iarchive >> p;
}
auto after = boost::lexical_cast<std::string>(p);
BOOST_CHECK_EQUAL(before, after);
BOOST_CHECK_EQUAL(p.get_nobj(), 2u);
BOOST_CHECK_EQUAL(p.get_nec(), 3u);
BOOST_CHECK_EQUAL(p.get_nic(), 4u);
BOOST_CHECK_EQUAL(p.fitness({1.}).size(), 9u);
}
BOOST_AUTO_TEST_CASE(extract_test)
{
problem p;
BOOST_CHECK(p.is<null_problem>());
BOOST_CHECK(!p.is<base_p>());
BOOST_CHECK((std::is_same<null_problem *, decltype(p.extract<null_problem>())>::value));
BOOST_CHECK(
(std::is_same<null_problem const *, decltype(static_cast<const problem &>(p).extract<null_problem>())>::value));
BOOST_CHECK(p.extract<null_problem>() != nullptr);
BOOST_CHECK(static_cast<const problem &>(p).extract<null_problem>() != nullptr);
BOOST_CHECK(p.extract<base_p>() == nullptr);
BOOST_CHECK(static_cast<const problem &>(p).extract<base_p>() == nullptr);
}
struct ts1 {
vector_double fitness(const vector_double &) const
{
return {2, 2, 2};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
};
struct ts2 {
vector_double fitness(const vector_double &) const
{
return {2, 2, 2};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
thread_safety get_thread_safety() const
{
return thread_safety::none;
}
};
struct ts3 {
vector_double fitness(const vector_double &) const
{
return {2, 2, 2};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
int get_thread_safety() const
{
return 2;
}
};
BOOST_AUTO_TEST_CASE(thread_safety_test)
{
BOOST_CHECK(problem{}.get_thread_safety() == thread_safety::basic);
BOOST_CHECK(problem{ts1{}}.get_thread_safety() == thread_safety::basic);
BOOST_CHECK(problem{ts2{}}.get_thread_safety() == thread_safety::none);
BOOST_CHECK(problem{ts3{}}.get_thread_safety() == thread_safety::basic);
}
struct gs1 {
vector_double fitness(const vector_double &) const
{
return {0, 0};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 1}};
}
sparsity_pattern gradient_sparsity() const
{
if (!n_grad_invs) {
++n_grad_invs;
return {};
}
return {{0, 0}};
}
static int n_grad_invs;
};
int gs1::n_grad_invs = 0;
struct gs2 {
vector_double fitness(const vector_double &) const
{
return {0, 0};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 1}};
}
sparsity_pattern gradient_sparsity() const
{
return {{0, 0}};
}
};
struct gs3 {
vector_double fitness(const vector_double &) const
{
return {0, 0};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 1}};
}
sparsity_pattern gradient_sparsity() const
{
return {{0, 0}, {0, 2}, {0, 1}};
}
};
BOOST_AUTO_TEST_CASE(custom_gs)
{
// Test a gradient sparsity that changes after the first invocation of gradient_sparsity().
problem p{gs1{}};
BOOST_CHECK_THROW(p.gradient_sparsity(), std::invalid_argument);
p = problem{gs2{}};
BOOST_CHECK_NO_THROW(p.gradient_sparsity());
// Gradient sparsity not sorted.
BOOST_CHECK_THROW(p = problem{gs3{}}, std::invalid_argument);
}
struct hs1 {
vector_double fitness(const vector_double &) const
{
return {0, 0};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 1}};
}
vector_double::size_type get_nobj() const
{
return 2;
}
std::vector<sparsity_pattern> hessians_sparsity() const
{
if (!n_hess_invs) {
++n_hess_invs;
return {{{1, 0}}, {{1, 0}}};
}
return {{{1, 0}}, {{1, 0}, {2, 0}}};
}
static int n_hess_invs;
};
int hs1::n_hess_invs = 0;
struct hs2 {
vector_double fitness(const vector_double &) const
{
return {0, 0};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 1}};
}
vector_double::size_type get_nobj() const
{
return 2;
}
std::vector<sparsity_pattern> hessians_sparsity() const
{
return {{{1, 0}}, {{1, 0}, {2, 0}}};
}
};
struct hs3 {
vector_double fitness(const vector_double &) const
{
return {0, 0};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 1}};
}
vector_double::size_type get_nobj() const
{
return 2;
}
std::vector<sparsity_pattern> hessians_sparsity() const
{
return {{{1, 0}, {2, 1}, {1, 1}}, {{1, 0}, {2, 0}}};
}
};
BOOST_AUTO_TEST_CASE(custom_hs)
{
// Test a hessians sparsity that changes after the first invocation of hessians_sparsity().
problem p{hs1{}};
BOOST_CHECK_THROW(p.hessians_sparsity(), std::invalid_argument);
p = problem{hs2{}};
BOOST_CHECK_NO_THROW(p.hessians_sparsity());
BOOST_CHECK_THROW(p = problem{hs3{}}, std::invalid_argument);
}
struct hess1 {
vector_double fitness(const vector_double &) const
{
return {0, 0};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0, 0, 0, 0, 0, 0}, {1, 1, 1, 1, 1, 1}};
}
vector_double::size_type get_nobj() const
{
return 2;
}
std::vector<vector_double> hessians(const vector_double &) const
{
return {{}};
}
};
BOOST_AUTO_TEST_CASE(broken_hessian)
{
// Test a hessians method that returns a number of vectors different from get_nf().
problem p{hess1{}};
BOOST_CHECK_THROW(p.hessians({1, 1, 1, 1, 1, 1}), std::invalid_argument);
}
struct minlp {
minlp(vector_double::size_type nix = 0u)
{
m_nix = nix;
}
vector_double fitness(const vector_double &x) const
{
return {std::sin(x[0] * x[1] * x[2]), x[0] + x[1] + x[2], x[0] * x[1] + x[1] * x[2] - x[0] * x[2]};
}
vector_double::size_type get_nobj() const
{
return 1u;
}
vector_double::size_type get_nec() const
{
return 1u;
}
vector_double::size_type get_nic() const
{
return 1u;
}
vector_double::size_type get_nix() const
{
return m_nix;
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{1, 1, 1}, {2, 2, 2}};
}
std::string get_name() const
{
return "A minlp problem";
}
vector_double::size_type m_nix;
};
BOOST_AUTO_TEST_CASE(minlp_test)
{
BOOST_CHECK((problem{minlp{1u}}.get_nix() == 1u));
BOOST_CHECK((problem{minlp{1u}}.get_ncx() == 2u));
BOOST_CHECK((problem{minlp{1u}}.get_nx() == 3u));
BOOST_CHECK((problem{minlp{2u}}.get_nix() == 2u));
BOOST_CHECK((problem{minlp{2u}}.get_ncx() == 1u));
BOOST_CHECK((problem{minlp{2u}}.get_nx() == 3u));
BOOST_CHECK((problem{minlp{3u}}.get_nix() == 3u));
BOOST_CHECK((problem{minlp{3u}}.get_ncx() == 0u));
BOOST_CHECK((problem{minlp{3u}}.get_nx() == 3u));
BOOST_CHECK_THROW(problem{minlp{5u}}, std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(increase_counter)
{
problem p;
BOOST_CHECK(p.get_fevals() == 0u);
p.increment_fevals(100u);
BOOST_CHECK(p.get_fevals() == 100u);
p.increment_fevals(10u);
BOOST_CHECK(p.get_fevals() == 110u);
p.increment_fevals(0u);
BOOST_CHECK(p.get_fevals() == 110u);
}
struct bf_s11n {
vector_double fitness(const vector_double &) const
{
return {0};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
vector_double batch_fitness(const vector_double &dvs) const
{
return vector_double(dvs.size(), 1.);
}
template <typename Archive>
void serialize(Archive &, unsigned)
{
}
};
PAGMO_S11N_PROBLEM_EXPORT(bf_s11n)
BOOST_AUTO_TEST_CASE(batch_fitness)
{
// Test a problem with no batch fitness.
problem p;
BOOST_CHECK(!has_batch_fitness<null_problem>::value);
BOOST_CHECK(!override_has_batch_fitness<null_problem>::value);
BOOST_CHECK(!p.has_batch_fitness());
BOOST_CHECK_EXCEPTION(p.batch_fitness(vector_double{1.}), not_implemented_error,
[](const not_implemented_error &nie) {
return boost::contains(nie.what(), "The batch_fitness() method has been invoked, but it "
"is not implemented in a UDP of type 'Null problem'");
});
// A UDP which provides batch_fitness().
struct bf0 {
vector_double fitness(const vector_double &) const
{
return {0};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
vector_double batch_fitness(const vector_double &dvs) const
{
return vector_double(dvs.size(), 1.);
}
};
p = problem{bf0{}};
BOOST_CHECK(has_batch_fitness<bf0>::value);
BOOST_CHECK(!override_has_batch_fitness<bf0>::value);
BOOST_CHECK(p.has_batch_fitness());
BOOST_CHECK(p.batch_fitness({1., 2., 3.}) == vector_double(3, 1.));
// A UDP which provides batch_fitness(), 2-dimensional.
struct bf1 {
vector_double fitness(const vector_double &) const
{
return {0};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0, 0}, {1, 1}};
}
vector_double batch_fitness(const vector_double &dvs) const
{
return vector_double(dvs.size() / 2u, 1.);
}
};
p = problem{bf1{}};
BOOST_CHECK(has_batch_fitness<bf1>::value);
BOOST_CHECK(!override_has_batch_fitness<bf1>::value);
BOOST_CHECK(p.has_batch_fitness());
BOOST_CHECK(p.batch_fitness({1., 2., 3., 4.}) == vector_double(2, 1.));
// Check throw on wrong input vector.
BOOST_CHECK_EXCEPTION(
p.batch_fitness(vector_double{1.}), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(
ia.what(),
"Invalid argument for a batch fitness evaluation: the length of the vector "
"representing the decision vectors, 1, is not an exact multiple of the dimension of the problem, 2");
});
// A UDP which provides batch_fitness(), but with wrong retval.
struct bf2 {
vector_double fitness(const vector_double &) const
{
return {0, 0};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
vector_double batch_fitness(const vector_double &dvs) const
{
return vector_double(dvs.size(), 1.);
}
vector_double::size_type get_nobj() const
{
return 2;
}
};
p = problem{bf2{}};
BOOST_CHECK(has_batch_fitness<bf2>::value);
BOOST_CHECK(!override_has_batch_fitness<bf2>::value);
BOOST_CHECK(p.has_batch_fitness());
BOOST_CHECK_EXCEPTION(
p.batch_fitness(vector_double{1.}), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"An invalid result was produced by a batch fitness evaluation: the length of "
"the vector representing the fitness vectors, 1, is not an exact multiple of the "
"fitness dimension of the problem, 2");
});
// A UDP which provides batch_fitness(), but with wrong number of fvs.
struct bf3 {
vector_double fitness(const vector_double &) const
{
return {};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
vector_double batch_fitness(const vector_double &) const
{
return vector_double{};
}
};
p = problem{bf3{}};
BOOST_CHECK(has_batch_fitness<bf3>::value);
BOOST_CHECK(!override_has_batch_fitness<bf3>::value);
BOOST_CHECK(p.has_batch_fitness());
BOOST_CHECK_EXCEPTION(
p.batch_fitness(vector_double{1.}), std::invalid_argument, [](const std::invalid_argument &ia) {
return boost::contains(ia.what(),
"An invalid result was produced by a batch fitness evaluation: the number of "
"produced fitness vectors, 0, differs from the number of input decision vectors, 1");
});
// A UDP which provides has_batch_fitness(), but no batch_fitness().
struct bf4 {
vector_double fitness(const vector_double &) const
{
return {0.};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
bool has_batch_fitness() const
{
return true;
}
};
p = problem{bf4{}};
BOOST_CHECK(!has_batch_fitness<bf4>::value);
BOOST_CHECK(override_has_batch_fitness<bf4>::value);
BOOST_CHECK_EXCEPTION(p.batch_fitness(vector_double{1.}), not_implemented_error,
[](const not_implemented_error &nie) {
return boost::contains(nie.what(), "The batch_fitness() method has been invoked, but it "
"is not implemented in a UDP of type");
});
// A UDP which provides has_batch_fitness() and batch_fitness().
struct bf5 {
vector_double fitness(const vector_double &) const
{
return {0};
}
std::pair<vector_double, vector_double> get_bounds() const
{
return {{0}, {1}};
}
vector_double batch_fitness(const vector_double &dvs) const
{
return vector_double(dvs.size(), 1.);
}
bool has_batch_fitness() const
{
return false;
}
};
p = problem{bf5{}};
BOOST_CHECK(has_batch_fitness<bf5>::value);
BOOST_CHECK(override_has_batch_fitness<bf5>::value);
BOOST_CHECK(!p.has_batch_fitness());
BOOST_CHECK(p.batch_fitness({1., 2., 3.}) == vector_double(3, 1.));
// Check the counter as well.
BOOST_CHECK(p.get_fevals() == 3u);
// Serialization check.
p = problem{bf_s11n{}};
BOOST_CHECK(p.has_batch_fitness());
std::stringstream ss;
auto before = boost::lexical_cast<std::string>(p);
// Now serialize, deserialize and compare the result.
{
boost::archive::binary_oarchive oarchive(ss);
oarchive << p;
}
// Change the content of p before deserializing.
p = problem{};
BOOST_CHECK(!p.has_batch_fitness());
{
boost::archive::binary_iarchive iarchive(ss);
iarchive >> p;
}
auto after = boost::lexical_cast<std::string>(p);
BOOST_CHECK_EQUAL(before, after);
BOOST_CHECK(p.has_batch_fitness());
}
BOOST_AUTO_TEST_CASE(is_valid)
{
problem p0;
BOOST_CHECK(p0.is_valid());
problem p1(std::move(p0));
BOOST_CHECK(!p0.is_valid());
p0 = problem{full_p{}};
BOOST_CHECK(p0.is_valid());
p1 = std::move(p0);
BOOST_CHECK(!p0.is_valid());
p0 = problem{full_p{}};
BOOST_CHECK(p0.is_valid());
}
BOOST_AUTO_TEST_CASE(generic_assignment)
{
problem p0;
BOOST_CHECK(p0.is<null_problem>());
BOOST_CHECK(&(p0 = full_p{}) == &p0);
BOOST_CHECK(p0.is_valid());
BOOST_CHECK(p0.is<full_p>());
BOOST_CHECK((!std::is_assignable<problem, void>::value));
BOOST_CHECK((!std::is_assignable<problem, int &>::value));
BOOST_CHECK((!std::is_assignable<problem, const int &>::value));
BOOST_CHECK((!std::is_assignable<problem, int &&>::value));
}
BOOST_AUTO_TEST_CASE(type_index)
{
problem p0;
BOOST_CHECK(p0.get_type_index() == std::type_index(typeid(null_problem)));
p0 = problem{grad_p_override{}};
BOOST_CHECK(p0.get_type_index() == std::type_index(typeid(grad_p_override)));
}
BOOST_AUTO_TEST_CASE(get_ptr)
{
problem p0;
BOOST_CHECK(p0.get_ptr() == p0.extract<null_problem>());
BOOST_CHECK(static_cast<const problem &>(p0).get_ptr() == static_cast<const problem &>(p0).extract<null_problem>());
p0 = problem{grad_p_override{}};
BOOST_CHECK(p0.get_ptr() == p0.extract<grad_p_override>());
BOOST_CHECK(static_cast<const problem &>(p0).get_ptr()
== static_cast<const problem &>(p0).extract<grad_p_override>());
}
|