From 7c78f52a8973dd611a217ea47af6e213f5841356 Mon Sep 17 00:00:00 2001 From: Denys Duchier Date: Thu, 1 Dec 2011 22:48:28 +0100 Subject: [PATCH] support search options, added 'restart' --- library/gecode/gecode-common.icc | 13 ++++++++-- library/gecode/gecode_yap.cc | 6 +++-- library/gecode/gecode_yap_hand_written.yap | 28 +++++++++++++++++++++- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/library/gecode/gecode-common.icc b/library/gecode/gecode-common.icc index 029227e17..6e83ddcc2 100644 --- a/library/gecode/gecode-common.icc +++ b/library/gecode/gecode-common.icc @@ -73,6 +73,13 @@ namespace generic_gecode virtual GenericSpace* next(void) { return engine.next(); } }; + struct GenericRestart: GenericEngine + { + Restart engine; + GenericRestart(GenericSpace* s): engine(s) {} + virtual GenericSpace* next(void) { return engine.next(); } + }; + struct GenericSpace: Space { Optimizing optim; @@ -137,12 +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() + GenericEngine* new_engine(bool restart=false) { freeze(); return (optim.what == Optimizing::OPT_NONE) ? static_cast(new GenericDFS(this)) - : static_cast(new GenericBAB(this)); + : (restart + ? static_cast(new GenericRestart(this)) + : static_cast(new GenericBAB(this))); } int _new_ivar(IntVar& v) diff --git a/library/gecode/gecode_yap.cc b/library/gecode/gecode_yap.cc index 51e0d4dd2..c45b28397 100644 --- a/library/gecode/gecode_yap.cc +++ b/library/gecode/gecode_yap.cc @@ -156,8 +156,10 @@ extern "C" { YAP_Term arg1 = YAP_ARG1; YAP_Term arg2 = YAP_ARG2; + YAP_Term arg3 = YAP_ARG3; + bool restart = YAP_IntOfTerm(YAP_ArgOfTerm(1, arg3)); GenericSpace* space = gecode_Space_from_term(arg1); - GenericEngine* engine = space->new_engine(); + GenericEngine* engine = space->new_engine(restart); YAP_Term y_engine = YAP_NewOpaqueObject(gecode_engine_tag, sizeof(GenericEngine*)); GenericEngine** ptr = @@ -1045,7 +1047,7 @@ extern "C" gecode_engine_handler.fail_handler = gecode_engine_fail_handler; gecode_engine_handler.write_handler = gecode_engine_write_handler; gecode_engine_tag = YAP_NewOpaqueType(&gecode_engine_handler); - YAP_UserCPredicate("gecode_new_engine", gecode_new_engine, 2); + YAP_UserCPredicate("gecode_new_engine", gecode_new_engine, 3); #ifdef DISJUNCTOR // opaque disjunctors and clauses gecode_disjunctor_handler.write_handler = gecode_disjunctor_write_handler; diff --git a/library/gecode/gecode_yap_hand_written.yap b/library/gecode/gecode_yap_hand_written.yap index 466670b93..7f43205b1 100644 --- a/library/gecode/gecode_yap_hand_written.yap +++ b/library/gecode/gecode_yap_hand_written.yap @@ -344,13 +344,38 @@ 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_offset(restart,0). + +gecode_search_options_from_alist(L,O) :- + gecode_search_options_init(O), + gecode_search_options_process_alist(L,O). + +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_set(F,V,O) :- + gecode_search_options_offset(F,I), + setarg(I,O,V). + search(Space, Solution) :- + search(Space, Solution, []). + +search(Space, Solution, Alist) :- assert_is_Space(Space,Space_), assert_var(Solution), - gecode_new_engine(Space_,Engine_), + gecode_search_options_from_alist(Alist,O), + gecode_new_engine(Space_,Engine_,O), gecode_engine_search(Engine_,Solution_), Solution='Space'(Solution_). + %% INSPECTING VARIABLES get_for_vars([],Space,[],F). @@ -647,6 +672,7 @@ new_setvars_(L,Space,N,X1,X2) :- length(L,N), new_setvars(L,Space,X1,X2). (X := clause(Disj)) :- !, new_clause(X,Disj). (X := search(Y)) :- !, search(Y,X). +(X := search(Y,L)) :- !, search(Y,X,L). % these should be autogenerated: (C += forward(X,Y)) :- !, new_forward(C,X,Y).