:- use_module(library(clpfd)).
:- ensure_loaded(['../../interpreter/prolog/r2cp']).
:- init_interpreter.


state_items_search_foldl_1([], I_and, _, I_and) :- 
	!.
state_items_search_foldl_1([XS_149|Tail], I_and, [], _K_) :- 
	rcp_att(siz, XS_149, _A_),
	fd_gt(_A_, 6, _B_),
	rcp_att(coord, XS_149, _C_),
	rcp_var(from(bin,0,1), Bin),
	fd_aeq(Bin+1, _D_),
	rcp_att(siz, XS_149, _E_),
	fd_aeq(_E_*3, _F_),
	fd_aeq(_F_/10, _G_),
	fd_aeq(max(1,_G_), _H_),
	interval_split(_C_, 1, _D_, _H_, _I_),
	fd_implies(_B_, _I_, _J_),
	fd_and(I_and, _J_, _L_),
	state_items_search_foldl_1(Tail, _L_, [], _K_).

state_items_search_foldl_1_search([], I_and, _, I_and) :- 
	!.
state_items_search_foldl_1_search([XS_149|Tail], I_and, [], _K_) :- 
	rcp_att(siz, XS_149, _A_),
	rcp_att(siz, XS_149, _B_),
	(
	fd_gt(_A_, 6),
	rcp_att(coord, XS_149, _C_),
	rcp_var(from(bin,0,1), Bin),
	fd_aeq(Bin+1, _D_),
	rcp_att(siz, XS_149, _E_),
	fd_aeq(_E_*3, _F_),
	fd_aeq(_F_/10, _G_),
	fd_aeq(max(1,_G_), _H_),
	interval_split_search(_C_, 1, _D_, _H_, _I_),
	fd_equiv(_I_, _J_, 1)
	;
	fd_leq(_B_, 6),
	fd_aeq(_J_, 1)
	),
	rcp_assert(I_and),
	rcp_assert(_J_),
	state_items_search_foldl_1_search(Tail, _L_, [], _K_).

state_items_search_foldl_2([], I_and, _, I_and) :- 
	!.
state_items_search_foldl_2([XS_150|Tail], I_and, [], _C_) :- 
	rcp_att(coord, XS_150, _A_),
	dichotomic_split(_A_, _B_),
	fd_and(I_and, _B_, _D_),
	state_items_search_foldl_2(Tail, _D_, [], _C_).

state_items_search_foldl_2_search([], I_and, _, I_and) :- 
	!.
state_items_search_foldl_2_search([XS_150|Tail], I_and, [], _C_) :- 
	rcp_att(coord, XS_150, _A_),
	dichotomic_split_search(_A_, _B_),
	rcp_assert(I_and),
	rcp_assert(_B_),
	state_items_search_foldl_2_search(Tail, _D_, [], _C_).

state_items_search_foldl_3([], I_and, _, I_and) :- 
	!.
state_items_search_foldl_3([YS_151|Tail], I_and, [], _H_) :- 
	rcp_att(coord, YS_151, _A_),
	rcp_var(from(bin,0,2), Bin_2),
	fd_aeq(Bin_2+1, _B_),
	rcp_att(siz, YS_151, _C_),
	fd_aeq(_C_*3, _D_),
	fd_aeq(_D_/10, _E_),
	fd_aeq(max(1,_E_), _F_),
	interval_split(_A_, 1, _B_, _F_, _G_),
	fd_and(I_and, _G_, _I_),
	state_items_search_foldl_3(Tail, _I_, [], _H_).

state_items_search_foldl_3_search([], I_and, _, I_and) :- 
	!.
state_items_search_foldl_3_search([YS_151|Tail], I_and, [], _H_) :- 
	rcp_att(coord, YS_151, _A_),
	rcp_var(from(bin,0,2), Bin_2),
	fd_aeq(Bin_2+1, _B_),
	rcp_att(siz, YS_151, _C_),
	fd_aeq(_C_*3, _D_),
	fd_aeq(_D_/10, _E_),
	fd_aeq(max(1,_E_), _F_),
	interval_split_search(_A_, 1, _B_, _F_, _G_),
	rcp_assert(I_and),
	rcp_assert(_G_),
	state_items_search_foldl_3_search(Tail, _I_, [], _H_).

state_items_search_foldl_4([], I_and, _, I_and) :- 
	!.
state_items_search_foldl_4([YS_152|Tail], I_and, [], _C_) :- 
	rcp_att(coord, YS_152, _A_),
	dichotomic_split(_A_, _B_),
	fd_and(I_and, _B_, _D_),
	state_items_search_foldl_4(Tail, _D_, [], _C_).

state_items_search_foldl_4_search([], I_and, _, I_and) :- 
	!.
state_items_search_foldl_4_search([YS_152|Tail], I_and, [], _C_) :- 
	rcp_att(coord, YS_152, _A_),
	dichotomic_split_search(_A_, _B_),
	rcp_assert(I_and),
	rcp_assert(_B_),
	state_items_search_foldl_4_search(Tail, _D_, [], _C_).

bin(_B_) :- 
	rcp_var(from(bin,0,1), Bin),
	rcp_var(from(bin,0,2), Bin_2),
	make_shape_box([Bin,Bin_2], _A_),
	rcp_var(from(bin,0,3), Bin_3),
	make_object_shape_area(_A_, [1,1], Bin_3, _B_).

bin_search(_B_) :- 
	rcp_var(from(bin,0,1), Bin),
	rcp_var(from(bin,0,2), Bin_2),
	make_shape_box_search([Bin,Bin_2], _A_),
	rcp_var(from(bin,0,3), Bin_3),
	make_object_shape_area_search(_A_, [1,1], Bin_3, _B_).

sum_foldr_1([], _, 0) :- 
	!.
sum_foldr_1([X_153|Tail], [L], _B_) :- 
	sum_foldr_1(Tail, [L], _A_),
	fd_aeq(X_153+_A_, _B_).

sum_foldr_1_search([], _, 0) :- 
	!.
sum_foldr_1_search([X_153|Tail], [L], _B_) :- 
	sum_foldr_1_search(Tail, [L], _A_),
	fd_aeq(X_153+_A_, _B_).

sum(L, _A_) :- 
	sum_foldr_1(L, [L], _A_).

sum_search(L, _A_) :- 
	sum_foldr_1_search(L, [L], _A_).

product_foldr_1([], _, 1) :- 
	!.
product_foldr_1([X_154|Tail], [L], _B_) :- 
	product_foldr_1(Tail, [L], _A_),
	fd_aeq(X_154*_A_, _B_).

product_foldr_1_search([], _, 1) :- 
	!.
product_foldr_1_search([X_154|Tail], [L], _B_) :- 
	product_foldr_1_search(Tail, [L], _A_),
	fd_aeq(X_154*_A_, _B_).

product(L, _A_) :- 
	product_foldr_1(L, [L], _A_).

product_search(L, _A_) :- 
	product_foldr_1_search(L, [L], _A_).

maximum_foldr_1([], _, _A_) :- 
	!.
maximum_foldr_1([X_155|Tail], [L], _B_) :- 
	maximum_foldr_1(Tail, [L], _A_),
	fd_aeq(max(X_155,_A_), _B_).

maximum_foldr_1_search([], _, _A_) :- 
	!.
maximum_foldr_1_search([X_155|Tail], [L], _B_) :- 
	maximum_foldr_1_search(Tail, [L], _A_),
	fd_aeq(max(X_155,_A_), _B_).

maximum(L, _A_) :- 
	maximum_foldr_1(L, [L], _A_).

maximum_search(L, _A_) :- 
	maximum_foldr_1_search(L, [L], _A_).

minimum_foldr_1([], _, _A_) :- 
	!.
minimum_foldr_1([X_156|Tail], [L], _B_) :- 
	minimum_foldr_1(Tail, [L], _A_),
	fd_aeq(min(X_156,_A_), _B_).

minimum_foldr_1_search([], _, _A_) :- 
	!.
minimum_foldr_1_search([X_156|Tail], [L], _B_) :- 
	minimum_foldr_1_search(Tail, [L], _A_),
	fd_aeq(min(X_156,_A_), _B_).

minimum(L, _A_) :- 
	minimum_foldr_1(L, [L], _A_).

minimum_search(L, _A_) :- 
	minimum_foldr_1_search(L, [L], _A_).

oversize(O1, O2, D, _O_) :- 
	origin(O1, D, _A_),
	origin(O2, D, _B_),
	fd_aeq(max(_A_,_B_), _C_),
	origin(O1, D, _D_),
	origin(O2, D, _E_),
	fd_aeq(min(_D_,_E_), _F_),
	fd_aeq(_C_-_F_, _G_),
	end(O1, D, _H_),
	end(O2, D, _I_),
	fd_aeq(max(_H_,_I_), _J_),
	end(O1, D, _K_),
	end(O2, D, _L_),
	fd_aeq(min(_K_,_L_), _M_),
	fd_aeq(_J_-_M_, _N_),
	fd_aeq(max(_G_,_N_), _O_).

oversize_search(O1, O2, D, _O_) :- 
	origin_search(O1, D, _A_),
	origin_search(O2, D, _B_),
	fd_aeq(max(_A_,_B_), _C_),
	origin_search(O1, D, _D_),
	origin_search(O2, D, _E_),
	fd_aeq(min(_D_,_E_), _F_),
	fd_aeq(_C_-_F_, _G_),
	end_search(O1, D, _H_),
	end_search(O2, D, _I_),
	fd_aeq(max(_H_,_I_), _J_),
	end_search(O1, D, _K_),
	end_search(O2, D, _L_),
	fd_aeq(min(_K_,_L_), _M_),
	fd_aeq(_J_-_M_, _N_),
	fd_aeq(max(_G_,_N_), _O_).

make_box(L, _A_) :- 
	rcp_rec([size=L], _A_).

make_box_search(L, _A_) :- 
	rcp_rec([size=L], _A_).

box_volume(B, _B_) :- 
	rcp_att(size, B, _A_),
	product(_A_, _B_).

box_volume_search(B, _B_) :- 
	rcp_att(size, B, _A_),
	product_search(_A_, _B_).

make_sbox(B, O, _A_) :- 
	rcp_rec([box=B,offset=O], _A_).

make_sbox_search(B, O, _A_) :- 
	rcp_rec([box=B,offset=O], _A_).

sbox_size(SB, D, _C_) :- 
	rcp_att(box, SB, _A_),
	rcp_att(size, _A_, _B_),
	nth1(D, _B_, _C_).

sbox_size_search(SB, D, _C_) :- 
	rcp_att(box, SB, _A_),
	rcp_att(size, _A_, _B_),
	nth1(D, _B_, _C_).

sbox_offset(SB, D, _B_) :- 
	rcp_att(offset, SB, _A_),
	nth1(D, _A_, _B_).

sbox_offset_search(SB, D, _B_) :- 
	rcp_att(offset, SB, _A_),
	nth1(D, _A_, _B_).

sbox_end(SB, D, _C_) :- 
	sbox_offset(SB, D, _A_),
	sbox_size(SB, D, _B_),
	fd_aeq(_A_+_B_, _C_).

sbox_end_search(SB, D, _C_) :- 
	sbox_offset_search(SB, D, _A_),
	sbox_size_search(SB, D, _B_),
	fd_aeq(_A_+_B_, _C_).

make_shape(SBs, _A_) :- 
	rcp_rec([sboxes=SBs], _A_).

make_shape_search(SBs, _A_) :- 
	rcp_rec([sboxes=SBs], _A_).

make_shape_box_foldr_1([], _, []) :- 
	!.
make_shape_box_foldr_1([_free_var_157|Tail], [L], [0|_A_]) :- 
	make_shape_box_foldr_1(Tail, [L], _A_).

make_shape_box_foldr_1_search([], _, []) :- 
	!.
make_shape_box_foldr_1_search([_free_var_157|Tail], [L], [0|_A_]) :- 
	make_shape_box_foldr_1_search(Tail, [L], _A_).

make_shape_box(L, _D_) :- 
	make_box(L, _A_),
	make_shape_box_foldr_1(L, [L], _B_),
	make_sbox(_A_, _B_, _C_),
	make_shape([_C_], _D_).

make_shape_box_search(L, _D_) :- 
	make_box_search(L, _A_),
	make_shape_box_foldr_1_search(L, [L], _B_),
	make_sbox_search(_A_, _B_, _C_),
	make_shape_search([_C_], _D_).

shape_volume_foldr_1([], _, []) :- 
	!.
shape_volume_foldr_1([SB_158|Tail], [S], [_B_|_C_]) :- 
	shape_volume_foldr_1(Tail, [S], _C_),
	rcp_att(box, SB_158, _A_),
	box_volume(_A_, _B_).

shape_volume_foldr_1_search([], _, []) :- 
	!.
shape_volume_foldr_1_search([SB_158|Tail], [S], [_B_|_C_]) :- 
	shape_volume_foldr_1_search(Tail, [S], _C_),
	rcp_att(box, SB_158, _A_),
	box_volume_search(_A_, _B_).

shape_volume(S, _C_) :- 
	rcp_att(sboxes, S, _A_),
	shape_volume_foldr_1(_A_, [S], _B_),
	sum(_B_, _C_).

shape_volume_search(S, _C_) :- 
	rcp_att(sboxes, S, _A_),
	shape_volume_foldr_1_search(_A_, [S], _B_),
	sum_search(_B_, _C_).

shape_origin_foldr_1([], _, []) :- 
	!.
shape_origin_foldr_1([SB_159|Tail], [D,S], [_A_|_B_]) :- 
	shape_origin_foldr_1(Tail, [D,S], _B_),
	sbox_offset(SB_159, D, _A_).

shape_origin_foldr_1_search([], _, []) :- 
	!.
shape_origin_foldr_1_search([SB_159|Tail], [D,S], [_A_|_B_]) :- 
	shape_origin_foldr_1_search(Tail, [D,S], _B_),
	sbox_offset_search(SB_159, D, _A_).

shape_origin(S, D, _C_) :- 
	rcp_att(sboxes, S, _A_),
	shape_origin_foldr_1(_A_, [D,S], _B_),
	minimum(_B_, _C_).

