Perl: the Markov chain saw PerlMonks

### Re: Algorithm to fit photos into spaces on pages

by Roy Johnson (Monsignor)
 on Nov 15, 2005 at 18:49 UTC ( #508725=note: print w/replies, xml ) Need Help??

in reply to Algorithm to fit photos into spaces on pages

Here's a regex solution. It will find a solution if one exists, and will run reasonably fast, all things considered.
```   my \$desired_page_count = 10;
# Structure holding all the different page layouts. ('l' = Landsca
+pe, 'p' = Portrait)
my \$pages = {
ll   => '01',
pp   => '02',
lp   => '03',
pl   => '04',
lpp  => '05',
pll  => '06',
plp  => '07',
lpl  => '08',
lll  => '09',
ppp  => '10',
ppl  => '11',
llp  => '12',
ppll => '13',
llpp => '14',
};
my \$regex = join '|', keys %\$pages;
print "Regex is \$regex\n";

my \$photos = "llppllpplpppllplpplpplpllplpll";

my \$pages_left = \$desired_page_count - 1;
while (\$photos =~ s/^(\$regex)(?=(?:\$regex){\$pages_left}\$)//) {
print "\$1\n";
# Reorder the regex so that the most-recently-used is the last o
+ption
\$regex = join '|', (grep {\$_ ne \$1} keys %\$pages), \$1;
--\$pages_left;
}
print "Failed to layout the whole set: \$photos does not match \$rege
+x\n" if \$photos;

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Algorithm to fit photos into spaces on pages
by SmugX (Beadle) on Nov 16, 2005 at 12:21 UTC

This is a really good solution, and I like it a lot. It's also _really_ fast.

However, I was feeding a couple of "real life" books through it, and curiously, it does seem a little "keen" to use the same layouts consecutively.

If I feed in a book of "llpllplppllpplplpplpplpllplplp" with \$desired_page_count = 13, your regexp routine returns a book featuring layouts "12,01,07,06,02,08,02,03,04,04,03,03,03" - valid, certainly, but the same layouts repeated at the end.

However, my sloooooow routine returns "01,04,03,05,01,02,08,11,02,03,01,07,03", without repeating layouts.

I assume (guess alert!) it's because the regexp routine is happy to proceed along a certain route as long as solutions exist - not necessarily "good" (by my definition!) solutions. Also, as you get nearer the end of the book, there are fewer valid layout combinations.

Does anyone have any suggestions for tweaking this behaviour?

Thanks again,
Neil.

Couldn't think how to do this yesterday, but it came to me this morning. Generate a regex for the whole thing at once, with the insurance that there be no two-in-a-row. The code actually gets simpler and faster, but on some very long random layouts, it does get hung up (meaning it's doing a lot of backtracking and probably not going to find a solution).

Update: The reason it's prone not to find a solution is that the no-two-in-a-row check is more strict than it needs to be. It will not allow 'pl' to be used if the next two chars are 'pl', even if you have a 'pll' layout available. The fix for this is to use an assertion ((??{ })) to generate each match as "any available pattern except the previous match". The 2nd code block below implements that.

```my \$desired_page_count = 10;
# Structure holding all the different page layouts. ('l' = Landscape,
+'p' = Portrait)
my \$pages = {
ll   => '01',
pp   => '02',
lp   => '03',
pl   => '04',
lpp  => '05',
pll  => '06',
plp  => '07',
lpl  => '08',
lll  => '09',
ppp  => '10',
ppl  => '11',
llp  => '12',
ppll => '13',
llpp => '14',
};

my \$photos = "llppllpplpppllplpplpplpllplpll";
# Make a really long layout -- at much over 100, it tends to hang up
\$photos .= substr 'lp', rand 2 for 1..100;
\$desired_page_count = int( length(\$photos)/ 2.7 );

