Monday 3 December 2012

Code for Paper Folding Problem Cont'd

(require picturing-programs)
(check-expect (paperfold 0)
              (rotate -90 (line 0 16 "black")))
(check-expect (paperfold 1)
              (beside/align
               "bottom"
               (rotate -60 (paperfold 0))
               (rotate 60 (paperfold 0))))
(define (paperfold d)
  (cond
    [(zero? d) (rotate -90 (line 0 16 "black"))]
    [(= 1 d)
     (beside/align
      "bottom"
      (rotate -60 (paperfold 0))
      (rotate 60 (paperfold 0)))]
    [(= 2 d)
     (beside/align
      "bottom"
      (rotate 180 (paperfold 1))
      (paperfold 0)
      (rotate 60 (paperfold 0)))]
    [else
     (beside/align
      "bottom"
      (beside/align
       "bottom"
       (crop-right (rotate 180 (paperfold (sub1 d))) 7)
       (rotate -90 (line 0 16 "black")))
      (paperfold (sub1 d)))]))

I tried creating a new function before the definition of the "paperfold" function to fix the problem of the disconnecting link, but that did not work out because it requires using "paperfold" and "paperfold" function requires the link function in its definition.Therefore instead of creating a new function and inserting it into the "paperfold" function, I directly inserted the definition into the recursion of the "paperfold" function.

"crop-right" and "beside/align" replaces the end of the broken link in the sequence. Conveniently the middle link is always a *-U-U-*. The use of "(crop-right (... ...) 7)" is incorrect, it does not fully cut off the incorrect link before it is attached to the second part of the sequence. However, the "notches" left from the incorrect crop can be used to indicate sections with more than two same points (e.g. D-D-D).

The code seems to work and produces expected results up to five folds. Cannot confirm the code beyond that because I cannot fold a strip of paper more than five times.

Dr.Racket Code for Paper Folding Problem

In regards to the paper folding problem, I tried to write a code in Dr.Racket for it.

(require picturing-programs)
(define (paperfold d)
  (cond
    [(zero? d) (rotate -90 (line 0 15 "black"))]
    [(= 1 d)
     (beside/align
      "bottom"
      (rotate -60 (paperfold 0))
      (rotate 60 (paperfold 0)))]
    [(= 2 d)
     (beside/align
      "bottom"
      (rotate 180 (paperfold 1))
      (paperfold 0)
      (rotate 60 (paperfold 0)))]
    [else
     (beside
      (rotate 180 (paperfold (sub1 d)))
      (paperfold (sub1 d)))]))

Run into a problem in recursion. Because when d >= 3, the code takes the 180 degree rotated version of sub1 d, the image produced is disconnected in the middle. Mentioned before in the previous post, the sequence of points of N folds is equal to reverse of points of N-1 folds appended to the front of points of N-1 folds with the exception that the middle point (always will be a middle point) is opposite. This is what is causing the image produced to be disconnected, considering creating a function which specifically replaces that one faulty part and entering it into the recursion.

(paperfold 3) looks like this
/-\//\_/

(paperfold 3) should look like this
/-\_/\_/

Sunday 21 October 2012

Regarding Paper Folding Problem

Note: Instead of recording the direction of the fold, I record the location of the fold.
For example: U-D-U represents up-down-up, meaning the fold created by the points of the is "\/"

One fold:
U-D-U
\/

Two folds:
D-U-D-D-U
/\_/

Three folds:
D-U-U-D-D-U-D-D-U
/-\_/\_/

Four folds:
D-U-U-D-U-U-D-D-D-U-U-D-D-U-D-D-U
/-\/-\__/-\_/\_/

Five folds:
D-U-U-D-U-U-D-D-U-U-U-D-D-U-D-D-D-U-U-D-U-U-D-D-D-U-U-D-D-U-D-D-U
/-\/-\_/--\_/\__/-\/-\__/-\_/\_/

First thing I realize is that number of points in the sequence is always equal to the number of points in the previous sequence times 2 then minus one. In the sequence of four folds there are 17 points, and in five folds there are 33 points.
17 * 2 = 34
34 - 1 = 33

