/* Configuration problem in Picat. Port of SICStus Prolog model config.pl """ SICSTUS CLPFD DEMONSTRATION PROGRAM Purpose : Configuration Problem Author : Mats Carlsson There are two types of alveoles and four types of cards. We want to plug in all cards using at most 5 alveoles at minimal cost. alveole type power slots price 1 0 0 0 2 150 8 150 3 200 16 200 card type power number 1 20 10 2 40 4 3 50 2 4 75 1 """ Solution from the SICStus Prolog model: | ?- config(bb,[]). [alv(0,0,0,0),alv(0,0,0,0),alv(7,0,0,0),alv(2,4,0,0),alv(1,0,2,1)]-550 Optimal solution from this Picat model with cost 550: [alv(0,0,0,0),alv(0,0,0,0),alv(7,0,0,0),alv(2,4,0,0),alv(1,0,2,1)] Without symmetry breaking, there are 2880 optimal solutions. With symmetry breaking there are 96 solutions. This problem seems to be from OPL's example config.mod (and config.dat). Cf my Picat model config.pi which solves the same problem using a more Picat like approach. This model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> time2(config(bb, [ff,split], Assignment,Cost,_StaticOrder,_Powers,_Slots,_Costs)), println(assignment=Assignment), println(optimal_cost=Cost), println("\nFind all optimal solutions:"), AllOptimal=findall(Assignment2,config(bb, [ff,split], Assignment2,Cost,_StaticOrder2,_Powers2,_Slots2,_Costs2)), foreach(Assignment3 in AllOptimal) println(Assignment3) end, println(len=AllOptimal.len), nl. go => true. % hakank: Added Assignments, Cost, etc % in order to get all optimal solutions. config(bb,Lab, Assignment,Cost,StaticOrder,Powers,Slots,Costs) :- problem(Assignment, Cost, Alveoles, StaticOrder,Powers,Slots,Costs), % append(Alveoles, StaticOrder, Variables), Variables = Alveoles ++ StaticOrder ++ Powers++Slots++Costs, if var(Cost) then solve($[min(Cost)|Lab], Variables) else solve(Lab, Variables) end. % hakank: Added Powers, Slots, and Costs problem(Assignment, Cost, Alveoles, StaticOrder, Powers,Slots,Costs) :- Alveoles = [_A1,_A2,_A3,_A4,_A5], Alveoles :: 1..3, domain_and_sum([C11,C21,C31,C41,C51], 10), domain_and_sum([C12,C22,C32,C42,C52], 4), domain_and_sum([C13,C23,C33,C43,C53], 2), domain_and_sum([C14,C24,C34,C44,C54], 1), Cards1I = [C11,C12,C13,C14], Cards2I = [C21,C22,C23,C24], Cards3I = [C31,C32,C33,C34], Cards4I = [C41,C42,C43,C44], Cards5I = [C51,C52,C53,C54], AllCards = [Cards1I,Cards2I,Cards3I,Cards4I,Cards5I], StaticOrder = [C14,C24,C34,C44,C54,C13,C23,C33,C43,C53,C12,C22,C32,C42,C52,C11,C21,C31,C41,C51], Assignment1 = [], Powers1 = [], Slots1 = [], foreach({Cards,Alv1} in zip(AllCards,Alveoles)) Cap :: 0..16, slots(Alv1, Cap), Slots1 := Slots1 ++ [Cap], sum(Cards) #=< Cap, Pow :: 0..200, power(Alv1, Pow), Powers1 := Powers1 ++ [Pow], scalar_product([20,40,50,75], Cards, #=<, Pow), S =.. [alv|Cards], Assignment1 := Assignment1 ++ [S] end, Assignment = Assignment1, Powers = Powers1, Slots = Slots1, % hakank: Symmetry breaking increasing(Alveoles), % redundant #1 sum(Slots) #>= 17, % redundant #2 sum(Powers) #>= 535, % redundant #3 Costs1 = [], foreach(Alv2 in Alveoles) Cost1 :: 0..200, cost(Alv2, Cost1), Costs1 := Costs1 ++ [Cost1] end, Costs = Costs1, sum(Costs) #= Cost. domain_and_sum(Vars, Sum) :- Vars :: 0..Sum, sum(Vars) #= Sum. slots(X, Y) :- element(X, [0,8,16], Y). power(X, Y) :- element(X, [0,150,200], Y). cost(X, Y) :- element(X, [0,150,200], Y). % This is from the end of the SICStus Prolog model. /* % genuine B&B search: 50 msec, 8 backtracks (9 nodes) | ?- config_bb([],Ass,Cost), statistics(runtime,R), fd_statistics. Tells detecting entailment: 107 Tells pruning: 253 Tells failing: 8 Total tells: 367 Asks detecting entailment: 17 Total asks: 89 Constraints created: 48 R = [113364300,50], Ass = [alv(0,0,0,0),alv(0,0,0,0),alv(7,0,0,0),alv(2,4,0,0),alv(1,0,2,1)], Cost = 550 ? yes % B&B search with restart: 40 msec, 4 backtracks (5 nodes) | ?- config_restart([],Ass,Cost), statistics(runtime,R), fd_statistics. Tells detecting entailment: 128 Tells pruning: 275 Tells failing: 4 Total tells: 403 Asks detecting entailment: 21 Total asks: 96 Constraints created: 48 R = [113364530,40], Ass = [alv(0,0,0,0),alv(0,0,0,0),alv(7,0,0,0),alv(2,4,0,0),alv(1,0,2,1)], Cost = 550 ? yes */