首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >利用Jess解决跨河难题

利用Jess解决跨河难题
EN

Stack Overflow用户
提问于 2017-04-29 19:30:19
回答 1查看 326关注 0票数 1

一开始,我解决了一个有无限循环的问题,我通过在约束模块中添加一个规则来修正这个问题。我已经考虑了所有的限制,但似乎所有的事实都因为某种原因被删除了.到目前为止,这是我的代码:

代码语言:javascript
运行
复制
;;MAIN Module
(deftemplate state
    (slot farmer-position)
    (slot fox-position)
    (slot goat-position)
    (slot cabbage-position)
    (slot id)
    (slot prev-state (default nil))
    (multislot move (default nil)))

(deftemplate finished
    (slot value))

(deffacts initial-facts
    (state (farmer-position s1) 
            (fox-position s1) 
            (goat-position s1) 
            (cabbage-position s1)
            (id 0))
    (opp s1 s2)
    (opp s2 s1))

;;CONSTARIN Modle
(defmodule CONSTRAIN)

(defrule CONSTRAIN::fox-goat
    (declare (auto-focus true))
    ?p<-(MAIN::state (fox-position ?f) (goat-position ?f) (farmer-position 
                                                                       ~?f))
    =>
    (retract ?p))

(defrule CONSTRAIN::goat-cabbge
    (declare (auto-focus true))
    ?p<-(MAIN::state (goat-position ?f) (cabbage-position ?f) (farmer-
                                                         position ~?f))
    =>
    (retract ?p))

(defrule CONSTRAIN::no-doubles
    (declare (auto-focus true)) 
    ?p1<-(MAIN::state (farmer-position ?s1) (fox-position ?s2) (goat-
                       position ?s3) (cabbage-position ?s4) (id ?id1))
    ?p2<-(MAIN::state (farmer-position ?s1) (fox-position ?s2) (goat-
          position ?s3) (cabbage-position ?s4) (id ?id2&:(> ?id2 ?id1)))
    =>
    (retract ?p2))

 (defrule CONSTRAIN::stop-exc
    (declare (auto-focus true)) 
    ?p1<-(MAIN::state (farmer-position s2) (fox-position s2) (goat-position 
                                                  s2) (cabbage-position s2))
    =>
    (assert (MAIN::finished (value yes))))

;;MOVE Module
(defmodule MOVE)

(defrule MOVE::move-fox
    ?p<-(MAIN::state (farmer-position ?old) (fox-position ?old) (id ?id))
    (not (MAIN::finished (value yes)))
    (opp ?old ?new)
    =>
    (duplicate ?p (farmer-position ?new)
        (fox-position ?new)
        (prev-state ?p)
        (id (+ ?id 1))
        (move fox ?new)))

(defrule MOVE::move-goat
    ?p<-(MAIN::state (farmer-position ?old) (goat-position ?old) (id ?id))
    (not (MAIN::finished (value yes)))
    (opp ?old ?new)
    => 
    (duplicate ?p (farmer-position ?new)
        (goat-position ?new)
        (prev-state ?p)
        (id (+ ?id 1))
        (move goat ?new)))

(defrule MOVE::move-cabbage
    ?p<-(MAIN::state (farmer-position ?old) (cabbage-position ?old) (id ?
                                                                       id))
    (not (MAIN::finished (value yes)))
    (opp ?old ?new)
    =>
    (duplicate ?p (farmer-position ?new)
       (cabbage-position ?new)
       (prev-state ?p)
       (id (+ ?id 1))
       (move cabbage ?new)))

 ;;RUN
 (reset)
 (watch all)
 (focus MOVE)
 (run)
 (facts)

这是我的输出:

