Thread Subject: Logical Rules

Subject: Logical Rules

From: Christopher Mouton

Date: 13 Sep, 2007 16:49:42

Message: 1 of 10

I am trying to see if Matlab has a toolbox for something
like this, or where I might look for ideas on how to
efficiently solve the following

A{1}=[1,2]
A{2}=[2,3]
A{3}=[1,3]
A{4}=[1,2,3,4]

This reads:

Position 1 can be 1 or 2
Position 2 can be 2 or 3
Position 3 can be 1 or 3
Position 4 can be 1, 2, 3 or 4

Given that each number can only be used once, we know that
Position 4 must be 4.

So my question is, how could I perform such logic in a
general way? Any ideas where I should look?

Thanks so much.

Subject: Logical Rules

From: dpb

Date: 13 Sep, 2007 17:25:21

Message: 2 of 10

Christopher Mouton wrote:
> I am trying to see if Matlab has a toolbox for something
> like this, or where I might look for ideas on how to
> efficiently solve the following
>
> A{1}=[1,2]
> A{2}=[2,3]
> A{3}=[1,3]
> A{4}=[1,2,3,4]
>
> This reads:
>
> Position 1 can be 1 or 2
> Position 2 can be 2 or 3
> Position 3 can be 1 or 3
> Position 4 can be 1, 2, 3 or 4
>
> Given that each number can only be used once, we know that
> Position 4 must be 4.
>
> So my question is, how could I perform such logic in a
> general way? Any ideas where I should look?

unique(), maybe?

But, strictly speaking, by my reading your example is inconsistent to
start with -- if each value can be used only once, then A(2) would have
to be [3] and then A(3) would have to be []?

Think need more precise definition of the problem and the expected
solution...

--

Subject: Logical Rules

From: Kelly Kearney

Date: 13 Sep, 2007 17:47:43

Message: 3 of 10

"Christopher Mouton" <mouton.no.spam@caltech.edu> wrote in
message <fcbpn6$eo0$1@fred.mathworks.com>...
> I am trying to see if Matlab has a toolbox for something
> like this, or where I might look for ideas on how to
> efficiently solve the following
>
> A{1}=[1,2]
> A{2}=[2,3]
> A{3}=[1,3]
> A{4}=[1,2,3,4]
>
> This reads:
>
> Position 1 can be 1 or 2
> Position 2 can be 2 or 3
> Position 3 can be 1 or 3
> Position 4 can be 1, 2, 3 or 4
>
> Given that each number can only be used once, we know that
> Position 4 must be 4.
>
> So my question is, how could I perform such logic in a
> general way? Any ideas where I should look?
>
> Thanks so much.

The most straightforward way would be to just form all
combinations possible, then throw out any that don't meet
the uniqueness criteria.

A{1}=[1,2];
A{2}=[2,3];
A{3}=[1,3];
A{4}=[1,2,3,4];

combos = [];
for a = A{1}
    for b = A{2}
        for c = A{3}
            for d = A{4}
                temp = [a b c d];
                if length(unique(temp)) == 4
                    combos = [combos;temp];
                end
            end
        end
    end
end


-Kelly

Subject: Logical Rules

From: Christopher Mouton

Date: 13 Sep, 2007 17:57:21

Message: 4 of 10

I would expect the solution to be

A{1}=[1,2]
A{2}=[2,3]
A{3}=[1,3]
A{4}=[4]

Subject: Logical Rules

From: Christopher Mouton

Date: 13 Sep, 2007 17:58:20

Message: 5 of 10

This would definitely work for this example.

But now I would like to be able to find much larger sets.
In particular A{1...256}, so it will require something
smart.

Subject: Logical Rules

From: dpb

Date: 13 Sep, 2007 18:00:51

Message: 6 of 10

Christopher Mouton wrote:
> I would expect the solution to be
>
> A{1}=[1,2]
> A{2}=[2,3]
> A{3}=[1,3]
> A{4}=[4]

Oh? Why? Then "used only once" means something different than used
only once. The answer to that would perhaps lead the "something clever"
solution...

Maybe I'm just dense today (some say every day :) ), but I don't yet see
the general rule...

--

Subject: Logical Rules

From: Christopher Mouton

Date: 13 Sep, 2007 18:07:46

Message: 7 of 10

