Message Boards Message Boards

0
|
4228 Views
|
6 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Problem with memoized function

Posted 8 years ago

I wrote two identical programs. One with a memoized module and the same without memoization. Each of them give a different result. Thanks to explain where is the problem.

See attached file

Attachments:
POSTED BY: Louis Rogliano
6 Replies
Posted 8 years ago

Works well now.

Louis

Attachments:
POSTED BY: Louis Rogliano
Posted 8 years ago

Louis,

Keep on trying. I'd suggest you start with a smaller version of the problem until you get the bugs worked out. I'd also suggest splitting out sub-parts of your algorithm. You can also time your solutions as you scale up the size of the problem to get an estimate of how your algorithm will perform on the entire original problem.

Good luck,

Eric

POSTED BY: Eric Rimbey
Posted 8 years ago

Eric,

With some additions corresponding to the problem, the algorithm work ... but need too much time without memoization. I try to modify it in order to oblige Mathematica to not miss any step in memoizing perhaps in introducing different values unrelated to the problem for each step.

POSTED BY: Louis Rogliano
Posted 8 years ago

Louis,

No, this is not a bug with Mathematica. It's using the memoized values, just as we would expect.

I'm having a hard time mapping your algorithm to that problem, so I'm not sure how to help you correct your algorithm. A brute force method would start from the realizations that any sequence of jumps could produce the croak sequence and for most starting positions there are 2^15 jump sequences, and so finding the probability for that particular croak sequence is a matter of counting the number of occurrences among all of those jump sequences (repeated for each starting point).

So, I'd decompose the algorithm into a function to give me all jump sequences of length N (so I can start smaller than the specified 15), a function to turn those into the associated integers for a given start point, map that across M cells (so I can start smaller than the specified 500), and a function to count occurrences of a target croak sequence (which I will set to simpler test cases in the early phase of developing the algorithm). With this decomposition and complexity reduction, it's easier to correct bugs before attempting the full problem.

But that's a brute force approach...

POSTED BY: Eric Rimbey
Posted 8 years ago

Thanks Eric for your reply and sorry for my bad english. You're right, I've added a new line to the program and I can see that some steps are missing with g. These steps are repetitions of others (when n = 1). (New file added) Is this a bug with Mathematica ? I use Mathematica 10.0.2.0.

The computation is relative to the problem #329 of the project Euler.

Louis

Attachments:
POSTED BY: Louis Rogliano
Posted 8 years ago

Louis,

I think you meant "memoized".

I'm pretty sure that one of your g[n - 1, a +- 1, bb, cc] gets evaluated with the same arguments as in a previous step in the recursion. In the case of g, the memoized value is used the second time, but in the case of h, when the same case occurs, it gets recomputed "fresh" rather than looking up a memoized version. It's at that point that g and h diverge.

Maybe tell us the computation you're trying for, and we can suggest a better, more Mathematica-esque approach.

Eric

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract