Without seeing your code, it's hard to say exactly what's best.

I assume when you say "mutating function", you mean "function that  
returns a fresh map representing a new individual".

One option is to make all of your "mutations" go through a single  
utility function, which will make the change and also assoc on a new  
fitness delay.  In the simplest case, this would just be the  
"constructor" for your individuals.  (There's no reason to use  
metadata unless you care about the equality semantics of  
individuals.)  This is the cleanest pure Clojure solution I can think  
of off the top of my head, and the same one I use to solve similar  
problems in my code.

Another solution is to forget about associating fitness directly with  
the individuals, and just create a memoized "get-fitness" function.   
In essence (off the top of my head)

(def get-fitness
   (memoize (fn [individual] (expensive-fitness-calcuation  
individual))))

This, however, will eat up memory since it will hang onto every  
individual it's ever seen.  You could solve this (at the cost of  
purity and thread-safety) by manually memoizing with a  
java.util.WeakHashMap instead.

Cheers,
Jason




On Mar 5, 2009, at 4:47 PM, Dan wrote:

>
> Thanks for the advice, I never noticed delay existed!
>
> It turns out I cannot put the fitness directly into the struct that
> contains individuals. It would mean that every mutating function would
> need to "reset" the fitness computation to avoid propagating a
> misleading fitness and that's clearly not their job. I can put it in
> the metadata and then it will not be carried on when I assoc and
> dissoc things in the struct. However, adding meta returns a new object
> that's the same as the old but with the meta added so I cannot add the
> fitness function to the metadata just before computation because other
> threads holding the data would keep their old objects.
>
> Beside burdening my mutation functions with keeping the fitness up to
> date, I don't see what I can do. Is there something I'm missing?
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to