0

I'm now solving a scheduling problem and I'm a bit stuck on the way how to describe number or count of certain items in result.

For example, the result is a list of numbers between 1..7 and I want to at least see or better to define constraints on the count of 1s in the result, 2s, 3s and so on.

I tried some solutions like this:

schedule(D1_D1,D1_D2,D1_N1,D1_N2,D1_A0,D2_D1,D2_D2,D2_N1,D2_N2,D2_A0,D3_D1,D3_D2,D3_N1,D3_N2,D3_A0,D4_D1,D4_D2,D4_N1,D4_N2,D4_A0,D5_D1,D5_D2,D5_N1,D5_N2,D5_A0,D6_D1,D6_D2,D6_N1,D6_N2,D6_A0,D7_D1,D7_D2,D7_N1,D7_N2,D7_A0,SUM_1, SUM_2, SUM_3, SUM_4, SUM_5, SUM_6) :-
Vars = [D1_D1,D1_D2,D1_N1,D1_N2,D1_A0,D2_D1,D2_D2,D2_N1,D2_N2,D2_A0,D3_D1,D3_D2,D3_N1,D3_N2,D3_A0,D4_D1,D4_D2,D4_N1,D4_N2,D4_A0,D5_D1,D5_D2,D5_N1,D5_N2,D5_A0,D6_D1,D6_D2,D6_N1,D6_N2,D6_A0,D7_D1,D7_D2,D7_N1,D7_N2,D7_A0],
Vars ins 1..6,
Sums = [SUM_1, SUM_2, SUM_3, SUM_4, SUM_5, SUM_6],
Sums ins 1..7,
all_distinct([D1_D1,D1_D2,D1_N1,D1_N2,D1_A0]),
all_distinct([D2_D1,D2_D2,D2_N1,D2_N2,D2_A0]),
all_distinct([D3_D1,D3_D2,D3_N1,D3_N2,D3_A0]),
all_distinct([D4_D1,D4_D2,D4_N1,D4_N2,D4_A0]),
all_distinct([D5_D1,D5_D2,D5_N1,D5_N2,D5_A0]),
all_distinct([D6_D1,D6_D2,D6_N1,D6_N2,D6_A0]),
all_distinct([D7_D1,D7_D2,D7_N1,D7_N2,D7_A0]),

include(=(1),Vars,I1),length(I1, SUM_1),
include(=(2),Vars,I2),length(I2, SUM_2),
include(=(3),Vars,I3),length(I3, SUM_3),
include(=(4),Vars,I4),length(I4, SUM_4),
include(=(5),Vars,I5),length(I5, SUM_5),
include(=(6),Vars,I6),length(I6, SUM_6),
append(Vars,Sums,Res),
label(Res).

I define some permutations of the numbers using all_distinct and later I want to see (get the value to the SUM_N) count of a certain number in the result (VARS) list.

But in SWI Prolog I get:

?- schedule(D1_D1,D1_D2,D1_N1,D1_N2,D1_A0,D2_D1,D2_D2,D2_N1,D2_N2,D2_A0,D3_D1,D3_D2,D3_N1,D3_N2,D3_A0,D4_D1,D4_D2,D4_N1,D4_N2,D4_A0,D5_D1,D5_D2,D5_N1,D5_N2,D5_A0,D6_D1,D6_D2,D6_N1,D6_N2,D6_A0,D7_D1,D7_D2,D7_N1,D7_N2,D7_A0,SUM_1, SUM_2, SUM_3, SUM_4, SUM_5, SUM_6).
   false.

But there are obviously some solutions matching the criteria. If I remove the "constraints" counting the ocurences of numbers I will get all the suitable permutations. Therefore I think the include(..),length(..) somehow constraints the result but I don't understand why since the result is pointed to a free variable.

false
  • 10,264
  • 13
  • 101
  • 209
Jan Drozen
  • 894
  • 2
  • 12
  • 28
  • https://stackoverflow.com/questions/32478193/using-a-constrained-variable-with-length-2 – User9213 Apr 05 '19 at 09:56
  • Maybe I have in the meantime found a smarter solution: https://stackoverflow.com/questions/16399792/how-to-add-domain-variable-to-global-cardinality?rq=1 – Jan Drozen Apr 05 '19 at 12:12
  • `include/3` is impure and thus incompatible with constraints. Minimal example: `X=2, X in 1..2, include(=(1),[X],[]).` succeeds, yet its generalization `X in 1..2, include(=(1),[X],[]).` fails. – false Apr 08 '19 at 09:29

0 Answers0