首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不能将clojure.lang.LazySeq转换为clojure.lang.Associative类

不能将clojure.lang.LazySeq转换为clojure.lang.Associative类
EN

Stack Overflow用户
提问于 2021-11-28 09:13:45
回答 2查看 280关注 0票数 0

我对Clojure很陌生,我试着实现一种遗传算法。因此,我遇到了一个问题,实现一直抛出以下错误:

代码语言:javascript
复制
class clojure.lang.LazySeq cannot be cast to class clojure.lang.Associative (clojure.lang.LazySeq and clojure.lang.Associative are in unnamed module 
of loader 'app')

需要指出的是,每个函数都是在REPL中单独测试的,并返回正确的结果,但是在将它们放在一起之后,会抛出错误,而且我不理解它,因为它似乎没有指定行号。

clojure的版本是来自主分支的版本,是在Windows上用maven构建的。

完整的代码:

代码语言:javascript
复制
(ns ga)

(defn new-individual
    [genome-length]
    {:genome (vec (repeatedly genome-length #(rand-int 2))) :fitness 0}
    )


(defn fitness-function
    [genome, target]
    (Math/abs (- (reduce + genome) target))
)




(defn calculate-fitness
    [population, target]
    (defn fitness-function-helper 
        [individual, target]
        (assoc individual :fitness (fitness-function (individual :genome) target))
    )
        (map (fn [individual] (#(fitness-function-helper individual target))) population)
)



(defn crossover
        [first-individual, second-individual, crossover-rate, target]
        (let [new-genome (map (fn [i1,i2] (let [crossover-probability (rand)]
                                        (cond
                                            (<= crossover-probability crossover-rate) i1
                                            :else i2
                                        )
                                    )
                            ) 
            (first-individual :genome) (second-individual :genome)
                )]
            {:genome new-genome :fitness (fitness-function new-genome target)}
        )
        
)
        


(defn mutate
  [individual, genome-length, target]
  (let [new-genome (assoc (individual :genome) (rand-int genome-length) (rand-int 2))]
    {:genome new-genome :fitness (fitness-function new-genome target)}
  )
)



(defn better
  [i1 i2]
  (< (i1 :fitness) (i2 :fitness)))

(defn tournament-selection
  [population, population-size, steps, tournament-size, new-population, target]
  (if (< steps tournament-size)
    (recur population population-size (inc steps) tournament-size (conj new-population (nth population ((comp rand-int -) population-size 2))) target
      
     )
        ;(println new-population)
        (first (sort better (calculate-fitness new-population target)))
        
    )
)

(defn new-generation [population, population-size, crossover-rate, target, tournament-size]
    (loop [steps 0 new-population ()]
        (if (< steps population-size)
        (let [i1 (tournament-selection population population-size 0 tournament-size () target)]
            (let [i2 (tournament-selection population population-size 0 tournament-size () target)]
                (let [offs (crossover i1 i2 crossover-rate target)]
                    (recur (inc steps) (conj new-population offs))
                )
            )
        )
        new-population
    )
        )
    )
    
    (defn new-mutated-generation [population, population-size, genome-length, target]
        (loop [steps 0 new-population ()]
            (if (< steps population-size)
                    (recur (inc steps) (conj new-population (mutate (nth population steps) genome-length target)))
                    new-population
            )

        )

    )
        

(defn evolve [population-size, genome-length, target]
(let [population (calculate-fitness (repeatedly population-size #(new-individual genome-length)) target)]
    (let [offsprings (new-generation population population-size 0.5 target 5)]
        (println (new-mutated-generation offsprings population-size genome-length target))
        )
    


)

)

(evolve 10 5 5)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-28 10:09:09

堆栈跟踪显示,有问题的代码是行

代码语言:javascript
复制
(let [new-genome (assoc (vec (individual :genome)) (rand-int genome-length) (rand-int 2))]

更具体地说,对assoc的调用。如果我们在上面插入以下行来编辑代码:

代码语言:javascript
复制
(println "Individual: " (individual :genome) ", " (class (individual :genome)))

印出来

代码语言:javascript
复制
Individual:  (0 1 1 0 1) ,  clojure.lang.LazySeq

问题是assoc不能与惰性序列(clojure.lang.LazySeq)一起使用,因为它没有实现阿索克需要的clojure.lang.Associative接口。

这个延迟序列是通过对该行上的map的调用来构造的:

代码语言:javascript
复制
(let [new-genome (map (fn [i1,i2] (let [crossover-probability (rand)]

如果将地图替换为马普夫,则代码如下所示

代码语言:javascript
复制
(let [new-genome (mapv (fn [i1,i2] (let [crossover-probability (rand)]

密码会有效的。

票数 2
EN

Stack Overflow用户

发布于 2021-11-28 10:08:02

错误发生在函数mutate中。它有以下消息来源:

代码语言:javascript
复制
(defn mutate
  [individual, genome-length, target]
  (let [new-genome (assoc (individual :genome) (rand-int genome-length) (rand-int 2))]
    {:genome new-genome :fitness (fitness-function new-genome target)}))

在一个步骤中,您使用以下参数调用它:{:genome (0 1 1 1 1), :fitness 1} 5 5 (基因组可能有不同的价值,但它总是数字序列)。

(individual :genome)返回(0 1 1 1 1) (序列),然后使用assoc,它是哈希映射或向量的函数。

基因组最初是载体,但在crossover函数中被转换成序列--在这里使用mapv而不是map

代码语言:javascript
复制
(defn crossover
  [first-individual, second-individual, crossover-rate, target]
  (let [new-genome (mapv (fn [i1, i2] (let [crossover-probability (rand)]
                                        (if (<= crossover-probability crossover-rate) i1 i2)))
                         (first-individual :genome) (second-individual :genome))]
    {:genome new-genome :fitness (fitness-function new-genome target)}))

顺便说一下,定义末尾的所有括号都属于同一行。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70142000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档