shape_origin_search(S, D, _C_) :- 
	rcp_att(sboxes, S, _A_),
	shape_origin_foldr_1_search(_A_, [D,S], _B_),
	minimum_search(_B_, _C_).

shape_end_foldr_1([], _, []) :- 
	!.
shape_end_foldr_1([SB_160|Tail], [D,S], [_A_|_B_]) :- 
	shape_end_foldr_1(Tail, [D,S], _B_),
	sbox_end(SB_160, D, _A_).

shape_end_foldr_1_search([], _, []) :- 
	!.
shape_end_foldr_1_search([SB_160|Tail], [D,S], [_A_|_B_]) :- 
	shape_end_foldr_1_search(Tail, [D,S], _B_),
	sbox_end_search(SB_160, D, _A_).

shape_end(S, D, _C_) :- 
	rcp_att(sboxes, S, _A_),
	shape_end_foldr_1(_A_, [D,S], _B_),
	maximum(_B_, _C_).

shape_end_search(S, D, _C_) :- 
	rcp_att(sboxes, S, _A_),
	shape_end_foldr_1_search(_A_, [D,S], _B_),
	maximum_search(_B_, _C_).

shape_size(S, D, _C_) :- 
	shape_end(S, D, _A_),
	shape_origin(S, D, _B_),
	fd_aeq(_A_-_B_, _C_).

shape_size_search(S, D, _C_) :- 
	shape_end_search(S, D, _A_),
	shape_origin_search(S, D, _B_),
	fd_aeq(_A_-_B_, _C_).

make_object(SL, OL, _A_) :- 
	rcp_var(from(make_object(SL,OL),0,0), Make_object_SL_OL),
	rcp_rec([shapes=SL,shape_index=Make_object_SL_OL,origin=OL], _A_).

make_object_search(SL, OL, _A_) :- 
	rcp_var(from(make_object(SL,OL),0,0), Make_object_SL_OL),
	rcp_rec([shapes=SL,shape_index=Make_object_SL_OL,origin=OL], _A_).

make_object(SL, OL, S, W, _A_) :- 
	rcp_rec([shapes=SL,shape_index=S,origin=OL,weight=W], _A_).

make_object_search(SL, OL, S, W, _A_) :- 
	rcp_rec([shapes=SL,shape_index=S,origin=OL,weight=W], _A_).

make_object_shape(S, L, _A_) :- 
	rcp_rec([shapes=[S],shape_index=1,origin=L], _A_).

make_object_shape_search(S, L, _A_) :- 
	rcp_rec([shapes=[S],shape_index=1,origin=L], _A_).

make_object_shape(S, L, W, _A_) :- 
	rcp_rec([shapes=[S],shape_index=1,origin=L,weight=W], _A_).

make_object_shape_search(S, L, W, _A_) :- 
	rcp_rec([shapes=[S],shape_index=1,origin=L,weight=W], _A_).

origin(O, D, _B_) :- 
	rcp_att(origin, O, _A_),
	nth1(D, _A_, _B_).

origin_search(O, D, _B_) :- 
	rcp_att(origin, O, _A_),
	nth1(D, _A_, _B_).

x(O, _A_) :- 
	origin(O, 1, _A_).

x_search(O, _A_) :- 
	origin_search(O, 1, _A_).

y(O, _A_) :- 
	origin(O, 2, _A_).

y_search(O, _A_) :- 
	origin_search(O, 2, _A_).

z(O, _A_) :- 
	origin(O, 3, _A_).

z_search(O, _A_) :- 
	origin_search(O, 3, _A_).

end_foldr_1([], _, []) :- 
	!.
end_foldr_1([S_161|Tail], [D,O], [_F_|_G_]) :- 
	end_foldr_1(Tail, [D,O], _G_),
	rcp_att(shape_index, O, _A_),
	rcp_att(shapes, O, _B_),
	nth1(_C_, _B_, S_161),
	fd_eq(_A_, _C_, _D_),
	shape_end(S_161, D, _E_),
	fd_aeq(_D_*_E_, _F_).

end_foldr_1_search([], _, []) :- 
	!.
end_foldr_1_search([S_161|Tail], [D,O], [_E_|_F_]) :- 
	end_foldr_1_search(Tail, [D,O], _F_),
	rcp_att(shape_index, O, _A_),
	rcp_att(shapes, O, _B_),
	nth1(_C_, _B_, S_161),
	shape_end_search(S_161, D, _D_),
	fd_aeq(fd_eq(_A_,_C_)*_D_, _E_).

end(O, D, _E_) :- 
	origin(O, D, _A_),
	rcp_att(shapes, O, _B_),
	end_foldr_1(_B_, [D,O], _C_),
	sum(_C_, _D_),
	fd_aeq(_A_+_D_, _E_).

end_search(O, D, _E_) :- 
	origin_search(O, D, _A_),
	rcp_att(shapes, O, _B_),
	end_foldr_1_search(_B_, [D,O], _C_),
	sum_search(_C_, _D_),
	fd_aeq(_A_+_D_, _E_).

size_foldr_1([], _, []) :- 
	!.
size_foldr_1([S_162|Tail], [D,O], [_F_|_G_]) :- 
	size_foldr_1(Tail, [D,O], _G_),
	rcp_att(shape_index, O, _A_),
	rcp_att(shapes, O, _B_),
	nth1(_C_, _B_, S_162),
	fd_eq(_A_, _C_, _D_),
	shape_size(S_162, D, _E_),
	fd_aeq(_D_*_E_, _F_).

size_foldr_1_search([], _, []) :- 
	!.
size_foldr_1_search([S_162|Tail], [D,O], [_E_|_F_]) :- 
	size_foldr_1_search(Tail, [D,O], _F_),
	rcp_att(shape_index, O, _A_),
	rcp_att(shapes, O, _B_),
	nth1(_C_, _B_, S_162),
	shape_size_search(S_162, D, _D_),
	fd_aeq(fd_eq(_A_,_C_)*_D_, _E_).

size(O, D, _C_) :- 
	rcp_att(shapes, O, _A_),
	size_foldr_1(_A_, [D,O], _B_),
	sum(_B_, _C_).

size_search(O, D, _C_) :- 
	rcp_att(shapes, O, _A_),
	size_foldr_1_search(_A_, [D,O], _B_),
	sum_search(_B_, _C_).

volume_foldr_1([], _, []) :- 
	!.
volume_foldr_1([S_163|Tail], [O], [_F_|_G_]) :- 
	volume_foldr_1(Tail, [O], _G_),
	rcp_att(shape_index, O, _A_),
	rcp_att(shapes, O, _B_),
	nth1(_C_, _B_, S_163),
	fd_eq(_A_, _C_, _D_),
	shape_volume(S_163, _E_),
	fd_aeq(_D_*_E_, _F_).

volume_foldr_1_search([], _, []) :- 
	!.
volume_foldr_1_search([S_163|Tail], [O], [_E_|_F_]) :- 
	volume_foldr_1_search(Tail, [O], _F_),
	rcp_att(shape_index, O, _A_),
	rcp_att(shapes, O, _B_),
	nth1(_C_, _B_, S_163),
	shape_volume_search(S_163, _D_),
	fd_aeq(fd_eq(_A_,_C_)*_D_, _E_).

volume(O, _C_) :- 
	rcp_att(shapes, O, _A_),
	volume_foldr_1(_A_, [O], _B_),
	sum(_B_, _C_).

volume_search(O, _C_) :- 
	rcp_att(shapes, O, _A_),
	volume_foldr_1_search(_A_, [O], _B_),
	sum_search(_B_, _C_).

distance(O1, O2, D, _H_) :- 
	origin(O1, D, _A_),
	origin(O2, D, _B_),
	fd_aeq(max(_A_,_B_), _C_),
	end(O1, D, _D_),
	end(O2, D, _E_),
	fd_aeq(min(_D_,_E_), _F_),
	fd_aeq(_C_-_F_, _G_),
	fd_aeq(max(0,_G_), _H_).

distance_search(O1, O2, D, _H_) :- 
	origin_search(O1, D, _A_),
	origin_search(O2, D, _B_),
	fd_aeq(max(_A_,_B_), _C_),
	end_search(O1, D, _D_),
	end_search(O2, D, _E_),
	fd_aeq(min(_D_,_E_), _F_),
	fd_aeq(_C_-_F_, _G_),
	fd_aeq(max(0,_G_), _H_).

make_object_shape_area(S, OL, A, _A_) :- 
	rcp_rec([shapes=[S],shape_index=1,origin=OL,area=A], _A_).

make_object_shape_area_search(S, OL, A, _A_) :- 
	rcp_rec([shapes=[S],shape_index=1,origin=OL,area=A], _A_).

items_area_foldr_1([], _, []) :- 
	!.
items_area_foldr_1([I_164|Tail], [Items], [_A_|_B_]) :- 
	items_area_foldr_1(Tail, [Items], _B_),
	volume(I_164, _A_).

items_area_foldr_1_search([], _, []) :- 
	!.
items_area_foldr_1_search([I_164|Tail], [Items], [_A_|_B_]) :- 
	items_area_foldr_1_search(Tail, [Items], _B_),
	volume_search(I_164, _A_).

items_area(Items, _B_) :- 
	items_area_foldr_1(Items, [Items], _A_),
	sum(_A_, _B_).

items_area_search(Items, _B_) :- 
	items_area_foldr_1_search(Items, [Items], _A_),
	sum_search(_A_, _B_).

items_foldr_1([], _, []) :- 
	!.
items_foldr_1([S_165|Tail], [N], [_A_|_B_]) :- 
	items_foldr_1(Tail, [N], _B_),
	item(S_165, _A_).

items_foldr_1_search([], _, []) :- 
	!.
items_foldr_1_search([S_165|Tail], [N], [_A_|_B_]) :- 
	items_foldr_1_search(Tail, [N], _B_),
	item_search(S_165, _A_).

items(N, _B_) :- 
	squares(N, _A_),
	items_foldr_1(_A_, [N], _B_).

items_search(N, _B_) :- 
	squares_search(N, _A_),
	items_foldr_1_search(_A_, [N], _B_).

item(S, _A_) :- 
	rcp_var(from(item(S),0,1), Item_S),
	rcp_var(from(item(S),0,2), Item_S_2),
	rcp_rec([shapes=[S],shape_index=1,origin=[Item_S,Item_S_2]], _A_).

item_search(S, _A_) :- 
	rcp_var(from(item(S),0,1), Item_S),
	rcp_var(from(item(S),0,2), Item_S_2),
	rcp_rec([shapes=[S],shape_index=1,origin=[Item_S,Item_S_2]], _A_).

squares_foldr_1([], _, []) :- 
	!.
squares_foldr_1([Size_166|Tail], [N], [_A_|_B_]) :- 
	squares_foldr_1(Tail, [N], _B_),
	square(Size_166, _A_).

squares_foldr_1_search([], _, []) :- 
	!.
squares_foldr_1_search([Size_166|Tail], [N], [_A_|_B_]) :- 
	squares_foldr_1_search(Tail, [N], _B_),
	square_search(Size_166, _A_).

squares(N, _C_) :- 
	rcp_range([2..N], _A_),
	reverse_list(_A_, _B_),
	squares_foldr_1(_B_, [N], _C_).

squares_search(N, _C_) :- 
	rcp_range([2..N], _A_),
	reverse_list_search(_A_, _B_),
	squares_foldr_1_search(_B_, [N], _C_).

square(S, _A_) :- 
	make_shape_box([S,S], _A_).

square_search(S, _A_) :- 
	make_shape_box_search([S,S], _A_).

w(O, _A_) :- 
	size(O, 1, _A_).

w_search(O, _A_) :- 
	size_search(O, 1, _A_).

h(O, _A_) :- 
	size(O, 2, _A_).

h_search(O, _A_) :- 
	size_search(O, 2, _A_).

l(O, _A_) :- 
	size(O, 3, _A_).

l_search(O, _A_) :- 
	size_search(O, 3, _A_).

xs_foldr_1([], _, []) :- 
	!.
xs_foldr_1([I_167|Tail], [Items], [_A_|_B_]) :- 
	xs_foldr_1(Tail, [Items], _B_),
	x(I_167, _A_).

xs_foldr_1_search([], _, []) :- 
	!.
xs_foldr_1_search([I_167|Tail], [Items], [_A_|_B_]) :- 
	xs_foldr_1_search(Tail, [Items], _B_),
	x_search(I_167, _A_).

xs(Items, _A_) :- 
	xs_foldr_1(Items, [Items], _A_).

xs_search(Items, _A_) :- 
	xs_foldr_1_search(Items, [Items], _A_).

ys_foldr_1([], _, []) :- 
	!.
ys_foldr_1([I_168|Tail], [Items], [_A_|_B_]) :- 
	ys_foldr_1(Tail, [Items], _B_),
	y(I_168, _A_).

ys_foldr_1_search([], _, []) :- 
	!.
ys_foldr_1_search([I_168|Tail], [Items], [_A_|_B_]) :- 
	ys_foldr_1_search(Tail, [Items], _B_),
	y_search(I_168, _A_).

ys(Items, _A_) :- 
	ys_foldr_1(Items, [Items], _A_).

ys_search(Items, _A_) :- 
	ys_foldr_1_search(Items, [Items], _A_).

reverse_list_foldl_1([], I_flip_cons, _, I_flip_cons) :- 
	!.
reverse_list_foldl_1([X_169|Tail], I_flip_cons, [L], _A_) :- 
	flip([I_flip_cons|X_169], _B_),
	reverse_list_foldl_1(Tail, _B_, [L], _A_).

reverse_list_foldl_1_search([], I_flip_cons, _, I_flip_cons) :- 
	!.
reverse_list_foldl_1_search([X_169|Tail], I_flip_cons, [L], _A_) :- 
	flip_search([I_flip_cons|X_169], _B_),
	reverse_list_foldl_1_search(Tail, _B_, [L], _A_).

reverse_list(L, _A_) :- 
	reverse_list_foldl_1(L, [], [L], _A_).