my \$page_regex = join '|', keys %\$pages;
my \$regex = "(\$page_regex)";
\$regex .= "(?!\\\$_)(\$page_regex)" for 1..(\$desired_page_count - 1);
print "\\$regex is \$regex\n";
if (my @pages = \$photos =~ /^\$regex\$/) {  # Added the anchors as per S
print "\$pages->{\$_}\n" for @pages;
}
else {
print "Could not come up with no-repeat layout in \$desired_page_cou
+nt pages\n";
}
```use warnings;
use strict;

my \$desired_page_count = 10;
# Structure holding all the different page layouts. ('l' = Landscape,
+'p' = Portrait)
my \$pages = {
ll   => '01',
pp   => '02',
lp   => '03',
pl   => '04',
lpp  => '05',
pll  => '06',
plp  => '07',
lpl  => '08',
lll  => '09',
ppp  => '10',
ppl  => '11',
llp  => '12',
ppll => '13',
llpp => '14',
};

# Construct a hash of regexen: each value is all the patterns except f
+or the
# key. This way, they don't need to be constructed repeatedly during t
+he matching
my @patterns =  keys %\$pages;
my %all_except = map {my \$k=\$_; (\$k => join '|', grep {\$_ ne \$k} @patt
+erns)} @patterns;

my \$photos = "llppllpplpppllplpplpplpllplpll";
# Make a really long layout
\$photos .= substr 'lp', rand 2 for 1..100;
\$desired_page_count = int( length(\$photos)/ 2.7 );

my \$page_regex = join '|', @patterns;
my \$regex = "(\$page_regex)";
\$regex .= "((??{\\$all_except{\\$\$_}}))" for 1..(\$desired_page_count - 1
+);
print "\\$regex is \$regex\n";
use re 'eval';
if (my @pages = \$photos =~ /\$regex/) {
print "\$pages->{\$_}\n" for @pages;
}
else {
print "Could not come up with no-repeat layout in \$desired_page_cou
+nt pages\n";
}

Caution: Contents may have been coded under pressure.

Excellent!

I was just playing around with your first lot of code when you posted your second update! :-) This works really well, and I can definitely take this idea and run with it.

I noticed that it seemed to have a tendency to use the earlier layouts - most books seemed to often use layouts 1-6, but rarely the later layouts.

I assumed this was because the layouts were always being presented to the regex engine in the same order. Indeed, if I tweak your second example to randomise the order, it seems to use a more general spread of layouts, with the (interesting?) side effect of generating a different valid book each time.

Then I noticed that in your second example at least, the photos aren't necessarily all used. I assumed I can prepend a '^' and append a '\$' to the regexp, and this seems to resolve the issue.

I'm hence left with:

```use warnings;
use strict;

my \$desired_page_count = 10;
# Structure holding all the different page layouts. ('l' = Landscape,
+'p' = Portrait)
my \$pages = {
ll   => '01',
pp   => '02',
lp   => '03',
pl   => '04',
lpp  => '05',
pll  => '06',
plp  => '07',
lpl  => '08',
lll  => '09',
ppp  => '10',
ppl  => '11',
llp  => '12',
ppll => '13',
llpp => '14',
};

# Construct a hash of regexen: each value is all the patterns except f
+or the
# key. This way, they don't need to be constructed repeatedly during t
+he matching
my @patterns =  keys %\$pages;
my %all_except = map {my \$k=\$_; (\$k => join '|', sort {rand(1) <=> ran
+d(1)} grep {\$_ ne \$k} @patterns)} @patterns;

my \$photos = "llppllpplpppllplpplpplpllplpll";
# Make a really long layout
\$photos .= substr 'lp', rand 2 for 1..50;
\$desired_page_count = int( length(\$photos)/ 2.7 );

my \$page_regex = join '|', sort {rand(1) <=> rand(1)} @patterns;
my \$regex = "^(\$page_regex)";
\$regex .= "((??{\\$all_except{\\$\$_}}))" for 1..(\$desired_page_count - 1
+);
\$regex .= "\\$";
print "\\$regex is \$regex\n";

use re 'eval';
if (my @pages = \$photos =~ /\$regex/) {
print "\$pages->{\$_}\n" for @pages;
}
else {
print "Could not come up with no-repeat layout in \$desired_page_cou
+nt pages\n";
}

Regards
Neil

Create A New User
Node Status?
node history
Node Type: note [id://508725]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (11)
As of 2017-08-21 13:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Who is your favorite scientist and why?

Results (323 votes). Check out past polls.

Notices?