CLIPS 巨大的内存使用量 [英] CLIPS huge memory usage

查看:75
本文介绍了CLIPS 巨大的内存使用量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用剪辑框架来构建专家系统.但是,我在内存使用 atm 方面遇到了问题,这将使它不适合我的任务.所以问题来了:

I'm using the clips framework to build an expert system. However I'm having trouble with memory usage atm, which would make it unsuitable for my task. So here is the problem:

它按预期生成了 144 个 SpinWave 事实,总共大约 150 个事实.每个事实不应该包含超过一堆整数(大约 15 个).CLIPS 消耗 1GB 的内存,发出大约 6mio 内存请求.我有点困惑为什么它分配那么多内存......有人可以指出我正确的方向或给出解释.我正在使用的代码如下.提前致谢!

It generates 144 of the SpinWave facts as its supposed to, with a total of around 150 facts. Each fact should not hold more than a bunch of integers (roughly 15). CLIPS consumes 1GB of memory with roughly 6mio memory requests made. I'm kind of baffled why it allocates that much memory... Can someone point me into the right direction or give an explanation. The code I'm using is below. Thanks in advance!

史蒂夫

; define helicity wave final or initial state template
(deftemplate SpinWaveMultiplet
    (slot unique_id (type INTEGER))
    (slot charge (type INTEGER))
    (slot isospin_num (type INTEGER))
    (slot isospin_denom (type INTEGER))
    (slot isospin_z_num (type INTEGER))
    (slot spin_num (type INTEGER))
    (slot spin_denom (type INTEGER))
    ; we have multislot of spin z to allow for specific components
    ; in the initial or final state
    (multislot spin_z_num)
    (slot parity (type INTEGER))
    (slot cparity (type INTEGER))
)

; define spin wave  
(deftemplate SpinWave
    (slot unique_id (type INTEGER))
    (slot charge (type INTEGER))
    (slot isospin_num (type INTEGER))
    (slot isospin_denom (type INTEGER))
    (slot isospin_z_num (type INTEGER))
    (slot spin_num (type INTEGER))
    (slot spin_denom (type INTEGER))
    (slot spin_z_num (type INTEGER))
    (slot parity (type INTEGER))
    (slot cparity (type INTEGER))
)

; allowed intermediate state spins
(deffacts user-conditions
    (AllowedQN
        (spin_nums 0 1 2) (spin_denom 1 ) (isospin_nums 0 1) (isospin_denom 1)
        (charge 0) (parity -1 1) (cparity -1 1)
    )
)


(deffacts initial-state
    (SpinWaveMultiplet (unique_id 0) (spin_num 1) (spin_denom 1) (spin_z_num -1 1)
        (isospin_num 0) (isospin_denom 1) (isospin_z_num 0)
    )
)

(deffacts final-state-list
    (SpinWaveMultiplet (unique_id 1) (spin_num 1) (spin_denom 1) (spin_z_num -1 1)
        (isospin_num 0) (isospin_denom 1) (isospin_z_num 0)
    )
    (SpinWaveMultiplet (unique_id 2) (spin_num 0) (spin_denom 1) (spin_z_num 0)
        (isospin_num 1) (isospin_denom 1) (isospin_z_num 0)
)
    (SpinWaveMultiplet (unique_id 3) (spin_num 0) (spin_denom 1) (spin_z_num 0)
        (isospin_num 1) (isospin_denom 1) (isospin_z_num 0)
    )
)

; create all spin waves
(defrule create-initial-spin-waves
    (AllowedQN
        (spin_nums $?spin_nums) (spin_denom ?spin_denom) 
        (isospin_nums $?isospin_nums) (isospin_denom ?isospin_denom)
        (charge $?charges)
        (parity $?parities)
        (cparity $?cparities)
    )
    =>
    (foreach ?charge ?charges
    (foreach ?parity ?parities
    (foreach ?cparity ?cparities

    (foreach ?isospin_num ?isospin_nums
        (bind ?isospin_z_num (* -1 ?isospin_num))
        (while (<= ?isospin_z_num ?isospin_num)
            (foreach ?spin_num ?spin_nums
                (bind ?spin_z_num (* -1 ?spin_num))
                (while (<= ?spin_z_num ?spin_num) 
                    (assert
                        (SpinWave (unique_id ?*total_unique_id_counter*) 
                        (spin_num ?spin_num) (spin_denom ?spin_denom) (spin_z_num ?spin_z_num)
                        (isospin_num ?isospin_num) (isospin_denom ?isospin_denom) (isospin_z_num ?isospin_z_num)
                        (charge ?charge) (parity ?parity) (cparity ?cparity)
                        )
                    )
                    (bind ?*total_unique_id_counter* (+ ?*total_unique_id_counter* 1))
                    (bind ?spin_z_num (+ ?spin_z_num ?spin_denom))
                )
            )
            (bind ?isospin_z_num (+ ?isospin_z_num ?isospin_denom))
        )
    )

    )
    )
    )
)

