为python中的试错机制寻求更好的设计建议? [英] seek a better design suggestion for a trial-and-error mechanism in python?

查看:194
本文介绍了为python中的试错机制寻求更好的设计建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

见下面的数据矩阵从传感器获得,只是INT数字,没有任何具体数字。

  ABCDEFGHIJK 
1 25 0 25 66 41 47 40 12 69 76 1
2 17 23 73 97 99 39 84 26 0 44 45
3 34 15 55 4 77 2 96 92 22 18 71
4 85 4 71 99 66 42 28 41 27 39 75
5 65 27 28 95 82 56 23 44 97 42 38
...
10 95 13 4 10 50 78 4 52 51 86 20
11 71 12 32 9 2 41 41 23 31 70
12 54 31 68 78 55 19 56 99 67 34 94
13 47 68 79 66 10 23 67 42 16 11 96
14 25 12 88 45 71 87 53 21 96 34 41

水平A到K是传感器名称,垂直是现在我想用试错法分析这些数据,我定义了一些概念来解释我想要的内容:

p>

源代码是我获得的所有原始数据。 o entry



一个条目是一组所有A到K传感器,例如以垂直第一行为例:条目是

  25 0 25 66 41 47 40 12 69 76 1 


$ b

o规则

一条规则是一个带有assert value return的suppose函数,到目前为止只是真或假。
例如,假设传感器A,E和F的值在一个情况下永远不会相同,如果一个条目的A = E = F,它会触发违规行为,并且此规则函数将返回false。



o范围:

范围是用于选择垂直条目的函数,例如前5项

然后,基本的想法是:

$ $ $ $ $ $ $ $ source + range = subsource(s)$ b $ sub subsource + rules = valiation(s)

最后我想得到一个列表,可能看起来像这样:

  rangeID ruleID违例
1 1 Y
2 1 N
3 1 Y
1 2 N
2 2 N
3 2 Y
1 3 N
2 3 Y
3 3 Y

但是问题是我在这里定义的规则和范围会变得很复杂,如果你看起来很快更深层次,他们有太多可能的组合,例如,可以定义A = E = F,可以定义B = E = F,C = E = F,C> F ......



所以很快我需要一个规则/范围发生器,它可以接受诸如A = E = F这些核心参数作为输入参数,以后再使用正则表达式字符串。这太复杂了,只是打败了我,单独留下我可能需要持久性规则唯一ID,数据存储问题,规则自嵌套组合问题......

所以我的问题是:


  1. 任何人都知道这种试验是否有一些模块/错误计算或我想要的规则定义?

  2. 任何人都可以与我分享我描述的更好的规则/范围设计吗?

  3. >

感谢您提供任何提示。

Rgs,



KC

解决方案

如果我明白你的要求是正确的,我可能不会冒险在Numbpy路径下,因为我不认为你的描述是真正需要的。以下是我如何解决您提交的特定问题的示例实施:

  l = [\ 
'':25,'b':0,'c':25,'d':66,'e':41,'f':47,'g':40,'h':12, 'i':69,'j':76,'k':1},\
{'a':25,'b':0,'c':25,'d':66, 'e':41,'f':47,'g':40,'h':12,'i':69,'j':76,'k':1} \
]
r = ['a = g = i','a = b','a = c']
res = []

#测试所有给定的规则
对于范围内的n(0,len(r)):
#我假设这里是平等的 - 你必须改变这个以接受其他操作符,如果需要的话
c = r [n] .split '=')
vals = []
#建立一个给定我们当前规则的值列表
给c中的e:
vals.append(l [0] [e ])
#使用len(set(v))给我们不同值的数量
res.append({'rangeID':0,'ruleI D':n,'violation':'Y'if len(set(vals))== 1 else'N'})

print res

输出:
$ b

[{ '违规':'N','ruleID':0,'rangeID':0},{'违例':'N','ruleID':1,'rangeID':0},{'violation':'Y ','ruleID':2,'rangeID':0}]



http://ideone.com/zbTZr



这里有一些假设(例如平等是唯一的(例如将规则解析为列表 dict s的列表)我用过,但我希望你可以自己想想。



当然,这可能是基于Numpy的解决方案比现在更简单,我现在只是没有想到(现在已经很晚了,现在我要去睡觉了)),但希望这可以帮助你解决问题。


$ b

编辑:

