gecoder 0.8.3 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. data/CHANGES +15 -0
  2. data/README +6 -2
  3. data/example/equation_system.rb +15 -0
  4. data/example/magic_sequence.rb +7 -7
  5. data/example/money.rb +36 -0
  6. data/example/queens.rb +7 -8
  7. data/example/send_most_money.rb +1 -1
  8. data/example/square_tiling.rb +2 -2
  9. data/example/sudoku-set.rb +11 -12
  10. data/example/sudoku.rb +40 -45
  11. data/ext/extconf.rb +0 -0
  12. data/lib/gecoder/bindings.rb +42 -0
  13. data/lib/gecoder/bindings/bindings.rb +16 -0
  14. data/lib/gecoder/interface.rb +2 -1
  15. data/lib/gecoder/interface/branch.rb +16 -9
  16. data/lib/gecoder/interface/constraints.rb +410 -451
  17. data/lib/gecoder/interface/constraints/bool/boolean.rb +205 -213
  18. data/lib/gecoder/interface/constraints/bool/channel.rb +4 -5
  19. data/lib/gecoder/interface/constraints/bool/linear.rb +192 -21
  20. data/lib/gecoder/interface/constraints/bool_enum/channel.rb +43 -39
  21. data/lib/gecoder/interface/constraints/bool_enum/extensional.rb +43 -49
  22. data/lib/gecoder/interface/constraints/bool_enum/relation.rb +38 -71
  23. data/lib/gecoder/interface/constraints/bool_enum_constraints.rb +73 -22
  24. data/lib/gecoder/interface/constraints/bool_var_constraints.rb +140 -61
  25. data/lib/gecoder/interface/constraints/extensional_regexp.rb +4 -4
  26. data/lib/gecoder/interface/constraints/fixnum_enum/element.rb +63 -0
  27. data/lib/gecoder/interface/constraints/fixnum_enum/operation.rb +65 -0
  28. data/lib/gecoder/interface/constraints/fixnum_enum_constraints.rb +42 -0
  29. data/lib/gecoder/interface/constraints/int/arithmetic.rb +131 -130
  30. data/lib/gecoder/interface/constraints/int/channel.rb +21 -31
  31. data/lib/gecoder/interface/constraints/int/domain.rb +45 -42
  32. data/lib/gecoder/interface/constraints/int/linear.rb +85 -239
  33. data/lib/gecoder/interface/constraints/int/relation.rb +141 -0
  34. data/lib/gecoder/interface/constraints/int_enum/arithmetic.rb +55 -64
  35. data/lib/gecoder/interface/constraints/int_enum/channel.rb +35 -37
  36. data/lib/gecoder/interface/constraints/int_enum/count.rb +53 -78
  37. data/lib/gecoder/interface/constraints/int_enum/distinct.rb +36 -46
  38. data/lib/gecoder/interface/constraints/int_enum/element.rb +39 -57
  39. data/lib/gecoder/interface/constraints/int_enum/equality.rb +15 -19
  40. data/lib/gecoder/interface/constraints/int_enum/extensional.rb +65 -72
  41. data/lib/gecoder/interface/constraints/int_enum/sort.rb +42 -45
  42. data/lib/gecoder/interface/constraints/int_enum_constraints.rb +79 -22
  43. data/lib/gecoder/interface/constraints/int_var_constraints.rb +215 -44
  44. data/lib/gecoder/interface/constraints/reifiable_constraints.rb +14 -14
  45. data/lib/gecoder/interface/constraints/selected_set/select.rb +120 -0
  46. data/lib/gecoder/interface/constraints/selected_set_constraints.rb +75 -0
  47. data/lib/gecoder/interface/constraints/set/cardinality.rb +43 -53
  48. data/lib/gecoder/interface/constraints/set/channel.rb +26 -29
  49. data/lib/gecoder/interface/constraints/set/connection.rb +89 -152
  50. data/lib/gecoder/interface/constraints/set/domain.rb +112 -65
  51. data/lib/gecoder/interface/constraints/set/include.rb +36 -0
  52. data/lib/gecoder/interface/constraints/set/operation.rb +96 -110
  53. data/lib/gecoder/interface/constraints/set/relation.rb +114 -137
  54. data/lib/gecoder/interface/constraints/set_elements/relation.rb +116 -0
  55. data/lib/gecoder/interface/constraints/set_elements_constraints.rb +97 -0
  56. data/lib/gecoder/interface/constraints/set_enum/channel.rb +23 -27
  57. data/lib/gecoder/interface/constraints/set_enum/distinct.rb +18 -19
  58. data/lib/gecoder/interface/constraints/set_enum/operation.rb +62 -53
  59. data/lib/gecoder/interface/constraints/set_enum/select.rb +79 -0
  60. data/lib/gecoder/interface/constraints/set_enum_constraints.rb +73 -23
  61. data/lib/gecoder/interface/constraints/set_var_constraints.rb +222 -57
  62. data/lib/gecoder/interface/enum_matrix.rb +4 -4
  63. data/lib/gecoder/interface/enum_wrapper.rb +71 -22
  64. data/lib/gecoder/interface/model.rb +167 -12
  65. data/lib/gecoder/interface/model_sugar.rb +84 -0
  66. data/lib/gecoder/interface/search.rb +30 -18
  67. data/lib/gecoder/interface/variables.rb +103 -33
  68. data/lib/gecoder/version.rb +2 -2
  69. data/specs/bool_var.rb +19 -12
  70. data/specs/constraints/{boolean.rb → bool/boolean.rb} +103 -28
  71. data/specs/constraints/bool/boolean_properties.rb +51 -0
  72. data/specs/constraints/bool/linear.rb +213 -0
  73. data/specs/constraints/bool_enum/bool_enum_relation.rb +117 -0
  74. data/specs/constraints/bool_enum/channel.rb +102 -0
  75. data/specs/constraints/{extensional.rb → bool_enum/extensional.rb} +32 -101
  76. data/specs/constraints/constraint_helper.rb +149 -179
  77. data/specs/constraints/constraint_receivers.rb +103 -0
  78. data/specs/constraints/constraints.rb +6 -63
  79. data/specs/constraints/fixnum_enum/element.rb +58 -0
  80. data/specs/constraints/fixnum_enum/operation.rb +67 -0
  81. data/specs/constraints/int/arithmetic.rb +149 -0
  82. data/specs/constraints/int/channel.rb +101 -0
  83. data/specs/constraints/int/domain.rb +106 -0
  84. data/specs/constraints/int/linear.rb +183 -0
  85. data/specs/constraints/int/linear_properties.rb +97 -0
  86. data/specs/constraints/int/relation.rb +84 -0
  87. data/specs/constraints/int_enum/arithmetic.rb +72 -0
  88. data/specs/constraints/int_enum/channel.rb +57 -0
  89. data/specs/constraints/int_enum/count.rb +72 -0
  90. data/specs/constraints/int_enum/distinct.rb +80 -0
  91. data/specs/constraints/int_enum/element.rb +61 -0
  92. data/specs/constraints/int_enum/equality.rb +29 -0
  93. data/specs/constraints/int_enum/extensional.rb +224 -0
  94. data/specs/constraints/int_enum/sort.rb +167 -0
  95. data/specs/constraints/operands.rb +264 -0
  96. data/specs/constraints/property_helper.rb +443 -0
  97. data/specs/constraints/reification_sugar.rb +4 -5
  98. data/specs/constraints/selected_set/select.rb +56 -0
  99. data/specs/constraints/selected_set/select_properties.rb +157 -0
  100. data/specs/constraints/set/cardinality.rb +58 -0
  101. data/specs/constraints/set/cardinality_properties.rb +46 -0
  102. data/specs/constraints/set/channel.rb +77 -0
  103. data/specs/constraints/set/connection.rb +176 -0
  104. data/specs/constraints/set/domain.rb +197 -0
  105. data/specs/constraints/set/include.rb +36 -0
  106. data/specs/constraints/set/operation.rb +132 -0
  107. data/specs/constraints/set/relation.rb +117 -0
  108. data/specs/constraints/set_elements/relation.rb +84 -0
  109. data/specs/constraints/set_enum/channel.rb +80 -0
  110. data/specs/constraints/set_enum/distinct.rb +59 -0
  111. data/specs/constraints/set_enum/operation.rb +111 -0
  112. data/specs/constraints/set_enum/select.rb +73 -0
  113. data/specs/enum_wrapper.rb +53 -3
  114. data/specs/int_var.rb +44 -25
  115. data/specs/model.rb +58 -1
  116. data/specs/model_sugar.rb +30 -0
  117. data/specs/search.rb +24 -5
  118. data/specs/selected_set.rb +39 -0
  119. data/specs/set_elements.rb +34 -0
  120. data/specs/set_var.rb +22 -8
  121. data/specs/spec_helper.rb +206 -6
  122. data/tasks/distribution.rake +22 -7
  123. data/tasks/svn.rake +3 -1
  124. metadata +218 -134
  125. data/lib/gecoder/interface/constraints/set_enum/selection.rb +0 -217
  126. data/specs/constraints/arithmetic.rb +0 -351
  127. data/specs/constraints/bool_enum_relation.rb +0 -160
  128. data/specs/constraints/cardinality.rb +0 -157
  129. data/specs/constraints/channel.rb +0 -454
  130. data/specs/constraints/connection.rb +0 -369
  131. data/specs/constraints/count.rb +0 -146
  132. data/specs/constraints/distinct.rb +0 -164
  133. data/specs/constraints/element.rb +0 -108
  134. data/specs/constraints/equality.rb +0 -31
  135. data/specs/constraints/int_domain.rb +0 -70
  136. data/specs/constraints/int_relation.rb +0 -82
  137. data/specs/constraints/linear.rb +0 -340
  138. data/specs/constraints/selection.rb +0 -292
  139. data/specs/constraints/set_domain.rb +0 -185
  140. data/specs/constraints/set_operation.rb +0 -285
  141. data/specs/constraints/set_relation.rb +0 -197
  142. data/specs/constraints/sort.rb +0 -179