reverse_list_search(L, _A_) :- 
	reverse_list_foldl_1_search(L, [], [L], _A_).

precedes(A, B, D, _C_) :- 
	end(A, D, _A_),
	origin(B, D, _B_),
	fd_lt(_A_, _B_, _C_).

precedes_search(A, B, D, _C_) :- 
	end_search(A, D, _A_),
	origin_search(B, D, _B_),
	fd_lt(_A_, _B_),
	fd_aeq(_C_, 1).

meets(A, B, D, _C_) :- 
	end(A, D, _A_),
	origin(B, D, _B_),
	fd_eq(_A_, _B_, _C_).

meets_search(A, B, D, _C_) :- 
	end_search(A, D, _A_),
	origin_search(B, D, _B_),
	fd_eq(_A_, _B_),
	fd_aeq(_C_, 1).

overlaps(A, B, D, _K_) :- 
	origin(A, D, _A_),
	origin(B, D, _B_),
	fd_lt(_A_, _B_, _C_),
	end(A, D, _D_),
	end(B, D, _E_),
	fd_lt(_D_, _E_, _F_),
	fd_and(_C_, _F_, _G_),
	origin(B, D, _H_),
	end(A, D, _I_),
	fd_lt(_H_, _I_, _J_),
	fd_and(_G_, _J_, _K_).

overlaps_search(A, B, D, _G_) :- 
	origin_search(A, D, _A_),
	origin_search(B, D, _B_),
	end_search(A, D, _C_),
	end_search(B, D, _D_),
	origin_search(B, D, _E_),
	end_search(A, D, _F_),
	fd_lt(_A_, _B_),
	fd_lt(_C_, _D_),
	fd_lt(_E_, _F_),
	fd_aeq(_G_, 1).

contains(A, B, D, _G_) :- 
	origin(A, D, _A_),
	origin(B, D, _B_),
	fd_lt(_A_, _B_, _C_),
	end(B, D, _D_),
	end(A, D, _E_),
	fd_lt(_D_, _E_, _F_),
	fd_and(_C_, _F_, _G_).

contains_search(A, B, D, _E_) :- 
	origin_search(A, D, _A_),
	origin_search(B, D, _B_),
	end_search(B, D, _C_),
	end_search(A, D, _D_),
	fd_lt(_A_, _B_),
	fd_lt(_C_, _D_),
	fd_aeq(_E_, 1).

starts(A, B, D, _G_) :- 
	origin(A, D, _A_),
	origin(B, D, _B_),
	fd_eq(_A_, _B_, _C_),
	end(A, D, _D_),
	end(B, D, _E_),
	fd_lt(_D_, _E_, _F_),
	fd_and(_C_, _F_, _G_).

starts_search(A, B, D, _E_) :- 
	origin_search(A, D, _A_),
	origin_search(B, D, _B_),
	end_search(A, D, _C_),
	end_search(B, D, _D_),
	fd_eq(_A_, _B_),
	fd_lt(_C_, _D_),
	fd_aeq(_E_, 1).

finishes(A, B, D, _G_) :- 
	origin(B, D, _A_),
	origin(A, D, _B_),
	fd_lt(_A_, _B_, _C_),
	end(A, D, _D_),
	end(B, D, _E_),
	fd_eq(_D_, _E_, _F_),
	fd_and(_C_, _F_, _G_).

finishes_search(A, B, D, _E_) :- 
	origin_search(B, D, _A_),
	origin_search(A, D, _B_),
	end_search(A, D, _C_),
	end_search(B, D, _D_),
	fd_lt(_A_, _B_),
	fd_eq(_C_, _D_),
	fd_aeq(_E_, 1).

equals(A, B, D, _G_) :- 
	origin(A, D, _A_),
	origin(B, D, _B_),
	fd_eq(_A_, _B_, _C_),
	end(A, D, _D_),
	end(B, D, _E_),
	fd_eq(_D_, _E_, _F_),
	fd_and(_C_, _F_, _G_).

equals_search(A, B, D, _E_) :- 
	origin_search(A, D, _A_),
	origin_search(B, D, _B_),
	end_search(A, D, _C_),
	end_search(B, D, _D_),
	fd_eq(_A_, _B_),
	fd_eq(_C_, _D_),
	fd_aeq(_E_, 1).

started_by(A, B, D, _G_) :- 
	origin(A, D, _A_),
	origin(B, D, _B_),
	fd_eq(_A_, _B_, _C_),
	end(B, D, _D_),
	end(A, D, _E_),
	fd_lt(_D_, _E_, _F_),
	fd_and(_C_, _F_, _G_).

started_by_search(A, B, D, _E_) :- 
	origin_search(A, D, _A_),
	origin_search(B, D, _B_),
	end_search(B, D, _C_),
	end_search(A, D, _D_),
	fd_eq(_A_, _B_),
	fd_lt(_C_, _D_),
	fd_aeq(_E_, 1).

finished_by(A, B, D, _G_) :- 
	origin(B, D, _A_),
	origin(A, D, _B_),
	fd_gt(_A_, _B_, _C_),
	end(A, D, _D_),
	end(B, D, _E_),
	fd_eq(_D_, _E_, _F_),
	fd_and(_C_, _F_, _G_).

finished_by_search(A, B, D, _E_) :- 
	origin_search(B, D, _A_),
	origin_search(A, D, _B_),
	end_search(A, D, _C_),
	end_search(B, D, _D_),
	fd_gt(_A_, _B_),
	fd_eq(_C_, _D_),
	fd_aeq(_E_, 1).

during(A, B, D, _G_) :- 
	origin(B, D, _A_),
	origin(A, D, _B_),
	fd_lt(_A_, _B_, _C_),
	end(A, D, _D_),
	end(B, D, _E_),
	fd_lt(_D_, _E_, _F_),
	fd_and(_C_, _F_, _G_).

during_search(A, B, D, _E_) :- 
	origin_search(B, D, _A_),
	origin_search(A, D, _B_),
	end_search(A, D, _C_),
	end_search(B, D, _D_),
	fd_lt(_A_, _B_),
	fd_lt(_C_, _D_),
	fd_aeq(_E_, 1).

overlapped_by(A, B, D, _K_) :- 
	origin(B, D, _A_),
	origin(A, D, _B_),
	fd_lt(_A_, _B_, _C_),
	origin(A, D, _D_),
	end(B, D, _E_),
	fd_lt(_D_, _E_, _F_),
	fd_and(_C_, _F_, _G_),
	end(A, D, _H_),
	end(B, D, _I_),
	fd_gt(_H_, _I_, _J_),
	fd_and(_G_, _J_, _K_).

overlapped_by_search(A, B, D, _G_) :- 
	origin_search(B, D, _A_),
	origin_search(A, D, _B_),
	origin_search(A, D, _C_),
	end_search(B, D, _D_),
	end_search(A, D, _E_),
	end_search(B, D, _F_),
	fd_lt(_A_, _B_),
	fd_lt(_C_, _D_),
	fd_gt(_E_, _F_),
	fd_aeq(_G_, 1).

met_by(A, B, D, _C_) :- 
	end(B, D, _A_),
	origin(A, D, _B_),
	fd_eq(_A_, _B_, _C_).

met_by_search(A, B, D, _C_) :- 
	end_search(B, D, _A_),
	origin_search(A, D, _B_),
	fd_eq(_A_, _B_),
	fd_aeq(_C_, 1).

preceded_by(A, B, D, _C_) :- 
	end(B, D, _A_),
	origin(A, D, _B_),
	fd_lt(_A_, _B_, _C_).

preceded_by_search(A, B, D, _C_) :- 
	end_search(B, D, _A_),
	origin_search(A, D, _B_),
	fd_lt(_A_, _B_),
	fd_aeq(_C_, 1).

contains_touch(A, B, D, _G_) :- 
	origin(A, D, _A_),
	origin(B, D, _B_),
	fd_leq(_A_, _B_, _C_),
	end(B, D, _D_),
	end(A, D, _E_),
	fd_leq(_D_, _E_, _F_),
	fd_and(_C_, _F_, _G_).

contains_touch_search(A, B, D, _E_) :- 
	origin_search(A, D, _A_),
	origin_search(B, D, _B_),
	end_search(B, D, _C_),
	end_search(A, D, _D_),
	fd_leq(_A_, _B_),
	fd_leq(_C_, _D_),
	fd_aeq(_E_, 1).

overlaps_sym(A, B, D, _G_) :- 
	end(A, D, _A_),
	origin(B, D, _B_),
	fd_gt(_A_, _B_, _C_),
	end(B, D, _D_),
	origin(A, D, _E_),
	fd_gt(_D_, _E_, _F_),
	fd_and(_C_, _F_, _G_).

overlaps_sym_search(A, B, D, _E_) :- 
	end_search(A, D, _A_),
	origin_search(B, D, _B_),
	end_search(B, D, _C_),
	origin_search(A, D, _D_),
	fd_gt(_A_, _B_),
	fd_gt(_C_, _D_),
	fd_aeq(_E_, 1).

disjoint_foldl_1([], I_or, _, I_or) :- 
	!.
disjoint_foldl_1([D_170|Tail], I_or, [Ds,O1,O2], _D_) :- 
	precedes(O1, O2, D_170, _A_),
	preceded_by(O1, O2, D_170, _B_),
	fd_or(_A_, _B_, _C_),
	fd_or(I_or, _C_, _E_),
	disjoint_foldl_1(Tail, _E_, [Ds,O1,O2], _D_).

disjoint_foldl_1_search([], I_or, _, I_or) :- 
	!.
disjoint_foldl_1_search([D_170|Tail], I_or, [Ds,O1,O2], _C_) :- 
	(
	rcp_assert(I_or)
	;
	(
	precedes_search(O1, O2, D_170, _A_),
	rcp_assert(_A_)
	;
	preceded_by_search(O1, O2, D_170, _B_),
	rcp_assert(_B_)
	)
	),
	disjoint_foldl_1_search(Tail, _D_, [Ds,O1,O2], _C_).

disjoint(O1, O2, Ds, _A_) :- 
	disjoint_foldl_1(Ds, 0, [Ds,O1,O2], _A_).

disjoint_search(O1, O2, Ds, _A_) :- 
	disjoint_foldl_1_search(Ds, 0, [Ds,O1,O2], _A_).

meet_foldl_1([], I_and, _, I_and) :- 
	!.
meet_foldl_1([D_171|Tail], I_and, [Ds,O1,O2], _F_) :- 
	precedes(O1, O2, D_171, _A_),
	fd_not(_A_, _B_),
	preceded_by(O1, O2, D_171, _C_),
	fd_not(_C_, _D_),
	fd_and(_B_, _D_, _E_),
	fd_and(I_and, _E_, _G_),
	meet_foldl_1(Tail, _G_, [Ds,O1,O2], _F_).

meet_foldl_1_search([], I_and, _, I_and) :- 
	!.
meet_foldl_1_search([D_171|Tail], I_and, [Ds,O1,O2], _E_) :- 
	rcp_assert(I_and),
	precedes_search(O1, O2, D_171, _A_),
	fd_not(_A_, _B_),
	rcp_assert(_B_),
	preceded_by_search(O1, O2, D_171, _C_),
	fd_not(_C_, _D_),
	rcp_assert(_D_),
	meet_foldl_1_search(Tail, _F_, [Ds,O1,O2], _E_).

meet_foldl_2([], I_or, _, I_or) :- 
	!.
meet_foldl_2([D_172|Tail], I_or, [Ds,O1,O2], _D_) :- 
	meets(O1, O2, D_172, _A_),
	met_by(O1, O2, D_172, _B_),
	fd_or(_A_, _B_, _C_),
	fd_or(I_or, _C_, _E_),
	meet_foldl_2(Tail, _E_, [Ds,O1,O2], _D_).

meet_foldl_2_search([], I_or, _, I_or) :- 
	!.
meet_foldl_2_search([D_172|Tail], I_or, [Ds,O1,O2], _C_) :- 
	(
	rcp_assert(I_or)
	;
	(
	meets_search(O1, O2, D_172, _A_),
	rcp_assert(_A_)
	;
	met_by_search(O1, O2, D_172, _B_),
	rcp_assert(_B_)
	)
	),
	meet_foldl_2_search(Tail, _D_, [Ds,O1,O2], _C_).

meet(O1, O2, Ds, _C_) :- 
	meet_foldl_1(Ds, 1, [Ds,O1,O2], _A_),
	meet_foldl_2(Ds, 0, [Ds,O1,O2], _B_),
	fd_and(_A_, _B_, _C_).

meet_search(O1, O2, Ds, _C_) :- 
	meet_foldl_1_search(Ds, 1, [Ds,O1,O2], _A_),
	rcp_assert(_A_),
	meet_foldl_2_search(Ds, 0, [Ds,O1,O2], _B_),
	rcp_assert(_B_),
	fd_aeq(_C_, 1).

equal_foldl_1([], I_and, _, I_and) :- 
	!.
equal_foldl_1([D_173|Tail], I_and, [Ds,O1,O2], _B_) :- 
	equals(O1, O2, D_173, _A_),
	fd_and(I_and, _A_, _C_),
	equal_foldl_1(Tail, _C_, [Ds,O1,O2], _B_).

equal_foldl_1_search([], I_and, _, I_and) :- 
	!.
equal_foldl_1_search([D_173|Tail], I_and, [Ds,O1,O2], _B_) :- 
	equals_search(O1, O2, D_173, _A_),
	rcp_assert(I_and),
	rcp_assert(_A_),
	equal_foldl_1_search(Tail, _C_, [Ds,O1,O2], _B_).

equal(O1, O2, Ds, _A_) :- 
	equal_foldl_1(Ds, 1, [Ds,O1,O2], _A_).

equal_search(O1, O2, Ds, _A_) :- 
	equal_foldl_1_search(Ds, 1, [Ds,O1,O2], _A_).

covers_foldl_1([], I_and, _, I_and) :- 
	!.
