[Cs254f11] Another q

Lee Spector lspector at hampshire.edu
Sun Oct 9 21:44:40 EDT 2011


Hi Jamie,

There are a couple of different problems in evalrepeat, which I've re-formated for clarity as:

(defn evalrepeat
  "This will find error in data run runnum times."
  [runnum]
  (let [error 0 i 0] ;establish variables for error and a counter
    (repeatedly 
      runnum
      (let [reset! x (get-in data [i 0])
            reset! y (get-in data [i 1])]
        (inc error 
             (expt (- @y 
                      (eval (random-arithmetic x))) 2))
        (inc i)))
    error))

Some of the problems:

- The second argument to repeatedly should be a function; more specifically a function that takes zero arguments. You have a big expression there, but unless the expression returns a function (which it won't, even if the bugs within it are fixed) then this won't work. Normally the second argument you pass to repeatedly would be a function that you've defined with defn or a (fn [] ...) expression or a #(...) expression. I suspect that you don't actually want to do a "repeatedly" here at all, but rather a loop defined with loop/recur or something like that.

- The brackets in a "let" expression should contain a variable name, then an expression that will be evaluated to give the variable its value, then (optionally) another variable name and value-producing expression, etc. You seem to be giving it the variable named "reset!", giving it the value of x, then giving as the second variable (get-in data [i 0]), etc. The specific error message you are getting is the complaint that (get-in data [i 0]) isn't a good variable name for a "let" binding.

- You call inc twice but you don't do anything with the values that it returns. Remember that inc, like most functions, does not change the arguments that you give it -- it just returns a new value. You may want those new values being passed, via recur, up to the binding forms at the top of a loop instead.

- One of your calls to inc takes two arguments, but inc can only take one. You might mean "+" instead.

This is a separate issue, but it looks like you will be getting errors from random arithmetic expressions, but not keeping track of the expressions themselves -- which would be necessary for the better ones to be used as parents to make children, etc. Normally what one would do is to generate the random expressions and then store them somewhere before evaluating them. So for example you might create a vector of random expressions, but it in a local variable you create with "let", then map a function down that vector and evaluate the fitness of each, storing all of the fitnesses in another local variable, use the fitnesses to select a subset of the expressions to mutate, etc.

 -Lee



On Oct 9, 2011, at 9:24 PM, James Matheson wrote:

> So thanks for the reply Omri it was very helpful.
> 
> Another question: here I'm creating a number of arithmetic equations,
> eval-ing them, and finding the error. I'm using atoms to store data
> from a vector. When I go to run, however, I get
> #<CompilerException java.lang.Exception: Unsupported binding form:
> (get-in data [i 0]) (NO_SOURCE_FILE:2137)>
> Anyone know why this might be going on? Also, if I have any obvious
> bugs in the code, please let me know!
> 
> N.B. The function I'm working on is at the end, the other parts of the
> code are used within it.
> 
> ;solution for div by zero
> (defn div-none
>  "This returns 0 if division is called with a second argument of 0."
>  [x y]
>  (if (= y 0)
>    0
>    (/ x y)))
> 
> ;globals
> (def x (atom 0)) ;; an atom set to zero
> (def y (atom 0)) ;; an atom set to zero
> (def data [[0 0], [1 1], [2 2], [3 3]])
> 
> ;overloaded generator for arithmetics
> (defn random-arithmetic
>  "Produces, prints, and evals a random arithmetic expression on an atom."
>  []
>  (reset! x (rand-int 10)) ;;set our atoms to a rand
>  (reset! y (rand-int 10))
>  (eval '((rand-nth (list + * - div-none)) @x @y))) ;;eval with a rand function
> 
> (defn random-arithmetic
>  "Produces, prints, and evals a random arithmetic expression on two
> passed values."
>  [a b]
>  (reset! x a) ;;set our atoms to given values
>  (reset! y b)
>  (eval '((rand-nth (list + * - div-none)) @x @y))) ;;eval with a rand function
> 
> (defn random-arithmetic
>  "Produces, prints, and evals a random arithmetic expression on two
> passed values."
>  [a]
>  (reset! x a)
>  (eval '((rand-nth (list + * - div-none)) @x (rand-int 10)))) ;;eval
> with a rand function
> 
> ;main function
> (defn evalrepeat
>  "This will find error in data run runnum times."
>  [runnum]
>  (let [error 0 i 0] ;establish variables for error and a counter
>    (repeatedly runnum
>                (let [reset! x (get-in data [i 0]) reset! y (get-in
> data [i 1])] ;set x, y
>                  (inc error (expt (- @y (eval (random-arithmetic
> @x))) 2)) ;get error
>                  (inc i)))
>    error))
> _______________________________________________
> Cs254f11 mailing list
> Cs254f11 at lists.hampshire.edu
> https://lists.hampshire.edu/mailman/listinfo/cs254f11

--
Lee Spector, Professor of Computer Science
Cognitive Science, Hampshire College
893 West Street, Amherst, MA 01002-3359
lspector at hampshire.edu, http://hampshire.edu/lspector/
Phone: 413-559-5352, Fax: 413-559-5438



More information about the Cs254f11 mailing list