gecoder 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/CHANGES +16 -3
  2. data/example/magic_sequence.rb +1 -1
  3. data/example/queens.rb +1 -1
  4. data/example/send_more_money.rb +1 -1
  5. data/example/sudoku.rb +1 -1
  6. data/ext/missing.cpp +18 -4
  7. data/ext/missing.h +8 -0
  8. data/lib/gecoder/bindings.rb +30 -3
  9. data/lib/gecoder/bindings/bindings.rb +22 -0
  10. data/lib/gecoder/interface/binding_changes.rb +81 -107
  11. data/lib/gecoder/interface/branch.rb +65 -14
  12. data/lib/gecoder/interface/constraints.rb +1 -0
  13. data/lib/gecoder/interface/constraints/bool_enum/boolean.rb +16 -12
  14. data/lib/gecoder/interface/constraints/int/arithmetic.rb +7 -3
  15. data/lib/gecoder/interface/constraints/int/linear.rb +19 -16
  16. data/lib/gecoder/interface/constraints/int_enum/arithmetic.rb +8 -4
  17. data/lib/gecoder/interface/constraints/int_enum/channel.rb +14 -6
  18. data/lib/gecoder/interface/constraints/int_enum/element.rb +7 -5
  19. data/lib/gecoder/interface/constraints/int_enum/sort.rb +1 -4
  20. data/lib/gecoder/interface/constraints/set/cardinality.rb +6 -3
  21. data/lib/gecoder/interface/constraints/set/connection.rb +136 -0
  22. data/lib/gecoder/interface/constraints/set_enum/channel.rb +18 -0
  23. data/lib/gecoder/interface/constraints/set_enum/distinct.rb +61 -0
  24. data/lib/gecoder/interface/constraints/set_enum_constraints.rb +32 -0
  25. data/lib/gecoder/interface/constraints/set_var_constraints.rb +1 -0
  26. data/lib/gecoder/interface/enum_wrapper.rb +12 -3
  27. data/lib/gecoder/interface/model.rb +77 -56
  28. data/lib/gecoder/interface/search.rb +74 -5
  29. data/lib/gecoder/interface/variables.rb +117 -15
  30. data/lib/gecoder/version.rb +1 -1
  31. data/specs/binding_changes.rb +9 -5
  32. data/specs/bool_var.rb +8 -12
  33. data/specs/branch.rb +85 -19
  34. data/specs/constraints/arithmetic.rb +99 -71
  35. data/specs/constraints/bool_enum.rb +26 -18
  36. data/specs/constraints/boolean.rb +53 -49
  37. data/specs/constraints/cardinality.rb +33 -26
  38. data/specs/constraints/channel.rb +77 -6
  39. data/specs/constraints/connection.rb +352 -0
  40. data/specs/constraints/constraints.rb +10 -1
  41. data/specs/constraints/count.rb +79 -39
  42. data/specs/constraints/distinct.rb +128 -9
  43. data/specs/constraints/element.rb +26 -19
  44. data/specs/constraints/equality.rb +2 -1
  45. data/specs/constraints/int_domain.rb +19 -12
  46. data/specs/constraints/int_relation.rb +12 -6
  47. data/specs/constraints/linear.rb +30 -30
  48. data/specs/constraints/reification_sugar.rb +8 -4
  49. data/specs/constraints/set_domain.rb +24 -18
  50. data/specs/constraints/set_relation.rb +38 -23
  51. data/specs/constraints/sort.rb +12 -10
  52. data/specs/enum_wrapper.rb +9 -3
  53. data/specs/int_var.rb +8 -4
  54. data/specs/logging.rb +24 -0
  55. data/specs/model.rb +25 -7
  56. data/specs/search.rb +41 -1
  57. data/specs/set_var.rb +36 -7
  58. data/specs/spec_helper.rb +3 -10
  59. data/vendor/rust/rust/templates/FunctionDefinition.rusttpl +1 -1
  60. metadata +12 -3
  61. data/specs/tmp +0 -22