covers_foldl_1([D_174|Tail], I_and, [Ds,O1,O2], _F_) :- 
	started_by(O1, O2, D_174, _A_),
	contains(O1, O2, D_174, _B_),
	fd_or(_A_, _B_, _C_),
	finished_by(O1, O2, D_174, _D_),
	fd_or(_C_, _D_, _E_),
	fd_and(I_and, _E_, _G_),
	covers_foldl_1(Tail, _G_, [Ds,O1,O2], _F_).

covers_foldl_1_search([], I_and, _, I_and) :- 
	!.
covers_foldl_1_search([D_174|Tail], I_and, [Ds,O1,O2], _D_) :- 
	rcp_assert(I_and),
	(
	(
	started_by_search(O1, O2, D_174, _A_),
	rcp_assert(_A_)
	;
	contains_search(O1, O2, D_174, _B_),
	rcp_assert(_B_)
	)
	;
	finished_by_search(O1, O2, D_174, _C_),
	rcp_assert(_C_)
	),
	covers_foldl_1_search(Tail, _E_, [Ds,O1,O2], _D_).

covers_foldl_2([], I_or, _, I_or) :- 
	!.
covers_foldl_2([D_175|Tail], I_or, [Ds,O1,O2], _C_) :- 
	contains(O1, O2, D_175, _A_),
	fd_not(_A_, _B_),
	fd_or(I_or, _B_, _D_),
	covers_foldl_2(Tail, _D_, [Ds,O1,O2], _C_).

covers_foldl_2_search([], I_or, _, I_or) :- 
	!.
covers_foldl_2_search([D_175|Tail], I_or, [Ds,O1,O2], _C_) :- 
	contains_search(O1, O2, D_175, _A_),
	fd_not(_A_, _B_),
	(
	rcp_assert(I_or)
	;
	rcp_assert(_B_)
	),
	covers_foldl_2_search(Tail, _D_, [Ds,O1,O2], _C_).

covers(O1, O2, Ds, _C_) :- 
	covers_foldl_1(Ds, 1, [Ds,O1,O2], _A_),
	covers_foldl_2(Ds, 0, [Ds,O1,O2], _B_),
	fd_and(_A_, _B_, _C_).

covers_search(O1, O2, Ds, _C_) :- 
	covers_foldl_1_search(Ds, 1, [Ds,O1,O2], _A_),
	rcp_assert(_A_),
	covers_foldl_2_search(Ds, 0, [Ds,O1,O2], _B_),
	rcp_assert(_B_),
	fd_aeq(_C_, 1).

covered_by_foldl_1([], I_and, _, I_and) :- 
	!.
covered_by_foldl_1([D_176|Tail], I_and, [Ds,O1,O2], _F_) :- 
	starts(O1, O2, D_176, _A_),
	during(O1, O2, D_176, _B_),
	fd_or(_A_, _B_, _C_),
	finishes(O1, O2, D_176, _D_),
	fd_or(_C_, _D_, _E_),
	fd_and(I_and, _E_, _G_),
	covered_by_foldl_1(Tail, _G_, [Ds,O1,O2], _F_).

covered_by_foldl_1_search([], I_and, _, I_and) :- 
	!.
covered_by_foldl_1_search([D_176|Tail], I_and, [Ds,O1,O2], _D_) :- 
	rcp_assert(I_and),
	(
	(
	starts_search(O1, O2, D_176, _A_),
	rcp_assert(_A_)
	;
	during_search(O1, O2, D_176, _B_),
	rcp_assert(_B_)
	)
	;
	finishes_search(O1, O2, D_176, _C_),
	rcp_assert(_C_)
	),
	covered_by_foldl_1_search(Tail, _E_, [Ds,O1,O2], _D_).

covered_by_foldl_2([], I_or, _, I_or) :- 
	!.
covered_by_foldl_2([D_177|Tail], I_or, [Ds,O1,O2], _C_) :- 
	during(O1, O2, D_177, _A_),
	fd_not(_A_, _B_),
	fd_or(I_or, _B_, _D_),
	covered_by_foldl_2(Tail, _D_, [Ds,O1,O2], _C_).

covered_by_foldl_2_search([], I_or, _, I_or) :- 
	!.
covered_by_foldl_2_search([D_177|Tail], I_or, [Ds,O1,O2], _C_) :- 
	during_search(O1, O2, D_177, _A_),
	fd_not(_A_, _B_),
	(
	rcp_assert(I_or)
	;
	rcp_assert(_B_)
	),
	covered_by_foldl_2_search(Tail, _D_, [Ds,O1,O2], _C_).

covered_by(O1, O2, Ds, _C_) :- 
	covered_by_foldl_1(Ds, 1, [Ds,O1,O2], _A_),
	covered_by_foldl_2(Ds, 0, [Ds,O1,O2], _B_),
	fd_and(_A_, _B_, _C_).

covered_by_search(O1, O2, Ds, _C_) :- 
	covered_by_foldl_1_search(Ds, 1, [Ds,O1,O2], _A_),
	rcp_assert(_A_),
	covered_by_foldl_2_search(Ds, 0, [Ds,O1,O2], _B_),
	rcp_assert(_B_),
	fd_aeq(_C_, 1).

contains_rcc_foldl_1([], I_and, _, I_and) :- 
	!.
contains_rcc_foldl_1([D_178|Tail], I_and, [Ds,O1,O2], _B_) :- 
	contains(O1, O2, D_178, _A_),
	fd_and(I_and, _A_, _C_),
	contains_rcc_foldl_1(Tail, _C_, [Ds,O1,O2], _B_).

contains_rcc_foldl_1_search([], I_and, _, I_and) :- 
	!.
contains_rcc_foldl_1_search([D_178|Tail], I_and, [Ds,O1,O2], _B_) :- 
	contains_search(O1, O2, D_178, _A_),
	rcp_assert(I_and),
	rcp_assert(_A_),
	contains_rcc_foldl_1_search(Tail, _C_, [Ds,O1,O2], _B_).

contains_rcc(O1, O2, Ds, _A_) :- 
	contains_rcc_foldl_1(Ds, 1, [Ds,O1,O2], _A_).

contains_rcc_search(O1, O2, Ds, _A_) :- 
	contains_rcc_foldl_1_search(Ds, 1, [Ds,O1,O2], _A_).

inside_foldl_1([], I_and, _, I_and) :- 
	!.
inside_foldl_1([D_179|Tail], I_and, [Ds,O1,O2], _B_) :- 
	during(O1, O2, D_179, _A_),
	fd_and(I_and, _A_, _C_),
	inside_foldl_1(Tail, _C_, [Ds,O1,O2], _B_).

inside_foldl_1_search([], I_and, _, I_and) :- 
	!.
inside_foldl_1_search([D_179|Tail], I_and, [Ds,O1,O2], _B_) :- 
	during_search(O1, O2, D_179, _A_),
	rcp_assert(I_and),
	rcp_assert(_A_),
	inside_foldl_1_search(Tail, _C_, [Ds,O1,O2], _B_).

inside(O1, O2, Ds, _A_) :- 
	inside_foldl_1(Ds, 1, [Ds,O1,O2], _A_).

inside_search(O1, O2, Ds, _A_) :- 
	inside_foldl_1_search(Ds, 1, [Ds,O1,O2], _A_).

overlap_foldl_1([], I_and, _, I_and) :- 
	!.
overlap_foldl_1([D_180|Tail], I_and, [Ds,O1,O2], _B_) :- 
	overlaps_sym(O1, O2, D_180, _A_),
	fd_and(I_and, _A_, _C_),
	overlap_foldl_1(Tail, _C_, [Ds,O1,O2], _B_).

overlap_foldl_1_search([], I_and, _, I_and) :- 
	!.
overlap_foldl_1_search([D_180|Tail], I_and, [Ds,O1,O2], _B_) :- 
	overlaps_sym_search(O1, O2, D_180, _A_),
	rcp_assert(I_and),
	rcp_assert(_A_),
	overlap_foldl_1_search(Tail, _C_, [Ds,O1,O2], _B_).

overlap(O1, O2, Ds, _A_) :- 
	overlap_foldl_1(Ds, 1, [Ds,O1,O2], _A_).

overlap_search(O1, O2, Ds, _A_) :- 
	overlap_foldl_1_search(Ds, 1, [Ds,O1,O2], _A_).

contains_touch_rcc_foldl_1([], I_and, _, I_and) :- 
	!.
contains_touch_rcc_foldl_1([D_181|Tail], I_and, [Ds,O1,O2], _B_) :- 
	contains_touch(O1, O2, D_181, _A_),
	fd_and(I_and, _A_, _C_),
	contains_touch_rcc_foldl_1(Tail, _C_, [Ds,O1,O2], _B_).

contains_touch_rcc_foldl_1_search([], I_and, _, I_and) :- 
	!.
contains_touch_rcc_foldl_1_search([D_181|Tail], I_and, [Ds,O1,O2], _B_) :- 
	contains_touch_search(O1, O2, D_181, _A_),
	rcp_assert(I_and),
	rcp_assert(_A_),
	contains_touch_rcc_foldl_1_search(Tail, _C_, [Ds,O1,O2], _B_).

contains_touch_rcc(O1, O2, Ds, _A_) :- 
	contains_touch_rcc_foldl_1(Ds, 1, [Ds,O1,O2], _A_).

contains_touch_rcc_search(O1, O2, Ds, _A_) :- 
	contains_touch_rcc_foldl_1_search(Ds, 1, [Ds,O1,O2], _A_).

above(O1, O2, _E_) :- 
	overlap(O1, O2, [1,2], _A_),
	preceded_by(O1, O2, 3, _B_),
	fd_and(_A_, _B_, _C_),
	met_by(O1, O2, 3, _D_),
	fd_or(_C_, _D_, _E_).

above_search(O1, O2, _D_) :- 
	(
	overlap_search(O1, O2, [1,2], _A_),
	rcp_assert(_A_),
	preceded_by_search(O1, O2, 3, _B_),
	rcp_assert(_B_)
	;
	met_by_search(O1, O2, 3, _C_),
	rcp_assert(_C_)
	),
	fd_aeq(_D_, 1).

on_top(O1, O2, _C_) :- 
	overlap(O1, O2, [1,2], _A_),
	met_by(O1, O2, 3, _B_),
	fd_and(_A_, _B_, _C_).

on_top_search(O1, O2, _C_) :- 
	overlap_search(O1, O2, [1,2], _A_),
	rcp_assert(_A_),
	met_by_search(O1, O2, 3, _B_),
	rcp_assert(_B_),
	fd_aeq(_C_, 1).

stack_oversize_foldl_3([], I_and, _, I_and) :- 
	!.
stack_oversize_foldl_3([D_187|Tail], I_and, [Items,Length,O1_182,O2_185], _C_) :- 
	oversize(O1_182, O2_185, D_187, _A_),
	fd_leq(_A_, Length, _B_),
	fd_and(I_and, _B_, _D_),
	stack_oversize_foldl_3(Tail, _D_, [Items,Length,O1_182,O2_185], _C_).

stack_oversize_foldl_3_search([], I_and, _, I_and) :- 
	!.
stack_oversize_foldl_3_search([D_187|Tail], I_and, [Items,Length,O1_182,O2_185], _B_) :- 
	oversize_search(O1_182, O2_185, D_187, _A_),
	rcp_assert(I_and),
	fd_leq_search(_A_, Length, _C_),
	rcp_assert(_C_),
	stack_oversize_foldl_3_search(Tail, _D_, [Items,Length,O1_182,O2_185], _B_).

stack_oversize_foldl_2([], I_and, _, I_and) :- 
	!.
stack_oversize_foldl_2([O2_185|Tail], I_and, [Items,Length,O1_182], _H_) :- 
	overlap(O1_182, O2_185, [1,2], _A_),
	rcp_att(uid, O1_182, _B_),
	rcp_att(uid, O2_185, _C_),
	fd_neq(_B_, _C_, _D_),
	fd_and(_A_, _D_, _E_),
	stack_oversize_foldl_3([1,2], 1, [Items,Length,O1_182,O2_185], _F_),
	fd_implies(_E_, _F_, _G_),
	fd_and(I_and, _G_, _I_),
	stack_oversize_foldl_2(Tail, _I_, [Items,Length,O1_182], _H_).

stack_oversize_foldl_2_search([], I_and, _, I_and) :- 
	!.
stack_oversize_foldl_2_search([O2_185|Tail], I_and, [Items,Length,O1_182], _J_) :- 
	rcp_att(uid, O1_182, _B_),
	rcp_att(uid, O2_185, _C_),
	rcp_att(uid, O1_182, _F_),
	rcp_att(uid, O2_185, _G_),
	(
	overlap_search(O1_182, O2_185, [1,2], _A_),
	rcp_assert(_A_),
	fd_neq(_B_, _C_),
	stack_oversize_foldl_3_search([1,2], 1, [Items,Length,O1_182,O2_185], _H_),
	fd_equiv(_H_, _I_, 1)
	;
	(
	overlap_search(O1_182, O2_185, [1,2], _D_),
	fd_not(_D_, _E_),
	rcp_assert(_E_)
	;
	fd_eq(_F_, _G_)
	),
	fd_aeq(_I_, 1)
	),
	rcp_assert(I_and),
	rcp_assert(_I_),
	stack_oversize_foldl_2_search(Tail, _K_, [Items,Length,O1_182], _J_).

stack_oversize_foldl_1([], I_and, _, I_and) :- 
	!.
stack_oversize_foldl_1([O1_182|Tail], I_and, [Items,Length], _B_) :- 
	stack_oversize_foldl_2(Items, 1, [Items,Length,O1_182], _A_),
	fd_and(I_and, _A_, _C_),
	stack_oversize_foldl_1(Tail, _C_, [Items,Length], _B_).

stack_oversize_foldl_1_search([], I_and, _, I_and) :- 
	!.
stack_oversize_foldl_1_search([O1_182|Tail], I_and, [Items,Length], _B_) :- 
	stack_oversize_foldl_2_search(Items, 1, [Items,Length,O1_182], _A_),
	rcp_assert(I_and),
	rcp_assert(_A_),
	stack_oversize_foldl_1_search(Tail, _C_, [Items,Length], _B_).