推荐答案

您发布的代码片段不会占用大量内存(小于 17 MB).CLIPS 使用 rete 算法保存规则部分匹配的状态,因此您可能有一个或多个规则匹配生成大量部分匹配的 SpinWave 事实.

The code fragment you posted doesn't consume a significant amount of memory (less than 17 MB). CLIPS uses the rete algorithm which saves the state of partial matches of rules, so it's likely that you have one or more rules matching the SpinWave facts that generate a large number of partial matches.

您可以使用matches 命令获取每个规则使用的部分匹配数的快照,并使用join-activity 命令获取可能存在性能问题的规则的整体快照.

You can use the matches command to get a snapshot of the number of partial matches used by each rule and the join-activity command for an overall snapshot of rules that might have performance issues.

例如,在运行行为基准测试后,我可以使用这些命令看到 find_seating 和 make_path 规则是我应该检查性能问题的规则.

For example, after running the manners benchmarks I can see using these commands that the find_seating and make_path rules are the ones I should check for performance issues.

CLIPS> (batch "manners128.bat")
TRUE
CLIPS> (clear)
CLIPS> (unwatch compilations)
CLIPS> (watch statistics)
CLIPS> (set-strategy depth)
depth
CLIPS> (load manners.clp)
:%%%%%%%********
TRUE
CLIPS> (reset)
CLIPS> (load-facts manners128.fct)
TRUE
CLIPS> (run)
8639 rules fired        Run time is 2.39572899999621 seconds.
3606.00051174973 rules per second.
4762 mean number of facts (8953 maximum).
1 mean number of instances (1 maximum).
138 mean number of activations (9490 maximum).
CLIPS> 
(progn$ 
   (?r (get-defrule-list)) 
   (printout t ?r ": " (join-activity ?r terse) crlf))
assign_first_seat: (1314 1315 1315)
find_seating: (5847660 7067211 7067211)
make_path: (49549 16510 16510)
path_done: (127 254 254)
are_we_done: (128 255 255)
continue: (0 127 127)
print_results: (257 258 128)
all_done: (0 1 0)
CLIPS> 
(progn$ 
   (?r (get-defrule-list)) 
   (printout t ?r ": " (matches ?r terse) crlf))
assign_first_seat: (439 0 0)
find_seating: (9260 0 0)
make_path: (16256 0 0)
path_done: (0 0 0)
are_we_done: (129 0 0)
continue: (0 0 0)
print_results: (8258 129 0)
all_done: (1 0 0)
CLIPS> 

根据您的评论,以下是您提到的导致问题的规则之一:

Following up from your commment, here's one of the rule you mention causing the problem:

(defrule check-charge 
   ?mymother <- (SpinWave (charge ?charge_mother)) 
   ?mydaughter1 <- (SpinWave (charge ?charge_daughter1)) 
   ?mydaughter2 <- (SpinWave (charge ?charge_daughter2)) 
   =>)

没有任何模式具有限制与其他模式匹配的事实数量的条件,因此基本上该规则匹配三个 SpinWave 事实的所有组合.由于有 144 个事实与这三种模式中的每一种匹配,因此该规则将有 2,985,984 (144 * 144 * 144) 次激活.因此,即使 CLIPS 没有为前两个模式生成部分匹配,仍然会有数百万个激活消耗内存,这些内存最终会在每个规则激活被允许执行时被释放.

None of the patterns have conditions which restrict the number of facts that will match the other patterns, so essentially this rules matches all combinations of three SpinWave facts. Since there are 144 facts matching each of the three patterns, there will be 2,985,984 (144 * 144 * 144) activations of the rule. So even if CLIPS were not generating partial matches for the first two patterns, there would still be millions of activations consuming memory that would eventually be released as each rule activation is allowed to execute.

在不知道规则应该做什么或 SpinWave 事实之间的关系的情况下,很难具体说明如何使规则更有效,但通常您希望模式在与它们匹配的事实数量上受到限制通过前面模式中的变量绑定.

Without knowing what the rule is supposed to do or the relationship between the SpinWave facts it's difficult to be specific on how to make the rule more efficient, but generally you want patterns to be restricted in the number of facts that will match them by the variable bindings from the preceding patterns.

因此,如果事实之间存在父/子关系,您可以为 SpinWave 事实添加并填充父槽,并使用它来减少生成的部分匹配/激活的数量:

So if there were a parent/child relationship between the facts, you can add and populate a parent slot for the SpinWave facts and use that to reduce the number of partial matches/activations generated:

(defrule check-charge 
   ?mymother <- (SpinWave (charge ?charge_mother) (unique_id ?id)) 
   ?mydaughter1 <- (SpinWave (charge ?charge_daughter1) (parent ?id)) 
   ?mydaughter2 <- (SpinWave (charge ?charge_daughter2) (parent ?id)) 
   =>)

这篇关于CLIPS 巨大的内存使用量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