The second thing that caught my eye is that the position of points for the directly preceding sequence can always be found at the end of the current sequence, assuming that the number of folds is three or more. Since the number of points is always an odd number (because the number of points is the previous number of points *2 then -1) there will always be a center point, starting from that center point is where the previous sequence can be found until the end of that sequence. Theoretically this makes sense because the current sequence is basically the previous sequence folded onto itself.

Therefore to find the sequence of the current number of folds, one will have to record the previous points of folds then (moving backwards from the center) reverse the points and get the sequence of the current number of folds. For example:

Three folds:
D-U-U-D-D-U-D-D-U
Reverse the points:
U-D-D-U-U-D-U-U-D
Reverse the sequence:
D-U-U-D-U-U-D-D-U
Remove final point of the sequence:
D-U-U-D-U-U-D-D
Add onto the front of sequence of previous number of folds:
D-U-U-D-U-U-D-D-D-U-U-D-D-U-D-D-U (four folds!)

For five folds:
First get the sequence of four folds:
D-U-U-D-U-U-D-D-D-U-U-D-D-U-D-D-U
Reverse the values:
U-D-D-U-D-D-U-U-U-D-D-U-U-D-U-U-D
Reverse the sequence:
D-U-U-D-U-U-D-D-U-U-U-D-D-U-D-D-U
Remove final point of the sequence:
D-U-U-D-U-U-D-D-U-U-U-D-D-U-D-D
Add onto the front of the sequence of previous number of folds:
D-U-U-D-U-U-D-D-U-U-U-D-D-U-D-D-D-U-U-D-U-U-D-D-D-U-U-D-D-U-D-D-U

For six folds:
Get the sequence of five folds:
D-U-U-D-U-U-D-D-U-U-U-D-D-U-D-D-D-U-U-D-U-U-D-D-D-U-U-D-D-U-D-D-U
Reverse the values:
U-D-D-U-D-D-U-U-D-D-D-U-U-D-U-U-U-D-D-U-D-D-U-U-U-D-D-U-U-D-U-U-D
Reverse the sequence:
D-U-U-D-U-U-D-D-U-U-U-D-D-U-D-D-U-U-U-D-U-U-D-D-D-U-U-D-D-U-D-D-U
Remove final point of the sequence:
D-U-U-D-U-U-D-D-U-U-U-D-D-U-D-D-U-U-U-D-U-U-D-D-D-U-U-D-D-U-D-D
Add onto the front of the sequence of previous number of folds:
D-U-U-D-U-U-D-D-U-U-U-D-D-U-D-D-U-U-U-D-U-U-D-D-D-U-U-D-D-U-D-D-D-U-U-D-U-U-D-D-U-U-U-D-D-U-D-D-D-U-U-D-U-U-D-D-D-U-U-D-D-U-D-D-U

Needs further verification, using as a algorithm in computer programming seems to take a lot of time.

EDIT: looking at the steps further, step 2 and 3 can be replaced by one step which is finding the middle point and reversing that then moving to step 4.

e.g. Three Folds:
D-U-U-D-D-U-D-D-U
Reverse middle point:
D-U-U-D-U-U-D-D-U
Remove final point and append to previous sequence:
D-U-U-D-U-U-D-D-D-U-U-D-D-U-D-D-U = Four Folds.

Sunday 14 October 2012

Algorithms for Real Life Processes

The example of making a PB&J sandwich was used during lecture this week, the issue of algorithms in real life concerns me. In Dr.Racket, when the code does not make sense the program does not work and it tells you what is wrong/missing etc.. But in real life the precision that is needed to achieve a working code such as in Dr.Racket is so much harder to pinpoint, things can go wrong at every turn and rarely is there a guide to tell you exactly what went wrong. I suppose that is one of the problems with AI or robots.

For example, I want to help me friend locate my keys. There are fundamental things which I have to assume he has knowledge of. First of all being that he is familiar with the layout of things in my apartment, and knowledge of what my keys look like (or what keys are). The issues that can be counted increases as I continue to question the knowledge of the friend listening to my instructions.