stack_oversize(Items, Length, _A_) :- 
	stack_oversize_foldl_1(Items, 1, [Items,Length], _A_).

stack_oversize_search(Items, Length, _A_) :- 
	stack_oversize_foldl_1_search(Items, 1, [Items,Length], _A_).

lighter(O1, O2, _C_) :- 
	rcp_att(weight, O1, _A_),
	rcp_att(weight, O2, _B_),
	fd_leq(_A_, _B_, _C_).

lighter_search(O1, O2, _C_) :- 
	rcp_att(weight, O1, _A_),
	rcp_att(weight, O2, _B_),
	fd_leq(_A_, _B_),
	fd_aeq(_C_, 1).

heavier(O1, O2, _C_) :- 
	rcp_att(weight, O1, _A_),
	rcp_att(weight, O2, _B_),
	fd_geq(_A_, _B_, _C_).

heavier_search(O1, O2, _C_) :- 
	rcp_att(weight, O1, _A_),
	rcp_att(weight, O2, _B_),
	fd_geq(_A_, _B_),
	fd_aeq(_C_, 1).

gravity_foldl_2([], I_or, _, I_or) :- 
	!.
gravity_foldl_2([O2_190|Tail], I_or, [Items,O1_188], _F_) :- 
	rcp_att(uid, O1_188, _A_),
	rcp_att(uid, O2_190, _B_),
	fd_neq(_A_, _B_, _C_),
	on_top(O1_188, O2_190, _D_),
	fd_and(_C_, _D_, _E_),
	fd_or(I_or, _E_, _G_),
	gravity_foldl_2(Tail, _G_, [Items,O1_188], _F_).

gravity_foldl_2_search([], I_or, _, I_or) :- 
	!.
gravity_foldl_2_search([O2_190|Tail], I_or, [Items,O1_188], _D_) :- 
	rcp_att(uid, O1_188, _A_),
	rcp_att(uid, O2_190, _B_),
	(
	rcp_assert(I_or)
	;
	fd_neq(_A_, _B_),
	on_top_search(O1_188, O2_190, _C_),
	rcp_assert(_C_)
	),
	gravity_foldl_2_search(Tail, _E_, [Items,O1_188], _D_).

gravity_foldl_1([], I_and, _, I_and) :- 
	!.
gravity_foldl_1([O1_188|Tail], I_and, [Items], _E_) :- 
	origin(O1_188, 3, _A_),
	fd_eq(_A_, 0, _B_),
	gravity_foldl_2(Items, 0, [Items,O1_188], _C_),
	fd_or(_B_, _C_, _D_),
	fd_and(I_and, _D_, _F_),
	gravity_foldl_1(Tail, _F_, [Items], _E_).

gravity_foldl_1_search([], I_and, _, I_and) :- 
	!.
gravity_foldl_1_search([O1_188|Tail], I_and, [Items], _C_) :- 
	origin_search(O1_188, 3, _A_),
	rcp_assert(I_and),
	(
	fd_eq(_A_, 0)
	;
	gravity_foldl_2_search(Items, 0, [Items,O1_188], _B_),
	rcp_assert(_B_)
	),
	gravity_foldl_1_search(Tail, _D_, [Items], _C_).

gravity(Items, _A_) :- 
	gravity_foldl_1(Items, 1, [Items], _A_).

gravity_search(Items, _A_) :- 
	gravity_foldl_1_search(Items, 1, [Items], _A_).

weight_stacking_foldl_2([], I_and, _, I_and) :- 
	!.
weight_stacking_foldl_2([O2_193|Tail], I_and, [Items,O1_191], _H_) :- 
	rcp_att(uid, O1_191, _A_),
	rcp_att(uid, O2_193, _B_),
	fd_neq(_A_, _B_, _C_),
	above(O1_191, O2_193, _D_),
	fd_and(_C_, _D_, _E_),
	lighter(O1_191, O2_193, _F_),
	fd_implies(_E_, _F_, _G_),
	fd_and(I_and, _G_, _I_),
	weight_stacking_foldl_2(Tail, _I_, [Items,O1_191], _H_).

weight_stacking_foldl_2_search([], I_and, _, I_and) :- 
	!.
weight_stacking_foldl_2_search([O2_193|Tail], I_and, [Items,O1_191], _J_) :- 
	rcp_att(uid, O1_191, _A_),
	rcp_att(uid, O2_193, _B_),
	rcp_att(uid, O1_191, _D_),
	rcp_att(uid, O2_193, _E_),
	(
	fd_neq(_A_, _B_),
	above_search(O1_191, O2_193, _C_),
	rcp_assert(_C_),
	lighter_search(O1_191, O2_193, _H_),
	fd_equiv(_H_, _I_, 1)
	;
	(
	fd_eq(_D_, _E_)
	;
	above_search(O1_191, O2_193, _F_),
	fd_not(_F_, _G_),
	rcp_assert(_G_)
	),
	fd_aeq(_I_, 1)
	),
	rcp_assert(I_and),
	rcp_assert(_I_),
	weight_stacking_foldl_2_search(Tail, _K_, [Items,O1_191], _J_).

weight_stacking_foldl_1([], I_and, _, I_and) :- 
	!.
weight_stacking_foldl_1([O1_191|Tail], I_and, [Items], _B_) :- 
	weight_stacking_foldl_2(Items, 1, [Items,O1_191], _A_),
	fd_and(I_and, _A_, _C_),
	weight_stacking_foldl_1(Tail, _C_, [Items], _B_).

weight_stacking_foldl_1_search([], I_and, _, I_and) :- 
	!.
weight_stacking_foldl_1_search([O1_191|Tail], I_and, [Items], _B_) :- 
	weight_stacking_foldl_2_search(Items, 1, [Items,O1_191], _A_),
	rcp_assert(I_and),
	rcp_assert(_A_),
	weight_stacking_foldl_1_search(Tail, _C_, [Items], _B_).

weight_stacking(Items, _A_) :- 
	weight_stacking_foldl_1(Items, 1, [Items], _A_).

weight_stacking_search(Items, _A_) :- 
	weight_stacking_foldl_1_search(Items, 1, [Items], _A_).

weight_balancing_foldr_1([], _, []) :- 
	!.
weight_balancing_foldr_1([Il_194|Tail], [Bin,D,Items,Ratio], [_F_|_G_]) :- 
	weight_balancing_foldr_1(Tail, [Bin,D,Items,Ratio], _G_),
	rcp_att(weight, Il_194, _A_),
	end(Il_194, D, _B_),
	end(Bin, D, _C_),
	fd_aeq(_C_/2, _D_),
	fd_leq(_B_, _D_, _E_),
	fd_aeq(_A_*_E_, _F_).

weight_balancing_foldr_1_search([], _, []) :- 
	!.
weight_balancing_foldr_1_search([Il_194|Tail], [Bin,D,Items,Ratio], [_E_|_F_]) :- 
	weight_balancing_foldr_1_search(Tail, [Bin,D,Items,Ratio], _F_),
	rcp_att(weight, Il_194, _A_),
	end_search(Il_194, D, _B_),
	end_search(Bin, D, _C_),
	fd_aeq(_C_/2, _D_),
	fd_aeq(_A_*fd_leq(_B_,_D_), _E_).

weight_balancing_foldr_2([], _, []) :- 
	!.
weight_balancing_foldr_2([Ir_198|Tail], [Bin,D,Items,Ratio], [_F_|_G_]) :- 
	weight_balancing_foldr_2(Tail, [Bin,D,Items,Ratio], _G_),
	rcp_att(weight, Ir_198, _A_),
	origin(Ir_198, D, _B_),
	end(Bin, D, _C_),
	fd_aeq(_C_/2, _D_),
	fd_geq(_B_, _D_, _E_),
	fd_aeq(_A_*_E_, _F_).

weight_balancing_foldr_2_search([], _, []) :- 
	!.
weight_balancing_foldr_2_search([Ir_198|Tail], [Bin,D,Items,Ratio], [_E_|_F_]) :- 
	weight_balancing_foldr_2_search(Tail, [Bin,D,Items,Ratio], _F_),
	rcp_att(weight, Ir_198, _A_),
	origin_search(Ir_198, D, _B_),
	end_search(Bin, D, _C_),
	fd_aeq(_C_/2, _D_),
	fd_aeq(_A_*fd_geq(_B_,_D_), _E_).

weight_balancing(Items, Bin, D, Ratio, _J_) :- 
	weight_balancing_foldr_1(Items, [Bin,D,Items,Ratio], _A_),
	sum(_A_, _B_),
	L_195=_B_,
	weight_balancing_foldr_2(Items, [Bin,D,Items,Ratio], _C_),
	sum(_C_, _D_),
	R_199=_D_,
	fd_aeq(max(L_195,R_199), _E_),
	fd_aeq(100*_E_, _F_),
	fd_aeq(100+Ratio, _G_),
	fd_aeq(min(L_195,R_199), _H_),
	fd_aeq(_G_*_H_, _I_),
	fd_leq(_F_, _I_, _J_).

weight_balancing_search(Items, Bin, D, Ratio, _J_) :- 
	weight_balancing_foldr_1_search(Items, [Bin,D,Items,Ratio], _A_),
	sum_search(_A_, _B_),
	L_195=_B_,
	weight_balancing_foldr_2_search(Items, [Bin,D,Items,Ratio], _C_),
	sum_search(_C_, _D_),
	R_199=_D_,
	fd_aeq(max(L_195,R_199), _E_),
	fd_aeq(100*_E_, _F_),
	fd_aeq(100+Ratio, _G_),
	fd_aeq(min(L_195,R_199), _H_),
	fd_aeq(_G_*_H_, _I_),
	fd_leq(_F_, _I_),
	fd_aeq(_J_, 1).

object_shape_domain(O, _E_) :- 
	rcp_att(shape_index, O, _A_),
	rcp_att(shapes, O, _B_),
	length(_B_, _C_),
	rcp_range([1.._C_], _D_),
	_A_ in _D_,
	fd_aeq(_E_, 1).

object_shape_domain_search(O, _E_) :- 
	rcp_att(shape_index, O, _A_),
	rcp_att(shapes, O, _B_),
	length(_B_, _C_),
	rcp_range([1.._C_], _D_),
	_A_ in _D_,
	fd_aeq(_E_, 1).

object_shape_domains_foldl_1([], I_and, _, I_and) :- 
	!.
object_shape_domains_foldl_1([I_200|Tail], I_and, [Items], _B_) :- 
	object_shape_domain(I_200, _A_),
	fd_and(I_and, _A_, _C_),
	object_shape_domains_foldl_1(Tail, _C_, [Items], _B_).

object_shape_domains_foldl_1_search([], I_and, _, I_and) :- 
	!.
object_shape_domains_foldl_1_search([I_200|Tail], I_and, [Items], _B_) :- 
	object_shape_domain_search(I_200, _A_),
	rcp_assert(I_and),
	rcp_assert(_A_),
	object_shape_domains_foldl_1_search(Tail, _C_, [Items], _B_).

object_shape_domains(Items, _A_) :- 
	object_shape_domains_foldl_1(Items, 1, [Items], _A_).

object_shape_domains_search(Items, _A_) :- 
	object_shape_domains_foldl_1_search(Items, 1, [Items], _A_).

non_overlapping_binary_foldl_2([], I_and, _, I_and) :- 
	!.
non_overlapping_binary_foldl_2([O2_203|Tail], I_and, [Dims,Items,O1_201], _G_) :- 
	rcp_att(uid, O1_201, _A_),
	rcp_att(uid, O2_203, _B_),
	fd_lt(_A_, _B_, _C_),
	overlap(O1_201, O2_203, Dims, _D_),
	fd_not(_D_, _E_),
	fd_implies(_C_, _E_, _F_),
	fd_and(I_and, _F_, _H_),
	non_overlapping_binary_foldl_2(Tail, _H_, [Dims,Items,O1_201], _G_).

non_overlapping_binary_foldl_2_search([], I_and, _, I_and) :- 
	!.
non_overlapping_binary_foldl_2_search([O2_203|Tail], I_and, [Dims,Items,O1_201], _H_) :- 
	rcp_att(uid, O1_201, _A_),
	rcp_att(uid, O2_203, _B_),
	rcp_att(uid, O1_201, _C_),
	rcp_att(uid, O2_203, _D_),
	(
	fd_lt(_A_, _B_),
	overlap_search(O1_201, O2_203, Dims, _E_),
	fd_not(_E_, _F_),
	fd_equiv(_F_, _G_, 1)
	;
	fd_geq(_C_, _D_),
	fd_aeq(_G_, 1)
	),
	rcp_assert(I_and),
	rcp_assert(_G_),
	non_overlapping_binary_foldl_2_search(Tail, _I_, [Dims,Items,O1_201], _H_).

non_overlapping_binary_foldl_1([], I_and, _, I_and) :- 
	!.
non_overlapping_binary_foldl_1([O1_201|Tail], I_and, [Dims,Items], _B_) :- 
	non_overlapping_binary_foldl_2(Items, 1, [Dims,Items,O1_201], _A_),
	fd_and(I_and, _A_, _C_),
	non_overlapping_binary_foldl_1(Tail, _C_, [Dims,Items], _B_).

non_overlapping_binary_foldl_1_search([], I_and, _, I_and) :- 
	!.
non_overlapping_binary_foldl_1_search([O1_201|Tail], I_and, [Dims,Items], _B_) :- 
	non_overlapping_binary_foldl_2_search(Items, 1, [Dims,Items,O1_201], _A_),
	rcp_assert(I_and),
	rcp_assert(_A_),
	non_overlapping_binary_foldl_1_search(Tail, _C_, [Dims,Items], _B_).

non_overlapping_binary(Items, Dims, _A_) :- 
	non_overlapping_binary_foldl_1(Items, 1, [Dims,Items], _A_).

non_overlapping_binary_search(Items, Dims, _A_) :- 
	non_overlapping_binary_foldl_1_search(Items, 1, [Dims,Items], _A_).

non_overlapping(Items, Dims, _A_) :- 
	geost(Items, Dims, _A_).

non_overlapping_search(Items, Dims, _A_) :- 
	geost_search(Items, Dims, _A_).

geost_constraint_foldr_2([], _, []) :- 
	!.
geost_constraint_foldr_2([S_206|Tail], [Dims,I_204,Items], [_A_|_B_]) :- 
	geost_constraint_foldr_2(Tail, [Dims,I_204,Items], _B_),
	rcp_att(uid, S_206, _A_).

geost_constraint_foldr_2_search([], _, []) :- 
	!.
geost_constraint_foldr_2_search([S_206|Tail], [Dims,I_204,Items], [_A_|_B_]) :- 
	geost_constraint_foldr_2_search(Tail, [Dims,I_204,Items], _B_),
	rcp_att(uid, S_206, _A_).

geost_constraint_foldr_1([], _, []) :- 
	!.
geost_constraint_foldr_1([I_204|Tail], [Dims,Items], [[_A_,_B_,_D_,_E_]|_F_]) :- 
	geost_constraint_foldr_1(Tail, [Dims,Items], _F_),
	rcp_att(uid, I_204, _A_),
	rcp_att(shape_index, I_204, _B_),
	rcp_att(shapes, I_204, _C_),
	geost_constraint_foldr_2(_C_, [Dims,I_204,Items], _D_),
	rcp_att(origin, I_204, _E_).

geost_constraint_foldr_1_search([], _, []) :- 
	!.
geost_constraint_foldr_1_search([I_204|Tail], [Dims,Items], [[_A_,_B_,_D_,_E_]|_F_]) :- 
	geost_constraint_foldr_1_search(Tail, [Dims,Items], _F_),
	rcp_att(uid, I_204, _A_),
	rcp_att(shape_index, I_204, _B_),
	rcp_att(shapes, I_204, _C_),
	geost_constraint_foldr_2_search(_C_, [Dims,I_204,Items], _D_),
	rcp_att(origin, I_204, _E_).

geost_constraint_foldr_5([], _, []) :- 
	!.
geost_constraint_foldr_5([SB_212|Tail], [Dims,I_207,Items,S_210], [[_A_,_B_,_D_]|_E_]) :- 
	geost_constraint_foldr_5(Tail, [Dims,I_207,Items,S_210], _E_),
	rcp_att(uid, S_210, _A_),
	rcp_att(offset, SB_212, _B_),
	rcp_att(box, SB_212, _C_),
	rcp_att(size, _C_, _D_).

geost_constraint_foldr_5_search([], _, []) :- 
	!.
geost_constraint_foldr_5_search([SB_212|Tail], [Dims,I_207,Items,S_210], [[_A_,_B_,_D_]|_E_]) :- 
	geost_constraint_foldr_5_search(Tail, [Dims,I_207,Items,S_210], _E_),
	rcp_att(uid, S_210, _A_),
	rcp_att(offset, SB_212, _B_),
	rcp_att(box, SB_212, _C_),
	rcp_att(size, _C_, _D_).

geost_constraint_foldr_4([], _, []) :- 
	!.
geost_constraint_foldr_4([S_210|Tail], [Dims,I_207,Items], [_B_|_C_]) :- 
	geost_constraint_foldr_4(Tail, [Dims,I_207,Items], _C_),
	rcp_att(sboxes, S_210, _A_),
	geost_constraint_foldr_5(_A_, [Dims,I_207,Items,S_210], _B_).

geost_constraint_foldr_4_search([], _, []) :- 
	!.
geost_constraint_foldr_4_search([S_210|Tail], [Dims,I_207,Items], [_B_|_C_]) :- 
	geost_constraint_foldr_4_search(Tail, [Dims,I_207,Items], _C_),
	rcp_att(sboxes, S_210, _A_),
	geost_constraint_foldr_5_search(_A_, [Dims,I_207,Items,S_210], _B_).

geost_constraint_foldr_3([], _, []) :- 
	!.
geost_constraint_foldr_3([I_207|Tail], [Dims,Items], [_B_|_C_]) :- 
	geost_constraint_foldr_3(Tail, [Dims,Items], _C_),
	rcp_att(shapes, I_207, _A_),
	geost_constraint_foldr_4(_A_, [Dims,I_207,Items], _B_).

geost_constraint_foldr_3_search([], _, []) :- 
	!.
geost_constraint_foldr_3_search([I_207|Tail], [Dims,Items], [_B_|_C_]) :- 
	geost_constraint_foldr_3_search(Tail, [Dims,Items], _C_),
	rcp_att(shapes, I_207, _A_),
	geost_constraint_foldr_4_search(_A_, [Dims,I_207,Items], _B_).

geost(Items, Dims, _D_) :- 
	geost_constraint_foldr_1(Items, [Dims,Items], _A_),
	geost_constraint_foldr_3(Items, [Dims,Items], _B_),
	length(Dims, _C_),
	geost_constraint(_A_, _B_, _C_, _D_).

geost_search(Items, Dims, _D_) :- 
	geost_constraint_foldr_1_search(Items, [Dims,Items], _A_),
	geost_constraint_foldr_3_search(Items, [Dims,Items], _B_),
	length(Dims, _C_),
	geost_constraint(_A_, _B_, _C_, _D_).

containmentAE_foldl_2([], I_or, _, I_or) :- 
	!.
containmentAE_foldl_2([B_215|Tail], I_or, [Bins,Dims,I_213,Items], _B_) :- 
	contains_touch_rcc(B_215, I_213, Dims, _A_),
	fd_or(I_or, _A_, _C_),
	containmentAE_foldl_2(Tail, _C_, [Bins,Dims,I_213,Items], _B_).

containmentAE_foldl_2_search([], I_or, _, I_or) :- 
	!.
containmentAE_foldl_2_search([B_215|Tail], I_or, [Bins,Dims,I_213,Items], _B_) :- 
	contains_touch_rcc_search(B_215, I_213, Dims, _A_),
	(
	rcp_assert(I_or)
	;
	rcp_assert(_A_)
	),
	containmentAE_foldl_2_search(Tail, _C_, [Bins,Dims,I_213,Items], _B_).

containmentAE_foldl_1([], I_and, _, I_and) :- 
	!.
containmentAE_foldl_1([I_213|Tail], I_and, [Bins,Dims,Items], _B_) :- 
	containmentAE_foldl_2(Bins, 0, [Bins,Dims,I_213,Items], _A_),
	fd_and(I_and, _A_, _C_),
	containmentAE_foldl_1(Tail, _C_, [Bins,Dims,Items], _B_).

containmentAE_foldl_1_search([], I_and, _, I_and) :- 
	!.
containmentAE_foldl_1_search([I_213|Tail], I_and, [Bins,Dims,Items], _B_) :- 
	containmentAE_foldl_2_search(Bins, 0, [Bins,Dims,I_213,Items], _A_),
	rcp_assert(I_and),
	rcp_assert(_A_),
	containmentAE_foldl_1_search(Tail, _C_, [Bins,Dims,Items], _B_).

containmentAE(Items, Bins, Dims, _A_) :- 
	containmentAE_foldl_1(Items, 1, [Bins,Dims,Items], _A_).

containmentAE_search(Items, Bins, Dims, _A_) :- 
	containmentAE_foldl_1_search(Items, 1, [Bins,Dims,Items], _A_).

bin_packing_binary(Items, Bins, Dims, _C_) :- 
	containmentAE(Items, Bins, Dims, _A_),
	non_overlapping_binary(Items, Dims, _B_),
	fd_and(_A_, _B_, _C_),
	variables(Items, _D_),
	rcp_labeling(_D_).

bin_packing_binary_search(Items, Bins, Dims, _D_) :- 
	variables(Items, _C_),
	containmentAE_search(Items, Bins, Dims, _A_),
	rcp_assert(_A_),
	non_overlapping_binary_search(Items, Dims, _B_),
	rcp_assert(_B_),
	rcp_labeling(_C_),
	fd_aeq(_D_, 1).

bin_packing(Items, Bins, Dims, _C_) :- 
	containmentAE(Items, Bins, Dims, _A_),
	non_overlapping(Items, Dims, _B_),
	fd_and(_A_, _B_, _C_),
	variables(Items, _D_),
	rcp_labeling(_D_).

bin_packing_search(Items, Bins, Dims, _D_) :- 
	variables(Items, _C_),
	containmentAE_search(Items, Bins, Dims, _A_),
	rcp_assert(_A_),
	non_overlapping_search(Items, Dims, _B_),
	rcp_assert(_B_),
	rcp_labeling(_C_),
	fd_aeq(_D_, 1).

containmentEA_foldl_2([], I_and, _, I_and) :- 
	!.
containmentEA_foldl_2([I_218|Tail], I_and, [B_216,Bins,Dims,Items], _B_) :- 
	contains_touch_rcc(B_216, I_218, Dims, _A_),
	fd_and(I_and, _A_, _C_),
	containmentEA_foldl_2(Tail, _C_, [B_216,Bins,Dims,Items], _B_).

containmentEA_foldl_2_search([], I_and, _, I_and) :- 
	!.
containmentEA_foldl_2_search([I_218|Tail], I_and, [B_216,Bins,Dims,Items], _B_) :- 
	contains_touch_rcc_search(B_216, I_218, Dims, _A_),
	rcp_assert(I_and),
	rcp_assert(_A_),
	containmentEA_foldl_2_search(Tail, _C_, [B_216,Bins,Dims,Items], _B_).

containmentEA_foldl_1([], I_or, _, I_or) :- 
	!.
containmentEA_foldl_1([B_216|Tail], I_or, [Bins,Dims,Items], _B_) :- 
	containmentEA_foldl_2(Items, 1, [B_216,Bins,Dims,Items], _A_),
	fd_or(I_or, _A_, _C_),
	containmentEA_foldl_1(Tail, _C_, [Bins,Dims,Items], _B_).

containmentEA_foldl_1_search([], I_or, _, I_or) :- 
	!.
containmentEA_foldl_1_search([B_216|Tail], I_or, [Bins,Dims,Items], _B_) :- 
	containmentEA_foldl_2_search(Items, 1, [B_216,Bins,Dims,Items], _A_),
	(
	rcp_assert(I_or)
	;
	rcp_assert(_A_)
	),
	containmentEA_foldl_1_search(Tail, _C_, [Bins,Dims,Items], _B_).

containmentEA(Items, Bins, Dims, _A_) :- 
	containmentEA_foldl_1(Bins, 0, [Bins,Dims,Items], _A_).

containmentEA_search(Items, Bins, Dims, _A_) :- 
	containmentEA_foldl_1_search(Bins, 0, [Bins,Dims,Items], _A_).

bin_design(Bin, Items, Dims, _C_) :- 
	containmentEA(Items, [Bin], Dims, _A_),
	non_overlapping(Items, Dims, _B_),
	fd_and(_A_, _B_, _C_),
	volume(Bin, _E_),
	minimize(((variables(Items,_D_),rcp_labeling(_D_)),rcp_labeling([_E_])), _E_),
	true.

bin_design_search(Bin, Items, Dims, _E_) :- 
	volume(Bin, _D_),
	minimize(((variables(Items,_C_),rcp_labeling(_C_)),rcp_labeling([_D_])), _D_),
	containmentEA_search(Items, [Bin], Dims, _A_),
	rcp_assert(_A_),
	non_overlapping_search(Items, Dims, _B_),
	rcp_assert(_B_),
	fd_aeq(_E_, 1).

solve_items_subproblem(Items, W, H, _E_) :- 
	state_items_domain(Items, W, H, _A_),
	state_items_constraints(Items, W, H, _B_),
	fd_and(_A_, _B_, _C_),
	state_items_search(Items, W, H, _D_),
	fd_and(_C_, _D_, _E_).

solve_items_subproblem_search(Items, W, H, _D_) :- 
	state_items_domain_search(Items, W, H, _A_),
	rcp_assert(_A_),
	state_items_constraints_search(Items, W, H, _B_),
	rcp_assert(_B_),
	state_items_search_search(Items, W, H, _C_),
	rcp_assert(_C_),
	fd_aeq(_D_, 1).

state_items_domain_foldl_1([], I_and, _, I_and) :- 
	!.
state_items_domain_foldl_1([It_219|Tail], I_and, [H,Items,W], _J_) :- 
	x(It_219, _A_),
	X_223=_A_,
	y(It_219, _B_),
	Y_226=_B_,
	w(It_219, _C_),
	S_228=_C_,
	variables(X_223, _D_),
	fd_aeq(W-S_228, _E_),
	fd_aeq(_E_+1, _F_),
	variables(Y_226, _G_),
	fd_aeq(H-S_228, _H_),
	fd_aeq(_H_+1, _I_),
	domain(_D_, 1, _F_),
	domain(_G_, 1, _I_),
	state_items_domain_foldl_1(Tail, I_and, [H,Items,W], _J_).

state_items_domain_foldl_1_search([], I_and, _, I_and) :- 
	!.
state_items_domain_foldl_1_search([It_219|Tail], I_and, [H,Items,W], _J_) :- 
	x_search(It_219, _A_),
	X_223=_A_,
	y_search(It_219, _B_),
	Y_226=_B_,
	w_search(It_219, _C_),
	S_228=_C_,
	variables(X_223, _D_),
	fd_aeq(W-S_228, _E_),
	fd_aeq(_E_+1, _F_),
	variables(Y_226, _G_),
	fd_aeq(H-S_228, _H_),
	fd_aeq(_H_+1, _I_),
	rcp_assert(I_and),
	domain(_D_, 1, _F_),
	domain(_G_, 1, _I_),
	state_items_domain_foldl_1_search(Tail, _K_, [H,Items,W], _J_).

state_items_domain(Items, W, H, _C_) :- 
	state_items_domain_foldl_1(Items, 1, [H,Items,W], _A_),
	lower_quadrant(Items, W, H, _B_),
	fd_and(_A_, _B_, _C_).

state_items_domain_search(Items, W, H, _C_) :- 
	state_items_domain_foldl_1_search(Items, 1, [H,Items,W], _A_),
	rcp_assert(_A_),
	lower_quadrant_search(Items, W, H, _B_),
	rcp_assert(_B_),
	fd_aeq(_C_, 1).

