Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Search 试图获得一个深度优先或广度优先的搜索函数_Search_Merge_Scheme_Currying_Depth - Fatal编程技术网

Search 试图获得一个深度优先或广度优先的搜索函数

Search 试图获得一个深度优先或广度优先的搜索函数,search,merge,scheme,currying,depth,Search,Merge,Scheme,Currying,Depth,我是新来的,我需要一个我正在scheme中编写的函数的帮助 基本上,它包含一个搜索功能,可以用于广度优先搜索或深度优先搜索。我想我已经实现了深度优先合并和广度优先合并 但是,问题是修改主搜索以用作“currying函数”,这样当特定于算法的合并过程(如深度优先合并或广度优先合并)作为参数传入时,搜索将使用该特定类型的搜索。回归 我有两个文件。硬币没问题,但搜索需要修复。如何修改此处的搜索功能,使其作为咖喱版 下面是我的代码。第一个是search.ss。作为早期的尝试,我做了一次搜索,但没有成功。

我是新来的,我需要一个我正在scheme中编写的函数的帮助

基本上,它包含一个搜索功能,可以用于广度优先搜索或深度优先搜索。我想我已经实现了深度优先合并和广度优先合并

但是,问题是修改主搜索以用作“currying函数”,这样当特定于算法的合并过程(如深度优先合并或广度优先合并)作为参数传入时,搜索将使用该特定类型的搜索。回归

我有两个文件。硬币没问题,但搜索需要修复。如何修改此处的搜索功能,使其作为咖喱版

下面是我的代码。第一个是search.ss。作为早期的尝试,我做了一次搜索,但没有成功。我需要使search或search2作为当前搜索工作(然后删除另一个)。我不确定,但我认为合并和两个搜索正在工作

;;;
;;; SEARCH:
;;;   -- Non-curried version of generic search algorithm
;;;   -- Can be customized for depth-first and breadth-first search
;;;   -- You must convert it to a curried version so that
;;;      - the function accepts 1 algorithm specific parameter and returns    a function
;;;      - that accepts 3 problem-specific parameters and returns a function
;;;      - that accepths 1 instance specific parameter and performs the search
;;;   -- The 5 parameters are described below
;;;
;;; Input:
;;;   merge-queue
;;;     -- algorithm specific
;;;     -- procedure that takes a list of new paths and a queue
;;;        and returns a new queue
;;;   extend
;;;     -- problem-specific
;;;     -- procedure that takes a state and a list of visited states,
;;;        and returns a list of states that are reachable in one move
;;;        from the given state
;;;   goal?
;;;     -- problem-specific
;;;     -- predicate that takes a state and returns true if the
;;;        state is a goal state, false otherwise
;;;   print-path
;;;     -- problem-specific
;;;     -- procedure that takes a state and prints out a state nicely
;;;   init-state
;;;     -- problem instance-specific
;;;     -- an initial state to start the search from
;;;
;;; OUTPUT:
;;;   -- When succeeded, a path from the initial state to a goal state
;;;   -- When failed, #f
;;;


