Debian Codegraph @

testsuite/lib/bohman_ssd.exp

raw
1 # Copyright (C) 2018 Free Software Foundation, Inc.
2 #
3 # This file is part of DejaGnu.
4 #
5 # DejaGnu is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # DejaGnu is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with DejaGnu; if not, write to the Free Software Foundation,
17 # Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # This file was written by Jacob Bachmeyer.
20
21 # This library provides functions for generating subset-sum-distinct sets
22 # using a construction published by Tom Bohman in:
23 # T. Bohman, A construction for sets of integers with distinct subset sums,
24 # The Electronic. Journal of Combinatorics 5 (1998) /#R3
25 # <URL:http://www.combinatorics.org/Volume_5/PDF/v5i1r3.pdf>,
26 # retrieved 2018-12-28 SHA-1 1c35035427b3406a44f7290f13ec8fbc3d105041
27 namespace eval ::math_utils::Bohman_SSD {
28
29 # b_n(i)
30 proc b { n i } {
31 if { $n <= 1 } { error "invalid parameter n: $n" }
32 if { $i <= 2*$n } { error "invalid parameter i: $i" }
33
34 if { $i >= 2*$n + 4 } {
35 return [expr { round(sqrt(2*($i + 2 - 2*$n))) }]
36 } elseif { $i == 2*$n + 3 } {
37 return [expr { $n + 2 }]
38 } else { # $i == 2*$n + 1 || $i == 2*$n + 2
39 return [expr { $n + 1 }]
40 }
41 }
42
43 variable d_memo
44 array unset d_memo
45 array set d_memo {}
46
47 # d_n(i)
48 proc d { n i } {
49 variable d_memo
50 if { [info exists d_memo($n,$i)] } { return $d_memo($n,$i) }
51
52 if { $n <= 1 } { error "invalid parameter n: $n" }
53 if { $i < 1 } { error "invalid parameter i: $i" }
54
55 if { $i == $n } {
56 return 1
57 } elseif { $i < $n } {
58 set j [expr { $n - $i }]
59 return [expr { 2 * round(pow(4,($j - 1))) }]
60 } elseif { $i <= 2*$n } {
61 set j [expr { $i - $n }]
62 return [expr { round(pow(4,($j - 1))) }]
63 } else { # $i > 2*$n
64 set sum 0
65 for { set j [expr { $i - [b $n $i] }] } { $j < $i } { incr j } {
66 incr sum [d $n $j]
67 }
68 set d_memo($n,$i) $sum
69 return $sum
70 }
71 }
72
73 # S_{n,m} returns list
74 proc S { n m } {
75 if { $n <= 1 } { error "invalid parameter n: $n" }
76 if { $m < 2*$n } { error "invalid parameter m: $m" }
77
78 set dv [list]
79 for { set i 1 } { $i <= $m } { incr i } { lappend dv [d $n $i] }
80 set sum 0
81 foreach d $dv { incr sum $d }
82 set result [list]
83 foreach d $dv {
84 lappend result $sum
85 incr sum -$d
86 }
87 return $result
88 }
89
90 # b'_n(i)
91 proc bp { n i } {
92 if { $n < 1 } { error "invalid parameter n: $n" }
93 if { $i <= 2*$n + 1 } { error "invalid parameter i: $i" }
94
95 if { $i >= 2*$n + 5 } {
96 return [expr { round(sqrt(2*($i + 1 - 2*$n))) }]
97 } elseif { $i == 2*$n + 2 } {
98 return [expr { $n + 1 }]
99 } else { # $i == 2*$n + 3 || $i == 2*$n + 4
100 return [expr { $n + 2 }]
101 }
102 }
103
104 variable dp_memo
105 array unset dp_memo
106 array set dp_memo {}
107
108 # d'_n(i)
109 proc dp { n i } {
110 variable dp_memo
111 if { [info exists dp_memo($n,$i)] } { return $dp_memo($n,$i) }
112
113 if { $n < 1 } { error "invalid parameter n: $n" }
114 if { $i < 1 } { error "invalid parameter i: $i" }
115
116 if { $i == $n + 1 } {
117 return 1
118 } elseif { $i < $n + 1 } {
119 set j [expr { $n + 1 - $i }]
120 return [expr { round(pow(4,($j - 1))) }]
121 } elseif { $i <= 2*$n + 1 } {
122 set j [expr { $i - $n - 1 }]
123 return [expr { 2 * round(pow(4,($j - 1))) }]
124 } else { # $i > 2*$n + 1
125 set sum 0
126 for { set j [expr { $i - [bp $n $i] }] } { $j < $i } { incr j } {
127 incr sum [dp $n $j]
128 }
129 set dp_memo($n,$i) $sum
130 return $sum
131 }
132 }
133 # The example for d'_3 in the paper is wrong starting at i=11. The
134 # paper says that it is 200, but it is actually 300.
135
136 # S'_{n,m} returns list
137 proc Sp { n m } {
138 if { $n < 1 } { error "invalid parameter n: $n" }
139 if { $m < 2*$n + 1 } { error "invalid parameter m: $m" }
140
141 set dv [list]
142 for { set i 1 } { $i <= $m } { incr i } { lappend dv [dp $n $i] }
143 set sum 0
144 foreach d $dv { incr sum $d }
145 set result [list]
146 foreach d $dv {
147 lappend result $sum
148 incr sum -$d
149 }
150 return $result
151 }
152
153 # Given a list of numbers, verify that all sums of all subsets are in
154 # fact unique.
155 #
156 # This is a brute force search and not based on Bohman's paper. This
157 # quickly becomes impractical for large lists, requiring inordinate
158 # amounts of both time and space.
159 proc check { base } {
160 set bound [expr { int(pow(2,[llength $base])) }]
161 for { set i 0 } { $i < $bound } { incr i } {
162 set R $i
163 set sum 0
164 foreach v $base {
165 if { $R & 1 } { incr sum $v }
166 set R [expr { $R >> 1 }]
167 }
168 if { [info exists output($sum)] } {
169 # emit counterexample
170 set cexl [list]
171 set R $i
172 foreach v $base {
173 if { $R & 1 } { lappend cexl $v }
174 set R [expr { $R >> 1 }]
175 }
176 set cex [join $cexl "+"]
177 append cex "=" $sum "="
178 set cexl [list]
179 set R $output($sum)
180 foreach v $base {
181 if { $R & 1 } { lappend cexl $v }
182 set R [expr { $R >> 1 }]
183 }
184 append cex [join $cexl "+"]
185 error "list is not subset-sum-distinct: $cex"
186 }
187 set output($sum) $i
188 }
189 return 1
190 }
191
192 # Given a list of numbers and a sum of a subset of that list, find a
193 # subset that produces the given sum. If the list of numbers is
194 # subset-sum-distinct, this will return the unique solution.
195 # Otherwise, an unspecified solution is returned. If the sum is not
196 # actually a sum of a subset of the list, an empty list is returned.
197 #
198 # This is a brute force search and not based on Bohman's paper. This
199 # requires constant space, but quickly becomes impractical for large
200 # lists, requiring inordinate time to complete.
201 proc summands { base goal } {
202 set bound [expr { int(pow(2,[llength $base])) }]
203 for { set i 0 } { $i < $bound } { incr i } {
204 set R $i
205 set sum 0
206 foreach v $base {
207 if { $R & 1 } { incr sum $v }
208 set R [expr { $R >> 1 }]
209 }
210 if { $sum == $goal } {
211 set resl [list]
212 set R $i
213 foreach v $base {
214 if { $R & 1 } { lappend resl $v }
215 set R [expr { $R >> 1 }]
216 }
217 return $resl
218 }
219 }
220 return [list]
221 }
222
223 }
224
225 #EOF