Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
lib/gecoder/interface/branch.rb | 163 | 80 | 100.00%
|
100.00%
|
Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.
1 module Gecode |
2 module Mixin |
3 # Specifies which variables that should be branched on (given as an |
4 # enum of operands or as a single operand). One can optionally |
5 # also select which of the variables that should be used first with |
6 # the :variable option and which value in that variable's domain |
7 # that should be used with the :value option. If nothing is |
8 # specified then :variable uses :none and value uses :min. |
9 # |
10 # The following values can be used with :variable for integer and |
11 # boolean enums: |
12 # [:none] The first unassigned variable. |
13 # [:smallest_min] The one with the smallest minimum. |
14 # [:largest_min] The one with the largest minimum. |
15 # [:smallest_max] The one with the smallest maximum. |
16 # [:largest_max] The one with the largest maximum. |
17 # [:smallest_size] The one with the smallest size. |
18 # [:largest_size] The one with the larges size. |
19 # [:smallest_degree] The one with the smallest degree. The degree of a |
20 # variable is defined as the number of dependant |
21 # propagators. In case of ties, choose the variable |
22 # with smallest domain. |
23 # [:largest_degree] The one with the largest degree. The degree of a |
24 # variable is defined as the number of dependant |
25 # propagators. In case of ties, choose the variable |
26 # with smallest domain. |
27 # [:smallest_min_regret] The one with the smallest min-regret. The |
28 # min-regret of a variable is the difference between |
29 # the smallest and second-smallest value still in |
30 # the domain. |
31 # [:largest_min_regret] The one with the largest min-regret. The |
32 # min-regret of a variable is the difference between |
33 # the smallest and second-smallest value still in |
34 # the domain. |
35 # [:smallest_max_regret] The one with the smallest max-regret The |
36 # max-regret of a variable is the difference between |
37 # the largest and second-largest value still in |
38 # the domain. |
39 # [:largest_max_regret] The one with the largest max-regret. The |
40 # max-regret of a variable is the difference between |
41 # the largest and second-largest value still in |
42 # the domain. |
43 # |
44 # The following values can be used with :value for integer and boolean |
45 # enums: |
46 # [:min] Selects the smallest value. |
47 # [:med] Select the median value. |
48 # [:max] Selects the largest vale |
49 # [:split_min] Selects the lower half of the domain. |
50 # [:split_max] Selects the upper half of the domain. |
51 # |
52 # The following values can be used with :variable for set enums: |
53 # [:none] The first unassigned set. |
54 # [:smallest_cardinality] The one with the smallest cardinality. |
55 # [:largest_cardinality] The one with the largest cardinality. |
56 # [:smallest_unknown] The one with the smallest number of unknown |
57 # elements |
58 # [:largest_unknown] The one with the largest number of unknown |
59 # elements |
60 # |
61 # The following values can be used with :value set enums: |
62 # [:min] Selects the smallest value in the unknown part of the set. |
63 # [:max] Selects the largest value in the unknown part of the set. |
64 def branch_on(variables, options = {}) |
65 if variables.respond_to?(:to_int_var) or |
66 variables.respond_to?(:to_bool_var) or |
67 variables.respond_to?(:to_set_var) |
68 variables = wrap_enum [variables] |
69 end |
70 |
71 if variables.respond_to? :to_int_enum |
72 add_branch(variables.to_int_enum, options, |
73 Constants::BRANCH_INT_VAR_CONSTANTS, |
74 Constants::BRANCH_INT_VALUE_CONSTANTS) |
75 elsif variables.respond_to? :to_bool_enum |
76 add_branch(variables.to_bool_enum, options, |
77 Constants::BRANCH_INT_VAR_CONSTANTS, |
78 Constants::BRANCH_INT_VALUE_CONSTANTS) |
79 elsif variables.respond_to? :to_set_enum |
80 add_branch(variables.to_set_enum, options, |
81 Constants::BRANCH_SET_VAR_CONSTANTS, |
82 Constants::BRANCH_SET_VALUE_CONSTANTS) |
83 else |
84 raise TypeError, "Unknown type of variable enum #{variables.class}." |
85 end |
86 end |
87 |
88 private |
89 |
90 # This is a hack to get RDoc to ignore the constants. |
91 module Constants #:nodoc: |
92 # Maps the names of the supported variable branch strategies for |
93 # integer and booleans to the corresponding constant in Gecode. |
94 BRANCH_INT_VAR_CONSTANTS = { |
95 :none => Gecode::Raw::INT_VAR_NONE, |
96 :smallest_min => Gecode::Raw::INT_VAR_MIN_MIN, |
97 :largest_min => Gecode::Raw::INT_VAR_MIN_MAX, |
98 :smallest_max => Gecode::Raw::INT_VAR_MAX_MIN, |
99 :largest_max => Gecode::Raw::INT_VAR_MAX_MAX, |
100 :smallest_size => Gecode::Raw::INT_VAR_SIZE_MIN, |
101 :largest_size => Gecode::Raw::INT_VAR_SIZE_MAX, |
102 :smallest_degree => Gecode::Raw::INT_VAR_DEGREE_MIN, |
103 :largest_degree => Gecode::Raw::INT_VAR_DEGREE_MAX, |
104 :smallest_min_regret => Gecode::Raw::INT_VAR_REGRET_MIN_MIN, |
105 :largest_min_regret => Gecode::Raw::INT_VAR_REGRET_MIN_MAX, |
106 :smallest_max_regret => Gecode::Raw::INT_VAR_REGRET_MAX_MIN, |
107 :largest_max_regret => Gecode::Raw::INT_VAR_REGRET_MAX_MAX |
108 } |
109 # Maps the names of the supported variable branch strategies for sets to |
110 # the corresponding constant in Gecode. |
111 BRANCH_SET_VAR_CONSTANTS = { #:nodoc: |
112 :none => Gecode::Raw::SET_VAR_NONE, |
113 :smallest_cardinality => Gecode::Raw::SET_VAR_MIN_CARD, |
114 :largest_cardinality => Gecode::Raw::SET_VAR_MAX_CARD, |
115 :smallest_unknown => Gecode::Raw::SET_VAR_MIN_UNKNOWN_ELEM, |
116 :largest_unknown => Gecode::Raw::SET_VAR_MAX_UNKNOWN_ELEM |
117 } |
118 |
119 # Maps the names of the supported value branch strategies for integers and |
120 # booleans to the corresponding constant in Gecode. |
121 BRANCH_INT_VALUE_CONSTANTS = { #:nodoc: |
122 :min => Gecode::Raw::INT_VAL_MIN, |
123 :med => Gecode::Raw::INT_VAL_MED, |
124 :max => Gecode::Raw::INT_VAL_MAX, |
125 :split_min => Gecode::Raw::INT_VAL_SPLIT_MIN, |
126 :split_max => Gecode::Raw::INT_VAL_SPLIT_MAX |
127 } |
128 # Maps the names of the supported value branch strategies for sets to the |
129 # corresponding constant in Gecode. |
130 BRANCH_SET_VALUE_CONSTANTS = { #:nodoc: |
131 :min => Gecode::Raw::SET_VAL_MIN, |
132 :max => Gecode::Raw::SET_VAL_MAX |
133 } |
134 end |
135 |
136 # Adds a branching selection for the specified variables with the specified |
137 # options. The hashes are used to decode the options into Gecode's |
138 # constants. |
139 def add_branch(variables, options, branch_var_hash, branch_value_hash) |
140 # Extract optional arguments. |
141 var_strat = options.delete(:variable) || :none |
142 val_strat = options.delete(:value) || :min |
143 |
144 # Check that the options are correct. |
145 unless options.empty? |
146 raise ArgumentError, 'Unknown branching option given: ' + |
147 options.keys.join(', ') |
148 end |
149 unless branch_var_hash.include? var_strat |
150 raise ArgumentError, "Unknown variable selection strategy: #{var_strat}" |
151 end |
152 unless branch_value_hash.include? val_strat |
153 raise ArgumentError, "Unknown value selection strategy: #{val_strat}" |
154 end |
155 |
156 # Add the branching as a gecode interaction. |
157 add_interaction do |
158 Gecode::Raw.branch(active_space, variables.bind_array, |
159 branch_var_hash[var_strat], branch_value_hash[val_strat]) |
160 end |
161 end |
162 end |
163 end |
Generated on Thu Jan 08 13:27:03 +0100 2015 with rcov 1.0.0