lower_quadrant(Items, W, H, _L_) :- 
	nth1(1, Items, _A_),
	FstIt_229=_A_,
	x(FstIt_229, _B_),
	X_233=_B_,
	y(FstIt_229, _C_),
	Y_236=_C_,
	w(FstIt_229, _D_),
	S_238=_D_,
	variables(X_233, _E_),
	fd_aeq(W-S_238, _F_),
	fd_aeq(_F_+2, _G_),
	fd_aeq(_G_/2, _H_),
	variables(Y_236, _I_),
	fd_aeq(H+1, _J_),
	fd_aeq(_J_/2, _K_),
	domain(_E_, 1, _H_),
	domain(_I_, 1, _K_),
	fd_aeq(_L_, 1).

lower_quadrant_search(Items, W, H, _L_) :- 
	nth1(1, Items, _A_),
	FstIt_229=_A_,
	x_search(FstIt_229, _B_),
	X_233=_B_,
	y_search(FstIt_229, _C_),
	Y_236=_C_,
	w_search(FstIt_229, _D_),
	S_238=_D_,
	variables(X_233, _E_),
	fd_aeq(W-S_238, _F_),
	fd_aeq(_F_+2, _G_),
	fd_aeq(_G_/2, _H_),
	variables(Y_236, _I_),
	fd_aeq(H+1, _J_),
	fd_aeq(_J_/2, _K_),
	domain(_E_, 1, _H_),
	domain(_I_, 1, _K_),
	fd_aeq(_L_, 1).

state_items_constraints_foldr_1([], _, []) :- 
	!.
state_items_constraints_foldr_1([It_239|Tail], [H,Items,W], [_A_|_B_]) :- 
	state_items_constraints_foldr_1(Tail, [H,Items,W], _B_),
	x(It_239, _A_).

state_items_constraints_foldr_1_search([], _, []) :- 
	!.
state_items_constraints_foldr_1_search([It_239|Tail], [H,Items,W], [_A_|_B_]) :- 
	state_items_constraints_foldr_1_search(Tail, [H,Items,W], _B_),
	x_search(It_239, _A_).

state_items_constraints_foldr_2([], _, []) :- 
	!.
state_items_constraints_foldr_2([It_245|Tail], [H,Items,W], [_A_|_B_]) :- 
	state_items_constraints_foldr_2(Tail, [H,Items,W], _B_),
	y(It_245, _A_).

state_items_constraints_foldr_2_search([], _, []) :- 
	!.
state_items_constraints_foldr_2_search([It_245|Tail], [H,Items,W], [_A_|_B_]) :- 
	state_items_constraints_foldr_2_search(Tail, [H,Items,W], _B_),
	y_search(It_245, _A_).

state_items_constraints_foldr_3([], _, []) :- 
	!.
state_items_constraints_foldr_3([It_249|Tail], [H,Items,W], [_A_|_B_]) :- 
	state_items_constraints_foldr_3(Tail, [H,Items,W], _B_),
	w(It_249, _A_).

state_items_constraints_foldr_3_search([], _, []) :- 
	!.
state_items_constraints_foldr_3_search([It_249|Tail], [H,Items,W], [_A_|_B_]) :- 
	state_items_constraints_foldr_3_search(Tail, [H,Items,W], _B_),
	w_search(It_249, _A_).

state_items_constraints(Items, W, H, _G_) :- 
	state_items_constraints_foldr_1(Items, [H,Items,W], _A_),
	Xs_240=_A_,
	state_items_constraints_foldr_2(Items, [H,Items,W], _B_),
	Ys_246=_B_,
	state_items_constraints_foldr_3(Items, [H,Items,W], _C_),
	Ss_250=_C_,
	disjoint2(Xs_240, Ys_246, Ss_250, _D_),
	cumulative(Xs_240, Ss_250, Ss_250, H, _E_),
	cumulative(Ys_246, Ss_250, Ss_250, W, _F_),
	_F_,
	_D_,
	_E_,
	fd_aeq(_G_, 1).

state_items_constraints_search(Items, W, H, _G_) :- 
	state_items_constraints_foldr_1_search(Items, [H,Items,W], _A_),
	Xs_240=_A_,
	state_items_constraints_foldr_2_search(Items, [H,Items,W], _B_),
	Ys_246=_B_,
	state_items_constraints_foldr_3_search(Items, [H,Items,W], _C_),
	Ss_250=_C_,
	disjoint2(Xs_240, Ys_246, Ss_250, _D_),
	rcp_assert(_D_),
	cumulative(Xs_240, Ss_250, Ss_250, H, _E_),
	rcp_assert(_E_),
	cumulative(Ys_246, Ss_250, Ss_250, W, _F_),
	rcp_assert(_F_),
	fd_aeq(_G_, 1).

state_items_search_foldr_1([], _, []) :- 
	!.
state_items_search_foldr_1([It_251|Tail], [H,Items,W], [_C_|_D_]) :- 
	state_items_search_foldr_1(Tail, [H,Items,W], _D_),
	x(It_251, _A_),
	w(It_251, _B_),
	rcp_rec([coord=_A_,siz=_B_], _C_).

state_items_search_foldr_1_search([], _, []) :- 
	!.
state_items_search_foldr_1_search([It_251|Tail], [H,Items,W], [_C_|_D_]) :- 
	state_items_search_foldr_1_search(Tail, [H,Items,W], _D_),
	x_search(It_251, _A_),
	w_search(It_251, _B_),
	rcp_rec([coord=_A_,siz=_B_], _C_).

state_items_search_foldr_2([], _, []) :- 
	!.
state_items_search_foldr_2([It_262|Tail], [H,Items,W], [_C_|_D_]) :- 
	state_items_search_foldr_2(Tail, [H,Items,W], _D_),
	y(It_262, _A_),
	h(It_262, _B_),
	rcp_rec([coord=_A_,siz=_B_], _C_).

state_items_search_foldr_2_search([], _, []) :- 
	!.
state_items_search_foldr_2_search([It_262|Tail], [H,Items,W], [_C_|_D_]) :- 
	state_items_search_foldr_2_search(Tail, [H,Items,W], _D_),
	y_search(It_262, _A_),
	h_search(It_262, _B_),
	rcp_rec([coord=_A_,siz=_B_], _C_).

state_items_search_foldl_3([], I_and, _, I_and) :- 
	!.
state_items_search_foldl_3([XS_289|Tail], I_and, [H,Items,W], _J_) :- 
	rcp_att(siz, XS_289, _A_),
	fd_gt(_A_, 6, _B_),
	rcp_att(coord, XS_289, _C_),
	rcp_att(siz, XS_289, _D_),
	fd_aeq(_D_*3, _E_),
	fd_aeq(_E_/10, _F_),
	fd_aeq(max(1,_F_), _G_),
	interval_split(_C_, Min_271, MaxX_278, _G_, _H_),
	fd_implies(_B_, _H_, _I_),
	fd_and(I_and, _I_, _K_),
	state_items_search_foldl_3(Tail, _K_, [H,Items,W], _J_).

state_items_search_foldl_3_search([], I_and, _, I_and) :- 
	!.
state_items_search_foldl_3_search([XS_289|Tail], I_and, [H,Items,W], _J_) :- 
	rcp_att(siz, XS_289, _A_),
	rcp_att(siz, XS_289, _B_),
	(
	fd_gt(_A_, 6),
	rcp_att(coord, XS_289, _C_),
	rcp_att(siz, XS_289, _D_),
	fd_aeq(_D_*3, _E_),
	fd_aeq(_E_/10, _F_),
	fd_aeq(max(1,_F_), _G_),
	interval_split_search(_C_, Min_271, MaxX_278, _G_, _H_),
	fd_equiv(_H_, _I_, 1)
	;
	fd_leq(_B_, 6),
	fd_aeq(_I_, 1)
	),
	rcp_assert(I_and),
	rcp_assert(_I_),
	state_items_search_foldl_3_search(Tail, _K_, [H,Items,W], _J_).

state_items_search_foldl_4([], I_and, _, I_and) :- 
	!.
state_items_search_foldl_4([XS_290|Tail], I_and, [H,Items,W], _C_) :- 
	rcp_att(coord, XS_290, _A_),
	dichotomic_split(_A_, _B_),
	fd_and(I_and, _B_, _D_),
	state_items_search_foldl_4(Tail, _D_, [H,Items,W], _C_).

state_items_search_foldl_4_search([], I_and, _, I_and) :- 
	!.
state_items_search_foldl_4_search([XS_290|Tail], I_and, [H,Items,W], _C_) :- 
	rcp_att(coord, XS_290, _A_),
	dichotomic_split_search(_A_, _B_),
	rcp_assert(I_and),
	rcp_assert(_B_),
	state_items_search_foldl_4_search(Tail, _D_, [H,Items,W], _C_).

state_items_search_foldl_5([], I_and, _, I_and) :- 
	!.
state_items_search_foldl_5([YS_291|Tail], I_and, [H,Items,W], _G_) :- 
	rcp_att(coord, YS_291, _A_),
	rcp_att(siz, YS_291, _B_),
	fd_aeq(_B_*3, _C_),
	fd_aeq(_C_/10, _D_),
	fd_aeq(max(1,_D_), _E_),
	interval_split(_A_, Min_271, MaxY_284, _E_, _F_),
	fd_and(I_and, _F_, _H_),
	state_items_search_foldl_5(Tail, _H_, [H,Items,W], _G_).

state_items_search_foldl_5_search([], I_and, _, I_and) :- 
	!.
state_items_search_foldl_5_search([YS_291|Tail], I_and, [H,Items,W], _G_) :- 
	rcp_att(coord, YS_291, _A_),
	rcp_att(siz, YS_291, _B_),
	fd_aeq(_B_*3, _C_),
	fd_aeq(_C_/10, _D_),
	fd_aeq(max(1,_D_), _E_),
	interval_split_search(_A_, Min_271, MaxY_284, _E_, _F_),
	rcp_assert(I_and),
	rcp_assert(_F_),
	state_items_search_foldl_5_search(Tail, _H_, [H,Items,W], _G_).

state_items_search_foldl_6([], I_and, _, I_and) :- 
	!.
state_items_search_foldl_6([YS_292|Tail], I_and, [H,Items,W], _C_) :- 
	rcp_att(coord, YS_292, _A_),
	dichotomic_split(_A_, _B_),
	fd_and(I_and, _B_, _D_),
	state_items_search_foldl_6(Tail, _D_, [H,Items,W], _C_).

state_items_search_foldl_6_search([], I_and, _, I_and) :- 
	!.
state_items_search_foldl_6_search([YS_292|Tail], I_and, [H,Items,W], _C_) :- 
	rcp_att(coord, YS_292, _A_),
	dichotomic_split_search(_A_, _B_),
	rcp_assert(I_and),
	rcp_assert(_B_),
	state_items_search_foldl_6_search(Tail, _D_, [H,Items,W], _C_).

state_items_search(Items, W, H, _I_) :- 
	state_items_search_foldr_1(Items, [H,Items,W], _A_),
	XSs_252=_A_,
	state_items_search_foldr_2(Items, [H,Items,W], _B_),
	YSs_263=_B_,
	Min_271=1,
	fd_aeq(W+1, _C_),
	MaxX_278=_C_,
	fd_aeq(H+1, _D_),
	MaxY_284=_D_,
	state_items_search_foldl_3_search(XSs_252, 1, [H,Items,W], _E_),
	rcp_assert(_E_),
	state_items_search_foldl_4_search(XSs_252, 1, [H,Items,W], _F_),
	rcp_assert(_F_),
	state_items_search_foldl_5_search(YSs_263, 1, [H,Items,W], _G_),
	rcp_assert(_G_),
	state_items_search_foldl_6_search(YSs_263, 1, [H,Items,W], _H_),
	rcp_assert(_H_),
	fd_aeq(_I_, 1).

state_items_search_search(Items, W, H, _I_) :- 
	state_items_search_foldr_1_search(Items, [H,Items,W], _A_),
	XSs_252=_A_,
	state_items_search_foldr_2_search(Items, [H,Items,W], _B_),
	YSs_263=_B_,
	Min_271=1,
	fd_aeq(W+1, _C_),
	MaxX_278=_C_,
	fd_aeq(H+1, _D_),
	MaxY_284=_D_,
	state_items_search_foldl_3_search(XSs_252, 1, [H,Items,W], _E_),
	rcp_assert(_E_),
	state_items_search_foldl_4_search(XSs_252, 1, [H,Items,W], _F_),
	rcp_assert(_F_),
	state_items_search_foldl_5_search(YSs_263, 1, [H,Items,W], _G_),
	rcp_assert(_G_),
	state_items_search_foldl_6_search(YSs_263, 1, [H,Items,W], _H_),
	rcp_assert(_H_),
	fd_aeq(_I_, 1).

solve_bin_subproblem(W, H, A, L, U, N, _E_) :- 
	state_bin_domain(W, H, A, L, U, N, _A_),
	state_bin_constraints(W, H, A, N, _B_),
	fd_and(_A_, _B_, _C_),
	state_bin_search(_D_),
	fd_and(_C_, _D_, _E_).

solve_bin_subproblem_search(W, H, A, L, U, N, _D_) :- 
	state_bin_domain_search(W, H, A, L, U, N, _A_),
	rcp_assert(_A_),
	state_bin_constraints_search(W, H, A, N, _B_),
	rcp_assert(_B_),
	state_bin_search_search(_C_),
	rcp_assert(_C_),
	fd_aeq(_D_, 1).

state_bin_domain(W, H, A, L, U, N, _C_) :- 
	variables([W,H], _A_),
	rcp_range([L..U], _B_),
	domain(_A_, N, L),
	A in _B_,
	fd_aeq(_C_, 1).

state_bin_domain_search(W, H, A, L, U, N, _C_) :- 
	variables([W,H], _A_),
	rcp_range([L..U], _B_),
	domain(_A_, N, L),
	A in _B_,
	fd_aeq(_C_, 1).