I hope I am not being stupid. Here are the two possible
solutions:

A{1}=[1]
A{2}=[2]
A{3}=[3]
A{4}=[4]

A{1}=[2]
A{2}=[3]
A{3}=[1]
A{4}=[4]

Thanks.

Subject: Logical Rules

From: dpb

Date: 13 Sep, 2007 18:48:06

Message: 8 of 10

Christopher Mouton wrote:
> I hope I am not being stupid. Here are the two possible
> solutions:
...

Oh, oops! I got my eyes crossed somehow... :(

But, sorry, I've no cleverer ideas. Not my area, but would some of the
genetics-related pattern matching have any bearing here for tricks?

--

Subject: Logical Rules

From: "J.N.

Date: 13 Sep, 2007 18:57:00

Message: 9 of 10

On Sep 13, 12:49 pm, "Christopher Mouton" <mouton.no.s...@caltech.edu>
wrote:
> I am trying to see if Matlab has a toolbox for something
> like this, or where I might look for ideas on how to
> efficiently solve the following
>
> A{1}=[1,2]
> A{2}=[2,3]
> A{3}=[1,3]
> A{4}=[1,2,3,4]
>
> This reads:
>
> Position 1 can be 1 or 2
> Position 2 can be 2 or 3
> Position 3 can be 1 or 3
> Position 4 can be 1, 2, 3 or 4
>
> Given that each number can only be used once, we know that
> Position 4 must be 4.
>
> So my question is, how could I perform such logic in a
> general way? Any ideas where I should look?
>
> Thanks so much.

This is called an Assignment Problem:
http://en.wikipedia.org/wiki/Assignment_problem

You are essentially trying to assign a set of entities {1,2,...,n} to
a set of slots {A1,A2,...,An}, with constraints that state that a slot
Ai is able to accept only a subset of the entities.

One way to model this is to create a n x n logical variable L (with
slots as rows and entities as columns) such that L(i,j) = true iff
entity j can be assigned to slot Ai.

hth

J.N.

Subject: Logical Rules

From: Randy Poe

Date: 13 Sep, 2007 20:03:30

Message: 10 of 10

On Sep 13, 2:57 pm, "J.N." <joao.nat...@gmail.com> wrote:
> On Sep 13, 12:49 pm, "Christopher Mouton" <mouton.no.s...@caltech.edu>
> wrote:
>
>
>
> > I am trying to see if Matlab has a toolbox for something
> > like this, or where I might look for ideas on how to
> > efficiently solve the following
>
> > A{1}=[1,2]
> > A{2}=[2,3]
> > A{3}=[1,3]
> > A{4}=[1,2,3,4]
>
> > This reads:
>
> > Position 1 can be 1 or 2
> > Position 2 can be 2 or 3
> > Position 3 can be 1 or 3
> > Position 4 can be 1, 2, 3 or 4
>
> > Given that each number can only be used once, we know that
> > Position 4 must be 4.
>
> > So my question is, how could I perform such logic in a
> > general way? Any ideas where I should look?
>
> > Thanks so much.
>
> This is called an Assignment Problem:http://en.wikipedia.org/wiki/Assignment_problem
>

Hmm. There are some problems on that page, mainly the
misuse of the term "Linear Program" which normally refers
to problems in which the variables can be any (positive)
real, not restricted to integers.

> You are essentially trying to assign a set of entities {1,2,...,n} to
> a set of slots {A1,A2,...,An}, with constraints that state that a slot
> Ai is able to accept only a subset of the entities.
>
> One way to model this is to create a n x n logical variable L (with
> slots as rows and entities as columns) such that L(i,j) = true iff
> entity j can be assigned to slot Ai.

Yes, the Wiki page describes the setup as an LP with these
n^2 variables. At n=256, that's not an unwieldy problem, but
you can see that it doesn't take much for such a problem to
get too large to fit in memory.

If you relax the requirement that the L(i,j) be integers, then
you do actually have a standard linear program and you can
use fast LP solving algorithms to solve it (this is called an
"LP relaxation"). The Optimization Toolbox includes one called
LINPROG.

Then you have to decide what to do with fractional values,
but usually some rounding scheme works and often
gives you a pretty good solution, though only in
special cases is that solution guaranteed optimal.

                  - Randy

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com