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