gecoder-with-gecode 0.7.1-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (159) hide show
  1. data/CHANGES +81 -0
  2. data/COPYING +17 -0
  3. data/LGPL-LICENSE +458 -0
  4. data/README +45 -0
  5. data/Rakefile +13 -0
  6. data/example/example_helper.rb +1 -0
  7. data/example/magic_sequence.rb +43 -0
  8. data/example/queens.rb +43 -0
  9. data/example/raw_bindings.rb +42 -0
  10. data/example/send_more_money.rb +43 -0
  11. data/example/send_most_money.rb +58 -0
  12. data/example/square_tiling.rb +84 -0
  13. data/example/sudoku-set.rb +110 -0
  14. data/example/sudoku.rb +61 -0
  15. data/lib/gecode.dll +0 -0
  16. data/lib/gecoder.rb +5 -0
  17. data/lib/gecoder/bindings.rb +54 -0
  18. data/lib/gecoder/bindings/bindings.rb +2210 -0
  19. data/lib/gecoder/interface.rb +8 -0
  20. data/lib/gecoder/interface/binding_changes.rb +313 -0
  21. data/lib/gecoder/interface/branch.rb +152 -0
  22. data/lib/gecoder/interface/constraints.rb +397 -0
  23. data/lib/gecoder/interface/constraints/bool/boolean.rb +246 -0
  24. data/lib/gecoder/interface/constraints/bool/linear.rb +29 -0
  25. data/lib/gecoder/interface/constraints/bool_enum/boolean.rb +84 -0
  26. data/lib/gecoder/interface/constraints/bool_enum_constraints.rb +8 -0
  27. data/lib/gecoder/interface/constraints/bool_var_constraints.rb +75 -0
  28. data/lib/gecoder/interface/constraints/int/arithmetic.rb +71 -0
  29. data/lib/gecoder/interface/constraints/int/domain.rb +78 -0
  30. data/lib/gecoder/interface/constraints/int/linear.rb +295 -0
  31. data/lib/gecoder/interface/constraints/int_enum/arithmetic.rb +72 -0
  32. data/lib/gecoder/interface/constraints/int_enum/channel.rb +100 -0
  33. data/lib/gecoder/interface/constraints/int_enum/count.rb +92 -0
  34. data/lib/gecoder/interface/constraints/int_enum/distinct.rb +69 -0
  35. data/lib/gecoder/interface/constraints/int_enum/element.rb +82 -0
  36. data/lib/gecoder/interface/constraints/int_enum/equality.rb +38 -0
  37. data/lib/gecoder/interface/constraints/int_enum/sort.rb +126 -0
  38. data/lib/gecoder/interface/constraints/int_enum_constraints.rb +37 -0
  39. data/lib/gecoder/interface/constraints/int_var_constraints.rb +58 -0
  40. data/lib/gecoder/interface/constraints/reifiable_constraints.rb +78 -0
  41. data/lib/gecoder/interface/constraints/set/cardinality.rb +75 -0
  42. data/lib/gecoder/interface/constraints/set/connection.rb +193 -0
  43. data/lib/gecoder/interface/constraints/set/domain.rb +109 -0
  44. data/lib/gecoder/interface/constraints/set/operation.rb +132 -0
  45. data/lib/gecoder/interface/constraints/set/relation.rb +178 -0
  46. data/lib/gecoder/interface/constraints/set_enum/channel.rb +18 -0
  47. data/lib/gecoder/interface/constraints/set_enum/distinct.rb +80 -0
  48. data/lib/gecoder/interface/constraints/set_enum/operation.rb +60 -0
  49. data/lib/gecoder/interface/constraints/set_enum/selection.rb +217 -0
  50. data/lib/gecoder/interface/constraints/set_enum_constraints.rb +34 -0
  51. data/lib/gecoder/interface/constraints/set_var_constraints.rb +72 -0
  52. data/lib/gecoder/interface/enum_matrix.rb +64 -0
  53. data/lib/gecoder/interface/enum_wrapper.rb +153 -0
  54. data/lib/gecoder/interface/model.rb +251 -0
  55. data/lib/gecoder/interface/search.rb +123 -0
  56. data/lib/gecoder/interface/variables.rb +254 -0
  57. data/lib/gecoder/version.rb +4 -0
  58. data/specs/binding_changes.rb +76 -0
  59. data/specs/bool_var.rb +74 -0
  60. data/specs/branch.rb +170 -0
  61. data/specs/constraints/arithmetic.rb +266 -0
  62. data/specs/constraints/bool_enum.rb +140 -0
  63. data/specs/constraints/boolean.rb +232 -0
  64. data/specs/constraints/cardinality.rb +154 -0
  65. data/specs/constraints/channel.rb +126 -0
  66. data/specs/constraints/connection.rb +373 -0
  67. data/specs/constraints/constraint_helper.rb +180 -0
  68. data/specs/constraints/constraints.rb +74 -0
  69. data/specs/constraints/count.rb +139 -0
  70. data/specs/constraints/distinct.rb +218 -0
  71. data/specs/constraints/element.rb +106 -0
  72. data/specs/constraints/equality.rb +31 -0
  73. data/specs/constraints/int_domain.rb +69 -0
  74. data/specs/constraints/int_relation.rb +78 -0
  75. data/specs/constraints/linear.rb +332 -0
  76. data/specs/constraints/reification_sugar.rb +96 -0
  77. data/specs/constraints/selection.rb +292 -0
  78. data/specs/constraints/set_domain.rb +181 -0
  79. data/specs/constraints/set_operation.rb +285 -0
  80. data/specs/constraints/set_relation.rb +201 -0
  81. data/specs/constraints/sort.rb +175 -0
  82. data/specs/distribution.rb +14 -0
  83. data/specs/enum_matrix.rb +43 -0
  84. data/specs/enum_wrapper.rb +122 -0
  85. data/specs/int_var.rb +144 -0
  86. data/specs/logging.rb +24 -0
  87. data/specs/model.rb +190 -0
  88. data/specs/search.rb +246 -0
  89. data/specs/set_var.rb +68 -0
  90. data/specs/spec_helper.rb +93 -0
  91. data/tasks/all_tasks.rb +1 -0
  92. data/tasks/building.howto +65 -0
  93. data/tasks/distribution.rake +156 -0
  94. data/tasks/rcov.rake +17 -0
  95. data/tasks/specs.rake +15 -0
  96. data/tasks/svn.rake +11 -0
  97. data/tasks/website.rake +51 -0
  98. data/vendor/gecode/win32/lib/libgecodeint.dll +0 -0
  99. data/vendor/gecode/win32/lib/libgecodekernel.dll +0 -0
  100. data/vendor/gecode/win32/lib/libgecodeminimodel.dll +0 -0
  101. data/vendor/gecode/win32/lib/libgecodesearch.dll +0 -0
  102. data/vendor/gecode/win32/lib/libgecodeset.dll +0 -0
  103. data/vendor/rust/README +28 -0
  104. data/vendor/rust/bin/cxxgenerator.rb +93 -0
  105. data/vendor/rust/include/rust_checks.hh +115 -0
  106. data/vendor/rust/include/rust_conversions.hh +102 -0
  107. data/vendor/rust/rust.rb +67 -0
  108. data/vendor/rust/rust/attribute.rb +51 -0
  109. data/vendor/rust/rust/bindings.rb +172 -0
  110. data/vendor/rust/rust/class.rb +339 -0
  111. data/vendor/rust/rust/constants.rb +48 -0
  112. data/vendor/rust/rust/container.rb +110 -0
  113. data/vendor/rust/rust/cppifaceparser.rb +129 -0
  114. data/vendor/rust/rust/cwrapper.rb +72 -0
  115. data/vendor/rust/rust/cxxclass.rb +98 -0
  116. data/vendor/rust/rust/element.rb +81 -0
  117. data/vendor/rust/rust/enum.rb +63 -0
  118. data/vendor/rust/rust/function.rb +407 -0
  119. data/vendor/rust/rust/namespace.rb +61 -0
  120. data/vendor/rust/rust/templates/AttributeDefinition.rusttpl +17 -0
  121. data/vendor/rust/rust/templates/AttributeInitBinding.rusttpl +9 -0
  122. data/vendor/rust/rust/templates/BindingsHeader.rusttpl +24 -0
  123. data/vendor/rust/rust/templates/BindingsUnit.rusttpl +46 -0
  124. data/vendor/rust/rust/templates/CWrapperClassDefinitions.rusttpl +64 -0
  125. data/vendor/rust/rust/templates/ClassDeclarations.rusttpl +7 -0
  126. data/vendor/rust/rust/templates/ClassInitialize.rusttpl +6 -0
  127. data/vendor/rust/rust/templates/ConstructorStub.rusttpl +21 -0
  128. data/vendor/rust/rust/templates/CxxClassDefinitions.rusttpl +91 -0
  129. data/vendor/rust/rust/templates/CxxMethodStub.rusttpl +12 -0
  130. data/vendor/rust/rust/templates/CxxStandaloneClassDefinitions.rusttpl +26 -0
  131. data/vendor/rust/rust/templates/EnumDeclarations.rusttpl +3 -0
  132. data/vendor/rust/rust/templates/EnumDefinitions.rusttpl +29 -0
  133. data/vendor/rust/rust/templates/FunctionDefinition.rusttpl +9 -0
  134. data/vendor/rust/rust/templates/FunctionInitAlias.rusttpl +5 -0
  135. data/vendor/rust/rust/templates/FunctionInitBinding.rusttpl +9 -0
  136. data/vendor/rust/rust/templates/MethodInitBinding.rusttpl +9 -0
  137. data/vendor/rust/rust/templates/ModuleDeclarations.rusttpl +3 -0
  138. data/vendor/rust/rust/templates/ModuleDefinitions.rusttpl +3 -0
  139. data/vendor/rust/rust/templates/StandaloneClassDeclarations.rusttpl +7 -0
  140. data/vendor/rust/rust/templates/VariableFunctionCall.rusttpl +14 -0
  141. data/vendor/rust/rust/type.rb +98 -0
  142. data/vendor/rust/test/Makefile +4 -0
  143. data/vendor/rust/test/constants.rb +36 -0
  144. data/vendor/rust/test/cppclass.cc +45 -0
  145. data/vendor/rust/test/cppclass.hh +67 -0
  146. data/vendor/rust/test/cppclass.rb +59 -0
  147. data/vendor/rust/test/cwrapper.c +74 -0
  148. data/vendor/rust/test/cwrapper.h +41 -0
  149. data/vendor/rust/test/cwrapper.rb +56 -0
  150. data/vendor/rust/test/dummyclass.hh +31 -0
  151. data/vendor/rust/test/lib/extension-test.rb +98 -0
  152. data/vendor/rust/test/operators.cc +41 -0
  153. data/vendor/rust/test/operators.hh +39 -0
  154. data/vendor/rust/test/operators.rb +39 -0
  155. data/vendor/rust/test/test-constants.rb +43 -0
  156. data/vendor/rust/test/test-cppclass.rb +82 -0
  157. data/vendor/rust/test/test-cwrapper.rb +80 -0
  158. data/vendor/rust/test/test-operators.rb +42 -0
  159. metadata +293 -0
