2012-02-07 114 views
0

團隊:加權排列組合算法

我建立一個業務規則引擎,情境感知 - 但它的權重 - 或者換句話說,每一個業務規則都有一個關鍵的部分定義的粒度級別。段不是組合的,因爲它們不能以任何順序加權,而是像組合鎖一樣(有趣的是,不恰當地命名但被廣泛接受)。

然而,減少代碼,有必要提供的業務規則,我們只建立排除文件意味着每一段可以與特定鍵值或都最終量。

所以,現在我們有一個抽象的背景,讓我們舉一個具體的例子。該定義的段如下:

  1. 業務線(LOB)
  2. 公司
  3. 國家

現在,讓我們假設在這個例子中,該LOB是ABC,公司是G和國家是WY。如果你打破下來我應該得到如下排列:

  • ABC_G_WY
  • ABC_G_ALL
  • ABC_ALL_WY
  • ABC_ALL_ALL
  • ALL_G_WY
  • ALL_G_ALL
  • ALL_ALL_WY
  • ALL_ALL_ALL

不過,我需要一個算法來解決這個問題。該段還必須在上述順序返回,因爲你必須先找到最有限的規則

我期待着您的回覆,謝謝大家提前!

+0

有沒有永遠只三段?另外,你可以發佈你的任何內容,到目前爲止,讓我們知道你卡在哪裏? – 2012-02-07 16:28:51

+1

您不應該將您的團隊負責人發送的電子郵件直接複製到SO中。如果他們也讀了呢? – 2012-02-07 16:43:09

+0

@AntonTykhyy - 我想我不明白你在說什麼。這不是我的團隊領導。 – 2012-02-07 16:46:44

回答

1
public static void Main(string[] args) 
{ 
    List<string> inputValues = new List<string>() { "ABC", "G", "WY" }; 
    List<string> results = new List<string>(); 

    int permutations = (int)Math.Pow(2.0, (double)inputValues.Count); 

    for (int i = 0; i < permutations; i++) 
    { 
     int mask = 1; 
     Stack<string> lineValues = new Stack<string>(); 
     for (int j = inputValues.Count-1; j >= 0; j--, mask <<= 1) 
     { 
      if ((i & mask) == 0) 
      { 
       lineValues.Push(inputValues[j]); 
      } 
      else 
      { 
       lineValues.Push("ALL"); 
      } 
     } 
     results.Add(string.Join("_", lineValues.ToArray())); //ToArray can go away in 4.0(?) I've been told. I'm still on 3.5 
    } 

    foreach (string s in results) 
    { 
     Console.WriteLine(s); 
    } 

    Console.WriteLine("Press any key to exit..."); 
    Console.ReadKey(true); 
} 
+0

固定訂貨 – Servy 2012-02-07 16:42:42

+0

我會+1你對這個答案,但我不能,因爲我沒有足夠的積分呢。只要我能,我會。 – 2012-02-15 20:55:47

0

如果我有問題的權利,你應該:

-Generate all binary strings of length N (there will be 2^N of them) 
-sort them by number of bits set 
-generate rules. Rule has 'ALL' in position i, if bit number i in the corresponding binary string is set