Let me add my 2 cents here.

The "delay" solution seems like the right one, with one caveat: two
otherwise identical individuals will no longer have the same hash
value or compare as "=".  If you're trying to remove duplicate
individuals or some such, this may be a problem; otherwise, probably
not.

user> (= (delay 2) (delay 2))
false

If this is the case, you probably want to store the delayed fitness
value as metadata on your individual:

(with-meta individual {:fitness (delay ...)})
(force (:fitness ^individual))

If you don't like delay/force, an alternative is to just wrap the
computation of the fitness in a single-element lazy-seq:

(lazy-seq (list (fitness ...))).

The first time you access this lazy-seq, the value will be computed
and then cached for future calls.  An advantage of this approach is
that you don't have to put the lazy-seq in metadata to save equality
semantics, because calling (= ...) or (hash ...) on your individual
will force the seq.  But, for efficiency, you'd probably still want to
store it in metadata ...

Cheers,
Jason



On Mar 4, 12:09 pm, bOR_ <boris.sch...@gmail.com> wrote:
> If you use hash-maps, or a struct-map as the basis of your individual,
> you can just make a key 'fitness', and store the once-calculated
> fitness in there.
>
> I'm not sure how, but it might be possible that at the creation of
> your animals, you assign the key 'fitness' a basic function that upon
> being accessed replaces itself with its outcome. I would be interested
> in seeing that coded. It would be a very neat way to do what you want.
>
> proof of concept, which is not going to win a beaty contest.
>
> user=> (def myhost (hash-map :c 15 :b 3 :a (fn [hostname] (assoc
> hostname :a (* (hostname :b) (hostname :c))))))
> #'user/myhost
> user=> myhost
> {:a #<user$fn__35 user$fn_...@d85cc>, :b 3, :c 15}
> user=> (def myhost ((myhost :a) myhost))
> #'user/myhost
> user=> myhost
> {:a 45, :b 3, :c 15}
> user=>
>
> On Mar 4, 6:27 pm, Meikel Brandmeyer <m...@kotka.de> wrote:
>
> > Hi,
>
> > Am 04.03.2009 um 18:21 schrieb Dan:
>
> > > How can I have my fitness computation be triggered on first access  
> > > and reused afterward?
>
> > Maybe you can use a Delay.
>
> >    {:fitness (delay (compute-my-fitness))}
>
> > And when you access the value use force.
>
> >    (-> my-thing :fitness force)
>
> > The first time it is accessed, the value is calculated.
> > Afterwards the cached value is returned. If you don't
> > use the value of interim stages, it is not calculated.
>
> > Sincerely
> > Meikel
>
> > <commercial>
> > If you use maps as above I also have a lazy-map
> > package, which evaluates the value of key only
> > in case it is really accessed.
>
> >http://bitbucket.org/kotarak/lazymap
> > </commercial>
>
> >  smime.p7s
> > 5KViewDownload
>
>
--~--~---------~--~----~------------~-------~--~----~
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