#!/usr/local/bin/perl # # David MacKay 2000 # pathcount.p [list of integers] # file contains a list of integers defining a matrix of 1 and 0 # with 1 denoting "accessible square" # # program runs f/b algm to count how many paths pass through each square. # first integer should be zero. # e.g. pathcount.p 3 4 5 6 # e.g. pathcount.p 1 2 3 4 5 6 7 8 9 # pathcount.p W=21 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 $rotate="rotate" ; $PATHS=1 ; $pathfile = "path" ; $greedyfile = "greedy" ; $outfile = "prob" ; $gridfile = "grid" ; $W = 10 ; # width of grid $H = 10 ; # height of grid (overruled below) $verbose = 0 ; eval "\$$1=\$2" while @ARGV && $ARGV[0]=~ /^(\w+)=(.*)/ && shift; $i = 0 ; $r[0] = 0 ; do { print $r[$i], " :" if ($verbose>1 ) ; print "="x$r[$i] if($verbose) ; $R[$i] = $W - $r[$i] ; print "."x$R[$i] if($verbose) ; print "\n" if($verbose) ; $i ++ ; } while ( ( ($r[$i] = $ARGV[0]) + 1) && shift ) ; $H = $i ; for ($i = 0 ; $i < $H ; $i ++ ) { $onleft = 0 ; print " == "x$r[$i] if($verbose) ; for ($x = $r[$i] ; $x < $W ; $x ++ ) { if ($i == 0 ) { $forward{$i,$x} = 1.0 ; } else { $onleft += $forward{$i-1,$x} ; $forward{$i,$x} = $onleft ; } printf "%3d ", $forward{$i,$x} if ($verbose ) ; } print "\n" if ($verbose ) ; } for ($x = $W-1 ; $x >= 0 ; $x -- ) { $onunder = 0 ; for ($i = $H - 1 ; $i >= 0 ; $i -- ) { if ($x == $W-1 ) { $backward{$i,$x} = 1.0 ; } elsif ( $forward{$i,$x} ) { $onunder += $backward{$i,$x+1} ; $backward{$i,$x} = $onunder ; } else { $backward{$i,$x} = 0 ; $forward{$i,$x} = 0 ; } $product{$i,$x} = $backward{$i,$x} * $forward{$i,$x} ; } } print STDERR "total number of paths $onunder \n" ; print "\n" if ($verbose ) ; for ($i = 0 ; $i < $H ; $i ++ ) { for ($x = 0 ; $x < $W ; $x ++ ) { printf "%3.0f ", $backward{$i,$x} if ($verbose ) ; } print "\n" if ($verbose ) ; } print "\n" if ($verbose ) ; for ($i = 0 ; $i < $H ; $i ++ ) { for ($x = 0 ; $x < $W ; $x ++ ) { printf "%3.0f ", $product{$i,$x} if ($verbose>0 ); } print "\n" if ($verbose>0 ); } $outfile = "$outfile$H.$W" ; open(O,">$outfile") ; for ($i = 0 ; $i < $H ; $i ++ ) { for ($x = 0 ; $x < $W ; $x ++ ) { printf O "%3.0f ", $product{$i,$x} ; } print O "\n" ; } $hintoncommand = "cat $outfile | hintond.p o=$W.ps rows=$W zero=0.0 pos=0.0 background=1.0 range=$onunder $W > /dev/null \n" ; # generate a grid for gnuplot to show $gridfile = "$gridfile$H.$W" ; open(O,">$gridfile") ; $v = 0 ; # used for creating the "inverse" of r[] $olds = 0 ; $WW = $W - 1 ;$HH = $H - 1 ; # make frame, if it is smallish if ($H+$W < 40 ) { printf O " -0.5 -0.5 \n %5.1f %5.1f \n%5.1f %5.1f \n%5.1f %5.1f \n -0.5 -0.5 \n\n", $WW+0.5 , -0.5 , $WW+0.5, $H-0.5 , -0.5, $H-0.5 ; } for( $s = 0 ; $s < $H ; $s ++ ) { $i = $s ; $x=$r[$i] ; print O "$x $i\n" ; print O "$WW $i\n" ; print O "\n" ; for ( ; $v < $x ; $v ++ ) { $ir[$v] = $olds ; } $v = $x ; $olds = $s ; $ir[$v] = $olds ; } for( $s = 0 ; $s < $W ; $s ++ ) { $x = $s ; $i=$ir[$x] ; print O "$x $i\n" ; print O "$x 0\n" ; print O "\n" ; } close(O); $pathfile = "$pathfile$H.$W" ; open(O,">$pathfile") ; for ($paths=0;$paths<$PATHS;$paths++ ) { $i=0;$x=0; print O "$x $i\n" ; for( $s = 0 ; $s < $HH+$WW ; $s ++ ) { $current = $backward{$i,$x} ; # if ($current<=0) {print STDERR "forbidden current location\n" ; } $right = $backward{$i,$x+1} ; # $below = $backward{$i+1,$x} ; if (rand()* $current >$right ) { $i ++ ; if ($below<=0) {print STDERR "forbidden location\n" ; } } else { $x ++ ; if ($right<=0) {print STDERR "forbidden location\n" ; } } print O "$x $i\n" ; } print O "\n" ; } # generate upper and lower paths $greedyfile = "$greedyfile$H.$W" ; open(O,">$greedyfile") ; $i=0;$x=0; print O "$x $i\n" ; for( $s = 0 ; $s < $HH+$WW ; $s ++ ) { $current = $product{$i,$x} ; # $below = $product{$i+1,$x} ; if ($below>0 ) { $i ++ ; } else { $x ++ ; } print O "$x $i\n" ; } print O "\n" ; $i=0;$x=0; print O "$x $i\n" ; for( $s = 0 ; $s < $HH+$WW ; $s ++ ) { $current = $product{$i,$x} ; # $b = $product{$i,$x+1} ; if ($b>0 ) { $x ++ ; } else { $i ++ ; } print O "$x $i\n" ; } close(O); $offset = 0.25 ; $voffset = 0.5 ; if ($H+$W > 40 ) { $voffset = 0.85 ; $offset = 0.5 ; } $WWW = $WW - $offset; print STDOUT "# gnuplot \n set size 0.7,0.7 \n set size square\n" ; printf STDOUT "set xrange [-0.5:%6.1f]\n" , $W-0.5 ; printf STDOUT "set yrange [-0.5:%6.1f]\n" , $H-0.5 ; print STDOUT "set linestyle 1 lt 1 lw 0.3 \n set nokey \n " ; print STDOUT "set linestyle 2 lt 1 lw 3.5 \n" ; print STDOUT "set linestyle 3 lt 3 lw 0.3 \n" ; print STDOUT "set noborder ; set noxtics ; set noytics \n" ; print STDOUT "plot"; print STDOUT " '$greedyfile' u 1:2 w l 3," ; print STDOUT " '$pathfile' u 1:2 w l 1 \n " ; print STDOUT "print 'solid grid lines'\n" ; print STDOUT "plot '$gridfile' u 1:2 w l ls 1 \n" ; print STDOUT "print 'dotted grid lines'\n" ; print STDOUT "plot '$gridfile' u 1:2 w l ls 1 \n" ; # was 3 print STDOUT "replot '$pathfile' u 1:2 w l ls 2 1 \n " ; # aligns were print STDOUT "set label 1 'A' at -0.5,$voffset center $rotate\n" ; # print STDOUT "set label 2 'B' at $WWW,$HH left $rotate\n" ; # right print STDOUT "set term postscript ; set output '$pathfile.ps'\n" ; print STDOUT "replot\n set term X ; replot\n" ; # pause 3 'done' \n" ; print STDERR $hintoncommand ; system($hintoncommand ) ;