English 中文(简体)
提供投入(algorithm)的配对规则
原标题:Rules matching given an input (algorithm)

请允许我指出以下几类(具有其可能的价值):

animal: any, cat, dog
color: any, white, black, gray
gender: any, male, female
[...]

或更一般地说,......

category: <array of values>

(1) 让我说,我有一套可预见的规则,例如:

when animal is any, color is gray, gender is male, call x
when animal is dog, color is gray, gender is male, call y
when animal is any, color is any, gender is any, call z
[...]

(2) And some input values.

Q. 是否有算法根据所提供的投入解决寻找匹配规则的问题(优先采用最具体的规则)?

页: 1

input (animal:dog, color:gray, gender:male)

它称为“y”

页: 1

input (color:gray, gender:female)

“z”

这样做的更适当方式是,根据规则(每级树木为一类)建造一个树苗?

如:

- any animal
  - any color
    - any gender => z
  - gray
     - male => x
- dog
  - gray
     - male => y

这样做是否有更好的办法?

感谢!

最佳回答

具有以下修改的决定树将发挥作用:

  1. Create the decision tree in normal way with any as edges using all the rules
  2. 反之,重复价值和任何数值,并跟踪每一解决办法中的任何数字,但至少应将其退回。

    def traverse(values, level, tree,anyCount): If tree is is a leaf: return (appr_func, anyCount)

    v1 = None
    if values[level] in tree:
        v1 = traverse(values, level+1, tree[values[level]]], anyCount)
    
    v2 = None
    if  any  in tree:
        v2 = traverse(values, level+1, tree[ any ], anyCount+1)
    
    if v1!=None:
        if v2!=None:
            if v1[1]<v2[1]:
                return v1
            else:
                return v2
        else:
            return v1
    elif v2!=None:
        return v2
    else:
        return None
    
问题回答

我将把这些规则编成一个地图,然后看上去。

map[hash(animal, color, gender)] = function to call

call map[hash(inputanimal, inputcolor, inputgender)]

这将确保以投入为基础,加快制定规则和解决正确职能。

如果该规则必须完全一致,否则就属于任何通用规则,那么任何规则都是通过:

if map.contains(hash(inAnimal, inColour, inGender)) 
   x = map[hash(inAnimal, inColour, inGender)]
else
   x = map[hash(any, any, any)]

否则,如果从投入开始,并连续选择每个参数的任何规则,那么你就可以这样做。

该散射功能接受一系列价值观。 当你试图遵守规则时,你首先要投入,然后先后向大家转手,直到你找到一个匹配点。

def key hash(array[])

......

A. 决 定

input[] = {inAnimal, inColour, inGender}
function x
for(i = 0 to input.size) {
   if(map.contains(hash(input)) {
      x = map[hash(input)]
      break
   }
   input[i] = any
}
call x

正确的答案取决于你希望怎样获得。 Reteetic。 谷歌“专家系统”。 我在拥有规则评价制度的大公司工作,但他们没有在内部书写,他们购买一揽子商业计划。

如果你的需求简单,那么每一级就有一个搜索树。 如果申请只有具体项目和一个一般的“任何”项目,则将予以罚款。 如果具有多个层次的通用性(“mammal”、“vertebrate”、“animal”),则会变得更加困难。

如果速度是一个问题,但记忆的使用不是问题,那么你也可以尝试表象方法。 单薄的每一条目都是高价值。 在最高层层,关键是“dog”、“cat”、“any”等最高级类别。 价值是另一个可笑的。 在二级表层中,关键是颜色,其价值是另一个 has。 因此。 最深层的表层包括功能点、关闭或主动使用你的节目语言提供的任何方法。

I would first give all variables a unique value (array position)

Animal: any = 0; cat = 1; dog = 2
Gender: any = 0; male = 1; female = 2
Color : any = 0; white = 1; black = 2; gray = 3;

然后,我会给每一种可能的组合带来一种研究价值。

             Animal-|        ANY        |       cat         |       dog         |       
                    ------------------------------------------------------------- 
             Gender-| any | male |female| any | male |female| any | male |female| 
                    ============================================================= 
      Color-any     |  0  |   1  |   2  |  3  |   4  |   5  |  6  |   7  |   8  |
                    ------------------------------------------------------------- 
            white   |  9  |  10  |  11  | 12  |  13  |  14  | 15  |  16  |  17  |
                    ------------------------------------------------------------- 
            black   | 18  |  19  |  20  | 21  |  22  |  23  | 24  |  25  |  26  |   
                    ------------------------------------------------------------- 
            gray    | 27  |  28  |  29  | 30  |  32  |  33  | 34  |  35  |  36  |
                    =============================================================

每一研究值可按以下方法计算:

(Animal * number of animals) + Gender + (Color * number of animals * number of sexes)

or in the case of: animal is any, (0) color is gray, (3) gender is male, (1)

(Animal * number of animals) + Sex + (Color * number of animals * number of sexes)
(   0   *        3         ) +  1  + (  3   *        3          *        3       )  

(0 * 3) + 1 + (3 * 3 * 3)  
   0    + 1 +      27       = 28 (the lookup value from the grid above)

28 means call X.

因此,在您的方案中:

Calculate you value Compare that value against known cases

if Value in (1,2,8,13,14,15,21,28)    then X
if value in (3,4,5,23,24,26,34,35,36) then Y
if value in (0,9,12,16,17,22,25)      then Z

显然,这些价值实际上可以储存在一个汇合档案中,这样,你就可以改变逻辑,而不必再做。

You can nearly directly translate this into scala code.

理论上,你将使用动物、动物、动物、 Dog子、大鼠,但为了与你一样,我在此离开了公约的道路:

abstract sealed class animal () {}
 object cat extends animal () {}
 object dog extends animal {}

abstract sealed class color () {}
 object white extends color {}
 object black extends color {}
 object gray  extends color {}

abstract sealed case class gender () {}
 object male   extends gender {}
 object female extends gender {}

def input (a: Option[animal], c: Option[color], g: Option[gender]) = (a, c, g) match {
  case (Some (dog), Some (gray), Some (male)) => println ("y called without freedom")
  case (_,          Some (gray), Some (male)) => println ("x called with animal" + a)
  case (_,          _,           _          ) => println ("z called with anmimal: " + a + "	color: " + c + "	gender: " + g)
}

input (Some (dog), Some (gray), Some (male))
input (None,       Some (gray), Some (female))

结果:

y called without freedom
x called with animal: None

You have to take care on the sorting in the input -Method. Specific methods have to come before unspecific ones.

还有一些情况,根据您的描述,这是无法判断的,但必须决定,首先检验的是:

(a, c, _)
(_, c, g)
(a, _, g) 

do all have 1 open case. If there is nothing else, (a, c, g) could match any of them, but will only match the first one.

您必须提供一般案件(_、_、_),但如果情况不明确,可能会犯错误。





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签