Class: Gecode::Model
- Inherits:
-
Object
- Object
- Gecode::Model
- Defined in:
- lib/gecoder/interface/model.rb,
lib/gecoder/interface/branch.rb,
lib/gecoder/interface/search.rb,
lib/gecoder/interface/enum_wrapper.rb
Overview
Model is the base class that all models must inherit from.
Formulating problems
Models are used to formulate the problems that Gecode should solve. Below is an example of a model that formulates the problem of finding a solution to the following equation system.
Equation system:
x + y = z
x = y - 3
0 <= x,y,z <= 9
Model:
class EquationProblem < Gecode::Model
attr :vars
def initialize
x, y, z = @vars = int_var_array(3, 0..9)
(x + y).must == z
x.must == y - 3
branch_on @vars
end
end
A model typically consists of three main parts:
- Variables
-
Variables specify how to view the problem. A solution is an assignment of the variables. In the example above we created an array of three integer variables with domains 0..9 and gave it the name
variables.There are three types of variables: integer variables (Gecode::IntVar, can be assigned one of many possible integer values), boolean variables (Gecode::BoolVar, can be assigned either true or false) and set variables (Gecode::SetVar, can be assigned a set of integers). Variables of the different types are constructed using #int_var, #int_var_array, #int_var_matrix, #bool_var, #bool_var_array, #bool_var_matrix, #set_var, #set_var_array and #set_var_matrix .
The various variables all have the functionality of Operand and have many properties depending on their type. For instance integer variables have the properties defined in Gecode::Int::IntOperand and enumerations of integer variables (such as the array
variableswe used) have the properties defined inGecode::IntEnum::IntEnumOperand .
- Constraints
-
Constraints are placed on the variables to ensure that a valid assignment of the variables must also be a solution. In the example above we constrained the variables so that all equations were satisfied (which is exactly when we have found a solution).
The various constraints that can be placed on the various kinds of operands are found in the respective constraint receivers. For instance, the constraints that can be placed on integer operands are found in Gecode::Int::IntConstraintReceiver and the constraints that can be placed on enumerations of integer operands are found in Gecode::IntEnum::IntEnumConstraintReceiver .
- Branching
-
“branch_on variables” in the example tells Gecode that it should explore the search space until it has assigned
variables(or exhausted the search space). It also tells Gecode in what order the search space should be explore, which can have a big effect on the search performance. See #branch_on for details.
Finding solutions
Solutions to a formulated problem are found are found by using methods such as #solve!, #solution, #each_solution . If one wants to find a solution that optimizes a certain quantity (i.e. maximizes a certain variable) then one should have a look at #maximize!, #minimize! and #optimize! .
The first solution to the example above could for instance be found using
puts EquationProblem.new.solve!.vars.values.join(', ')
which would find the first solution to the problem, access the assigned values of variables and print them (in order x, y, z).
Shorter ways of formulating problems
Problems can also be formulated without defining a new class by using Gecode#solve et al.
Additionally one can use “foo_is_an …” to create an accessor of name foo, without having to use instance variables. The above problem becomes
class EquationProblem < Gecode::Model
def initialize
x, y, z = vars_is_an int_var_array(3, 0..9)
(x + y).must == z
x.must == y - 3
branch_on vars
end
end
Defined Under Namespace
Modules: Constants
Constant Summary collapse
- MAX_INT =
The largest integer allowed in the domain of an integer variable.
Gecode::Raw::IntLimits::MAX
- MIN_INT =
The smallest integer allowed in the domain of an integer variable.
Gecode::Raw::IntLimits::MIN
- SET_MAX_INT =
The largest integer allowed in the domain of a set variable.
Gecode::Raw::SetLimits::MAX
- SET_MIN_INT =
The smallest integer allowed in the domain of a set variable.
Gecode::Raw::SetLimits::MIN
- LARGEST_INT_DOMAIN =
The largest possible domain for an integer variable.
MIN_INT..MAX_INT
- NON_NEGATIVE_INT_DOMAIN =
The largest possible domain, without negative integers, for an integer variable.
0..MAX_INT
- LARGEST_SET_BOUND =
The largest possible bound for a set variable.
SET_MIN_INT..SET_MAX_INT
Class Method Summary collapse
-
.constrain(home, best) ⇒ Object
Called by spaces when they want to constrain as part of BAB-search.
-
.constrain_proc=(proc) ⇒ Object
Sets the proc that should be used to handle constrain requests.
Instance Method Summary collapse
-
#active_space ⇒ Object
Retrieves the currently used space.
-
#add_constraint(constraint) ⇒ Object
Adds the specified constraint to the model.
-
#add_interaction(&block) ⇒ Object
Adds a block containing something that interacts with Gecode to a queue where it is potentially executed.
-
#allow_space_access(&block) ⇒ Object
Allows the model’s active space to be accessed while the block is executed.
-
#bool_var ⇒ Object
Creates a new boolean variable.
-
#bool_var_array(count) ⇒ Object
Creates an array containing the specified number of boolean variables.
-
#bool_var_matrix(row_count, col_count) ⇒ Object
Creates a matrix containing the specified number rows and columns of boolean variables.
-
#branch_on(variables, options = {}) ⇒ Object
Specifies which variables that should be branched on (given as an enum of operands or as a single operand).
-
#each_solution(&block) ⇒ Object
Yields each solution that the model has.
-
#int_var(domain = LARGEST_INT_DOMAIN) ⇒ Object
Creates a new integer variable with the specified domain.
-
#int_var_array(count, domain = LARGEST_INT_DOMAIN) ⇒ Object
Creates an array containing the specified number of integer variables with the specified domain.
-
#int_var_matrix(row_count, col_count, domain = LARGEST_INT_DOMAIN) ⇒ Object
Creates a matrix containing the specified number rows and columns of integer variables with the specified domain.
-
#maximize!(var) ⇒ Object
Finds the solution that maximizes a given integer variable.
-
#method_missing(name_symbol, *args) ⇒ Object
Wraps method missing to handle #foo_is_a and #foo_is_an .
-
#minimize!(var) ⇒ Object
Finds the solution that minimizes a given integer variable.
-
#optimize!(&block) ⇒ Object
Finds the optimal solution.
-
#reset! ⇒ Object
Returns to the original state, before any search was made (but propagation might have been performed).
-
#search_stats ⇒ Object
Returns search statistics providing various information from Gecode about the search that resulted in the model’s current variable state.
-
#set_var(glb_domain = [], lub_domain = LARGEST_SET_BOUND, cardinality_range = nil) ⇒ Object
Creates a set variable with the specified domain for greatest lower bound and least upper bound (specified as either a fixnum, range or enum).
-
#set_var_array(count, glb_domain = [], lub_domain = LARGEST_SET_BOUND, cardinality_range = nil) ⇒ Object
Creates an array containing the specified number of set variables.
-
#set_var_matrix(row_count, col_count, glb_domain = [], lub_domain = LARGEST_SET_BOUND, cardinality_range = nil) ⇒ Object
Creates a matrix containing the specified number of rows and columns filled with set variables.
-
#solution(&block) ⇒ Object
Yields the first solution (if any) to the block.
-
#solve! ⇒ Object
Finds the first solution to the modelled problem and updates the variables to that solution.
-
#track_variable(variable) ⇒ Object
Starts tracking a variable that depends on the space.
-
#wrap_enum(enum) ⇒ Object
Wraps a custom enumerable so that constraints can be specified using it.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name_symbol, *args) ⇒ Object
Wraps method missing to handle #foo_is_a and #foo_is_an .
“<variable_name>_is_a <variable>” or “<variable_name>_is_an <variable>”, replacing “<variable_name>” with the variable’s name and “<variable>” with the variable, adds an instance variable and accessor with the specified name.
The method also returns the variable given.
Example
# Add an instance variable and accessor named "foo" that return
# the integer variable.
foo_is_an int_var(0..9)
# Add an instance variable and accessor named "bar" that return
# the boolean variable array.
bool_var_array(2)
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/gecoder/interface/model.rb', line 287 def method_missing(name_symbol, *args) name = name_symbol.to_s if name =~ /._is_an?$/ name.sub!(/_is_an?$/, '') unless args.size == 1 raise ArgumentError, "Wrong number of argmuments (#{args.size} for 1)." end if respond_to? name raise ArgumentError, "Method with name #{name} already exists." end if instance_variable_defined? "@#{name}" raise ArgumentError, "Instance variable with name @#{name} already exists." end # We use the meta class to avoid defining the variable in all # other instances of the class. eval <<-"end_eval" @#{name} = args.first class <<self attr :#{name} end end_eval return args.first else super end end |
Class Method Details
.constrain(home, best) ⇒ Object
Called by spaces when they want to constrain as part of BAB-search.
173 174 175 176 177 178 179 |
# File 'lib/gecoder/interface/search.rb', line 173 def constrain(home, best) #:nodoc: if @constrain_proc.nil? raise NotImplementedError, 'Constrain method not implemented.' else @constrain_proc.call(home, best) end end |
.constrain_proc=(proc) ⇒ Object
Sets the proc that should be used to handle constrain requests.
168 169 170 |
# File 'lib/gecoder/interface/search.rb', line 168 def constrain_proc=(proc) #:nodoc: @constrain_proc = proc end |
Instance Method Details
#active_space ⇒ Object
Retrieves the currently used space. Calling this method is only allowed when sanctioned by the model beforehand, e.g. when the model asks a constraint to post itself. Otherwise an RuntimeError is raised.
The space returned by this method should never be stored, it should be rerequested from the model every time that it’s needed.
223 224 225 226 227 228 229 |
# File 'lib/gecoder/interface/model.rb', line 223 def active_space #:nodoc: unless @allow_space_access raise 'Space access is restricted and the permission to access the ' + 'space has not been given.' end selected_space end |
#add_constraint(constraint) ⇒ Object
Adds the specified constraint to the model. Returns the newly added constraint.
233 234 235 236 237 238 |
# File 'lib/gecoder/interface/model.rb', line 233 def add_constraint(constraint) #:nodoc: add_interaction do constraint.post end return constraint end |
#add_interaction(&block) ⇒ Object
Adds a block containing something that interacts with Gecode to a queue where it is potentially executed.
242 243 244 |
# File 'lib/gecoder/interface/model.rb', line 242 def add_interaction(&block) #:nodoc: gecode_interaction_queue << block end |
#allow_space_access(&block) ⇒ Object
Allows the model’s active space to be accessed while the block is executed. Don’t use this unless you know what you’re doing. Anything that the space is used for (such as bound variables) must be released before the block ends.
Returns the result of the block.
252 253 254 255 256 257 258 259 260 261 |
# File 'lib/gecoder/interface/model.rb', line 252 def allow_space_access(&block) #:nodoc: # We store the old value so that nested calls don't become a problem, i.e. # access is allowed as long as one call to this method is still on the # stack. old = @allow_space_access @allow_space_access = true res = yield @allow_space_access = old return res end |
#bool_var ⇒ Object
Creates a new boolean variable.
162 163 164 |
# File 'lib/gecoder/interface/model.rb', line 162 def bool_var BoolVar.new(self, variable_creation_space.new_bool_var) end |
#bool_var_array(count) ⇒ Object
Creates an array containing the specified number of boolean variables.
167 168 169 170 171 |
# File 'lib/gecoder/interface/model.rb', line 167 def bool_var_array(count) build_var_array(count) do BoolVar.new(self, variable_creation_space.new_bool_var) end end |
#bool_var_matrix(row_count, col_count) ⇒ Object
Creates a matrix containing the specified number rows and columns of boolean variables.
175 176 177 178 179 |
# File 'lib/gecoder/interface/model.rb', line 175 def bool_var_matrix(row_count, col_count) build_var_matrix(row_count, col_count) do BoolVar.new(self, variable_creation_space.new_bool_var) end end |
#branch_on(variables, options = {}) ⇒ Object
Specifies which variables that should be branched on (given as an enum of operands or as a single operand). One can optionally also select which of the variables that should be used first with the :variable option and which value in that variable’s domain that should be used with the :value option. If nothing is specified then :variable uses :none and value uses :min.
The following values can be used with :variable for integer and boolean enums:
- :none
-
The first unassigned variable.
- :smallest_min
-
The one with the smallest minimum.
- :largest_min
-
The one with the largest minimum.
- :smallest_max
-
The one with the smallest maximum.
- :largest_max
-
The one with the largest maximum.
- :smallest_size
-
The one with the smallest size.
- :largest_size
-
The one with the larges size.
- :smallest_degree
-
The one with the smallest degree. The degree of a variable is defined as the number of dependant propagators. In case of ties, choose the variable with smallest domain.
- :largest_degree
-
The one with the largest degree. The degree of a variable is defined as the number of dependant propagators. In case of ties, choose the variable with smallest domain.
- :smallest_min_regret
-
The one with the smallest min-regret. The min-regret of a variable is the difference between the smallest and second-smallest value still in the domain.
- :largest_min_regret
-
The one with the largest min-regret. The min-regret of a variable is the difference between the smallest and second-smallest value still in the domain.
- :smallest_max_regret
-
The one with the smallest max-regret The max-regret of a variable is the difference between the largest and second-largest value still in the domain.
- :largest_max_regret
-
The one with the largest max-regret. The max-regret of a variable is the difference between the largest and second-largest value still in the domain.
The following values can be used with :value for integer and boolean enums:
- :min
-
Selects the smallest value.
- :med
-
Select the median value.
- :max
-
Selects the largest vale
- :split_min
-
Selects the lower half of the domain.
- :split_max
-
Selects the upper half of the domain.
The following values can be used with :variable for set enums:
- :none
-
The first unassigned set.
- :smallest_cardinality
-
The one with the smallest cardinality.
- :largest_cardinality
-
The one with the largest cardinality.
- :smallest_unknown
-
The one with the smallest number of unknown elements
- :largest_unknown
-
The one with the largest number of unknown elements
The following values can be used with :value set enums:
- :min
-
Selects the smallest value in the unknown part of the set.
- :max
-
Selects the largest value in the unknown part of the set.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/gecoder/interface/branch.rb', line 64 def branch_on(variables, = {}) if variables.respond_to?(:to_int_var) or variables.respond_to?(:to_bool_var) or variables.respond_to?(:to_set_var) variables = wrap_enum [variables] end if variables.respond_to? :to_int_enum add_branch(variables.to_int_enum, , Constants::BRANCH_INT_VAR_CONSTANTS, Constants::BRANCH_INT_VALUE_CONSTANTS) elsif variables.respond_to? :to_bool_enum add_branch(variables.to_bool_enum, , Constants::BRANCH_INT_VAR_CONSTANTS, Constants::BRANCH_INT_VALUE_CONSTANTS) elsif variables.respond_to? :to_set_enum add_branch(variables.to_set_enum, , Constants::BRANCH_SET_VAR_CONSTANTS, Constants::BRANCH_SET_VALUE_CONSTANTS) else raise TypeError, "Unknown type of variable enum #{variables.class}." end end |
#each_solution(&block) ⇒ Object
Yields each solution that the model has.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/gecoder/interface/search.rb', line 46 def each_solution(&block) dfs = dfs_engine next_solution = nil while not (next_solution = dfs.next).nil? self.active_space = next_solution @statistics = dfs.statistics yield self end self.reset! end |
#int_var(domain = LARGEST_INT_DOMAIN) ⇒ Object
Creates a new integer variable with the specified domain. The domain can either be a range, a single element, or an enumeration of elements. If no domain is specified then the largest possible domain is used.
134 135 136 137 |
# File 'lib/gecoder/interface/model.rb', line 134 def int_var(domain = LARGEST_INT_DOMAIN) args = domain_arguments(domain) IntVar.new(self, variable_creation_space.new_int_var(*args)) end |
#int_var_array(count, domain = LARGEST_INT_DOMAIN) ⇒ Object
Creates an array containing the specified number of integer variables with the specified domain. The domain can either be a range, a single element, or an enumeration of elements. If no domain is specified then the largest possible domain is used.
143 144 145 146 147 148 |
# File 'lib/gecoder/interface/model.rb', line 143 def int_var_array(count, domain = LARGEST_INT_DOMAIN) args = domain_arguments(domain) build_var_array(count) do IntVar.new(self, variable_creation_space.new_int_var(*args)) end end |
#int_var_matrix(row_count, col_count, domain = LARGEST_INT_DOMAIN) ⇒ Object
Creates a matrix containing the specified number rows and columns of integer variables with the specified domain. The domain can either be a range, a single element, or an enumeration of elements. If no domain is specified then the largest possible domain is used.
154 155 156 157 158 159 |
# File 'lib/gecoder/interface/model.rb', line 154 def int_var_matrix(row_count, col_count, domain = LARGEST_INT_DOMAIN) args = domain_arguments(domain) build_var_matrix(row_count, col_count) do IntVar.new(self, variable_creation_space.new_int_var(*args)) end end |
#maximize!(var) ⇒ Object
Finds the solution that maximizes a given integer variable. The name of the method that accesses the variable from the model should be given. To for instance maximize a variable named “profit”, that’s accessible through the model, one would use the following.
model.maximize! :profit
Raises Gecode::NoSolutionError if no solution can be found.
136 137 138 139 140 141 142 143 144 145 |
# File 'lib/gecoder/interface/search.rb', line 136 def maximize!(var) variable = self.method(var).call unless variable.kind_of? Gecode::IntVar raise ArgumentError.new("Expected integer variable, got #{variable.class}.") end optimize! do |model, best_so_far| model.method(var).call.must > best_so_far.method(var).call.value end end |
#minimize!(var) ⇒ Object
Finds the solution that minimizes a given integer variable. The name of the method that accesses the variable from the model should be given. To for instance minimize a variable named “cost”, that’s accessible through the model, one would use the following.
model.minimize! :cost
Raises Gecode::NoSolutionError if no solution can be found.
155 156 157 158 159 160 161 162 163 164 |
# File 'lib/gecoder/interface/search.rb', line 155 def minimize!(var) variable = self.method(var).call unless variable.kind_of? Gecode::IntVar raise ArgumentError.new("Expected integer variable, got #{variable.class}.") end optimize! do |model, best_so_far| model.method(var).call.must < best_so_far.method(var).call.value end end |
#optimize!(&block) ⇒ Object
Finds the optimal solution. Optimality is defined by the provided block which is given one parameter, a solution to the problem. The block should constrain the solution so that that only “better” solutions can be new solutions. For instance if one wants to optimize a variable named price (accessible from the model) to be as low as possible then one should write the following.
model.optimize! do |model, best_so_far|
model.price.must < best_so_far.price.val
end
Raises Gecode::NoSolutionError if no solution can be found.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/gecoder/interface/search.rb', line 90 def optimize!(&block) # Execute constraints. perform_queued_gecode_interactions # Set the method used for constrain calls by the BAB-search. Model.constrain_proc = lambda do |home_space, best_space| self.active_space = best_space @variable_creation_space = home_space yield(self, self) self.active_space = home_space @variable_creation_space = nil perform_queued_gecode_interactions end # Perform the search. = Gecode::Raw::Search::Options.new .c_d = Gecode::Raw::Search::Config::MINIMAL_DISTANCE .a_d = Gecode::Raw::Search::Config::ADAPTIVE_DISTANCE .stop = nil bab = Gecode::Raw::BAB.new(selected_space, ) result = nil previous_solution = nil until (previous_solution = bab.next).nil? result = previous_solution end @statistics = bab.statistics # Reset the method used constrain calls and return the result. Model.constrain_proc = nil raise Gecode::NoSolutionError if result.nil? # Switch to the result. self.active_space = result return self end |
#reset! ⇒ Object
Returns to the original state, before any search was made (but propagation might have been performed). Returns the reset model.
25 26 27 28 29 |
# File 'lib/gecoder/interface/search.rb', line 25 def reset! self.active_space = base_space @statistics = nil return self end |
#search_stats ⇒ Object
Returns search statistics providing various information from Gecode about the search that resulted in the model’s current variable state. If the model’s variables have not undergone any search then nil is returned. The statistics is a hash with the following keys:
- :propagations
-
The number of propagation steps performed.
- :failures
-
The number of failed nodes in the search tree.
- :clones
-
The number of clones created.
- :commits
-
The number of commit operations performed.
- :memory
-
The peak memory allocated to Gecode.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/gecoder/interface/search.rb', line 66 def search_stats return nil if @statistics.nil? return { :propagations => @statistics.propagate, :failures => @statistics.fail, :clones => @statistics.clone, :commits => @statistics.commit, :memory => @statistics.memory } end |
#set_var(glb_domain = [], lub_domain = LARGEST_SET_BOUND, cardinality_range = nil) ⇒ Object
Creates a set variable with the specified domain for greatest lower bound and least upper bound (specified as either a fixnum, range or enum). If no bounds are specified then the empty set is used as greatest lower bound and the largest possible set as least upper bound.
A range for the allowed cardinality of the set can also be specified, if none is specified, or nil is given, then the default range (anything) will be used. If only a single Fixnum is specified as cardinality_range then it’s used as lower bound.
190 191 192 193 194 |
# File 'lib/gecoder/interface/model.rb', line 190 def set_var(glb_domain = [], lub_domain = LARGEST_SET_BOUND, cardinality_range = nil) args = set_bounds_to_parameters(glb_domain, lub_domain, cardinality_range) SetVar.new(self, variable_creation_space.new_set_var(*args)) end |
#set_var_array(count, glb_domain = [], lub_domain = LARGEST_SET_BOUND, cardinality_range = nil) ⇒ Object
Creates an array containing the specified number of set variables. The parameters beyond count are the same as for #set_var .
198 199 200 201 202 203 204 |
# File 'lib/gecoder/interface/model.rb', line 198 def set_var_array(count, glb_domain = [], lub_domain = LARGEST_SET_BOUND, cardinality_range = nil) args = set_bounds_to_parameters(glb_domain, lub_domain, cardinality_range) build_var_array(count) do SetVar.new(self, variable_creation_space.new_set_var(*args)) end end |
#set_var_matrix(row_count, col_count, glb_domain = [], lub_domain = LARGEST_SET_BOUND, cardinality_range = nil) ⇒ Object
Creates a matrix containing the specified number of rows and columns filled with set variables. The parameters beyond row and column counts are the same as for #set_var .
209 210 211 212 213 214 215 |
# File 'lib/gecoder/interface/model.rb', line 209 def set_var_matrix(row_count, col_count, glb_domain = [], lub_domain = LARGEST_SET_BOUND, cardinality_range = nil) args = set_bounds_to_parameters(glb_domain, lub_domain, cardinality_range) build_var_matrix(row_count, col_count) do SetVar.new(self, variable_creation_space.new_set_var(*args)) end end |
#solution(&block) ⇒ Object
Yields the first solution (if any) to the block. If no solution is found then the block is not used. Returns the result of the block (nil in case the block wasn’t run).
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/gecoder/interface/search.rb', line 34 def solution(&block) begin solution = self.solve! res = yield solution self.reset! return res rescue Gecode::NoSolutionError return nil end end |
#solve! ⇒ Object
Finds the first solution to the modelled problem and updates the variables to that solution. The found solution is also returned. Raises Gecode::NoSolutionError if no solution can be found.
14 15 16 17 18 19 20 21 |
# File 'lib/gecoder/interface/search.rb', line 14 def solve! dfs = dfs_engine space = dfs.next @statistics = dfs.statistics raise Gecode::NoSolutionError if space.nil? self.active_space = space return self end |
#track_variable(variable) ⇒ Object
Starts tracking a variable that depends on the space. All variables created should call this method for their respective models.
265 266 267 |
# File 'lib/gecoder/interface/model.rb', line 265 def track_variable(variable) #:nodoc: (@variables ||= []) << variable end |
#wrap_enum(enum) ⇒ Object
Wraps a custom enumerable so that constraints can be specified using it. The argument is altered and returned.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/gecoder/interface/enum_wrapper.rb', line 5 def wrap_enum(enum) unless enum.kind_of? Enumerable raise TypeError, 'Only enumerables can be wrapped.' end if enum.kind_of? Gecode::EnumMethods raise ArgumentError, 'The enumration is already wrapped.' end elements = enum.to_a if elements.empty? raise ArgumentError, 'Enumerable must not be empty.' end if elements.all?{ |var| var.respond_to? :to_int_var } elements.map!{ |var| var.to_int_var } class <<enum include Gecode::IntEnumMethods end elsif elements.all?{ |var| var.respond_to? :to_bool_var } elements.map!{ |var| var.to_bool_var } class <<enum include Gecode::BoolEnumMethods end elsif elements.all?{ |var| var.respond_to? :to_set_var } elements.map!{ |var| var.to_set_var } class <<enum include Gecode::SetEnumMethods end elsif elements.all?{ |var| var.kind_of? Fixnum } class <<enum include Gecode::FixnumEnumMethods end else raise TypeError, "Enumerable doesn't contain operands #{enum.inspect}." end enum.model = self return enum end |