From 9a78735aaf4c44e055d1ea62c400cc2d29a8a5bd Mon Sep 17 00:00:00 2001 From: Denys Duchier Date: Sat, 3 Dec 2011 22:04:04 +0100 Subject: [PATCH] added support for search options --- library/gecode/DOC.txt | 18 ++++++++- library/gecode/gecode-common.icc | 14 +++---- library/gecode/gecode_yap.cc | 9 ++++- library/gecode/gecode_yap_hand_written.yap | 47 +++++++++++++++------- 4 files changed, 63 insertions(+), 25 deletions(-) diff --git a/library/gecode/DOC.txt b/library/gecode/DOC.txt index 24d2049ce..b37143f88 100644 --- a/library/gecode/DOC.txt +++ b/library/gecode/DOC.txt @@ -60,10 +60,24 @@ represented by atoms with the same name as the Gecode constant SEARCHING FOR SOLUTIONS ======================= - SolSpace := search(Space) + SolSpace := search(Space) This is a backtrackable predicate that enumerates all solution spaces -(SolSpace). +(SolSpace). It may also take options: + + SolSpace := search(Space,Options) + +Options is a list whose elements maybe: + +restart + to select the Restart search engine +threads=N + to activate the parallel search engine and control the number of + workers (see Gecode doc) +c_d=N + to set the commit distance for recomputation +a_d=N + to set the adaptive distance for recomputation EXTRACTING INFO FROM A SOLUTION =============================== diff --git a/library/gecode/gecode-common.icc b/library/gecode/gecode-common.icc index 6e83ddcc2..80cb69ab5 100644 --- a/library/gecode/gecode-common.icc +++ b/library/gecode/gecode-common.icc @@ -62,21 +62,21 @@ namespace generic_gecode struct GenericDFS: GenericEngine { DFS engine; - GenericDFS(GenericSpace* s) : engine(s) {} + GenericDFS(GenericSpace* s,Search::Options& opt) : engine(s,opt) {} virtual GenericSpace* next(void) { return engine.next(); } }; struct GenericBAB: GenericEngine { BAB engine; - GenericBAB(GenericSpace* s) : engine(s) {} + GenericBAB(GenericSpace* s,Search::Options& opt) : engine(s,opt) {} virtual GenericSpace* next(void) { return engine.next(); } }; struct GenericRestart: GenericEngine { Restart engine; - GenericRestart(GenericSpace* s): engine(s) {} + GenericRestart(GenericSpace* s,Search::Options& opt): engine(s,opt) {} virtual GenericSpace* next(void) { return engine.next(); } }; @@ -144,14 +144,14 @@ namespace generic_gecode BoolVar get_bvar(int i) const { return (_bvars) ? (*_bvars)[i] : bvars[i]; } SetVar get_svar(int i) const { return (_svars) ? (*_svars)[i] : svars[i]; } - GenericEngine* new_engine(bool restart=false) + GenericEngine* new_engine(bool restart, Search::Options& opt) { freeze(); return (optim.what == Optimizing::OPT_NONE) - ? static_cast(new GenericDFS(this)) + ? static_cast(new GenericDFS(this,opt)) : (restart - ? static_cast(new GenericRestart(this)) - : static_cast(new GenericBAB(this))); + ? static_cast(new GenericRestart(this,opt)) + : static_cast(new GenericBAB(this,opt))); } int _new_ivar(IntVar& v) diff --git a/library/gecode/gecode_yap.cc b/library/gecode/gecode_yap.cc index c45b28397..416867189 100644 --- a/library/gecode/gecode_yap.cc +++ b/library/gecode/gecode_yap.cc @@ -158,8 +158,15 @@ extern "C" YAP_Term arg2 = YAP_ARG2; YAP_Term arg3 = YAP_ARG3; bool restart = YAP_IntOfTerm(YAP_ArgOfTerm(1, arg3)); + double threads = YAP_FloatOfTerm(YAP_ArgOfTerm(2, arg3)); + unsigned int c_d = YAP_IntOfTerm(YAP_ArgOfTerm(3, arg3)); + unsigned int a_d = YAP_IntOfTerm(YAP_ArgOfTerm(4, arg3)); + Search::Options opt; + opt.threads = threads; + opt.c_d = c_d; + opt.a_d = a_d; GenericSpace* space = gecode_Space_from_term(arg1); - GenericEngine* engine = space->new_engine(restart); + GenericEngine* engine = space->new_engine(restart,opt); YAP_Term y_engine = YAP_NewOpaqueObject(gecode_engine_tag, sizeof(GenericEngine*)); GenericEngine** ptr = diff --git a/library/gecode/gecode_yap_hand_written.yap b/library/gecode/gecode_yap_hand_written.yap index 7f43205b1..e5b89be4d 100644 --- a/library/gecode/gecode_yap_hand_written.yap +++ b/library/gecode/gecode_yap_hand_written.yap @@ -344,25 +344,42 @@ maximize(Space,IVar1,IVar2) :- assert_is_IntVar(IVar2,IVar2_), gecode_space_maximize_ratio(Space_,IVar1_,IVar2_). -gecode_search_options_init(search_options(0)). +gecode_search_options_init(search_options(0,1.0,8,2)). +gecode_search_options_offset(restart,1). +gecode_search_options_offset(threads,2). +gecode_search_options_offset(c_d ,3). +gecode_search_options_offset(a_d ,4). -gecode_search_options_offset(restart,0). +gecode_search_option_set(O,V,R) :- + gecode_search_options_offset(O,I), + setarg(I,R,V). -gecode_search_options_from_alist(L,O) :- - gecode_search_options_init(O), - gecode_search_options_process_alist(L,O). +gecode_search_options_from_alist(L,R) :- + gecode_search_options_init(R), + gecode_search_options_process_alist(L,R). -gecode_search_options_process_alist([],O). -gecode_search_options_process_alist([F=V|L],O) :- !, - gecode_search_options_set(F,V,O), - gecode_search_options_process_alist(L,O). -gecode_search_options_process_alist([F|L],O) :- !, - gecode_search_options_set(F,1,O), - gecode_search_options_process_alist(L,O). +gecode_search_options_process_alist([],R). +gecode_search_options_process_alist([H|T],R) :- !, + gecode_search_options_process1(H,R), + gecode_search_options_process_alist(T,R). -gecode_search_options_set(F,V,O) :- - gecode_search_options_offset(F,I), - setarg(I,O,V). +gecode_search_options_process1(restart,R) :- !, + gecode_search_option_set(restart,1,R). +gecode_search_options_process1(threads=N,R) :- !, + (integer(N) -> V is float(N) + ; (float(N) -> V=N + ; throw(bad_search_option_value(threads=N)))), + gecode_search_option_set(threads,V,R). +gecode_search_options_process1(c_d=N,R) :- !, + (integer(N) -> V=N + ; throw(bad_search_option_value(c_d=N))), + gecode_search_option_set(c_d,V,R). +gecode_search_options_process1(a_d=N,R) :- !, + (integer(N) -> V=N + ; throw(bad_search_option_value(a_d=N))), + gecode_search_option_set(a_d,V,R). +gecode_search_options_process1(O,_) :- + throw(gecode_error(unrecognized_search_option(O))). search(Space, Solution) :- search(Space, Solution, []).