Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
lib/gecoder/interface/constraints/set/connection.rb | 131 | 81 | 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::Set |
2 module SetOperand |
3 # Produces an IntOperand representing the minimum of the set. |
4 # |
5 # ==== Examples |
6 # |
7 # # The minimum of +set+. |
8 # set.min |
9 def min |
10 Connection::SetMinOperand.new(@model, self) |
11 end |
12 |
13 # Produces an IntOperand representing the maximum of the set. |
14 # |
15 # ==== Examples |
16 # |
17 # # The maximum of +set+. |
18 # set.max |
19 def max |
20 Connection::SetMaxOperand.new(@model, self) |
21 end |
22 |
23 # Produces an IntOperand representing the sum of the values in the |
24 # set. One of the following options may also be given: |
25 # [:weights] Produces the weighted sum using the specified hash |
26 # of weights. The hash should map each value to |
27 # that value's weight. |
28 # [:substitutions] Produces the sum of the set with all elements |
29 # replaced according to the hash. |
30 # |
31 # Elements not included in the weights or substitutions hash are |
32 # removed from the upper bound of the set. |
33 # |
34 # ==== Examples |
35 # |
36 # # The sum of +set+. |
37 # set.sum |
38 # |
39 # # The sum of +set+ with primes < 10 given twice the weight. |
40 # set.sum(:weights => {2 => 2, 3 => 2, 5 => 2, 7 => 2}) |
41 # |
42 # # The sum of +set+ with odd values in [1,6] being counted as 1. |
43 # set.sum(:substitutions => {1 => 1, 3 => 1, 5 => 1}) |
44 def sum(options = {:weights => weights = Hash.new(1)}) |
45 if options.empty? or options.keys.size > 1 |
46 raise ArgumentError, 'At most one of the options :weights and ' + |
47 ':substitutions may be specified.' |
48 end |
49 |
50 case options.keys.first |
51 when :substitutions |
52 subs = options[:substitutions] |
53 when :weights |
54 weights = options[:weights] |
55 subs = Hash.new do |hash, key| |
56 if weights[key].nil? |
57 hash[key] = nil |
58 else |
59 hash[key] = key * weights[key] |
60 end |
61 end |
62 else raise ArgumentError, "Unrecognized option #{options.keys.first}." |
63 end |
64 Connection::SetSumOperand.new(@model, self, subs) |
65 end |
66 end |
67 |
68 # A module that gathers the classes and modules used in connection |
69 # constraints. |
70 module Connection #:nodoc: |
71 class SetMinOperand < Gecode::Int::ShortCircuitEqualityOperand #:nodoc: |
72 def initialize(model, set_op) |
73 super model |
74 @set = set_op |
75 end |
76 |
77 def constrain_equal(int_operand, constrain, propagation_options) |
78 set = @set.to_set_var |
79 if constrain |
80 int_operand.must_be.in set.upper_bound.min..set.lower_bound.min |
81 end |
82 |
83 Gecode::Raw::min(@model.active_space, set.bind, |
84 int_operand.to_int_var.bind) |
85 end |
86 end |
87 |
88 class SetMaxOperand < Gecode::Int::ShortCircuitEqualityOperand #:nodoc: |
89 def initialize(model, set_op) |
90 super model |
91 @set = set_op |
92 end |
93 |
94 def constrain_equal(int_operand, constrain, propagation_options) |
95 set = @set.to_set_var |
96 if constrain |
97 int_operand.must_be.in set.upper_bound.min..set.lower_bound.min |
98 end |
99 |
100 Gecode::Raw::max(@model.active_space, set.bind, |
101 int_operand.to_int_var.bind) |
102 end |
103 end |
104 |
105 class SetSumOperand < Gecode::Int::ShortCircuitEqualityOperand #:nodoc: |
106 def initialize(model, set_op, subs) |
107 super model |
108 @set = set_op |
109 @subs = subs |
110 end |
111 |
112 def constrain_equal(int_operand, constrain, propagation_options) |
113 set = @set.to_set_var |
114 lub = set.upper_bound.to_a |
115 lub.delete_if{ |e| @subs[e].nil? } |
116 substituted_lub = lub.map{ |e| @subs[e] } |
117 if constrain |
118 # Compute the theoretical bounds of the weighted sum. This is slightly |
119 # sloppy since we could also use the contents of the greatest lower |
120 # bound. |
121 min = substituted_lub.find_all{ |e| e < 0}.inject(0){ |x, y| x + y } |
122 max = substituted_lub.find_all{ |e| e > 0}.inject(0){ |x, y| x + y } |
123 int_operand.must_be.in min..max |
124 end |
125 |
126 Gecode::Raw::weights(@model.active_space, lub, substituted_lub, |
127 set.bind, int_operand.to_int_var.bind) |
128 end |
129 end |
130 end |
131 end |
Generated on Thu Jan 08 13:27:03 +0100 2015 with rcov 1.0.0