@@ -1,197 +0,0 @@
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 = option_expectation do |strength, kind, 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 = option_expectation do |strength, kind, 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
- end
150
-
151
- Gecode::Constraints::Util::RELATION_TYPES.each_pair do |relation, type|
152
- it "should translate #{relation} with variable to relation constraint" do
153
- @expect.call(type, @int_var)
154
- @set.elements.must.send(relation, @int_var)
155
- @model.solve!
156
- end
157
-
158
- it "should translate #{relation} with constant to relation constraint" do
159
- @expect.call(type, @int_constant)
160
- @set.elements.must.send(relation, @int_constant)
161
- @model.solve!
162
- end
163
- end
164
-
165
- Gecode::Constraints::Util::NEGATED_RELATION_TYPES.each_pair do |relation, type|
166
- it "should translate negated #{relation} with variable to relation constraint" do
167
- @expect.call(type, @int_var)
168
- @set.elements.must_not.send(relation, @int_var)
169
- @model.solve!
170
- end
171
-
172
- it "should translate negated #{relation} with constant to relation constraint" do
173
- @expect.call(type, @int_constant)
174
- @set.elements.must_not.send(relation, @int_constant)
175
- @model.solve!
176
- end
177
- end
178
-
179
- it 'should constrain the elements of the set' do
180
- @set.elements.must <= @int_var
181
- @int_var.must == 0
182
- @model.solve!
183
- @set.should be_assigned
184
- @set.value.should include(0)
185
- @set.value.should_not include(1,2)
186
- end
187
-
188
- it 'should constrain the elements of the set (constant parameter)' do
189
- @set.elements.must <= 0
190
- @model.solve!
191
- @set.should be_assigned
192
- @set.value.should include(0)
193
- @set.value.should_not include(1,2)
194
- end
195
-
196
- it_should_behave_like 'non-reifiable set constraint'
197
- end
@@ -1,179 +0,0 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
2
- require File.dirname(__FILE__) + '/constraint_helper'
3
-
4
- class SortSampleProblem < Gecode::Model
5
- attr :vars
6
- attr :sorted
7
- attr :indices
8
-
9
- def initialize
10
- @vars = int_var_array(4, 10..19)
11
- @sorted = int_var_array(4, 10..19)
12
- @indices = int_var_array(4, 0..9)
13
-
14
- # To make it more interesting
15
- @vars.must_be.distinct
16
-
17
- branch_on @vars
18
- end
19
- end
20
-
21
- describe Gecode::Constraints::IntEnum::Sort, ' (without :as and :order)' do
22
- before do
23
- @model = SortSampleProblem.new
24
- @vars = @model.vars
25
- @sorted = @model.sorted
26
-
27
- @invoke_options = lambda do |hash|
28
- @vars.must_be.sorted(hash)
29
- @model.solve!
30
- end
31
- @expect_options = option_expectation do |strength, kind, reif_var|
32
- if reif_var.nil?
33
- Gecode::Raw.should_receive(:rel).exactly(@vars.size - 1).times.with(
34
- an_instance_of(Gecode::Raw::Space),
35
- an_instance_of(Gecode::Raw::IntVar), Gecode::Raw::IRT_LQ,
36
- an_instance_of(Gecode::Raw::IntVar), strength, kind)
37
- else
38
- Gecode::Raw.should_receive(:rel).once.with(
39
- an_instance_of(Gecode::Raw::Space), anything,
40
- an_instance_of(Gecode::Raw::BoolVarArray),
41
- anything, anything, anything)
42
- Gecode::Raw.should_receive(:rel).exactly(@vars.size - 1).times.with(
43
- an_instance_of(Gecode::Raw::Space),
44
- an_instance_of(Gecode::Raw::IntVar), Gecode::Raw::IRT_LQ,
45
- an_instance_of(Gecode::Raw::IntVar),
46
- an_instance_of(Gecode::Raw::BoolVar), strength, kind)
47
- end
48
- end
49
- end
50
-
51
- it 'should translate into n relation constraints' do
52
- @expect_options.call({})
53
- @invoke_options.call({})
54
- end
55
-
56
- it 'should constraint variables to be sorted' do
57
- @vars.must_be.sorted
58
- values = @model.solve!.vars.values
59
- values.should == values.sort
60
- end
61
-
62
- it 'should allow negation' do
63
- @vars.must_not_be.sorted
64
- @model.solve!
65
- values = @vars.values
66
- values.should_not == values.sort
67
- end
68
-
69
- it_should_behave_like 'reifiable constraint'
70
- end
71
-
72
- describe Gecode::Constraints::IntEnum::Sort, ' (with :as)' do
73
- before do
74
- @model = SortSampleProblem.new
75
- @vars = @model.vars
76
- @sorted = @model.sorted
77
-
78
- # Make it a bit more interesting.
79
- @vars[0].must > @vars[3] + 1
80
-
81
- @invoke_options = lambda do |hash|
82
- @vars.must_be.sorted hash.update(:as => @sorted)
83
- @model.solve!
84
- end
85
- @expect_options = option_expectation do |strength, kind, reif_var|
86
- Gecode::Raw.should_receive(:sorted).once.with(
87
- an_instance_of(Gecode::Raw::Space),
88
- an_instance_of(Gecode::Raw::IntVarArray),
89
- an_instance_of(Gecode::Raw::IntVarArray), strength, kind)
90
- end
91
- end
92
-
93
- it 'should translate into a sortedness constraints' do
94
- @expect_options.call({})
95
- @invoke_options.call({})
96
- end
97
-
98
- it 'should constraint variables to be sorted' do
99
- @vars.must_be.sorted(:as => @sorted)
100
- @model.solve!
101
- values = @sorted.values
102
- values.should == values.sort
103
- end
104
-
105
- it 'should not allow targets that are not int var enums' do
106
- lambda{ @vars.must_be.sorted(:as => 'hello') }.should raise_error(TypeError)
107
- end
108
-
109
- it 'should not allow negation' do
110
- lambda{ @vars.must_not_be.sorted(:as => @sorted) }.should raise_error(
111
- Gecode::MissingConstraintError)
112
- end
113
-
114
- it_should_behave_like 'non-reifiable constraint'
115
- end
116
-
117
- describe Gecode::Constraints::IntEnum::Sort, ' (with :order)' do
118
- before do
119
- @model = SortSampleProblem.new
120
- @vars = @model.vars
121
- @sorted = @model.sorted
122
- @indices = @model.indices
123
-
124
- # Make it a bit more interesting.
125
- @vars[0].must > @vars[3] + 1
126
-
127
- @invoke_options = lambda do |hash|
128
- @vars.must_be.sorted hash.update(:order => @indices, :as => @sorted)
129
- @model.solve!
130
- end
131
- @expect_options = option_expectation do |strength, kind, reif_var|
132
- Gecode::Raw.should_receive(:sorted).once.with(
133
- an_instance_of(Gecode::Raw::Space),
134
- an_instance_of(Gecode::Raw::IntVarArray),
135
- an_instance_of(Gecode::Raw::IntVarArray),
136
- an_instance_of(Gecode::Raw::IntVarArray), strength, kind)
137
- end
138
- end
139
-
140
- it 'should translate into a sortedness constraints' do
141
- @expect_options.call({})
142
- @invoke_options.call({})
143
- end
144
-
145
- it 'should translate into a sortedness constraints, even without a target' do
146
- @expect_options.call({})
147
- @vars.must_be.sorted(:order => @indices)
148
- @model.solve!
149
- end
150
-
151
- it 'should constraint variables to be sorted with the specified indices' do
152
- @vars.must_be.sorted(:as => @sorted, :order => @indices)
153
- @model.solve!
154
- sorted_values = @sorted.values
155
- sorted_values.should == sorted_values.sort
156
- expected_indices = @vars.map{ |v| sorted_values.index(v.value) }
157
- @indices.values.should == expected_indices
158
- end
159
-
160
- it 'should not allow targets that are not int var enums' do
161
- lambda do
162
- @vars.must_be.sorted(:as => 'hello', :order => @indices)
163
- end.should raise_error(TypeError)
164
- end
165
-
166
- it 'should not allow order that are not int var enums' do
167
- lambda do
168
- @vars.must_be.sorted(:as => @sorted, :order => 'hello')
169
- end.should raise_error(TypeError)
170
- end
171
-
172
- it 'should not allow negation' do
173
- lambda do
174
- @vars.must_not_be.sorted(:as => @sorted, :order => @indices)
175
- end.should raise_error(Gecode::MissingConstraintError)
176
- end
177
-
178
- it_should_behave_like 'non-reifiable constraint'
179
- end