View source with raw comments or as raw
    1/*  $Id$
    2
    3    Part of CPL(R) (Constraint Logic Programming over Reals)
    4
    5    Author:        Leslie De Koninck
    6    E-mail:        Leslie.DeKoninck@cs.kuleuven.be
    7    WWW:           http://www.swi-prolog.org
    8		   http://www.ai.univie.ac.at/cgi-bin/tr-online?number+95-09
    9    Copyright (C): 2004, K.U. Leuven and
   10		   1992-1995, Austrian Research Institute for
   11		              Artificial Intelligence (OFAI),
   12			      Vienna, Austria
   13
   14    This software is part of Leslie De Koninck's master thesis, supervised
   15    by Bart Demoen and daily advisor Tom Schrijvers.  It is based on CLP(Q,R)
   16    by Christian Holzbaur for SICStus Prolog and distributed under the
   17    license details below with permission from all mentioned authors.
   18
   19    This program is free software; you can redistribute it and/or
   20    modify it under the terms of the GNU General Public License
   21    as published by the Free Software Foundation; either version 2
   22    of the License, or (at your option) any later version.
   23
   24    This program is distributed in the hope that it will be useful,
   25    but WITHOUT ANY WARRANTY; without even the implied warranty of
   26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   27    GNU General Public License for more details.
   28
   29    You should have received a copy of the GNU Lesser General Public
   30    License along with this library; if not, write to the Free Software
   31    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   32
   33    As a special exception, if you link this library with other files,
   34    compiled with a Free Software compiler, to produce an executable, this
   35    library does not by itself cause the resulting executable to be covered
   36    by the GNU General Public License. This exception does not however
   37    invalidate any other reasons why the executable file might be covered by
   38    the GNU General Public License.
   39*/
   40
   41:- module(bb_r,
   42	[
   43	    bb_inf/3,
   44	    bb_inf/5,
   45	    vertex_value/2
   46	]).   47:- use_module(library(error), [type_error/2]).   48:- use_module(bv_r,
   49	[
   50	    deref/2,
   51	    deref_var/2,
   52	    determine_active_dec/1,
   53	    inf/2,
   54	    iterate_dec/2,
   55	    sup/2,
   56	    var_with_def_assign/2
   57	]).   58:- use_module(nf_r,
   59	[
   60	    {}/1,
   61	    entailed/1,
   62	    nf/2,
   63	    nf_constant/2,
   64	    repair/2,
   65	    wait_linear/3
   66	]).   67
   68% bb_inf(Ints,Term,Inf)
   69%
   70% Finds the infimum of Term where the variables Ints are to be integers.
   71% The infimum is stored in Inf.
   72
   73bb_inf(Is,Term,Inf) :-
   74	bb_inf(Is,Term,Inf,_,0.001).
   75
   76bb_inf(Is,Term,Inf,Vertex,Eps) :-
   77	nf(Eps,ENf),
   78	nf_constant(ENf,EpsN),
   79	wait_linear(Term,Nf,bb_inf_internal(Is,Nf,EpsN,Inf,Vertex)).
   80
   81% ---------------------------------------------------------------------
   82
   83% bb_inf_internal(Is,Lin,Eps,Inf,Vertex)
   84%
   85% Finds an infimum Inf for linear expression in normal form Lin, where
   86% all variables in Is are to be integers. Eps denotes the margin in which
   87% we accept a number as an integer (to deal with rounding errors etc.).
   88
   89% The incumbent must survive the backtracking that drives the search, but
   90% it must not survive the call itself.  It is therefore kept in a mutable
   91% term that is local to this call rather than in a global variable, which
   92% would clobber a global of the same name in the calling program and would
   93% make nested calls interfere.
   94
   95bb_inf_internal(Is,Lin,Eps,Inf,Vertex) :-
   96	State = state(none),
   97	(   bb_intern(Is,IsNf,Eps),
   98	    repair(Lin,LinR),	% bb_narrow ...
   99	    deref(LinR,Lind),
  100	    var_with_def_assign(Dep,Lind),
  101	    determine_active_dec(Lind),
  102	    bb_loop(Dep,IsNf,Eps,State),
  103	    fail
  104	;   arg(1,State,InfVal-Vertex),
  105	    {Inf =:= InfVal}
  106	).
  107
  108% bb_loop(Opt,Is,Eps,State)
  109%
  110% Minimizes the value of Opt where variables Is have to be integer values.
  111% Eps denotes the rounding error that is acceptable. This predicate can be
  112% backtracked to try different strategies.
  113
  114bb_loop(Opt,Is,Eps,State) :-
  115	bb_reoptimize(Opt,Inf),
  116	bb_better_bound(State,Inf),
  117	vertex_value(Is,Ivs),
  118	(   bb_first_nonint(Is,Ivs,Eps,Viol,Floor,Ceiling)
  119	->  bb_branch(Viol,Floor,Ceiling),
  120	    bb_loop(Opt,Is,Eps,State)
  121	;   round_values(Ivs,RoundVertex),
  122	    nb_setarg(1,State,Inf-RoundVertex) % new provisional optimum
  123	).
  124
  125% bb_reoptimize(Obj,Inf)
  126%
  127% Minimizes the value of Obj and puts the result in Inf.
  128% This new minimization is necessary as making a bound integer may yield a
  129% different optimum. The added inequalities may also have led to binding.
  130
  131bb_reoptimize(Obj,Inf) :-
  132	(   var(Obj)
  133	->  iterate_dec(Obj,Inf)
  134	;   Inf = Obj
  135	).
  136
  137% bb_better_bound(State,Inf)
  138%
  139% Checks if the new infimum Inf is better than the previous one (if such exists).
  140
  141bb_better_bound(State,Inf) :-
  142	arg(1,State,Best),
  143	(   Best = Inc-_
  144	->  Inf - Inc < -1.0e-10
  145	;   true
  146	).
  147
  148% bb_branch(V,U,L)
  149%
  150% Stores that V =< U or V >= L, can be used for different strategies within bb_loop/3.
  151
  152bb_branch(V,U,_) :- {V =< U}.
  153bb_branch(V,_,L) :- {V >= L}.
  154
  155% vertex_value(Vars,Values)
  156%
  157% Returns in <Values> the current values of the variables in <Vars>.
  158
  159vertex_value([],[]).
  160vertex_value([X|Xs],[V|Vs]) :-
  161	rhs_value(X,V),
  162	vertex_value(Xs,Vs).
  163
  164% rhs_value(X,Value)
  165%
  166% Returns in <Value> the current value of variable <X>.
  167
  168rhs_value(Xn,Value) :-
  169	(   nonvar(Xn)
  170	->  Value = Xn
  171	;   var(Xn)
  172	->  deref_var(Xn,Xd),
  173	    Xd = [I,R|_],
  174	    Value is R+I
  175	).
  176
  177% bb_first_nonint(Ints,Rhss,Eps,Viol,Floor,Ceiling)
  178%
  179% Finds the first variable in Ints which doesn't have an active integer bound.
  180% Rhss contain the Rhs (R + I) values corresponding to the variables.
  181% The first variable that hasn't got an active integer bound, is returned in
  182% Viol. The floor and ceiling of its actual bound is returned in Floor and Ceiling.
  183
  184bb_first_nonint([I|Is],[Rhs|Rhss],Eps,Viol,F,C) :-
  185	(   Floor is floor(Rhs+1.0e-10),
  186	    Ceiling is ceiling(Rhs-1.0e-10),
  187	    Eps - min(Rhs-Floor,Ceiling-Rhs) < -1.0e-10
  188	->  Viol = I,
  189	    F = Floor,
  190	    C = Ceiling
  191	;   bb_first_nonint(Is,Rhss,Eps,Viol,F,C)
  192	).
  193
  194% round_values([X|Xs],[Xr|Xrs])
  195%
  196% Rounds of the values of the first list into the second list.
  197
  198round_values([],[]).
  199round_values([X|Xs],[Y|Ys]) :-
  200	Y is round(X),
  201	round_values(Xs,Ys).
  202
  203% bb_intern([X|Xs],[Xi|Xis],Eps)
  204%
  205% Turns the elements of the first list into integers into the second
  206% list via bb_intern/4.
  207
  208bb_intern([],[],_).
  209bb_intern([X|Xs],[Xi|Xis],Eps) :-
  210	nf(X,Xnf),
  211	bb_intern(Xnf,Xi,X,Eps),
  212	bb_intern(Xs,Xis,Eps).
  213
  214
  215% bb_intern(Nf,X,Term,Eps)
  216%
  217% Makes sure that Term which is normalized into Nf, is integer.
  218% X contains the possibly changed Term. If Term is a variable,
  219% then its bounds are hightened or lowered to the next integer.
  220% Otherwise, it is checked it Term is integer.
  221
  222bb_intern([],X,_,_) :-
  223	!,
  224	X = 0.0.
  225bb_intern([v(I,[])],X,_,Eps) :-
  226	!,
  227	X = I,
  228	min(I-floor(I+1e-010),ceiling(I-1e-010)-I) - Eps < 1e-010.
  229bb_intern([v(One,[V^1])],X,_,_) :-
  230	Test is One - 1.0,
  231	Test =< 1e-010,
  232	Test >= -1e-010,
  233	!,
  234	V = X,
  235	bb_narrow_lower(X),
  236	bb_narrow_upper(X).
  237bb_intern(_,_,Term,_) :-
  238	type_error(var, Term).
  239
  240% bb_narrow_lower(X)
  241%
  242% Narrows the lower bound so that it is an integer bound.
  243% We do this by finding the infimum of X and asserting that X
  244% is larger than the first integer larger or equal to the infimum
  245% (second integer if X is to be strict larger than the first integer).
  246
  247bb_narrow_lower(X) :-
  248	(   inf(X,Inf)
  249	->  Bound is ceiling(Inf-1.0e-10),
  250	    (   entailed(X > Bound)
  251	    ->  {X >= Bound+1}
  252	    ;   {X >= Bound}
  253	    )
  254	;   true
  255	).
  256
  257% bb_narrow_upper(X)
  258%
  259% See bb_narrow_lower/1. This predicate handles the upper bound.
  260
  261bb_narrow_upper(X) :-
  262	(   sup(X,Sup)
  263	->  Bound is floor(Sup+1.0e-10),
  264	    (   entailed(X < Bound)
  265	    ->  {X =< Bound-1}
  266	    ;   {X =< Bound}
  267	    )
  268	;   true
  269	).
  270
  271		 /*******************************
  272		 *	       SANDBOX		*
  273		 *******************************/
  274:- multifile
  275	sandbox:safe_primitive/1.  276
  277sandbox:safe_primitive(bb_r:bb_inf(_,_,_)).
  278sandbox:safe_primitive(bb_r:bb_inf(_,_,_,_,_))