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(bv_r,
   48	[
   49	    deref/2,
   50	    deref_var/2,
   51	    determine_active_dec/1,
   52	    inf/2,
   53	    iterate_dec/2,
   54	    sup/2,
   55	    var_with_def_assign/2
   56	]).   57:- use_module(nf_r,
   58	[
   59	    {}/1,
   60	    entailed/1,
   61	    nf/2,
   62	    nf_constant/2,
   63	    repair/2,
   64	    wait_linear/3
   65	]).   66
   67% bb_inf(Ints,Term,Inf)
   68%
   69% Finds the infimum of Term where the variables Ints are to be integers.
   70% The infimum is stored in Inf.
   71
   72bb_inf(Is,Term,Inf) :-
   73	bb_inf(Is,Term,Inf,_,0.001).
   74
   75bb_inf(Is,Term,Inf,Vertex,Eps) :-
   76	nf(Eps,ENf),
   77	nf_constant(ENf,EpsN),
   78	wait_linear(Term,Nf,bb_inf_internal(Is,Nf,EpsN,Inf,Vertex)).
   79
   80% ---------------------------------------------------------------------
   81
   82% bb_inf_internal(Is,Lin,Eps,Inf,Vertex)
   83%
   84% Finds an infimum Inf for linear expression in normal form Lin, where
   85% all variables in Is are to be integers. Eps denotes the margin in which
   86% we accept a number as an integer (to deal with rounding errors etc.).
   87
   88bb_inf_internal(Is,Lin,Eps,_,_) :-
   89	bb_intern(Is,IsNf,Eps),
   90	nb_delete(prov_opt),
   91	repair(Lin,LinR),	% bb_narrow ...
   92	deref(LinR,Lind),
   93	var_with_def_assign(Dep,Lind),
   94	determine_active_dec(Lind),
   95	bb_loop(Dep,IsNf,Eps),
   96	fail.
   97bb_inf_internal(_,_,_,Inf,Vertex) :-
   98	catch(nb_getval(prov_opt,InfVal-Vertex),_,fail),
   99	{Inf =:= InfVal},
  100	nb_delete(prov_opt).
  101
  102% bb_loop(Opt,Is,Eps)
  103%
  104% Minimizes the value of Opt where variables Is have to be integer values.
  105% Eps denotes the rounding error that is acceptable. This predicate can be
  106% backtracked to try different strategies.
  107
  108bb_loop(Opt,Is,Eps) :-
  109	bb_reoptimize(Opt,Inf),
  110	bb_better_bound(Inf),
  111	vertex_value(Is,Ivs),
  112	(   bb_first_nonint(Is,Ivs,Eps,Viol,Floor,Ceiling)
  113	->  bb_branch(Viol,Floor,Ceiling),
  114	    bb_loop(Opt,Is,Eps)
  115	;   round_values(Ivs,RoundVertex),
  116	    nb_setval(prov_opt,Inf-RoundVertex) % new provisional optimum
  117	).
  118
  119% bb_reoptimize(Obj,Inf)
  120%
  121% Minimizes the value of Obj and puts the result in Inf.
  122% This new minimization is necessary as making a bound integer may yield a
  123% different optimum. The added inequalities may also have led to binding.
  124
  125bb_reoptimize(Obj,Inf) :-
  126	var(Obj),
  127	iterate_dec(Obj,Inf).
  128bb_reoptimize(Obj,Inf) :-
  129	nonvar(Obj),
  130	Inf = Obj.
  131
  132% bb_better_bound(Inf)
  133%
  134% Checks if the new infimum Inf is better than the previous one (if such exists).
  135
  136bb_better_bound(Inf) :-
  137	catch((nb_getval(prov_opt,Inc-_),Inf - Inc < -1.0e-10),_,true).
  138
  139% bb_branch(V,U,L)
  140%
  141% Stores that V =< U or V >= L, can be used for different strategies within bb_loop/3.
  142
  143bb_branch(V,U,_) :- {V =< U}.
  144bb_branch(V,_,L) :- {V >= L}.
  145
  146% vertex_value(Vars,Values)
  147%
  148% Returns in <Values> the current values of the variables in <Vars>.
  149
  150vertex_value([],[]).
  151vertex_value([X|Xs],[V|Vs]) :-
  152	rhs_value(X,V),
  153	vertex_value(Xs,Vs).
  154
  155% rhs_value(X,Value)
  156%
  157% Returns in <Value> the current value of variable <X>.
  158
  159rhs_value(Xn,Value) :-
  160	(   nonvar(Xn)
  161	->  Value = Xn
  162	;   var(Xn)
  163	->  deref_var(Xn,Xd),
  164	    Xd = [I,R|_],
  165	    Value is R+I
  166	).
  167
  168% bb_first_nonint(Ints,Rhss,Eps,Viol,Floor,Ceiling)
  169%
  170% Finds the first variable in Ints which doesn't have an active integer bound.
  171% Rhss contain the Rhs (R + I) values corresponding to the variables.
  172% The first variable that hasn't got an active integer bound, is returned in
  173% Viol. The floor and ceiling of its actual bound is returned in Floor and Ceiling.
  174
  175bb_first_nonint([I|Is],[Rhs|Rhss],Eps,Viol,F,C) :-
  176	(   Floor is floor(Rhs+1.0e-10),
  177	    Ceiling is ceiling(Rhs-1.0e-10),
  178	    Eps - min(Rhs-Floor,Ceiling-Rhs) < -1.0e-10
  179	->  Viol = I,
  180	    F = Floor,
  181	    C = Ceiling
  182	;   bb_first_nonint(Is,Rhss,Eps,Viol,F,C)
  183	).
  184
  185% round_values([X|Xs],[Xr|Xrs])
  186%
  187% Rounds of the values of the first list into the second list.
  188
  189round_values([],[]).
  190round_values([X|Xs],[Y|Ys]) :-
  191	Y is round(X),
  192	round_values(Xs,Ys).
  193
  194% bb_intern([X|Xs],[Xi|Xis],Eps)
  195%
  196% Turns the elements of the first list into integers into the second
  197% list via bb_intern/4.
  198
  199bb_intern([],[],_).
  200bb_intern([X|Xs],[Xi|Xis],Eps) :-
  201	nf(X,Xnf),
  202	bb_intern(Xnf,Xi,X,Eps),
  203	bb_intern(Xs,Xis,Eps).
  204
  205
  206% bb_intern(Nf,X,Term,Eps)
  207%
  208% Makes sure that Term which is normalized into Nf, is integer.
  209% X contains the possibly changed Term. If Term is a variable,
  210% then its bounds are hightened or lowered to the next integer.
  211% Otherwise, it is checked it Term is integer.
  212
  213bb_intern([],X,_,_) :-
  214	!,
  215	X = 0.0.
  216bb_intern([v(I,[])],X,_,Eps) :-
  217	!,
  218	X = I,
  219	min(I-floor(I+1e-010),ceiling(I-1e-010)-I) - Eps < 1e-010.
  220bb_intern([v(One,[V^1])],X,_,_) :-
  221	Test is One - 1.0,
  222	Test =< 1e-010,
  223	Test >= -1e-010,
  224	!,
  225	V = X,
  226	bb_narrow_lower(X),
  227	bb_narrow_upper(X).
  228bb_intern(_,_,Term,_) :-
  229	throw(instantiation_error(bb_inf(Term,_,_),1)).
  230
  231% bb_narrow_lower(X)
  232%
  233% Narrows the lower bound so that it is an integer bound.
  234% We do this by finding the infimum of X and asserting that X
  235% is larger than the first integer larger or equal to the infimum
  236% (second integer if X is to be strict larger than the first integer).
  237
  238bb_narrow_lower(X) :-
  239	(   inf(X,Inf)
  240	->  Bound is ceiling(Inf-1.0e-10),
  241	    (   entailed(X > Bound)
  242	    ->  {X >= Bound+1}
  243	    ;   {X >= Bound}
  244	    )
  245	;   true
  246	).
  247
  248% bb_narrow_upper(X)
  249%
  250% See bb_narrow_lower/1. This predicate handles the upper bound.
  251
  252bb_narrow_upper(X) :-
  253	(   sup(X,Sup)
  254	->  Bound is floor(Sup+1.0e-10),
  255	    (   entailed(X < Bound)
  256	    ->  {X =< Bound-1}
  257	    ;   {X =< Bound}
  258	    )
  259	;   true
  260	).
  261
  262		 /*******************************
  263		 *	       SANDBOX		*
  264		 *******************************/
  265:- multifile
  266	sandbox:safe_primitive/1.  267
  268sandbox:safe_primitive(bb_r:bb_inf(_,_,_)).
  269sandbox:safe_primitive(bb_r:bb_inf(_,_,_,_,_))