代码语言:javascript
运行
复制
<== Focus MAIN
 ==> Focus MOVE
 FIRE 1 MOVE::move-fox f-1,, f-2
  ==> f-4 (MAIN::state (farmer-position s2) (fox-position s2) (goat-position 
 s1) (cabbage-position s1) (id 1) (prev-state <Fact-1>) (move fox s2))
 ==> Activation: CONSTRAIN::goat-cabbge :  f-4
 ==> Activation: MOVE::move-fox :  f-4,, f-3
 <== Focus MOVE
  ==> Focus CONSTRAIN
 FIRE 2 CONSTRAIN::goat-cabbge f-4
 <== f-4 (MAIN::state (farmer-position s2) (fox-position s2) (goat-position 
 s1) (cabbage-position s1) (id 1) (prev-state <Fact-1>) (move fox s2))
 <== Activation: MOVE::move-fox :  f-4,, f-3
 <== Focus CONSTRAIN
 ==> Focus MOVE
 FIRE 3 MOVE::move-cabbage f-1,, f-2
  ==> f-5 (MAIN::state (farmer-position s2) (fox-position s1) (goat-position 
 s1) (cabbage-position s2) (id 1) (prev-state <Fact-1>) (move cabbage s2))
 ==> Activation: CONSTRAIN::fox-goat :  f-5
 ==> Activation: MOVE::move-cabbage :  f-5,, f-3
 <== Focus MOVE
  ==> Focus CONSTRAIN
 FIRE 4 CONSTRAIN::fox-goat f-5
 <== f-5 (MAIN::state (farmer-position s2) (fox-position s1) (goat-position 
 s1) (cabbage-position s2) (id 1) (prev-state <Fact-1>) (move cabbage s2))
 <== Activation: MOVE::move-cabbage :  f-5,, f-3
 <== Focus CONSTRAIN
 ==> Focus MOVE
FIRE 5 MOVE::move-goat f-1,, f-2
 ==> f-6 (MAIN::state (farmer-position s2) (fox-position s1) (goat-position 
s2) (cabbage-position s1) (id 1) (prev-state <Fact-1>) (move goat s2))
==> Activation: MOVE::move-goat :  f-6,, f-3
FIRE 6 MOVE::move-goat f-6,, f-3
 ==> f-7 (MAIN::state (farmer-position s1) (fox-position s1) (goat-position 
s1) (cabbage-position s1) (id 2) (prev-state <Fact-6>) (move goat s1))
==> Activation: CONSTRAIN::no-doubles :  f-1, f-7
==> Activation: MOVE::move-fox :  f-7,, f-2
==> Activation: MOVE::move-goat :  f-7,, f-2
==> Activation: MOVE::move-cabbage :  f-7,, f-2
<== Focus MOVE
==> Focus CONSTRAIN
FIRE 7 CONSTRAIN::no-doubles f-1, f-7
<== f-7 (MAIN::state (farmer-position s1) (fox-position s1) (goat-position 
s1) (cabbage-position s1) (id 2) (prev-state <Fact-6>) (move goat s1))
 <== Activation: MOVE::move-fox :  f-7,, f-2
 <== Activation: MOVE::move-goat :  f-7,, f-2
 <== Activation: MOVE::move-cabbage :  f-7,, f-2
 <== Focus CONSTRAIN
 ==> Focus MOVE
 <== Focus MOVE
 ==> Focus MAIN
 <== Focus MAIN
 For a total of 0 facts in module MOVE.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-29 20:53:15

事实是f-6没有被收回。唯一有效的第一步是农民和山羊过河,这就是f-6所代表的移动。你没有规则让农民单独过河,所以对农民来说,从f-6移动到河的唯一有效方法是用山羊把你带回到最初的位置。由于f-1所表示的初始位置与f-7相同,规则no-doubles将f-7收缩,并且没有剩余的有效移动。

如果您从http://www.jessrules.com/jess/download.shtml下载Jess,则可以在dilemma.clp文件中的示例目录中找到跨河示例的代码。

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

https://stackoverflow.com/questions/43699673

复制
相关文章

相似问题

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