gecoder-with-gecode 0.9.0-x86-mswin32-60

Sign up to get free protection for your applications and to get access to all the features.
Files changed (203) hide show
  1. data/CHANGES +137 -0
  2. data/COPYING +17 -0
  3. data/LGPL-LICENSE +458 -0
  4. data/README +58 -0
  5. data/Rakefile +14 -0
  6. data/example/equation_system.rb +15 -0
  7. data/example/example_helper.rb +1 -0
  8. data/example/magic_sequence.rb +43 -0
  9. data/example/money.rb +36 -0
  10. data/example/queens.rb +42 -0
  11. data/example/send_more_money.rb +43 -0
  12. data/example/send_most_money.rb +58 -0
  13. data/example/square_tiling.rb +84 -0
  14. data/example/sudoku-set.rb +106 -0
  15. data/example/sudoku.rb +56 -0
  16. data/lib/gecode.dll +0 -0
  17. data/lib/gecoder.rb +5 -0
  18. data/lib/gecoder/bindings.rb +96 -0
  19. data/lib/gecoder/bindings/bindings.rb +2029 -0
  20. data/lib/gecoder/interface.rb +9 -0
  21. data/lib/gecoder/interface/binding_changes.rb +9 -0
  22. data/lib/gecoder/interface/branch.rb +163 -0
  23. data/lib/gecoder/interface/constraints.rb +471 -0
  24. data/lib/gecoder/interface/constraints/bool/boolean.rb +251 -0
  25. data/lib/gecoder/interface/constraints/bool/channel.rb +7 -0
  26. data/lib/gecoder/interface/constraints/bool/linear.rb +200 -0
  27. data/lib/gecoder/interface/constraints/bool_enum/channel.rb +68 -0
  28. data/lib/gecoder/interface/constraints/bool_enum/extensional.rb +106 -0
  29. data/lib/gecoder/interface/constraints/bool_enum/relation.rb +55 -0
  30. data/lib/gecoder/interface/constraints/bool_enum_constraints.rb +84 -0
  31. data/lib/gecoder/interface/constraints/bool_var_constraints.rb +155 -0
  32. data/lib/gecoder/interface/constraints/extensional_regexp.rb +101 -0
  33. data/lib/gecoder/interface/constraints/fixnum_enum/element.rb +63 -0
  34. data/lib/gecoder/interface/constraints/fixnum_enum/operation.rb +65 -0
  35. data/lib/gecoder/interface/constraints/fixnum_enum_constraints.rb +42 -0
  36. data/lib/gecoder/interface/constraints/int/arithmetic.rb +150 -0
  37. data/lib/gecoder/interface/constraints/int/channel.rb +51 -0
  38. data/lib/gecoder/interface/constraints/int/domain.rb +80 -0
  39. data/lib/gecoder/interface/constraints/int/linear.rb +143 -0
  40. data/lib/gecoder/interface/constraints/int/relation.rb +141 -0
  41. data/lib/gecoder/interface/constraints/int_enum/arithmetic.rb +63 -0
  42. data/lib/gecoder/interface/constraints/int_enum/channel.rb +86 -0
  43. data/lib/gecoder/interface/constraints/int_enum/count.rb +66 -0
  44. data/lib/gecoder/interface/constraints/int_enum/distinct.rb +64 -0
  45. data/lib/gecoder/interface/constraints/int_enum/element.rb +64 -0
  46. data/lib/gecoder/interface/constraints/int_enum/equality.rb +37 -0
  47. data/lib/gecoder/interface/constraints/int_enum/extensional.rb +187 -0
  48. data/lib/gecoder/interface/constraints/int_enum/sort.rb +135 -0
  49. data/lib/gecoder/interface/constraints/int_enum_constraints.rb +95 -0
  50. data/lib/gecoder/interface/constraints/int_var_constraints.rb +230 -0
  51. data/lib/gecoder/interface/constraints/reifiable_constraints.rb +78 -0
  52. data/lib/gecoder/interface/constraints/selected_set/select.rb +120 -0
  53. data/lib/gecoder/interface/constraints/selected_set_constraints.rb +75 -0
  54. data/lib/gecoder/interface/constraints/set/cardinality.rb +65 -0
  55. data/lib/gecoder/interface/constraints/set/channel.rb +51 -0
  56. data/lib/gecoder/interface/constraints/set/connection.rb +130 -0
  57. data/lib/gecoder/interface/constraints/set/domain.rb +156 -0
  58. data/lib/gecoder/interface/constraints/set/include.rb +36 -0
  59. data/lib/gecoder/interface/constraints/set/operation.rb +118 -0
  60. data/lib/gecoder/interface/constraints/set/relation.rb +155 -0
  61. data/lib/gecoder/interface/constraints/set_elements/relation.rb +116 -0
  62. data/lib/gecoder/interface/constraints/set_elements_constraints.rb +97 -0
  63. data/lib/gecoder/interface/constraints/set_enum/channel.rb +45 -0
  64. data/lib/gecoder/interface/constraints/set_enum/distinct.rb +43 -0
  65. data/lib/gecoder/interface/constraints/set_enum/operation.rb +69 -0
  66. data/lib/gecoder/interface/constraints/set_enum/select.rb +79 -0
  67. data/lib/gecoder/interface/constraints/set_enum_constraints.rb +84 -0
  68. data/lib/gecoder/interface/constraints/set_var_constraints.rb +243 -0
  69. data/lib/gecoder/interface/enum_matrix.rb +64 -0
  70. data/lib/gecoder/interface/enum_wrapper.rb +205 -0
  71. data/lib/gecoder/interface/model.rb +453 -0
  72. data/lib/gecoder/interface/model_sugar.rb +84 -0
  73. data/lib/gecoder/interface/search.rb +197 -0
  74. data/lib/gecoder/interface/variables.rb +306 -0
  75. data/lib/gecoder/version.rb +4 -0
  76. data/specs/bool_var.rb +81 -0
  77. data/specs/branch.rb +185 -0
  78. data/specs/constraints/bool/boolean.rb +317 -0
  79. data/specs/constraints/bool/boolean_properties.rb +51 -0
  80. data/specs/constraints/bool/linear.rb +213 -0
  81. data/specs/constraints/bool_enum/bool_enum_relation.rb +117 -0
  82. data/specs/constraints/bool_enum/channel.rb +102 -0
  83. data/specs/constraints/bool_enum/extensional.rb +225 -0
  84. data/specs/constraints/constraint_helper.rb +234 -0
  85. data/specs/constraints/constraint_receivers.rb +103 -0
  86. data/specs/constraints/constraints.rb +26 -0
  87. data/specs/constraints/fixnum_enum/element.rb +58 -0
  88. data/specs/constraints/fixnum_enum/operation.rb +67 -0
  89. data/specs/constraints/int/arithmetic.rb +149 -0
  90. data/specs/constraints/int/channel.rb +101 -0
  91. data/specs/constraints/int/domain.rb +106 -0
  92. data/specs/constraints/int/linear.rb +183 -0
  93. data/specs/constraints/int/linear_properties.rb +97 -0
  94. data/specs/constraints/int/relation.rb +84 -0
  95. data/specs/constraints/int_enum/arithmetic.rb +72 -0
  96. data/specs/constraints/int_enum/channel.rb +57 -0
  97. data/specs/constraints/int_enum/count.rb +72 -0
  98. data/specs/constraints/int_enum/distinct.rb +80 -0
  99. data/specs/constraints/int_enum/element.rb +61 -0
  100. data/specs/constraints/int_enum/equality.rb +29 -0
  101. data/specs/constraints/int_enum/extensional.rb +224 -0
  102. data/specs/constraints/int_enum/sort.rb +167 -0
  103. data/specs/constraints/operands.rb +264 -0
  104. data/specs/constraints/property_helper.rb +443 -0
  105. data/specs/constraints/reification_sugar.rb +69 -0
  106. data/specs/constraints/selected_set/select.rb +56 -0
  107. data/specs/constraints/selected_set/select_properties.rb +157 -0
  108. data/specs/constraints/set/cardinality.rb +58 -0
  109. data/specs/constraints/set/cardinality_properties.rb +46 -0
  110. data/specs/constraints/set/channel.rb +77 -0
  111. data/specs/constraints/set/connection.rb +176 -0
  112. data/specs/constraints/set/domain.rb +197 -0
  113. data/specs/constraints/set/include.rb +36 -0
  114. data/specs/constraints/set/operation.rb +132 -0
  115. data/specs/constraints/set/relation.rb +117 -0
  116. data/specs/constraints/set_elements/relation.rb +84 -0
  117. data/specs/constraints/set_enum/channel.rb +80 -0
  118. data/specs/constraints/set_enum/distinct.rb +59 -0
  119. data/specs/constraints/set_enum/operation.rb +111 -0
  120. data/specs/constraints/set_enum/select.rb +73 -0
  121. data/specs/distribution.rb +14 -0
  122. data/specs/enum_matrix.rb +43 -0
  123. data/specs/enum_wrapper.rb +179 -0
  124. data/specs/examples.rb +17 -0
  125. data/specs/int_var.rb +163 -0
  126. data/specs/logging.rb +24 -0
  127. data/specs/model.rb +325 -0
  128. data/specs/model_sugar.rb +30 -0
  129. data/specs/search.rb +383 -0
  130. data/specs/selected_set.rb +39 -0
  131. data/specs/set_elements.rb +34 -0
  132. data/specs/set_var.rb +82 -0
  133. data/specs/spec_helper.rb +265 -0
  134. data/tasks/all_tasks.rb +1 -0
  135. data/tasks/dependencies.txt +22 -0
  136. data/tasks/distribution.rake +194 -0
  137. data/tasks/rcov.rake +18 -0
  138. data/tasks/specs.rake +21 -0
  139. data/tasks/svn.rake +16 -0
  140. data/tasks/website.rake +51 -0
  141. data/vendor/gecode/win32/lib/libgecodeint.dll +0 -0
  142. data/vendor/gecode/win32/lib/libgecodekernel.dll +0 -0
  143. data/vendor/gecode/win32/lib/libgecodeminimodel.dll +0 -0
  144. data/vendor/gecode/win32/lib/libgecodesearch.dll +0 -0
  145. data/vendor/gecode/win32/lib/libgecodeset.dll +0 -0
  146. data/vendor/gecode/win32/lib/libgecodesupport.dll +0 -0
  147. data/vendor/rust/README +28 -0
  148. data/vendor/rust/bin/cxxgenerator.rb +93 -0
  149. data/vendor/rust/include/rust_checks.hh +116 -0
  150. data/vendor/rust/include/rust_conversions.hh +102 -0
  151. data/vendor/rust/rust.rb +67 -0
  152. data/vendor/rust/rust/attribute.rb +51 -0
  153. data/vendor/rust/rust/bindings.rb +172 -0
  154. data/vendor/rust/rust/class.rb +337 -0
  155. data/vendor/rust/rust/constants.rb +48 -0
  156. data/vendor/rust/rust/container.rb +110 -0
  157. data/vendor/rust/rust/cppifaceparser.rb +129 -0
  158. data/vendor/rust/rust/cwrapper.rb +72 -0
  159. data/vendor/rust/rust/cxxclass.rb +96 -0
  160. data/vendor/rust/rust/element.rb +81 -0
  161. data/vendor/rust/rust/enum.rb +63 -0
  162. data/vendor/rust/rust/function.rb +407 -0
  163. data/vendor/rust/rust/namespace.rb +61 -0
  164. data/vendor/rust/rust/templates/AttributeDefinition.rusttpl +17 -0
  165. data/vendor/rust/rust/templates/AttributeInitBinding.rusttpl +9 -0
  166. data/vendor/rust/rust/templates/BindingsHeader.rusttpl +24 -0
  167. data/vendor/rust/rust/templates/BindingsUnit.rusttpl +46 -0
  168. data/vendor/rust/rust/templates/CWrapperClassDefinitions.rusttpl +64 -0
  169. data/vendor/rust/rust/templates/ClassDeclarations.rusttpl +7 -0
  170. data/vendor/rust/rust/templates/ClassInitialize.rusttpl +6 -0
  171. data/vendor/rust/rust/templates/ConstructorStub.rusttpl +21 -0
  172. data/vendor/rust/rust/templates/CxxClassDefinitions.rusttpl +100 -0
  173. data/vendor/rust/rust/templates/CxxMethodStub.rusttpl +12 -0
  174. data/vendor/rust/rust/templates/CxxStandaloneClassDefinitions.rusttpl +26 -0
  175. data/vendor/rust/rust/templates/EnumDeclarations.rusttpl +3 -0
  176. data/vendor/rust/rust/templates/EnumDefinitions.rusttpl +29 -0
  177. data/vendor/rust/rust/templates/FunctionDefinition.rusttpl +9 -0
  178. data/vendor/rust/rust/templates/FunctionInitAlias.rusttpl +5 -0
  179. data/vendor/rust/rust/templates/FunctionInitBinding.rusttpl +9 -0
  180. data/vendor/rust/rust/templates/MethodInitBinding.rusttpl +9 -0
  181. data/vendor/rust/rust/templates/ModuleDeclarations.rusttpl +3 -0
  182. data/vendor/rust/rust/templates/ModuleDefinitions.rusttpl +3 -0
  183. data/vendor/rust/rust/templates/StandaloneClassDeclarations.rusttpl +7 -0
  184. data/vendor/rust/rust/templates/VariableFunctionCall.rusttpl +14 -0
  185. data/vendor/rust/rust/type.rb +98 -0
  186. data/vendor/rust/test/Makefile +4 -0
  187. data/vendor/rust/test/constants.rb +36 -0
  188. data/vendor/rust/test/cppclass.cc +45 -0
  189. data/vendor/rust/test/cppclass.hh +67 -0
  190. data/vendor/rust/test/cppclass.rb +59 -0
  191. data/vendor/rust/test/cwrapper.c +74 -0
  192. data/vendor/rust/test/cwrapper.h +41 -0
  193. data/vendor/rust/test/cwrapper.rb +56 -0
  194. data/vendor/rust/test/dummyclass.hh +31 -0
  195. data/vendor/rust/test/lib/extension-test.rb +98 -0
  196. data/vendor/rust/test/operators.cc +41 -0
  197. data/vendor/rust/test/operators.hh +39 -0
  198. data/vendor/rust/test/operators.rb +39 -0
  199. data/vendor/rust/test/test-constants.rb +43 -0
  200. data/vendor/rust/test/test-cppclass.rb +82 -0
  201. data/vendor/rust/test/test-cwrapper.rb +80 -0
  202. data/vendor/rust/test/test-operators.rb +42 -0
  203. metadata +393 -0
