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
|
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
* Guido Tack <tack@gecode.org>
*
* Contributing authors:
* Kevin Leo <kevin.leo@monash.edu>
* Maxim Shishmarev <maxim.shishmarev@monash.edu>
*
* Copyright:
* Kevin Leo, 2017
* Christian Schulte, 2002
* Maxim Shishmarev, 2017
* Guido Tack, 2004
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef GECODE_SEARCH_HH
#define GECODE_SEARCH_HH
#include <initializer_list>
#include <gecode/kernel.hh>
/*
* Configure linking
*
*/
#if !defined(GECODE_STATIC_LIBS) && \
(defined(__CYGWIN__) || defined(__MINGW32__) || defined(_MSC_VER))
#ifdef GECODE_BUILD_SEARCH
#define GECODE_SEARCH_EXPORT __declspec( dllexport )
#else
#define GECODE_SEARCH_EXPORT __declspec( dllimport )
#endif
#else
#ifdef GECODE_GCC_HAS_CLASS_VISIBILITY
#define GECODE_SEARCH_EXPORT __attribute__ ((visibility("default")))
#else
#define GECODE_SEARCH_EXPORT
#endif
#endif
// Configure auto-linking
#ifndef GECODE_BUILD_SEARCH
#define GECODE_LIBRARY_NAME "Search"
#include <gecode/support/auto-link.hpp>
#endif
namespace Gecode { namespace Search {
/// %Sequential search engine implementations
namespace Sequential {}
/// %Parallel search engine implementations
namespace Parallel {}
/// %Meta search engine implementations
namespace Meta {}
namespace Meta {
/// %Sequential meta search engine implementations
namespace Sequential {}
/// %Parallel meta search engine implementations
namespace Parallel {}
}
/**
* \brief %Search configuration
*
* \ingroup TaskModelSearch
*/
namespace Config {
/// Whether engines create a clone when being initialized
const bool clone = true;
/// Number of threads to use
const double threads = 1.0;
/// Create a clone after every \a c_d commits (commit distance)
const unsigned int c_d = 8;
/// Create a clone during recomputation if distance is greater than \a a_d (adaptive distance)
const unsigned int a_d = 2;
/// Minimal number of open nodes for stealing
const unsigned int steal_limit = 3;
/// Initial delay in milliseconds for all but first worker thread
const unsigned int initial_delay = 5;
/// Default discrepancy limit for LDS
const unsigned int d_l = 5;
/// Base for geometric restart sequence
const double base = 1.5;
/// Size of a slice in a portfolio and scale factor for restarts(in number of failures)
const unsigned int slice = 250;
/// Depth limit for no-good generation during search
const unsigned int nogoods_limit = 128;
/// Default port for CPProfiler
const unsigned int cpprofiler_port = 6565U;
}
}}
#include <gecode/search/exception.hpp>
namespace Gecode { namespace Search {
/**
* \brief %Search engine statistics
* \ingroup TaskModelSearch
*/
class Statistics : public StatusStatistics {
public:
/// Number of failed nodes in search tree
unsigned long long int fail;
/// Number of nodes expanded
unsigned long long int node;
/// Maximum depth of search stack
unsigned long int depth;
/// Number of restarts
unsigned long int restart;
/// Number of no-goods posted
unsigned long int nogood;
/// Initialize
Statistics(void);
/// Reset
void reset(void);
/// Return sum with \a s
Statistics operator +(const Statistics& s);
/// Increment by statistics \a s
Statistics& operator +=(const Statistics& s);
};
}}
#include <gecode/search/statistics.hpp>
namespace Gecode { namespace Search {
class WrapTraceRecorder;
class TraceRecorder;
class EdgeTraceRecorder;
}}
#include <string>
#include <sstream>
namespace Gecode {
/// Support for tracing search
class SearchTracer {
friend class Search::WrapTraceRecorder;
friend class Search::TraceRecorder;
friend class Search::EdgeTraceRecorder;
public:
/// Which type of engine
enum EngineType {
DFS = 0, ///< Engine is a DFS engine
BAB = 1, ///< Engine is a BAB engine
LDS = 2, ///< Engine is a LDS engine
RBS = 3, ///< Engine is a RBS engine
PBS = 4, ///< Engine is a PBS engine
AOE = 5 ///< Unspecified engine (any other engine)
};
/// Information about an engine
class EngineInfo {
protected:
/// The engine type
EngineType _type;
/// First worker or engine
unsigned int _fst;
/// Last worker or engine
unsigned int _lst;
public:
/// Do not initialize
EngineInfo(void);
/// Initialize
EngineInfo(EngineType et, unsigned int fst, unsigned int lst);
/// \name Engine type information
//@{
/// Return engine type
EngineType type(void) const;
/// Return whether engine is a meta engine
bool meta(void) const;
//@}
/// \name Information for basic (non-meta) engines
//@{
/// Return id of first worker
unsigned int wfst(void) const;
/// Return id of last worker plus one
unsigned int wlst(void) const;
/// Return number of workers
unsigned int workers(void) const;
//@}
/// \name Information for meta engines
//@{
/// Return id of first engine
unsigned int efst(void) const;
/// Return id of last engine
unsigned int elst(void) const;
/// Return number of engines
unsigned int engines(void) const;
//@}
};
/// Edge information
class EdgeInfo {
protected:
/// The parent worker id (edge does not exist if UINT_MAX)
unsigned int _wid;
/// The parent node id
unsigned int _nid;
/// Number of alternative
unsigned int _a;
/// String corresponding to alternative
std::string _s;
public:
/// Initialize
void init(unsigned int wid, unsigned int nid, unsigned int a);
/// Initialize
void init(unsigned int wid, unsigned int nid, unsigned int a,
const Space& s, const Choice & c);
/// Invalidate edge information (for stealing)
void invalidate(void);
/// Initialize as non existing
EdgeInfo(void);
/// Initialize
EdgeInfo(unsigned int wid, unsigned int nid, unsigned int a);
/// Test whether edge actually exists
operator bool(void) const;
/// Return parent worker id
unsigned int wid(void) const;
/// Return parent node id
unsigned int nid(void) const;
/// Return number of alternative
unsigned int alternative(void) const;
/// Return string for alternative
std::string string(void) const;
};
/// Node type
enum NodeType {
SOLVED = 0, /// A solution node
FAILED = 1, /// A failed node
BRANCH = 2 /// A branch node
};
/// Node information
class NodeInfo {
protected:
/// The node type
NodeType _nt;
/// The worker id
unsigned int _wid;
/// The node id
unsigned int _nid;
/// The corresponding space
const Space& _s;
/// The corresponding choice (nullptr if type is not BRANCH)
const Choice* _c;
public:
/// Initialize node info
NodeInfo(NodeType nt,
unsigned int wid, unsigned int nid,
const Space& s, const Choice* c = nullptr);
/// Return node type
NodeType type(void) const;
/// Return worker id
unsigned int wid(void) const;
/// Return node id
unsigned int nid(void) const;
/// Return corresponding space
const Space& space(void) const;
/// Return corresponding choice
const Choice& choice(void) const;
};
private:
/// Mutex for serialized access
Support::Mutex m;
/// Number of pending engine and workers calls
unsigned int pending;
/// Number of engines
unsigned int n_e;
/// Number of workers
unsigned int n_w;
/// Number of active workers (for termination)
unsigned int n_active;
/// The engines
Support::DynamicArray<EngineInfo,Heap> es;
/// Mapping of workers to engines
Support::DynamicArray<unsigned int,Heap> w2e;
/// Register new engine
void engine(EngineType t, unsigned int n);
/// Register new worker
void worker(unsigned int& wid, unsigned int& eid);
/// Deregister a worker
void worker(void);
/// \name Unsynchronized internal calls
//{@
/// The engine with id \a eid goes to a next round (restart or next iteration in LDS)
void _round(unsigned int eid);
/// The engine skips an edge
void _skip(const EdgeInfo& ei);
/// The engine creates a new node with information \a ei and \a ni
void _node(const EdgeInfo& ei, const NodeInfo& ni);
//@}
public:
/// Initialize
SearchTracer(void);
/// \name Engine information
//@{
/// Return number of workers
unsigned int workers(void) const;
/// Return number of engines
unsigned int engines(void) const;
/// Provide access to engine with id \a eid
const EngineInfo& engine(unsigned int eid) const;
/// Return the engine id of a worker with id \a wid
unsigned int eid(unsigned int wid) const;
//@}
/// \name Trace event functions
//@{
/// The search engine initializes
virtual void init(void) = 0;
/// The engine with id \a eid goes to a next round (restart or next iteration in LDS)
virtual void round(unsigned int eid) = 0;
/// The engine skips an edge
virtual void skip(const EdgeInfo& ei) = 0;
/// The engine creates a new node with information \a ei and \a ni
virtual void node(const EdgeInfo& ei, const NodeInfo& ni) = 0;
/// All workers are done
virtual void done(void) = 0;
//@}
/// Delete
virtual ~SearchTracer(void);
};
class GECODE_SEARCH_EXPORT StdSearchTracer : public SearchTracer {
protected:
/// Output stream to use
std::ostream& os;
/// Map engine type to string
static const char* t2s[EngineType::AOE + 1];
public:
/// Initialize with output stream \a os
StdSearchTracer(std::ostream& os = std::cerr);
/// The search engine initializes
virtual void init(void);
/// The engine with id \a eid goes to a next round (restart or next iteration in LDS)
virtual void round(unsigned int eid);
/// The engine skips an edge
virtual void skip(const EdgeInfo& ei);
/// The engine creates a new node with information \a ei and \a ni
virtual void node(const EdgeInfo& ei, const NodeInfo& ni);
/// All workers are done
virtual void done(void);
/// Delete
virtual ~StdSearchTracer(void);
/// Default tracer (printing to std::cerr)
static StdSearchTracer def;
};
}
#include <gecode/search/tracer.hpp>
#include <gecode/search/trace-recorder.hpp>
#ifdef GECODE_HAS_CPPROFILER
namespace Gecode {
/// Code that is specific to the CPProfiler
namespace CPProfiler {}
}
namespace Gecode { namespace CPProfiler {
/// The actual connector to the CPProfiler
class Connector;
}}
namespace Gecode {
/// Class to record search trace info for CPProfiler
class GECODE_SEARCH_EXPORT CPProfilerSearchTracer : public SearchTracer {
public:
/// Class to send solution information to CPProfiler
class GECODE_SEARCH_EXPORT GetInfo : public HeapAllocated {
public:
/// Initialize
GetInfo(void);
/// Return info for a space
virtual std::string getInfo(const Space& home) const = 0;
/// Delete
virtual ~GetInfo(void);
};
private:
/// Connector to connect to running instance of CPProfiler
CPProfiler::Connector* connector;
/// Execution id to be displayed by CPProfiler
int execution_id;
/// Name of script being traced
std::string name;
/// Number of the current restart
int restart;
/// Send solution information to CPProfiler
const GetInfo* pgi;
public:
/// Initialize
CPProfilerSearchTracer(int eid, std::string name,
unsigned int port = Search::Config::cpprofiler_port,
const GetInfo* pgi = nullptr);
/// The search engine initializes
virtual void init(void);
/// The engine with id \a eid goes to a next round (restart or next iteration in LDS)
virtual void round(unsigned int eid);
/// The engine skips an edge
virtual void skip(const EdgeInfo& ei);
/// The engine creates a new node with information \a ei and \a ni
virtual void node(const EdgeInfo& ei, const NodeInfo& ni);
/// All workers are done
virtual void done(void);
/// Delete
virtual ~CPProfilerSearchTracer(void);
};
}
#endif
namespace Gecode { namespace Search {
/**
* \brief Base class for cutoff generators for restart-based meta engine
* \ingroup TaskModelSearch
*/
class GECODE_SEARCH_EXPORT Cutoff : public HeapAllocated {
public:
/// \name Constructors and member functions
//@{
/// Default constructor
Cutoff(void);
/// Return the current cutoff value
virtual unsigned long long int operator ()(void) const = 0;
/// Increment and return the next cutoff value
virtual unsigned long long int operator ++(void) = 0;
/// Destructor
virtual ~Cutoff(void);
//@}
/// \name Predefined cutoff generators
//@{
/// Create generator for constant sequence with constant \a s
static Cutoff*
constant(unsigned long long int scale=Config::slice);
/// Create generator for linear sequence scaled by \a scale
static Cutoff*
linear(unsigned long long int scale=Config::slice);
/** Create generator for geometric sequence scaled by
* \a scale using base \a base
*/
static Cutoff*
geometric(unsigned long long int scale=Config::slice,
double base=Config::base);
/// Create generator for luby sequence with scale-factor \a scale
static Cutoff*
luby(unsigned long long int scale=Config::slice);
/** Create generator for random sequence with seed \a seed that
* generates values between \a min and \a max with \a n steps
* between the extreme values.
*/
static Cutoff*
rnd(unsigned int seed,
unsigned long long int min, unsigned long long int max,
unsigned long long int n);
/// Append cutoff values from \a c2 after \a n values from \a c1
static Cutoff*
append(Cutoff* c1, unsigned long long int n, Cutoff* c2);
/// Merge cutoff values from \a c1 with values from \a c2
static Cutoff*
merge(Cutoff* c1, Cutoff* c2);
/// Create generator that repeats \a n times each cutoff value from \a c
static Cutoff*
repeat(Cutoff* c, unsigned long long int n);
//@}
};
/**
* \brief Cutoff generator for constant sequence
* \ingroup TaskModelSearch
*/
class GECODE_SEARCH_EXPORT CutoffConstant : public Cutoff {
protected:
/// Constant
unsigned long long int c;
public:
/// Constructor
CutoffConstant(unsigned long long int c);
/// Return the current cutoff value
virtual unsigned long long int operator ()(void) const;
/// Increment and return the next cutoff value
virtual unsigned long long int operator ++(void);
};
/**
* \brief Cutoff generator for linear sequence
* \ingroup TaskModelSearch
*/
class GECODE_SEARCH_EXPORT CutoffLinear : public Cutoff {
protected:
/// Scale factor
unsigned long long int scale;
/// Next number in sequence
unsigned long long int n;
public:
/// Constructor
CutoffLinear(unsigned long long int scale);
/// Return the current cutoff value
virtual unsigned long long int operator ()(void) const;
/// Increment and return the next cutoff value
virtual unsigned long long int operator ++(void);
};
/**
* \brief Cutoff generator for the Luby sequence
* \ingroup TaskModelSearch
*/
class GECODE_SEARCH_EXPORT CutoffLuby : public Cutoff {
protected:
/// Iteration number
unsigned long long int i;
/// Scale factor
unsigned long long int scale;
/// Number of pre-computed luby values
static const unsigned long long int n_start = 63U;
/// Precomputed luby-values
static unsigned long int start[n_start];
/// Compute binary logarithm of \a i
static unsigned long long int log(unsigned long long int i);
/// Compute Luby number for step \a i
static unsigned long long int luby(unsigned long long int i);
public:
/// Constructor
CutoffLuby(unsigned long long int scale);
/// Return the current cutoff value
virtual unsigned long long int operator ()(void) const;
/// Increment and return the next cutoff value
virtual unsigned long long int operator ++(void);
};
/**
* \brief Cutoff generator for the geometric sequence
* \ingroup TaskModelSearch
*/
class GECODE_SEARCH_EXPORT CutoffGeometric : public Cutoff {
protected:
/// Current cutoff value
double n;
/// Scale factor
double scale;
/// Base
double base;
public:
/// Constructor
CutoffGeometric(unsigned long long int scale, double base);
/// Return the current cutoff value
virtual unsigned long long int operator ()(void) const;
/// Increment and return the next cutoff value
virtual unsigned long long int operator ++(void);
};
/**
* \brief Cutoff generator for the random sequence
* \ingroup TaskModelSearch
*/
class GECODE_SEARCH_EXPORT CutoffRandom : public Cutoff {
protected:
/// Random number generator
Support::RandomGenerator rnd;
/// Minimum cutoff value
unsigned long long int min;
/// Random values
unsigned long long int n;
/// Step size
unsigned long long int step;
/// Current value
unsigned long long int cur;
public:
/// Constructor
CutoffRandom(unsigned int seed,
unsigned long long int min, unsigned long long int max,
unsigned long long int n);
/// Return the current cutoff value
virtual unsigned long long int operator ()(void) const;
/// Increment and return the next cutoff value
virtual unsigned long long int operator ++(void);
};
/**
* \brief Cutoff generator appending two cutoff generators
* \ingroup TaskModelSearch
*/
class GECODE_SEARCH_EXPORT CutoffAppend : public Cutoff {
protected:
/// First cutoff generators
Cutoff* c1;
/// Second cutoff generators
Cutoff* c2;
/// How many number to take from the first
unsigned long long int n;
public:
/// Constructor
CutoffAppend(Cutoff* c1, unsigned long long int n, Cutoff* c2);
/// Return the current cutoff value
virtual unsigned long long int operator ()(void) const;
/// Increment and return the next cutoff value
virtual unsigned long long int operator ++(void);
/// Destructor
virtual ~CutoffAppend(void);
};
/**
* \brief Cutoff generator merging two cutoff generators
* \ingroup TaskModelSearch
*/
class GECODE_SEARCH_EXPORT CutoffMerge : public Cutoff {
protected:
/// First cutoff generator
Cutoff* c1;
/// Second cutoff generator
Cutoff* c2;
public:
/// Constructor
CutoffMerge(Cutoff* c1, Cutoff* c2);
/// Return the current cutoff value
virtual unsigned long long int operator ()(void) const;
/// Increment and return the next cutoff value
virtual unsigned long long int operator ++(void);
/// Destructor
virtual ~CutoffMerge(void);
};
/**
* \brief Cutoff generator that repeats a cutoff from another cutoff generator
* \ingroup TaskModelSearch
*/
class GECODE_SEARCH_EXPORT CutoffRepeat : public Cutoff {
protected:
/// Actual cutoff generator
Cutoff* c;
// Current cutoff
unsigned long long int cutoff;
// Iteration
unsigned long long int i;
// Number of repetitions
unsigned long long int n;
public:
/// Constructor
CutoffRepeat(Cutoff* c, unsigned long long int n);
/// Return the current cutoff value
virtual unsigned long long int operator ()(void) const;
/// Increment and return the next cutoff value
virtual unsigned long long int operator ++(void);
/// Destructor
virtual ~CutoffRepeat(void);
};
}}
#include <gecode/search/cutoff.hpp>
namespace Gecode { namespace Search {
class Stop;
/**
* \brief %Search engine options
*
* Defines options for search engines. Not all search engines might
* honor all option values.
*
* - \a c_d as minimal recomputation distance: this guarantees that
* a path between two nodes in the search tree for which copies are
* stored has at least length \a c_d. That is, in order to recompute
* a node in the search tree, \a c_d recomputation steps are needed.
* The minimal recomputation distance yields a guarantee on saving
* memory compared to full copying: it stores \a c_d times less nodes
* than full copying.
* - \a a_d as adaptive recomputation distance: when a node needs to be
* recomputed and the path is longer than \a a_d, an intermediate copy
* is created (approximately in the middle of the path) to speed up
* future recomputation. Note that small values of \a a_d can increase
* the memory consumption considerably.
*
* Full copying corresponds to a maximal recomputation distance
* \a c_d of 1.
*
* All recomputation performed is based on batch recomputation: batch
* recomputation performs propagation only once for an entire path
* used in recomputation.
*
* The number of threads to be used is controlled by a double \f$n\f$
* (assume that \f$m\f$ is the number of processing units available). If
* \f$1 \leq n\f$, \f$n\f$ threads are chosen (of course with rounding).
* If \f$n \leq -1\f$, then \f$m + n\f$ threads are
* chosen (all but \f$-n\f$ processing units get a thread). If \f$n\f$
* is zero, \f$m\f$ threads are chosen. If \f$0<n<1\f$,
* \f$n \times m\f$ threads are chosen. If \f$-1 <n<0\f$,
* \f$(1+n)\times m\f$ threads are chosen.
*
* \ingroup TaskModelSearch
*/
class Options {
public:
/// Whether engines create a clone when being initialized
bool clone;
/// Number of threads to use
double threads;
/// Create a clone after every \a c_d commits (commit distance)
unsigned int c_d;
/// Create a clone during recomputation if distance is greater than \a a_d (adaptive distance)
unsigned int a_d;
/// Discrepancy limit (for LDS)
unsigned int d_l;
/// Number of assets (engines) in a portfolio
unsigned int assets;
/// Size of a slice in a portfolio (in number of failures)
unsigned int slice;
/// Depth limit for extraction of no-goods
unsigned int nogoods_limit;
/// Stop object for stopping search
Stop* stop;
/// Cutoff for restart-based search
Cutoff* cutoff;
/// Tracer object for tracing search
SearchTracer* tracer;
/// Default options
GECODE_SEARCH_EXPORT static const Options def;
/// Initialize with default values
Options(void);
/// Expand with real number of threads
GECODE_SEARCH_EXPORT Options
expand(void) const;
};
}}
#include <gecode/search/options.hpp>
namespace Gecode { namespace Search {
/**
* \defgroup TaskModelSearchStop Stop-objects for stopping search
* \ingroup TaskModelSearch
*
* Allows to specify various criteria when a search engine should
* stop exploration. Only exploration but neither recomputation
* nor propagation will be interrupted.
*
*/
/**
* \brief Base-class for %Stop-object
* \ingroup TaskModelSearchStop
*/
class GECODE_SEARCH_EXPORT Stop : public HeapAllocated {
public:
/// \name Constructors and member functions
//@{
/// Default constructor
Stop(void);
/// Stop search, if returns true
virtual bool stop(const Statistics& s, const Options& o) = 0;
/// Destructor
virtual ~Stop(void);
//@}
/// \name Predefined stop objects
//@{
/// Stop if node limit \a l has been exceeded
static Stop* node(unsigned long long int l);
/// Stop if failure limit \a l has been exceeded
static Stop* fail(unsigned long long int l);
/// Stop if time limit \a l (in milliseconds) has been exceeded
static Stop* time(double l);
/// Stop if restart limit \a l has been exceeded
static Stop* restart(unsigned long long int l);
//@}
};
/**
* \brief %Stop-object based on number of nodes
*
* The number of nodes reported (by the statistics) is the
* number since the engine started exploration. It is not the
* number since the last stop!
* \ingroup TaskModelSearchStop
*/
class GECODE_SEARCH_EXPORT NodeStop : public Stop {
protected:
/// Node limit
unsigned long long int l;
public:
/// Stop if node limit \a l is exceeded
NodeStop(unsigned long long int l);
/// Return current limit
unsigned long long int limit(void) const;
/// Set current limit to \a l nodes
void limit(unsigned long long int l);
/// Return true if node limit is exceeded
virtual bool stop(const Statistics& s, const Options& o);
};
/**
* \brief %Stop-object based on number of failures
*
* The number of failures reported (by the statistics) is the
* number since the engine started exploration. It is not the
* number since the last stop!
* \ingroup TaskModelSearchStop
*/
class GECODE_SEARCH_EXPORT FailStop : public Stop {
protected:
/// Failure limit
unsigned long long int l;
public:
/// Stop if failure limit \a l is exceeded
FailStop(unsigned long long int l);
/// Return current limit
unsigned long long int limit(void) const;
/// Set current limit to \a l failures
void limit(unsigned long long int l);
/// Return true if failure limit is exceeded
virtual bool stop(const Statistics& s, const Options& o);
};
/**
* \brief %Stop-object based on time
* \ingroup TaskModelSearchStop
*/
class GECODE_SEARCH_EXPORT TimeStop : public Stop {
protected:
/// Time when execution should stop
Support::Timer t;
/// Current limit in milliseconds
double l;
public:
/// Stop if search exceeds \a l milliseconds (from creation of this object)
TimeStop(double l);
/// Return current limit in milliseconds
double limit(void) const;
/// Set current limit to \a l milliseconds
void limit(double l);
/// Reset time to zero
void reset(void);
/// Return true if time limit is exceeded
virtual bool stop(const Statistics& s, const Options& o);
};
/**
* \brief %Stop-object based on number of restarts
* \ingroup TaskModelSearchStop
*/
class GECODE_SEARCH_EXPORT RestartStop : public Stop {
protected:
/// Restart limit
unsigned long long int l;
public:
/// Stop if restart limit \a l is exceeded
RestartStop(unsigned long long int l);
/// Return current limit
unsigned long long int limit(void) const;
/// Set current limit to \a l restarts
void limit(unsigned long long int l);
/// Return true if failure limit is exceeded
virtual bool stop(const Statistics& s, const Options& o);
};
}}
#include <gecode/search/stop.hpp>
namespace Gecode { namespace Search {
/**
* \brief %Search engine implementation interface
*/
class GECODE_SEARCH_EXPORT Engine : public HeapAllocated {
public:
/// Return next solution (nullptr, if none exists or search has been stopped)
virtual Space* next(void) = 0;
/// Return statistics
virtual Statistics statistics(void) const = 0;
/// Check whether engine has been stopped
virtual bool stopped(void) const = 0;
/// Constrain future solutions to be better than \a b (raises exception)
virtual void constrain(const Space& b);
/// Reset engine to restart at space \a s (does nothing)
virtual void reset(Space* s);
/// Return no-goods (the no-goods are empty)
virtual NoGoods& nogoods(void);
/// Destructor
virtual ~Engine(void);
};
}}
#include <gecode/search/engine.hpp>
namespace Gecode { namespace Search {
/// Base-class for search engines
template<class T>
class Base : public HeapAllocated {
template<class, class>
friend Engine* build(Space*, const Options&);
template<class, template<class> class>
friend Engine* build(Space*, const Options&);
protected:
/// The actual search engine
Engine* e;
/// Constructor
Base(Engine* e = nullptr);
public:
/// Return next solution (nullptr, if none exists or search has been stopped)
virtual T* next(void);
/// Return statistics
virtual Statistics statistics(void) const;
/// Check whether engine has been stopped
virtual bool stopped(void) const;
/// Destructor
virtual ~Base(void);
private:
/// Disallow copy constructor
Base(const Base&);
/// Disallow assignment operator
Base& operator =(const Base&);
};
}}
#include <gecode/search/base.hpp>
namespace Gecode { namespace Search {
/// Build an engine of type \a E for a script \a T
template<class T, class E>
Engine* build(Space* s, const Options& opt);
/// Build a parametric engine of type \a E for a script \a T
template<class T, template<class> class E>
Engine* build(Space* s, const Options& opt);
/// A class for building search engines
class GECODE_SEARCH_EXPORT Builder : public HeapAllocated {
protected:
/// Stored and already expanded options
Options opt;
/// Whether engine to be built is a best solution search engine
const bool b;
public:
/// Initialize with options \a opt and \a best solution search support
Builder(const Options& opt, bool best);
/// Provide access to options
Options& options(void);
/// Provide access to options
const Options& options(void) const;
/// Whether engine is a best solution search engine
bool best(void) const;
/// Build an engine according to stored options for \a s
virtual Engine* operator() (Space* s) const = 0;
/// Destructor
virtual ~Builder(void);
};
}}
#include <gecode/search/build.hpp>
namespace Gecode {
/// Type for a search engine builder
typedef Search::Builder* SEB;
}
#include <gecode/search/traits.hpp>
namespace Gecode {
/// Passing search engine builder arguments
class SEBs : public ArgArray<SEB> {
public:
/// \name Constructors and initialization
//@{
/// Allocate empty array
SEBs(void);
/// Allocate array with \a n elements
explicit SEBs(int n);
/// Allocate array and copy elements from \a x
SEBs(const std::vector<SEB>& x);
/// Allocate array with and initialize with \a x
SEBs(std::initializer_list<SEB> x);
/// Allocate array and copy elements from \a first to \a last
template<class InputIterator>
SEBs(InputIterator first, InputIterator last);
/// Initialize from primitive argument array \a a (copy elements)
SEBs(const ArgArray<SEB>& a);
//@}
};
}
#include <gecode/search/sebs.hpp>
namespace Gecode {
/**
* \brief Depth-first search engine
*
* This class supports depth-first search for subclasses \a T of
* Space.
* \ingroup TaskModelSearch
*/
template<class T>
class DFS : public Search::Base<T> {
public:
/// Initialize search engine for space \a s with options \a o
DFS(T* s, const Search::Options& o=Search::Options::def);
/// Whether engine does best solution search
static const bool best = false;
};
/// Invoke depth-first search engine for subclass \a T of space \a s with options \a o
template<class T>
T* dfs(T* s, const Search::Options& o=Search::Options::def);
/// Return a depth-first search engine builder
template<class T>
SEB dfs(const Search::Options& o=Search::Options::def);
}
#include <gecode/search/dfs.hpp>
namespace Gecode {
/**
* \brief Depth-first branch-and-bound search engine
*
* Additionally, \a s must implement a member function
* \code virtual void constrain(const T& t) \endcode
* Whenever exploration requires to add a constraint
* to the space \a c currently being explored, the engine
* executes \c c.constrain(t) where \a t is the so-far
* best solution.
* \ingroup TaskModelSearch
*/
template<class T>
class BAB : public Search::Base<T> {
public:
/// Initialize engine for space \a s and options \a o
BAB(T* s, const Search::Options& o=Search::Options::def);
/// Whether engine does best solution search
static const bool best = true;
};
/**
* \brief Perform depth-first branch-and-bound search for subclass \a T of space \a s and options \a o
*
* Additionally, \a s must implement a member function
* \code virtual void constrain(const T& t) \endcode
* Whenever exploration requires to add a constraint
* to the space \a c currently being explored, the engine
* executes \c c.constrain(t) where \a t is the so-far
* best solution.
*
* \ingroup TaskModelSearch
*/
template<class T>
T* bab(T* s, const Search::Options& o=Search::Options::def);
/// Return a depth-first branch-and-bound search engine builder
template<class T>
SEB bab(const Search::Options& o=Search::Options::def);
}
#include <gecode/search/bab.hpp>
namespace Gecode {
/**
* \brief Limited discrepancy search engine
* \ingroup TaskModelSearch
*/
template<class T>
class LDS : public Search::Base<T> {
public:
/// Initialize engine for space \a s and options \a o
LDS(T* s, const Search::Options& o=Search::Options::def);
/// Whether engine does best solution search
static const bool best = false;
};
/**
* \brief Invoke limited-discrepancy search for \a s as root node and options\a o
* \ingroup TaskModelSearch
*/
template<class T>
T* lds(T* s, const Search::Options& o=Search::Options::def);
/// Return a limited discrepancy search engine builder
template<class T>
SEB lds(const Search::Options& o=Search::Options::def);
}
#include <gecode/search/lds.hpp>
namespace Gecode {
/**
* \brief Meta-engine performing restart-based search
*
* The engine uses the Cutoff sequence supplied in the options \a o to
* periodically restart the search of engine \a E.
*
* The class \a T can implement member functions
* \code virtual bool master(const MetaInfo& mi) \endcode
* and
* \code virtual bool slave(const MetaInfo& mi) \endcode
*
* Whenever exploration restarts or a solution is found, the
* engine executes the functions on the master and slave
* space. For more details, consult "Modeling and Programming
* with Gecode".
*
* \ingroup TaskModelSearch
*/
template<class T, template<class> class E = DFS>
class RBS : public Search::Base<T> {
using Search::Base<T>::e;
public:
/// Initialize engine for space \a s and options \a o
RBS(T* s, const Search::Options& o);
/// Whether engine does best solution search
static const bool best = E<T>::best;
};
/**
* \brief Perform restart-based search
*
* The engine uses the Cutoff sequence supplied in the options \a o to
* periodically restart the search of engine \a E.
*
* The class \a T can implement member functions
* \code virtual bool master(const MetaInfo& mi) \endcode
* and
* \code virtual bool slave(const MetaInfo& mi) \endcode
*
* Whenever exploration restarts or a solution is found, the
* engine executes the functions on the master and slave
* space. For more details, consult "Modeling and Programming
* with Gecode".
*
* \ingroup TaskModelSearch
*/
template<class T, template<class> class E>
T* rbs(T* s, const Search::Options& o);
/// Return a restart search engine builder
template<class T, template<class> class E>
SEB rbs(const Search::Options& o);
}
#include <gecode/search/rbs.hpp>
namespace Gecode { namespace Search { namespace Meta {
/// Build a sequential engine
template<class T, template<class> class E>
Engine* sequential(T* master, const Search::Statistics& stat, Options& opt);
/// Build a sequential engine
template<class T, template<class> class E>
Engine* sequential(T* master, SEBs& sebs,
const Search::Statistics& stat, Options& opt, bool best);
#ifdef GECODE_HAS_THREADS
/// Build a parallel engine
template<class T, template<class> class E>
Engine* parallel(T* master, const Search::Statistics& stat, Options& opt);
/// Build a parallel engine
template<class T, template<class> class E>
Engine* parallel(T* master, SEBs& sebs,
const Search::Statistics& stat, Options& opt, bool best);
#endif
}}}
namespace Gecode {
/**
* \brief Meta engine using a portfolio of search engines
*
* The engine will run a portfolio with a number of assets as defined
* by the options \a o. The engine supports parallel execution of
* assets by using the number of threads as defined by the options.
*
* The class \a T can implement member functions
* \code virtual bool master(const MetaInfo& mi) \endcode
* and
* \code virtual bool slave(const MetaInfo& mi) \endcode
*
* When the assets are created, these functions are executed.
* For more details, consult "Modeling and Programming with Gecode".
*
* \ingroup TaskModelSearch
*/
template<class T, template<class> class E = DFS>
class PBS : public Search::Base<T> {
using Search::Base<T>::e;
protected:
/// The actual build function
void build(T* s, SEBs& sebs, const Search::Options& o);
public:
/// Initialize with engines running copies of \a s with options \a o
PBS(T* s, const Search::Options& o=Search::Options::def);
/// Initialize with engine builders \a sebs
PBS(T* s, SEBs& sebs, const Search::Options& o=Search::Options::def);
/// Whether engine does best solution search
static const bool best = E<T>::best;
};
/**
* \brief Run a portfolio of search engines
*
* The engine will run a portfolio with a number of assets as defined
* by the options \a o. The engine supports parallel execution of
* assets by using the number of threads as defined by the options.
*
* The class \a T can implement member functions
* \code virtual bool master(const MetaInfo& mi) \endcode
* and
* \code virtual bool slave(const MetaInfo& mi) \endcode
*
* When the assets are created, these functions are executed.
* For more details, consult "Modeling and Programming with Gecode".
*
* \ingroup TaskModelSearch
*/
template<class T, template<class> class E>
T* pbs(T* s, const Search::Options& o=Search::Options::def);
/// Return a portfolio search engine builder
template<class T>
SEB pbs(const Search::Options& o=Search::Options::def);
}
#include <gecode/search/pbs.hpp>
#endif
// STATISTICS: search-other
|