@@ -27,10 +27,12 @@ describe Gecode::Constraints::ReifiableConstraint do
27
27
  end
28
28
 
29
29
  it 'should translate disjunctions' do
30
- Gecode::Raw.should_receive(:rel).once.with(@model.active_space,
30
+ Gecode::Raw.should_receive(:rel).once.with(
31
+ an_instance_of(Gecode::Raw::Space),
31
32
  an_instance_of(Gecode::Raw::IntVar), Gecode::Raw::IRT_GR, 0,
32
33
  an_instance_of(Gecode::Raw::BoolVar), Gecode::Raw::ICL_DEF)
33
- Gecode::Raw.should_receive(:rel).once.with(@model.active_space,
34
+ Gecode::Raw.should_receive(:rel).once.with(
35
+ an_instance_of(Gecode::Raw::Space),
34
36
  an_instance_of(Gecode::Raw::IntVar), Gecode::Raw::IRT_EQ, 3,
35
37
  an_instance_of(Gecode::Raw::BoolVar), Gecode::Raw::ICL_DEF)
36
38
  (@x.must > 0) | (@y.must == 3)
@@ -50,10 +52,12 @@ describe Gecode::Constraints::ReifiableConstraint do
50
52
  end
51
53
 
52
54
  it 'should translate conjunctions' do
53
- Gecode::Raw.should_receive(:rel).once.with(@model.active_space,
55
+ Gecode::Raw.should_receive(:rel).once.with(
56
+ an_instance_of(Gecode::Raw::Space),
54
57
  an_instance_of(Gecode::Raw::IntVar), Gecode::Raw::IRT_GR, 0,
55
58
  an_instance_of(Gecode::Raw::BoolVar), Gecode::Raw::ICL_DEF)
56
- Gecode::Raw.should_receive(:rel).once.with(@model.active_space,
59
+ Gecode::Raw.should_receive(:rel).once.with(
60
+ an_instance_of(Gecode::Raw::Space),
57
61
  an_instance_of(Gecode::Raw::IntVar), Gecode::Raw::IRT_EQ, 2,
58
62
  an_instance_of(Gecode::Raw::BoolVar), Gecode::Raw::ICL_DEF)
59
63
  (@x.must > 0) & (@y.must == 2)
@@ -14,14 +14,17 @@ describe Gecode::Constraints::Set::Domain do
14
14
  @singleton = 0
15
15
 
16
16
  @expect = lambda do |relation_type, rhs, reif_var, negated|
17
- if reif_var.nil? and !negated
18
- Gecode::Raw.should_receive(:dom).once.with(@model.active_space,
19
- @set.bind, relation_type, *expect_constant_set(rhs))
20
- else
21
- params = [@model.active_space, @set.bind, relation_type]
22
- params << expect_constant_set(rhs)
23
- params << an_instance_of(Gecode::Raw::BoolVar)
24
- Gecode::Raw.should_receive(:dom).once.with(*params.flatten)
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
+ @set.bind, relation_type, *expect_constant_set(rhs))
22
+ else
23
+ params = [@model.active_space, @set.bind, relation_type]
24
+ params << expect_constant_set(rhs)
25
+ params << an_instance_of(Gecode::Raw::BoolVar)
26
+ Gecode::Raw.should_receive(:dom).once.with(*params.flatten)
27
+ end
25
28
  end
26
29
  end
27
30
 
@@ -91,14 +94,17 @@ describe Gecode::Constraints::Set::Domain, ' (equality)' do
91
94
  @singleton = 0
92
95
 
93
96
  @expect = lambda do |relation_type, rhs, reif_var|
94
- if reif_var.nil?
95
- Gecode::Raw.should_receive(:dom).once.with(@model.active_space,
96
- @set.bind, relation_type, *expect_constant_set(rhs))
97
- else
98
- params = [@model.active_space, @set.bind, relation_type]
99
- params << expect_constant_set(rhs)
100
- params << an_instance_of(Gecode::Raw::BoolVar)
101
- Gecode::Raw.should_receive(:dom).once.with(*params.flatten)
97
+ @model.allow_space_access do
98
+ if reif_var.nil?
99
+ Gecode::Raw.should_receive(:dom).once.with(
100
+ an_instance_of(Gecode::Raw::Space),
101
+ @set.bind, relation_type, *expect_constant_set(rhs))
102
+ else
103
+ params = [@model.active_space, @set.bind, relation_type]
104
+ params << expect_constant_set(rhs)
105
+ params << an_instance_of(Gecode::Raw::BoolVar)
106
+ Gecode::Raw.should_receive(:dom).once.with(*params.flatten)
107
+ end
102
108
  end
103
109
  end
104
110
 
@@ -152,8 +158,8 @@ describe Gecode::Constraints::Set::Domain, ' (equality)' do
152
158
  @set.must == @singleton
153
159
  @model.solve!
154
160
  @set.should be_assigned
155
- @set.should include(@singleton)
156
- @set.val_size.should == 1
161
+ @set.value.should include(@singleton)
162
+ @set.value.size.should == 1
157
163
  end
158
164
 
159
165
  it 'should constrain the domain with inequality' do
@@ -10,13 +10,17 @@ describe Gecode::Constraints::Set::Relation do
10
10
  @set2 = @model.set_var([1], 0..3)
11
11
 
12
12
  @expect = lambda do |relation_type, rhs, reif_var, negated|
13
- if reif_var.nil? and !negated
14
- Gecode::Raw.should_receive(:rel).once.with(@model.active_space,
15
- @set.bind, relation_type, @set2.bind)
16
- else
17
- Gecode::Raw.should_receive(:rel).once.with(@model.active_space,
18
- @set.bind, relation_type, @set2.bind,
19
- an_instance_of(Gecode::Raw::BoolVar))
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
+ @set.bind, relation_type, @set2.bind)
18
+ else
19
+ Gecode::Raw.should_receive(:rel).once.with(
20
+ an_instance_of(Gecode::Raw::Space),
21
+ @set.bind, relation_type, @set2.bind,
22
+ an_instance_of(Gecode::Raw::BoolVar))
23
+ end
20
24
  end
21
25
  end
22
26
 
@@ -57,13 +61,17 @@ describe Gecode::Constraints::Set::Relation, ' (equality)' do
57
61
  @set2 = @model.set_var([1], 0..1)
58
62
 
59
63
  @expect = lambda do |relation_type, rhs, reif_var|
60
- if reif_var.nil?
61
- Gecode::Raw.should_receive(:rel).once.with(@model.active_space,
62
- @set.bind, relation_type, @set2.bind)
63
- else
64
- Gecode::Raw.should_receive(:rel).once.with(@model.active_space,
65
- @set.bind, relation_type, @set2.bind,
66
- an_instance_of(Gecode::Raw::BoolVar))
64
+ @model.allow_space_access do
65
+ if reif_var.nil?
66
+ Gecode::Raw.should_receive(:rel).once.with(
67
+ an_instance_of(Gecode::Raw::Space),
68
+ @set.bind, relation_type, @set2.bind)
69
+ else
70
+ Gecode::Raw.should_receive(:rel).once.with(
71
+ an_instance_of(Gecode::Raw::Space),
72
+ @set.bind, relation_type, @set2.bind,
73
+ an_instance_of(Gecode::Raw::BoolVar))
74
+ end
67
75
  end
68
76
  end
69
77
 
@@ -119,12 +127,19 @@ describe Gecode::Constraints::Set::Relation, ' (elements)' do
119
127
  @int_constant = 2
120
128
  @model.branch_on @model.wrap_enum([@set])
121
129
  @expect = lambda do |relation_type, rhs|
122
- if rhs.kind_of? Fixnum
123
- rhs = an_instance_of(Gecode::Raw::IntVar)
130
+ @model.allow_space_access do
131
+ if rhs.kind_of? Fixnum
132
+ rhs = an_instance_of(Gecode::Raw::IntVar)
133
+ end
134
+ rhs = rhs.bind if rhs.respond_to? :bind
135
+ Gecode::Raw.should_receive(:rel).once.with(
136
+ an_instance_of(Gecode::Raw::Space),
137
+ @set.bind, relation_type, rhs)
124
138
  end
125
- rhs = rhs.bind if rhs.respond_to? :bind
126
- Gecode::Raw.should_receive(:rel).once.with(@model.active_space,
127
- @set.bind, relation_type, rhs)
139
+ end
140
+
141
+ @invoke_options = lambda do |hash|
142
+ @set.elements.must_be.equal_to(@int_var, hash)
128
143
  end
129
144
 
130
145
  @invoke_options = lambda do |hash|
@@ -165,16 +180,16 @@ describe Gecode::Constraints::Set::Relation, ' (elements)' do
165
180
  @int_var.must == 0
166
181
  @model.solve!
167
182
  @set.should be_assigned
168
- @set.should include(0)
169
- @set.should_not include(1,2)
183
+ @set.value.should include(0)
184
+ @set.value.should_not include(1,2)
170
185
  end
171
186
 
172
187
  it 'should constrain the elements of the set (constant parameter)' do
173
188
  @set.elements.must <= 0
174
189
  @model.solve!
175
190
  @set.should be_assigned
176
- @set.should include(0)
177
- @set.should_not include(1,2)
191
+ @set.value.should include(0)
192
+ @set.value.should_not include(1,2)
178
193
  end
179
194
 
180
195
  it_should_behave_like 'non-reifiable set constraint'
@@ -31,12 +31,12 @@ describe Gecode::Constraints::IntEnum::Sort, ' (without :as and :order)' do
31
31
  @expect_options = lambda do |strength, reif_var|
32
32
  if reif_var.nil?
33
33
  Gecode::Raw.should_receive(:rel).exactly(@vars.size - 1).times.with(
34
- @model.active_space,
34
+ an_instance_of(Gecode::Raw::Space),
35
35
  an_instance_of(Gecode::Raw::IntVar), Gecode::Raw::IRT_LQ,
36
36
  an_instance_of(Gecode::Raw::IntVar), strength)
37
37
  else
38
38
  Gecode::Raw.should_receive(:rel).exactly(@vars.size - 1).times.with(
39
- @model.active_space,
39
+ an_instance_of(Gecode::Raw::Space),
40
40
  an_instance_of(Gecode::Raw::IntVar), Gecode::Raw::IRT_LQ,
41
41
  an_instance_of(Gecode::Raw::IntVar),
42
42
  an_instance_of(Gecode::Raw::BoolVar), strength)
@@ -51,14 +51,14 @@ describe Gecode::Constraints::IntEnum::Sort, ' (without :as and :order)' do
51
51
 
52
52
  it 'should constraint variables to be sorted' do
53
53
  @vars.must_be.sorted
54
- values = @model.solve!.vars.map{ |x| x.val }
54
+ values = @model.solve!.vars.values
55
55
  values.should == values.sort
56
56
  end
57
57
 
58
58
  it 'should allow negation' do
59
59
  @vars.must_not_be.sorted
60
60
  @model.solve!
61
- values = @vars.map{ |x| x.val }
61
+ values = @vars.values
62
62
  values.should_not == values.sort
63
63
  end
64
64
 
@@ -79,7 +79,8 @@ describe Gecode::Constraints::IntEnum::Sort, ' (with :as)' do
79
79
  @model.solve!
80
80
  end
81
81
  @expect_options = lambda do |strength, reif_var|
82
- Gecode::Raw.should_receive(:sortedness).once.with(@model.active_space,
82
+ Gecode::Raw.should_receive(:sortedness).once.with(
83
+ an_instance_of(Gecode::Raw::Space),
83
84
  an_instance_of(Gecode::Raw::IntVarArray),
84
85
  an_instance_of(Gecode::Raw::IntVarArray), strength)
85
86
  end
@@ -93,7 +94,7 @@ describe Gecode::Constraints::IntEnum::Sort, ' (with :as)' do
93
94
  it 'should constraint variables to be sorted' do
94
95
  @vars.must_be.sorted(:as => @sorted)
95
96
  @model.solve!
96
- values = @sorted.map{ |x| x.val }
97
+ values = @sorted.values
97
98
  values.should == values.sort
98
99
  end
99
100
 
@@ -124,7 +125,8 @@ describe Gecode::Constraints::IntEnum::Sort, ' (with :order)' do
124
125
  @model.solve!
125
126
  end
126
127
  @expect_options = lambda do |strength, reif_var|
127
- Gecode::Raw.should_receive(:sortedness).once.with(@model.active_space,
128
+ Gecode::Raw.should_receive(:sortedness).once.with(
129
+ an_instance_of(Gecode::Raw::Space),
128
130
  an_instance_of(Gecode::Raw::IntVarArray),
129
131
  an_instance_of(Gecode::Raw::IntVarArray),
130
132
  an_instance_of(Gecode::Raw::IntVarArray), strength)
@@ -145,10 +147,10 @@ describe Gecode::Constraints::IntEnum::Sort, ' (with :order)' do
145
147
  it 'should constraint variables to be sorted with the specified indices' do
146
148
  @vars.must_be.sorted(:as => @sorted, :order => @indices)
147
149
  @model.solve!
148
- sorted_values = @sorted.map{ |x| x.val }
150
+ sorted_values = @sorted.values
149
151
  sorted_values.should == sorted_values.sort
150
- expected_indices = @vars.map{ |v| sorted_values.index(v.val) }
151
- @indices.map{ |i| i.val }.should == expected_indices
152
+ expected_indices = @vars.map{ |v| sorted_values.index(v.value) }
153
+ @indices.values.should == expected_indices
152
154
  end
153
155
 
154
156
  it 'should not allow targets that are not int var enums' do
@@ -68,7 +68,9 @@ describe Gecode::IntEnumMethods do
68
68
  end
69
69
 
70
70
  it 'should convert to an int var array' do
71
- @int_enum.to_int_var_array.should be_kind_of(Gecode::Raw::IntVarArray)
71
+ @model.allow_space_access do
72
+ @int_enum.to_int_var_array.should be_kind_of(Gecode::Raw::IntVarArray)
73
+ end
72
74
  end
73
75
 
74
76
  it 'should compute the smallest domain range' do
@@ -84,7 +86,9 @@ describe Gecode::BoolEnumMethods do
84
86
  end
85
87
 
86
88
  it 'should convert to a bool var array' do
87
- @bool_enum.to_bool_var_array.should be_kind_of(Gecode::Raw::BoolVarArray)
89
+ @model.allow_space_access do
90
+ @bool_enum.to_bool_var_array.should be_kind_of(Gecode::Raw::BoolVarArray)
91
+ end
88
92
  end
89
93
  end
90
94
 
@@ -95,7 +99,9 @@ describe Gecode::SetEnumMethods do
95
99
  end
96
100
 
97
101
  it 'should convert to a set var array' do
98
- @set_enum.to_set_var_array.should be_kind_of(Gecode::Raw::SetVarArray)
102
+ @model.allow_space_access do
103
+ @set_enum.to_set_var_array.should be_kind_of(Gecode::Raw::SetVarArray)
104
+ end
99
105
  end
100
106
  end
101
107
 
data/specs/int_var.rb CHANGED
@@ -15,13 +15,13 @@ describe 'non-empty int variable', :shared => true do
15
15
 
16
16
  it 'should contain every element in its domain' do
17
17
  @domain.each do |i|
18
- @var.should be_in(i)
18
+ @var.should include(i)
19
19
  end
20
20
  end
21
21
 
22
22
  it 'should not contain elements outside its domain' do
23
- @var.should_not be_in(@domain.min - 1)
24
- @var.should_not be_in(@domain.max + 1)
23
+ @var.should_not include(@domain.min - 1)
24
+ @var.should_not include(@domain.max + 1)
25
25
  end
26
26
 
27
27
  it 'should have a width equal to the domain width' do
@@ -54,6 +54,10 @@ describe Gecode::FreeIntVar, ' (with range domain of size > 1)' do
54
54
  it 'should have a range domain' do
55
55
  @var.should be_range
56
56
  end
57
+
58
+ it 'should raise error when trying to access assigned value' do
59
+ lambda{ @var.value }.should raise_error(RuntimeError)
60
+ end
57
61
  end
58
62
 
59
63
  describe Gecode::FreeIntVar, ' (defined with three-dot range)' do
@@ -85,7 +89,7 @@ describe Gecode::FreeIntVar, ' (with non-range domain of size > 1)' do
85
89
  end
86
90
 
87
91
  it 'should not contain the domain\'s holes' do
88
- @var.should_not be_in(0)
92
+ @var.should_not include(0)
89
93
  end
90
94
  end
91
95
 
data/specs/logging.rb ADDED
@@ -0,0 +1,24 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe Gecode::LoggingLayer do
4
+ before do
5
+ # Enable logging.
6
+ Gecode.instance_eval{ remove_const(:Raw) }
7
+ Gecode.const_set(:Raw, Gecode::LoggingLayer)
8
+ end
9
+
10
+ after do
11
+ # Disable logging.
12
+ Gecode.instance_eval{ remove_const(:Raw) }
13
+ Gecode.const_set(:Raw, GecodeRaw)
14
+ end
15
+
16
+ it "shouldn't interfere with calls through Gecode::Raw" do
17
+ lambda do
18
+ model = Gecode::Model.new
19
+ int_var = model.int_var(0..9)
20
+ int_var.must >= 5
21
+ model.solve!
22
+ end.should_not raise_error
23
+ end
24
+ end
data/specs/model.rb CHANGED
@@ -116,13 +116,13 @@ describe Gecode::Model, ' (set creation)' do
116
116
 
117
117
  it 'should allow the creation of set variables with specified lower cardinality bound' do
118
118
  @model.set_var(@glb_range, @lub_range,
119
- @lower_card).card_min.should >= @lower_card
119
+ @lower_card).cardinality.begin.should >= @lower_card
120
120
  end
121
121
 
122
122
  it 'should allow the creation of set variables with specified cardinality range' do
123
123
  var = @model.set_var(@glb_range, @lub_range, @lower_card..@upper_card)
124
- var.card_max.should <= @upper_card
125
- var.card_min.should >= @lower_card
124
+ var.cardinality.end.should <= @upper_card
125
+ var.cardinality.begin.should >= @lower_card
126
126
  end
127
127
 
128
128
  it 'should allow the creation of arrays of set variables' do
@@ -130,8 +130,8 @@ describe Gecode::Model, ' (set creation)' do
130
130
  arr.size.should == 3
131
131
  arr.each do |var|
132
132
  var.should have_bounds(@glb_enum, @lub_enum)
133
- var.card_max.should <= @upper_card
134
- var.card_min.should >= @lower_card
133
+ var.cardinality.end.should <= @upper_card
134
+ var.cardinality.begin.should >= @lower_card
135
135
  end
136
136
  end
137
137
 
@@ -142,8 +142,8 @@ describe Gecode::Model, ' (set creation)' do
142
142
  matrix.column_size.should == 5
143
143
  matrix.each do |var|
144
144
  var.should have_bounds(@glb_enum, @lub_enum)
145
- var.card_max.should <= @upper_card
146
- var.card_min.should >= @lower_card
145
+ var.cardinality.end.should <= @upper_card
146
+ var.cardinality.begin.should >= @lower_card
147
147
  end
148
148
  end
149
149
 
@@ -161,4 +161,22 @@ describe Gecode::Model, ' (set creation)' do
161
161
  lambda{ @model.set_var(@lub_enum, @glb_enum).should }.should raise_error(
162
162
  ArgumentError)
163
163
  end
164
+ end
165
+
166
+ describe Gecode::Model, ' (space access restriction)' do
167
+ before do
168
+ @model = Gecode::Model.new
169
+ end
170
+
171
+ it 'should raise error if not allowed to access space' do
172
+ lambda{ @model.active_space }.should raise_error(RuntimeError)
173
+ end
174
+
175
+ it 'should not raise error because of space restriction if allowed to access space' do
176
+ lambda do
177
+ @model.allow_space_access do
178
+ @model.active_space
179
+ end
180
+ end.should_not raise_error(RuntimeError)
181
+ end
164
182
  end
data/specs/search.rb CHANGED
@@ -19,6 +19,20 @@ class SampleProblem < Gecode::Model
19
19
  end
20
20
  end
21
21
 
22
+ class SampleOptimizationProblem < Gecode::Model
23
+ attr :x
24
+ attr :y
25
+ attr :z
26
+
27
+ def initialize
28
+ @x,@y = int_var_array(2, 0..5)
29
+ @z = int_var(0..25)
30
+ (@x * @y).must == @z
31
+
32
+ branch_on wrap_enum([@x, @y]), :variable => :smallest_size, :value => :min
33
+ end
34
+ end
35
+
22
36
  describe Gecode::Model, ' (with multiple solutions)' do
23
37
  before do
24
38
  @domain = 0..3
@@ -45,7 +59,7 @@ describe Gecode::Model, ' (with multiple solutions)' do
45
59
  it 'should pass every solution to #each_solution' do
46
60
  solutions = []
47
61
  @model.each_solution do |s|
48
- solutions << s.var.val
62
+ solutions << s.var.value
49
63
  end
50
64
  Set.new(solutions).should == Set.new([2,3])
51
65
  end
@@ -143,6 +157,10 @@ describe Gecode::Model, ' (without solution)' do
143
157
  it 'should return nil when calling #solve!' do
144
158
  @model.solve!.should be_nil
145
159
  end
160
+
161
+ it 'should return nil when calling #optimize!' do
162
+ @model.optimize!{}.should be_nil
163
+ end
146
164
  end
147
165
 
148
166
  describe Gecode::Model, ' (without constraints)' do
@@ -154,4 +172,26 @@ describe Gecode::Model, ' (without constraints)' do
154
172
  it 'should produce a solution' do
155
173
  @model.solve!.should_not be_nil
156
174
  end
175
+ end
176
+
177
+ describe Gecode::Model, '(optimization search)' do
178
+ before do
179
+ @model = SampleOptimizationProblem.new
180
+ end
181
+
182
+ it 'should optimize the solution' do
183
+ solution = @model.optimize! do |model, best_so_far|
184
+ model.z.must > best_so_far.z.value
185
+ end
186
+ solution.should_not be_nil
187
+ solution.x.value.should == 5
188
+ solution.y.value.should == 5
189
+ solution.z.value.should == 25
190
+ end
191
+
192
+ it 'should raise error if no constrain proc has been defined' do
193
+ lambda do
194
+ Gecode::Model.constrain(nil, nil)
195
+ end.should raise_error(NotImplementedError)
196
+ end
157
197
  end