@@ -0,0 +1,68 @@
1
+ module Gecode::BoolEnum
2
+ class BoolEnumConstraintReceiver
3
+ # Constrains this enumeration to "channel" +integer_operand+. This
4
+ # constrains the integer operand to take value i exactly when the
5
+ # operand at index i in the boolean enumeration is true and the others
6
+ # are false.
7
+ #
8
+ # Beyond the common options the channel constraint can
9
+ # also take the following option:
10
+ #
11
+ # [:offset] Specifies an offset for the integer operand. If the offset is
12
+ # set to k then the integer operand takes value i+k exactly
13
+ # when the operand at index i in the boolean enumeration is
14
+ # true and the rest are false.
15
+ #
16
+ # Neither reification nor negation is supported. The int operand
17
+ # and the enumeration can be interchanged.
18
+ #
19
+ # ==== Examples
20
+ #
21
+ # # Constrains the enumeration called +option_is_selected+ to be false
22
+ # # in the first four positions and have exactly one true operand in
23
+ # # the other.
24
+ # option_is_selected.must.channel selected_option_index
25
+ # selected_option_index.must_be > 3
26
+ #
27
+ # # Constrains the enumeration called +option_is_selected+ to be false
28
+ # # in the first five positions and have exactly one true operand in
29
+ # # the other.
30
+ # selected_option_index.must.channel(option_is_selected, :offset => 1)
31
+ # selected_option_index.must_be > 3
32
+ def channel(integer_operand, options = {})
33
+ if @params[:negate]
34
+ raise Gecode::MissingConstraintError, 'A negated channel constraint ' +
35
+ 'is not implemented.'
36
+ end
37
+ if options.has_key? :reify
38
+ raise ArgumentError, 'The channel constraint does not support the ' +
39
+ 'reification option.'
40
+ end
41
+ unless integer_operand.respond_to? :to_int_var
42
+ raise TypeError, 'Expected an integer operand, got ' +
43
+ "#{integer_operand.class}."
44
+ end
45
+
46
+ @params.update(:rhs => integer_operand,
47
+ :offset => options.delete(:offset) || 0)
48
+ @params.update(Gecode::Util.decode_options(options))
49
+ @model.add_constraint Channel::ChannelConstraint.new(@model, @params)
50
+ end
51
+
52
+ # Provides commutativity with SetConstraintReceiver#channel
53
+ provide_commutativity(:channel){ |rhs, _| rhs.respond_to? :to_set_var }
54
+ end
55
+
56
+ # A module that gathers the classes and modules used in channel constraints
57
+ # involving one boolean enum and one integer operand.
58
+ module Channel #:nodoc:
59
+ class ChannelConstraint < Gecode::Constraint #:nodoc:
60
+ def post
61
+ lhs, rhs, offset = @params.values_at(:lhs, :rhs, :offset)
62
+ Gecode::Raw::channel(@model.active_space,
63
+ lhs.to_bool_enum.bind_array, rhs.to_int_var.bind,
64
+ offset, *propagation_options)
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,106 @@
1
+ module Gecode::BoolEnum
2
+ class BoolEnumConstraintReceiver
3
+ # Constrains all the operands in this enumeration to be equal to
4
+ # one of the specified tuples. Neither negation nor reification is
5
+ # supported.
6
+ #
7
+ # ==== Examples
8
+ #
9
+ # # Constrains the three boolean operands in +bools+ to either
10
+ # # be true, false, true, or false, false, true.
11
+ # bools.must_be.in [[true, false, true], [false, false, true]]
12
+ #
13
+ # # The same as above, but preferring speed over low memory usage.
14
+ # bools.must_be.in([[true, false, true], [false, false, true]],
15
+ # :kind => :speed)
16
+ def in(tuples, options = {})
17
+ if @params[:negate]
18
+ raise Gecode::MissingConstraintError, 'A negated tuple constraint is ' +
19
+ 'not implemented.'
20
+ end
21
+ unless options[:reify].nil?
22
+ raise ArgumentError, 'Reification is not supported by the tuple ' +
23
+ 'constraint.'
24
+ end
25
+
26
+ util = Gecode::Util
27
+
28
+ # Check that the tuples are correct.
29
+ expected_size = @params[:lhs].size
30
+ util::Extensional.perform_tuple_checks(tuples, expected_size) do |tuple|
31
+ unless tuple.all?{ |x| x.kind_of?(TrueClass) or x.kind_of?(FalseClass) }
32
+ raise TypeError, 'All tuples must contain booleans.'
33
+ end
34
+ end
35
+
36
+ @params[:tuples] = tuples
37
+ @model.add_constraint Extensional::TupleConstraint.new(@model,
38
+ @params.update(Gecode::Util.decode_options(options)))
39
+ end
40
+
41
+ # Constrains the sequence of operands in this enumeration to match
42
+ # a specified regexp in the boolean domain. Neither negation nor
43
+ # reification is supported.
44
+ #
45
+ # The regular expressions are specified as described in
46
+ # IntEnumConstraintReceiver#match but true and false can be
47
+ # used instead of integers.
48
+ #
49
+ # ==== Examples
50
+ #
51
+ # # Constrains the two boolean operands in +bools+ to be false
52
+ # # and true respectively.
53
+ # bools.must.match [false, true]
54
+ #
55
+ # # Constrains the boolean operands in +bools+ to be false,
56
+ # # except for three consecutive operands which should be true
57
+ # # followed by false followed by true.
58
+ # bools.must.match [repeat(false), true, false, true, repeat(false)]]
59
+ #
60
+ def match(regexp, options = {})
61
+ if @params[:negate]
62
+ raise Gecode::MissingConstraintError, 'A negated regexp constraint ' +
63
+ 'is not implemented.'
64
+ end
65
+ unless options[:reify].nil?
66
+ raise ArgumentError, 'Reification is not supported by the regexp ' +
67
+ 'constraint.'
68
+ end
69
+
70
+ @params[:regexp] =
71
+ Gecode::Util::Extensional.parse_regexp regexp
72
+ @params.update Gecode::Util.decode_options(options)
73
+ @model.add_constraint Extensional::RegexpConstraint.new(@model, @params)
74
+ end
75
+ end
76
+
77
+ # A module that gathers the classes and modules used in extensional
78
+ # constraints.
79
+ module Extensional #:nodoc:
80
+ class TupleConstraint < Gecode::Constraint #:nodoc:
81
+ def post
82
+ # Bind lhs.
83
+ lhs = @params[:lhs].to_bool_enum.bind_array
84
+
85
+ # Create the tuple set.
86
+ tuple_set = Gecode::Raw::TupleSet.new
87
+ @params[:tuples].each do |tuple|
88
+ tuple_set.add tuple.map{ |b| b ? 1 : 0 }
89
+ end
90
+ tuple_set.finalize
91
+
92
+ # Post the constraint.
93
+ Gecode::Raw::extensional(@model.active_space, lhs, tuple_set,
94
+ *propagation_options)
95
+ end
96
+ end
97
+
98
+ class RegexpConstraint < Gecode::Constraint #:nodoc:
99
+ def post
100
+ lhs, regexp = @params.values_at(:lhs, :regexp)
101
+ Gecode::Raw::extensional(@model.active_space,
102
+ lhs.to_bool_enum.bind_array, regexp, *propagation_options)
103
+ end
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,55 @@
1
+ module Gecode::BoolEnum
2
+ module BoolEnumOperand
3
+ # Produces a BoolOperand that represents the conjunction (AND) of all
4
+ # boolean operands in this enumeration.
5
+ #
6
+ # ==== Examples
7
+ #
8
+ # # Conjunction of all elements in +bool_enum+.
9
+ # bool_enum.conjunction
10
+ def conjunction
11
+ Relation::BoolEnumConjunctionOperand.new(@model, self)
12
+ end
13
+
14
+ # Produces a BoolOperand that represents the disjunction (OR) of all
15
+ # boolean operands in this enumeration.
16
+ #
17
+ # ==== Examples
18
+ #
19
+ # # Disjunction of all elements in +bool_enum+.
20
+ # bool_enum.disjunction
21
+ def disjunction
22
+ Relation::BoolEnumDisjunctionOperand.new(@model, self)
23
+ end
24
+ end
25
+
26
+ # A module that gathers the classes and modules used by boolean enumeration
27
+ # relation constraints.
28
+ module Relation #:nodoc:
29
+ class BoolEnumConjunctionOperand < Gecode::Bool::ShortCircuitEqualityOperand #:nodoc:
30
+ def initialize(model, bool_enum)
31
+ super model
32
+ @enum = bool_enum
33
+ end
34
+
35
+ def constrain_equal(bool_operand, constrain_domain, propagation_options)
36
+ Gecode::Raw::rel(@model.active_space, Gecode::Raw::BOT_AND,
37
+ @enum.to_bool_enum.bind_array, bool_operand.to_bool_var.bind,
38
+ *propagation_options)
39
+ end
40
+ end
41
+
42
+ class BoolEnumDisjunctionOperand < Gecode::Bool::ShortCircuitEqualityOperand #:nodoc:
43
+ def initialize(model, bool_enum)
44
+ super model
45
+ @enum = bool_enum
46
+ end
47
+
48
+ def constrain_equal(bool_operand, constrain_domain, propagation_options)
49
+ Gecode::Raw::rel(@model.active_space, Gecode::Raw::BOT_OR,
50
+ @enum.to_bool_enum.bind_array, bool_operand.to_bool_var.bind,
51
+ *propagation_options)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,84 @@
1
+ # A module containing constraints that have enumerations of boolean
2
+ # operands as left hand side.
3
+ module Gecode::BoolEnum #:nodoc:
4
+ # A BoolEnumOperand is a enumeration of BoolOperand on which the
5
+ # constraints defined in BoolEnumConstraintReceiver can be placed.
6
+ #
7
+ # Enumerations of boolean operands can be created either by using
8
+ # Gecode::Model#bool_var_array and Gecode::Model#bool_var_matrix, or
9
+ # by wrapping an existing enumeration containing BoolOperand using
10
+ # Gecode::Model#wrap_enum. The enumerations, no matter how they were
11
+ # created, all respond to the properties defined by BoolEnumOperand.
12
+ #
13
+ # ==== Examples
14
+ #
15
+ # Produces an array of five boolean operands inside a problem formulation
16
+ # using Gecode::Model#bool_var_array:
17
+ #
18
+ # bool_enum = bool_var_array(5)
19
+ #
20
+ # Uses Gecode::Model#wrap_enum inside a problem formulation to create
21
+ # a BoolEnumOperand from an existing enumeration containing the
22
+ # boolean operands +bool_operand1+ and +bool_operand2+:
23
+ #
24
+ # bool_enum = wrap_enum([bool_operand1, bool_operand2])
25
+ #
26
+ #--
27
+ # Classes that mix in BoolEnumOperand must define #model and
28
+ # #to_bool_enum .
29
+ module BoolEnumOperand
30
+ include Gecode::Operand
31
+
32
+ def method_missing(method, *args) #:nodoc:
33
+ if Gecode::BoolEnum::Dummy.instance_methods.include? method.to_s
34
+ # Delegate to the bool enum.
35
+ to_bool_enum.method(method).call(*args)
36
+ else
37
+ super
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def construct_receiver(params)
44
+ BoolEnumConstraintReceiver.new(@model, params)
45
+ end
46
+ end
47
+
48
+ # BoolEnumConstraintReceiver contains all constraints that can be
49
+ # placed on a BoolEnumOperand.
50
+ #
51
+ # Constraints are placed by calling BoolEnumOperand#must (or any other
52
+ # of the variations defined in Operand), which produces a
53
+ # BoolEnumConstraintReceiver from which the desired constraint can be
54
+ # used.
55
+ #
56
+ # ==== Examples
57
+ #
58
+ # Constrains +bool_enum+, with three boolean operands, to take the
59
+ # value of the tuples [false, true, false] or [true, false, true]
60
+ # using BoolEnumConstraintReceiver#in:
61
+ #
62
+ # bool_enum.must_be.in [[false, true, false], [true, false, true]]
63
+ #
64
+ # Constrains +bool_enum+ to channel +int_operand+ using
65
+ # BoolEnumConstraintReceiver#channel:
66
+ #
67
+ # bool_enum.must.channel int_operand
68
+ #
69
+ class BoolEnumConstraintReceiver < Gecode::ConstraintReceiver
70
+ # Raises TypeError unless the left hand side is an bool enum
71
+ # operand.
72
+ def initialize(model, params) #:nodoc:
73
+ super
74
+
75
+ unless params[:lhs].respond_to? :to_bool_enum
76
+ raise TypeError, 'Must have bool enum operand as left hand side.'
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ require 'gecoder/interface/constraints/bool_enum/relation'
83
+ require 'gecoder/interface/constraints/bool_enum/extensional'
84
+ require 'gecoder/interface/constraints/bool_enum/channel'
@@ -0,0 +1,155 @@
1
+ # A module that deals with the operands, properties and constraints of
2
+ # boolean operands.
3
+ module Gecode::Bool #:nodoc:
4
+ # A BoolOperand is a combination of variables on which the
5
+ # constraints defined in BoolConstraintReceiver can be placed.
6
+ #
7
+ # Boolean operands can be created either by using
8
+ # Gecode::Model#bool_var et al, or by using properties that produce
9
+ # boolean operands. The operands, no matter how they were created,
10
+ # all respond to the properties defined by BoolOperand.
11
+ #
12
+ # ==== Examples
13
+ #
14
+ # Produces a single boolean operand (more specifically a BoolVar)
15
+ # inside a problem formulation, using Gecode::Model#bool_var:
16
+ #
17
+ # bool_operand = bool_var
18
+ #
19
+ # Uses the BoolOperand#& property to produce a new boolean
20
+ # operand representing +bool_operand1+ AND +bool_operand2+:
21
+ #
22
+ # new_bool_operand = bool_operand1 & bool_operand2
23
+ #
24
+ # Uses the BoolEnumOperand#conjunction property to produce a new
25
+ # boolean operand representing the conjunction of all boolean operands
26
+ # in the enumeration +bool_enum+:
27
+ #
28
+ # new_bool_operand = bool_enum.conjunction
29
+ #
30
+ #--
31
+ # Classes that mix in BoolOperand must define #model and #to_bool_var .
32
+ module BoolOperand
33
+ include Gecode::Operand
34
+
35
+ def method_missing(method, *args) #:nodoc:
36
+ if Gecode::BoolVar.instance_methods.include? method.to_s
37
+ # Delegate to the bool var.
38
+ to_bool_var.method(method).call(*args)
39
+ else
40
+ super
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def construct_receiver(params)
47
+ BoolConstraintReceiver.new(@model, params)
48
+ end
49
+ end
50
+
51
+ # BoolConstraintReceiver contains all constraints that can be
52
+ # placed on a BoolOperand.
53
+ #
54
+ # Constraints are placed by calling BoolOperand#must (or any other
55
+ # of the variations defined in Operand), which produces a
56
+ # BoolConstraintReceiver from which the desired constraint can be used.
57
+ #
58
+ # Each constraint accepts a number of options. See ConstraintReceiver
59
+ # for more information.
60
+ #
61
+ # ==== Examples
62
+ #
63
+ # Constrains +bool_operand+ to be true using
64
+ # BoolConstraintReceiver#true:
65
+ #
66
+ # bool_operand.must_be.true
67
+ #
68
+ # Constrains +bool_operand1+ AND +bool_operand2+ to be true using
69
+ # the BoolOperand#& property and BoolConstraintReceiver#true:
70
+ #
71
+ # (bool_operand1 & bool_operand2).must_be.true
72
+ #
73
+ # Constrains the conjunction of all boolean operands in +bool_enum+ to
74
+ # _not_ imply +bool_operand+ using the
75
+ # BoolEnumOperand#conjunction property and BoolConstraintReceiver#imply:
76
+ #
77
+ # bool_enum.conjunction.must_not.imply bool_operand
78
+ #
79
+ # The same as above, but specifying that strength :domain should be
80
+ # used and that the constraint should be reified with +bool_operand2+:
81
+ #
82
+ # bool_enum.conjunction.must_not.imply(bool_operand, :strength => :domain, :reify => bool_operand2)
83
+ #
84
+ class BoolConstraintReceiver < Gecode::ConstraintReceiver
85
+ # Raises TypeError unless the left hand side is an bool operand.
86
+ def initialize(model, params) #:nodoc:
87
+ super
88
+
89
+ unless params[:lhs].respond_to? :to_bool_var
90
+ raise TypeError, 'Must have bool operand as left hand side.'
91
+ end
92
+ end
93
+ end
94
+
95
+ # An operand that short circuits boolean equality.
96
+ class ShortCircuitEqualityOperand #:nodoc:
97
+ include Gecode::Bool::BoolOperand
98
+ attr :model
99
+
100
+ def initialize(model)
101
+ @model = model
102
+ end
103
+
104
+ def construct_receiver(params)
105
+ params.update(:lhs => self)
106
+ receiver = BoolConstraintReceiver.new(@model, params)
107
+ op = self
108
+ receiver.instance_eval{ @short_circuit = op }
109
+ class <<receiver
110
+ alias_method :equality_without_short_circuit, :==
111
+ def ==(operand, options = {})
112
+ if !@params[:negate] and options[:reify].nil? and
113
+ operand.respond_to? :to_bool_var
114
+ # Short circuit the constraint.
115
+ @params.update Gecode::Util.decode_options(options)
116
+ @model.add_constraint(Gecode::BlockConstraint.new(
117
+ @model, @params) do
118
+ @short_circuit.constrain_equal(operand, false,
119
+ @params.values_at(:strength, :kind))
120
+ end)
121
+ else
122
+ equality_without_short_circuit(operand, options)
123
+ end
124
+ end
125
+ alias_comparison_methods
126
+ end
127
+
128
+ return receiver
129
+ end
130
+
131
+ def to_bool_var
132
+ variable = model.bool_var
133
+ options =
134
+ Gecode::Util.decode_options({}).values_at(:strength, :kind)
135
+ model.add_interaction do
136
+ constrain_equal(variable, true, options)
137
+ end
138
+ return variable
139
+ end
140
+
141
+ private
142
+
143
+ # Constrains this operand to equal +bool_operand+ using the
144
+ # specified +propagation_options+. If +constrain_domain+ is true
145
+ # then the method should also attempt to constrain the bounds of the
146
+ # domain of +bool_operand+.
147
+ def constrain_equal(bool_operand, constrain_domain, propagation_options)
148
+ raise NotImplementedError, 'Abstract method has not been implemented.'
149
+ end
150
+ end
151
+ end
152
+
153
+ require 'gecoder/interface/constraints/bool/boolean'
154
+ require 'gecoder/interface/constraints/bool/linear'
155
+ require 'gecoder/interface/constraints/bool/channel'