View source with raw comments or as raw
    1/*  $Id$
    2
    3    Part of CLP(Q) (Constraint Logic Programming over Rationals)
    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): 2006, K.U. Leuven and
   10		   1992-1995, Austrian Research Institute for
   11		              Artificial Intelligence (OFAI),
   12			      Vienna, Austria
   13
   14    This software is based on CLP(Q,R) by Christian Holzbaur for SICStus
   15    Prolog and distributed under the license details below with permission from
   16    all mentioned authors.
   17
   18    This program is free software; you can redistribute it and/or
   19    modify it under the terms of the GNU General Public License
   20    as published by the Free Software Foundation; either version 2
   21    of the License, or (at your option) any later version.
   22
   23    This program is distributed in the hope that it will be useful,
   24    but WITHOUT ANY WARRANTY; without even the implied warranty of
   25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   26    GNU General Public License for more details.
   27
   28    You should have received a copy of the GNU Lesser General Public
   29    License along with this library; if not, write to the Free Software
   30    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   31
   32    As a special exception, if you link this library with other files,
   33    compiled with a Free Software compiler, to produce an executable, this
   34    library does not by itself cause the resulting executable to be covered
   35    by the GNU General Public License. This exception does not however
   36    invalidate any other reasons why the executable file might be covered by
   37    the GNU General Public License.
   38*/
   39
   40:- module(bb_q,
   41	[
   42	    bb_inf/3,
   43	    bb_inf/4,
   44	    vertex_value/2
   45	]).   46:- use_module(bv_q,
   47	[
   48	    deref/2,
   49	    deref_var/2,
   50	    determine_active_dec/1,
   51	    inf/2,
   52	    iterate_dec/2,
   53	    sup/2,
   54	    var_with_def_assign/2
   55	]).   56:- use_module(nf_q,
   57	[
   58	    {}/1,
   59	    entailed/1,
   60	    nf/2,
   61	    nf_constant/2,
   62	    repair/2,
   63	    wait_linear/3
   64	]).   65
   66% bb_inf(Ints,Term,Inf)
   67%
   68% Finds the infimum of Term where the variables Ints are to be integers.
   69% The infimum is stored in Inf.
   70
   71bb_inf(Is,Term,Inf) :-
   72	bb_inf(Is,Term,Inf,_).
   73
   74bb_inf(Is,Term,Inf,Vertex) :-
   75	wait_linear(Term,Nf,bb_inf_internal(Is,Nf,Inf,Vertex)).
   76
   77% ---------------------------------------------------------------------
   78
   79% bb_inf_internal(Is,Lin,Inf,Vertex)
   80%
   81% Finds an infimum <Inf> for linear expression in normal form <Lin>, where
   82% all variables in <Is> are to be integers.
   83
   84bb_inf_internal(Is,Lin,_,_) :-
   85	bb_intern(Is,IsNf),
   86	nb_delete(prov_opt),
   87	repair(Lin,LinR),	% bb_narrow ...
   88	deref(LinR,Lind),
   89	var_with_def_assign(Dep,Lind),
   90	determine_active_dec(Lind),
   91	bb_loop(Dep,IsNf),
   92	fail.
   93bb_inf_internal(_,_,Inf,Vertex) :-
   94	nb_current(prov_opt,InfVal-Vertex),
   95	{Inf =:= InfVal},
   96	nb_delete(prov_opt).
   97
   98% bb_loop(Opt,Is)
   99%
  100% Minimizes the value of Opt where variables Is have to be integer values.
  101
  102bb_loop(Opt,Is) :-
  103	bb_reoptimize(Opt,Inf),
  104	bb_better_bound(Inf),
  105	vertex_value(Is,Ivs),
  106	(   bb_first_nonint(Is,Ivs,Viol,Floor,Ceiling)
  107	->  bb_branch(Viol,Floor,Ceiling),
  108	    bb_loop(Opt,Is)
  109	;   nb_setval(prov_opt,Inf-Ivs) % new provisional optimum
  110	).
  111
  112% bb_reoptimize(Obj,Inf)
  113%
  114% Minimizes the value of Obj and puts the result in Inf.
  115% This new minimization is necessary as making a bound integer may yield a
  116% different optimum. The added inequalities may also have led to binding.
  117
  118bb_reoptimize(Obj,Inf) :-
  119	var(Obj),
  120	iterate_dec(Obj,Inf).
  121bb_reoptimize(Obj,Inf) :-
  122	nonvar(Obj),
  123	Inf = Obj.
  124
  125% bb_better_bound(Inf)
  126%
  127% Checks if the new infimum Inf is better than the previous one (if such exists).
  128
  129bb_better_bound(Inf) :-
  130	nb_current(prov_opt,Inc-_), !,
  131	Inf < Inc.
  132bb_better_bound(_).
  133
  134% bb_branch(V,U,L)
  135%
  136% Stores that V =< U or V >= L, can be used for different strategies within
  137% bb_loop/3.
  138
  139bb_branch(V,U,_) :- {V =< U}.
  140bb_branch(V,_,L) :- {V >= L}.
  141
  142% vertex_value(Vars,Values)
  143%
  144% Returns in <Values> the current values of the variables in <Vars>.
  145
  146vertex_value([],[]).
  147vertex_value([X|Xs],[V|Vs]) :-
  148	rhs_value(X,V),
  149	vertex_value(Xs,Vs).
  150
  151% rhs_value(X,Value)
  152%
  153% Returns in <Value> the current value of variable <X>.
  154
  155rhs_value(Xn,Value) :-
  156	(   nonvar(Xn)
  157	->  Value = Xn
  158	;   var(Xn)
  159	->  deref_var(Xn,Xd),
  160	    Xd = [I,R|_],
  161	    Value is R+I
  162	).
  163
  164% bb_first_nonint(Ints,Rhss,Eps,Viol,Floor,Ceiling)
  165%
  166% Finds the first variable in Ints which doesn't have an active integer bound.
  167% Rhss contain the Rhs (R + I) values corresponding to the variables.
  168% The first variable that hasn't got an active integer bound, is returned in
  169% Viol. The floor and ceiling of its actual bound is returned in Floor and Ceiling.
  170
  171bb_first_nonint([I|Is],[Rhs|Rhss],Viol,F,C) :-
  172	(   integer(Rhs)
  173	->  bb_first_nonint(Is,Rhss,Viol,F,C)
  174	;   Viol = I,
  175	    F is floor(Rhs),
  176	    C is ceiling(Rhs)
  177	).
  178
  179% bb_intern([X|Xs],[Xi|Xis])
  180%
  181% Turns the elements of the first list into integers into the second
  182% list via bb_intern/3.
  183
  184bb_intern([],[]).
  185bb_intern([X|Xs],[Xi|Xis]) :-
  186	nf(X,Xnf),
  187	bb_intern(Xnf,Xi,X),
  188	bb_intern(Xs,Xis).
  189
  190
  191% bb_intern(Nf,X,Term)
  192%
  193% Makes sure that Term which is normalized into Nf, is integer.
  194% X contains the possibly changed Term. If Term is a variable,
  195% then its bounds are hightened or lowered to the next integer.
  196% Otherwise, it is checked it Term is integer.
  197
  198bb_intern([],X,_) :-
  199	!,
  200	X = 0.
  201bb_intern([v(I,[])],X,_) :-
  202	!,
  203	integer(I),
  204	X = I.
  205bb_intern([v(1,[V^1])],X,_) :-
  206	!,
  207	V = X,
  208	bb_narrow_lower(X),
  209	bb_narrow_upper(X).
  210bb_intern(_,_,Term) :-
  211	throw(instantiation_error(bb_inf(Term,_),1)).
  212
  213% bb_narrow_lower(X)
  214%
  215% Narrows the lower bound so that it is an integer bound.
  216% We do this by finding the infimum of X and asserting that X
  217% is larger than the first integer larger or equal to the infimum
  218% (second integer if X is to be strict larger than the first integer).
  219
  220bb_narrow_lower(X) :-
  221	(   inf(X,Inf)
  222	->  Bound is ceiling(Inf),
  223	    (   entailed(X > Bound)
  224	    ->  {X >= Bound+1}
  225	    ;   {X >= Bound}
  226	    )
  227	;   true
  228	).
  229
  230% bb_narrow_upper(X)
  231%
  232% See bb_narrow_lower/1. This predicate handles the upper bound.
  233
  234bb_narrow_upper(X) :-
  235	(   sup(X,Sup)
  236	->  Bound is floor(Sup),
  237	    (   entailed(X < Bound)
  238	    ->  {X =< Bound-1}
  239	    ;   {X =< Bound}
  240	    )
  241	;   true
  242	).
  243
  244		 /*******************************
  245		 *	       SANDBOX		*
  246		 *******************************/
  247:- multifile
  248	sandbox:safe_primitive/1.  249
  250sandbox:safe_primitive(bb_q:bb_inf(_,_,_)).
  251sandbox:safe_primitive(bb_q:bb_inf(_,_,_,_))