Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^6: Travelling problem (Anyone better 86850?)

by BrowserUk (Pope)
on Dec 23, 2013 at 18:51 UTC ( #1068243=note: print w/ replies, xml ) Need Help??


in reply to Re^5: Travelling problem (Anyone better 86850?)
in thread Travelling problem

There's thems that talk, and thems that do :)


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.


Comment on Re^6: Travelling problem (Anyone better 86850?)
Re^7: Travelling problem (Anyone better 86850?)
by LanX (Canon) on Dec 23, 2013 at 19:04 UTC
    Well brain vs muscles?

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Still talking up a storm.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^7: Travelling problem (Anyone better 86850?)
by hdb (Prior) on Dec 23, 2013 at 19:39 UTC

    Speaking of which...

    I am looking at my code in Re^6: Travelling problem (Anyone better 86850?) and I am wondering whether it is simple to make it multi-threaded. In the definition of sub path_recursive there is a call to path_recursive. Would it be possible to kick a new thread of for each of these, if the maximal number of threads is not yet encountered and execute it directly if we are at the maximum already?

    All the threads need to share the global variable $glength which stores the length of the best path so far.

    I have no experience with threads in Perl yet, so if there is a simple modification of my script, I would be grateful for hints...

    Update: this is what I am trying and it seems faster:

    use strict; use warnings; use threads; use threads::shared; use List::Util qw( sum min ); my $glength :shared; my @dist = <DATA>; $_ = [ split /\s+/ ] and shift @$_ for @dist; $glength = 0.5 * sum map { sum @$_ } @dist; sub path_recursive { my( $bound, $len, $path, $end, $tbv, $dist, $in_a_thread ) = @_; if( !@$tbv ) { $len += $dist->[ $path->[-1] ]->[$end]; if( $len < $glength ) { $glength = $len; print "$len: @$path $end ",scalar(localtime),"\n"; } threads->exit() if $in_a_thread; return; } my $last = $dist->[ $path->[-1] ]; my @sorted = sort { $last->[$a] <=> $last->[$b] } @$tbv; for (1..min($bound,@sorted)){ my $next = shift @sorted; if( scalar( threads->list(threads::running) ) < 15 ) { threads->create( \&path_recursive, $bound, $len + $last->[$next] +, [ @$path, $next ], $end, [ @sorted ], $dist, 1 ); } else { path_recursive( $bound, $len + $last->[$next], [ @$path, $next ] +, $end, [ @sorted ], $dist, 0 ); } push @sorted, $next; } } path_recursive shift(), 0, [ 1 ], 24, [ 2..23 ], \@dist, 0; sleep 10 while threads->list(threads::running); __DATA__

      Initial reaction (more later if life doesn't intercede:):

      1. You have a 15-core system?
      2. You are doing no locking on your shared variables?
      3. What do you think that threads->exit does that return doesn't?
      4. Isn't pushing a value to an array called @sorted violating the expectations of the reader, even if not those of the algorithm?
      5. You neither threads::detach() nor threads::join() your threads.

        Have you run this code to completion yet?

      6. Why past $end as a parameter, when it is never modified?

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Thanks for your help. I have tried to implement 1. - 5. but I now get the following:

        $ time perl tsp2_v6.pl 2 95383: 1 2 11 16 7 12 8 3 19 20 18 15 5 10 14 23 9 4 13 21 17 6 22 24 +Tue Dec 24 09:35:20 2013 95166: 1 21 17 12 7 16 11 2 6 13 4 9 23 14 10 5 15 18 20 3 19 8 22 24 +Tue Dec 24 09:35:20 2013 92839: 1 21 13 4 9 23 14 6 17 12 7 16 11 2 8 3 19 20 18 15 5 10 22 24 +Tue Dec 24 09:35:20 2013 90498: 1 21 17 12 7 16 11 2 6 13 4 9 23 14 10 5 15 18 20 3 8 19 22 24 +Tue Dec 24 09:35:20 2013 89483: 1 21 13 4 9 23 14 6 17 12 7 2 11 16 8 3 19 20 18 15 5 10 22 24 +Tue Dec 24 09:35:20 2013 88838: 1 21 13 4 9 23 14 6 17 12 7 2 11 16 8 19 3 20 18 15 5 10 22 24 +Tue Dec 24 09:35:20 2013 86867: 1 2 11 16 7 12 17 21 6 13 4 9 23 14 10 5 15 18 20 3 8 19 22 24 +Tue Dec 24 09:35:22 2013 85294: 1 2 11 16 7 8 12 17 21 6 13 4 9 23 14 10 5 15 18 20 3 19 22 24 +Tue Dec 24 09:35:24 2013 84860: 1 2 11 16 8 7 12 17 21 6 13 4 9 23 14 10 5 15 18 20 3 19 22 24 +Tue Dec 24 09:35:32 2013 Perl exited with active threads: 5 running and unjoined 194 finished and unjoined 0 running and detached real 0m32.107s user 1m18.492s sys 0m24.782s

        Any idea?

        Here is my updated code:

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1068243]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2014-09-22 23:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (208 votes), past polls