;;Either this or search2 needs to be rewritten into a curried version
;;To accept either depth-first-merge or breadth-first merge as merge procedures into merge-queue
(define search
  (lambda (merge-queue init-config extend goal?  print-state)
    (letrec
      ((helper
     (lambda (queue)
   (newline)
   (for-each
    (lambda (p) (print-path p print-state))
    queue)
       (cond ((null? queue)  #f)
             ((goal? (caar queue))
      (print-state (caar queue))
      (newline)
      (let ((ans (reverse (car queue))))
        (for-each (lambda (x) (print-state x) (newline)) ans)
        ans))
             (else
              (let ((successors (extend (caar queue))))
        (print-state (caar queue)) (newline)
                (cond ((null? successors)
                       (helper (cdr queue)))
                      (else
           (for-each (lambda (x) (print-state x) (newline))
                 successors)
           (helper
            (merge-queue (cdr queue)
                 (extend-path successors (car queue))))))))))))
  (helper
   (list (list (config->state init-config ))))))



(define search2
  (lambda (merge-queue extend goal? print-path init-state)
(letrec
    ((search-helper
       (lambda (queue visited)
         (cond
           ((null? queue) #f)
           ((goal? (caar queue))
            (begin
              (print-path (car queue))
              (car queue)))
           (else
             (let ((successors (extend (caar queue) visited)))
               (cond
                 ((null? successors)
                  (search-helper (cdr queue) visited))
                 (else
                   (let ((new-paths (extend-path successors (car queue))))
                     (search-helper
          (merge-queue queue new-paths)
          (cond
           (merge-queue))
                       (append successors visited)))))))))))
  (search-helper
    (list (list init-state))   ; initial queue
    (list init-state)))))      ; initial visited


(define extend-path
  (lambda (successors path)
    (if (null? successors)
    '()
    (cons (cons (car successors) path)
      (extend-path (cdr successors) path)))))



;; merge new extended paths to queue for depth first search
;; - uncomment and define your merge for depth first search

(define depth-first-merge
  (lambda (queue paths)
    (append! paths queue)))

;; merge new extended paths to queue for breadth first search
;; - uncomment and define your merge for breadth first search

(define breadth-first-merge
  (lambda (queue paths)
    (append! queue paths)))


;; customize the generic search for depth first search
;; - uncomment and define your depth-first-search in terms of your 
;; curried version of search and depth-first-merge
;; Curry Methods are helpful to this.

(define depth-first-search (search depth-first-merge))
  (lambda (extend goal? print-path)
    (search (depth-first-merge extend goal? print-path))))



;; customize the generic search for breadth first search
;; - uncomment and define your breadth-first-search in terms of your
;;   curried version of search and breadth-first-merge

(define breadth-first-search  (search breadth-first-merge))
  (lambda (extend goal? print-path)
    (search (breadth-first-merge extend goal? print-path))))
这是硬币文件代码,用来补充搜索代码。它们位于不同的文件中,它会加载search.ss(上面的一个)来工作

;;加载特定于算法的搜索代码
(加载“search.ss”)
;;; 解决旧英国硬币问题的特定问题代码
;;; 使用简单搜索过程的当前版本。
;;; 讲座中讨论了旧的英国硬币问题。
;;;
;;; 要解决此问题,请加载此文件并运行
;;;    (硬币深度优先)
;;; 或
;;;    (硬币宽度优先)
;;; 其中,金额替换为某个数字,例如48。
;;;
;;; 在此,状态表示如下:
;;;       (金额(硬币1硬币2…)
;;;
;;; 州政府的汽车代表了你需要进一步支付多少零钱。
;;; 该州的cadr代表您使用的硬币组合
;;; 到目前为止还没有支付。例如
;;;       (48 ())
;;; 是初始状态,金额为48美分,并且
;;;       (0 (24 24)
可以是使用两枚24美分硬币的目标州之一。
有7种古老的英国硬币
(定义旧英国硬币(120 30 24 12 6 3 1))
或者,你也可以为我们的硬币做同样的事情
(定义美国硬币(100 50 25 10 5 1))
在这里,我们将制作旧的英国硬币
(定义*硬币*旧英国硬币)
状态是目标状态吗?
(定义目标?
(lambda(州)
(零?(汽车状态)))
返回一个州的孩子
(定义扩展)
(lambda(国家访问)
(let((硬币(适用硬币州*硬币*))
(地图
(兰姆达(硬币)
(列表(-(汽车州)硬币)
(附加(cadr州)(列表硬币)))
(硬币)
;从一个州找到所有适用的硬币
(定义适用的硬币)
(lambda(国家访问货币)
(续)
((空?硬币)))

((对函数进行curry是指重新定义函数,使其接受的参数数量小于当前定义,并返回一个新函数,该函数接受其余参数并执行第一个参数的工作。例如,您可以curry以下两个参数求和函数:

(define add 
  (lambda (a b)
    (+ a b)))

(add 7 10)  ;; => 17
以以下方式:

(define add-to
  (lambda (a)
    (lambda (b)
      (+ a b))))

((add-to 7) 10)  ;; => 17

(define add-to-7 (add-to 7))  ;; we give a name to the function that add 7 to its argument

(add-to-7 8)  ;; => 15

(add-to-7 9)  ;; => 16
因此,要转换
search2
函数(必须扩展该函数,因为它的最后一个参数是特定于问题实例的参数):

根据需要,您可以简单地编写如下内容:

(define search2
  (lambda (merge-queue)
    (lambda (extend goal? print-path)
      (lambda (init-state)
        ...body of search2...
然后,使用正确数量的参数调用它,您可以获得稍后要调用的“部分”函数。例如,您可以将通用深度优先搜索定义为:

(define depth-first-search (search2 depth-first-merge))
然后,您可以定义硬币问题专用的深度优先搜索,给出硬币功能的适当定义:

(define coin-depth-first (depth-first-search coin-extend coin-goal? coin-print-path))
最后,你可以用一定的数量来解决这个问题:

(coin-depth-first 100)

好的,现在我修复了这个问题,但现在我尝试它时会说,“异常,变量Print Path没有绑定,无论何时我尝试它。函数中有Print state,状态中有Print Path。请检查是否始终使用相同的名称。
(define coin-depth-first (depth-first-search coin-extend coin-goal? coin-print-path))
(coin-depth-first 100)