Mathematica:Thread :: tdlen:{Null} {}中长度不等的对象不能组合。 >>

时间:2011-09-22 10:56:02

标签: wolfram-mathematica

我有问题:

Thread :: tdlen:{Null} {}中长度不等的对象无法组合。 >>

这似乎发生在while测试中,因为我在比较数字时没有任何意义......?

该程序是一个解决0-1背包动态编程问题的程序,虽然我使用循环,而不是递归。

我已经放了一些打印输出,我只能认为问题出在while循环中并且没有意义。

(*  0-1 Knapsack problem
 item = {value, weight} 
  Constraint is maxweight. Objective is to max value. 
 Input on the form:
 Matrix[{value,weight},
       {value,weight},
        ...
      ]
*)

lookup[x_, y_, m_] := m[[x, y]];

generateTable[items_, maxweight_] := {
  nbrofitems = Dimensions[items][[1]];
  keep = values = Table[0, {j, 0, nbrofitems}, {i, 1, maxweight}];
  For[j = 2, j <= nbrofitems + 1, j++,
   itemweight = items[[j - 1, 2]];
   itemvalue = items[[j - 1, 1]];
   For[i = 1, i <= maxweight, i++,
    {
     x = lookup[j - 1, i, values];
     diff = i - itemweight;
     If[diff > 0, y = lookup[j - 1, diff, values], y = 0];
     If[itemweight <= i ,
      {If[x < itemvalue + y,
        {values[[j, i]] = itemvalue + y; keep[[j, i]] = 1;},
        {values[[j, i]] = x; keep[[j, i]] = 0;}]
       },
      y(*y eller x?*)]
     }
    ]
   ];
  {values, keep}
  }

pickItems[keep_, items_, maxweight_] :=
 {
  (*w=remaining weight in knapsack*)
  (*i=current item*)
  w = maxweight;
  knapsack = {};
  nbrofitems = Dimensions[items][[1]];
  i = nbrofitems + 1;
  x = 0;
  While[i > 0 && x < 10,
   {
      Print["lopp round starting"];
      x++;
      Print["i"];
      Print[i];
      Print["w"];
      Print[w];
      Print["keep[i,w]"];
      Print[keep[[i, w]]];
      If[keep[[i, w]] == 1,
       {Append[knapsack, i];
        Print["tjolahej"];
        w -= items[[i - 1, 2]];
        i -= 1;
        Print["tjolahopp"];
        },
       i -= 1;
       ];
      Print[i];
      Print["loop round done"];
      }
     knapsack;
   ]
  }

Clear[keep, v, a, b, c]
maxweight = 5;
nbrofitems = 3;
a = {5, 3};
b = {3, 2};
c = {4, 1};
items = {a, b, c};


MatrixForm[items]
results = generateTable[items, 5];
keep = results[[1]][[2]];
Print["keep:"];
MatrixForm[keep]
Print["------"];
results2 = pickItems[keep, items, 5];
MatrixForm[results2]

2 个答案:

答案 0 :(得分:1)

这不是对所询问的具体问题的真正答案,而是对发生此错误时的一般情况的一些提示。简短的回答是,这是将不等长度列表传递给某些Listable函数,用户定义或内置函数的标志。

Mathematica的许多内置函数都是Listable(具有Listable属性)。这基本上意味着,给定列表代替一些或所有参数,Mathematica会自动将函数与它们进行线程化。真正发生的是内部调用Thread(或者至少,它出现)。

可以说明这一点
In[15]:= 
ClearAll[f];
SetAttributes[f,Listable];
f[{1,2},{3,4,5}]

During evaluation of In[15]:= Thread::tdlen: Objects of unequal length in 
f[{1,2},{3,4,5}] cannot be combined. >>
Out[17]= f[{1,2},{3,4,5}]

明确使用Thread可以获得相同的行为:

In[19]:= 
ClearAll[ff];
Thread[ff[{1,2},{3,4,5}]]

During evaluation of In[19]:= Thread::tdlen: Objects of unequal length in 
ff[{1,2},{3,4,5}] cannot be combined. >>
Out[20]= ff[{1,2},{3,4,5}]

Listable函数的情况下,这有点隐藏了。一些典型示例包括{1, 2} + {3, 4, 5}{1, 2}^{3, 4, 5}等内容。我更详细地讨论了这个问题here

答案 1 :(得分:0)

试试这个版本:

pickItems[keep_, items_, maxweight_] := Module[{},
  {(*w=remaining weight in knapsack*)(*i=current item*)w = maxweight;
   knapsack = {};
   nbrofitems = Dimensions[items][[1]];
   i = nbrofitems + 1;
   x = 0;
   While[i > 0 && x < 10,
    {
     Print["lopp round starting"];
     x++;
     Print["i"];
     Print[i];
     Print["w"];
     Print[w];
     Print["keep[i,w]"];
     Print[keep[[i, w]]];

     If[keep[[i, w]] == 1,
      {
       Append[knapsack, i];
       Print["tjolahej"];
       w -= items[[i - 1, 2]];
       i -= 1;
       Print["tjolahopp"];
       },
      i -= 1;
      ];

     Print[i];
     Print["loop round done"]
     };
     knapsack
    ]
   }
  ]

现在没有错误,但我不知道它到底做了什么:)