Multidimensional Possibilities

I recently had to solve a one-off problem for work, and the solution I
came up with was about as non-DRY as you can imagine. I couldn’t
figure out how to make it simple, however. In order to expand my
knowledge, I’m hoping to get some ideas (or even related problem
domains) to solve this generically.

The problem was this:

This website has a menu of industries, a menu of categories, and a
menu of products.
If a single industry is selected, highlight the categories and
products are available for that industry.
If a single category is selected, highlight the industries and
products are available for that category.
If a single product is selected, highlight the industries and
categories available for that industry.
If a pair of items is selected (for example, one industry and one
product) show all the related items that match the intersection (for
example, all the categories that apply to the intersection of those
two choices).

I solved it by storing the data as a series of nested hashes, dictated
an order in which axes were specified, and hard-coded it for the 3
axes above, and repeated almost the same code for all 6 possible axis
queries (axis1, axis2, axis3, axis1+axis2, axis1+axis3, axis2+axis3).
It was huge and bloated and menial.

SO…this is like a data cube (in this particular set of 3
dimensions). I’m looking for a solution that is generic to n
dimensions. Doing that will simultaneously make the code simpler and
more generally useful.

Here’s Ruby code and plenty of test cases to exactly describe what I’m
looking for. It’s echoed at http://pastie.caboo.se/130061 for easier
reading and preventing unintended line-wrapping.

My preemptive thanks to all who provide some ideas (or working code)
towards the solution.

BOYv NUM>| one | two | three |

----------±----------±----------±---------+

jim | alpha | | beta |

jam | beta | alpha | |

jem | delta | | delta |

jom |beta, alpha| omega | delta |

AXES = [ :boy, :num, :greek ]

COMBOS = {
:jim => {
:one => [ :alpha ],
:three => [ :beta ]
},
:jam => {
:one => [ :beta ],
:two => [ :alpha ]
},
:jem => {
:one => [ :delta ],
:three => [ :delta ]
},
:jom => {
:one => [ :beta, :alpha ],
:two => [ :omega ],
:three => [ :delta ]
}
}

def make_database( axes, nested_hash )

Parse however you like to create

your own data structure.

end

def fetch_options( database, axes_values = {} )

Return all valid possibilities based on the supplied values

end

require ‘test/unit’
class TestMultiDimensionalDB < Test::Unit::TestCase
def setup
$db = make_database( AXES, COMBOS )
end
def test_one_axis
opts = fetch_options( $db, :greek=>:omega )
assert( opts[:boy] == [:jom],
“The only boy with greek:omega is jom.” )
assert( opts[:num] == [:two],
“The only num with greek:omega is two.” )

opts = fetch_options( $db, :greek=>:alpha )
assert( opts[:boy].length == 3,
  "There are 3 boys with greek:alpha." )
assert( opts[:boy].include?(:jim),
  "One of the 3 boys with greek:alpha is jim." )
assert( opts[:boy].include?(:jam),
  "One of the 3 boys with greek:alpha is jam." )
assert( opts[:boy].include?(:jom),
  "One of the 3 boys with greek:alpha is jom." )

assert( opts[:num].length == 2,
  "There are 2 nums with greek:alpha." )
assert( opts[:num].include?(:one) && opts[:num].include?(:two),
  "The nums with greek:alpha are one and two." )



opts = fetch_options( $db, :boy=>:jem )
assert( opts[:num].length == 2,
  "There are 2 nums with the boy:jem." )
assert( opts[:num].include?(:one) && opts[:num].include?(:three),
  "The nums with the boy:jem are one and three." )

assert( opts[:greek] == [:delta]
  "The only greek with the boy:jem is delta" )



opts = fetch_options( $db, :num=>:one )
assert( opts[:boy].length == 4,
  "There are 4 boys with num:one." )
assert( opts[:boy].include?(:jim),
  "One of the 4 boys with num:one is jim." )
assert( opts[:boy].include?(:jam),
  "One of the 4 boys with num:one is jam." )
assert( opts[:boy].include?(:jem),
  "One of the 4 boys with num:one is jem." )
assert( opts[:boy].include?(:jom),
  "One of the 4 boys with num:one is jom." )

assert( opts[:greek].length == 3,
  "There are 3 greek with num:one." )
assert( opts[:greek].include?(:alpha),
  "One of the 3 greeks with num:one is alpha." )
assert( opts[:greek].include?(:beta),
  "One of the 3 greeks with num:one is beta." )
assert( opts[:greek].include?(:delta),
  "One of the 3 greeks with num:one is delta." )

end

def test_two_axes
opts = fetch_options( $db, :greek=>:alpha, :num=>:one )
assert( opts[:boy].length == 2,
“There are 2 boys with greek:alpha and num:one.” )
assert( opts[:boy].include?(:jim) && opts[:boy].include?(:jom),
“The boys with greek:alpha and num:one are jim and jom.” )

opts = fetch_options( $db, :greek=>:alpha, :num=>:two )
assert( opts[:boy] == [:jam],
  "The only boy with greek:alpha and num:two is jam." )



opts = fetch_options( $db, :boy=>:jom, :num=>:one )
assert( opts[:greek].length == 2,
  "There are 2 greeks with the boy:jom and num:one." )
assert( opts[:greek].include?(:alpha) && opts[:greek].include?

(:beta),
“The greeks with the boy:jom and num:one are alpha and beta.” )

opts = fetch_options( $db, :boy=>:jam, :greek=>:omega )
assert( opts[:num].length == 0,
  "There are no nums with the boy:jam and greek:omega." )



opts = fetch_options( $db, :boy=>:jem, :greek=>:delta )
assert( opts[:num].length == 2,
  "There are 2 nums with the boy:jem and greek:delta." )
assert( opts[:num].include?(:one) && opts[:num].include?(:three),
  "The nums with the boy:jem and greek:delta are one and three." )

end
end

On Dec 18, 2007 11:00 PM, Phrogz [email protected] wrote:

product) show all the related items that match the intersection (for
example, all the categories that apply to the intersection of those
two choices).

Use a database - they’re written to solve this exact problem!

martin

Okay, a bit less of a copout:

Store the data in a linear array of records. Also, have a hash of
{[axis, value] => bitvector}, where the bitvector is 1 for every
record containing that value on that axis (e.g. {[boy, jim] =>
1100000} means that of the seven records, the first two have boy:jim)

Then you simply and together the bitvectors for all your criteria and
traverse the resultant bitvector, picking out array indices.

Add/delete a record: for every axis in your new record, hash[axis,
value].bitvector.toggle(record.index)

Note that deletion leaves gaps you cannot compact, though you can
maintain a subsidiary datastructure pointing to deleted records so
they can be reclaimed one by one

martin

On Dec 18, 10:45 am, Martin DeMello [email protected] wrote:

If a single product is selected, highlight the industries and
categories available for that industry.
If a pair of items is selected (for example, one industry and one
product) show all the related items that match the intersection (for
example, all the categories that apply to the intersection of those
two choices).

Use a database - they’re written to solve this exact problem!

What does that look like? Given n tables of entries, you have a single
extra table with n columns, where each row enumerates a given possible
combination? And then you just query that table to get a flat list of
all possible combinations, and then spin through the rows and build up
your arrays of possibilities?

Not a bad suggestion, and so simple.

On Dec 19, 2007 12:40 AM, Phrogz [email protected] wrote:

Use a database - they’re written to solve this exact problem!

What does that look like? Given n tables of entries, you have a single
extra table with n columns, where each row enumerates a given possible
combination? And then you just query that table to get a flat list of
all possible combinations, and then spin through the rows and build up
your arrays of possibilities?

Not a bad suggestion, and so simple.

Why do you even need the n tables of entries? Since your data is
simply a set of points in a multidimensional space, you could have a
single table where the columns correspond to the axes and the rows are
the coordinates of the point on each axis. Searching is simply a
matter of synthesising sql statements to select subsets.

martin

On Dec 18, 2007 11:30 AM, Phrogz [email protected] wrote:

If a single industry is selected, highlight the categories and
products are available for that industry.
If a single category is selected, highlight the industries and
products are available for that category.
If a single product is selected, highlight the industries and
categories available for that industry.
If a pair of items is selected (for example, one industry and one
product) show all the related items that match the intersection (for
example, all the categories that apply to the intersection of those
two choices).

I’ll have a look at your code later, but this looks to me solvable
using queries to the database; that is, if you have a database. If
not, then you are trying to create one, which task I wouldn’t wish on
anybody (that’s a way of saying I wish you well :). The problem
domain, though, seems confined to be tackled solely in Ruby and still
be maintainable, the solution unstable. I think it would be an
awesome Ruby Q.!

If you have a database, using SQL (fill in the blanks)…

select from industries, categories, products
where

I know, db guys/gals like to use caps for key words, but I tend to be
lazy on lists.

I’m pretty certain you were well aware of this, but I thought I’d
throw the SQL info in for other newbies (like myself, not you) that
struggle with this kind of situation.

From there, it would depend on the tool a person would use to access
the db, you know, all the ones people keep bringing up on the list.

Todd

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs