added support for search options

This commit is contained in:
Denys Duchier 2011-12-03 22:04:04 +01:00
parent 9ebcae23ea
commit 9a78735aaf
4 changed files with 63 additions and 25 deletions

View File

@ -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
===============================

View File

@ -62,21 +62,21 @@ namespace generic_gecode
struct GenericDFS: GenericEngine
{
DFS<GenericSpace> 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<GenericSpace> 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<GenericSpace> 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<GenericEngine*>(new GenericDFS(this))
? static_cast<GenericEngine*>(new GenericDFS(this,opt))
: (restart
? static_cast<GenericEngine*>(new GenericRestart(this))
: static_cast<GenericEngine*>(new GenericBAB(this)));
? static_cast<GenericEngine*>(new GenericRestart(this,opt))
: static_cast<GenericEngine*>(new GenericBAB(this,opt)));
}
int _new_ivar(IntVar& v)

View File

@ -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 =

View File

@ -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, []).