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,251 @@
1
+ module Gecode::Bool
2
+ module BoolOperand
3
+ # Produces a new BoolOperand representing this operand OR +bool_op+.
4
+ #
5
+ # ==== Examples
6
+ #
7
+ # # +b1+ and +b2+
8
+ # b1 & b2
9
+ def |(bool_op)
10
+ bool_expression_operation(:|, bool_op)
11
+ end
12
+
13
+ # Produces a new BoolOperand representing this operand AND +bool_op+.
14
+ #
15
+ # ==== Examples
16
+ #
17
+ # # (+b1+ and +b2+) or +b3+
18
+ # (b1 & b1) | b3
19
+ def &(bool_op)
20
+ bool_expression_operation(:&, bool_op)
21
+ end
22
+
23
+ # Produces a new BoolOperand representing this operand XOR +bool_op+.
24
+ #
25
+ # ==== Examples
26
+ #
27
+ # # (+b1+ and +b2+) or (+b3+ exclusive or +b1+)
28
+ # (b1 & b2) | (b3 ^ b1)
29
+ def ^(bool_op)
30
+ bool_expression_operation(:^, bool_op)
31
+ end
32
+
33
+ # Produces a new BoolOperand representing that this operand implies
34
+ # +bool_op+.
35
+ #
36
+ # ==== Examples
37
+ #
38
+ # # (+b1+ implies +b2+) and (+b3+ implies +b2+)
39
+ # (b1.implies b2) & (b3.implies b2)
40
+ def implies(bool_op)
41
+ bool_expression_operation(:implies, bool_op)
42
+ end
43
+
44
+ private
45
+
46
+ # Performs the bool expression operation +operator+ on self
47
+ # and +operand2+.
48
+ def bool_expression_operation(operator, operand2)
49
+ unless operand2.respond_to? :to_minimodel_bool_expr
50
+ operand2 = ExpressionNode.new operand2
51
+ end
52
+ operand1 = self
53
+ unless operand1.respond_to? :to_minimodel_bool_expr
54
+ operand1 = ExpressionNode.new(self, @model)
55
+ end
56
+ ExpressionTree.new(operand1, operator, operand2)
57
+ end
58
+ end
59
+
60
+ class BoolConstraintReceiver
61
+ # Constrains the boolean operand to be equal to +bool_op+. Any of
62
+ # <tt>==</tt>, +equal+ and +equal_to+ may be used for equality.
63
+ #
64
+ # ==== Examples
65
+ #
66
+ # # +b1+ and +b2+ must equal +b1+ or +b2+.
67
+ # (b1 & b2).must == (b1 | b2)
68
+ #
69
+ # # +b1+ and +b2+ must not equal +b3+. We reify it with +bool+ and select
70
+ # # the strength +domain+.
71
+ # (b1 & b2).must_not.equal(b3, :reify => bool, :select => :domain)
72
+ def ==(bool_op, options = {})
73
+ unless bool_op.respond_to? :to_minimodel_bool_expr
74
+ bool_op = ExpressionNode.new(bool_op, @model)
75
+ end
76
+ @params.update Gecode::Util.decode_options(options)
77
+ @params.update(:lhs => @params[:lhs], :rhs => bool_op)
78
+ @model.add_constraint BooleanConstraint.new(@model, @params)
79
+ end
80
+ alias_comparison_methods
81
+
82
+ # Constrains the boolean operand to imply +bool_op+.
83
+ #
84
+ # ==== Examples
85
+ #
86
+ # # +b1+ must imply +b2+
87
+ # b1.must.imply b2
88
+ #
89
+ # # +b1+ and +b2+ must not imply +b3+. We reify it with +bool+ and select
90
+ # # +domain+ as strength.
91
+ # (b1 & b2).must_not.imply(b3, :reify => bool, :strength => :domain)
92
+ def imply(bool_op, options = {})
93
+ @params.update Gecode::Util.decode_options(options)
94
+ @params.update(:lhs => @params[:lhs].implies(bool_op), :rhs => true)
95
+ @model.add_constraint BooleanConstraint.new(@model, @params)
96
+ end
97
+
98
+ # Constrains the boolean operand to be true.
99
+ #
100
+ # ==== Examples
101
+ #
102
+ # # +b1+ and +b2+ must be true.
103
+ # (b1 & b2).must_be.true
104
+ #
105
+ # # (+b1+ implies +b2+) and (+b3+ implies +b2+) must be true.
106
+ # ((b1.implies b2) & (b3.implies b2)).must_be.true
107
+ #
108
+ # # +b1+ and +b2+ must be true. We reify it with +bool+ and select the
109
+ # # strength +domain+.
110
+ # (b1 & b2).must_be.true(:reify => bool, :strength => :domain)
111
+ def true(options = {})
112
+ @params.update Gecode::Util.decode_options(options)
113
+ @model.add_constraint BooleanConstraint.new(@model,
114
+ @params.update(:rhs => true))
115
+ end
116
+
117
+ # Constrains the boolean operand to be false.
118
+ #
119
+ # ==== Examples
120
+ #
121
+ # # +b1+ and +b2+ must be false.
122
+ # (b1 & b2).must_be.false
123
+ #
124
+ # # (+b1+ implies +b2+) and (+b3+ implies +b2+) must be false.
125
+ # ((b1.implies b2) & (b3.implies b2)).must_be.false
126
+ #
127
+ # # +b1+ and +b2+ must be false. We reify it with +bool+ and select the
128
+ # # strength +domain+.
129
+ # (b1 & b2).must_be.false(:reify => bool, :strength => :domain)
130
+ def false(options = {})
131
+ @params[:negate] = !@params[:negate]
132
+ self.true(options)
133
+ end
134
+ end
135
+
136
+ class BooleanConstraint < Gecode::ReifiableConstraint #:nodoc:
137
+ def post
138
+ lhs, rhs, negate, reif_var =
139
+ @params.values_at(:lhs, :rhs, :negate, :reif)
140
+
141
+ if lhs.respond_to? :to_bool_var
142
+ lhs = ExpressionNode.new(lhs, @model)
143
+ end
144
+ space = (lhs.model || rhs.model).active_space
145
+
146
+ bot_eqv = Gecode::Raw::IRT_EQ
147
+ bot_xor = Gecode::Raw::IRT_NQ
148
+
149
+ if rhs.respond_to? :to_minimodel_bool_expr
150
+ if reif_var.nil?
151
+ tree = ExpressionTree.new(lhs, :==, rhs)
152
+ tree.to_minimodel_bool_expr.post(space, !negate,
153
+ *propagation_options)
154
+ else
155
+ tree = ExpressionTree.new(lhs, :==, rhs)
156
+ var = tree.to_minimodel_bool_expr.post(space, *propagation_options)
157
+ Gecode::Raw::rel(space, var, (negate ? bot_xor : bot_eqv),
158
+ reif_var.to_bool_var.bind, *propagation_options)
159
+ end
160
+ else
161
+ should_hold = !negate & rhs
162
+ if reif_var.nil?
163
+ lhs.to_minimodel_bool_expr.post(space, should_hold,
164
+ *propagation_options)
165
+ else
166
+ var = lhs.to_minimodel_bool_expr.post(space, *propagation_options)
167
+ Gecode::Raw::rel(space, var,
168
+ (should_hold ? bot_eqv : bot_xor),
169
+ reif_var.to_bool_var.bind, *propagation_options)
170
+ end
171
+ end
172
+ end
173
+ end
174
+
175
+ # Describes a binary tree of expression nodes which together form a boolean
176
+ # expression.
177
+ class ExpressionTree #:nodoc:
178
+ include BoolOperand
179
+
180
+ private
181
+
182
+ # Maps the names of the methods to the corresponding bool constraint in
183
+ # Gecode.
184
+ OPERATION_TYPES = {
185
+ :| => Gecode::Raw::MiniModel::BoolExpr::NT_OR,
186
+ :& => Gecode::Raw::MiniModel::BoolExpr::NT_AND,
187
+ :^ => Gecode::Raw::MiniModel::BoolExpr::NT_XOR,
188
+ :implies => Gecode::Raw::MiniModel::BoolExpr::NT_IMP,
189
+ :== => Gecode::Raw::MiniModel::BoolExpr::NT_EQV
190
+ }
191
+
192
+ public
193
+
194
+ # Constructs a new expression with the specified binary operation
195
+ # applied to the specified trees.
196
+ def initialize(left_tree, operation, right_tree)
197
+ @left = left_tree
198
+ @operation = operation
199
+ @right = right_tree
200
+ end
201
+
202
+ # Returns a MiniModel boolean expression representing the tree.
203
+ def to_minimodel_bool_expr
204
+ Gecode::Raw::MiniModel::BoolExpr.new(
205
+ @left.to_minimodel_bool_expr, OPERATION_TYPES[@operation],
206
+ @right.to_minimodel_bool_expr)
207
+ end
208
+
209
+ # Fetches the space that the expression's variables is in.
210
+ def model
211
+ @left.model || @right.model
212
+ end
213
+
214
+ def to_bool_var
215
+ bool_var = model.bool_var
216
+ tree = ExpressionTree.new(self, :==, ExpressionNode.new(bool_var))
217
+ model.add_interaction do
218
+ tree.to_minimodel_bool_expr.post(model.active_space, true,
219
+ Gecode::Raw::ICL_DEF, Gecode::Raw::PK_DEF)
220
+ end
221
+ return bool_var
222
+ end
223
+
224
+ private
225
+
226
+ # Produces a receiver (for the BoolOperand module).
227
+ def construct_receiver(params)
228
+ params.update(:lhs => self)
229
+ BoolConstraintReceiver.new(model, params)
230
+ end
231
+ end
232
+
233
+ # Describes a single node in a boolean expression.
234
+ class ExpressionNode #:nodoc:
235
+ attr :model
236
+
237
+ def initialize(value, model = nil)
238
+ unless value.respond_to? :to_bool_var
239
+ raise TypeError, 'Invalid type used in boolean equation: ' +
240
+ "#{value.class}."
241
+ end
242
+ @value = value
243
+ @model = model
244
+ end
245
+
246
+ # Returns a MiniModel boolean expression representing the tree.
247
+ def to_minimodel_bool_expr
248
+ Gecode::Raw::MiniModel::BoolExpr.new(@value.to_bool_var.bind)
249
+ end
250
+ end
251
+ end
@@ -0,0 +1,7 @@
1
+ module Gecode::Bool
2
+ class BoolConstraintReceiver
3
+ # Provides commutivity with IntVarReceiver#==
4
+ provide_commutativity(:==){ |rhs, _| rhs.respond_to? :to_int_var }
5
+ alias_comparison_methods
6
+ end
7
+ end
@@ -0,0 +1,200 @@
1
+ module Gecode::Bool
2
+ module BoolLinearOperations #:nodoc:
3
+ # Produces an IntOperand representing the value of this boolean
4
+ # operand (0 or 1) plus +op2+.
5
+ #
6
+ # ==== Examples
7
+ #
8
+ # # +bool1+ plus +bool2+
9
+ # bool1 + bool2
10
+ def +(op2)
11
+ bool_linear_expression_operation(:+, op2)
12
+ end
13
+
14
+ # Produces an IntOperand representing the value of this boolean
15
+ # operand (0 or 1) times a constant.
16
+ #
17
+ # ==== Examples
18
+ #
19
+ # # +bool+ times 17
20
+ # bool * 17
21
+ def *(fixnum)
22
+ if fixnum.kind_of? Fixnum
23
+ bool_linear_expression_operation(:*, fixnum)
24
+ else
25
+ raise TypeError, "Expected fixnum, got #{fixnum.class}."
26
+ end
27
+ end
28
+
29
+ # Produces an IntOperand representing the value of this boolean
30
+ # operand (0 or 1) minus +op2+.
31
+ #
32
+ # ==== Examples
33
+ #
34
+ # # +bool1+ minus +bool2+
35
+ # bool1 - bool2
36
+ def -(op2)
37
+ bool_linear_expression_operation(:-, op2)
38
+ end
39
+
40
+ private
41
+
42
+ # Performs the bool linear expression operation +operator+ on self
43
+ # and +operand2+.
44
+ def bool_linear_expression_operation(operator, operand2)
45
+ linear_module = Linear
46
+ unless operand2.respond_to? :to_minimodel_lin_exp
47
+ operand2 = linear_module::ExpressionNode.new operand2
48
+ end
49
+ operand1 = self
50
+ unless operand1.respond_to? :to_minimodel_lin_exp
51
+ operand1 = linear_module::ExpressionNode.new(self, @model)
52
+ end
53
+ linear_module::ExpressionTree.new(operand1, operand2, operator)
54
+ end
55
+ end
56
+
57
+ module BoolOperand
58
+ # We include the operations and then redefine them so that they show
59
+ # up in the documentation.
60
+ include BoolLinearOperations
61
+
62
+ # Produces an IntOperand representing the value of this boolean
63
+ # operand (0 or 1) plus +op2+.
64
+ #
65
+ # ==== Examples
66
+ #
67
+ # # +bool1+ plus +bool2+
68
+ # bool1 + bool2
69
+ def +(op2)
70
+ bool_linear_expression_operation(:+, op2)
71
+ end
72
+
73
+ # Produces an IntOperand representing the value of this boolean
74
+ # operand (0 or 1) times a constant.
75
+ #
76
+ # ==== Examples
77
+ #
78
+ # # +bool+ times 17
79
+ # bool * 17
80
+ def *(fixnum)
81
+ if fixnum.kind_of? Fixnum
82
+ bool_linear_expression_operation(:*, fixnum)
83
+ else
84
+ raise TypeError, "Expected fixnum, got #{fixnum.class}."
85
+ end
86
+ end
87
+
88
+ # Produces an IntOperand representing the value of this boolean
89
+ # operand (0 or 1) minus +op2+.
90
+ #
91
+ # ==== Examples
92
+ #
93
+ # # +bool1+ minus +bool2+
94
+ # bool1 - bool2
95
+ def -(op2)
96
+ bool_linear_expression_operation(:-, op2)
97
+ end
98
+ end
99
+
100
+ # A module that gathers the classes and modules used in linear constraints.
101
+ module Linear #:nodoc:
102
+ class LinearRelationConstraint < Gecode::ReifiableConstraint #:nodoc:
103
+ def post
104
+ lhs, rhs, relation_type, reif_var =
105
+ @params.values_at(:lhs, :rhs, :relation_type, :reif)
106
+ reif_var = reif_var.to_bool_var.bind if reif_var.respond_to? :to_bool_var
107
+ final_exp = (lhs.to_minimodel_lin_exp - rhs.to_minimodel_lin_exp)
108
+ if reif_var.nil?
109
+ final_exp.post(@model.active_space, relation_type,
110
+ *propagation_options)
111
+ else
112
+ final_exp.post(@model.active_space, relation_type, reif_var,
113
+ *propagation_options)
114
+ end
115
+ end
116
+ end
117
+
118
+ # Describes a binary tree of expression nodes which together form a linear
119
+ # expression.
120
+ class ExpressionTree < Gecode::Int::ShortCircuitRelationsOperand #:nodoc:
121
+ include Gecode::Bool::BoolLinearOperations
122
+ attr :model
123
+
124
+ # Constructs a new expression with the specified operands.
125
+ def initialize(left_node, right_node, operation)
126
+ super(left_node.model || right_node.model)
127
+ @left = left_node
128
+ @right = right_node
129
+ @operation = operation
130
+ @model = @left.model || @right.model
131
+ end
132
+
133
+ # Converts the linear expression to an instance of
134
+ # Gecode::Raw::MiniModel::LinExpr
135
+ def to_minimodel_lin_exp
136
+ @left.to_minimodel_lin_exp.send(@operation, @right.to_minimodel_lin_exp)
137
+ end
138
+
139
+ alias_method :pre_bool_construct_receiver, :construct_receiver
140
+ def construct_receiver(params)
141
+ receiver = pre_bool_construct_receiver(params)
142
+ lhs = self
143
+ receiver.instance_eval{ @lhs = lhs }
144
+ class <<receiver
145
+ alias_method :pre_bool_equality, :==
146
+ def ==(op, options = {})
147
+ if op.respond_to? :to_bool_var
148
+ (@lhs - op).must.equal(0, options)
149
+ else
150
+ pre_bool_equality(op, options)
151
+ end
152
+ end
153
+ alias_comparison_methods
154
+ end
155
+ return receiver
156
+ end
157
+
158
+ def relation_constraint(relation, bool_operand_or_fix, params)
159
+ unless params[:negate]
160
+ relation_type =
161
+ Gecode::Util::RELATION_TYPES[relation]
162
+ else
163
+ relation_type =
164
+ Gecode::Util::NEGATED_RELATION_TYPES[relation]
165
+ end
166
+
167
+ unless bool_operand_or_fix.respond_to? :to_minimodel_lin_exp
168
+ bool_operand_or_fix = Linear::ExpressionNode.new(bool_operand_or_fix);
169
+ end
170
+
171
+ params.update(:rhs => bool_operand_or_fix, :relation_type => relation_type)
172
+ LinearRelationConstraint.new(model, params)
173
+ end
174
+ end
175
+
176
+ # Describes a single node in a linear expression.
177
+ class ExpressionNode #:nodoc:
178
+ attr :model
179
+
180
+ def initialize(value, model = nil)
181
+ unless value.respond_to?(:to_bool_var) or value.kind_of?(Fixnum)
182
+ raise TypeError, 'Expected bool operand or ' +
183
+ "fixnum, got #{value.class}."
184
+ end
185
+ @value = value
186
+ @model = model
187
+ end
188
+
189
+ # Converts the linear expression to an instance of
190
+ # Gecode::Raw::MiniModel::LinExpr
191
+ def to_minimodel_lin_exp
192
+ expression = @value
193
+ if expression.respond_to? :to_bool_var
194
+ expression = expression.to_bool_var.bind * 1
195
+ end
196
+ expression
197
+ end
198
+ end
199
+ end
200
+ end