Woops,错过了其他的东西(忘了在发布之前添加它) - 我只测试 l (给定的范围)中的第一个元素。你只想把它粘在另一个中作为循环,而不是使用硬编码的 0 索引。


See below data matrix get from sensors, just INT numbers, nothing specical.

    A   B   C   D   E   F   G   H   I   J   K
1   25  0   25  66  41  47  40  12  69  76  1
2   17  23  73  97  99  39  84  26  0   44  45
3   34  15  55  4   77  2   96  92  22  18  71
4   85  4   71  99  66  42  28  41  27  39  75
5   65  27  28  95  82  56  23  44  97  42  38
…                                           
10  95  13  4   10  50  78  4   52  51  86  20
11  71  12  32  9   2   41  41  23  31  70  
12  54  31  68  78  55  19  56  99  67  34  94
13  47  68  79  66  10  23  67  42  16  11  96
14  25  12  88  45  71  87  53  21  96  34  41

The horizontal A to K is the sensor name, and vertical is the data from sensor by the timer manner.

Now I want to analysis those data with trial-and-error methods, I defined some concepts to explain what I want:

o source

source is all the raw data I get

o entry

a entry is a set of all A to K sensor, take the vertical 1st row for example: the entry is

25  0   25  66  41  47  40  12  69  76  1

o rules

a rule is a "suppose" function with assert value return, so far just "true" or "false". For example, I suppose the sensor A, E and F value will never be same in one enrty, if one entry with A=E=F, it will tigger violation and this rule function will return false.

o range:

a range is function for selecting vertical entry, for example, the first 5 entries

Then, the basic idea is:

o   source + range = subsource(s)
o   subsource + rules = valiation(s)

The finally I want to get a list may looks like this:

rangeID ruleID violation
1   1   Y
2   1   N
3   1   Y
1   2   N
2   2   N
3   2   Y
1   3   N
2   3   Y
3   3   Y

But the problem is the rule and range I defined here will getting very complicated soon if you looks deeper, they have too much possible combinations, take "A=E=F" for example, one can define "B=E=F","C=E=F","C>F" ......

So soon I need a rule/range generator which may accept those "core parameters" such as "A=E=F" as input parameter even using regex string later. That is too complicated just defeated me, leave alone I may need to persistence rules unique ID, data storage problem, rules self nest combination problem ......

So my questions are:

  1. Anyone knows if there's some module/soft fit for this kind of trial-and-error calculation or the rules defination I want?

  2. Anyone can share me a better rules/range design I described?

Thanks for any hints.

Rgs,

KC

解决方案

If I understand what you're asking correctly, I probably wouldn't even venture down the Numbpy path as I don't think given your description that it's really required. Here's a sample implementation of how I might go about solving the specific issue that you presented:

l = [\
        {'a':25, 'b':0, 'c':25, 'd':66, 'e':41, 'f':47, 'g':40, 'h':12, 'i':69, 'j':76, 'k':1},\
        {'a':25, 'b':0, 'c':25, 'd':66, 'e':41, 'f':47, 'g':40, 'h':12, 'i':69, 'j':76, 'k':1}\
]
r = ['a=g=i', 'a=b', 'a=c']
res = []

# test all given rules
for n in range(0, len(r)):
        # i'm assuming equality here - you'd have to change this to accept other operators if needed
        c = r[n].split('=')
        vals = []
        # build up a list of values given our current rule
        for e in c:
                vals.append(l[0][e])
        # using len(set(v)) gives us the number of distinct values
        res.append({'rangeID': 0, 'ruleID':n, 'violation':'Y' if len(set(vals)) == 1 else 'N'})

print res

Output:

[{'violation': 'N', 'ruleID': 0, 'rangeID': 0}, {'violation': 'N', 'ruleID': 1, 'rangeID': 0}, {'violation': 'Y', 'ruleID': 2, 'rangeID': 0}]

http://ideone.com/zbTZr

There are a few assumptions made here (such as equality being the only operator in use in your rules) and some functionality left out (such as parsing your input to the list of dicts I used, but I'm hopeful that you can figure that out on your own.

Of course, there could be a Numpy-based solution that's simpler than this that I'm just not thinking of at the moment (it's late and I'm going to bed now ;)), but hopefully this helps you out anyway.

Edit:

Woops, missed something else (forgot to add it in prior to posting) - I only test the first element in l (the given range).. You'd just want to stick that in another for loop rather than using that hard-coded 0 index.

这篇关于为python中的试错机制寻求更好的设计建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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