2013-07-02 87 views
3


我试图找到优化问题中两个阵列的无主导值。试图找到两个阵列的无主导解决方案

我有两个阵列,lost_bars_arrayprices_array。我想要做的是最小化丢失的酒吧,并最大限度地提高价格。我想要得到一个名为no_dominated的最终数组,其中两个数组的非主导值的索引是一样的。这很容易理解。让我用一个例子来解释一下。

我们有inital两个数组:
enter image description here

的理论框架的过程是这样的:

获得的第一个值
enter image description here

2.获取第二个(索引#1),看看它是否比第一个更好。这意味着我们必须看看第二个人是否失去了更少的酒吧和更大的价格。情况并非如此,它有很少的杠杆,但价格也很便宜,所以我们不能说这是一个更好的解决方案。我们把它保存为更多钞票的解决方案
enter image description here

现在到了下一个,索引#2,这一次提高了索引#1的解决方案,因为它具有较少的丢失BRS和更大的代价。但它并没有改善#0号指数,因为它损失较少,而且价格也较低。因此,我们将索引#0和索引#2保存为可能的解决方案。
enter image description here

4.指数#3,#4,有更多的损失吧,少的价格,所以才拒绝这些可能的解决方案。

5.最后,索引#5,具有较少的丢失的条和更多的价格比索引#0,因此,索引#0溶液被拒绝,并最终没有主导的值将是的索引#的那些2和索引#5。 enter image description here

这是我到目前为止尝试过的。

#include <stdio.h> 

int main(int argc, char** argv) 
{ 
    int lost_bars_array[] = {15, 10, 8, 15, 17, 9}; 
    int prices_array[] = {35, 25, 30, 10, 15, 36}; 
    int no_dominated[6]; 
    int cont    = 6; 

    for (int actual_pos = 0; actual_pos < cont; actual_pos++) 
    { 
     if (actual_pos == 0) 
     { 
      no_dominated[actual_pos] = actual_pos; 
     } 
     else 
     { 
      // Here's where I've the problems, I dont know how to handle the arrays 
      for (int p = 0; p < actual_pos; p++) 
      { 
       if (lost_bars_array[p] > lost_bars_array[p + 1] && 
        prices_array[p] < prices_array[p + 1]) 
       { 
        no_dominated[p] = p; 
       } 

      } 
     } 
    } 

    printf("\nThe non-dominated solutions index are: %d"); 
    for (int i = 0; i < 6; i++) 
    { 
     printf(" %d ", no_dominated[i]); 
    } 

    printf("\n"); 

    return 0; 
} 

该代码应输出索引2 5作为解决方案。 我希望我解释正确的问题。
任何帮助表示赞赏。提前致谢。

+0

'问题应该输出2 5作为解决方案。“这使问题成为读者的练习。考虑改写。 – devnull

+0

你对处理数组有什么不了解? –

+1

假设'no_dominated'包含你想要从你的例子中保存的索引,你需要一个单独的计数器来处理'no_dominated'(不是p),只有当找到可能的解决方案时才会增加(在你的例子中,它永远不会如果我改变了'lost_bars_array'和'prices_array',那么这个失败似乎就失败了,因为你最多有3个索引被保存了,所以内部循环只需要遍历这些索引 – Evert

回答

1

下面的程序得到你想要的,但我只测试它为您的具体情况;如果只有一个主导解决方案,或者在其他一些特殊情况下,它可能会失败。但是你也许可以从中解决问题。

有两点需要注意:

  • 你忘了,包括可能的解决方案;布尔型&&将只保存保证更好的解决方案

  • 最后一个双循环有点破解;散列会更容易(解决方案索引将是散列),但我认为这需要C++或额外的库。

代码:

#include <stdio.h> 

int main() 
{ 
    int lost_bars_array[] = {15,10,8,15,17,9}; 
    int prices_array[] = {35,25,30,10,15,36}; 
    int no_dominated[6]; 
    int cont = 6; 
    int ndom = 0; 

    no_dominated[ndom] = 0; 
    ndom++; 
    for (int actual_pos = 1; actual_pos < cont; actual_pos++) { 
     int ntmp = ndom; 
     for(int p = 0; p < ntmp; p++) { 
      if (lost_bars_array[no_dominated[p]] > lost_bars_array[actual_pos] && 
       prices_array[no_dominated[p]] < prices_array[actual_pos]) { 
       // Replace with a better solution 
       no_dominated[p] = actual_pos; 
     } else if (lost_bars_array[no_dominated[p]] > lost_bars_array[actual_pos] || 
       prices_array[no_dominated[p]] < prices_array[actual_pos]) { 
       // Save as potential solution 
       no_dominated[ndom] = actual_pos; 
       ndom++; 
      } 
     } 
    } 
    // Remove double solutions 
    for (int i = 0; i < ndom; i++) { 
     for (int j = i; j < ndom; j++) { 
      if (no_dominated[i] == no_dominated[j]) { 
       ndom--; 
      } 
     } 
    } 
    printf("\nThe non-dominated solutions index are:"); 
    for(int i = 0; i < ndom; i++) 
     printf(" %d ", no_dominated[i]); 
    printf("\n"); 
    return 0; 
} 
+0

是的, int lost_bars_array [10] = {11,17,13,11,12,16,15,20,18,17};和'float prices_array [10] = {11756.30,11956.70,12232.30,12184.70,12184.70,11756.30, 11565.90,11565.90,11470.70,11470.70};' –

1

如果您有n个指标比较(在您的实现N = 2个阵列,数据以最大化/最小化),在你的数据,你可以有最多n(2 )非主导元素。

因此,您可以在每个数组中找到最大/最小元素和索引,这些将成为您的解决方案(如果两个索引相同,只添加其中之一)。

您根本不必担心控制权,no_dominated不必具有您输入的大小。它的大小将介于1和您的度量/数据阵列的数量之间(在示例情况下为1或2)