1 module Gecode::SetEnum |

2 class SetEnumConstraintReceiver |

3 # Constrains all pairs of set operands in the enumeration to at |

4 # most have one element in common and be of a specified size. |

5 # Providing a size is not optional. |

6 # |

7 # Neither negation nor reification is supported. |

8 # |

9 # ==== Examples |

10 # |

11 # # All set operands in +sets+ must have cardinality 17 and no pair may |

12 # # have more than one element in common. |

13 # sets.must.at_most_share_one_element(:size => 17) |

14 def at_most_share_one_element(options = {}) |

15 unless options.has_key? :size |

16 raise ArgumentError, 'Option :size has to be specified.' |

17 end |

18 # TODO can we use Set::Util::decode_options here instead? |

19 unless options.size == 1 |

20 raise ArgumentError, 'Only the option :size is accepted, got ' + |

21 "#{options.keys.join(', ')}." |

22 end |

23 if @params[:negate] |

24 raise Gecode::MissingConstraintError, 'A negated atmost one ' + |

25 'constrain is not implemented.' |

26 end |

27 |

28 @model.add_constraint Distinct::AtMostOneConstraint.new( |

29 @model, @params.update(options)) |

30 end |

31 end |

32 |

33 # A module that gathers the classes and modules used in distinct constraints. |

34 module Distinct #:nodoc: |

35 class AtMostOneConstraint < Gecode::Constraint #:nodoc: |

36 def post |

37 sets, size = @params.values_at(:lhs, :size) |

38 Gecode::Raw::atmostOne(@model.active_space, |

39 sets.to_set_enum.bind_array, size) |

40 end |

41 end |

42 end |

43 end |