00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 #ifndef OR_TOOLS_CONSTRAINT_SOLVER_CONSTRAINT_SOLVER_H_
00064 #define OR_TOOLS_CONSTRAINT_SOLVER_CONSTRAINT_SOLVER_H_
00065
00066 #include "base/hash.h"
00067 #include "base/hash.h"
00068 #include <iosfwd>
00069 #include <string>
00070 #include <utility>
00071 #include <vector>
00072
00073 #include "base/callback.h"
00074 #include "base/commandlineflags.h"
00075 #include "base/integral_types.h"
00076 #include "base/logging.h"
00077 #include "base/macros.h"
00078 #include "base/scoped_ptr.h"
00079 #include "base/stringprintf.h"
00080 #include "base/sysinfo.h"
00081 #include "base/timer.h"
00082 #include "base/concise_iterator.h"
00083 #include "base/map-util.h"
00084 #include "base/hash.h"
00085 #include "base/random.h"
00086 #include "util/tuple_set.h"
00087
00088 class Closure;
00089 class File;
00090 template <class A1, class A2, class A3> class Callback3;
00091 template <typename R, typename T1, typename T2, typename T3>
00092 class ResultCallback3;
00093 template <typename R, typename T1, typename T2> class ResultCallback2;
00094 template <typename R, typename T1> class ResultCallback1;
00095 template <typename T1> class Callback1;
00096 template <typename T> class ResultCallback;
00097
00098 using operations_research::WallTimer;
00099 #define ClockTimer WallTimer
00100
00101 using std::string;
00102
00103 namespace operations_research {
00104
00105 class Action;
00106 class Assignment;
00107 class AssignmentProto;
00108 class BaseObject;
00109 class CPArgumentProto;
00110 class CPConstraintProto;
00111 class CPIntegerExpressionProto;
00112 class CPIntervalVariableProto;
00113 class CPModelLoader;
00114 class CPModelProto;
00115 class CPSequenceVariableProto;
00116 class CastConstraint;
00117 class ClockTimer;
00118 class ConstIntArray;
00119 class Constraint;
00120 class Decision;
00121 class DecisionBuilder;
00122 class DecisionVisitor;
00123 class Demon;
00124 class DemonProfiler;
00125 class DemonProfiler;
00126 class DependencyGraph;
00127 class Dimension;
00128 class ExpressionCache;
00129 class IntExpr;
00130 class IntTupleSet;
00131 class IntVar;
00132 class IntVarAssignmentProto;
00133 class IntVarElement;
00134 class IntervalVar;
00135 class IntervalVarAssignmentProto;
00136 class IntervalVarElement;
00137 class LocalSearchFilter;
00138 class LocalSearchOperator;
00139 class LocalSearchPhaseParameters;
00140 class MPSolver;
00141 class ModelCache;
00142 class ModelVisitor;
00143 class NoGoodManager;
00144 class NoGoodTerm;
00145 class OptimizeVar;
00146 class Pack;
00147 class PropagationBaseObject;
00148 class PropagationMonitor;
00149 class Queue;
00150 class RevBitMatrix;
00151 class RevBitSet;
00152 class Search;
00153 class SearchLimit;
00154 class SearchLimitProto;
00155 class SearchMonitor;
00156 class SequenceVar;
00157 class SequenceVarAssignmentProto;
00158 class SolutionCollector;
00159 class SolutionPool;
00160 class Solver;
00161 class SymmetryBreaker;
00162 struct StateInfo;
00163 struct Trail;
00164 template <class T> class ConstPtrArray;
00165 template <class T> class SimpleRevFIFO;
00166
00167
00168
00169
00170
00171 struct SolverParameters {
00172 public:
00173 enum TrailCompression {
00174 NO_COMPRESSION, COMPRESS_WITH_ZLIB
00175 };
00176
00177 enum ProfileLevel {
00178 NO_PROFILING,
00179 NORMAL_PROFILING
00180 };
00181
00182 enum TraceLevel {
00183 NO_TRACE,
00184 NORMAL_TRACE
00185 };
00186
00187 static const TrailCompression kDefaultTrailCompression;
00188 static const int kDefaultTrailBlockSize;
00189 static const int kDefaultArraySplitSize;
00190 static const bool kDefaultNameStoring;
00191 static const ProfileLevel kDefaultProfileLevel;
00192 static const TraceLevel kDefaultTraceLevel;
00193 static const bool kDefaultNameAllVariables;
00194
00195 SolverParameters();
00196
00197
00198
00199
00200 TrailCompression compress_trail;
00201
00202
00203
00204 int trail_block_size;
00205
00206
00207
00208 int array_split_size;
00209
00210
00211
00212 bool store_names;
00213
00214
00215
00216
00217 ProfileLevel profile_level;
00218
00219
00220 TraceLevel trace_level;
00221
00222
00223 bool name_all_variables;
00224 };
00225
00226
00227
00228
00229 struct DefaultPhaseParameters {
00230 public:
00231 static const int kDefaultNumberOfSplits;
00232 static const int kDefaultHeuristicPeriod;
00233 static const int kDefaultHeuristicNumFailuresLimit;
00234 static const int kDefaultSeed;
00235 static const double kDefaultRestartLogSize;
00236 static const bool kDefaultUseNoGoods;
00237
00238 enum VariableSelection {
00239 CHOOSE_MAX_SUM_IMPACT = 0,
00240 CHOOSE_MAX_AVERAGE_IMPACT = 1,
00241 CHOOSE_MAX_VALUE_IMPACT = 2,
00242 };
00243
00244 enum ValueSelection {
00245 SELECT_MIN_IMPACT = 0,
00246 SELECT_MAX_IMPACT = 1,
00247 };
00248
00249 enum DisplayLevel {
00250 NONE = 0,
00251 NORMAL = 1,
00252 VERBOSE = 2
00253 };
00254
00255 DefaultPhaseParameters()
00256 : var_selection_schema(CHOOSE_MAX_SUM_IMPACT),
00257 value_selection_schema(SELECT_MIN_IMPACT),
00258 initialization_splits(kDefaultNumberOfSplits),
00259 run_all_heuristics(true),
00260 heuristic_period(kDefaultHeuristicPeriod),
00261 heuristic_num_failures_limit(kDefaultHeuristicNumFailuresLimit),
00262 persistent_impact(true),
00263 random_seed(kDefaultSeed),
00264 restart_log_size(kDefaultRestartLogSize),
00265 display_level(NORMAL),
00266 use_no_goods(kDefaultUseNoGoods),
00267 use_impacts(true) {}
00268
00269
00270
00271 VariableSelection var_selection_schema;
00272
00273
00274 ValueSelection value_selection_schema;
00275
00276
00277
00278 int initialization_splits;
00279
00280
00281
00282
00283 bool run_all_heuristics;
00284
00285
00286 int heuristic_period;
00287
00288
00289 int heuristic_num_failures_limit;
00290
00291
00292
00293 bool persistent_impact;
00294
00295
00296 int random_seed;
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307 double restart_log_size;
00308
00309
00310
00311 DisplayLevel display_level;
00312
00313
00314 bool use_no_goods;
00315
00316
00317 bool use_impacts;
00318 };
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342 class Solver {
00343 public:
00344
00345 typedef ResultCallback1<int64, int64> IndexEvaluator1;
00346 typedef ResultCallback2<int64, int64, int64> IndexEvaluator2;
00347 typedef ResultCallback3<int64, int64, int64, int64> IndexEvaluator3;
00348 typedef ResultCallback2<IntExpr*,
00349 CPModelLoader*,
00350 const CPIntegerExpressionProto&>
00351 IntegerExpressionBuilder;
00352 typedef ResultCallback2<Constraint*,
00353 CPModelLoader*,
00354 const CPConstraintProto&> ConstraintBuilder;
00355 typedef ResultCallback2<IntervalVar*,
00356 CPModelLoader*,
00357 const CPIntervalVariableProto&>
00358 IntervalVariableBuilder;
00359 typedef ResultCallback2<SequenceVar*,
00360 CPModelLoader*,
00361 const CPSequenceVariableProto&>
00362 SequenceVariableBuilder;
00363
00364
00365
00366
00367
00368 struct IntegerCastInfo {
00369 IntegerCastInfo() : variable(NULL), expression(NULL), maintainer(NULL) {}
00370 IntegerCastInfo(IntVar* const v, IntExpr* const e, Constraint* const c)
00371 : variable(v), expression(e), maintainer(c) {}
00372 IntVar* variable;
00373 IntExpr* expression;
00374 Constraint* maintainer;
00375 };
00376
00377
00378 static const int kNumPriorities = 3;
00379
00380
00381
00382 enum IntVarStrategy {
00383
00384 INT_VAR_DEFAULT,
00385
00386
00387 INT_VAR_SIMPLE,
00388
00389
00390
00391
00392 CHOOSE_FIRST_UNBOUND,
00393
00394
00395 CHOOSE_RANDOM,
00396
00397
00398
00399
00400
00401
00402
00403 CHOOSE_MIN_SIZE_LOWEST_MIN,
00404
00405
00406
00407
00408
00409
00410
00411 CHOOSE_MIN_SIZE_HIGHEST_MIN,
00412
00413
00414
00415
00416
00417
00418
00419 CHOOSE_MIN_SIZE_LOWEST_MAX,
00420
00421
00422
00423
00424
00425
00426
00427 CHOOSE_MIN_SIZE_HIGHEST_MAX,
00428
00429
00430
00431 CHOOSE_PATH,
00432 };
00433
00434
00435
00436 enum IntValueStrategy {
00437
00438 INT_VALUE_DEFAULT,
00439
00440
00441 INT_VALUE_SIMPLE,
00442
00443
00444 ASSIGN_MIN_VALUE,
00445
00446
00447 ASSIGN_MAX_VALUE,
00448
00449
00450 ASSIGN_RANDOM_VALUE,
00451
00452
00453
00454
00455 ASSIGN_CENTER_VALUE,
00456 };
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468 enum EvaluatorStrategy {
00469
00470
00471
00472
00473 CHOOSE_STATIC_GLOBAL_BEST,
00474
00475
00476
00477
00478
00479 CHOOSE_DYNAMIC_GLOBAL_BEST,
00480 };
00481
00482
00483 enum SequenceStrategy {
00484 SEQUENCE_DEFAULT,
00485 SEQUENCE_SIMPLE,
00486 CHOOSE_MIN_SLACK_RANK_FORWARD,
00487 CHOOSE_RANDOM_RANK_FORWARD,
00488 };
00489
00490
00491 enum IntervalStrategy {
00492 INTERVAL_DEFAULT,
00493 INTERVAL_SIMPLE,
00494 INTERVAL_SET_TIMES_FORWARD
00495 };
00496
00497
00498
00499 enum LocalSearchOperators {
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509 TWOOPT,
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525 OROPT,
00526
00527
00528 RELOCATE,
00529
00530
00531
00532
00533
00534
00535
00536
00537 EXCHANGE,
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548 CROSS,
00549
00550
00551
00552
00553
00554
00555
00556 MAKEACTIVE,
00557
00558
00559
00560
00561
00562
00563 MAKEINACTIVE,
00564
00565
00566
00567
00568
00569
00570 SWAPACTIVE,
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582 EXTENDEDSWAPACTIVE,
00583
00584
00585
00586
00587
00588
00589
00590
00591 PATHLNS,
00592
00593
00594
00595
00596 UNACTIVELNS,
00597
00598
00599
00600
00601
00602
00603
00604
00605 INCREMENT,
00606
00607
00608
00609
00610 DECREMENT,
00611
00612
00613
00614
00615
00616
00617
00618
00619 SIMPLELNS
00620 };
00621
00622
00623
00624 enum EvaluatorLocalSearchOperators {
00625
00626
00627
00628
00629 LK,
00630
00631
00632
00633
00634
00635
00636
00637 TSPOPT,
00638
00639
00640
00641
00642
00643
00644
00645 TSPLNS
00646 };
00647
00648
00649
00650
00651
00652 enum LocalSearchFilterBound {
00653
00654 GE,
00655
00656 LE,
00657
00658
00659 EQ
00660 };
00661
00662
00663
00664 enum LocalSearchOperation {
00665
00666
00667 SUM,
00668
00669
00670
00671 PROD,
00672
00673
00674
00675 MAX,
00676
00677
00678
00679 MIN
00680 };
00681
00682
00683
00684
00685 enum DemonPriority {
00686
00687
00688 DELAYED_PRIORITY = 0,
00689
00690
00691 VAR_PRIORITY = 1,
00692
00693
00694 NORMAL_PRIORITY = 2,
00695 };
00696
00697
00698
00699 enum BinaryIntervalRelation {
00700
00701 ENDS_AFTER_END,
00702
00703
00704 ENDS_AFTER_START,
00705
00706
00707 ENDS_AT_END,
00708
00709
00710 ENDS_AT_START,
00711
00712
00713 STARTS_AFTER_END,
00714
00715
00716 STARTS_AFTER_START,
00717
00718
00719 STARTS_AT_END,
00720
00721
00722 STARTS_AT_START,
00723
00724
00725
00726
00727 STAYS_IN_SYNC
00728 };
00729
00730
00731
00732 enum UnaryIntervalRelation {
00733
00734 ENDS_AFTER,
00735
00736
00737 ENDS_AT,
00738
00739
00740 ENDS_BEFORE,
00741
00742
00743 STARTS_AFTER,
00744
00745
00746 STARTS_AT,
00747
00748
00749 STARTS_BEFORE,
00750
00751
00752
00753
00754 CROSS_DATE,
00755
00756
00757
00758
00759 AVOID_DATE
00760 };
00761
00762
00763
00764
00765
00766
00767 enum DecisionModification {
00768
00769
00770 NO_CHANGE,
00771
00772
00773
00774
00775 KEEP_LEFT,
00776
00777
00778
00779
00780 KEEP_RIGHT,
00781
00782
00783
00784 KILL_BOTH,
00785
00786
00787
00788 SWITCH_BRANCHES
00789 };
00790
00791
00792
00793 enum MarkerType {
00794 SENTINEL,
00795 SIMPLE_MARKER,
00796 CHOICE_POINT,
00797 REVERSIBLE_ACTION
00798 };
00799
00800
00801 enum SolverState {
00802 OUTSIDE_SEARCH,
00803 IN_ROOT_NODE,
00804 IN_SEARCH,
00805 AT_SOLUTION,
00806 NO_MORE_SOLUTIONS,
00807 PROBLEM_INFEASIBLE
00808 };
00809
00810 explicit Solver(const string& modelname);
00811 Solver(const string& modelname, const SolverParameters& parameters);
00812 ~Solver();
00813
00814
00815 const SolverParameters& parameters() const { return parameters_; }
00816
00817
00818
00819
00820
00821
00822 template <class T> void SaveValue(T* o) {
00823 InternalSaveValue(o);
00824 }
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838 template <typename T> T* RevAlloc(T* object) {
00839 return reinterpret_cast<T*>(SafeRevAlloc(object));
00840 }
00841
00842
00843
00844
00845
00846
00847
00848 template <typename T> T* RevAllocArray(T* object) {
00849 return reinterpret_cast<T*>(SafeRevAllocArray(object));
00850 }
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887 void AddConstraint(Constraint* const c);
00888
00889
00890
00891 void AddCastConstraint(CastConstraint* const c,
00892 IntVar* const target_var,
00893 IntExpr* const casted_expression);
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934 bool Solve(DecisionBuilder* const db, const std::vector<SearchMonitor*>& monitors);
00935 bool Solve(DecisionBuilder* const db,
00936 SearchMonitor* const * monitors, int size);
00937 bool Solve(DecisionBuilder* const db);
00938 bool Solve(DecisionBuilder* const db, SearchMonitor* const m1);
00939 bool Solve(DecisionBuilder* const db,
00940 SearchMonitor* const m1,
00941 SearchMonitor* const m2);
00942 bool Solve(DecisionBuilder* const db,
00943 SearchMonitor* const m1,
00944 SearchMonitor* const m2,
00945 SearchMonitor* const m3);
00946 bool Solve(DecisionBuilder* const db,
00947 SearchMonitor* const m1,
00948 SearchMonitor* const m2,
00949 SearchMonitor* const m3,
00950 SearchMonitor* const m4);
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961 void NewSearch(DecisionBuilder* const db,
00962 const std::vector<SearchMonitor*>& monitors);
00963 void NewSearch(DecisionBuilder* const db,
00964 SearchMonitor* const * monitors, int size);
00965 void NewSearch(DecisionBuilder* const db);
00966 void NewSearch(DecisionBuilder* const db, SearchMonitor* const m1);
00967 void NewSearch(DecisionBuilder* const db,
00968 SearchMonitor* const m1,
00969 SearchMonitor* const m2);
00970 void NewSearch(DecisionBuilder* const db,
00971 SearchMonitor* const m1,
00972 SearchMonitor* const m2,
00973 SearchMonitor* const m3);
00974 void NewSearch(DecisionBuilder* const db,
00975 SearchMonitor* const m1,
00976 SearchMonitor* const m2,
00977 SearchMonitor* const m3,
00978 SearchMonitor* const m4);
00979
00980 bool NextSolution();
00981 void RestartSearch();
00982 void EndSearch();
00983
00984
00985
00986
00987
00988
00989
00990
00991 bool NestedSolve(DecisionBuilder* const db,
00992 bool restore,
00993 const std::vector<SearchMonitor*>& monitors);
00994 bool NestedSolve(DecisionBuilder* const db,
00995 bool restore,
00996 SearchMonitor* const * monitors,
00997 int size);
00998 bool NestedSolve(DecisionBuilder* const db, bool restore);
00999 bool NestedSolve(DecisionBuilder* const db,
01000 bool restore,
01001 SearchMonitor* const m1);
01002 bool NestedSolve(DecisionBuilder* const db,
01003 bool restore,
01004 SearchMonitor* const m1, SearchMonitor* const m2);
01005 bool NestedSolve(DecisionBuilder* const db,
01006 bool restore,
01007 SearchMonitor* const m1,
01008 SearchMonitor* const m2,
01009 SearchMonitor* const m3);
01010
01011
01012 bool CheckAssignment(Assignment* const assignment);
01013
01014
01015
01016
01017 bool CheckConstraint(Constraint* const constraint);
01018
01019
01020 SolverState state() const { return state_; }
01021
01022
01023 void Fail();
01024
01025
01026
01027 void ExportModel(CPModelProto* const proto) const;
01028
01029
01030 void ExportModel(const std::vector<SearchMonitor*>& monitors,
01031 CPModelProto* const proto) const;
01032
01033 bool LoadModel(const CPModelProto& proto);
01034
01035
01036 bool LoadModel(const CPModelProto& proto, std::vector<SearchMonitor*>* monitors);
01037
01038 static bool UpgradeModel(CPModelProto* const proto);
01039
01040 #if !defined(SWIG)
01041
01042
01043
01044
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055 bool CollectDecisionVariables(
01056 std::vector<IntVar*>* const primary_integer_variables,
01057 std::vector<IntVar*>* const secondary_integer_variables,
01058 std::vector<SequenceVar*>* const sequence_variables,
01059 std::vector<IntervalVar*>* const interval_variables);
01060 #endif // SWIG
01061
01062
01063 void RegisterBuilder(const string& tag,
01064 ConstraintBuilder* const builder);
01065
01066 void RegisterBuilder(const string& tag,
01067 IntegerExpressionBuilder* const builder);
01068
01069 void RegisterBuilder(const string& tag,
01070 IntervalVariableBuilder* const builder);
01071
01072 void RegisterBuilder(const string& tag,
01073 SequenceVariableBuilder* const builder);
01074
01075 ConstraintBuilder* GetConstraintBuilder(const string& tag) const;
01076 IntegerExpressionBuilder*
01077 GetIntegerExpressionBuilder(const string& tag) const;
01078 IntervalVariableBuilder* GetIntervalVariableBuilder(const string& tag) const;
01079 SequenceVariableBuilder* GetSequenceVariableBuilder(const string& tag) const;
01080
01081
01082
01083
01084
01085
01086 void AddBacktrackAction(Action* a, bool fast);
01087
01088
01089 string DebugString() const;
01090
01091
01092 static int64 MemoryUsage();
01093
01094
01095
01096 int64 wall_time() const;
01097
01098
01099 int64 branches() const { return branches_; }
01100
01101
01102 int64 solutions() const;
01103
01104
01105 int64 demon_runs(DemonPriority p) const { return demon_runs_[p]; }
01106
01107
01108 int64 failures() const { return fails_; }
01109
01110
01111 int64 neighbors() const { return neighbors_; }
01112
01113
01114 int64 filtered_neighbors() const { return filtered_neighbors_; }
01115
01116
01117 int64 accepted_neighbors() const { return accepted_neighbors_; }
01118
01119
01120
01121 uint64 stamp() const;
01122
01123
01124 uint64 fail_stamp() const;
01125
01126
01127
01128
01129
01130
01131
01132
01133
01134
01135
01136
01137 IntVar* MakeIntVar(int64 vmin, int64 vmax, const string& name);
01138
01139
01140 IntVar* MakeIntVar(const std::vector<int64>& values, const string& name);
01141
01142
01143 IntVar* MakeIntVar(const std::vector<int>& values, const string& name);
01144
01145
01146 IntVar* MakeIntVar(int64 vmin, int64 vmax);
01147
01148
01149 IntVar* MakeIntVar(const std::vector<int64>& values);
01150
01151
01152 IntVar* MakeIntVar(const std::vector<int>& values);
01153
01154
01155 IntVar* MakeBoolVar(const string& name);
01156
01157
01158 IntVar* MakeBoolVar();
01159
01160
01161 IntVar* MakeIntConst(int64 val, const string& name);
01162
01163
01164 IntVar* MakeIntConst(int64 val);
01165
01166
01167
01168
01169 void MakeIntVarArray(int var_count,
01170 int64 vmin,
01171 int64 vmax,
01172 const string& name,
01173 std::vector<IntVar*>* vars);
01174
01175
01176 void MakeIntVarArray(int var_count,
01177 int64 vmin,
01178 int64 vmax,
01179 std::vector<IntVar*>* vars);
01180
01181 IntVar** MakeIntVarArray(int var_count,
01182 int64 vmin,
01183 int64 vmax,
01184 const string& name);
01185
01186
01187
01188
01189
01190 void MakeBoolVarArray(int var_count,
01191 const string& name,
01192 std::vector<IntVar*>* vars);
01193
01194
01195 void MakeBoolVarArray(int var_count,
01196 std::vector<IntVar*>* vars);
01197
01198 IntVar** MakeBoolVarArray(int var_count, const string& name);
01199
01200
01201
01202
01203 IntExpr* MakeSum(IntExpr* const left, IntExpr* const right);
01204
01205 IntExpr* MakeSum(IntExpr* const expr, int64 value);
01206
01207 IntExpr* MakeSum(IntVar* const* vars, int size);
01208
01209 IntExpr* MakeSum(const std::vector<IntVar*>& vars);
01210
01211
01212 IntExpr* MakeScalProd(const std::vector<IntVar*>& vars,
01213 const std::vector<int64>& coefs);
01214
01215 IntExpr* MakeScalProd(IntVar* const* vars,
01216 const int64* const coefs,
01217 int size);
01218
01219 IntExpr* MakeScalProd(const std::vector<IntVar*>& vars,
01220 const std::vector<int>& coefs);
01221
01222 IntExpr* MakeScalProd(IntVar* const* vars,
01223 const int* const coefs,
01224 int size);
01225
01226
01227 IntExpr* MakeDifference(IntExpr* const left, IntExpr* const right);
01228
01229 IntExpr* MakeDifference(int64 value, IntExpr* const expr);
01230
01231 IntExpr* MakeOpposite(IntExpr* const expr);
01232
01233
01234 IntExpr* MakeProd(IntExpr* const left, IntExpr* const right);
01235
01236 IntExpr* MakeProd(IntExpr* const expr, int64 value);
01237
01238
01239 IntExpr* MakeDiv(IntExpr* const expr, int64 value);
01240
01241 IntExpr* MakeDiv(IntExpr* const numerator, IntExpr* const denominator);
01242
01243
01244 IntExpr* MakeAbs(IntExpr* const expr);
01245
01246 IntExpr* MakeSquare(IntExpr* const expr);
01247
01248
01249 IntExpr* MakeElement(const int64* const vals, int size, IntVar* const index);
01250
01251 IntExpr* MakeElement(const std::vector<int64>& vals, IntVar* const index);
01252
01253 IntExpr* MakeElement(const int* const vals, int size, IntVar* const index);
01254
01255 IntExpr* MakeElement(const std::vector<int>& vals, IntVar* const index);
01256
01257
01258
01259
01260 IntExpr* MakeElement(IndexEvaluator1* values, IntVar* const index);
01261
01262
01263
01264
01265
01266
01267 IntExpr* MakeMonotonicElement(IndexEvaluator1* values,
01268 bool increasing,
01269 IntVar* const index);
01270
01271 IntExpr* MakeElement(IndexEvaluator2* values,
01272 IntVar* const index1,
01273 IntVar* const index2);
01274
01275
01276 IntExpr* MakeElement(const IntVar* const * vars, int size,
01277 IntVar* const index);
01278
01279 IntExpr* MakeElement(const std::vector<IntVar*>& vars, IntVar* const index);
01280
01281
01282 IntExpr* MakeMin(const std::vector<IntVar*>& vars);
01283
01284 IntExpr* MakeMin(IntVar* const* vars, int size);
01285
01286 IntExpr* MakeMin(IntExpr* const left, IntExpr* const right);
01287
01288 IntExpr* MakeMin(IntExpr* const expr, int64 val);
01289
01290 IntExpr* MakeMin(IntExpr* const expr, int val);
01291
01292
01293 IntExpr* MakeMax(const std::vector<IntVar*>& vars);
01294
01295 IntExpr* MakeMax(IntVar* const* vars, int size);
01296
01297 IntExpr* MakeMax(IntExpr* const left, IntExpr* const right);
01298
01299 IntExpr* MakeMax(IntExpr* const expr, int64 val);
01300
01301 IntExpr* MakeMax(IntExpr* const expr, int val);
01302
01303
01304 IntExpr* MakeConvexPiecewiseExpr(IntVar* e,
01305 int64 early_cost, int64 early_date,
01306 int64 late_date, int64 late_cost);
01307
01308
01309
01310 IntExpr* MakeSemiContinuousExpr(IntExpr* const e,
01311 int64 fixed_charge,
01312 int64 step);
01313
01314
01315
01316 Constraint* MakeTrueConstraint();
01317
01318 Constraint* MakeFalseConstraint();
01319 Constraint* MakeFalseConstraint(const string& explanation);
01320
01321
01322 Constraint* MakeIsEqualCstCt(IntVar* const v, int64 c, IntVar* const b);
01323
01324 IntVar* MakeIsEqualCstVar(IntVar* const var, int64 value);
01325
01326 Constraint* MakeIsEqualCt(IntExpr* const v1, IntExpr* v2, IntVar* const b);
01327
01328 IntVar* MakeIsEqualVar(IntExpr* const var, IntExpr* v2);
01329
01330 Constraint* MakeEquality(IntVar* const left, IntVar* const right);
01331
01332 Constraint* MakeEquality(IntExpr* const expr, int64 value);
01333
01334 Constraint* MakeEquality(IntExpr* const expr, int value);
01335
01336
01337 Constraint* MakeIsDifferentCstCt(IntVar* const v, int64 c, IntVar* const b);
01338
01339 IntVar* MakeIsDifferentCstVar(IntVar* const v, int64 c);
01340
01341 IntVar* MakeIsDifferentVar(IntExpr* const v1, IntExpr* const v2);
01342
01343 Constraint* MakeIsDifferentCt(IntExpr* const v1, IntExpr* const v2,
01344 IntVar* const b);
01345
01346 Constraint* MakeNonEquality(IntVar* const left, IntVar* const right);
01347
01348 Constraint* MakeNonEquality(IntVar* const expr, int64 value);
01349
01350 Constraint* MakeNonEquality(IntVar* const expr, int value);
01351
01352
01353 Constraint* MakeIsLessOrEqualCstCt(IntVar* const v, int64 c,
01354 IntVar* const b);
01355
01356 IntVar* MakeIsLessOrEqualCstVar(IntVar* const v, int64 c);
01357
01358 IntVar* MakeIsLessOrEqualVar(IntExpr* const left, IntExpr* const right);
01359
01360 Constraint* MakeIsLessOrEqualCt(IntExpr* const left, IntExpr* const right,
01361 IntVar* const b);
01362
01363 Constraint* MakeLessOrEqual(IntVar* const left, IntVar* const right);
01364
01365 Constraint* MakeLessOrEqual(IntExpr* const expr, int64 value);
01366
01367 Constraint* MakeLessOrEqual(IntExpr* const expr, int value);
01368
01369
01370 Constraint* MakeIsGreaterOrEqualCstCt(IntVar* const v, int64 c,
01371 IntVar* const b);
01372
01373 IntVar* MakeIsGreaterOrEqualCstVar(IntVar* const v, int64 c);
01374
01375 IntVar* MakeIsGreaterOrEqualVar(IntExpr* const left, IntExpr* const right);
01376
01377 Constraint* MakeIsGreaterOrEqualCt(IntExpr* const left, IntExpr* const right,
01378 IntVar* const b);
01379
01380 Constraint* MakeGreaterOrEqual(IntVar* const left, IntVar* const right);
01381
01382 Constraint* MakeGreaterOrEqual(IntExpr* const expr, int64 value);
01383
01384 Constraint* MakeGreaterOrEqual(IntExpr* const expr, int value);
01385
01386
01387 Constraint* MakeIsGreaterCstCt(IntVar* const v, int64 c, IntVar* const b);
01388
01389 IntVar* MakeIsGreaterCstVar(IntVar* const v, int64 c);
01390
01391 IntVar* MakeIsGreaterVar(IntExpr* const left, IntExpr* const right);
01392
01393 Constraint* MakeIsGreaterCt(IntExpr* const left, IntExpr* const right,
01394 IntVar* const b);
01395
01396 Constraint* MakeGreater(IntVar* const left, IntVar* const right);
01397
01398 Constraint* MakeGreater(IntExpr* const expr, int64 value);
01399
01400 Constraint* MakeGreater(IntExpr* const expr, int value);
01401
01402
01403 Constraint* MakeIsLessCstCt(IntVar* const v, int64 c, IntVar* const b);
01404
01405 IntVar* MakeIsLessCstVar(IntVar* const v, int64 c);
01406
01407 IntVar* MakeIsLessVar(IntExpr* const left, IntExpr* const right);
01408
01409 Constraint* MakeIsLessCt(IntExpr* const left, IntExpr* const right,
01410 IntVar* const b);
01411
01412 Constraint* MakeLess(IntVar* const left, IntVar* const right);
01413
01414 Constraint* MakeLess(IntExpr* const expr, int64 value);
01415
01416 Constraint* MakeLess(IntExpr* const expr, int value);
01417
01418
01419 Constraint* MakeSumLessOrEqual(const std::vector<IntVar*>& vars, int64 cst);
01420 Constraint* MakeSumLessOrEqual(IntVar* const* vars, int size, int64 cst);
01421
01422 Constraint* MakeSumGreaterOrEqual(const std::vector<IntVar*>& vars, int64 cst);
01423 Constraint* MakeSumGreaterOrEqual(IntVar* const* vars, int size, int64 cst);
01424
01425 Constraint* MakeSumEquality(const std::vector<IntVar*>& vars, int64 cst);
01426 Constraint* MakeSumEquality(IntVar* const* vars, int size, int64 cst);
01427 Constraint* MakeSumEquality(const std::vector<IntVar*>& vars, IntVar* const var);
01428 Constraint* MakeSumEquality(IntVar* const* vars, int size, IntVar* const var);
01429
01430 Constraint* MakeScalProdEquality(const std::vector<IntVar*>& vars,
01431 const std::vector<int64>& coefficients,
01432 int64 cst);
01433 Constraint* MakeScalProdEquality(IntVar* const* vars,
01434 int size,
01435 int64 const * coefficients,
01436 int64 cst);
01437 Constraint* MakeScalProdEquality(IntVar* const* vars,
01438 int size,
01439 int const * coefficients,
01440 int64 cst);
01441 Constraint* MakeScalProdEquality(const std::vector<IntVar*>& vars,
01442 const std::vector<int>& coefficients,
01443 int64 cst);
01444 Constraint* MakeScalProdGreaterOrEqual(const std::vector<IntVar*>& vars,
01445 const std::vector<int64>& coefficients,
01446 int64 cst);
01447 Constraint* MakeScalProdGreaterOrEqual(IntVar* const* vars,
01448 int size,
01449 int64 const * coefficients,
01450 int64 cst);
01451 Constraint* MakeScalProdGreaterOrEqual(const std::vector<IntVar*>& vars,
01452 const std::vector<int>& coefficients,
01453 int64 cst);
01454 Constraint* MakeScalProdGreaterOrEqual(IntVar* const* vars,
01455 int size,
01456 int const * coefficients,
01457 int64 cst);
01458 Constraint* MakeScalProdLessOrEqual(const std::vector<IntVar*>& vars,
01459 const std::vector<int64>& coefficients,
01460 int64 cst);
01461 Constraint* MakeScalProdLessOrEqual(IntVar* const* vars,
01462 int size,
01463 int64 const * coefficients,
01464 int64 cst);
01465 Constraint* MakeScalProdLessOrEqual(const std::vector<IntVar*>& vars,
01466 const std::vector<int>& coefficients,
01467 int64 cst);
01468 Constraint* MakeScalProdLessOrEqual(IntVar* const* vars,
01469 int size,
01470 int const * coefficients,
01471 int64 cst);
01472
01473
01474
01475 Demon* MakeConstraintInitialPropagateCallback(Constraint* const ct);
01476
01477
01478
01479 Demon* MakeDelayedConstraintInitialPropagateCallback(Constraint* const ct);
01480
01481 Demon* MakeCallbackDemon(Callback1<Solver*>* const callback);
01482
01483 Demon* MakeCallbackDemon(Closure* const closure);
01484
01485
01486
01487 Constraint* MakeBetweenCt(IntVar* const v, int64 l, int64 u);
01488
01489
01490 Constraint* MakeIsBetweenCt(IntVar* const v, int64 l, int64 u,
01491 IntVar* const b);
01492
01493
01494 Constraint* MakeIsMemberCt(IntVar* const v, const int64* const values,
01495 int size, IntVar* const b);
01496 Constraint* MakeIsMemberCt(IntVar* const v, const std::vector<int64>& values,
01497 IntVar* const b);
01498 Constraint* MakeIsMemberCt(IntVar* const v, const int* const values,
01499 int size, IntVar* const b);
01500 Constraint* MakeIsMemberCt(IntVar* const v, const std::vector<int>& values,
01501 IntVar* const b);
01502 IntVar* MakeIsMemberVar(IntVar* const v, const int64* const values, int size);
01503 IntVar* MakeIsMemberVar(IntVar* const v, const std::vector<int64>& values);
01504 IntVar* MakeIsMemberVar(IntVar* const v, const int* const values, int size);
01505 IntVar* MakeIsMemberVar(IntVar* const v, const std::vector<int>& values);
01506
01507
01508 Constraint* MakeMemberCt(IntVar* const v, const int64* const values,
01509 int size);
01510 Constraint* MakeMemberCt(IntVar* const v, const std::vector<int64>& values);
01511 Constraint* MakeMemberCt(IntVar* const v, const int* const values, int size);
01512 Constraint* MakeMemberCt(IntVar* const v, const std::vector<int>& values);
01513
01514
01515
01516 Constraint* MakeCount(const std::vector<IntVar*>& v, int64 value, int64 count);
01517
01518 Constraint* MakeCount(const std::vector<IntVar*>& v, int64 value,
01519 IntVar* const count);
01520
01521
01522 Constraint* MakeDistribute(const std::vector<IntVar*>& vars,
01523 const std::vector<int64>& values,
01524 const std::vector<IntVar*>& cards);
01525
01526 Constraint* MakeDistribute(const std::vector<IntVar*>& vars,
01527 const std::vector<int>& values,
01528 const std::vector<IntVar*>& cards);
01529
01530 Constraint* MakeDistribute(const std::vector<IntVar*>& vars,
01531 const std::vector<IntVar*>& cards);
01532
01533
01534 Constraint* MakeDistribute(const std::vector<IntVar*>& vars,
01535 int64 card_min,
01536 int64 card_max,
01537 int64 card_size);
01538
01539
01540
01541
01542
01543 Constraint* MakeDeviation(const std::vector<IntVar*>& vars,
01544 IntVar* const deviation_var,
01545 int64 total_sum);
01546
01547
01548
01549 Constraint* MakeAllDifferent(const std::vector<IntVar*>& vars);
01550
01551
01552
01553
01554 Constraint* MakeAllDifferent(const std::vector<IntVar*>& vars,
01555 bool stronger_propagation);
01556
01557
01558
01559 Constraint* MakeAllDifferent(const IntVar* const* vars,
01560 int size, bool stronger_propagation);
01561
01562
01563
01564
01565
01566
01567
01568
01569
01570
01571 Constraint* MakeNoCycle(const std::vector<IntVar*>& nexts,
01572 const std::vector<IntVar*>& active,
01573 ResultCallback1<bool, int64>* sink_handler = NULL);
01574 Constraint* MakeNoCycle(const IntVar* const* nexts,
01575 const IntVar* const* active,
01576 int size,
01577 ResultCallback1<bool, int64>* sink_handler = NULL);
01578 Constraint* MakeNoCycle(const std::vector<IntVar*>& nexts,
01579 const std::vector<IntVar*>& active,
01580 ResultCallback1<bool, int64>* sink_handler,
01581 bool assume_paths);
01582 Constraint* MakeNoCycle(const IntVar* const* nexts,
01583 const IntVar* const* active,
01584 int size,
01585 ResultCallback1<bool, int64>* sink_handler,
01586 bool assume_paths);
01587
01588
01589
01590
01591
01592 Constraint* MakePathCumul(const std::vector<IntVar*>& nexts,
01593 const std::vector<IntVar*>& active,
01594 const std::vector<IntVar*>& cumuls,
01595 const std::vector<IntVar*>& transits);
01596 Constraint* MakePathCumul(const IntVar* const* nexts,
01597 const IntVar* const* active,
01598 const IntVar* const* cumuls,
01599 const IntVar* const* transits,
01600 int next_size,
01601 int cumul_size);
01602
01603
01604
01605
01606 Constraint* MakeMapDomain(IntVar* const var, IntVar* const * vars, int size);
01607
01608
01609
01610
01611 Constraint* MakeMapDomain(IntVar* const var, const std::vector<IntVar*>& vars);
01612
01613
01614
01615
01616
01617 Constraint* MakeAllowedAssignments(const std::vector<IntVar*>& vars,
01618 const IntTupleSet& tuples);
01619
01620
01621
01622
01623
01624
01625
01626
01627 Constraint* MakeTransitionConstraint(
01628 const std::vector<IntVar*>& vars,
01629 const IntTupleSet& transitions,
01630 int64 initial_state,
01631 const std::vector<int64>& final_states);
01632
01633
01634
01635
01636
01637
01638
01639
01640 Constraint* MakeTransitionConstraint(
01641 const std::vector<IntVar*>& vars,
01642 const IntTupleSet& transitions,
01643 int64 initial_state,
01644 const std::vector<int>& final_states);
01645
01646 #if defined(SWIGPYTHON)
01647
01648 Constraint* MakeAllowedAssignments(const std::vector<IntVar*>& vars,
01649 const std::vector<std::vector<int64> >& raw_tuples) {
01650 IntTupleSet tuples(vars.size());
01651 tuples.InsertAll(raw_tuples);
01652 return MakeAllowedAssignments(vars, tuples);
01653 }
01654
01655 Constraint* MakeTransitionConstraint(
01656 const std::vector<IntVar*>& vars,
01657 const std::vector<std::vector<int64> >& raw_transitions,
01658 int64 initial_state,
01659 const std::vector<int>& final_states) {
01660 IntTupleSet transitions(3);
01661 transitions.InsertAll(raw_transitions);
01662 return MakeTransitionConstraint(vars,
01663 transitions,
01664 initial_state,
01665 final_states);
01666 }
01667 #endif
01668
01669
01670
01671
01672
01673
01674
01675
01676
01677 Pack* MakePack(const std::vector<IntVar*>& vars, int number_of_bins);
01678
01679
01680
01681
01682
01683
01684
01685 IntervalVar* MakeFixedDurationIntervalVar(int64 start_min,
01686 int64 start_max,
01687 int64 duration,
01688 bool optional,
01689 const string& name);
01690
01691
01692
01693 void MakeFixedDurationIntervalVarArray(int count,
01694 int64 start_min,
01695 int64 start_max,
01696 int64 duration,
01697 bool optional,
01698 const string& name,
01699 std::vector<IntervalVar*>* array);
01700
01701
01702
01703 IntervalVar* MakeFixedInterval(int64 start,
01704 int64 duration,
01705 const string& name);
01706
01707
01708
01709 IntervalVar* MakeMirrorInterval(IntervalVar* const interval_var);
01710
01711
01712
01713
01714
01715
01716
01717
01718
01719
01720
01721
01722
01723
01724
01725
01726
01727
01728 IntervalVar* MakeIntervalRelaxedMin(IntervalVar* const interval_var);
01729
01730
01731
01732
01733
01734
01735
01736
01737
01738
01739
01740
01741
01742
01743
01744
01745
01746
01747 IntervalVar* MakeIntervalRelaxedMax(IntervalVar* const interval_var);
01748
01749
01750
01751
01752
01753 Constraint* MakeIntervalVarRelation(IntervalVar* const t,
01754 UnaryIntervalRelation r,
01755 int64 d);
01756
01757
01758 Constraint* MakeIntervalVarRelation(IntervalVar* const t1,
01759 BinaryIntervalRelation r,
01760 IntervalVar* const t2);
01761
01762
01763
01764
01765 Constraint* MakeTemporalDisjunction(IntervalVar* const t1,
01766 IntervalVar* const t2,
01767 IntVar* const alt);
01768
01769
01770
01771 Constraint* MakeTemporalDisjunction(IntervalVar* const t1,
01772 IntervalVar* const t2);
01773
01774
01775
01776 Constraint* MakeDisjunctiveConstraint(const std::vector<IntervalVar*>& intervals);
01777
01778
01779
01780 SequenceVar* MakeSequenceVar(const std::vector<IntervalVar*>& intervals,
01781 const string& name);
01782
01783
01784
01785
01786
01787
01788
01789
01790
01791
01792 Constraint* MakeCumulative(const std::vector<IntervalVar*>& intervals,
01793 const std::vector<int64>& demands,
01794 int64 capacity,
01795 const string& name);
01796
01797
01798
01799
01800
01801
01802
01803
01804
01805
01806 Constraint* MakeCumulative(const std::vector<IntervalVar*>& intervals,
01807 const std::vector<int>& demands,
01808 int64 capacity,
01809 const string& name);
01810
01811
01812
01813
01814 Assignment* MakeAssignment();
01815
01816
01817 Assignment* MakeAssignment(const Assignment* const a);
01818
01819
01820
01821
01822 SolutionCollector* MakeFirstSolutionCollector(
01823 const Assignment* const assignment);
01824
01825
01826 SolutionCollector* MakeFirstSolutionCollector();
01827
01828
01829 SolutionCollector* MakeLastSolutionCollector(
01830 const Assignment* const assignment);
01831
01832
01833 SolutionCollector* MakeLastSolutionCollector();
01834
01835
01836
01837
01838
01839 SolutionCollector* MakeBestValueSolutionCollector(
01840 const Assignment* const assignment, bool maximize);
01841
01842
01843
01844
01845
01846 SolutionCollector* MakeBestValueSolutionCollector(bool maximize);
01847
01848
01849 SolutionCollector* MakeAllSolutionCollector(
01850 const Assignment* const assignment);
01851
01852
01853 SolutionCollector* MakeAllSolutionCollector();
01854
01855
01856
01857
01858 OptimizeVar* MakeMinimize(IntVar* const v, int64 step);
01859
01860
01861 OptimizeVar* MakeMaximize(IntVar* const v, int64 step);
01862
01863
01864 OptimizeVar* MakeOptimize(bool maximize, IntVar* const v, int64 step);
01865
01866
01867
01868 OptimizeVar* MakeWeightedMinimize(const std::vector<IntVar*>& vars,
01869 const std::vector<int64>& weights,
01870 int64 step);
01871
01872
01873
01874 OptimizeVar* MakeWeightedMinimize(const std::vector<IntVar*>& vars,
01875 const std::vector<int>& weights,
01876 int64 step);
01877
01878
01879 OptimizeVar* MakeWeightedMaximize(const std::vector<IntVar*>& vars,
01880 const std::vector<int64>& weights,
01881 int64 step);
01882
01883
01884 OptimizeVar* MakeWeightedMaximize(const std::vector<IntVar*>& vars,
01885 const std::vector<int>& weights,
01886 int64 step);
01887
01888
01889 OptimizeVar* MakeWeightedOptimize(bool maximize,
01890 const std::vector<IntVar*>& vars,
01891 const std::vector<int64>& weights,
01892 int64 step);
01893
01894
01895 OptimizeVar* MakeWeightedOptimize(bool maximize,
01896 const std::vector<IntVar*>& vars,
01897 const std::vector<int>& weights,
01898 int64 step);
01899
01900
01901
01902
01903
01904
01905
01906
01907
01908
01909
01910
01911
01912
01913
01914
01915
01916
01917
01918
01919 SearchMonitor* MakeTabuSearch(bool maximize,
01920 IntVar* const v,
01921 int64 step,
01922 const std::vector<IntVar*>& vars,
01923 int64 keep_tenure,
01924 int64 forbid_tenure,
01925 double tabu_factor);
01926 SearchMonitor* MakeTabuSearch(bool maximize,
01927 IntVar* const v,
01928 int64 step,
01929 const IntVar* const* vars,
01930 int size,
01931 int64 keep_tenure,
01932 int64 forbid_tenure,
01933 double tabu_factor);
01934
01935
01936
01937 SearchMonitor* MakeSimulatedAnnealing(bool maximize,
01938 IntVar* const v,
01939 int64 step,
01940 int64 initial_temperature);
01941
01942
01943
01944 SearchMonitor* MakeGuidedLocalSearch(bool maximize,
01945 IntVar* const objective,
01946 IndexEvaluator2* objective_function,
01947 int64 step,
01948 const std::vector<IntVar*>& vars,
01949 double penalty_factor);
01950 SearchMonitor* MakeGuidedLocalSearch(bool maximize,
01951 IntVar* const objective,
01952 IndexEvaluator2* objective_function,
01953 int64 step,
01954 const IntVar* const* vars,
01955 int size,
01956 double penalty_factor);
01957 SearchMonitor* MakeGuidedLocalSearch(bool maximize,
01958 IntVar* const objective,
01959 IndexEvaluator3* objective_function,
01960 int64 step,
01961 const std::vector<IntVar*>& vars,
01962 const std::vector<IntVar*>& secondary_vars,
01963 double penalty_factor);
01964 SearchMonitor* MakeGuidedLocalSearch(bool maximize,
01965 IntVar* const objective,
01966 IndexEvaluator3* objective_function,
01967 int64 step,
01968 const IntVar* const* vars,
01969 const IntVar* const* secondary_vars,
01970 int size,
01971 double penalty_factor);
01972
01973
01974
01975
01976
01977
01978 SearchMonitor* MakeLubyRestart(int scale_factor);
01979
01980
01981
01982 SearchMonitor* MakeConstantRestart(int frequency);
01983
01984
01985
01986
01987
01988
01989 SearchLimit* MakeTimeLimit(int64 time_in_ms);
01990
01991
01992
01993 SearchLimit* MakeBranchesLimit(int64 branches);
01994
01995
01996
01997 SearchLimit* MakeFailuresLimit(int64 failures);
01998
01999
02000
02001 SearchLimit* MakeSolutionsLimit(int64 solutions);
02002
02003
02004
02005 SearchLimit* MakeLimit(int64 time,
02006 int64 branches,
02007 int64 failures,
02008 int64 solutions);
02009
02010
02011 SearchLimit* MakeLimit(int64 time,
02012 int64 branches,
02013 int64 failures,
02014 int64 solutions,
02015 bool smart_time_check);
02016
02017
02018 SearchLimit* MakeLimit(int64 time,
02019 int64 branches,
02020 int64 failures,
02021 int64 solutions,
02022 bool smart_time_check,
02023 bool cumulative);
02024
02025 SearchLimit* MakeLimit(const SearchLimitProto& proto);
02026
02027
02028
02029
02030 SearchLimit* MakeLimit(SearchLimit* const limit_1,
02031 SearchLimit* const limit_2);
02032
02033 void UpdateLimits(int64 time,
02034 int64 branches,
02035 int64 failures,
02036 int64 solutions,
02037 SearchLimit* limit);
02038
02039 int64 GetTime(SearchLimit* limit);
02040
02041
02042
02043 SearchLimit* MakeCustomLimit(ResultCallback<bool>* limiter);
02044
02045
02046
02047
02048
02049
02050
02051 NoGoodManager* MakeNoGoodManager();
02052
02053
02054
02055
02056
02057 SearchMonitor* MakeTreeMonitor(const IntVar* const* vars, int size,
02058 const string& file_tree,
02059 const string& file_visualization);
02060
02061
02062
02063
02064 SearchMonitor* MakeTreeMonitor(const std::vector<IntVar*>& vars,
02065 const string& file_tree,
02066 const string& file_visualization);
02067
02068
02069
02070
02071
02072 SearchMonitor* MakeTreeMonitor(const IntVar* const* vars, int size,
02073 const string& file_config,
02074 const string& file_tree,
02075 const string& file_visualization);
02076
02077
02078
02079
02080
02081 SearchMonitor* MakeTreeMonitor(const std::vector<IntVar*>& vars,
02082 const string& file_config,
02083 const string& file_tree,
02084 const string& file_visualization);
02085
02086 #if !defined(SWIG)
02087
02088
02089
02090
02091 SearchMonitor* MakeTreeMonitor(const IntVar* const* vars, int size,
02092 string* const tree_xml,
02093 string* const visualization_xml);
02094
02095
02096
02097
02098
02099 SearchMonitor* MakeTreeMonitor(const std::vector<IntVar*>& vars,
02100 string* const tree_xml,
02101 string* const visualization_xml);
02102
02103
02104
02105
02106
02107 SearchMonitor* MakeTreeMonitor(const IntVar* const* vars, int size,
02108 string* const config_xml,
02109 string* const tree_xml,
02110 string* const visualization_xml);
02111
02112
02113
02114
02115
02116 SearchMonitor* MakeTreeMonitor(const std::vector<IntVar*>& vars,
02117 string* const config_xml,
02118 string* const tree_xml,
02119 string* const visualization_xml);
02120
02121 #endif // #if !defined(SWIG)
02122
02123
02124
02125
02126
02127
02128
02129 SearchMonitor* MakeSearchLog(int branch_count);
02130
02131
02132 SearchMonitor* MakeSearchLog(int branch_count, IntVar* const objective);
02133
02134
02135
02136 SearchMonitor* MakeSearchLog(int branch_count,
02137 ResultCallback<string>* display_callback);
02138
02139
02140
02141 SearchMonitor* MakeSearchLog(int branch_count,
02142 IntVar* objective,
02143 ResultCallback<string>* display_callback);
02144
02145
02146
02147
02148 SearchMonitor* MakeSearchLog(int branch_count, OptimizeVar* const objective);
02149
02150
02151
02152 SearchMonitor* MakeSearchLog(int branch_count,
02153 OptimizeVar* const objective,
02154 ResultCallback<string>* display_callback);
02155
02156
02157
02158
02159
02160
02161 SearchMonitor* MakeSearchTrace(const string& prefix);
02162
02163
02164
02165
02166 ModelVisitor* MakePrintModelVisitor();
02167
02168 ModelVisitor* MakeStatisticsModelVisitor();
02169
02170
02171
02172 SearchMonitor* MakeSymmetryManager(const std::vector<SymmetryBreaker*>& visitors);
02173 SearchMonitor* MakeSymmetryManager(SymmetryBreaker* const * visitors,
02174 int size);
02175 SearchMonitor* MakeSymmetryManager(SymmetryBreaker* const v1);
02176 SearchMonitor* MakeSymmetryManager(SymmetryBreaker* const v1,
02177 SymmetryBreaker* const v2);
02178 SearchMonitor* MakeSymmetryManager(SymmetryBreaker* const v1,
02179 SymmetryBreaker* const v2,
02180 SymmetryBreaker* const v3);
02181 SearchMonitor* MakeSymmetryManager(SymmetryBreaker* const v1,
02182 SymmetryBreaker* const v2,
02183 SymmetryBreaker* const v3,
02184 SymmetryBreaker* const v4);
02185
02186
02187 #if !defined(SWIG)
02188 SearchMonitor* MakeSimplexConnection(Callback1<MPSolver*>* const builder,
02189 Callback1<MPSolver*>* const modifier,
02190 Callback1<MPSolver*>* const runner,
02191 int simplex_frequency);
02192 #endif // #if !defined(SWIG)
02193
02194
02195
02196
02197
02198
02199
02200
02201 SearchMonitor* MakeSimplexConstraint(int simplex_frequency);
02202
02203
02204
02205
02206 Decision* MakeAssignVariableValue(IntVar* const var, int64 value);
02207 Decision* MakeVariableLessOrEqualValue(IntVar* const var, int64 value);
02208 Decision* MakeVariableGreaterOrEqualValue(IntVar* const var, int64 value);
02209 Decision* MakeSplitVariableDomain(IntVar* const var,
02210 int64 value,
02211 bool start_with_lower_half);
02212 Decision* MakeAssignVariableValueOrFail(IntVar* const var, int64 value);
02213 Decision* MakeAssignVariablesValues(const IntVar* const* vars, int size,
02214 const int64* const values);
02215 Decision* MakeAssignVariablesValues(const std::vector<IntVar*>& vars,
02216 const std::vector<int64>& values);
02217 Decision* MakeFailDecision();
02218
02219
02220
02221
02222
02223
02224
02225
02226
02227 DecisionBuilder* Compose(DecisionBuilder* const db1,
02228 DecisionBuilder* const db2);
02229 DecisionBuilder* Compose(DecisionBuilder* const db1,
02230 DecisionBuilder* const db2,
02231 DecisionBuilder* const db3);
02232 DecisionBuilder* Compose(DecisionBuilder* const db1,
02233 DecisionBuilder* const db2,
02234 DecisionBuilder* const db3,
02235 DecisionBuilder* const db4);
02236 DecisionBuilder* Compose(const std::vector<DecisionBuilder*>& dbs);
02237
02238
02239
02240
02241
02242
02243
02244
02245
02246
02247
02248
02249
02250
02251
02252
02253
02254 DecisionBuilder* Try(DecisionBuilder* const db1,
02255 DecisionBuilder* const db2);
02256 DecisionBuilder* Try(DecisionBuilder* const db1,
02257 DecisionBuilder* const db2,
02258 DecisionBuilder* const db3);
02259 DecisionBuilder* Try(DecisionBuilder* const db1,
02260 DecisionBuilder* const db2,
02261 DecisionBuilder* const db3,
02262 DecisionBuilder* const db4);
02263 DecisionBuilder* Try(const std::vector<DecisionBuilder*>& dbs);
02264
02265
02266
02267 DecisionBuilder* MakePhase(const std::vector<IntVar*>& vars,
02268 IntVarStrategy var_str,
02269 IntValueStrategy val_str);
02270 DecisionBuilder* MakePhase(const IntVar* const* vars,
02271 int size,
02272 IntVarStrategy var_str,
02273 IntValueStrategy val_str);
02274
02275 DecisionBuilder* MakePhase(const std::vector<IntVar*>& vars,
02276 IndexEvaluator1* var_evaluator,
02277 IntValueStrategy val_str);
02278 DecisionBuilder* MakePhase(const IntVar* const* vars,
02279 int size,
02280 IndexEvaluator1* var_evaluator,
02281 IntValueStrategy val_str);
02282
02283 DecisionBuilder* MakePhase(const std::vector<IntVar*>& vars,
02284 IntVarStrategy var_str,
02285 IndexEvaluator2* val_eval);
02286 DecisionBuilder* MakePhase(const IntVar* const* vars,
02287 int size,
02288 IntVarStrategy var_str,
02289 IndexEvaluator2* val_eval);
02290
02291 DecisionBuilder* MakePhase(const std::vector<IntVar*>& vars,
02292 IndexEvaluator1* var_evaluator,
02293 IndexEvaluator2* val_eval);
02294 DecisionBuilder* MakePhase(const IntVar* const* vars,
02295 int size,
02296 IndexEvaluator1* var_evaluator,
02297 IndexEvaluator2* val_eval);
02298
02299 DecisionBuilder* MakePhase(const std::vector<IntVar*>& vars,
02300 IntVarStrategy var_str,
02301 IndexEvaluator2* val_eval,
02302 IndexEvaluator1* tie_breaker);
02303 DecisionBuilder* MakePhase(const IntVar* const* vars,
02304 int size,
02305 IntVarStrategy var_str,
02306 IndexEvaluator2* val_eval,
02307 IndexEvaluator1* tie_breaker);
02308
02309 DecisionBuilder* MakePhase(const std::vector<IntVar*>& vars,
02310 IndexEvaluator1* var_evaluator,
02311 IndexEvaluator2* val_eval,
02312 IndexEvaluator1* tie_breaker);
02313 DecisionBuilder* MakePhase(const IntVar* const* vars,
02314 int size,
02315 IndexEvaluator1* var_evaluator,
02316 IndexEvaluator2* val_eval,
02317 IndexEvaluator1* tie_breaker);
02318
02319 DecisionBuilder* MakeDefaultPhase(const IntVar* const* vars, int size);
02320 DecisionBuilder* MakeDefaultPhase(const std::vector<IntVar*>& vars);
02321 DecisionBuilder* MakeDefaultPhase(const IntVar* const* vars,
02322 int size,
02323 const DefaultPhaseParameters& parameters);
02324 DecisionBuilder* MakeDefaultPhase(const std::vector<IntVar*>& vars,
02325 const DefaultPhaseParameters& parameters);
02326
02327
02328 DecisionBuilder* MakePhase(IntVar* const v0,
02329 IntVarStrategy var_str,
02330 IntValueStrategy val_str);
02331 DecisionBuilder* MakePhase(IntVar* const v0,
02332 IntVar* const v1,
02333 IntVarStrategy var_str,
02334 IntValueStrategy val_str);
02335 DecisionBuilder* MakePhase(IntVar* const v0,
02336 IntVar* const v1,
02337 IntVar* const v2,
02338 IntVarStrategy var_str,
02339 IntValueStrategy val_str);
02340 DecisionBuilder* MakePhase(IntVar* const v0,
02341 IntVar* const v1,
02342 IntVar* const v2,
02343 IntVar* const v3,
02344 IntVarStrategy var_str,
02345 IntValueStrategy val_str);
02346
02347
02348
02349
02350
02351
02352
02353
02354 Decision* MakeScheduleOrPostpone(IntervalVar* const var,
02355 int64 est,
02356 int64* const marker);
02357
02358
02359
02360 Decision* MakeRankFirstInterval(SequenceVar* const sequence, int index);
02361
02362
02363
02364 Decision* MakeRankLastInterval(SequenceVar* const sequence, int index);
02365
02366
02367
02368
02369
02370
02371 DecisionBuilder* MakePhase(const std::vector<IntVar*>& vars,
02372 IndexEvaluator2* evaluator,
02373 EvaluatorStrategy str);
02374 DecisionBuilder* MakePhase(const IntVar* const* vars,
02375 int size,
02376 IndexEvaluator2* evaluator,
02377 EvaluatorStrategy str);
02378
02379
02380
02381
02382
02383
02384
02385
02386 DecisionBuilder* MakePhase(const std::vector<IntVar*>& vars,
02387 IndexEvaluator2* evaluator,
02388 IndexEvaluator1* tie_breaker,
02389 EvaluatorStrategy str);
02390 DecisionBuilder* MakePhase(const IntVar* const* vars,
02391 int size,
02392 IndexEvaluator2* evaluator,
02393 IndexEvaluator1* tie_breaker,
02394 EvaluatorStrategy str);
02395
02396
02397
02398 DecisionBuilder* MakePhase(const std::vector<IntervalVar*>& intervals,
02399 IntervalStrategy str);
02400
02401 DecisionBuilder* MakePhase(const std::vector<SequenceVar*>& sequences,
02402 SequenceStrategy str);
02403
02404
02405
02406 DecisionBuilder* MakeDecisionBuilderFromAssignment(
02407 Assignment* const assignment,
02408 DecisionBuilder* const db,
02409 const IntVar* const* vars,
02410 int size);
02411
02412
02413
02414 DecisionBuilder* MakeConstraintAdder(Constraint* const ct);
02415
02416
02417
02418
02419
02420 DecisionBuilder* MakeSolveOnce(DecisionBuilder* const db);
02421 DecisionBuilder* MakeSolveOnce(DecisionBuilder* const db,
02422 SearchMonitor* const monitor1);
02423 DecisionBuilder* MakeSolveOnce(DecisionBuilder* const db,
02424 SearchMonitor* const monitor1,
02425 SearchMonitor* const monitor2);
02426 DecisionBuilder* MakeSolveOnce(DecisionBuilder* const db,
02427 SearchMonitor* const monitor1,
02428 SearchMonitor* const monitor2,
02429 SearchMonitor* const monitor3);
02430 DecisionBuilder* MakeSolveOnce(DecisionBuilder* const db,
02431 SearchMonitor* const monitor1,
02432 SearchMonitor* const monitor2,
02433 SearchMonitor* const monitor3,
02434 SearchMonitor* const monitor4);
02435 DecisionBuilder* MakeSolveOnce(DecisionBuilder* const db,
02436 const std::vector<SearchMonitor*>& monitors);
02437
02438
02439
02440
02441
02442
02443
02444
02445 DecisionBuilder* MakeNestedOptimize(DecisionBuilder* const db,
02446 Assignment* const solution,
02447 bool maximize,
02448 int64 step);
02449 DecisionBuilder* MakeNestedOptimize(DecisionBuilder* const db,
02450 Assignment* const solution,
02451 bool maximize,
02452 int64 step,
02453 SearchMonitor* const monitor1);
02454 DecisionBuilder* MakeNestedOptimize(DecisionBuilder* const db,
02455 Assignment* const solution,
02456 bool maximize,
02457 int64 step,
02458 SearchMonitor* const monitor1,
02459 SearchMonitor* const monitor2);
02460 DecisionBuilder* MakeNestedOptimize(DecisionBuilder* const db,
02461 Assignment* const solution,
02462 bool maximize,
02463 int64 step,
02464 SearchMonitor* const monitor1,
02465 SearchMonitor* const monitor2,
02466 SearchMonitor* const monitor3);
02467 DecisionBuilder* MakeNestedOptimize(DecisionBuilder* const db,
02468 Assignment* const solution,
02469 bool maximize,
02470 int64 step,
02471 SearchMonitor* const monitor1,
02472 SearchMonitor* const monitor2,
02473 SearchMonitor* const monitor3,
02474 SearchMonitor* const monitor4);
02475 DecisionBuilder* MakeNestedOptimize(DecisionBuilder* const db,
02476 Assignment* const solution,
02477 bool maximize,
02478 int64 step,
02479 const std::vector<SearchMonitor*>& monitors);
02480
02481
02482
02483 DecisionBuilder* MakeRestoreAssignment(Assignment* assignment);
02484
02485
02486
02487 DecisionBuilder* MakeStoreAssignment(Assignment* assignment);
02488
02489
02490
02491 LocalSearchOperator* MakeOperator(const std::vector<IntVar*>& vars,
02492 LocalSearchOperators op);
02493 LocalSearchOperator* MakeOperator(const IntVar* const* vars,
02494 int size,
02495 LocalSearchOperators op);
02496 LocalSearchOperator* MakeOperator(const std::vector<IntVar*>& vars,
02497 const std::vector<IntVar*>& secondary_vars,
02498 LocalSearchOperators op);
02499 LocalSearchOperator* MakeOperator(const IntVar* const* vars,
02500 const IntVar* const* secondary_vars,
02501 int size,
02502 LocalSearchOperators op);
02503
02504
02505 LocalSearchOperator* MakeOperator(const std::vector<IntVar*>& vars,
02506 IndexEvaluator3* const evaluator,
02507 EvaluatorLocalSearchOperators op);
02508 LocalSearchOperator* MakeOperator(const IntVar* const* vars,
02509 int size,
02510 IndexEvaluator3* const evaluator,
02511 EvaluatorLocalSearchOperators op);
02512 LocalSearchOperator* MakeOperator(const std::vector<IntVar*>& vars,
02513 const std::vector<IntVar*>& secondary_vars,
02514 IndexEvaluator3* const evaluator,
02515 EvaluatorLocalSearchOperators op);
02516 LocalSearchOperator* MakeOperator(const IntVar* const* vars,
02517 const IntVar* const* secondary_vars,
02518 int size,
02519 IndexEvaluator3* const evaluator,
02520 EvaluatorLocalSearchOperators op);
02521
02522
02523
02524
02525
02526
02527
02528
02529 LocalSearchOperator* MakeRandomLNSOperator(const std::vector<IntVar*>& vars,
02530 int number_of_variables);
02531 LocalSearchOperator* MakeRandomLNSOperator(const std::vector<IntVar*>& vars,
02532 int number_of_variables,
02533 int32 seed);
02534 LocalSearchOperator* MakeRandomLNSOperator(const IntVar* const* vars,
02535 int size,
02536 int number_of_variables);
02537 LocalSearchOperator* MakeRandomLNSOperator(const IntVar* const* vars,
02538 int size,
02539 int number_of_variables,
02540 int32 seed);
02541
02542
02543
02544
02545
02546
02547 LocalSearchOperator* MakeMoveTowardTargetOperator(const Assignment& target);
02548
02549
02550
02551
02552
02553
02554
02555 LocalSearchOperator* MakeMoveTowardTargetOperator(
02556 const std::vector<IntVar*>& variables,
02557 const std::vector<int64>& target_values);
02558
02559
02560
02561
02562
02563
02564
02565
02566
02567
02568
02569
02570
02571
02572
02573
02574
02575
02576
02577
02578
02579
02580
02581
02582
02583
02584
02585
02586
02587
02588 LocalSearchOperator* ConcatenateOperators(
02589 const std::vector<LocalSearchOperator*>& ops);
02590 LocalSearchOperator* ConcatenateOperators(
02591 const std::vector<LocalSearchOperator*>& ops, bool restart);
02592 LocalSearchOperator* ConcatenateOperators(
02593 const std::vector<LocalSearchOperator*>& ops,
02594 ResultCallback2<int64, int, int>* const evaluator);
02595
02596
02597 LocalSearchOperator* RandomConcatenateOperators(
02598 const std::vector<LocalSearchOperator*>& ops);
02599
02600
02601
02602
02603
02604
02605 LocalSearchOperator* MakeNeighborhoodLimit(LocalSearchOperator* const op,
02606 int64 limit);
02607
02608
02609
02610
02611
02612
02613
02614
02615
02616
02617
02618
02619
02620
02621
02622
02623
02624
02625
02626
02627
02628
02629
02630
02631
02632
02633
02634
02635 DecisionBuilder* MakeLocalSearchPhase(
02636 Assignment* const assignment,
02637 LocalSearchPhaseParameters* const parameters);
02638 DecisionBuilder* MakeLocalSearchPhase(
02639 const std::vector<IntVar*>& vars,
02640 DecisionBuilder* const first_solution,
02641 LocalSearchPhaseParameters* const parameters);
02642 DecisionBuilder* MakeLocalSearchPhase(
02643 IntVar* const* vars, int size,
02644 DecisionBuilder* const first_solution,
02645 LocalSearchPhaseParameters* const parameters);
02646
02647
02648 SolutionPool* MakeDefaultSolutionPool();
02649
02650
02651 LocalSearchPhaseParameters* MakeLocalSearchPhaseParameters(
02652 LocalSearchOperator* const ls_operator,
02653 DecisionBuilder* const sub_decision_builder);
02654 LocalSearchPhaseParameters* MakeLocalSearchPhaseParameters(
02655 LocalSearchOperator* const ls_operator,
02656 DecisionBuilder* const sub_decision_builder,
02657 SearchLimit* const limit);
02658 LocalSearchPhaseParameters* MakeLocalSearchPhaseParameters(
02659 LocalSearchOperator* const ls_operator,
02660 DecisionBuilder* const sub_decision_builder,
02661 SearchLimit* const limit,
02662 const std::vector<LocalSearchFilter*>& filters);
02663
02664 LocalSearchPhaseParameters* MakeLocalSearchPhaseParameters(
02665 SolutionPool* const pool,
02666 LocalSearchOperator* const ls_operator,
02667 DecisionBuilder* const sub_decision_builder);
02668 LocalSearchPhaseParameters* MakeLocalSearchPhaseParameters(
02669 SolutionPool* const pool,
02670 LocalSearchOperator* const ls_operator,
02671 DecisionBuilder* const sub_decision_builder,
02672 SearchLimit* const limit);
02673 LocalSearchPhaseParameters* MakeLocalSearchPhaseParameters(
02674 SolutionPool* const pool,
02675 LocalSearchOperator* const ls_operator,
02676 DecisionBuilder* const sub_decision_builder,
02677 SearchLimit* const limit,
02678 const std::vector<LocalSearchFilter*>& filters);
02679
02680
02681 LocalSearchFilter* MakeVariableDomainFilter();
02682 LocalSearchFilter* MakeLocalSearchObjectiveFilter(
02683 const IntVar* const* vars,
02684 int size,
02685 IndexEvaluator2* const values,
02686 const IntVar* const objective,
02687 Solver::LocalSearchFilterBound filter_enum,
02688 Solver::LocalSearchOperation op_enum);
02689 LocalSearchFilter* MakeLocalSearchObjectiveFilter(
02690 const std::vector<IntVar*>& vars,
02691 IndexEvaluator2* const values,
02692 const IntVar* const objective,
02693 Solver::LocalSearchFilterBound filter_enum,
02694 Solver::LocalSearchOperation op_enum);
02695 LocalSearchFilter* MakeLocalSearchObjectiveFilter(
02696 const IntVar* const* vars,
02697 const IntVar* const* secondary_vars,
02698 int size,
02699 ResultCallback3<int64, int64, int64, int64>* const values,
02700 const IntVar* const objective,
02701 Solver::LocalSearchFilterBound filter_enum,
02702 Solver::LocalSearchOperation op_enum);
02703
02704
02705
02706 void TopPeriodicCheck();
02707
02708
02709
02710 int TopProgressPercent();
02711
02712
02713
02714
02715 void PushState();
02716 void PopState();
02717
02718
02719
02720 int SearchDepth() const;
02721
02722
02723
02724 int SearchLeftDepth() const;
02725
02726
02727
02728
02729 int SolveDepth() const;
02730
02731
02732 void SetBranchSelector(
02733 ResultCallback1<Solver::DecisionModification, Solver*>* const bs);
02734
02735
02736 DecisionBuilder* MakeApplyBranchSelector(
02737 ResultCallback1<Solver::DecisionModification, Solver*>* const bs);
02738
02739
02740 template <class T> void SaveAndSetValue(T* adr, T val) {
02741 if (*adr != val) {
02742 InternalSaveValue(adr);
02743 *adr = val;
02744 }
02745 }
02746
02747
02748 template <class T> void SaveAndAdd(T* adr, T val) {
02749 if (val != 0) {
02750 InternalSaveValue(adr);
02751 (*adr) += val;
02752 }
02753 }
02754
02755
02756 int64 Rand64(int64 size) {
02757 return random_.Next64() % size;
02758 }
02759
02760
02761 int32 Rand32(int32 size) {
02762 return random_.Next() % size;
02763 }
02764
02765
02766 void ReSeed(int32 seed) {
02767 random_.Reset(seed);
02768 }
02769
02770
02771 void AddFailHook(Action* a);
02772
02773
02774
02775
02776 void ExportProfilingOverview(const string& filename);
02777
02778
02779
02780
02781 bool CurrentlyInSolve() const;
02782
02783
02784
02785 int constraints() const { return constraints_list_.size(); }
02786
02787
02788 void Accept(ModelVisitor* const visitor) const;
02789
02790 void Accept(ModelVisitor* const visitor,
02791 const std::vector<SearchMonitor*>& monitors) const;
02792
02793
02794 Decision* balancing_decision() const { return balancing_decision_.get(); }
02795
02796
02797
02798 void set_fail_intercept(Closure* const c) { fail_intercept_ = c; }
02799 void clear_fail_intercept() { fail_intercept_ = NULL; }
02800
02801 DemonProfiler* demon_profiler() const { return demon_profiler_; }
02802
02803 bool HasName(const PropagationBaseObject* object) const;
02804
02805 Demon* RegisterDemon(Demon* const d);
02806
02807 IntExpr* RegisterIntExpr(IntExpr* const expr);
02808
02809 IntVar* RegisterIntVar(IntVar* const var);
02810
02811
02812 IntervalVar* RegisterIntervalVar(IntervalVar* const var);
02813
02814
02815 Search* ActiveSearch() const;
02816
02817 ModelCache* Cache() const;
02818
02819 bool InstrumentsDemons() const;
02820
02821 bool IsProfilingEnabled() const;
02822
02823 bool InstrumentsVariables() const;
02824
02825 bool NameAllVariables() const;
02826
02827 string model_name() const;
02828
02829 DependencyGraph* Graph() const;
02830
02831 PropagationMonitor* GetPropagationMonitor() const;
02832
02833
02834 void AddPropagationMonitor(PropagationMonitor* const monitor);
02835
02836 friend class BaseIntExpr;
02837 friend class Constraint;
02838 friend class DemonProfiler;
02839 friend class FindOneNeighbor;
02840 friend class IntVar;
02841 friend class PropagationBaseObject;
02842 friend class Queue;
02843 friend class SearchMonitor;
02844
02845 #ifndef SWIG
02846 friend void InternalSaveBooleanVarValue(Solver* const, IntVar* const);
02847 friend void SetQueueCleanerOnFail(Solver* const, IntVar* const);
02848 template<class> friend class SimpleRevFIFO;
02849 template<class K, class V> friend class RevImmutableMultiMap;
02850 #endif
02851
02852 private:
02853 void Init();
02854 void PushState(MarkerType t, const StateInfo& info);
02855 MarkerType PopState(StateInfo* info);
02856 void PushSentinel(int magic_code);
02857 void BacktrackToSentinel(int magic_code);
02858 void CallFailHooks();
02859 void ProcessConstraints();
02860 bool BacktrackOneLevel(Decision** fd);
02861 void JumpToSentinelWhenNested();
02862 void JumpToSentinel();
02863 void check_alloc_state();
02864 void FreezeQueue();
02865 void Enqueue(Demon* d);
02866 void ProcessDemonsOnQueue();
02867 void UnfreezeQueue();
02868 void set_queue_action_on_fail(Action* a);
02869 void set_queue_cleaner_on_fail(IntVar* const var);
02870 void clear_queue_action_on_fail();
02871
02872 void InternalSaveValue(int* valptr);
02873 void InternalSaveValue(int64* valptr);
02874 void InternalSaveValue(uint64* valptr);
02875 void InternalSaveValue(bool* valptr);
02876 void InternalSaveValue(void** valptr);
02877 void InternalSaveValue(int64** valptr) {
02878 InternalSaveValue(reinterpret_cast<void**>(valptr));
02879 }
02880
02881 BaseObject* SafeRevAlloc(BaseObject* ptr);
02882
02883 int* SafeRevAllocArray(int* ptr);
02884 int64* SafeRevAllocArray(int64* ptr);
02885 uint64* SafeRevAllocArray(uint64* ptr);
02886 BaseObject** SafeRevAllocArray(BaseObject** ptr);
02887 IntVar** SafeRevAllocArray(IntVar** ptr);
02888 IntExpr** SafeRevAllocArray(IntExpr** ptr);
02889 Constraint** SafeRevAllocArray(Constraint** ptr);
02890
02891
02892 void* UnsafeRevAllocAux(void* ptr);
02893 template <class T> T* UnsafeRevAlloc(T* ptr) {
02894 return reinterpret_cast<T*>(
02895 UnsafeRevAllocAux(reinterpret_cast<void*>(ptr)));
02896 }
02897 void** UnsafeRevAllocArrayAux(void** ptr);
02898 template <class T> T** UnsafeRevAllocArray(T** ptr) {
02899 return reinterpret_cast<T**>(
02900 UnsafeRevAllocArrayAux(reinterpret_cast<void**>(ptr)));
02901 }
02902
02903 void InitCachedIntConstants();
02904 void InitCachedConstraint();
02905 void InitBuilders();
02906 void DeleteBuilders();
02907
02908
02909
02910
02911 Search* TopLevelSearch() const {
02912 return searches_.at(1);
02913 }
02914
02915
02916
02917 Search* ParentSearch() const {
02918 const size_t search_size = searches_.size();
02919 DCHECK_GT(search_size, 1);
02920 return searches_[search_size - 2];
02921 }
02922
02923
02924 string GetName(const PropagationBaseObject* object);
02925 void SetName(const PropagationBaseObject* object, const string& name);
02926
02927 const string name_;
02928 const SolverParameters parameters_;
02929 hash_map<const PropagationBaseObject*, string> propagation_object_names_;
02930 hash_map<const PropagationBaseObject*, IntegerCastInfo> cast_information_;
02931 hash_set<const Constraint*> cast_constraints_;
02932 const string empty_name_;
02933 scoped_ptr<Queue> queue_;
02934 scoped_ptr<Trail> trail_;
02935 std::vector<Constraint*> constraints_list_;
02936 std::vector<Constraint*> additional_constraints_list_;
02937 std::vector<int> additional_constraints_parent_list_;
02938 SolverState state_;
02939 int64 branches_;
02940 int64 fails_;
02941 int64 decisions_;
02942 int64 demon_runs_[kNumPriorities];
02943 int64 neighbors_;
02944 int64 filtered_neighbors_;
02945 int64 accepted_neighbors_;
02946 scoped_ptr<Action> variable_cleaner_;
02947 scoped_ptr<ClockTimer> timer_;
02948 std::vector<Search*> searches_;
02949 ACMRandom random_;
02950 SimpleRevFIFO<Action*>* fail_hooks_;
02951 uint64 fail_stamp_;
02952 scoped_ptr<Decision> balancing_decision_;
02953
02954 Closure* fail_intercept_;
02955
02956 DemonProfiler* const demon_profiler_;
02957
02958
02959 enum { MIN_CACHED_INT_CONST = -8, MAX_CACHED_INT_CONST = 8 };
02960 IntVar* cached_constants_[MAX_CACHED_INT_CONST + 1 - MIN_CACHED_INT_CONST];
02961
02962
02963 Constraint* true_constraint_;
02964 Constraint* false_constraint_;
02965
02966 scoped_ptr<Decision> fail_decision_;
02967 int constraint_index_;
02968 int additional_constraint_index_;
02969
02970
02971 hash_map<string, IntegerExpressionBuilder*> expression_builders_;
02972 hash_map<string, ConstraintBuilder*> constraint_builders_;
02973 hash_map<string, IntervalVariableBuilder*> interval_builders_;
02974 hash_map<string, SequenceVariableBuilder*> sequence_builders_;
02975
02976 scoped_ptr<ModelCache> model_cache_;
02977 scoped_ptr<DependencyGraph> dependency_graph_;
02978 scoped_ptr<PropagationMonitor> propagation_monitor_;
02979 PropagationMonitor* print_trace_;
02980 int anonymous_variable_index_;
02981
02982 DISALLOW_COPY_AND_ASSIGN(Solver);
02983 };
02984
02985 std::ostream& operator << (std::ostream& out, const Solver* const s);
02986
02987
02988
02989
02990
02991
02992 inline int64 Zero() {
02993 return 0LL;
02994 }
02995
02996
02997
02998
02999
03000
03001
03002
03003
03004
03005
03006 class BaseObject {
03007 public:
03008 BaseObject() {}
03009 virtual ~BaseObject() {}
03010 virtual string DebugString() const { return "BaseObject"; }
03011
03012 private:
03013 DISALLOW_COPY_AND_ASSIGN(BaseObject);
03014 };
03015
03016 std::ostream& operator <<(std::ostream& out, const BaseObject* o);
03017
03018
03019
03020
03021 class PropagationBaseObject : public BaseObject {
03022 public:
03023 explicit PropagationBaseObject(Solver* const s) : solver_(s) {}
03024 virtual ~PropagationBaseObject() {}
03025
03026 virtual string DebugString() const {
03027 if (name().empty()) {
03028 return "PropagationBaseObject";
03029 } else {
03030 return StringPrintf("PropagationBaseObject: %s", name().c_str());
03031 }
03032 }
03033 Solver* solver() const { return solver_; }
03034
03035
03036 void FreezeQueue() { solver_->FreezeQueue(); }
03037
03038
03039
03040 void UnfreezeQueue() { solver_->UnfreezeQueue(); }
03041
03042
03043
03044
03045 void Enqueue(Demon* d) { solver_->Enqueue(d); }
03046
03047
03048
03049 void ProcessDemonsOnQueue() { solver_->ProcessDemonsOnQueue(); }
03050
03051
03052
03053 void set_queue_action_on_fail(Action* a) {
03054 solver_->set_queue_action_on_fail(a);
03055 }
03056
03057
03058 void clear_queue_action_on_fail() {
03059 solver_->clear_queue_action_on_fail();
03060 }
03061
03062
03063 virtual string name() const;
03064 void set_name(const string& name);
03065
03066 bool HasName() const;
03067
03068 virtual string BaseName() const;
03069
03070
03071 private:
03072 Solver* const solver_;
03073 DISALLOW_COPY_AND_ASSIGN(PropagationBaseObject);
03074 };
03075
03076
03077
03078 class Decision : public BaseObject {
03079 public:
03080 Decision() {}
03081 virtual ~Decision() {}
03082
03083
03084 virtual void Apply(Solver* const s) = 0;
03085
03086
03087 virtual void Refute(Solver* const s) = 0;
03088
03089 virtual string DebugString() const {
03090 return "Decision";
03091 }
03092
03093 virtual void Accept(DecisionVisitor* const visitor) const;
03094
03095 private:
03096 DISALLOW_COPY_AND_ASSIGN(Decision);
03097 };
03098
03099
03100
03101 class DecisionVisitor : public BaseObject {
03102 public:
03103 DecisionVisitor() {}
03104 virtual ~DecisionVisitor() {}
03105 virtual void VisitSetVariableValue(IntVar* const var, int64 value);
03106 virtual void VisitSplitVariableDomain(IntVar* const var,
03107 int64 value,
03108 bool start_with_lower_half);
03109 virtual void VisitScheduleOrPostpone(IntervalVar* const var, int64 est);
03110 virtual void VisitRankFirstInterval(SequenceVar* const sequence, int index);
03111 virtual void VisitRankLastInterval(SequenceVar* const sequence, int index);
03112 virtual void VisitUnknownDecision();
03113
03114 private:
03115 DISALLOW_COPY_AND_ASSIGN(DecisionVisitor);
03116 };
03117
03118
03119
03120 class DecisionBuilder : public BaseObject {
03121 public:
03122 DecisionBuilder() {}
03123 virtual ~DecisionBuilder() {}
03124
03125
03126
03127
03128 virtual Decision* Next(Solver* const s) = 0;
03129 virtual string DebugString() const;
03130 #if !defined(SWIG)
03131
03132
03133
03134
03135 virtual void AppendMonitors(Solver* const solver,
03136 std::vector<SearchMonitor*>* const extras);
03137 virtual void Accept(ModelVisitor* const visitor) const;
03138 #endif
03139
03140 private:
03141 DISALLOW_COPY_AND_ASSIGN(DecisionBuilder);
03142 };
03143
03144
03145
03146
03147
03148
03149
03150
03151
03152
03153 class Demon : public BaseObject {
03154 public:
03155
03156
03157 Demon() : stamp_(GG_ULONGLONG(0)) {}
03158 virtual ~Demon() {}
03159
03160
03161 virtual void Run(Solver* const s) = 0;
03162
03163
03164
03165
03166 virtual Solver::DemonPriority priority() const;
03167
03168 virtual string DebugString() const;
03169
03170
03171
03172 void inhibit(Solver* const s);
03173
03174
03175 void desinhibit(Solver* const s);
03176
03177 private:
03178 friend class Queue;
03179 void set_stamp(int64 stamp) { stamp_ = stamp; }
03180 uint64 stamp() const { return stamp_; }
03181 uint64 stamp_;
03182 DISALLOW_COPY_AND_ASSIGN(Demon);
03183 };
03184
03185
03186
03187 class Action : public BaseObject {
03188 public:
03189 Action() {}
03190 virtual ~Action() {}
03191
03192
03193 virtual void Run(Solver* const s) = 0;
03194 virtual string DebugString() const;
03195
03196 private:
03197 DISALLOW_COPY_AND_ASSIGN(Action);
03198 };
03199
03200
03201 class ModelVisitor : public BaseObject {
03202 public:
03203
03204 static const char kAbs[];
03205 static const char kAllDifferent[];
03206 static const char kAllowedAssignments[];
03207 static const char kBetween[];
03208 static const char kConvexPiecewise[];
03209 static const char kCountEqual[];
03210 static const char kCumulative[];
03211 static const char kDeviation[];
03212 static const char kDifference[];
03213 static const char kDisjunctive[];
03214 static const char kDistribute[];
03215 static const char kDivide[];
03216 static const char kDurationExpr[];
03217 static const char kElement[];
03218 static const char kElementEqual[];
03219 static const char kEndExpr[];
03220 static const char kEquality[];
03221 static const char kFalseConstraint[];
03222 static const char kGreater[];
03223 static const char kGreaterOrEqual[];
03224 static const char kIntegerVariable[];
03225 static const char kIntervalBinaryRelation[];
03226 static const char kIntervalDisjunction[];
03227 static const char kIntervalUnaryRelation[];
03228 static const char kIntervalVariable[];
03229 static const char kIsBetween[];
03230 static const char kIsDifferent[];
03231 static const char kIsEqual[];
03232 static const char kIsGreaterOrEqual[];
03233 static const char kIsLessOrEqual[];
03234 static const char kIsMember[];
03235 static const char kLess[];
03236 static const char kLessOrEqual[];
03237 static const char kLinkExprVar[];
03238 static const char kMapDomain[];
03239 static const char kMax[];
03240 static const char kMaxEqual[];
03241 static const char kMember[];
03242 static const char kMin[];
03243 static const char kMinEqual[];
03244 static const char kNoCycle[];
03245 static const char kNonEqual[];
03246 static const char kOpposite[];
03247 static const char kPack[];
03248 static const char kPathCumul[];
03249 static const char kPerformedExpr[];
03250 static const char kProduct[];
03251 static const char kScalProd[];
03252 static const char kScalProdEqual[];
03253 static const char kScalProdGreaterOrEqual[];
03254 static const char kScalProdLessOrEqual[];
03255 static const char kSemiContinuous[];
03256 static const char kSequenceVariable[];
03257 static const char kSquare[];
03258 static const char kStartExpr[];
03259 static const char kSum[];
03260 static const char kSumEqual[];
03261 static const char kSumGreaterOrEqual[];
03262 static const char kSumLessOrEqual[];
03263 static const char kTransition[];
03264 static const char kTrueConstraint[];
03265
03266
03267 static const char kCountAssignedItemsExtension[];
03268 static const char kCountUsedBinsExtension[];
03269 static const char kInt64ToBoolExtension[];
03270 static const char kInt64ToInt64Extension[];
03271 static const char kObjectiveExtension[];
03272 static const char kSearchLimitExtension[];
03273 static const char kUsageEqualVariableExtension[];
03274 static const char kUsageLessConstantExtension[];
03275 static const char kVariableGroupExtension[];
03276 static const char kVariableUsageLessConstantExtension[];
03277 static const char kWeightedSumOfAssignedEqualVariableExtension[];
03278
03279
03280 static const char kActiveArgument[];
03281 static const char kAssumePathsArgument[];
03282 static const char kBranchesLimitArgument[];
03283 static const char kCapacityArgument[];
03284 static const char kCardsArgument[];
03285 static const char kCoefficientsArgument[];
03286 static const char kCountArgument[];
03287 static const char kCumulativeArgument[];
03288 static const char kCumulsArgument[];
03289 static const char kDemandsArgument[];
03290 static const char kDurationMaxArgument[];
03291 static const char kDurationMinArgument[];
03292 static const char kEarlyCostArgument[];
03293 static const char kEarlyDateArgument[];
03294 static const char kEndMaxArgument[];
03295 static const char kEndMinArgument[];
03296 static const char kExpressionArgument[];
03297 static const char kFailuresLimitArgument[];
03298 static const char kFinalStatesArgument[];
03299 static const char kFixedChargeArgument[];
03300 static const char kIndex2Argument[];
03301 static const char kIndexArgument[];
03302 static const char kInitialState[];
03303 static const char kIntervalArgument[];
03304 static const char kIntervalsArgument[];
03305 static const char kLateCostArgument[];
03306 static const char kLateDateArgument[];
03307 static const char kLeftArgument[];
03308 static const char kMaxArgument[];
03309 static const char kMaximizeArgument[];
03310 static const char kMinArgument[];
03311 static const char kNextsArgument[];
03312 static const char kOptionalArgument[];
03313 static const char kRangeArgument[];
03314 static const char kRelationArgument[];
03315 static const char kRightArgument[];
03316 static const char kSequenceArgument[];
03317 static const char kSequencesArgument[];
03318 static const char kSizeArgument[];
03319 static const char kSmartTimeCheckArgument[];
03320 static const char kSolutionLimitArgument[];
03321 static const char kStartMaxArgument[];
03322 static const char kStartMinArgument[];
03323 static const char kStepArgument[];
03324 static const char kTargetArgument[];
03325 static const char kTimeLimitArgument[];
03326 static const char kTransitsArgument[];
03327 static const char kTuplesArgument[];
03328 static const char kValueArgument[];
03329 static const char kValuesArgument[];
03330 static const char kVariableArgument[];
03331 static const char kVarsArgument[];
03332
03333
03334 static const char kMirrorOperation[];
03335 static const char kRelaxedMaxOperation[];
03336 static const char kRelaxedMinOperation[];
03337 static const char kSumOperation[];
03338 static const char kDifferenceOperation[];
03339 static const char kProductOperation[];
03340
03341 virtual ~ModelVisitor();
03342
03343
03344
03345
03346 virtual void BeginVisitModel(const string& solver_name);
03347 virtual void EndVisitModel(const string& solver_name);
03348 virtual void BeginVisitConstraint(const string& type_name,
03349 const Constraint* const constraint);
03350 virtual void EndVisitConstraint(const string& type_name,
03351 const Constraint* const constraint);
03352 virtual void BeginVisitExtension(const string& type);
03353 virtual void EndVisitExtension(const string& type);
03354 virtual void BeginVisitIntegerExpression(const string& type_name,
03355 const IntExpr* const expr);
03356 virtual void EndVisitIntegerExpression(const string& type_name,
03357 const IntExpr* const expr);
03358 virtual void VisitIntegerVariable(const IntVar* const variable,
03359 const IntExpr* const delegate);
03360 virtual void VisitIntegerVariable(const IntVar* const variable,
03361 const string& operation,
03362 int64 value,
03363 const IntVar* const delegate);
03364 virtual void VisitIntervalVariable(const IntervalVar* const variable,
03365 const string& operation,
03366 const IntervalVar* const delegate);
03367 virtual void VisitIntervalVariable(const IntervalVar* const variable,
03368 const string& operation,
03369 const IntervalVar* const * delegate,
03370 int size);
03371 virtual void VisitSequenceVariable(const SequenceVar* const sequence);
03372
03373
03374
03375 virtual void VisitIntegerArgument(const string& arg_name, int64 value);
03376
03377 virtual void VisitIntegerArrayArgument(const string& arg_name,
03378 const int64* const values,
03379 int size);
03380 virtual void VisitIntegerMatrixArgument(const string& arg_name,
03381 const IntTupleSet& tuples);
03382
03383
03384 virtual void VisitIntegerExpressionArgument(
03385 const string& arg_name,
03386 const IntExpr* const argument);
03387
03388 virtual void VisitIntegerVariableArrayArgument(
03389 const string& arg_name,
03390 const IntVar* const * arguments,
03391 int size);
03392
03393
03394 virtual void VisitIntervalArgument(const string& arg_name,
03395 const IntervalVar* const argument);
03396
03397 virtual void VisitIntervalArrayArgument(const string& arg_name,
03398 const IntervalVar* const * argument,
03399 int size);
03400
03401 virtual void VisitSequenceArgument(const string& arg_name,
03402 const SequenceVar* const argument);
03403
03404 virtual void VisitSequenceArrayArgument(const string& arg_name,
03405 const SequenceVar* const * argument,
03406 int size);
03407
03408 #if !defined(SWIG)
03409
03410
03411 void VisitConstIntArrayArgument(const string& arg_name,
03412 const ConstIntArray& argument);
03413 void VisitInt64ToBoolExtension(ResultCallback1<bool, int64>* const callback,
03414 int64 index_min,
03415 int64 index_max);
03416 void VisitInt64ToInt64Extension(ResultCallback1<int64, int64>* const callback,
03417 int64 index_min,
03418 int64 index_max);
03419
03420 void VisitInt64ToInt64AsArray(ResultCallback1<int64, int64>* const callback,
03421 const string& arg_name,
03422 int64 index_max);
03423 void VisitIntegerVariableArrayArgument(
03424 const string& arg_name,
03425 const ConstPtrArray<IntVar>& arguments);
03426 #endif // #if !defined(SWIG)
03427 };
03428
03429
03430
03431
03432
03433
03434
03435 class Constraint : public PropagationBaseObject {
03436 public:
03437 explicit Constraint(Solver* const solver) : PropagationBaseObject(solver) {}
03438 virtual ~Constraint() {}
03439
03440
03441
03442 virtual void Post() = 0;
03443
03444
03445
03446 virtual void InitialPropagate() = 0;
03447 virtual string DebugString() const;
03448
03449
03450
03451 void PostAndPropagate();
03452
03453
03454 virtual void Accept(ModelVisitor* const visitor) const;
03455
03456
03457 bool IsCastConstraint() const;
03458
03459
03460
03461
03462 virtual IntVar* Var();
03463
03464 private:
03465 DISALLOW_COPY_AND_ASSIGN(Constraint);
03466 };
03467
03468
03469
03470
03471 class CastConstraint : public Constraint {
03472 public:
03473 CastConstraint(Solver* const solver, IntVar* const target_var)
03474 : Constraint(solver), target_var_(target_var) {
03475 CHECK_NOTNULL(target_var);
03476 }
03477 virtual ~CastConstraint() {}
03478
03479 IntVar* target_var() const { return target_var_; }
03480
03481 protected:
03482 IntVar* const target_var_;
03483 };
03484
03485
03486 class SearchMonitor : public BaseObject {
03487 public:
03488 static const int kNoProgress = -1;
03489
03490 explicit SearchMonitor(Solver* const s) : solver_(s) {}
03491 virtual ~SearchMonitor() {}
03492
03493 virtual void EnterSearch();
03494
03495
03496 virtual void RestartSearch();
03497
03498
03499 virtual void ExitSearch();
03500
03501
03502 virtual void BeginNextDecision(DecisionBuilder* const b);
03503
03504
03505 virtual void EndNextDecision(DecisionBuilder* const b, Decision* const d);
03506
03507
03508 virtual void ApplyDecision(Decision* const d);
03509
03510
03511 virtual void RefuteDecision(Decision* const d);
03512
03513
03514
03515 virtual void AfterDecision(Decision* const d, bool apply);
03516
03517
03518 virtual void BeginFail();
03519
03520
03521 virtual void EndFail();
03522
03523
03524 virtual void BeginInitialPropagation();
03525
03526
03527 virtual void EndInitialPropagation();
03528
03529
03530
03531
03532 virtual bool AcceptSolution();
03533
03534
03535
03536
03537 virtual bool AtSolution();
03538
03539
03540 virtual void NoMoreSolutions();
03541
03542
03543
03544 virtual bool LocalOptimum();
03545
03546
03547 virtual bool AcceptDelta(Assignment* delta, Assignment* deltadelta);
03548
03549
03550 virtual void AcceptNeighbor();
03551
03552 Solver* solver() const { return solver_; }
03553
03554
03555 void FinishCurrentSearch();
03556
03557
03558 void RestartCurrentSearch();
03559
03560
03561 virtual void PeriodicCheck();
03562
03563
03564
03565 virtual int ProgressPercent() { return kNoProgress; }
03566
03567
03568 virtual void Accept(ModelVisitor* const visitor) const;
03569
03570
03571
03572 virtual void Install();
03573
03574 private:
03575 Solver* const solver_;
03576 DISALLOW_COPY_AND_ASSIGN(SearchMonitor);
03577 };
03578
03579
03580
03581
03582
03583
03584 template <class T> class Rev {
03585 public:
03586 explicit Rev(const T& val) : stamp_(0), value_(val) {}
03587
03588 const T& Value() const { return value_; }
03589
03590 void SetValue(Solver* const s, const T& val) {
03591 if (val != value_) {
03592 if (stamp_ < s->stamp()) {
03593 s->SaveValue(&value_);
03594 stamp_ = s->stamp();
03595 }
03596 value_ = val;
03597 }
03598 }
03599
03600 private:
03601 uint64 stamp_;
03602 T value_;
03603 };
03604
03605
03606 template <class T> class NumericalRev : public Rev<T> {
03607 public:
03608 explicit NumericalRev(const T& val) : Rev<T>(val) {}
03609
03610 void Add(Solver* const s, const T& to_add) {
03611 this->SetValue(s, this->Value() + to_add);
03612 }
03613
03614 void Incr(Solver* const s) {
03615 Add(s, 1);
03616 }
03617
03618 void Decr(Solver* const s) {
03619 Add(s, -1);
03620 }
03621 };
03622
03623
03624
03625
03626
03627
03628 template <class T> class RevArray {
03629 public:
03630 RevArray(int size, const T& val)
03631 : stamps_(new uint64[size]), values_(new T[size]) {
03632 for (int i = 0; i < size; ++i) {
03633 stamps_[i] = 0;
03634 values_[i] = val;
03635 }
03636 }
03637
03638 ~RevArray() {}
03639
03640 const T& Value(int index) const { return values_[index]; }
03641
03642 #if !defined(SWIG)
03643 const T& operator[](int index) const { return values_[index]; }
03644 #endif
03645
03646 void SetValue(Solver* const s, int index, const T& val) {
03647 if (val != values_[index]) {
03648 if (stamps_[index] < s->stamp()) {
03649 s->SaveValue(&values_[index]);
03650 stamps_[index] = s->stamp();
03651 }
03652 values_[index] = val;
03653 }
03654 }
03655
03656 private:
03657 scoped_array<uint64> stamps_;
03658 scoped_array<T> values_;
03659 };
03660
03661
03662 template <class T> class NumericalRevArray : public RevArray<T> {
03663 public:
03664 NumericalRevArray(int size, const T& val) : RevArray<T>(size, val) {}
03665
03666 void Add(Solver* const s, int index, const T& to_add) {
03667 this->SetValue(s, index, this->Value(index) + to_add);
03668 }
03669
03670 void Incr(Solver* const s, int index) {
03671 Add(s, index, 1);
03672 }
03673
03674 void Decr(Solver* const s, int index) {
03675 Add(s, index, -1);
03676 }
03677 };
03678
03679
03680
03681
03682
03683
03684
03685
03686
03687 class IntExpr : public PropagationBaseObject {
03688 public:
03689 explicit IntExpr(Solver* const s) : PropagationBaseObject(s) {}
03690 virtual ~IntExpr() {}
03691
03692 virtual int64 Min() const = 0;
03693 virtual void SetMin(int64 m) = 0;
03694 virtual int64 Max() const = 0;
03695 virtual void SetMax(int64 m) = 0;
03696
03697
03698
03699 virtual void Range(int64* l, int64* u) {
03700 *l = Min();
03701 *u = Max();
03702 }
03703
03704 virtual void SetRange(int64 l, int64 u) {
03705 SetMin(l);
03706 SetMax(u);
03707 }
03708
03709
03710 virtual void SetValue(int64 v) { SetRange(v, v); }
03711
03712
03713 virtual bool Bound() const { return (Min() == Max()); }
03714
03715
03716 virtual bool IsVar() const { return false; }
03717
03718
03719 virtual IntVar* Var() = 0;
03720
03721
03722
03723
03724
03725 IntVar* VarWithName(const string& name);
03726
03727
03728 virtual void WhenRange(Demon* d) = 0;
03729
03730
03731 virtual void Accept(ModelVisitor* const visitor) const;
03732
03733 private:
03734 DISALLOW_COPY_AND_ASSIGN(IntExpr);
03735 };
03736
03737
03738
03739
03740
03741
03742
03743
03744
03745
03746
03747
03748
03749
03750
03751
03752
03753
03754
03755 class IntVarIterator : public BaseObject {
03756 public:
03757 virtual ~IntVarIterator() {}
03758
03759
03760 virtual void Init() = 0;
03761
03762
03763 virtual bool Ok() const = 0;
03764
03765
03766 virtual int64 Value() const = 0;
03767
03768
03769 virtual void Next() = 0;
03770
03771
03772 virtual string DebugString() const {
03773 return "IntVar::Iterator";
03774 }
03775 };
03776
03777
03778
03779
03780 class IntVar : public IntExpr {
03781 public:
03782 explicit IntVar(Solver* const s);
03783 IntVar(Solver* const s, const string& name);
03784 virtual ~IntVar() {}
03785
03786 virtual bool IsVar() const { return true; }
03787 virtual IntVar* Var() { return this; }
03788
03789
03790
03791 virtual int64 Value() const = 0;
03792
03793
03794 virtual void RemoveValue(int64 v) = 0;
03795
03796
03797
03798 virtual void RemoveInterval(int64 l, int64 u) = 0;
03799
03800
03801 virtual void RemoveValues(const int64* const values, int size);
03802
03803
03804 void RemoveValues(const std::vector<int64>& values) {
03805 RemoveValues(values.data(), values.size());
03806 }
03807
03808
03809 virtual void SetValues(const int64* const values, int size);
03810
03811
03812 void SetValues(const std::vector<int64>& values) {
03813 SetValues(values.data(), values.size());
03814 }
03815
03816
03817
03818 virtual void WhenBound(Demon* d) = 0;
03819
03820
03821
03822 virtual void WhenDomain(Demon* d) = 0;
03823
03824
03825 virtual uint64 Size() const = 0;
03826
03827
03828 virtual bool Contains(int64 v) const = 0;
03829
03830
03831
03832 virtual IntVarIterator* MakeHoleIterator(bool reversible) const = 0;
03833
03834
03835
03836 virtual IntVarIterator* MakeDomainIterator(bool reversible) const = 0;
03837
03838
03839 virtual int64 OldMin() const = 0;
03840
03841
03842 virtual int64 OldMax() const = 0;
03843
03844 virtual int VarType() const;
03845
03846
03847 virtual void Accept(ModelVisitor* const visitor) const;
03848
03849 private:
03850 DISALLOW_COPY_AND_ASSIGN(IntVar);
03851 };
03852
03853
03854
03855
03856
03857
03858 class SolutionCollector : public SearchMonitor {
03859 public:
03860 SolutionCollector(Solver* const s, const Assignment* assignment);
03861 explicit SolutionCollector(Solver* const s);
03862 virtual ~SolutionCollector();
03863
03864
03865 void Add(IntVar* const var);
03866 void Add(const std::vector<IntVar*>& vars);
03867 void Add(IntervalVar* const var);
03868 void Add(const std::vector<IntervalVar*>& vars);
03869 void AddObjective(IntVar* const objective);
03870
03871
03872 virtual void EnterSearch();
03873
03874
03875 int solution_count() const;
03876
03877
03878 Assignment* solution(int n) const;
03879
03880
03881 int64 wall_time(int n) const;
03882
03883
03884 int64 branches(int n) const;
03885
03886
03887
03888 int64 failures(int n) const;
03889
03890
03891 int64 objective_value(int n) const;
03892
03893
03894 int64 Value(int n, IntVar* const var) const;
03895
03896
03897 int64 StartValue(int n, IntervalVar* const var) const;
03898
03899
03900 int64 EndValue(int n, IntervalVar* const var) const;
03901
03902
03903 int64 DurationValue(int n, IntervalVar* const var) const;
03904
03905
03906 int64 PerformedValue(int n, IntervalVar* const var) const;
03907
03908 protected:
03909
03910 void PushSolution();
03911
03912 void PopSolution();
03913
03914 void check_index(int n) const;
03915 scoped_ptr<Assignment> prototype_;
03916 std::vector<Assignment*> solutions_;
03917 std::vector<Assignment*> recycle_solutions_;
03918 std::vector<int64> times_;
03919 std::vector<int64> branches_;
03920 std::vector<int64> failures_;
03921 std::vector<int64> objective_values_;
03922
03923 private:
03924 DISALLOW_COPY_AND_ASSIGN(SolutionCollector);
03925 };
03926
03927
03928
03929
03930
03931
03932
03933
03934
03935
03936 class OptimizeVar : public SearchMonitor {
03937 public:
03938 OptimizeVar(Solver* const s, bool maximize, IntVar* const a, int64 step);
03939 virtual ~OptimizeVar();
03940
03941
03942 int64 best() const { return best_; }
03943
03944
03945 IntVar* Var() const { return var_; }
03946
03947 virtual void EnterSearch();
03948 virtual void RestartSearch();
03949 virtual void RefuteDecision(Decision* d);
03950 virtual bool AtSolution();
03951 virtual bool AcceptSolution();
03952 virtual string Print() const;
03953 virtual string DebugString() const;
03954 virtual void Accept(ModelVisitor* const visitor) const;
03955
03956 void ApplyBound();
03957
03958 private:
03959 IntVar* const var_;
03960 int64 step_;
03961 int64 best_;
03962 bool maximize_;
03963 bool found_initial_solution_;
03964
03965 DISALLOW_COPY_AND_ASSIGN(OptimizeVar);
03966 };
03967
03968
03969
03970
03971 class SearchLimit : public SearchMonitor {
03972 public:
03973 explicit SearchLimit(Solver* const s) : SearchMonitor(s), crossed_(false) { }
03974 virtual ~SearchLimit();
03975
03976
03977 bool crossed() const { return crossed_; }
03978
03979
03980
03981
03982
03983 virtual bool Check() = 0;
03984
03985
03986 virtual void Init() = 0;
03987
03988
03989
03990 virtual void Copy(const SearchLimit* const limit) = 0;
03991
03992
03993 virtual SearchLimit* MakeClone() const = 0;
03994
03995
03996 virtual void EnterSearch();
03997 virtual void BeginNextDecision(DecisionBuilder* const b);
03998 virtual void PeriodicCheck();
03999 virtual void RefuteDecision(Decision* const d);
04000 virtual string DebugString() const {
04001 return StringPrintf("SearchLimit(crossed = %i)", crossed_);
04002 }
04003
04004 private:
04005 bool crossed_;
04006 DISALLOW_COPY_AND_ASSIGN(SearchLimit);
04007 };
04008
04009
04010
04011
04012
04013
04014
04015
04016
04017
04018
04019
04020 class NoGood {
04021 public:
04022 ~NoGood();
04023
04024 void AddIntegerVariableEqualValueTerm(IntVar* const var, int64 value);
04025
04026 void AddIntegerVariableNotEqualValueTerm(IntVar* const var, int64 value);
04027
04028
04029
04030
04031 bool Apply(Solver* const solver);
04032
04033 string DebugString() const;
04034
04035
04036 private:
04037 std::vector<NoGoodTerm*> terms_;
04038 };
04039
04040
04041
04042
04043
04044
04045
04046 class NoGoodManager : public SearchMonitor {
04047 public:
04048 explicit NoGoodManager(Solver* const s) : SearchMonitor(s) {}
04049 virtual ~NoGoodManager() {}
04050
04051
04052
04053
04054 virtual void Clear() = 0;
04055
04056 NoGood* MakeNoGood();
04057
04058 virtual void AddNoGood(NoGood* const nogood) = 0;
04059
04060 virtual int NoGoodCount() const = 0;
04061
04062 virtual string DebugString() const = 0;
04063
04064
04065
04066 virtual void EnterSearch();
04067 virtual void BeginNextDecision(DecisionBuilder* const db);
04068 virtual bool AcceptSolution();
04069
04070 private:
04071
04072
04073
04074 virtual void Init() = 0;
04075
04076 virtual void Apply() = 0;
04077
04078 DISALLOW_COPY_AND_ASSIGN(NoGoodManager);
04079 };
04080
04081
04082
04083
04084
04085
04086
04087
04088
04089
04090
04091
04092 class IntervalVar : public PropagationBaseObject {
04093 public:
04094
04095 static const int64 kMinValidValue;
04096
04097 static const int64 kMaxValidValue;
04098 IntervalVar(Solver* const solver, const string& name)
04099 : PropagationBaseObject(solver),
04100 start_expr_(NULL), duration_expr_(NULL), end_expr_(NULL),
04101 performed_expr_(NULL) {
04102 set_name(name);
04103 }
04104 virtual ~IntervalVar() {}
04105
04106
04107
04108 virtual int64 StartMin() const = 0;
04109 virtual int64 StartMax() const = 0;
04110 virtual void SetStartMin(int64 m) = 0;
04111 virtual void SetStartMax(int64 m) = 0;
04112 virtual void SetStartRange(int64 mi, int64 ma) = 0;
04113 virtual void WhenStartRange(Demon* const d) = 0;
04114 virtual void WhenStartBound(Demon* const d) = 0;
04115
04116
04117 virtual int64 DurationMin() const = 0;
04118 virtual int64 DurationMax() const = 0;
04119 virtual void SetDurationMin(int64 m) = 0;
04120 virtual void SetDurationMax(int64 m) = 0;
04121 virtual void SetDurationRange(int64 mi, int64 ma) = 0;
04122 virtual void WhenDurationRange(Demon* const d) = 0;
04123 virtual void WhenDurationBound(Demon* const d) = 0;
04124
04125
04126 virtual int64 EndMin() const = 0;
04127 virtual int64 EndMax() const = 0;
04128 virtual void SetEndMin(int64 m) = 0;
04129 virtual void SetEndMax(int64 m) = 0;
04130 virtual void SetEndRange(int64 mi, int64 ma) = 0;
04131 virtual void WhenEndRange(Demon* const d) = 0;
04132 virtual void WhenEndBound(Demon* const d) = 0;
04133
04134
04135
04136 virtual bool MustBePerformed() const = 0;
04137 virtual bool MayBePerformed() const = 0;
04138 bool CannotBePerformed() const { return !MayBePerformed(); }
04139 bool IsPerformedBound() {
04140 return MustBePerformed() == MayBePerformed();
04141 }
04142 virtual void SetPerformed(bool val) = 0;
04143 virtual void WhenPerformedBound(Demon* const d) = 0;
04144
04145
04146 void WhenAnything(Demon* const d);
04147
04148
04149
04150
04151 IntExpr* StartExpr();
04152 IntExpr* DurationExpr();
04153 IntExpr* EndExpr();
04154 IntExpr* PerformedExpr();
04155
04156
04157 virtual void Accept(ModelVisitor* const visitor) const = 0;
04158
04159 private:
04160 IntExpr* start_expr_;
04161 IntExpr* duration_expr_;
04162 IntExpr* end_expr_;
04163 IntExpr* performed_expr_;
04164 DISALLOW_COPY_AND_ASSIGN(IntervalVar);
04165 };
04166
04167
04168
04169
04170
04171
04172
04173
04174
04175 class SequenceVar : public PropagationBaseObject {
04176 public:
04177 SequenceVar(Solver* const s,
04178 const IntervalVar* const * intervals,
04179 int size,
04180 const string& name);
04181 virtual ~SequenceVar();
04182
04183 virtual string DebugString() const;
04184
04185
04186
04187 void DurationRange(int64* const dmin, int64* const dmax) const;
04188
04189
04190
04191 void HorizonRange(int64* const hmin, int64* const hmax) const;
04192
04193
04194
04195 void ActiveHorizonRange(int64* const hmin, int64* const hmax) const;
04196
04197
04198 int Ranked() const;
04199
04200
04201
04202 int NotRanked() const;
04203
04204
04205
04206 void RankFirst(int index);
04207
04208
04209
04210 void RankNotFirst(int index);
04211
04212
04213
04214 void RankLast(int index);
04215
04216
04217
04218 void RankNotLast(int index);
04219
04220
04221
04222 void AddPrecedence(int before, int after);
04223
04224
04225
04226 void ComputePossibleFirstsAndLasts(std::vector<int>* const possible_firsts,
04227 std::vector<int>* const possible_lasts);
04228
04229
04230
04231
04232
04233
04234 void RankSequence(const std::vector<int>& rank_firsts,
04235 const std::vector<int>& rank_lasts,
04236 const std::vector<int>& unperformed);
04237
04238
04239
04240
04241
04242
04243
04244
04245
04246 void FillSequence(std::vector<int>* const rank_first,
04247 std::vector<int>* const rank_lasts,
04248 std::vector<int>* const unperformed) const;
04249
04250
04251 IntervalVar* Interval(int index) const;
04252
04253
04254 int size() const { return size_; }
04255
04256
04257 virtual void Accept(ModelVisitor* const visitor) const;
04258
04259 private:
04260 bool IsBefore(int before, int after) const;
04261 bool IsActive(int index) const;
04262 void ComputeRanks(const std::vector<int>& rank_first,
04263 const std::vector<int>& rank_last);
04264 void AddPrecedences(const std::vector<int>& rank_first,
04265 const std::vector<int>& rank_last);
04266 void ComputeTransitiveClosure(const std::vector<int>& rank_first,
04267 const std::vector<int>& rank_last);
04268 void MarkUnperformed(const std::vector<int>& unperformed);
04269
04270 scoped_array<IntervalVar*> intervals_;
04271 const int size_;
04272
04273
04274 RevArray<int> ranked_before_;
04275
04276 NumericalRev<int> count_ranked_first_;
04277
04278
04279 NumericalRevArray<int> ranked_after_;
04280
04281 NumericalRev<int> count_ranked_last_;
04282
04283 scoped_ptr<RevBitMatrix> precedence_matrix_;
04284
04285 hash_set<int> first_set_;
04286 hash_set<int> last_set_;
04287 };
04288
04289
04290
04291
04292
04293
04294
04295 class AssignmentElement {
04296 public:
04297 AssignmentElement() : activated_(true) {}
04298
04299 void Activate() { activated_ = true; }
04300 void Deactivate() { activated_ = false; }
04301 bool Activated() const { return activated_; }
04302
04303 private:
04304 bool activated_;
04305 };
04306
04307
04308
04309 class IntVarElement : public AssignmentElement {
04310 public:
04311 IntVarElement();
04312 explicit IntVarElement(IntVar* const var);
04313 void Reset(IntVar* const var);
04314 IntVarElement* Clone();
04315 void Copy(const IntVarElement& element);
04316 IntVar* Var() const { return var_; }
04317 void Store() {
04318 min_ = var_->Min();
04319 max_ = var_->Max();
04320 }
04321 void Restore() { var_->SetRange(min_, max_); }
04322 void LoadFromProto(const IntVarAssignmentProto& int_var_assignment_proto);
04323 void WriteToProto(IntVarAssignmentProto* int_var_assignment_proto) const;
04324
04325 int64 Min() const { return min_; }
04326 void SetMin(int64 m) { min_ = m; }
04327 int64 Max() const { return max_; }
04328 void SetMax(int64 m) { max_ = m; }
04329 int64 Value() const {
04330 DCHECK_EQ(min_, max_);
04331
04332 return min_;
04333 }
04334 bool Bound() const { return (max_ == min_); }
04335 void SetRange(int64 l, int64 u) {
04336 min_ = l;
04337 max_ = u;
04338 }
04339 void SetValue(int64 v) {
04340 min_ = v;
04341 max_ = v;
04342 }
04343 string DebugString() const;
04344
04345 bool operator==(const IntVarElement& element) const;
04346 bool operator!=(const IntVarElement& element) const {
04347 return !(*this == element);
04348 }
04349
04350 private:
04351 IntVar* var_;
04352 int64 min_;
04353 int64 max_;
04354 };
04355
04356
04357
04358 class IntervalVarElement : public AssignmentElement {
04359 public:
04360 IntervalVarElement();
04361 explicit IntervalVarElement(IntervalVar* const var);
04362 void Reset(IntervalVar* const var);
04363 IntervalVarElement* Clone();
04364 void Copy(const IntervalVarElement& element);
04365 IntervalVar* Var() const { return var_; }
04366 void Store();
04367 void Restore();
04368 void LoadFromProto(
04369 const IntervalVarAssignmentProto& interval_var_assignment_proto);
04370 void WriteToProto(
04371 IntervalVarAssignmentProto* interval_var_assignment_proto) const;
04372
04373 int64 StartMin() const { return start_min_; }
04374 int64 StartMax() const { return start_max_; }
04375 int64 StartValue() const {
04376 CHECK_EQ(start_max_, start_min_);
04377 return start_max_;
04378 }
04379 int64 DurationMin() const { return duration_min_; }
04380 int64 DurationMax() const { return duration_max_; }
04381 int64 DurationValue() const {
04382 CHECK_EQ(duration_max_, duration_min_);
04383 return duration_max_;
04384 }
04385 int64 EndMin() const { return end_min_; }
04386 int64 EndMax() const { return end_max_; }
04387 int64 EndValue() const {
04388 CHECK_EQ(end_max_, end_min_);
04389 return end_max_;
04390 }
04391 int64 PerformedMin() const { return performed_min_; }
04392 int64 PerformedMax() const { return performed_max_; }
04393 int64 PerformedValue() const {
04394 CHECK_EQ(performed_max_, performed_min_);
04395 return performed_max_;
04396 }
04397 void SetStartMin(int64 m) { start_min_ = m; }
04398 void SetStartMax(int64 m) { start_max_ = m; }
04399 void SetStartRange(int64 mi, int64 ma) {
04400 start_min_ = mi;
04401 start_max_ = ma;
04402 }
04403 void SetStartValue(int64 v) {
04404 start_min_ = v;
04405 start_max_ = v;
04406 }
04407 void SetDurationMin(int64 m) { duration_min_ = m; }
04408 void SetDurationMax(int64 m) { duration_max_ = m; }
04409 void SetDurationRange(int64 mi, int64 ma) {
04410 duration_min_ = mi;
04411 duration_max_ = ma;
04412 }
04413 void SetDurationValue(int64 v) {
04414 duration_min_ = v;
04415 duration_max_ = v;
04416 }
04417 void SetEndMin(int64 m) { end_min_ = m; }
04418 void SetEndMax(int64 m) { end_max_ = m; }
04419 void SetEndRange(int64 mi, int64 ma) {
04420 end_min_ = mi;
04421 end_max_ = ma;
04422 }
04423 void SetEndValue(int64 v) {
04424 end_min_ = v;
04425 end_max_ = v;
04426 }
04427 void SetPerformedMin(int64 m) { performed_min_ = m; }
04428 void SetPerformedMax(int64 m) { performed_max_ = m; }
04429 void SetPerformedRange(int64 mi, int64 ma) {
04430 performed_min_ = mi;
04431 performed_max_ = ma;
04432 }
04433 void SetPerformedValue(int64 v) {
04434 performed_min_ = v;
04435 performed_max_ = v;
04436 }
04437 string DebugString() const;
04438 bool operator==(const IntervalVarElement& element) const;
04439 bool operator!=(const IntervalVarElement& element) const {
04440 return !(*this == element);
04441 }
04442
04443 private:
04444 int64 start_min_;
04445 int64 start_max_;
04446 int64 duration_min_;
04447 int64 duration_max_;
04448 int64 end_min_;
04449 int64 end_max_;
04450 int64 performed_min_;
04451 int64 performed_max_;
04452 IntervalVar* var_;
04453 };
04454
04455
04456
04457
04458
04459
04460
04461
04462
04463
04464
04465
04466
04467
04468
04469
04470 class SequenceVarElement : public AssignmentElement {
04471 public:
04472 SequenceVarElement();
04473 explicit SequenceVarElement(SequenceVar* const var);
04474 void Reset(SequenceVar* const var);
04475 SequenceVarElement* Clone();
04476 void Copy(const SequenceVarElement& element);
04477 SequenceVar* Var() const { return var_; }
04478 void Store();
04479 void Restore();
04480 void LoadFromProto(
04481 const SequenceVarAssignmentProto& sequence_var_assignment_proto);
04482 void WriteToProto(
04483 SequenceVarAssignmentProto* sequence_var_assignment_proto) const;
04484
04485 #if !defined(SWIG)
04486 const std::vector<int>& ForwardSequence() const;
04487 const std::vector<int>& BackwardSequence() const;
04488 const std::vector<int>& Unperformed() const;
04489 #endif
04490 void SetSequence(const std::vector<int>& forward_sequence,
04491 const std::vector<int>& backward_sequence,
04492 const std::vector<int>& unperformed);
04493 void SetForwardSequence(const std::vector<int>& forward_sequence);
04494 void SetBackwardSequence(const std::vector<int>& backward_sequence);
04495 void SetUnperformed(const std::vector<int>& unperformed);
04496
04497 string DebugString() const;
04498
04499 bool operator==(const SequenceVarElement& element) const;
04500 bool operator!=(const SequenceVarElement& element) const {
04501 return !(*this == element);
04502 }
04503
04504 private:
04505 bool CheckClassInvariants();
04506
04507 SequenceVar* var_;
04508 std::vector<int> forward_sequence_;
04509 std::vector<int> backward_sequence_;
04510 std::vector<int> unperformed_;
04511 };
04512
04513
04514
04515 template <class V, class E> class AssignmentContainer {
04516 public:
04517 AssignmentContainer() {}
04518 E& Add(V* const var) {
04519 CHECK_NOTNULL(var);
04520 int index = -1;
04521 if (!Find(var, &index)) {
04522 return FastAdd(var);
04523 } else {
04524 return elements_[index];
04525 }
04526 }
04527
04528 E& FastAdd(V* const var) {
04529 DCHECK(var != NULL);
04530 E e(var);
04531 elements_.push_back(e);
04532 return elements_.back();
04533 }
04534 void Clear() {
04535 elements_.clear();
04536 if (!elements_map_.empty()) {
04537 elements_map_.clear();
04538 }
04539 }
04540 bool Empty() const {
04541 return elements_.empty();
04542 }
04543
04544 void Copy(const AssignmentContainer<V, E>& container) {
04545 for (int i = 0; i < container.elements_.size(); ++i) {
04546 const E& element = container.elements_[i];
04547 const V* const var = element.Var();
04548 int index = -1;
04549 if (i < elements_.size() && elements_[i].Var() == var) {
04550 index = i;
04551 } else if (!Find(var, &index)) {
04552 continue;
04553 }
04554 DCHECK_GE(index, 0);
04555 E& local_element(elements_[index]);
04556 local_element.Copy(element);
04557 if (element.Activated()) {
04558 local_element.Activate();
04559 } else {
04560 local_element.Deactivate();
04561 }
04562 }
04563 }
04564 bool Contains(const V* const var) const {
04565 int index;
04566 return Find(var, &index);
04567 }
04568 E& MutableElement(const V* const var) {
04569 int index = -1;
04570 const bool found = Find(var, &index);
04571 CHECK(found) << "Unknown variable " << var->DebugString() << " in solution";
04572 return MutableElement(index);
04573 }
04574 const E& Element(const V* const var) const {
04575 int index = -1;
04576 const bool found = Find(var, &index);
04577 CHECK(found) << "Unknown variable " << var->DebugString() << " in solution";
04578 return Element(index);
04579 }
04580 const std::vector<E>& elements() const { return elements_; }
04581 E& MutableElement(int index) { return elements_[index]; }
04582 const E& Element(int index) const { return elements_[index]; }
04583 int Size() const { return elements_.size(); }
04584 void Store() {
04585 for (int i = 0; i < elements_.size(); ++i) {
04586 elements_[i].Store();
04587 }
04588 }
04589 void Restore() {
04590 for (int i = 0; i < elements_.size(); ++i) {
04591 E& element = elements_[i];
04592 if (element.Activated()) {
04593 element.Restore();
04594 }
04595 }
04596 }
04597
04598
04599
04600
04601 bool operator==(const AssignmentContainer<V, E>& container) const {
04602
04603 if (Size() != container.Size()) {
04604 return false;
04605 }
04606
04607 EnsureMapIsUpToDate();
04608
04609
04610
04611 typedef ConstIter<std::vector<E> > Iterator;
04612 for (Iterator it(container.elements_); !it.at_end(); ++it) {
04613 const int position = FindWithDefault(elements_map_, it->Var(), -1);
04614 if (position < 0 || elements_[position] != *it) {
04615 return false;
04616 }
04617 }
04618 return true;
04619 }
04620 bool operator!=(const AssignmentContainer<V, E>& container) const {
04621 return !(*this == container);
04622 }
04623
04624 private:
04625 void EnsureMapIsUpToDate() const {
04626 hash_map<const V*, int>* map =
04627 const_cast<hash_map<const V*, int>* >(&elements_map_);
04628 for (int i = map->size(); i < elements_.size(); ++i) {
04629 (*map)[elements_[i].Var()] = i;
04630 }
04631 }
04632 bool Find(const V* const var, int* index) const {
04633 EnsureMapIsUpToDate();
04634 DCHECK_EQ(elements_map_.size(), elements_.size());
04635 return FindCopy(elements_map_, var, index);
04636 }
04637
04638 std::vector<E> elements_;
04639 hash_map<const V*, int> elements_map_;
04640 };
04641
04642
04643
04644
04645
04646 class Assignment : public PropagationBaseObject {
04647 public:
04648 typedef AssignmentContainer<IntVar, IntVarElement> IntContainer;
04649 typedef AssignmentContainer<IntervalVar, IntervalVarElement>
04650 IntervalContainer;
04651 typedef AssignmentContainer<SequenceVar, SequenceVarElement>
04652 SequenceContainer;
04653
04654 explicit Assignment(Solver* const s);
04655 explicit Assignment(const Assignment* const copy);
04656 virtual ~Assignment();
04657
04658 void Clear();
04659 bool Empty() const {
04660 return int_var_container_.Empty() &&
04661 interval_var_container_.Empty() &&
04662 sequence_var_container_.Empty();
04663 }
04664 int Size() const {
04665 return NumIntVars() + NumIntervalVars() + NumSequenceVars();
04666 }
04667 int NumIntVars() const {
04668 return int_var_container_.Size();
04669 }
04670 int NumIntervalVars() const {
04671 return interval_var_container_.Size();
04672 }
04673 int NumSequenceVars() const {
04674 return sequence_var_container_.Size();
04675 }
04676 void Store();
04677 void Restore();
04678
04679
04680
04681 bool Load(const string& filename);
04682 #if !defined(SWIG)
04683 bool Load(File* file);
04684 #endif // #if !defined(SWIG)
04685 void Load(const AssignmentProto& proto);
04686
04687 bool Save(const string& filename) const;
04688 #if !defined(SWIG)
04689 bool Save(File* file) const;
04690 #endif // #if !defined(SWIG)
04691 void Save(AssignmentProto* const proto) const;
04692
04693 void AddObjective(IntVar* const v);
04694 IntVar* Objective() const;
04695 bool HasObjective() const { return (objective_element_.Var() != NULL); }
04696 int64 ObjectiveMin() const;
04697 int64 ObjectiveMax() const;
04698 int64 ObjectiveValue() const;
04699 bool ObjectiveBound() const;
04700 void SetObjectiveMin(int64 m);
04701 void SetObjectiveMax(int64 m);
04702 void SetObjectiveValue(int64 value);
04703 void SetObjectiveRange(int64 l, int64 u);
04704
04705 IntVarElement& Add(IntVar* const v);
04706 void Add(IntVar* const* vars, int size);
04707 void Add(const std::vector<IntVar*>& v);
04708
04709 IntVarElement& FastAdd(IntVar* const v);
04710 int64 Min(const IntVar* const v) const;
04711 int64 Max(const IntVar* const v) const;
04712 int64 Value(const IntVar* const v) const;
04713 bool Bound(const IntVar* const v) const;
04714 void SetMin(const IntVar* const v, int64 m);
04715 void SetMax(const IntVar* const v, int64 m);
04716 void SetRange(const IntVar* const v, int64 l, int64 u);
04717 void SetValue(const IntVar* const v, int64 value);
04718
04719 IntervalVarElement& Add(IntervalVar* const v);
04720 void Add(IntervalVar* const * vars, int size);
04721 void Add(const std::vector<IntervalVar*>& vars);
04722
04723 IntervalVarElement& FastAdd(IntervalVar* const v);
04724 int64 StartMin(const IntervalVar* const v) const;
04725 int64 StartMax(const IntervalVar* const v) const;
04726 int64 StartValue(const IntervalVar* const v) const;
04727 int64 DurationMin(const IntervalVar* const v) const;
04728 int64 DurationMax(const IntervalVar* const v) const;
04729 int64 DurationValue(const IntervalVar* const c) const;
04730 int64 EndMin(const IntervalVar* const v) const;
04731 int64 EndMax(const IntervalVar* const v) const;
04732 int64 EndValue(const IntervalVar* const v) const;
04733 int64 PerformedMin(const IntervalVar* const v) const;
04734 int64 PerformedMax(const IntervalVar* const v) const;
04735 int64 PerformedValue(const IntervalVar* const v) const;
04736 void SetStartMin(const IntervalVar* const v, int64 m);
04737 void SetStartMax(const IntervalVar* const v, int64 m);
04738 void SetStartRange(const IntervalVar* const v, int64 mi, int64 ma);
04739 void SetStartValue(const IntervalVar* const v, int64 value);
04740 void SetDurationMin(const IntervalVar* const v, int64 m);
04741 void SetDurationMax(const IntervalVar* const v, int64 m);
04742 void SetDurationRange(const IntervalVar* const v, int64 mi, int64 ma);
04743 void SetDurationValue(const IntervalVar* const v, int64 value);
04744 void SetEndMin(const IntervalVar* const v, int64 m);
04745 void SetEndMax(const IntervalVar* const v, int64 m);
04746 void SetEndRange(const IntervalVar* const v, int64 mi, int64 ma);
04747 void SetEndValue(const IntervalVar* const v, int64 value);
04748 void SetPerformedMin(const IntervalVar* const v, int64 m);
04749 void SetPerformedMax(const IntervalVar* const v, int64 m);
04750 void SetPerformedRange(const IntervalVar* const v, int64 mi, int64 ma);
04751 void SetPerformedValue(const IntervalVar* const v, int64 value);
04752
04753 SequenceVarElement& Add(SequenceVar* const v);
04754 void Add(SequenceVar* const * vars, int size);
04755 void Add(const std::vector<SequenceVar*>& vars);
04756
04757 SequenceVarElement& FastAdd(SequenceVar* const v);
04758 #if !defined(SWIG)
04759 const std::vector<int>& ForwardSequence(const SequenceVar* const v) const;
04760 const std::vector<int>& BackwardSequence(const SequenceVar* const v) const;
04761 const std::vector<int>& Unperformed(const SequenceVar* const v) const;
04762 #endif
04763 void SetSequence(const SequenceVar* const v,
04764 const std::vector<int>& forward_sequence,
04765 const std::vector<int>& backward_sequence,
04766 const std::vector<int>& unperformed);
04767 void SetForwardSequence(const SequenceVar* const v,
04768 const std::vector<int>& forward_sequence);
04769 void SetBackwardSequence(const SequenceVar* const v,
04770 const std::vector<int>& backward_sequence);
04771 void SetUnperformed(const SequenceVar* const v,
04772 const std::vector<int>& unperformed);
04773
04774 void Activate(const IntVar* const v);
04775 void Deactivate(const IntVar* const v);
04776 bool Activated(const IntVar* const v) const;
04777
04778 void Activate(const IntervalVar* const v);
04779 void Deactivate(const IntervalVar* const v);
04780 bool Activated(const IntervalVar* const v) const;
04781
04782 void Activate(const SequenceVar* const v);
04783 void Deactivate(const SequenceVar* const v);
04784 bool Activated(const SequenceVar* const v) const;
04785
04786 void ActivateObjective();
04787 void DeactivateObjective();
04788 bool ActivatedObjective() const;
04789
04790 virtual string DebugString() const;
04791
04792 bool Contains(const IntVar* const var) const;
04793 bool Contains(const IntervalVar* const var) const;
04794 bool Contains(const SequenceVar* const var) const;
04795
04796 void Copy(const Assignment* assignment);
04797
04798
04799 const IntContainer& IntVarContainer() const {
04800 return int_var_container_;
04801 }
04802 IntContainer& MutableIntVarContainer() {
04803 return int_var_container_;
04804 }
04805 const IntervalContainer& IntervalVarContainer() const {
04806 return interval_var_container_;
04807 }
04808 IntervalContainer& MutableIntervalVarContainer() {
04809 return interval_var_container_;
04810 }
04811 const SequenceContainer& SequenceVarContainer() const {
04812 return sequence_var_container_;
04813 }
04814 SequenceContainer& MutableSequenceVarContainer() {
04815 return sequence_var_container_;
04816 }
04817 bool operator==(const Assignment& assignment) const {
04818 return int_var_container_ == assignment.int_var_container_
04819 && interval_var_container_ == assignment.interval_var_container_
04820 && sequence_var_container_ == assignment.sequence_var_container_
04821 && objective_element_ == assignment.objective_element_;
04822 }
04823 bool operator!=(const Assignment& assignment) const {
04824 return !(*this == assignment);
04825 }
04826
04827 private:
04828 IntContainer int_var_container_;
04829 IntervalContainer interval_var_container_;
04830 SequenceContainer sequence_var_container_;
04831 IntVarElement objective_element_;
04832 DISALLOW_COPY_AND_ASSIGN(Assignment);
04833 };
04834
04835 std::ostream& operator<<(std::ostream& out, const Assignment& assignment);
04836
04837
04838
04839 class Pack : public Constraint {
04840 public:
04841 Pack(Solver* const s,
04842 const IntVar* const * vars,
04843 int vsize,
04844 int64 number_of_bins);
04845
04846 virtual ~Pack();
04847
04848
04849
04850
04851
04852
04853
04854
04855
04856
04857
04858
04859 void AddWeightedSumLessOrEqualConstantDimension(const std::vector<int64>& weights,
04860 const std::vector<int64>& bounds);
04861
04862
04863
04864 void AddWeightedSumEqualVarDimension(const std::vector<int64>& weights,
04865 const std::vector<IntVar*>& loads);
04866
04867
04868
04869
04870
04871
04872
04873
04874
04875
04876 void AddSumVariableWeightsLessOrEqualConstantDimension(
04877 const std::vector<IntVar*>& weights,
04878 const std::vector<int64>& capacities);
04879
04880
04881
04882 void AddWeightedSumOfAssignedDimension(const std::vector<int64>& weights,
04883 IntVar* const cost_var);
04884
04885
04886
04887 void AddCountUsedBinDimension(IntVar* const count_var);
04888
04889
04890
04891 void AddCountAssignedItemsDimension(IntVar* const count_var);
04892
04893
04894 virtual void Post();
04895 void ClearAll();
04896 void PropagateDelayed();
04897 virtual void InitialPropagate();
04898 void Propagate();
04899 void OneDomain(int var_index);
04900 virtual string DebugString() const;
04901 bool IsUndecided(int64 var_index, int64 bin_index) const;
04902 void SetImpossible(int64 var_index, int64 bin_index);
04903 void Assign(int64 var_index, int64 bin_index);
04904 bool IsAssignedStatusKnown(int64 var_index) const;
04905 bool IsPossible(int64 var_index, int64 bin_index) const;
04906 IntVar* AssignVar(int64 var_index, int64 bin_index) const;
04907 void SetAssigned(int64 var_index);
04908 void SetUnassigned(int64 var_index);
04909 void RemoveAllPossibleFromBin(int64 bin_index);
04910 void AssignAllPossibleToBin(int64 bin_index);
04911 void AssignFirstPossibleToBin(int64 bin_index);
04912 void AssignAllRemainingItems();
04913 void UnassignAllRemainingItems();
04914
04915 virtual void Accept(ModelVisitor* const visitor) const;
04916
04917 private:
04918 bool IsInProcess() const;
04919 scoped_array<IntVar*> vars_;
04920 const int vsize_;
04921 const int64 bins_;
04922 std::vector<Dimension*> dims_;
04923 scoped_ptr<RevBitMatrix> unprocessed_;
04924 std::vector<std::vector<int64> > forced_;
04925 std::vector<std::vector<int64> > removed_;
04926 scoped_array<IntVarIterator*> holes_;
04927 uint64 stamp_;
04928 Demon* demon_;
04929 std::vector<std::pair<int64, int64> > to_set_;
04930 std::vector<std::pair<int64, int64> > to_unset_;
04931 bool in_process_;
04932 };
04933
04934
04935
04936
04937
04938 class SolutionPool : public BaseObject {
04939 public:
04940 SolutionPool() {}
04941 virtual ~SolutionPool() {}
04942
04943
04944
04945 virtual void Initialize(Assignment* const assignment) = 0;
04946
04947
04948
04949 virtual void RegisterNewSolution(Assignment* const assignment) = 0;
04950
04951
04952
04953 virtual void GetNextSolution(Assignment* const assignment) = 0;
04954
04955
04956
04957 virtual bool SyncNeeded(Assignment* const local_assignment) = 0;
04958 };
04959
04960
04961 }
04962 #endif // OR_TOOLS_CONSTRAINT_SOLVER_CONSTRAINT_SOLVER_H_