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(clpcd_bb, 42 [ 43 bb_inf/5 44 ]). 45 46:- use_module(library(clpcd/domain_ops)). 47:- use_module(library(clpcd/bv)). 48:- use_module(library(clpcd/nf)). 49:- use_module(library(clpcd/solve)). 50:- use_module(library(clpcd/detact)). 51 52% bb_inf(Ints,Term,Inf) 53% 54% Finds the infimum of Term where the variables Ints are to be integers. 55% The infimum is stored in Inf. 56 57bb_inf(CLP, Is, Term, Inf, Vertex) :- 58 wait_linear(CLP, Term, Nf, bb_inf_internal(CLP, Is, Nf, Inf, Vertex)). 59 60% --------------------------------------------------------------------- 61 62% bb_inf_internal(Is,Lin,Inf,Vertex) 63% 64% Finds an infimum Inf for linear expression in normal form Lin, where 65% all variables in Is are to be integers. 66 67bb_inf_internal(CLP, Is, Lin, _, _) :- 68 bb_intern(Is, CLP, IsNf), 69 nb_delete(prov_opt), 70 repair(CLP, Lin, LinR), % bb_narrow ... 71 deref(CLP,LinR,Lind), 72 var_with_def_assign(CLP, Dep, Lind), 73 determine_active_dec(CLP, Lind), 74 bb_loop(CLP, Dep, IsNf), 75 fail. 76bb_inf_internal(CLP, _, _, Inf, Vertex) :- 77 nb_current(prov_opt,InfVal-Vertex), 78 add_constraint(Inf =:= InfVal, CLP), 79 nb_delete(prov_opt). 80 81% bb_loop(CLP, Opt, Is) 82% 83% Minimizes the value of Opt where variables Is have to be integer values. 84% This predicate can be backtracked to try different strategies. 85 86bb_loop(CLP, Opt, Is) :- 87 bb_reoptimize(CLP, Opt, Inf), 88 bb_better_bound(CLP, Inf), 89 vertex_value(Is, CLP, Ivs), 90 ( bb_first_nonint(CLP, Is, Ivs, Viol, Floor, Ceiling) 91 -> bb_branch(CLP, Viol, Floor, Ceiling), 92 bb_loop(CLP, Opt, Is) 93 ; round_values(Ivs,CLP,RoundVertex), 94 nb_setval(prov_opt,Inf-RoundVertex) % new provisional optimum 95 ). 96 97% bb_reoptimize(Obj,Inf) 98% 99% Minimizes the value of Obj and puts the result in Inf. 100% This new minimization is necessary as making a bound integer may yield a 101% different optimum. The added inequalities may also have led to binding. 102 103bb_reoptimize(CLP, Obj, Inf) :- 104 var(Obj), 105 !, 106 iterate_dec(CLP, Obj, Inf). 107bb_reoptimize(_, Obj, Inf) :- 108 Inf = Obj. 109 110% bb_better_bound(Inf) 111% 112% Checks if the new infimum Inf is better than the previous one (if such exists). 113 114bb_better_bound(CLP, Inf) :- 115 nb_current(prov_opt,Inc-_), !, 116 compare_d(CLP, <, Inf, Inc). 117bb_better_bound(_, _). 118 119% bb_branch(V,U,L) 120% 121% Stores that V =< U or V >= L, can be used for different strategies within bb_loop/3. 122 123bb_branch(CLP, V, U, _) :- add_constraint(V =< U, CLP). 124bb_branch(CLP, V, _, L) :- add_constraint(V >= L, CLP). 125 126% bb_first_nonint(Ints,Rhss,Viol,Floor,Ceiling) 127% 128% Finds the first variable in Ints which doesn't have an active integer bound. 129% Rhss contain the Rhs (R + I) values corresponding to the variables. 130% The first variable that hasn't got an active integer bound, is returned in 131% Viol. The floor and ceiling of its actual bound is returned in Floor and Ceiling. 132 133bb_first_nonint(CLP, [I|Is], [Rhs|Rhss], Viol, F, C) :- 134 ( floor_d(CLP, Rhs, Floor), 135 ceiling_d(CLP, Rhs, Ceiling), 136 compare_d(CLP, <, epsilon, min(Rhs-Floor, Ceiling-Rhs)) 137 -> Viol = I, 138 F = Floor, 139 C = Ceiling 140 ; bb_first_nonint(CLP, Is, Rhss, Viol, F, C) 141 ). 142 143% round_values([X|Xs],CLP,[Xr|Xrs]) 144% 145% Rounds of the values of the first list into the second list. 146 147round_values([],_,[]). 148round_values([X|Xs],CLP,[Y|Ys]) :- 149 eval_d(CLP, round(X), Y), 150 round_values(Xs,CLP,Ys). 151 152% bb_intern([X|Xs],[Xi|Xis]) 153% 154% Turns the elements of the first list into integers into the second 155% list via bb_intern/3. 156 157bb_intern([], _, []). 158bb_intern([X|Xs], CLP, [Xi|Xis]) :- 159 nf(X, CLP, Xnf), 160 bb_intern(Xnf, CLP, Xi, X), 161 bb_intern(Xs, CLP, Xis). 162 163 164% bb_intern(Nf,X,Term) 165% 166% Makes sure that Term which is normalized into Nf, is integer. 167% X contains the possibly changed Term. If Term is a variable, 168% then its bounds are hightened or lowered to the next integer. 169% Otherwise, it is checked it Term is integer. 170 171bb_intern([], _, X, _) :- 172 !, 173 X = 0. 174bb_intern([v(I,[])], CLP, X, _) :- 175 !, 176 X = I, 177 compare_d(CLP, =, I, integer(I)). 178bb_intern([v(One,[V^1])], CLP, X, _) :- 179 compare_d(CLP, =, One, 1), 180 !, 181 V = X, 182 bb_narrow_lower(CLP, X), 183 bb_narrow_upper(CLP, X). 184bb_intern(_, CLP, _, Term) :- 185 throw(instantiation_error(bb_inf(CLP, Term, _, _),1)). 186 187% bb_narrow_lower(X) 188% 189% Narrows the lower bound so that it is an integer bound. 190% We do this by finding the infimum of X and asserting that X 191% is larger than the first integer larger or equal to the infimum 192% (second integer if X is to be strict larger than the first integer). 193 194bb_narrow_lower(CLP, X) :- 195 ( inf(CLP, X, Inf) 196 -> ceiling_d(CLP, Inf, Bound), 197 ( entailed(CLP, X > Bound) 198 -> add_constraint(X >= Bound+1, CLP) 199 ; add_constraint(X >= Bound, CLP) 200 ) 201 ; true 202 ). 203 204% bb_narrow_upper(X) 205% 206% See bb_narrow_lower/1. This predicate handles the upper bound. 207 208bb_narrow_upper(CLP, X) :- 209 ( sup(CLP, X, Sup) 210 -> floor_d(CLP, Sup, Bound), 211 ( entailed(CLP, X < Bound) 212 -> add_constraint(X =< Bound-1, CLP) 213 ; add_constraint(X =< Bound, CLP) 214 ) 215 ; true 216 )