@@ -0,0 +1,285 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+ require File.dirname(__FILE__) + '/constraint_helper'
3
+
4
+ describe Gecode::Constraints::Set::Operation do
5
+ before do
6
+ @model = Gecode::Model.new
7
+ @set1 = @model.set_var([], 0..20)
8
+ @set2 = @model.set_var([], 0..20)
9
+ @rhs = @model.set_var([], 0..20)
10
+ @model.branch_on @model.wrap_enum([@set1, @set2, @rhs])
11
+ @constant_set = [4,9,17]
12
+ @wrapped_constant_set = @model.wrap_enum(@constant_set)
13
+
14
+ @expect = lambda do |op1, operation_type, op2, relation_type, rhs, reif_var, negated|
15
+ op1, op2, rhs = [op1, op2, rhs].map do |expression|
16
+ # Convert the expression to the corresponding expected class.
17
+ if expression.respond_to? :bind
18
+ an_instance_of(Gecode::Raw::SetVar)
19
+ else
20
+ an_instance_of(Gecode::Raw::IntSet)
21
+ end
22
+ end
23
+
24
+ Gecode::Raw.should_receive(:rel).once.with(
25
+ an_instance_of(Gecode::Raw::Space), op1, operation_type, op2,
26
+ relation_type, rhs)
27
+ end
28
+
29
+ # For options spec.
30
+ @invoke_options = lambda do |hash|
31
+ @set1.union(@set2).must_be.superset_of(@rhs, hash)
32
+ @model.solve!
33
+ end
34
+ @expect_options = lambda do |strength, reif_var|
35
+ @expect.call(@set1, Gecode::Raw::SOT_SUP, @set2, Gecode::Raw::SRT_SUP,
36
+ @rhs, reif_var, false)
37
+ end
38
+ end
39
+
40
+ Gecode::Constraints::Util::SET_OPERATION_TYPES.each_pair do |relation, type|
41
+ it "should translate #{relation} with variable operands and variable rhs" do
42
+ @expect.call(@set1, type, @set2, Gecode::Raw::SRT_SUP, @rhs, nil, false)
43
+ @set1.send(relation, @set2).must_be.superset_of(@rhs)
44
+ @model.solve!
45
+ end
46
+
47
+ it "should translate #{relation} with variable operands and constant rhs" do
48
+ @expect.call(@set1, type, @set2, Gecode::Raw::SRT_SUP, @constant_set,
49
+ nil, false)
50
+ @set1.send(relation, @set2).must_be.superset_of(@constant_set)
51
+ @model.solve!
52
+ end
53
+
54
+ it "should translate #{relation} with variable lhs, constant operand and variable rhs" do
55
+ @expect.call(@set1, type, @constant_set, Gecode::Raw::SRT_SUP, @rhs, nil,
56
+ false)
57
+ @set1.send(relation, @constant_set).must_be.superset_of(@rhs)
58
+ @model.solve!
59
+ end
60
+
61
+ it "should translate #{relation} with variable lhs, constant operand and constant rhs" do
62
+ @expect.call(@set1, type, @constant_set, Gecode::Raw::SRT_SUP,
63
+ @constant_set, nil, false)
64
+ @set1.send(relation, @constant_set).must_be.superset_of(@constant_set)
65
+ @model.solve!
66
+ end
67
+
68
+ it "should translate #{relation} with constant lhs, variable operand and variable rhs" do
69
+ @expect.call(@constant_set, type, @set2, Gecode::Raw::SRT_SUP,
70
+ @rhs, nil, false)
71
+ @wrapped_constant_set.send(relation, @set2).must_be.superset_of(@rhs)
72
+ @model.solve!
73
+ end
74
+
75
+ it "should translate #{relation} with constant lhs, variable operand and constant rhs" do
76
+ @expect.call(@constant_set, type, @set2, Gecode::Raw::SRT_SUP,
77
+ @constant_set, nil, false)
78
+ @wrapped_constant_set.send(relation, @set2).must_be.superset_of(@constant_set)
79
+ @model.solve!
80
+ end
81
+
82
+ it "should raise error for #{relation} with constant lhs, operand and rhs" do
83
+ lambda do
84
+ @wrapped_constant_set.send(relation, @constant_set).must_be.superset_of(
85
+ @constant_set)
86
+ end.should raise_error(ArgumentError)
87
+ end
88
+ end
89
+
90
+ it 'should raise error if negated' do
91
+ lambda do
92
+ @set1.union(@set2).must_not_be.subset_of(@rhs)
93
+ end.should raise_error(Gecode::MissingConstraintError)
94
+ end
95
+
96
+ it 'should constrain the sets according to the operation (variable operands, variable rhs)' do
97
+ @set1.intersection(@set2).must == @rhs
98
+ @rhs.must == @constant_set
99
+ @model.solve!.should_not be_nil
100
+ (@set1.value.to_a & @set2.value.to_a).sort.should == @constant_set
101
+ end
102
+
103
+ it 'should constrain the sets according to the operation (variable operands, constant rhs)' do
104
+ @set1.intersection(@set2).must == @constant_set
105
+ @model.solve!.should_not be_nil
106
+ (@set1.value.to_a & @set2.value.to_a).sort.should == @constant_set
107
+ end
108
+
109
+ it 'should constrain the sets according to the operation (variable lhs, constant operand and rhs)' do
110
+ @set1.union(@constant_set).must == @constant_set
111
+ @model.solve!.should_not be_nil
112
+ (@set1.value.to_a + @constant_set).uniq.sort.should == @constant_set.sort
113
+ end
114
+
115
+ it 'should constrain the sets according to the operation (variable lhs and rhs, constant operand)' do
116
+ @set1.union(@constant_set).must == @rhs
117
+ @model.solve!.should_not be_nil
118
+ (@set1.value.to_a + @constant_set).uniq.sort.should == @rhs.value.to_a.sort
119
+ end
120
+
121
+ it 'should constrain the sets according to the operation (constant lhs, variable operand and rhs)' do
122
+ @wrapped_constant_set.minus(@set2).must == @rhs
123
+ @model.solve!.should_not be_nil
124
+ (@constant_set - @set2.value.to_a).sort.should == @rhs.value.sort
125
+ end
126
+
127
+ it 'should constrain the sets according to the operation (constant lhs and rhs, variable operand)' do
128
+ @wrapped_constant_set.minus(@set2).must == @constant_set
129
+ @model.solve!.should_not be_nil
130
+ (@constant_set - @set2.value.to_a).sort.should == @constant_set
131
+ end
132
+
133
+ it_should_behave_like 'non-reifiable set constraint'
134
+ end
135
+
136
+ describe 'set enum operation constraint', :shared => true do
137
+ include GecodeR::Specs::SetHelper
138
+
139
+ before do
140
+ @expect = lambda do |enum, operation_type, relation, rhs, reif_var, negated|
141
+ if rhs.respond_to? :bind
142
+ expected_target = [an_instance_of(Gecode::Raw::SetVar)]
143
+ relation_constraint = :rel
144
+ else
145
+ expected_target = expect_constant_set(rhs)
146
+ relation_constraint = :dom
147
+ end
148
+
149
+ if reif_var.nil?
150
+ if !negated and relation == Gecode::Raw::IRT_EQ and
151
+ !rhs.kind_of? Enumerable
152
+ Gecode::Raw.should_receive(:rel).once.with(
153
+ an_instance_of(Gecode::Raw::Space), operation_type,
154
+ an_instance_of(Gecode::Raw::SetVarArray),
155
+ *expected_target)
156
+ Gecode::Raw.should_receive(:dom).exactly(0).times
157
+ else
158
+ if relation_constraint == :rel
159
+ Gecode::Raw.should_receive(:rel).twice
160
+ else
161
+ Gecode::Raw.should_receive(:rel).once.with(
162
+ an_instance_of(Gecode::Raw::Space), operation_type,
163
+ an_instance_of(Gecode::Raw::SetVarArray),
164
+ an_instance_of(Gecode::Raw::SetVar))
165
+ Gecode::Raw.should_receive(relation_constraint).at_most(:twice)
166
+ end
167
+ end
168
+ else
169
+ if relation_constraint == :rel
170
+ Gecode::Raw.should_receive(:rel).twice
171
+ else
172
+ Gecode::Raw.should_receive(:rel).once.with(
173
+ an_instance_of(Gecode::Raw::Space), operation_type,
174
+ an_instance_of(Gecode::Raw::SetVarArray),
175
+ an_instance_of(Gecode::Raw::SetVar))
176
+ expected_target << an_instance_of(Gecode::Raw::BoolVar)
177
+ Gecode::Raw.should_receive(relation_constraint).once.with(
178
+ an_instance_of(Gecode::Raw::Space),
179
+ an_instance_of(Gecode::Raw::SetVar), relation, *expected_target)
180
+ end
181
+ end
182
+ end
183
+
184
+ # For composite spec.
185
+ @invoke_relation = lambda do |relation, target, negated|
186
+ if negated
187
+ @stub.must_not.send(relation, target)
188
+ else
189
+ @stub.must.send(relation, target)
190
+ end
191
+ @model.solve!
192
+ end
193
+ @expect_relation = lambda do |relation, target, negated|
194
+ @expect.call(@sets, @operation_type, relation, target, nil, negated)
195
+ end
196
+
197
+ # For options spec.
198
+ @invoke_options = lambda do |hash|
199
+ @stub.must_be.superset_of(@rhs, hash)
200
+ @model.solve!
201
+ end
202
+ @expect_options = lambda do |strength, reif_var|
203
+ @expect.call(@sets, @operation_type, Gecode::Raw::SRT_SUP, @rhs,
204
+ reif_var, false)
205
+ end
206
+ end
207
+
208
+ it_should_behave_like 'reifiable set constraint'
209
+ it_should_behave_like 'composite set constraint'
210
+ end
211
+
212
+ describe Gecode::Constraints::SetEnum::Operation, ' (union)' do
213
+ before do
214
+ @model = Gecode::Model.new
215
+ @sets = @model.set_var_array(10, [], 0..20)
216
+ @target = @rhs = @model.set_var([], 0..20)
217
+ @model.branch_on @sets
218
+
219
+ @stub = @sets.union
220
+ @operation_type = Gecode::Raw::SOT_UNION
221
+ end
222
+
223
+ it 'should constrain the union of the sets' do
224
+ @sets.union.must_be.subset_of [1,4,17]
225
+ @sets.union.must_be.superset_of 1
226
+ @model.solve!.should_not be_nil
227
+ union = @sets.values.inject([]){ |union, set| union += set.to_a }.uniq
228
+ union.should include(1)
229
+ (union - [1,4,17]).should be_empty
230
+ end
231
+
232
+ it_should_behave_like 'set enum operation constraint'
233
+ end
234
+
235
+ describe Gecode::Constraints::SetEnum::Operation, ' (intersection)' do
236
+ before do
237
+ @model = Gecode::Model.new
238
+ @sets = @model.set_var_array(10, [], 0..20)
239
+ @target = @rhs = @model.set_var([], 0..20)
240
+ @model.branch_on @sets
241
+
242
+ @stub = @sets.intersection
243
+ @operation_type = Gecode::Raw::SOT_INTER
244
+ end
245
+
246
+ it 'should constrain the intersection of the sets' do
247
+ @sets.intersection.must_be.subset_of [1,4,17]
248
+ @sets.intersection.must_be.superset_of [1]
249
+ @model.solve!.should_not be_nil
250
+ intersection = @sets.values.inject(nil) do |intersection, set|
251
+ next set.to_a if intersection.nil?
252
+ intersection &= set.to_a
253
+ end.uniq
254
+ intersection.should include(1)
255
+ (intersection - [1,4,17]).should be_empty
256
+ end
257
+
258
+ it_should_behave_like 'set enum operation constraint'
259
+ end
260
+
261
+ describe Gecode::Constraints::SetEnum::Operation, ' (disjoint union)' do
262
+ before do
263
+ @model = Gecode::Model.new
264
+ @sets = @model.set_var_array(10, [], 0..20)
265
+ @target = @rhs = @model.set_var([], 0..20)
266
+ @model.branch_on @sets
267
+
268
+ @stub = @sets.disjoint_union
269
+ @operation_type = Gecode::Raw::SOT_DUNION
270
+ end
271
+
272
+ it 'should constrain the disjoint union of the sets' do
273
+ @sets.disjoint_union.must_be.subset_of [1,4,17]
274
+ @sets.disjoint_union.must_be.superset_of [1]
275
+ @model.solve!.should_not be_nil
276
+ disjoint_union = @sets.values.inject([]) do |union, set|
277
+ intersection = union & set.to_a
278
+ union += set.to_a - intersection
279
+ end.uniq
280
+ disjoint_union.should include(1)
281
+ (disjoint_union - [1,4,17]).should be_empty
282
+ end
283
+
284
+ it_should_behave_like 'set enum operation constraint'
285
+ end
@@ -0,0 +1,201 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+ require File.dirname(__FILE__) + '/constraint_helper'
3
+
4
+ describe Gecode::Constraints::Set::Relation do
5
+ include GecodeR::Specs::SetHelper
6
+
7
+ before do
8
+ @model = Gecode::Model.new
9
+ @set = @model.set_var([0], 0..3)
10
+ @set2 = @model.set_var([1], 0..3)
11
+
12
+ @expect = lambda do |relation_type, rhs, reif_var, negated|
13
+ @model.allow_space_access do
14
+ if reif_var.nil? and !negated
15
+ Gecode::Raw.should_receive(:rel).once.with(
16
+ an_instance_of(Gecode::Raw::Space),
17
+ an_instance_of(Gecode::Raw::SetVar), relation_type,
18
+ an_instance_of(Gecode::Raw::SetVar))
19
+ else
20
+ Gecode::Raw.should_receive(:rel).once.with(
21
+ an_instance_of(Gecode::Raw::Space),
22
+ an_instance_of(Gecode::Raw::SetVar), relation_type,
23
+ an_instance_of(Gecode::Raw::SetVar),
24
+ an_instance_of(Gecode::Raw::BoolVar))
25
+ end
26
+ end
27
+ end
28
+
29
+ # For options spec.
30
+ @invoke_options = lambda do |hash|
31
+ @set.must_be.superset_of(@set2, hash)
32
+ @model.solve!
33
+ end
34
+ @expect_options = lambda do |strength, reif_var|
35
+ @expect.call(Gecode::Raw::SRT_SUP, @set2, reif_var, false)
36
+ end
37
+ end
38
+
39
+ Gecode::Constraints::Util::SET_RELATION_TYPES.each_pair do |relation, type|
40
+ next if relation == :==
41
+
42
+ it "should translate #{relation} with set to relation constraint" do
43
+ @expect.call(type, @set2, nil, false)
44
+ @set.must.send(relation, @set2)
45
+ @model.solve!
46
+ end
47
+
48
+ it "should translate negated #{relation} with set to relation constraint" do
49
+ @expect.call(type, @set2, nil, true)
50
+ @set.must_not.send(relation, @set2)
51
+ @model.solve!
52
+ end
53
+ end
54
+ it_should_behave_like 'reifiable set constraint'
55
+ end
56
+
57
+ describe Gecode::Constraints::Set::Relation, ' (equality)' do
58
+ include GecodeR::Specs::SetHelper
59
+
60
+ before do
61
+ @model = Gecode::Model.new
62
+ @set = @model.set_var([0], 0..1)
63
+ @set2 = @model.set_var([1], 0..1)
64
+
65
+ @expect = lambda do |relation_type, rhs, reif_var|
66
+ @model.allow_space_access do
67
+ if reif_var.nil?
68
+ Gecode::Raw.should_receive(:rel).once.with(
69
+ an_instance_of(Gecode::Raw::Space),
70
+ an_instance_of(Gecode::Raw::SetVar), relation_type,
71
+ an_instance_of(Gecode::Raw::SetVar))
72
+ else
73
+ Gecode::Raw.should_receive(:rel).once.with(
74
+ an_instance_of(Gecode::Raw::Space),
75
+ an_instance_of(Gecode::Raw::SetVar), relation_type,
76
+ an_instance_of(Gecode::Raw::SetVar),
77
+ an_instance_of(Gecode::Raw::BoolVar))
78
+ end
79
+ end
80
+ end
81
+
82
+ # For options spec.
83
+ @invoke_options = lambda do |hash|
84
+ @set.must_be.equal_to(@set2, hash)
85
+ @model.solve!
86
+ end
87
+ @expect_options = lambda do |strength, reif_var|
88
+ @expect.call(Gecode::Raw::SRT_EQ, @set2, reif_var)
89
+ end
90
+ end
91
+
92
+ it 'should translate equality with set to relation constraint' do
93
+ @expect.call(Gecode::Raw::SRT_EQ, @set2, nil)
94
+ @set.must == @set2
95
+ @model.solve!
96
+ end
97
+
98
+ it 'should translate negated equality with set to domain constraint' do
99
+ @expect.call(Gecode::Raw::SRT_NQ, @set2, nil)
100
+ @set.must_not == @set2
101
+ @model.solve!
102
+ end
103
+
104
+ it 'should constrain sets to be equality when not negated' do
105
+ @set.must == @set2
106
+ @model.solve!
107
+ @set.should have_bounds(0..1, 0..1)
108
+ @set2.should have_bounds(0..1, 0..1)
109
+ end
110
+
111
+ it 'should constrain sets to not be equal when negated' do
112
+ @set.must_not == @set2
113
+ union = @model.set_var([0, 1], [0, 1])
114
+ @set.must_not == union
115
+ @set2.must_not == union
116
+ @model.solve!
117
+ @set.should have_bounds([0], [0])
118
+ @set2.should have_bounds([1], [1])
119
+ end
120
+
121
+ it_should_behave_like 'reifiable set constraint'
122
+ end
123
+
124
+ describe Gecode::Constraints::Set::Relation, ' (elements)' do
125
+ include GecodeR::Specs::SetHelper
126
+
127
+ before do
128
+ @model = Gecode::Model.new
129
+ @set = @model.set_var([0], 0..2)
130
+ @int_var = @model.int_var(0..2)
131
+ @int_constant = 2
132
+ @model.branch_on @model.wrap_enum([@set])
133
+ @expect = lambda do |relation_type, rhs|
134
+ @model.allow_space_access do
135
+ if rhs.kind_of? Fixnum
136
+ rhs = an_instance_of(Gecode::Raw::IntVar)
137
+ end
138
+ rhs = rhs.bind if rhs.respond_to? :bind
139
+ Gecode::Raw.should_receive(:rel).once.with(
140
+ an_instance_of(Gecode::Raw::Space),
141
+ an_instance_of(Gecode::Raw::SetVar), relation_type,
142
+ an_instance_of(Gecode::Raw::IntVar))
143
+ end
144
+ end
145
+
146
+ @invoke_options = lambda do |hash|
147
+ @set.elements.must_be.equal_to(@int_var, hash)
148
+ end
149
+
150
+ @invoke_options = lambda do |hash|
151
+ @set.elements.must_be.equal_to(@int_var, hash)
152
+ end
153
+ end
154
+
155
+ Gecode::Constraints::Util::RELATION_TYPES.each_pair do |relation, type|
156
+ it "should translate #{relation} with variable to relation constraint" do
157
+ @expect.call(type, @int_var)
158
+ @set.elements.must.send(relation, @int_var)
159
+ @model.solve!
160
+ end
161
+
162
+ it "should translate #{relation} with constant to relation constraint" do
163
+ @expect.call(type, @int_constant)
164
+ @set.elements.must.send(relation, @int_constant)
165
+ @model.solve!
166
+ end
167
+ end
168
+
169
+ Gecode::Constraints::Util::NEGATED_RELATION_TYPES.each_pair do |relation, type|
170
+ it "should translate negated #{relation} with variable to relation constraint" do
171
+ @expect.call(type, @int_var)
172
+ @set.elements.must_not.send(relation, @int_var)
173
+ @model.solve!
174
+ end
175
+
176
+ it "should translate negated #{relation} with constant to relation constraint" do
177
+ @expect.call(type, @int_constant)
178
+ @set.elements.must_not.send(relation, @int_constant)
179
+ @model.solve!
180
+ end
181
+ end
182
+
183
+ it 'should constrain the elements of the set' do
184
+ @set.elements.must <= @int_var
185
+ @int_var.must == 0
186
+ @model.solve!
187
+ @set.should be_assigned
188
+ @set.value.should include(0)
189
+ @set.value.should_not include(1,2)
190
+ end
191
+
192
+ it 'should constrain the elements of the set (constant parameter)' do
193
+ @set.elements.must <= 0
194
+ @model.solve!
195
+ @set.should be_assigned
196
+ @set.value.should include(0)
197
+ @set.value.should_not include(1,2)
198
+ end
199
+
200
+ it_should_behave_like 'non-reifiable set constraint'
201
+ end