state_bin_constraints(W, H, A, N, _U_) :- 
	fd_aeq(W+1, _A_),
	fd_aeq(_A_/2, _B_),
	K_293=_B_,
	fd_aeq(W*H, _C_),
	fd_eq(A, _C_, _D_),
	fd_leq(W, H, _E_),
	fd_and(_D_, _E_, _F_),
	fd_aeq(2*N, _G_),
	fd_aeq(_G_-1, _H_),
	fd_geq(W, _H_, _I_),
	fd_aeq(N*N, _J_),
	fd_aeq(_J_+N, _K_),
	fd_aeq(K_293-1, _L_),
	fd_aeq(K_293-1, _M_),
	fd_aeq(_L_*_M_, _N_),
	fd_aeq(_K_-_N_, _O_),
	fd_aeq(K_293-1, _P_),
	fd_aeq(_O_-_P_, _Q_),
	fd_aeq(_Q_/2, _R_),
	fd_geq(H, _R_, _S_),
	fd_or(_I_, _S_, _T_),
	fd_and(_F_, _T_, _U_).

state_bin_constraints_search(W, H, A, N, _O_) :- 
	fd_aeq(W+1, _A_),
	fd_aeq(_A_/2, _B_),
	K_293=_B_,
	fd_aeq(W*H, _C_),
	fd_aeq(2*N, _D_),
	fd_aeq(_D_-1, _E_),
	fd_aeq(N*N, _F_),
	fd_aeq(_F_+N, _G_),
	fd_aeq(K_293-1, _H_),
	fd_aeq(K_293-1, _I_),
	fd_aeq(_H_*_I_, _J_),
	fd_aeq(_G_-_J_, _K_),
	fd_aeq(K_293-1, _L_),
	fd_aeq(_K_-_L_, _M_),
	fd_aeq(_M_/2, _N_),
	fd_eq(A, _C_),
	fd_leq(W, H),
	(
	fd_geq(W, _E_)
	;
	fd_geq(H, _N_)
	),
	fd_aeq(_O_, 1).

var_order_criterion(1, Candidate, [], _B_) :- 
	rcp_call(Candidate, _A_),
	rcp_att(area, _A_, _B_).
var_order_criterion(2, Candidate, [], _B_) :- 
	rcp_call(Candidate, _A_),
	w(_A_, _B_).
var_order_criterion(3, Candidate, [], _B_) :- 
	rcp_call(Candidate, _A_),
	h(_A_, _B_).

state_bin_search(_C_) :- 
	bin(_A_),
	variables(_A_, _B_),
	rcp_labeling(_B_),
	variable_ordering([is(var_order_criterion(1,[])),
			   is(var_order_criterion(2,[])),
			   is(var_order_criterion(3,[]))]),
	fd_aeq(_C_, 1).

state_bin_search_search(_C_) :- 
	bin_search(_A_),
	variables(_A_, _B_),
	rcp_labeling(_B_),
	variable_ordering([is(var_order_criterion(1,[])),
			   is(var_order_criterion(2,[])),
			   is(var_order_criterion(3,[]))]),
	fd_aeq(_C_, 1).

interval_predicate_foldr_1([], _, []) :- 
	!.
interval_predicate_foldr_1([K_294|Tail], [L,Max,Min,X], [_D_|_E_]) :- 
	interval_predicate_foldr_1(Tail, [L,Max,Min,X], _E_),
	fd_aeq(K_294*L, _A_),
	fd_aeq(Min+_A_, _B_),
	fd_aeq(_B_+K_294, _C_),
	fd_aeq(_C_-1, _D_).

interval_predicate_foldr_1_search([], _, []) :- 
	!.
interval_predicate_foldr_1_search([K_294|Tail], [L,Max,Min,X], [_D_|_E_]) :- 
	interval_predicate_foldr_1_search(Tail, [L,Max,Min,X], _E_),
	fd_aeq(K_294*L, _A_),
	fd_aeq(Min+_A_, _B_),
	fd_aeq(_B_+K_294, _C_),
	fd_aeq(_C_-1, _D_).

interval_split_foldl_2([], I_interval_predicate, _, I_interval_predicate) :- 
	!.
interval_split_foldl_2([Cut_295|Tail], I_interval_predicate, [L,Max,Min,X], _A_) :- 
	interval_predicate(I_interval_predicate, [X,L], _B_),
	interval_split_foldl_2(Tail, _B_, [L,Max,Min,X], _A_).

interval_split_foldl_2_search([], I_interval_predicate, _, I_interval_predicate) :- 
	!.
interval_split_foldl_2_search([Cut_295|Tail], I_interval_predicate, [L,Max,Min,X], _A_) :- 
	interval_predicate_search(I_interval_predicate, [X,L], _B_),
	interval_split_foldl_2_search(Tail, _B_, [L,Max,Min,X], _A_).

interval_split(X, Min, Max, L, _G_) :- 
	fd_aeq(Max-Min, _A_),
	fd_aeq(L+1, _B_),
	fd_aeq(_A_/_B_, _C_),
	fd_aeq(_C_+1, _D_),
	rcp_range([1.._D_], _E_),
	interval_predicate_foldr_1(_E_, [L,Max,Min,X], _F_),
	interval_split_foldl_2(_F_, 1, [L,Max,Min,X], _G_).

interval_split_search(X, Min, Max, L, _G_) :- 
	fd_aeq(Max-Min, _A_),
	fd_aeq(L+1, _B_),
	fd_aeq(_A_/_B_, _C_),
	fd_aeq(_C_+1, _D_),
	rcp_range([1.._D_], _E_),
	interval_predicate_foldr_1_search(_E_, [L,Max,Min,X], _F_),
	interval_split_foldl_2_search(_F_, 1, [L,Max,Min,X], _G_).

interval_predicate(E, List, _H_) :- 
	nth1(1, List, _A_),
	X_296=_A_,
	nth1(2, List, _B_),
	L_299=_B_,
	fd_min(X_296, _C_),
	fd_aeq(_C_+L_299, _D_),
	XDLBL_301=_D_,
	fd_leq(X_296, XDLBL_301, _E_),
	fd_gt(X_296, XDLBL_301, _F_),
	fd_and(_F_, E, _G_),
	fd_or(_E_, _G_, _H_).

interval_predicate_search(E, List, _E_) :- 
	nth1(1, List, _A_),
	X_296=_A_,
	nth1(2, List, _B_),
	L_299=_B_,
	fd_min(X_296, _C_),
	fd_aeq(_C_+L_299, _D_),
	XDLBL_301=_D_,
	(
	fd_leq(X_296, XDLBL_301)
	;
	fd_gt(X_296, XDLBL_301),
	rcp_assert(E)
	),
	fd_aeq(_E_, 1).

dichotomic_split_foldl_1([], I_dichotomy_predicate, _, I_dichotomy_predicate) :- 
	!.
dichotomic_split_foldl_1([Cut_304|Tail], I_dichotomy_predicate, [X], _A_) :- 
	dichotomy_predicate(I_dichotomy_predicate, X, _B_),
	dichotomic_split_foldl_1(Tail, _B_, [X], _A_).

dichotomic_split_foldl_1_search([], I_dichotomy_predicate, _, I_dichotomy_predicate) :- 
	!.
dichotomic_split_foldl_1_search([Cut_304|Tail], I_dichotomy_predicate, [X], _A_) :- 
	dichotomy_predicate_search(I_dichotomy_predicate, X, _B_),
	dichotomic_split_foldl_1_search(Tail, _B_, [X], _A_).

dichotomic_split(X, _G_) :- 
	fd_max(X, _A_),
	fd_min(X, _B_),
	fd_aeq(_A_-_B_, _C_),
	fd_aeq(_C_+1, _D_),
	L_302=_D_,
	log(2, L_302, _E_),
	rcp_range([1.._E_], _F_),
	dichotomic_split_foldl_1(_F_, 1, [X], _G_).

dichotomic_split_search(X, _G_) :- 
	fd_max(X, _A_),
	fd_min(X, _B_),
	fd_aeq(_A_-_B_, _C_),
	fd_aeq(_C_+1, _D_),
	L_302=_D_,
	log(2, L_302, _E_),
	rcp_range([1.._E_], _F_),
	dichotomic_split_foldl_1_search(_F_, 1, [X], _G_).

dichotomy_predicate(List, X, _G_) :- 
	fd_min(X, _A_),
	fd_max(X, _B_),
	fd_aeq(_A_+_B_, _C_),
	fd_aeq(_C_/2, _D_),
	XDM_305=_D_,
	fd_leq(X, XDM_305, _E_),
	fd_gt(X, XDM_305, _F_),
	fd_or(_E_, _F_, _G_).

dichotomy_predicate_search(List, X, _E_) :- 
	fd_min(X, _A_),
	fd_max(X, _B_),
	fd_aeq(_A_+_B_, _C_),
	fd_aeq(_C_/2, _D_),
	XDM_305=_D_,
	(
	fd_leq(X, XDM_305)
	;
	fd_gt(X, XDM_305)
	),
	fd_aeq(_E_, 1).

solve([Bin_3,Bin_2,Bin,Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2,Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5,Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2_2,Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5_2]) :- 
	rcp_var(from(bin,0,3), Bin_3),
	_A_#=Bin-3+1,
	_B_#=Bin_2-3+1,
	rcp_var(from(item(rcp_rec([sboxes=[rcp_rec([box=rcp_rec([size=[2,2],uid=4]),offset=[0,0],uid=5])]])),0,1), Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5),
	_C_#=Bin-2+1,
	rcp_var(from(item(rcp_rec([sboxes=[rcp_rec([box=rcp_rec([size=[2,2],uid=4]),offset=[0,0],uid=5])]])),0,2), Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5_2),
	_D_#=Bin_2-2+1,
	rcp_var(from(item(rcp_rec([sboxes=[rcp_rec([box=rcp_rec([size=[3,3],uid=1]),offset=[0,0],uid=2])]])),0,1), Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2),
	_E_#=(Bin-3+2)/2,
	rcp_var(from(item(rcp_rec([sboxes=[rcp_rec([box=rcp_rec([size=[3,3],uid=1]),offset=[0,0],uid=2])]])),0,2), Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2_2),
	_F_#=(Bin_2+1)/2,
	rcp_var(from(bin,0,2), Bin_2),
	rcp_var(from(bin,0,1), Bin),
	domain([Bin,Bin_2], 3, 14),
	Bin_3 in 14..214,
	Bin_3#=Bin*Bin_2,
	Bin#=<Bin_2,
	Bin#>=5#\/Bin_2#>=(12-((Bin+1)/2-1)*((Bin+1)/2-1)-((Bin+1)/2-1))/2,
	labeling([], [Bin_3,Bin,Bin_2]),
	domain([Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2], 1, _A_),
	domain([Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2_2], 1, _B_),
	domain([Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5], 1, _C_),
	domain([Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5_2], 1, _D_),
	domain([Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2], 1, _E_),
	domain([Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2_2], 1, _F_),
	disjoint2([r(Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2,3,Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2_2,3),r(Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5,2,Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5_2,2)]),
	domain([_89399,_89401], 0, 1000),
	cumulative([task(Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2,3,_89399,3,0),task(Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5,2,_89401,2,0)], [limit(Bin_2),global(true)]),
	domain([_89464,_89466], 0, 1000),
	cumulative([task(Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2_2,3,_89464,3,0),task(Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5_2,2,_89466,2,0)], [limit(Bin),global(true)]),
	rcp_var(from(item(rcp_rec([sboxes=[rcp_rec([box=rcp_rec([size=[3,3],uid=1]),offset=[0,0],uid=2])]])),0,1), Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2),
	rcp_rec([coord=Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2,siz=3,uid=13], _G_),
	rcp_var(from(item(rcp_rec([sboxes=[rcp_rec([box=rcp_rec([size=[2,2],uid=4]),offset=[0,0],uid=5])]])),0,1), Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5),
	rcp_rec([coord=Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5,siz=2,uid=14], _H_),
	state_items_search_foldl_1_search([_G_,_H_], 1, [], _I_),
	rcp_assert(_I_),
	rcp_var(from(item(rcp_rec([sboxes=[rcp_rec([box=rcp_rec([size=[3,3],uid=1]),offset=[0,0],uid=2])]])),0,1), Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2),
	rcp_rec([coord=Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2,siz=3,uid=13], _J_),
	rcp_var(from(item(rcp_rec([sboxes=[rcp_rec([box=rcp_rec([size=[2,2],uid=4]),offset=[0,0],uid=5])]])),0,1), Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5),
	rcp_rec([coord=Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5,siz=2,uid=14], _K_),
	state_items_search_foldl_2_search([_J_,_K_], 1, [], _L_),
	rcp_assert(_L_),
	rcp_var(from(item(rcp_rec([sboxes=[rcp_rec([box=rcp_rec([size=[3,3],uid=1]),offset=[0,0],uid=2])]])),0,2), Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2_2),
	rcp_rec([coord=Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2_2,siz=3,uid=15], _M_),
	rcp_var(from(item(rcp_rec([sboxes=[rcp_rec([box=rcp_rec([size=[2,2],uid=4]),offset=[0,0],uid=5])]])),0,2), Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5_2),
	rcp_rec([coord=Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5_2,siz=2,uid=16], _N_),
	state_items_search_foldl_3_search([_M_,_N_], 1, [], _O_),
	rcp_assert(_O_),
	rcp_var(from(item(rcp_rec([sboxes=[rcp_rec([box=rcp_rec([size=[3,3],uid=1]),offset=[0,0],uid=2])]])),0,2), Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2_2),
	rcp_rec([coord=Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_3_3_uid_1_offset_0_0_uid_2_2,siz=3,uid=15], _P_),
	rcp_var(from(item(rcp_rec([sboxes=[rcp_rec([box=rcp_rec([size=[2,2],uid=4]),offset=[0,0],uid=5])]])),0,2), Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5_2),
	rcp_rec([coord=Item_rcp_rec_sboxes_rcp_rec_box_rcp_rec_size_2_2_uid_4_offset_0_0_uid_5_2,siz=2,uid=16], _Q_),
	state_items_search_foldl_4_search([_P_,_Q_], 1, [], _R_),
	rcp_assert(_R_).
